Skip to contentSkip to conversation

Fix IPv4 default gateway with multiple route tables

PATCH Superseded Vladislav Grishenko 3 participants 15 messages Apr 13, 2021 – Apr 18, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Fix IPv4 default gateway with multiple route tables

Current default gateway selection for zero destignation address just dumps and parses all the routing tables. If any of non-main table with default route comes first, wrong default gateway can be picked. Since adding/removing routes currently handles only main table, let's stick to RT_TABLE_MAIN while selecting default route too.

---
 src/openvpn/networking_sitnl.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
src/openvpn/networking_sitnl.c +17 -1
index 2bc70a50..56543648 100644
@@ -426,6 +426,7 @@ typedef struct {
     inet_address_t gw;
     char iface[IFNAMSIZ];
     bool default_only;
+    unsigned int table;
 } route_res_t;
 
 static int
@@ -435,7 +436,7 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
     struct rtmsg *r = NLMSG_DATA(n);
     struct rtattr *rta = RTM_RTA(r);
     int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
-    unsigned int ifindex = 0;
+    unsigned int table, ifindex = 0;
 
     /* filter-out non-zero dst prefixes */
     if (res->default_only && r->rtm_dst_len != 0)
@@ -443,6 +444,9 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
         return 1;
     }
 
+    /* route table, ignored with RTA_TABLE */
+    table = r->rtm_table;
+
     while (RTA_OK(rta, len))
     {
         switch (rta->rta_type)
@@ -460,11 +464,22 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
             case RTA_GATEWAY:
                 memcpy(&res->gw, RTA_DATA(rta), res->addr_size);
                 break;
+
+            /* route table */
+            case RTA_TABLE:
+                table = *(unsigned int *)RTA_DATA(rta);
+                break;
         }
 
         rta = RTA_NEXT(rta, len);
     }
 
+    /* filter-out zero dns prefixes from other tables */
+    if (res->table && res->table != table)
+    {
+        return 1;
+    }
+
     if (!if_indextoname(ifindex, res->iface))
     {
         msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifname for index %d",
@@ -507,6 +522,7 @@ sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst,
             {
                 req.n.nlmsg_flags |= NLM_F_DUMP;
                 res.default_only = true;
+                res.table = RT_TABLE_MAIN;
             }
             else
             {
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 Add basic support for multipath gateway

Load balancing setup over multiple upstreams may include multipath gateway route, which is not not supported by OpenVPN. Let's add basic support for that for selecting best route for zero destignation address - use any one of nexthop addresses as a gateway, weights are not handled.

Setup example:

    ip route add default \
        nexthop via 192.168.1.1 dev eth1 weight 1 \
        nexthop via 192.168.2.1 dev eth2 weight 1
---
 src/openvpn/networking_sitnl.c | 37 ++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
src/openvpn/networking_sitnl.c +37 -0
index 56543648..8f084687 100644
@@ -449,6 +449,11 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
 
     while (RTA_OK(rta, len))
     {
+#ifdef RTA_MULTIPATH
+        struct rtnexthop *nh;
+        int nhlen;
+#endif
+
         switch (rta->rta_type)
         {
             /* route interface */
@@ -469,6 +474,38 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
             case RTA_TABLE:
                 table = *(unsigned int *)RTA_DATA(rta);
                 break;
+
+#ifdef RTA_MULTIPATH
+            /* multipath nexthops */
+            case RTA_MULTIPATH:
+                nh = RTA_DATA(rta);
+                nhlen = RTA_PAYLOAD(rta);
+
+                while (RTNH_OK(nh, nhlen))
+                {
+                    struct rtattr *nha = RTNH_DATA(nh);
+                    int nhalen = nh->rtnh_len - sizeof(*nh);
+
+                    /* route interface */
+                    ifindex = nh->rtnh_ifindex;
+
+                    while (RTA_OK(nha, nhalen))
+                    {
+                        switch (nha->rta_type)
+                        {
+                            /* GW for the route */
+                            case RTA_GATEWAY:
+                                memcpy(&res->gw, RTA_DATA(nha), res->addr_size);
+                                break;
+                        }
+
+                        nha = RTA_NEXT(nha, nhalen);
+                    }
+
+                    nh = RTNH_NEXT(nh);
+                }
+                break;
+#endif
         }
 
         rta = RTA_NEXT(rta, len);
Signature · 1 line
2.17.1
#14 lore raw
In reply to Vladislav Grishenko

Hi,

Quoted 1 line · Vladislav Grishenko

Current default gateway selection for zero destignation address just

little typ0: destiGnation

Quoted 46 lines

dumps and parses all the routing tables. If any of non-main table with default route comes first, wrong default gateway can be picked. Since adding/removing routes currently handles only main table, let's stick to RT_TABLE_MAIN while selecting default route too.

---
 src/openvpn/networking_sitnl.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
src/openvpn/networking_sitnl.c +6 -1
index 2bc70a50..56543648 100644
@@ -426,6 +426,7 @@ typedef struct {
     inet_address_t gw;
     char iface[IFNAMSIZ];
     bool default_only;
+    unsigned int table;
 } route_res_t;
 
 static int
@@ -435,7 +436,7 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
     struct rtmsg *r = NLMSG_DATA(n);
     struct rtattr *rta = RTM_RTA(r);
     int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
-    unsigned int ifindex = 0;
+    unsigned int table, ifindex = 0;
 
     /* filter-out non-zero dst prefixes */
     if (res->default_only && r->rtm_dst_len != 0)
@@ -443,6 +444,9 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
         return 1;
     }
 
+    /* route table, ignored with RTA_TABLE */
+    table = r->rtm_table;
+
     while (RTA_OK(rta, len))
     {
         switch (rta->rta_type)
@@ -460,11 +464,22 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
             case RTA_GATEWAY:
                 memcpy(&res->gw, RTA_DATA(rta), res->addr_size);
                 break;
+

src/openvpn/networking_sitnl.c sitnl_route_save(struct nlmsghdr *n, void *arg) · line 477

Quoted 10 lines
src/openvpn/networking_sitnl.c +5 -0
+            /* route table */
+            case RTA_TABLE:
+                table = *(unsigned int *)RTA_DATA(rta);
+                break;
         }
 
         rta = RTA_NEXT(rta, len);
     }
 
+    /* filter-out zero dns prefixes from other tables */

This comments seems off?
I'd suggest something like:

/* filter out any route not coming from the selected table */

src/openvpn/networking_sitnl.c sitnl_route_save(struct nlmsghdr *n, void *arg) · line 480

Quoted 3 lines
src/openvpn/networking_sitnl.c +3 -0
+    if (res->table && res->table != table)
+    {
+        return 1;

Am I wrong or at this point we have already copied the GW to res->gw thus, the caller will believe we have a result?

IMHO we should save the GW pointer in a temporary variable and copy it only after this check has passed.

As a side notw, could you please resend this patch based on release/2.5? We should start by fixing the bug there and then porting this patch to master.

Regards,

Quoted 14 lines
src/openvpn/networking_sitnl.c +3 -0
+    }
+
     if (!if_indextoname(ifindex, res->iface))
     {
         msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifname for index %d",
@@ -507,6 +522,7 @@ sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst,
             {
                 req.n.nlmsg_flags |= NLM_F_DUMP;
                 res.default_only = true;
+                res.table = RT_TABLE_MAIN;
             }
             else
             {
Signature · 1 line
Antonio Quartulli
#2 lore raw

PATCH v2 1/2 Fix IPv4 default gateway with multiple route tables

In reply to Vladislav Grishenko

Current default gateway selection for zero destination address just dumps and parses all the routing tables. If any of non-main table with default route comes first, wrong default gateway can be picked. Since adding/removing routes currently handles only main table, let's stick to RT_TABLE_MAIN while selecting default route too.

---
 src/openvpn/networking_sitnl.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
src/openvpn/networking_sitnl.c +22 -2
index 2bc70a50..402d3303 100644
@@ -426,6 +426,7 @@ typedef struct {
     inet_address_t gw;
     char iface[IFNAMSIZ];
     bool default_only;
+    unsigned int table;
 } route_res_t;
 
 static int
@@ -435,7 +436,8 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
     struct rtmsg *r = NLMSG_DATA(n);
     struct rtattr *rta = RTM_RTA(r);
     int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
-    unsigned int ifindex = 0;
+    unsigned int table, ifindex = 0;
+    inet_address_t gw;
 
     /* filter-out non-zero dst prefixes */
     if (res->default_only && r->rtm_dst_len != 0)
@@ -443,6 +445,11 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
         return 1;
     }
 
+    /* route table, ignored with RTA_TABLE */
+    table = r->rtm_table;
+    /* initial route gateway  */
+    gw = res->gw;
+
     while (RTA_OK(rta, len))
     {
         switch (rta->rta_type)
@@ -458,19 +465,31 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
 
             /* GW for the route */
             case RTA_GATEWAY:
-                memcpy(&res->gw, RTA_DATA(rta), res->addr_size);
+                memcpy(&gw, RTA_DATA(rta), res->addr_size);
+                break;
+
+            /* route table */
+            case RTA_TABLE:
+                table = *(unsigned int *)RTA_DATA(rta);
                 break;
         }
 
         rta = RTA_NEXT(rta, len);
     }
 
+    /* filter out any route not coming from the selected table */
+    if (res->table && res->table != table)
+    {
+        return 1;
+    }
+
     if (!if_indextoname(ifindex, res->iface))
     {
         msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifname for index %d",
             __func__, ifindex);
         return -1;
     }
+    res->gw = gw;
 
     return 0;
 }
@@ -507,6 +526,7 @@ sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst,
             {
                 req.n.nlmsg_flags |= NLM_F_DUMP;
                 res.default_only = true;
+                res.table = RT_TABLE_MAIN;
             }
             else
             {
Signature · 1 line
2.17.1
#3 lore raw

PATCH v2 2/2 Add basic support for multipath gateway

Load balancing setup over multiple upstreams may include multipath gateway route, which is not not supported by OpenVPN. Let's add basic support for that for selecting best route for zero destination address - use any one of nexthop addresses as a gateway, weights are not handled.

Setup example:

    ip route add default \
        nexthop via 192.168.1.1 dev eth1 weight 1 \
        nexthop via 192.168.2.1 dev eth2 weight 1
---
 src/openvpn/networking_sitnl.c | 35 ++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
src/openvpn/networking_sitnl.c +35 -0
index 402d3303..02c34d6b 100644
@@ -452,6 +452,9 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
 
     while (RTA_OK(rta, len))
     {
+        struct rtnexthop *nh;
+        int nhlen;
+
         switch (rta->rta_type)
         {
             /* route interface */
@@ -472,6 +475,38 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
             case RTA_TABLE:
                 table = *(unsigned int *)RTA_DATA(rta);
                 break;
+
+            /* multipath nexthops */
+            case RTA_MULTIPATH:
+                nh = RTA_DATA(rta);
+                nhlen = RTA_PAYLOAD(rta);
+
+                while (RTNH_OK(nh, nhlen))
+                {
+                    struct rtattr *nha = RTNH_DATA(nh);
+                    int nhalen = nh->rtnh_len - sizeof(*nh);
+
+                    /* route interface */
+                    ifindex = nh->rtnh_ifindex;
+                    /* initial route gateway  */
+                    gw = res->gw;
+
+                    while (RTA_OK(nha, nhalen))
+                    {
+                        switch (nha->rta_type)
+                        {
+                            /* GW for the route */
+                            case RTA_GATEWAY:
+                                memcpy(&gw, RTA_DATA(nha), res->addr_size);
+                                break;
+                        }
+
+                        nha = RTA_NEXT(nha, nhalen);
+                    }
+
+                    nh = RTNH_NEXT(nh);
+                }
+                break;
         }
 
         rta = RTA_NEXT(rta, len);
Signature · 1 line
2.17.1
#4 lore raw
In reply to Vladislav Grishenko

Hi,

Quoted 66 lines · Vladislav Grishenko

Current default gateway selection for zero destination address just dumps and parses all the routing tables. If any of non-main table with default route comes first, wrong default gateway can be picked. Since adding/removing routes currently handles only main table, let's stick to RT_TABLE_MAIN while selecting default route too.

---
 src/openvpn/networking_sitnl.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
src/openvpn/networking_sitnl.c +17 -2
index 2bc70a50..402d3303 100644
@@ -426,6 +426,7 @@ typedef struct {
     inet_address_t gw;
     char iface[IFNAMSIZ];
     bool default_only;
+    unsigned int table;
 } route_res_t;
 
 static int
@@ -435,7 +436,8 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
     struct rtmsg *r = NLMSG_DATA(n);
     struct rtattr *rta = RTM_RTA(r);
     int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
-    unsigned int ifindex = 0;
+    unsigned int table, ifindex = 0;
+    inet_address_t gw;
 
     /* filter-out non-zero dst prefixes */
     if (res->default_only && r->rtm_dst_len != 0)
@@ -443,6 +445,11 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
         return 1;
     }
 
+    /* route table, ignored with RTA_TABLE */
+    table = r->rtm_table;
+    /* initial route gateway  */
+    gw = res->gw;
+
     while (RTA_OK(rta, len))
     {
         switch (rta->rta_type)
@@ -458,19 +465,31 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
 
             /* GW for the route */
             case RTA_GATEWAY:
-                memcpy(&res->gw, RTA_DATA(rta), res->addr_size);
+                memcpy(&gw, RTA_DATA(rta), res->addr_size);
+                break;
+
+            /* route table */
+            case RTA_TABLE:
+                table = *(unsigned int *)RTA_DATA(rta);
                 break;
         }
 
         rta = RTA_NEXT(rta, len);
     }
 
+    /* filter out any route not coming from the selected table */
+    if (res->table && res->table != table)
+    {

src/openvpn/networking_sitnl.c sitnl_route_save(struct nlmsghdr *n, void *arg) · line 492

Quoted 10 lines
src/openvpn/networking_sitnl.c +4 -0
+        return 1;
+    }
+
     if (!if_indextoname(ifindex, res->iface))
     {
         msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifname for index %d",
             __func__, ifindex);
         return -1;
     }
+    res->gw = gw;

We would be potentially copying the GW value 3 times now. How about simplifying this back and forth with the following?

src/openvpn/networking_sitnl.c +7 -5
index 402d3303..cc80ca29 100644
@@ -437,7 +437,7 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
     struct rtattr *rta = RTM_RTA(r);
     int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
     unsigned int table, ifindex = 0;
-    inet_address_t gw;
+    uint8_t *gw = NULL;
 
     /* filter-out non-zero dst prefixes */
     if (res->default_only && r->rtm_dst_len != 0)
@@ -447,8 +447,6 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
 
     /* route table, ignored with RTA_TABLE */
     table = r->rtm_table;
-    /* initial route gateway  */
-    gw = res->gw;
 
     while (RTA_OK(rta, len))
     {
@@ -465,7 +463,7 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
 
             /* GW for the route */
             case RTA_GATEWAY:
-                memcpy(&gw, RTA_DATA(rta), res->addr_size);
+                gw = RTA_DATA(rta);
                 break;
 
             /* route table */
@@ -489,7 +487,11 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
             __func__, ifindex);
         return -1;
     }
-    res->gw = gw;
+
+    if (gw)
+    {
+           memcpy(&res->gw, gw, res->addr_size);
+    }
 
     return 0;
 }

What do you think?

Best Regards,

Quoted 12 lines
src/openvpn/networking_sitnl.c +1 -0
     return 0;
 }
@@ -507,6 +526,7 @@ sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst,
             {
                 req.n.nlmsg_flags |= NLM_F_DUMP;
                 res.default_only = true;
+                res.table = RT_TABLE_MAIN;
             }
             else
             {
Signature · 1 line
Antonio Quartulli
#5 lore raw

PATCH v3 1/2 Fix IPv4 default gateway with multiple route tables

In reply to Vladislav Grishenko

Current default gateway selection for zero destination address just dumps and parses all the routing tables. If any of non-main table with default route comes first, wrong default gateway can be picked. Since adding/removing routes currently handles only main table, let's stick to RT_TABLE_MAIN while selecting default route too.

v2: keep gateway address unchanged on lookup error
v3: reduce ammout of gateway address copying

---
 src/openvpn/networking_sitnl.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
src/openvpn/networking_sitnl.c +24 -2
index 2bc70a50..ea1621ed 100644
@@ -426,6 +426,7 @@ typedef struct {
     inet_address_t gw;
     char iface[IFNAMSIZ];
     bool default_only;
+    unsigned int table;
 } route_res_t;
 
 static int
@@ -435,7 +436,8 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
     struct rtmsg *r = NLMSG_DATA(n);
     struct rtattr *rta = RTM_RTA(r);
     int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
-    unsigned int ifindex = 0;
+    unsigned int table, ifindex = 0;
+    void *gw = NULL;
 
     /* filter-out non-zero dst prefixes */
     if (res->default_only && r->rtm_dst_len != 0)
@@ -443,6 +445,9 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
         return 1;
     }
 
+    /* route table, ignored with RTA_TABLE */
+    table = r->rtm_table;
+
     while (RTA_OK(rta, len))
     {
         switch (rta->rta_type)
@@ -458,13 +463,24 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
 
             /* GW for the route */
             case RTA_GATEWAY:
-                memcpy(&res->gw, RTA_DATA(rta), res->addr_size);
+                gw = RTA_DATA(rta);
+                break;
+
+            /* route table */
+            case RTA_TABLE:
+                table = *(unsigned int *)RTA_DATA(rta);
                 break;
         }
 
         rta = RTA_NEXT(rta, len);
     }
 
+    /* filter out any route not coming from the selected table */
+    if (res->table && res->table != table)
+    {
+        return 1;
+    }
+
     if (!if_indextoname(ifindex, res->iface))
     {
         msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifname for index %d",
@@ -472,6 +488,11 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
         return -1;
     }
 
+    if (gw)
+    {
+        memcpy(&res->gw, gw, res->addr_size);
+    }
+
     return 0;
 }
 
@@ -507,6 +528,7 @@ sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst,
             {
                 req.n.nlmsg_flags |= NLM_F_DUMP;
                 res.default_only = true;
+                res.table = RT_TABLE_MAIN;
             }
             else
             {
Signature · 1 line
2.17.1
#6 lore raw

PATCH v3 2/2 Add basic support for multipath gateway

Load balancing setup over multiple upstreams may include multipath gateway route, which is not not supported by OpenVPN. Let's add basic support for that for selecting best route for zero destination address - use any one of nexthop addresses as a gateway, weights are not handled.

Setup example:

    ip route add default \
        nexthop via 192.168.1.1 dev eth1 weight 1 \
        nexthop via 192.168.2.1 dev eth2 weight 1

v2: keep gateway address unchanged on lookup error
v3: reduce ammout of gateway address copying

---
 src/openvpn/networking_sitnl.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
src/openvpn/networking_sitnl.c +34 -0
index ea1621ed..aa35f5f5 100644
@@ -450,6 +450,9 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
 
     while (RTA_OK(rta, len))
     {
+        struct rtnexthop *nh;
+        int nhlen;
+
         switch (rta->rta_type)
         {
             /* route interface */
@@ -470,6 +473,37 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
             case RTA_TABLE:
                 table = *(unsigned int *)RTA_DATA(rta);
                 break;
+
+            /* multipath nexthops */
+            case RTA_MULTIPATH:
+                nh = RTA_DATA(rta);
+                nhlen = RTA_PAYLOAD(rta);
+
+                while (RTNH_OK(nh, nhlen))
+                {
+                    struct rtattr *nha = RTNH_DATA(nh);
+                    int nhalen = nh->rtnh_len - sizeof(*nh);
+
+                    /* init route interface & gateway */
+                    ifindex = nh->rtnh_ifindex;
+                    gw = NULL;
+
+                    while (RTA_OK(nha, nhalen))
+                    {
+                        switch (nha->rta_type)
+                        {
+                            /* GW for the route */
+                            case RTA_GATEWAY:
+                                gw = RTA_DATA(nha);
+                                break;
+                        }
+
+                        nha = RTA_NEXT(nha, nhalen);
+                    }
+
+                    nh = RTNH_NEXT(nh);
+                }
+                break;
         }
 
         rta = RTA_NEXT(rta, len);
Signature · 1 line
2.17.1
#10 lore raw
In reply to Vladislav Grishenko

Hi,

Quoted 56 lines · Vladislav Grishenko

Current default gateway selection for zero destination address just dumps and parses all the routing tables. If any of non-main table with default route comes first, wrong default gateway can be picked. Since adding/removing routes currently handles only main table, let's stick to RT_TABLE_MAIN while selecting default route too.

v2: keep gateway address unchanged on lookup error
v3: reduce ammout of gateway address copying

---
 src/openvpn/networking_sitnl.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
src/openvpn/networking_sitnl.c +10 -2
index 2bc70a50..ea1621ed 100644
@@ -426,6 +426,7 @@ typedef struct {
     inet_address_t gw;
     char iface[IFNAMSIZ];
     bool default_only;
+    unsigned int table;
 } route_res_t;
 
 static int
@@ -435,7 +436,8 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
     struct rtmsg *r = NLMSG_DATA(n);
     struct rtattr *rta = RTM_RTA(r);
     int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
-    unsigned int ifindex = 0;
+    unsigned int table, ifindex = 0;
+    void *gw = NULL;
 
     /* filter-out non-zero dst prefixes */
     if (res->default_only && r->rtm_dst_len != 0)
@@ -443,6 +445,9 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
         return 1;
     }
 
+    /* route table, ignored with RTA_TABLE */
+    table = r->rtm_table;
+
     while (RTA_OK(rta, len))
     {
         switch (rta->rta_type)
@@ -458,13 +463,24 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
 
             /* GW for the route */
             case RTA_GATEWAY:
-                memcpy(&res->gw, RTA_DATA(rta), res->addr_size);
+                gw = RTA_DATA(rta);
+                break;
+
+            /* route table */

src/openvpn/networking_sitnl.c sitnl_route_save(struct nlmsghdr *n, void *arg) · line 479

Quoted 10 lines
src/openvpn/networking_sitnl.c +4 -0
+            case RTA_TABLE:
+                table = *(unsigned int *)RTA_DATA(rta);
                 break;
         }
 
         rta = RTA_NEXT(rta, len);
     }
 
+    /* filter out any route not coming from the selected table */
+    if (res->table && res->table != table)

Today I was wondering if 0 could be a meaningful table ID, since this code uses 0 as "no table was selected for filtering" (thus creating a false negative).

After checking rtnetlink.h, I could see that 0 is indeed the UNSPEC value (RT_TABLE_UNSPEC=0), therefore your patch is correct.

However, to prevent the next casual reader from asking the same question, wouldn't it make sense to change this comparison with:

if (res->table != RT_TABLE_UNSPEC && res->table != table)

Quoted 20 lines
src/openvpn/networking_sitnl.c +9 -0
+    {
+        return 1;
+    }
+
     if (!if_indextoname(ifindex, res->iface))
     {
         msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifname for index %d",
@@ -472,6 +488,11 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
         return -1;
     }
 
+    if (gw)
+    {
+        memcpy(&res->gw, gw, res->addr_size);
+    }
+
     return 0;
 }
 

src/openvpn/networking_sitnl.c sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst, · line 531

Quoted 4 lines
src/openvpn/networking_sitnl.c +1 -0
@@ -507,6 +528,7 @@ sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst,
             {
                 req.n.nlmsg_flags |= NLM_F_DUMP;
                 res.default_only = true;
+                res.table = RT_TABLE_MAIN;

Another question: why restricting the research to the MAIN table only for IPv4 and only when searching a default route?

Like you said in the commit message: OpenVPN only operates on the MAIN routing table for the time being, therefore, shouldn't we just set "res.table = RT_TABLE_MAIN" by default *all the time* for now?

Otherwise we are fixing this problem only for the ipv4/default search, but the function remains "buggy" for the other cases.

src/openvpn/networking_sitnl.c sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst, · line 534

Quoted 4 lines
src/openvpn/networking_sitnl.c +0 -0
             }
             else
             {

Regards,

Signature · 1 line
Antonio Quartulli
#11 lore raw

Hi,

Quoted 4 lines

However, to prevent the next casual reader from asking the same question, wouldn't it make sense to change this comparison with:

if (res->table != RT_TABLE_UNSPEC && res->table != table)

I don’t think it's necessary for following reasons:
1. Readability. "if (res->table)" reads as "if filter table is set ". Changing it to "if (res->table != RT_TABLE_UNSPEC)" gives the same reading imho.
2. RT_TABLE_UNSPEC was and will be always zero, because zillion of software incl. iproute2 depends on zeroed structs with 0 == UNSPEC
3. Comparing with RT_TABLE_UNSPEC here will require additional code to explicitely init res.table with RT_TABLE_UNSPEC, which in fact is will be noop after CLEAR(res) and have little sense, since there's no chance to have RT_TABLE_UNSPEC != 0. Anyway, if you still thinks it's required, I can do change.

Quoted 2 lines

Another question: why restricting the research to the MAIN table only for IPv4 and only when searching a default route?

For real dst, incl ::/128 we do the right thing - perform "ip route get" that will get real gateway for specified dst no matter what table or anything else is. And we performs "ip route show" dump only to get gateway for "0.0.0.0" destination by a reason.
Getting default gateway for "0.0.0.0/0" destination is not logically possible at all, for example what is default gateway for this case:

	0.0.0.0/1 via a.a.a.a
	128.0.0.0/1 via b.b.b.b

Therefore for 0.0.0.0/0 case heuristic is used, if there's a default route, if it's in main table and so on, that’s why only dumping needs it. Any other - don't.

Quoted 2 lines

Otherwise we are fixing this problem only for the ipv4/default search, but the function remains "buggy" for the other cases.

Buggy here is searching default gateway for 0.0.0.0/0 itself. Other cases are right from the scratch :)

--
Best Regards, Vladislav Grishenko

Quoted 137 lines

-----Original Message-----
From: Antonio Quartulli <[email protected]...>
Sent: Friday, April 16, 2021 7:01 PM
To: Vladislav Grishenko <[email protected]...>; openvpn- [email protected]
Subject: Re: [Openvpn-devel] [PATCH v3 1/2] Fix IPv4 default gateway with multiple route tables

Hi,

Quoted 56 lines · Vladislav Grishenko

Current default gateway selection for zero destination address just dumps and parses all the routing tables. If any of non-main table with default route comes first, wrong default gateway can be picked. Since adding/removing routes currently handles only main table, let's stick to RT_TABLE_MAIN while selecting default route too.

v2: keep gateway address unchanged on lookup error
v3: reduce ammout of gateway address copying

---
 src/openvpn/networking_sitnl.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/src/openvpn/networking_sitnl.c index 2bc70a50..ea1621ed 100644

src/openvpn/networking_sitnl.c +3 -1
@@ -426,6 +426,7 @@ typedef struct {
     inet_address_t gw;
     char iface[IFNAMSIZ];
     bool default_only;
+    unsigned int table;
 } route_res_t;
 
 static int
@@ -435,7 +436,8 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
     struct rtmsg *r = NLMSG_DATA(n);
     struct rtattr *rta = RTM_RTA(r);
     int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
-    unsigned int ifindex = 0;
+    unsigned int table, ifindex = 0;
+    void *gw = NULL;
 
     /* filter-out non-zero dst prefixes */
     if (res->default_only && r->rtm_dst_len != 0) @@ -443,6 +445,9 @@

sitnl_route_save(struct nlmsghdr *n, void *arg)

         return 1;
     }
src/openvpn/networking_sitnl.c +7 -1
+    /* route table, ignored with RTA_TABLE */
+    table = r->rtm_table;
+
     while (RTA_OK(rta, len))
     {
         switch (rta->rta_type)
@@ -458,13 +463,24 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)
 
             /* GW for the route */
             case RTA_GATEWAY:
-                memcpy(&res->gw, RTA_DATA(rta), res->addr_size);
+                gw = RTA_DATA(rta);
+                break;
+
+            /* route table */

src/openvpn/networking_sitnl.c sitnl_route_save(struct nlmsghdr *n, void *arg) · line 479

Quoted 10 lines
src/openvpn/networking_sitnl.c +4 -0
+            case RTA_TABLE:
+                table = *(unsigned int *)RTA_DATA(rta);
                 break;
         }
 
         rta = RTA_NEXT(rta, len);
     }
 
+    /* filter out any route not coming from the selected table */
+    if (res->table && res->table != table)

Today I was wondering if 0 could be a meaningful table ID, since this code uses 0 as "no table was selected for filtering" (thus creating a false negative).

After checking rtnetlink.h, I could see that 0 is indeed the UNSPEC value (RT_TABLE_UNSPEC=0), therefore your patch is correct.

However, to prevent the next casual reader from asking the same question, wouldn't it make sense to change this comparison with:

if (res->table != RT_TABLE_UNSPEC && res->table != table)

src/openvpn/networking_sitnl.c sitnl_route_best_gw(sa_family_t af_family, const

Quoted 20 lines
src/openvpn/networking_sitnl.c +4 -0
+    {
+        return 1;
+    }
+
     if (!if_indextoname(ifindex, res->iface))
     {
         msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifname for index

%d", @@ -472,6 +488,11 @@ sitnl_route_save(struct nlmsghdr *n, void *arg)

         return -1;
     }
src/openvpn/networking_sitnl.c +5 -0
+    if (gw)
+    {
+        memcpy(&res->gw, gw, res->addr_size);
+    }
+
     return 0;
 }
@@ -507,6 +528,7 @@ sitnl_route_best_gw(sa_family_t af_family, const

inet_address_t *dst,

src/openvpn/networking_sitnl.c sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst, · line 531

Quoted 4 lines
src/openvpn/networking_sitnl.c +1 -0
             {
                 req.n.nlmsg_flags |= NLM_F_DUMP;
                 res.default_only = true;
+                res.table = RT_TABLE_MAIN;

Another question: why restricting the research to the MAIN table only for IPv4 and only when searching a default route?

Like you said in the commit message: OpenVPN only operates on the MAIN routing table for the time being, therefore, shouldn't we just set "res.table = RT_TABLE_MAIN" by default *all the time* for now?

Otherwise we are fixing this problem only for the ipv4/default search, but the function remains "buggy" for the other cases.

src/openvpn/networking_sitnl.c sitnl_route_best_gw(sa_family_t af_family, const · line 534

Quoted 4 lines
src/openvpn/networking_sitnl.c +0 -0
             }
             else
             {

Regards,

--
Antonio Quartulli

#12 lore raw

Hi,

Quoted 5 lines · Vladislav Grishenko

Hi,

Quoted 3 lines

However, to prevent the next casual reader from asking the same question, wouldn't it make sense to change this comparison with:

Quoted 7 lines
Quoted 1 line

if (res->table != RT_TABLE_UNSPEC && res->table != table)

I don’t think it's necessary for following reasons:
1. Readability. "if (res->table)" reads as "if filter table is set ". Changing it to "if (res->table != RT_TABLE_UNSPEC)" gives the same reading imho.
2. RT_TABLE_UNSPEC was and will be always zero, because zillion of software incl. iproute2 depends on zeroed structs with 0 == UNSPEC
3. Comparing with RT_TABLE_UNSPEC here will require additional code to explicitely init res.table with RT_TABLE_UNSPEC, which in fact is will be noop after CLEAR(res) and have little sense, since there's no chance to have RT_TABLE_UNSPEC != 0. Anyway, if you still thinks it's required, I can do change.

No, it's ok. You are right that 0 is just assumed to be the UNSPEC filter (i.e. don't filter). Let's keep it as it is.

Quoted 9 lines
Quoted 3 lines

Another question: why restricting the research to the MAIN table only for IPv4 and only when searching a default route?

For real dst, incl ::/128 we do the right thing - perform "ip route get" that will get real gateway for specified dst no matter what table or anything else is. And we performs "ip route show" dump only to get gateway for "0.0.0.0" destination by a reason.
Getting default gateway for "0.0.0.0/0" destination is not logically possible at all, for example what is default gateway for this case:

	0.0.0.0/1 via a.a.a.a
	128.0.0.0/1 via b.b.b.b
Quoted 8 lines

Therefore for 0.0.0.0/0 case heuristic is used, if there's a default route, if it's in main table and so on, that’s why only dumping needs it. Any other - don't.

Quoted 2 lines

Otherwise we are fixing this problem only for the ipv4/default search, but the function remains "buggy" for the other cases.

Buggy here is searching default gateway for 0.0.0.0/0 itself. Other cases are right from the scratch :)

Right. The problem I was seeing is simply that we are not providing all info to the kernel as we should.
This requires further improvement later, but not as part of this fix :-)

So the patch is fine as it is, thanks!

@Gert: this should go to 2.5 too. Thanks!

Regards,

Signature · 1 line
Antonio Quartulli
#7 lore raw

Re: [Openvpn-devel] [PATCH v3 2/2] Add basic support for multipath gateway

In reply to Vladislav Grishenko

Hi,

Quoted 5 lines · Vladislav Grishenko

Load balancing setup over multiple upstreams may include multipath gateway route, which is not not supported by OpenVPN. Let's add basic support for that for selecting best route for zero destination address - use any one of nexthop addresses as a gateway, weights are not handled.

What happens with the current code when we encounter such a situation?

I assume it will "just not work" and find no gateway address?

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#13 lore raw
In reply to Vladislav Grishenko

Code looks reasonable (though I won't claim to understand the nuances of Netlink :-) ) - took me a bit to remember what is done here with the callback function for matching, but yeah, seems to do what it says on the lid. Did some very basic testing on a system with just a single route table and nothing breaks :-)

Your patch has been applied to the master and release/2.5 branch (bugfix).

commit c7f95891a4a0aabb64e7d4f3200525c1a2fcf433 (master) commit 48ba0b059138dec7a94d2368375344d39c4e05e9 (release/2.5)

Author: Vladislav Grishenko
Date:   Fri Apr 16 17:07:07 2021 +0500
     Fix IPv4 default gateway with multiple route tables

     Signed-off-by: Vladislav Grishenko <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg22130.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#8 lore raw

Re: [Openvpn-devel] [PATCH v3 2/2] Add basic support for multipath gateway

In reply to Gert Doering
Quoted 9 lines · Gert Doering

Hi,

Quoted 5 lines · Vladislav Grishenko

Load balancing setup over multiple upstreams may include multipath gateway route, which is not not supported by OpenVPN. Let's add basic support for that for selecting best route for zero destination address - use any one of nexthop addresses as a gateway, weights are not handled.

Quoted 3 lines

What happens with the current code when we encounter such a situation?

I assume it will "just not work" and find no gateway address?

Yeah, nexthops groups are just not parsed. So if the default GW is configured this way, it will not be found.

This said, it's very likely the main routing table will have a classic default route most of the time.

Still, we want to handle nexthop groups somehow.

Cheers,

Signature · 1 line
Antonio Quartulli
#9 lore raw

Re: [Openvpn-devel] [PATCH v3 2/2] Add basic support for multipath gateway

Hi,

Quoted 1 line · Antonio Quartulli

Still, we want to handle nexthop groups somehow.

Then it looks like this needs testing and an ACK :-)

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...