201-multiple_default_gateways.patch 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. --- a/include/linux/rtnetlink.h
  2. +++ b/include/linux/rtnetlink.h
  3. @@ -293,6 +293,8 @@
  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 @@
  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 @@
  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. @@ -1208,6 +1208,7 @@
  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. @@ -1643,6 +1644,7 @@
  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. @@ -1653,6 +1655,7 @@
  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. @@ -1716,7 +1719,7 @@
  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. @@ -1751,6 +1754,7 @@
  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. @@ -1788,6 +1792,7 @@
  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. @@ -1795,6 +1800,7 @@
  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. @@ -1816,19 +1822,21 @@
  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. @@ -1841,7 +1849,8 @@
  106. struct fib_result* res,
  107. const struct flowi *fl,
  108. struct in_device *in_dev,
  109. - __be32 daddr, __be32 saddr, u32 tos)
  110. + __be32 daddr, __be32 saddr, u32 tos,
  111. + u32 lsrc)
  112. {
  113. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  114. struct rtable* rth = NULL, *rtres;
  115. @@ -1857,7 +1866,7 @@
  116. /* distinguish between multipath and singlepath */
  117. if (hopcount < 2)
  118. return ip_mkroute_input_def(skb, res, fl, in_dev, daddr,
  119. - saddr, tos);
  120. + saddr, tos, 0);
  121. /* add all alternatives to the routing cache */
  122. for (hop = 0; hop < hopcount; hop++) {
  123. @@ -1869,7 +1878,7 @@
  124. /* create a routing cache entry */
  125. err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos,
  126. - &rth);
  127. + 0, &rth);
  128. if (err)
  129. return err;
  130. @@ -1889,7 +1898,7 @@
  131. skb->dst = &rtres->u.dst;
  132. return err;
  133. #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  134. - return ip_mkroute_input_def(skb, res, fl, in_dev, daddr, saddr, tos);
  135. + return ip_mkroute_input_def(skb, res, fl, in_dev, daddr, saddr, tos, lsrc);
  136. #endif /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  137. }
  138. @@ -1905,18 +1914,18 @@
  139. */
  140. static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  141. - u8 tos, struct net_device *dev)
  142. + u8 tos, struct net_device *dev, u32 lsrc)
  143. {
  144. struct fib_result res;
  145. struct in_device *in_dev = in_dev_get(dev);
  146. struct flowi fl = { .nl_u = { .ip4_u =
  147. { .daddr = daddr,
  148. - .saddr = saddr,
  149. + .saddr = lsrc ? : saddr,
  150. .tos = tos,
  151. .scope = RT_SCOPE_UNIVERSE,
  152. } },
  153. .mark = skb->mark,
  154. - .iif = dev->ifindex };
  155. + .iif = lsrc? loopback_dev.ifindex : dev->ifindex };
  156. unsigned flags = 0;
  157. u32 itag = 0;
  158. struct rtable * rth;
  159. @@ -1949,6 +1958,12 @@
  160. if (BADCLASS(daddr) || ZERONET(daddr) || LOOPBACK(daddr))
  161. goto martian_destination;
  162. + if (lsrc) {
  163. + if (MULTICAST(lsrc) || BADCLASS(lsrc) ||
  164. + ZERONET(lsrc) || LOOPBACK(lsrc))
  165. + goto e_inval;
  166. + }
  167. +
  168. /*
  169. * Now we are ready to route packet.
  170. */
  171. @@ -1958,6 +1973,10 @@
  172. goto no_route;
  173. }
  174. free_res = 1;
  175. + if (lsrc && res.type != RTN_UNICAST && res.type != RTN_NAT)
  176. + goto e_inval;
  177. + fl.iif = dev->ifindex;
  178. + fl.fl4_src = saddr;
  179. RT_CACHE_STAT_INC(in_slow_tot);
  180. @@ -1982,7 +2001,7 @@
  181. if (res.type != RTN_UNICAST)
  182. goto martian_destination;
  183. - err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos);
  184. + err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos, lsrc);
  185. if (err == -ENOBUFS)
  186. goto e_nobufs;
  187. if (err == -EINVAL)
  188. @@ -1997,6 +2016,8 @@
  189. brd_input:
  190. if (skb->protocol != htons(ETH_P_IP))
  191. goto e_inval;
  192. + if (lsrc)
  193. + goto e_inval;
  194. if (ZERONET(saddr))
  195. spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  196. @@ -2037,6 +2058,7 @@
  197. rth->u.dst.dev = &loopback_dev;
  198. dev_hold(rth->u.dst.dev);
  199. rth->idev = in_dev_get(rth->u.dst.dev);
  200. + rth->fl.fl4_gw = 0;
  201. rth->rt_gateway = daddr;
  202. rth->rt_spec_dst= spec_dst;
  203. rth->u.dst.input= ip_local_deliver;
  204. @@ -2086,8 +2108,9 @@
  205. goto e_inval;
  206. }
  207. -int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  208. - u8 tos, struct net_device *dev)
  209. +static inline int
  210. +ip_route_input_cached(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  211. + u8 tos, struct net_device *dev, u32 lsrc)
  212. {
  213. struct rtable * rth;
  214. unsigned hash;
  215. @@ -2102,6 +2125,7 @@
  216. if (rth->fl.fl4_dst == daddr &&
  217. rth->fl.fl4_src == saddr &&
  218. rth->fl.iif == iif &&
  219. + rth->fl.fl4_lsrc == lsrc &&
  220. rth->fl.oif == 0 &&
  221. rth->fl.mark == skb->mark &&
  222. rth->fl.fl4_tos == tos) {
  223. @@ -2148,7 +2172,19 @@
  224. rcu_read_unlock();
  225. return -EINVAL;
  226. }
  227. - return ip_route_input_slow(skb, daddr, saddr, tos, dev);
  228. + return ip_route_input_slow(skb, daddr, saddr, tos, dev, lsrc);
  229. +}
  230. +
  231. +int ip_route_input(struct sk_buff *skb, u32 daddr, u32 saddr,
  232. + u8 tos, struct net_device *dev)
  233. +{
  234. + return ip_route_input_cached(skb, daddr, saddr, tos, dev, 0);
  235. +}
  236. +
  237. +int ip_route_input_lookup(struct sk_buff *skb, u32 daddr, u32 saddr,
  238. + u8 tos, struct net_device *dev, u32 lsrc)
  239. +{
  240. + return ip_route_input_cached(skb, daddr, saddr, tos, dev, lsrc);
  241. }
  242. static inline int __mkroute_output(struct rtable **result,
  243. @@ -2227,6 +2263,7 @@
  244. rth->fl.fl4_tos = tos;
  245. rth->fl.fl4_src = oldflp->fl4_src;
  246. rth->fl.oif = oldflp->oif;
  247. + rth->fl.fl4_gw = oldflp->fl4_gw;
  248. rth->fl.mark = oldflp->mark;
  249. rth->rt_dst = fl->fl4_dst;
  250. rth->rt_src = fl->fl4_src;
  251. @@ -2367,6 +2404,7 @@
  252. struct flowi fl = { .nl_u = { .ip4_u =
  253. { .daddr = oldflp->fl4_dst,
  254. .saddr = oldflp->fl4_src,
  255. + .gw = oldflp->fl4_gw,
  256. .tos = tos & IPTOS_RT_MASK,
  257. .scope = ((tos & RTO_ONLINK) ?
  258. RT_SCOPE_LINK :
  259. @@ -2470,6 +2508,7 @@
  260. dev_out = &loopback_dev;
  261. dev_hold(dev_out);
  262. fl.oif = loopback_dev.ifindex;
  263. + fl.fl4_gw = 0;
  264. res.type = RTN_LOCAL;
  265. flags |= RTCF_LOCAL;
  266. goto make_route;
  267. @@ -2477,7 +2516,7 @@
  268. if (fib_lookup(&fl, &res)) {
  269. res.fi = NULL;
  270. - if (oldflp->oif) {
  271. + if (oldflp->oif && dev_out->flags & IFF_UP) {
  272. /* Apparently, routing tables are wrong. Assume,
  273. that the destination is on link.
  274. @@ -2517,6 +2556,7 @@
  275. dev_out = &loopback_dev;
  276. dev_hold(dev_out);
  277. fl.oif = dev_out->ifindex;
  278. + fl.fl4_gw = 0;
  279. if (res.fi)
  280. fib_info_put(res.fi);
  281. res.fi = NULL;
  282. @@ -2524,13 +2564,12 @@
  283. goto make_route;
  284. }
  285. + if (res.type == RTN_UNICAST)
  286. + fib_select_default(&fl, &res);
  287. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  288. - if (res.fi->fib_nhs > 1 && fl.oif == 0)
  289. + if (res.fi->fib_nhs > 1)
  290. fib_select_multipath(&fl, &res);
  291. - else
  292. #endif
  293. - if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
  294. - fib_select_default(&fl, &res);
  295. if (!fl.fl4_src)
  296. fl.fl4_src = FIB_RES_PREFSRC(res);
  297. @@ -2567,6 +2606,7 @@
  298. rth->fl.fl4_src == flp->fl4_src &&
  299. rth->fl.iif == 0 &&
  300. rth->fl.oif == flp->oif &&
  301. + rth->fl.fl4_gw == flp->fl4_gw &&
  302. rth->fl.mark == flp->mark &&
  303. !((rth->fl.fl4_tos ^ flp->fl4_tos) &
  304. (IPTOS_RT_MASK | RTO_ONLINK))) {
  305. @@ -3267,3 +3307,4 @@
  306. EXPORT_SYMBOL(__ip_select_ident);
  307. EXPORT_SYMBOL(ip_route_input);
  308. EXPORT_SYMBOL(ip_route_output_key);
  309. +EXPORT_SYMBOL(ip_route_input_lookup);