linux: backport IPv6 SAS fixes for source-specific routes
[oweals/openwrt.git] / target / linux / generic / patches-4.0 / 667-ipv6-Fixed-source-specific-default-route-handling.patch
1 From e16e888b525503be05b3aea64190e8b3bdef44d0 Mon Sep 17 00:00:00 2001
2 From: Markus Stenberg <markus.stenberg@iki.fi>
3 Date: Tue, 5 May 2015 13:36:59 +0300
4 Subject: [PATCH] ipv6: Fixed source specific default route handling.
5
6 If there are only IPv6 source specific default routes present, the
7 host gets -ENETUNREACH on e.g. connect() because ip6_dst_lookup_tail
8 calls ip6_route_output first, and given source address any, it fails,
9 and ip6_route_get_saddr is never called.
10
11 The change is to use the ip6_route_get_saddr, even if the initial
12 ip6_route_output fails, and then doing ip6_route_output _again_ after
13 we have appropriate source address available.
14
15 Note that this is '99% fix' to the problem; a correct fix would be to
16 do route lookups only within addrconf.c when picking a source address,
17 and never call ip6_route_output before source address has been
18 populated.
19
20 Signed-off-by: Markus Stenberg <markus.stenberg@iki.fi>
21 Signed-off-by: David S. Miller <davem@davemloft.net>
22 ---
23  net/ipv6/ip6_output.c | 39 +++++++++++++++++++++++++++++++--------
24  net/ipv6/route.c      |  5 +++--
25  2 files changed, 34 insertions(+), 10 deletions(-)
26
27 diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
28 index 7fde1f2..c217775 100644
29 --- a/net/ipv6/ip6_output.c
30 +++ b/net/ipv6/ip6_output.c
31 @@ -886,22 +886,45 @@ static int ip6_dst_lookup_tail(struct sock *sk,
32  #endif
33         int err;
34  
35 -       if (!*dst)
36 -               *dst = ip6_route_output(net, sk, fl6);
37 -
38 -       err = (*dst)->error;
39 -       if (err)
40 -               goto out_err_release;
41 +       /* The correct way to handle this would be to do
42 +        * ip6_route_get_saddr, and then ip6_route_output; however,
43 +        * the route-specific preferred source forces the
44 +        * ip6_route_output call _before_ ip6_route_get_saddr.
45 +        *
46 +        * In source specific routing (no src=any default route),
47 +        * ip6_route_output will fail given src=any saddr, though, so
48 +        * that's why we try it again later.
49 +        */
50 +       if (ipv6_addr_any(&fl6->saddr) && (!*dst || !(*dst)->error)) {
51 +               struct rt6_info *rt;
52 +               bool had_dst = *dst != NULL;
53  
54 -       if (ipv6_addr_any(&fl6->saddr)) {
55 -               struct rt6_info *rt = (struct rt6_info *) *dst;
56 +               if (!had_dst)
57 +                       *dst = ip6_route_output(net, sk, fl6);
58 +               rt = (*dst)->error ? NULL : (struct rt6_info *)*dst;
59                 err = ip6_route_get_saddr(net, rt, &fl6->daddr,
60                                           sk ? inet6_sk(sk)->srcprefs : 0,
61                                           &fl6->saddr);
62                 if (err)
63                         goto out_err_release;
64 +
65 +               /* If we had an erroneous initial result, pretend it
66 +                * never existed and let the SA-enabled version take
67 +                * over.
68 +                */
69 +               if (!had_dst && (*dst)->error) {
70 +                       dst_release(*dst);
71 +                       *dst = NULL;
72 +               }
73         }
74  
75 +       if (!*dst)
76 +               *dst = ip6_route_output(net, sk, fl6);
77 +
78 +       err = (*dst)->error;
79 +       if (err)
80 +               goto out_err_release;
81 +
82  #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
83         /*
84          * Here if the dst entry we've looked up
85 diff --git a/net/ipv6/route.c b/net/ipv6/route.c
86 index 5c48293..d358888 100644
87 --- a/net/ipv6/route.c
88 +++ b/net/ipv6/route.c
89 @@ -2245,9 +2245,10 @@ int ip6_route_get_saddr(struct net *net,
90                         unsigned int prefs,
91                         struct in6_addr *saddr)
92  {
93 -       struct inet6_dev *idev = ip6_dst_idev((struct dst_entry *)rt);
94 +       struct inet6_dev *idev =
95 +               rt ? ip6_dst_idev((struct dst_entry *)rt) : NULL;
96         int err = 0;
97 -       if (rt->rt6i_prefsrc.plen)
98 +       if (rt && rt->rt6i_prefsrc.plen)
99                 *saddr = rt->rt6i_prefsrc.addr;
100         else
101                 err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
102 -- 
103 2.1.4
104