aboutsummaryrefslogtreecommitdiffstats
path: root/include/net/route.h
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2011-04-26 16:28:44 -0400
committerDavid S. Miller <davem@davemloft.net>2011-04-27 16:59:04 -0400
commit2d7192d6cbab20e153c47fa1559ffd41ceef0e79 (patch)
treeaac4c4132f5b4a173ad8f8d0bf24427e039bbc89 /include/net/route.h
parent15ecd039b7182d725f4294e01f2fb12c3a88db17 (diff)
ipv4: Sanitize and simplify ip_route_{connect,newports}()
These functions are used together as a unit for route resolution during connect(). They address the chicken-and-egg problem that exists when ports need to be allocated during connect() processing, yet such port allocations require addressing information from the routing code. It's currently more heavy handed than it needs to be, and in particular we allocate and initialize a flow object twice. Let the callers provide the on-stack flow object. That way we only need to initialize it once in the ip_route_connect() call. Later, if ip_route_newports() needs to do anything, it re-uses that flow object as-is except for the ports which it updates before the route re-lookup. Also, describe why this set of facilities are needed and how it works in a big comment. Signed-off-by: David S. Miller <davem@davemloft.net> Reviewed-by: Eric Dumazet <eric.dumazet@gmail.com>
Diffstat (limited to 'include/net/route.h')
-rw-r--r--include/net/route.h88
1 files changed, 56 insertions, 32 deletions
diff --git a/include/net/route.h b/include/net/route.h
index 3684c3edbae4..79530da31b34 100644
--- a/include/net/route.h
+++ b/include/net/route.h
@@ -217,17 +217,37 @@ static inline char rt_tos2priority(u8 tos)
217 return ip_tos2prio[IPTOS_TOS(tos)>>1]; 217 return ip_tos2prio[IPTOS_TOS(tos)>>1];
218} 218}
219 219
220static inline struct rtable *ip_route_connect(__be32 dst, __be32 src, u32 tos, 220/* ip_route_connect() and ip_route_newports() work in tandem whilst
221 int oif, u8 protocol, 221 * binding a socket for a new outgoing connection.
222 __be16 sport, __be16 dport, 222 *
223 struct sock *sk, bool can_sleep) 223 * In order to use IPSEC properly, we must, in the end, have a
224 * route that was looked up using all available keys including source
225 * and destination ports.
226 *
227 * However, if a source port needs to be allocated (the user specified
228 * a wildcard source port) we need to obtain addressing information
229 * in order to perform that allocation.
230 *
231 * So ip_route_connect() looks up a route using wildcarded source and
232 * destination ports in the key, simply so that we can get a pair of
233 * addresses to use for port allocation.
234 *
235 * Later, once the ports are allocated, ip_route_newports() will make
236 * another route lookup if needed to make sure we catch any IPSEC
237 * rules keyed on the port information.
238 *
239 * The callers allocate the flow key on their stack, and must pass in
240 * the same flowi4 object to both the ip_route_connect() and the
241 * ip_route_newports() calls.
242 */
243
244static inline void ip_route_connect_init(struct flowi4 *fl4, __be32 dst, __be32 src,
245 u32 tos, int oif, u8 protocol,
246 __be16 sport, __be16 dport,
247 struct sock *sk, bool can_sleep)
224{ 248{
225 struct net *net = sock_net(sk); 249 __u8 flow_flags = 0;
226 struct rtable *rt;
227 struct flowi4 fl4;
228 __u8 flow_flags;
229 250
230 flow_flags = 0;
231 if (inet_sk(sk)->transparent) 251 if (inet_sk(sk)->transparent)
232 flow_flags |= FLOWI_FLAG_ANYSRC; 252 flow_flags |= FLOWI_FLAG_ANYSRC;
233 if (protocol == IPPROTO_TCP) 253 if (protocol == IPPROTO_TCP)
@@ -235,41 +255,45 @@ static inline struct rtable *ip_route_connect(__be32 dst, __be32 src, u32 tos,
235 if (can_sleep) 255 if (can_sleep)
236 flow_flags |= FLOWI_FLAG_CAN_SLEEP; 256 flow_flags |= FLOWI_FLAG_CAN_SLEEP;
237 257
238 flowi4_init_output(&fl4, oif, sk->sk_mark, tos, RT_SCOPE_UNIVERSE, 258 flowi4_init_output(fl4, oif, sk->sk_mark, tos, RT_SCOPE_UNIVERSE,
239 protocol, flow_flags, dst, src, dport, sport); 259 protocol, flow_flags, dst, src, dport, sport);
260}
261
262static inline struct rtable *ip_route_connect(struct flowi4 *fl4,
263 __be32 dst, __be32 src, u32 tos,
264 int oif, u8 protocol,
265 __be16 sport, __be16 dport,
266 struct sock *sk, bool can_sleep)
267{
268 struct net *net = sock_net(sk);
269 struct rtable *rt;
270
271 ip_route_connect_init(fl4, dst, src, tos, oif, protocol,
272 sport, dport, sk, can_sleep);
240 273
241 if (!dst || !src) { 274 if (!dst || !src) {
242 rt = __ip_route_output_key(net, &fl4); 275 rt = __ip_route_output_key(net, fl4);
243 if (IS_ERR(rt)) 276 if (IS_ERR(rt))
244 return rt; 277 return rt;
245 fl4.daddr = rt->rt_dst; 278 fl4->daddr = rt->rt_dst;
246 fl4.saddr = rt->rt_src; 279 fl4->saddr = rt->rt_src;
247 ip_rt_put(rt); 280 ip_rt_put(rt);
248 } 281 }
249 security_sk_classify_flow(sk, flowi4_to_flowi(&fl4)); 282 security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
250 return ip_route_output_flow(net, &fl4, sk); 283 return ip_route_output_flow(net, fl4, sk);
251} 284}
252 285
253static inline struct rtable *ip_route_newports(struct rtable *rt, 286static inline struct rtable *ip_route_newports(struct flowi4 *fl4, struct rtable *rt,
254 u8 protocol, __be16 orig_sport, 287 __be16 orig_sport, __be16 orig_dport,
255 __be16 orig_dport, __be16 sport, 288 __be16 sport, __be16 dport,
256 __be16 dport, struct sock *sk) 289 struct sock *sk)
257{ 290{
258 if (sport != orig_sport || dport != orig_dport) { 291 if (sport != orig_sport || dport != orig_dport) {
259 struct flowi4 fl4; 292 fl4->fl4_dport = dport;
260 __u8 flow_flags; 293 fl4->fl4_sport = sport;
261
262 flow_flags = 0;
263 if (inet_sk(sk)->transparent)
264 flow_flags |= FLOWI_FLAG_ANYSRC;
265 if (protocol == IPPROTO_TCP)
266 flow_flags |= FLOWI_FLAG_PRECOW_METRICS;
267 flowi4_init_output(&fl4, rt->rt_oif, rt->rt_mark, rt->rt_tos,
268 RT_SCOPE_UNIVERSE, protocol, flow_flags,
269 rt->rt_dst, rt->rt_src, dport, sport);
270 ip_rt_put(rt); 294 ip_rt_put(rt);
271 security_sk_classify_flow(sk, flowi4_to_flowi(&fl4)); 295 security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
272 return ip_route_output_flow(sock_net(sk), &fl4, sk); 296 return ip_route_output_flow(sock_net(sk), fl4, sk);
273 } 297 }
274 return rt; 298 return rt;
275} 299}