201-multiple_default_gateways.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. Index: linux-2.6.21.7/include/linux/netfilter_ipv4/ip_nat.h
  2. ===================================================================
  3. --- linux-2.6.21.7.orig/include/linux/netfilter_ipv4/ip_nat.h
  4. +++ linux-2.6.21.7/include/linux/netfilter_ipv4/ip_nat.h
  5. @@ -64,6 +64,13 @@ struct ip_nat_info
  6. struct ip_conntrack;
  7. +/* Call input routing for SNAT-ed traffic */
  8. +extern unsigned int ip_nat_route_input(unsigned int hooknum,
  9. + struct sk_buff **pskb,
  10. + const struct net_device *in,
  11. + const struct net_device *out,
  12. + int (*okfn)(struct sk_buff *));
  13. +
  14. /* Set up the info structure to map into this range. */
  15. extern unsigned int ip_nat_setup_info(struct ip_conntrack *conntrack,
  16. const struct ip_nat_range *range,
  17. Index: linux-2.6.21.7/include/linux/rtnetlink.h
  18. ===================================================================
  19. --- linux-2.6.21.7.orig/include/linux/rtnetlink.h
  20. +++ linux-2.6.21.7/include/linux/rtnetlink.h
  21. @@ -293,6 +293,8 @@ struct rtnexthop
  22. #define RTNH_F_DEAD 1 /* Nexthop is dead (used by multipath) */
  23. #define RTNH_F_PERVASIVE 2 /* Do recursive gateway lookup */
  24. #define RTNH_F_ONLINK 4 /* Gateway is forced on link */
  25. +#define RTNH_F_SUSPECT 8 /* We don't know the real state */
  26. +#define RTNH_F_BADSTATE (RTNH_F_DEAD | RTNH_F_SUSPECT)
  27. /* Macros to handle hexthops */
  28. Index: linux-2.6.21.7/include/net/flow.h
  29. ===================================================================
  30. --- linux-2.6.21.7.orig/include/net/flow.h
  31. +++ linux-2.6.21.7/include/net/flow.h
  32. @@ -19,6 +19,8 @@ struct flowi {
  33. struct {
  34. __be32 daddr;
  35. __be32 saddr;
  36. + __u32 lsrc;
  37. + __u32 gw;
  38. __u8 tos;
  39. __u8 scope;
  40. } ip4_u;
  41. @@ -43,6 +45,8 @@ struct flowi {
  42. #define fl6_flowlabel nl_u.ip6_u.flowlabel
  43. #define fl4_dst nl_u.ip4_u.daddr
  44. #define fl4_src nl_u.ip4_u.saddr
  45. +#define fl4_lsrc nl_u.ip4_u.lsrc
  46. +#define fl4_gw nl_u.ip4_u.gw
  47. #define fl4_tos nl_u.ip4_u.tos
  48. #define fl4_scope nl_u.ip4_u.scope
  49. Index: linux-2.6.21.7/net/ipv4/route.c
  50. ===================================================================
  51. --- linux-2.6.21.7.orig/net/ipv4/route.c
  52. +++ linux-2.6.21.7/net/ipv4/route.c
  53. @@ -1208,6 +1208,7 @@ void ip_rt_redirect(__be32 old_gw, __be3
  54. /* Gateway is different ... */
  55. rt->rt_gateway = new_gw;
  56. + if (rt->fl.fl4_gw) rt->fl.fl4_gw = new_gw;
  57. /* Redirect received -> path was valid */
  58. dst_confirm(&rth->u.dst);
  59. @@ -1643,6 +1644,7 @@ static int ip_route_input_mc(struct sk_b
  60. rth->fl.fl4_tos = tos;
  61. rth->fl.mark = skb->mark;
  62. rth->fl.fl4_src = saddr;
  63. + rth->fl.fl4_lsrc = 0;
  64. rth->rt_src = saddr;
  65. #ifdef CONFIG_NET_CLS_ROUTE
  66. rth->u.dst.tclassid = itag;
  67. @@ -1653,6 +1655,7 @@ static int ip_route_input_mc(struct sk_b
  68. dev_hold(rth->u.dst.dev);
  69. rth->idev = in_dev_get(rth->u.dst.dev);
  70. rth->fl.oif = 0;
  71. + rth->fl.fl4_gw = 0;
  72. rth->rt_gateway = daddr;
  73. rth->rt_spec_dst= spec_dst;
  74. rth->rt_type = RTN_MULTICAST;
  75. @@ -1716,7 +1719,7 @@ static void ip_handle_martian_source(str
  76. static inline int __mkroute_input(struct sk_buff *skb,
  77. struct fib_result* res,
  78. struct in_device *in_dev,
  79. - __be32 daddr, __be32 saddr, u32 tos,
  80. + __be32 daddr, __be32 saddr, u32 tos, u32 lsrc,
  81. struct rtable **result)
  82. {
  83. @@ -1751,6 +1754,7 @@ static inline int __mkroute_input(struct
  84. flags |= RTCF_DIRECTSRC;
  85. if (out_dev == in_dev && err && !(flags & (RTCF_NAT | RTCF_MASQ)) &&
  86. + !lsrc &&
  87. (IN_DEV_SHARED_MEDIA(out_dev) ||
  88. inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
  89. flags |= RTCF_DOREDIRECT;
  90. @@ -1788,6 +1792,7 @@ static inline int __mkroute_input(struct
  91. rth->fl.mark = skb->mark;
  92. rth->fl.fl4_src = saddr;
  93. rth->rt_src = saddr;
  94. + rth->fl.fl4_lsrc = lsrc;
  95. rth->rt_gateway = daddr;
  96. rth->rt_iif =
  97. rth->fl.iif = in_dev->dev->ifindex;
  98. @@ -1795,6 +1800,7 @@ static inline int __mkroute_input(struct
  99. dev_hold(rth->u.dst.dev);
  100. rth->idev = in_dev_get(rth->u.dst.dev);
  101. rth->fl.oif = 0;
  102. + rth->fl.fl4_gw = 0;
  103. rth->rt_spec_dst= spec_dst;
  104. rth->u.dst.input = ip_forward;
  105. @@ -1816,19 +1822,21 @@ static inline int ip_mkroute_input_def(s
  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. struct rtable* rth = NULL;
  114. int err;
  115. unsigned hash;
  116. + fib_select_default(fl, res);
  117. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  118. - if (res->fi && res->fi->fib_nhs > 1 && fl->oif == 0)
  119. + if (res->fi && res->fi->fib_nhs > 1)
  120. fib_select_multipath(fl, res);
  121. #endif
  122. /* create a routing cache entry */
  123. - err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth);
  124. + err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, lsrc, &rth);
  125. if (err)
  126. return err;
  127. @@ -1841,7 +1849,8 @@ static inline int ip_mkroute_input(struc
  128. struct fib_result* res,
  129. const struct flowi *fl,
  130. struct in_device *in_dev,
  131. - __be32 daddr, __be32 saddr, u32 tos)
  132. + __be32 daddr, __be32 saddr, u32 tos,
  133. + u32 lsrc)
  134. {
  135. #ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
  136. struct rtable* rth = NULL, *rtres;
  137. @@ -1857,7 +1866,7 @@ static inline int ip_mkroute_input(struc
  138. /* distinguish between multipath and singlepath */
  139. if (hopcount < 2)
  140. return ip_mkroute_input_def(skb, res, fl, in_dev, daddr,
  141. - saddr, tos);
  142. + saddr, tos, 0);
  143. /* add all alternatives to the routing cache */
  144. for (hop = 0; hop < hopcount; hop++) {
  145. @@ -1869,7 +1878,7 @@ static inline int ip_mkroute_input(struc
  146. /* create a routing cache entry */
  147. err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos,
  148. - &rth);
  149. + 0, &rth);
  150. if (err)
  151. return err;
  152. @@ -1889,7 +1898,7 @@ static inline int ip_mkroute_input(struc
  153. skb->dst = &rtres->u.dst;
  154. return err;
  155. #else /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  156. - return ip_mkroute_input_def(skb, res, fl, in_dev, daddr, saddr, tos);
  157. + return ip_mkroute_input_def(skb, res, fl, in_dev, daddr, saddr, tos, lsrc);
  158. #endif /* CONFIG_IP_ROUTE_MULTIPATH_CACHED */
  159. }
  160. @@ -1905,18 +1914,18 @@ static inline int ip_mkroute_input(struc
  161. */
  162. static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  163. - u8 tos, struct net_device *dev)
  164. + u8 tos, struct net_device *dev, u32 lsrc)
  165. {
  166. struct fib_result res;
  167. struct in_device *in_dev = in_dev_get(dev);
  168. struct flowi fl = { .nl_u = { .ip4_u =
  169. { .daddr = daddr,
  170. - .saddr = saddr,
  171. + .saddr = lsrc ? : saddr,
  172. .tos = tos,
  173. .scope = RT_SCOPE_UNIVERSE,
  174. } },
  175. .mark = skb->mark,
  176. - .iif = dev->ifindex };
  177. + .iif = lsrc? loopback_dev.ifindex : dev->ifindex };
  178. unsigned flags = 0;
  179. u32 itag = 0;
  180. struct rtable * rth;
  181. @@ -1949,6 +1958,12 @@ static int ip_route_input_slow(struct sk
  182. if (BADCLASS(daddr) || ZERONET(daddr) || LOOPBACK(daddr))
  183. goto martian_destination;
  184. + if (lsrc) {
  185. + if (MULTICAST(lsrc) || BADCLASS(lsrc) ||
  186. + ZERONET(lsrc) || LOOPBACK(lsrc))
  187. + goto e_inval;
  188. + }
  189. +
  190. /*
  191. * Now we are ready to route packet.
  192. */
  193. @@ -1958,6 +1973,10 @@ static int ip_route_input_slow(struct sk
  194. goto no_route;
  195. }
  196. free_res = 1;
  197. + if (lsrc && res.type != RTN_UNICAST && res.type != RTN_NAT)
  198. + goto e_inval;
  199. + fl.iif = dev->ifindex;
  200. + fl.fl4_src = saddr;
  201. RT_CACHE_STAT_INC(in_slow_tot);
  202. @@ -1982,7 +2001,7 @@ static int ip_route_input_slow(struct sk
  203. if (res.type != RTN_UNICAST)
  204. goto martian_destination;
  205. - err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos);
  206. + err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos, lsrc);
  207. if (err == -ENOBUFS)
  208. goto e_nobufs;
  209. if (err == -EINVAL)
  210. @@ -1997,6 +2016,8 @@ out: return err;
  211. brd_input:
  212. if (skb->protocol != htons(ETH_P_IP))
  213. goto e_inval;
  214. + if (lsrc)
  215. + goto e_inval;
  216. if (ZERONET(saddr))
  217. spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  218. @@ -2037,6 +2058,7 @@ local_input:
  219. rth->u.dst.dev = &loopback_dev;
  220. dev_hold(rth->u.dst.dev);
  221. rth->idev = in_dev_get(rth->u.dst.dev);
  222. + rth->fl.fl4_gw = 0;
  223. rth->rt_gateway = daddr;
  224. rth->rt_spec_dst= spec_dst;
  225. rth->u.dst.input= ip_local_deliver;
  226. @@ -2086,8 +2108,9 @@ martian_source:
  227. goto e_inval;
  228. }
  229. -int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  230. - u8 tos, struct net_device *dev)
  231. +static inline int
  232. +ip_route_input_cached(struct sk_buff *skb, __be32 daddr, __be32 saddr,
  233. + u8 tos, struct net_device *dev, u32 lsrc)
  234. {
  235. struct rtable * rth;
  236. unsigned hash;
  237. @@ -2102,6 +2125,7 @@ int ip_route_input(struct sk_buff *skb,
  238. if (rth->fl.fl4_dst == daddr &&
  239. rth->fl.fl4_src == saddr &&
  240. rth->fl.iif == iif &&
  241. + rth->fl.fl4_lsrc == lsrc &&
  242. rth->fl.oif == 0 &&
  243. rth->fl.mark == skb->mark &&
  244. rth->fl.fl4_tos == tos) {
  245. @@ -2148,7 +2172,19 @@ int ip_route_input(struct sk_buff *skb,
  246. rcu_read_unlock();
  247. return -EINVAL;
  248. }
  249. - return ip_route_input_slow(skb, daddr, saddr, tos, dev);
  250. + return ip_route_input_slow(skb, daddr, saddr, tos, dev, lsrc);
  251. +}
  252. +
  253. +int ip_route_input(struct sk_buff *skb, u32 daddr, u32 saddr,
  254. + u8 tos, struct net_device *dev)
  255. +{
  256. + return ip_route_input_cached(skb, daddr, saddr, tos, dev, 0);
  257. +}
  258. +
  259. +int ip_route_input_lookup(struct sk_buff *skb, u32 daddr, u32 saddr,
  260. + u8 tos, struct net_device *dev, u32 lsrc)
  261. +{
  262. + return ip_route_input_cached(skb, daddr, saddr, tos, dev, lsrc);
  263. }
  264. static inline int __mkroute_output(struct rtable **result,
  265. @@ -2227,6 +2263,7 @@ static inline int __mkroute_output(struc
  266. rth->fl.fl4_tos = tos;
  267. rth->fl.fl4_src = oldflp->fl4_src;
  268. rth->fl.oif = oldflp->oif;
  269. + rth->fl.fl4_gw = oldflp->fl4_gw;
  270. rth->fl.mark = oldflp->mark;
  271. rth->rt_dst = fl->fl4_dst;
  272. rth->rt_src = fl->fl4_src;
  273. @@ -2367,6 +2404,7 @@ static int ip_route_output_slow(struct r
  274. struct flowi fl = { .nl_u = { .ip4_u =
  275. { .daddr = oldflp->fl4_dst,
  276. .saddr = oldflp->fl4_src,
  277. + .gw = oldflp->fl4_gw,
  278. .tos = tos & IPTOS_RT_MASK,
  279. .scope = ((tos & RTO_ONLINK) ?
  280. RT_SCOPE_LINK :
  281. @@ -2470,6 +2508,7 @@ static int ip_route_output_slow(struct r
  282. dev_out = &loopback_dev;
  283. dev_hold(dev_out);
  284. fl.oif = loopback_dev.ifindex;
  285. + fl.fl4_gw = 0;
  286. res.type = RTN_LOCAL;
  287. flags |= RTCF_LOCAL;
  288. goto make_route;
  289. @@ -2477,7 +2516,7 @@ static int ip_route_output_slow(struct r
  290. if (fib_lookup(&fl, &res)) {
  291. res.fi = NULL;
  292. - if (oldflp->oif) {
  293. + if (oldflp->oif && dev_out->flags & IFF_UP) {
  294. /* Apparently, routing tables are wrong. Assume,
  295. that the destination is on link.
  296. @@ -2517,6 +2556,7 @@ static int ip_route_output_slow(struct r
  297. dev_out = &loopback_dev;
  298. dev_hold(dev_out);
  299. fl.oif = dev_out->ifindex;
  300. + fl.fl4_gw = 0;
  301. if (res.fi)
  302. fib_info_put(res.fi);
  303. res.fi = NULL;
  304. @@ -2524,13 +2564,12 @@ static int ip_route_output_slow(struct r
  305. goto make_route;
  306. }
  307. + if (res.type == RTN_UNICAST)
  308. + fib_select_default(&fl, &res);
  309. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  310. - if (res.fi->fib_nhs > 1 && fl.oif == 0)
  311. + if (res.fi->fib_nhs > 1)
  312. fib_select_multipath(&fl, &res);
  313. - else
  314. #endif
  315. - if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
  316. - fib_select_default(&fl, &res);
  317. if (!fl.fl4_src)
  318. fl.fl4_src = FIB_RES_PREFSRC(res);
  319. @@ -2567,6 +2606,7 @@ int __ip_route_output_key(struct rtable
  320. rth->fl.fl4_src == flp->fl4_src &&
  321. rth->fl.iif == 0 &&
  322. rth->fl.oif == flp->oif &&
  323. + rth->fl.fl4_gw == flp->fl4_gw &&
  324. rth->fl.mark == flp->mark &&
  325. !((rth->fl.fl4_tos ^ flp->fl4_tos) &
  326. (IPTOS_RT_MASK | RTO_ONLINK))) {
  327. @@ -3199,3 +3239,4 @@ int __init ip_rt_init(void)
  328. EXPORT_SYMBOL(__ip_select_ident);
  329. EXPORT_SYMBOL(ip_route_input);
  330. EXPORT_SYMBOL(ip_route_output_key);
  331. +EXPORT_SYMBOL(ip_route_input_lookup);