1 --- a/include/linux/rtnetlink.h
2 +++ b/include/linux/rtnetlink.h
3 @@ -293,6 +293,8 @@ struct rtnexthop
4 #define RTNH_F_DEAD 1 /* Nexthop is dead (used by multipath) */
5 #define RTNH_F_PERVASIVE 2 /* Do recursive gateway lookup */
6 #define RTNH_F_ONLINK 4 /* Gateway is forced on link */
7 +#define RTNH_F_SUSPECT 8 /* We don't know the real state */
8 +#define RTNH_F_BADSTATE (RTNH_F_DEAD | RTNH_F_SUSPECT)
10 /* Macros to handle hexthops */
12 --- a/include/net/flow.h
13 +++ b/include/net/flow.h
14 @@ -19,6 +19,8 @@ struct flowi {
23 @@ -43,6 +45,8 @@ struct flowi {
24 #define fl6_flowlabel nl_u.ip6_u.flowlabel
25 #define fl4_dst nl_u.ip4_u.daddr
26 #define fl4_src nl_u.ip4_u.saddr
27 +#define fl4_lsrc nl_u.ip4_u.lsrc
28 +#define fl4_gw nl_u.ip4_u.gw
29 #define fl4_tos nl_u.ip4_u.tos
30 #define fl4_scope nl_u.ip4_u.scope
32 --- a/net/ipv4/route.c
33 +++ b/net/ipv4/route.c
34 @@ -1116,6 +1116,7 @@ void ip_rt_redirect(__be32 old_gw, __be3
36 /* Gateway is different ... */
37 rt->rt_gateway = new_gw;
38 + if (rt->fl.fl4_gw) rt->fl.fl4_gw = new_gw;
40 /* Redirect received -> path was valid */
41 dst_confirm(&rth->u.dst);
42 @@ -1551,6 +1552,7 @@ static int ip_route_input_mc(struct sk_b
43 rth->fl.fl4_tos = tos;
44 rth->fl.mark = skb->mark;
45 rth->fl.fl4_src = saddr;
46 + rth->fl.fl4_lsrc = 0;
48 #ifdef CONFIG_NET_CLS_ROUTE
49 rth->u.dst.tclassid = itag;
50 @@ -1561,6 +1563,7 @@ static int ip_route_input_mc(struct sk_b
51 dev_hold(rth->u.dst.dev);
52 rth->idev = in_dev_get(rth->u.dst.dev);
55 rth->rt_gateway = daddr;
56 rth->rt_spec_dst= spec_dst;
57 rth->rt_type = RTN_MULTICAST;
58 @@ -1624,7 +1627,7 @@ static void ip_handle_martian_source(str
59 static inline int __mkroute_input(struct sk_buff *skb,
60 struct fib_result* res,
61 struct in_device *in_dev,
62 - __be32 daddr, __be32 saddr, u32 tos,
63 + __be32 daddr, __be32 saddr, u32 tos, u32 lsrc,
64 struct rtable **result)
67 @@ -1659,6 +1662,7 @@ static inline int __mkroute_input(struct
68 flags |= RTCF_DIRECTSRC;
70 if (out_dev == in_dev && err && !(flags & (RTCF_NAT | RTCF_MASQ)) &&
72 (IN_DEV_SHARED_MEDIA(out_dev) ||
73 inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
74 flags |= RTCF_DOREDIRECT;
75 @@ -1692,6 +1696,7 @@ static inline int __mkroute_input(struct
76 rth->fl.mark = skb->mark;
77 rth->fl.fl4_src = saddr;
79 + rth->fl.fl4_lsrc = lsrc;
80 rth->rt_gateway = daddr;
82 rth->fl.iif = in_dev->dev->ifindex;
83 @@ -1699,6 +1704,7 @@ static inline int __mkroute_input(struct
84 dev_hold(rth->u.dst.dev);
85 rth->idev = in_dev_get(rth->u.dst.dev);
88 rth->rt_spec_dst= spec_dst;
90 rth->u.dst.input = ip_forward;
91 @@ -1720,19 +1726,21 @@ static inline int ip_mkroute_input(struc
92 struct fib_result* res,
93 const struct flowi *fl,
94 struct in_device *in_dev,
95 - __be32 daddr, __be32 saddr, u32 tos)
96 + __be32 daddr, __be32 saddr, u32 tos,
99 struct rtable* rth = NULL;
103 + fib_select_default(fl, res);
104 #ifdef CONFIG_IP_ROUTE_MULTIPATH
105 - if (res->fi && res->fi->fib_nhs > 1 && fl->oif == 0)
106 + if (res->fi && res->fi->fib_nhs > 1)
107 fib_select_multipath(fl, res);
110 /* create a routing cache entry */
111 - err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth);
112 + err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, lsrc, &rth);
116 @@ -1752,18 +1760,18 @@ static inline int ip_mkroute_input(struc
119 static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
120 - u8 tos, struct net_device *dev)
121 + u8 tos, struct net_device *dev, u32 lsrc)
123 struct fib_result res;
124 struct in_device *in_dev = in_dev_get(dev);
125 struct flowi fl = { .nl_u = { .ip4_u =
128 + .saddr = lsrc ? : saddr,
130 .scope = RT_SCOPE_UNIVERSE,
133 - .iif = dev->ifindex };
134 + .iif = lsrc? loopback_dev.ifindex : dev->ifindex };
138 @@ -1796,6 +1804,12 @@ static int ip_route_input_slow(struct sk
139 if (BADCLASS(daddr) || ZERONET(daddr) || LOOPBACK(daddr))
140 goto martian_destination;
143 + if (MULTICAST(lsrc) || BADCLASS(lsrc) ||
144 + ZERONET(lsrc) || LOOPBACK(lsrc))
149 * Now we are ready to route packet.
151 @@ -1805,6 +1819,10 @@ static int ip_route_input_slow(struct sk
155 + if (lsrc && res.type != RTN_UNICAST && res.type != RTN_NAT)
157 + fl.iif = dev->ifindex;
158 + fl.fl4_src = saddr;
160 RT_CACHE_STAT_INC(in_slow_tot);
162 @@ -1829,7 +1847,7 @@ static int ip_route_input_slow(struct sk
163 if (res.type != RTN_UNICAST)
164 goto martian_destination;
166 - err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos);
167 + err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos, lsrc);
171 @@ -1844,6 +1862,8 @@ out: return err;
173 if (skb->protocol != htons(ETH_P_IP))
179 spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
180 @@ -1884,6 +1904,7 @@ local_input:
181 rth->u.dst.dev = &loopback_dev;
182 dev_hold(rth->u.dst.dev);
183 rth->idev = in_dev_get(rth->u.dst.dev);
184 + rth->fl.fl4_gw = 0;
185 rth->rt_gateway = daddr;
186 rth->rt_spec_dst= spec_dst;
187 rth->u.dst.input= ip_local_deliver;
188 @@ -1933,8 +1954,9 @@ martian_source:
192 -int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr,
193 - u8 tos, struct net_device *dev)
195 +ip_route_input_cached(struct sk_buff *skb, __be32 daddr, __be32 saddr,
196 + u8 tos, struct net_device *dev, u32 lsrc)
200 @@ -1949,6 +1971,7 @@ int ip_route_input(struct sk_buff *skb,
201 if (rth->fl.fl4_dst == daddr &&
202 rth->fl.fl4_src == saddr &&
203 rth->fl.iif == iif &&
204 + rth->fl.fl4_lsrc == lsrc &&
206 rth->fl.mark == skb->mark &&
207 rth->fl.fl4_tos == tos) {
208 @@ -1995,7 +2018,19 @@ int ip_route_input(struct sk_buff *skb,
212 - return ip_route_input_slow(skb, daddr, saddr, tos, dev);
213 + return ip_route_input_slow(skb, daddr, saddr, tos, dev, lsrc);
216 +int ip_route_input(struct sk_buff *skb, u32 daddr, u32 saddr,
217 + u8 tos, struct net_device *dev)
219 + return ip_route_input_cached(skb, daddr, saddr, tos, dev, 0);
222 +int ip_route_input_lookup(struct sk_buff *skb, u32 daddr, u32 saddr,
223 + u8 tos, struct net_device *dev, u32 lsrc)
225 + return ip_route_input_cached(skb, daddr, saddr, tos, dev, lsrc);
228 static inline int __mkroute_output(struct rtable **result,
229 @@ -2067,6 +2102,7 @@ static inline int __mkroute_output(struc
230 rth->fl.fl4_tos = tos;
231 rth->fl.fl4_src = oldflp->fl4_src;
232 rth->fl.oif = oldflp->oif;
233 + rth->fl.fl4_gw = oldflp->fl4_gw;
234 rth->fl.mark = oldflp->mark;
235 rth->rt_dst = fl->fl4_dst;
236 rth->rt_src = fl->fl4_src;
237 @@ -2145,6 +2181,7 @@ static int ip_route_output_slow(struct r
238 struct flowi fl = { .nl_u = { .ip4_u =
239 { .daddr = oldflp->fl4_dst,
240 .saddr = oldflp->fl4_src,
241 + .gw = oldflp->fl4_gw,
242 .tos = tos & IPTOS_RT_MASK,
243 .scope = ((tos & RTO_ONLINK) ?
245 @@ -2248,6 +2285,7 @@ static int ip_route_output_slow(struct r
246 dev_out = &loopback_dev;
248 fl.oif = loopback_dev.ifindex;
250 res.type = RTN_LOCAL;
253 @@ -2255,7 +2293,7 @@ static int ip_route_output_slow(struct r
255 if (fib_lookup(&fl, &res)) {
258 + if (oldflp->oif && dev_out->flags & IFF_UP) {
259 /* Apparently, routing tables are wrong. Assume,
260 that the destination is on link.
262 @@ -2295,6 +2333,7 @@ static int ip_route_output_slow(struct r
263 dev_out = &loopback_dev;
265 fl.oif = dev_out->ifindex;
268 fib_info_put(res.fi);
270 @@ -2302,13 +2341,12 @@ static int ip_route_output_slow(struct r
274 + if (res.type == RTN_UNICAST)
275 + fib_select_default(&fl, &res);
276 #ifdef CONFIG_IP_ROUTE_MULTIPATH
277 - if (res.fi->fib_nhs > 1 && fl.oif == 0)
278 + if (res.fi->fib_nhs > 1)
279 fib_select_multipath(&fl, &res);
282 - if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
283 - fib_select_default(&fl, &res);
286 fl.fl4_src = FIB_RES_PREFSRC(res);
287 @@ -2345,6 +2383,7 @@ int __ip_route_output_key(struct rtable
288 rth->fl.fl4_src == flp->fl4_src &&
290 rth->fl.oif == flp->oif &&
291 + rth->fl.fl4_gw == flp->fl4_gw &&
292 rth->fl.mark == flp->mark &&
293 !((rth->fl.fl4_tos ^ flp->fl4_tos) &
294 (IPTOS_RT_MASK | RTO_ONLINK))) {
295 @@ -3030,3 +3069,4 @@ int __init ip_rt_init(void)
296 EXPORT_SYMBOL(__ip_select_ident);
297 EXPORT_SYMBOL(ip_route_input);
298 EXPORT_SYMBOL(ip_route_output_key);
299 +EXPORT_SYMBOL(ip_route_input_lookup);