201-multiple_default_gateways.patch 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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)
  9. /* Macros to handle hexthops */
  10. --- a/include/net/flow.h
  11. +++ b/include/net/flow.h
  12. @@ -19,6 +19,8 @@ struct flowi {
  13. struct {
  14. __be32 daddr;
  15. __be32 saddr;
  16. + __u32 lsrc;
  17. + __u32 gw;
  18. __u8 tos;
  19. __u8 scope;
  20. } ip4_u;
  21. @@ -43,6 +45,8 @@ struct flowi {
  22. #define fl6_flowlabel nl_u.ip6_u.flowlabel
  23. #define fl4_dst nl_u.ip4_u.daddr
  24. #define fl4_src nl_u.ip4_u.saddr
  25. +#define fl4_lsrc nl_u.ip4_u.lsrc
  26. +#define fl4_gw nl_u.ip4_u.gw
  27. #define fl4_tos nl_u.ip4_u.tos
  28. #define fl4_scope nl_u.ip4_u.scope
  29. --- a/net/ipv4/route.c
  30. +++ b/net/ipv4/route.c
  31. @@ -1116,6 +1116,7 @@ void ip_rt_redirect(__be32 old_gw, __be3
  32. /* Gateway is different ... */
  33. rt->rt_gateway = new_gw;
  34. + if (rt->fl.fl4_gw) rt->fl.fl4_gw = new_gw;
  35. /* Redirect received -> path was valid */
  36. dst_confirm(&rth->u.dst);
  37. @@ -1551,6 +1552,7 @@ static int ip_route_input_mc(struct sk_b
  38. rth->fl.fl4_tos = tos;
  39. rth->fl.mark = skb->mark;
  40. rth->fl.fl4_src = saddr;
  41. + rth->fl.fl4_lsrc = 0;
  42. rth->rt_src = saddr;
  43. #ifdef CONFIG_NET_CLS_ROUTE
  44. rth->u.dst.tclassid = itag;
  45. @@ -1561,6 +1563,7 @@ static int ip_route_input_mc(struct sk_b
  46. dev_hold(rth->u.dst.dev);
  47. rth->idev = in_dev_get(rth->u.dst.dev);
  48. rth->fl.oif = 0;
  49. + rth->fl.fl4_gw = 0;
  50. rth->rt_gateway = daddr;
  51. rth->rt_spec_dst= spec_dst;
  52. rth->rt_type = RTN_MULTICAST;
  53. @@ -1624,7 +1627,7 @@ static void ip_handle_martian_source(str
  54. static inline int __mkroute_input(struct sk_buff *skb,
  55. struct fib_result* res,
  56. struct in_device *in_dev,
  57. - __be32 daddr, __be32 saddr, u32 tos,
  58. + __be32 daddr, __be32 saddr, u32 tos, u32 lsrc,
  59. struct rtable **result)
  60. {
  61. @@ -1659,6 +1662,7 @@ static inline int __mkroute_input(struct
  62. flags |= RTCF_DIRECTSRC;
  63. if (out_dev == in_dev && err && !(flags & (RTCF_NAT | RTCF_MASQ)) &&
  64. + !lsrc &&
  65. (IN_DEV_SHARED_MEDIA(out_dev) ||
  66. inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
  67. flags |= RTCF_DOREDIRECT;
  68. @@ -1692,6 +1696,7 @@ static inline int __mkroute_input(struct
  69. rth->fl.mark = skb->mark;
  70. rth->fl.fl4_src = saddr;
  71. rth->rt_src = saddr;
  72. + rth->fl.fl4_lsrc = lsrc;
  73. rth->rt_gateway = daddr;
  74. rth->rt_iif =
  75. rth->fl.iif = in_dev->dev->ifindex;
  76. @@ -1699,6 +1704,7 @@ static inline int __mkroute_input(struct
  77. dev_hold(rth->u.dst.dev);
  78. rth->idev = in_dev_get(rth->u.dst.dev);
  79. rth->fl.oif = 0;
  80. + rth->fl.fl4_gw = 0;
  81. rth->rt_spec_dst= spec_dst;
  82. rth->u.dst.input = ip_forward;
  83. @@ -1720,19 +1726,21 @@ static inline int ip_mkroute_input(struc
  84. struct fib_result* res,
  85. const struct flowi *fl,
  86. struct in_device *in_dev,
  87. - __be32 daddr, __be32 saddr, u32 tos)
  88. + __be32 daddr, __be32 saddr, u32 tos,
  89. + u32 lsrc)
  90. {
  91. struct rtable* rth = NULL;
  92. int err;
  93. unsigned hash;
  94. + fib_select_default(fl, res);
  95. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  96. - if (res->fi && res->fi->fib_nhs > 1 && fl->oif == 0)
  97. + if (res->fi && res->fi->fib_nhs > 1)
  98. fib_select_multipath(fl, res);
  99. #endif
  100. /* create a routing cache entry */
  101. - err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth);
  102. + err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, lsrc, &rth);
  103. if (err)
  104. return err;
  105. @@ -1752,18 +1760,18 @@ static inline int ip_mkroute_input(struc
  106. */
  107. static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  108. - u8 tos, struct net_device *dev)
  109. + u8 tos, struct net_device *dev, u32 lsrc)
  110. {
  111. struct fib_result res;
  112. struct in_device *in_dev = in_dev_get(dev);
  113. struct flowi fl = { .nl_u = { .ip4_u =
  114. { .daddr = daddr,
  115. - .saddr = saddr,
  116. + .saddr = lsrc ? : saddr,
  117. .tos = tos,
  118. .scope = RT_SCOPE_UNIVERSE,
  119. } },
  120. .mark = skb->mark,
  121. - .iif = dev->ifindex };
  122. + .iif = lsrc? loopback_dev.ifindex : dev->ifindex };
  123. unsigned flags = 0;
  124. u32 itag = 0;
  125. struct rtable * rth;
  126. @@ -1796,6 +1804,12 @@ static int ip_route_input_slow(struct sk
  127. if (BADCLASS(daddr) || ZERONET(daddr) || LOOPBACK(daddr))
  128. goto martian_destination;
  129. + if (lsrc) {
  130. + if (MULTICAST(lsrc) || BADCLASS(lsrc) ||
  131. + ZERONET(lsrc) || LOOPBACK(lsrc))
  132. + goto e_inval;
  133. + }
  134. +
  135. /*
  136. * Now we are ready to route packet.
  137. */
  138. @@ -1805,6 +1819,10 @@ static int ip_route_input_slow(struct sk
  139. goto no_route;
  140. }
  141. free_res = 1;
  142. + if (lsrc && res.type != RTN_UNICAST && res.type != RTN_NAT)
  143. + goto e_inval;
  144. + fl.iif = dev->ifindex;
  145. + fl.fl4_src = saddr;
  146. RT_CACHE_STAT_INC(in_slow_tot);
  147. @@ -1829,7 +1847,7 @@ static int ip_route_input_slow(struct sk
  148. if (res.type != RTN_UNICAST)
  149. goto martian_destination;
  150. - err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos);
  151. + err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos, lsrc);
  152. if (err == -ENOBUFS)
  153. goto e_nobufs;
  154. if (err == -EINVAL)
  155. @@ -1844,6 +1862,8 @@ out: return err;
  156. brd_input:
  157. if (skb->protocol != htons(ETH_P_IP))
  158. goto e_inval;
  159. + if (lsrc)
  160. + goto e_inval;
  161. if (ZERONET(saddr))
  162. spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  163. @@ -1884,6 +1904,7 @@ local_input:
  164. rth->u.dst.dev = &loopback_dev;
  165. dev_hold(rth->u.dst.dev);
  166. rth->idev = in_dev_get(rth->u.dst.dev);
  167. + rth->fl.fl4_gw = 0;
  168. rth->rt_gateway = daddr;
  169. rth->rt_spec_dst= spec_dst;
  170. rth->u.dst.input= ip_local_deliver;
  171. @@ -1933,8 +1954,9 @@ martian_source:
  172. goto e_inval;
  173. }
  174. -int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  175. - u8 tos, struct net_device *dev)
  176. +static inline int
  177. +ip_route_input_cached(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  178. + u8 tos, struct net_device *dev, u32 lsrc)
  179. {
  180. struct rtable * rth;
  181. unsigned hash;
  182. @@ -1949,6 +1971,7 @@ int ip_route_input(struct sk_buff *skb,
  183. if (rth->fl.fl4_dst == daddr &&
  184. rth->fl.fl4_src == saddr &&
  185. rth->fl.iif == iif &&
  186. + rth->fl.fl4_lsrc == lsrc &&
  187. rth->fl.oif == 0 &&
  188. rth->fl.mark == skb->mark &&
  189. rth->fl.fl4_tos == tos) {
  190. @@ -1995,7 +2018,19 @@ int ip_route_input(struct sk_buff *skb,
  191. rcu_read_unlock();
  192. return -EINVAL;
  193. }
  194. - return ip_route_input_slow(skb, daddr, saddr, tos, dev);
  195. + return ip_route_input_slow(skb, daddr, saddr, tos, dev, lsrc);
  196. +}
  197. +
  198. +int ip_route_input(struct sk_buff *skb, u32 daddr, u32 saddr,
  199. + u8 tos, struct net_device *dev)
  200. +{
  201. + return ip_route_input_cached(skb, daddr, saddr, tos, dev, 0);
  202. +}
  203. +
  204. +int ip_route_input_lookup(struct sk_buff *skb, u32 daddr, u32 saddr,
  205. + u8 tos, struct net_device *dev, u32 lsrc)
  206. +{
  207. + return ip_route_input_cached(skb, daddr, saddr, tos, dev, lsrc);
  208. }
  209. static inline int __mkroute_output(struct rtable **result,
  210. @@ -2067,6 +2102,7 @@ static inline int __mkroute_output(struc
  211. rth->fl.fl4_tos = tos;
  212. rth->fl.fl4_src = oldflp->fl4_src;
  213. rth->fl.oif = oldflp->oif;
  214. + rth->fl.fl4_gw = oldflp->fl4_gw;
  215. rth->fl.mark = oldflp->mark;
  216. rth->rt_dst = fl->fl4_dst;
  217. rth->rt_src = fl->fl4_src;
  218. @@ -2145,6 +2181,7 @@ static int ip_route_output_slow(struct r
  219. struct flowi fl = { .nl_u = { .ip4_u =
  220. { .daddr = oldflp->fl4_dst,
  221. .saddr = oldflp->fl4_src,
  222. + .gw = oldflp->fl4_gw,
  223. .tos = tos & IPTOS_RT_MASK,
  224. .scope = ((tos & RTO_ONLINK) ?
  225. RT_SCOPE_LINK :
  226. @@ -2248,6 +2285,7 @@ static int ip_route_output_slow(struct r
  227. dev_out = &loopback_dev;
  228. dev_hold(dev_out);
  229. fl.oif = loopback_dev.ifindex;
  230. + fl.fl4_gw = 0;
  231. res.type = RTN_LOCAL;
  232. flags |= RTCF_LOCAL;
  233. goto make_route;
  234. @@ -2255,7 +2293,7 @@ static int ip_route_output_slow(struct r
  235. if (fib_lookup(&fl, &res)) {
  236. res.fi = NULL;
  237. - if (oldflp->oif) {
  238. + if (oldflp->oif && dev_out->flags & IFF_UP) {
  239. /* Apparently, routing tables are wrong. Assume,
  240. that the destination is on link.
  241. @@ -2295,6 +2333,7 @@ static int ip_route_output_slow(struct r
  242. dev_out = &loopback_dev;
  243. dev_hold(dev_out);
  244. fl.oif = dev_out->ifindex;
  245. + fl.fl4_gw = 0;
  246. if (res.fi)
  247. fib_info_put(res.fi);
  248. res.fi = NULL;
  249. @@ -2302,13 +2341,12 @@ static int ip_route_output_slow(struct r
  250. goto make_route;
  251. }
  252. + if (res.type == RTN_UNICAST)
  253. + fib_select_default(&fl, &res);
  254. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  255. - if (res.fi->fib_nhs > 1 && fl.oif == 0)
  256. + if (res.fi->fib_nhs > 1)
  257. fib_select_multipath(&fl, &res);
  258. - else
  259. #endif
  260. - if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
  261. - fib_select_default(&fl, &res);
  262. if (!fl.fl4_src)
  263. fl.fl4_src = FIB_RES_PREFSRC(res);
  264. @@ -2345,6 +2383,7 @@ int __ip_route_output_key(struct rtable
  265. rth->fl.fl4_src == flp->fl4_src &&
  266. rth->fl.iif == 0 &&
  267. rth->fl.oif == flp->oif &&
  268. + rth->fl.fl4_gw == flp->fl4_gw &&
  269. rth->fl.mark == flp->mark &&
  270. !((rth->fl.fl4_tos ^ flp->fl4_tos) &
  271. (IPTOS_RT_MASK | RTO_ONLINK))) {
  272. @@ -3030,3 +3069,4 @@ int __init ip_rt_init(void)
  273. EXPORT_SYMBOL(__ip_select_ident);
  274. EXPORT_SYMBOL(ip_route_input);
  275. EXPORT_SYMBOL(ip_route_output_key);
  276. +EXPORT_SYMBOL(ip_route_input_lookup);