|
|
049c96 |
From 3cdde59a38727b58edb9e165fcbb9befa5025f43 Mon Sep 17 00:00:00 2001
|
|
|
049c96 |
From: Phil Sutter <psutter@redhat.com>
|
|
|
049c96 |
Date: Wed, 20 Jul 2016 21:31:37 +0200
|
|
|
049c96 |
Subject: [PATCH] ip route: restore route entries in correct order
|
|
|
049c96 |
|
|
|
049c96 |
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1348133
|
|
|
049c96 |
Upstream Status: iproute2.git commit 74af8dd9620e4
|
|
|
049c96 |
|
|
|
049c96 |
commit 74af8dd9620e4322babf9d2a936b1d333a4e37e0
|
|
|
049c96 |
Author: Xin Long <lucien.xin@gmail.com>
|
|
|
049c96 |
Date: Tue Jul 12 21:37:58 2016 +0800
|
|
|
049c96 |
|
|
|
049c96 |
ip route: restore route entries in correct order
|
|
|
049c96 |
|
|
|
049c96 |
Sometimes we cannot restore route entries, because in kernel
|
|
|
049c96 |
[1] fib_check_nh()
|
|
|
049c96 |
[2] fib_valid_prefsrc()
|
|
|
049c96 |
cause some routes to depend on existence of others while adding.
|
|
|
049c96 |
|
|
|
049c96 |
For example, we saved all the routes, and flushed all tables
|
|
|
049c96 |
[a] default via 192.168.122.1 dev eth0
|
|
|
049c96 |
[b] 192.168.122.0/24 dev eth0 src 192.168.122.21
|
|
|
049c96 |
[c] broadcast 127.0.0.0 dev lo table local src 127.0.0.1
|
|
|
049c96 |
[d] local 127.0.0.0/8 dev lo table local src 127.0.0.1
|
|
|
049c96 |
[e] local 127.0.0.1 dev lo table local src 127.0.0.1
|
|
|
049c96 |
[f] broadcast 127.255.255.255 dev lo table local src 127.0.0.1
|
|
|
049c96 |
[g] broadcast 192.168.122.0 dev eth0 table local src 192.168.122.21
|
|
|
049c96 |
[h] local 192.168.122.21 dev eth0 table local src 192.168.122.21
|
|
|
049c96 |
[i] broadcast 192.168.122.255 dev eth0 table local src 192.168.122.21
|
|
|
049c96 |
|
|
|
049c96 |
Now start to restore them:
|
|
|
049c96 |
If we want to add [a], we have to add [b] first, as [1] and
|
|
|
049c96 |
'via 192.168.122.1' in [a].
|
|
|
049c96 |
If we want to add [b], we have to add [h] first, as [2] and
|
|
|
049c96 |
'src 192.168.122.21' in [b].
|
|
|
049c96 |
|
|
|
049c96 |
So the correct order to restore should be like:
|
|
|
049c96 |
[e][h] -> [b][c][d][f][g][i] -> [a]
|
|
|
049c96 |
|
|
|
049c96 |
This patch fixes it by traversing the file 3 times, it only restores
|
|
|
049c96 |
part of them in each run according to the following conditions, to
|
|
|
049c96 |
make sure every entry can be restored successfully.
|
|
|
049c96 |
1. !gw && (!fib_prefsrc || fib_prefsrc == cfg->fc_dst)
|
|
|
049c96 |
2. !gw && (fib_prefsrc != cfg->fc_dst)
|
|
|
049c96 |
3. gw
|
|
|
049c96 |
|
|
|
049c96 |
Signed-off-by: Xin Long <lucien.xin@gmail.com>
|
|
|
049c96 |
Acked-by: Phil Sutter <phil@nwl.cc>
|
|
|
049c96 |
---
|
|
|
049c96 |
ip/iproute.c | 47 +++++++++++++++++++++++++++++++++++++++++++++--
|
|
|
049c96 |
1 file changed, 45 insertions(+), 2 deletions(-)
|
|
|
049c96 |
|
|
|
049c96 |
diff --git a/ip/iproute.c b/ip/iproute.c
|
|
|
049c96 |
index 74599e4..fca5802 100644
|
|
|
049c96 |
--- a/ip/iproute.c
|
|
|
049c96 |
+++ b/ip/iproute.c
|
|
|
049c96 |
@@ -1585,12 +1585,42 @@ static int iproute_get(int argc, char **argv)
|
|
|
049c96 |
exit(0);
|
|
|
049c96 |
}
|
|
|
049c96 |
|
|
|
049c96 |
+static int rtattr_cmp(struct rtattr *rta1, struct rtattr *rta2)
|
|
|
049c96 |
+{
|
|
|
049c96 |
+ if (!rta1 || !rta2 || rta1->rta_len != rta2->rta_len)
|
|
|
049c96 |
+ return 1;
|
|
|
049c96 |
+
|
|
|
049c96 |
+ return memcmp(RTA_DATA(rta1), RTA_DATA(rta2), RTA_PAYLOAD(rta1));
|
|
|
049c96 |
+}
|
|
|
049c96 |
+
|
|
|
049c96 |
static int restore_handler(const struct sockaddr_nl *nl,
|
|
|
049c96 |
struct rtnl_ctrl_data *ctrl,
|
|
|
049c96 |
struct nlmsghdr *n, void *arg)
|
|
|
049c96 |
{
|
|
|
049c96 |
- int ret;
|
|
|
049c96 |
+ struct rtmsg *r = NLMSG_DATA(n);
|
|
|
049c96 |
+ struct rtattr *tb[RTA_MAX+1];
|
|
|
049c96 |
+ int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
|
|
|
049c96 |
+ int ret, prio = *(int *)arg;
|
|
|
049c96 |
+
|
|
|
049c96 |
+ parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
|
|
|
049c96 |
|
|
|
049c96 |
+ /* Restore routes in correct order:
|
|
|
049c96 |
+ * 0. ones for local addresses,
|
|
|
049c96 |
+ * 1. ones for local networks,
|
|
|
049c96 |
+ * 2. others (remote networks/hosts).
|
|
|
049c96 |
+ */
|
|
|
049c96 |
+ if (!prio && !tb[RTA_GATEWAY] && (!tb[RTA_PREFSRC] ||
|
|
|
049c96 |
+ !rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST])))
|
|
|
049c96 |
+ goto restore;
|
|
|
049c96 |
+ else if (prio == 1 && !tb[RTA_GATEWAY] &&
|
|
|
049c96 |
+ rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST]))
|
|
|
049c96 |
+ goto restore;
|
|
|
049c96 |
+ else if (prio == 2 && tb[RTA_GATEWAY])
|
|
|
049c96 |
+ goto restore;
|
|
|
049c96 |
+
|
|
|
049c96 |
+ return 0;
|
|
|
049c96 |
+
|
|
|
049c96 |
+restore:
|
|
|
049c96 |
n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
|
|
|
049c96 |
|
|
|
049c96 |
ll_init_map(&rth);
|
|
|
049c96 |
@@ -1623,10 +1653,23 @@ static int route_dump_check_magic(void)
|
|
|
049c96 |
|
|
|
049c96 |
static int iproute_restore(void)
|
|
|
049c96 |
{
|
|
|
049c96 |
+ int pos, prio;
|
|
|
049c96 |
+
|
|
|
049c96 |
if (route_dump_check_magic())
|
|
|
049c96 |
exit(-1);
|
|
|
049c96 |
|
|
|
049c96 |
- exit(rtnl_from_file(stdin, &restore_handler, NULL));
|
|
|
049c96 |
+ pos = ftell(stdin);
|
|
|
049c96 |
+ for (prio = 0; prio < 3; prio++) {
|
|
|
049c96 |
+ int err;
|
|
|
049c96 |
+
|
|
|
049c96 |
+ err = rtnl_from_file(stdin, &restore_handler, &prio;;
|
|
|
049c96 |
+ if (err)
|
|
|
049c96 |
+ exit(err);
|
|
|
049c96 |
+
|
|
|
049c96 |
+ fseek(stdin, pos, SEEK_SET);
|
|
|
049c96 |
+ }
|
|
|
049c96 |
+
|
|
|
049c96 |
+ exit(0);
|
|
|
049c96 |
}
|
|
|
049c96 |
|
|
|
049c96 |
static int show_handler(const struct sockaddr_nl *nl,
|
|
|
049c96 |
--
|
|
|
049c96 |
1.8.3.1
|
|
|
049c96 |
|