xref: /openbmc/linux/include/net/route.h (revision b42597e2)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET  is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Definitions for the IP router.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Version:	@(#)route.h	1.0.4	05/27/93
91da177e4SLinus Torvalds  *
1002c30a84SJesper Juhl  * Authors:	Ross Biro
111da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
121da177e4SLinus Torvalds  * Fixes:
131da177e4SLinus Torvalds  *		Alan Cox	:	Reformatted. Added ip_rt_local()
141da177e4SLinus Torvalds  *		Alan Cox	:	Support for TCP parameters.
151da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Major changes for new routing code.
161da177e4SLinus Torvalds  *		Mike McLagan    :	Routing by source
171da177e4SLinus Torvalds  *		Robert Olsson   :	Added rt_cache statistics
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
201da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
211da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
221da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
231da177e4SLinus Torvalds  */
241da177e4SLinus Torvalds #ifndef _ROUTE_H
251da177e4SLinus Torvalds #define _ROUTE_H
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include <net/dst.h>
281da177e4SLinus Torvalds #include <net/inetpeer.h>
291da177e4SLinus Torvalds #include <net/flow.h>
3079876874SKOVACS Krisztian #include <net/inet_sock.h>
311da177e4SLinus Torvalds #include <linux/in_route.h>
321da177e4SLinus Torvalds #include <linux/rtnetlink.h>
331da177e4SLinus Torvalds #include <linux/route.h>
341da177e4SLinus Torvalds #include <linux/ip.h>
351da177e4SLinus Torvalds #include <linux/cache.h>
36beb8d13bSVenkat Yekkirala #include <linux/security.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define RTO_ONLINK	0x01
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define RT_CONN_FLAGS(sk)   (RT_TOS(inet_sk(sk)->tos) | sock_flag(sk, SOCK_LOCALROUTE))
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds struct fib_nh;
4362fa8a84SDavid S. Miller struct fib_info;
44fd2c3ef7SEric Dumazet struct rtable {
451da177e4SLinus Torvalds 	struct dst_entry	dst;
461da177e4SLinus Torvalds 
475e2b61f7SDavid S. Miller 	/* Lookup key. */
485e2b61f7SDavid S. Miller 	__be32			rt_key_dst;
495e2b61f7SDavid S. Miller 	__be32			rt_key_src;
50093c2ca4SEric Dumazet 
5129e75252SEric Dumazet 	int			rt_genid;
5295c96174SEric Dumazet 	unsigned int		rt_flags;
531da177e4SLinus Torvalds 	__u16			rt_type;
54475949d8SDavid S. Miller 	__u8			rt_key_tos;
551da177e4SLinus Torvalds 
56f2c3fe24SAl Viro 	__be32			rt_dst;	/* Path destination	*/
57f2c3fe24SAl Viro 	__be32			rt_src;	/* Path source		*/
581b86a58fSOGAWA Hirofumi 	int			rt_route_iif;
591da177e4SLinus Torvalds 	int			rt_iif;
605e2b61f7SDavid S. Miller 	int			rt_oif;
615e2b61f7SDavid S. Miller 	__u32			rt_mark;
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 	/* Info on neighbour */
64f2c3fe24SAl Viro 	__be32			rt_gateway;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	/* Miscellaneous cached information */
675943634fSDavid S. Miller 	u32			rt_pmtu;
6862fa8a84SDavid S. Miller 	struct fib_info		*fi; /* for client ref to shared metrics */
691da177e4SLinus Torvalds };
701da177e4SLinus Torvalds 
71b8400f37SSteffen Klassert static inline bool rt_is_input_route(const struct rtable *rt)
72c7537967SDavid S. Miller {
731b86a58fSOGAWA Hirofumi 	return rt->rt_route_iif != 0;
74c7537967SDavid S. Miller }
75c7537967SDavid S. Miller 
76b8400f37SSteffen Klassert static inline bool rt_is_output_route(const struct rtable *rt)
77c7537967SDavid S. Miller {
781b86a58fSOGAWA Hirofumi 	return rt->rt_route_iif == 0;
79c7537967SDavid S. Miller }
80c7537967SDavid S. Miller 
81fd2c3ef7SEric Dumazet struct ip_rt_acct {
821da177e4SLinus Torvalds 	__u32 	o_bytes;
831da177e4SLinus Torvalds 	__u32 	o_packets;
841da177e4SLinus Torvalds 	__u32 	i_bytes;
851da177e4SLinus Torvalds 	__u32 	i_packets;
861da177e4SLinus Torvalds };
871da177e4SLinus Torvalds 
88fd2c3ef7SEric Dumazet struct rt_cache_stat {
891da177e4SLinus Torvalds         unsigned int in_hit;
901da177e4SLinus Torvalds         unsigned int in_slow_tot;
911da177e4SLinus Torvalds         unsigned int in_slow_mc;
921da177e4SLinus Torvalds         unsigned int in_no_route;
931da177e4SLinus Torvalds         unsigned int in_brd;
941da177e4SLinus Torvalds         unsigned int in_martian_dst;
951da177e4SLinus Torvalds         unsigned int in_martian_src;
961da177e4SLinus Torvalds         unsigned int out_hit;
971da177e4SLinus Torvalds         unsigned int out_slow_tot;
981da177e4SLinus Torvalds         unsigned int out_slow_mc;
991da177e4SLinus Torvalds         unsigned int gc_total;
1001da177e4SLinus Torvalds         unsigned int gc_ignored;
1011da177e4SLinus Torvalds         unsigned int gc_goal_miss;
1021da177e4SLinus Torvalds         unsigned int gc_dst_overflow;
1031da177e4SLinus Torvalds         unsigned int in_hlist_search;
1041da177e4SLinus Torvalds         unsigned int out_hlist_search;
1051da177e4SLinus Torvalds };
1061da177e4SLinus Torvalds 
1077d720c3eSTejun Heo extern struct ip_rt_acct __percpu *ip_rt_acct;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds struct in_device;
1101da177e4SLinus Torvalds extern int		ip_rt_init(void);
11194206125SDavid S. Miller extern void		ip_rt_redirect(struct sk_buff *skb, __be32 new_gw);
11276e6ebfbSDenis V. Lunev extern void		rt_cache_flush(struct net *net, int how);
1136561a3b1SDavid S. Miller extern void		rt_cache_flush_batch(struct net *net);
114813b3b5dSDavid S. Miller extern struct rtable *__ip_route_output_key(struct net *, struct flowi4 *flp);
1159d6ec938SDavid S. Miller extern struct rtable *ip_route_output_flow(struct net *, struct flowi4 *flp,
116b23dd4feSDavid S. Miller 					   struct sock *sk);
1172774c131SDavid S. Miller extern struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig);
118407eadd9SEric Dumazet 
1199d6ec938SDavid S. Miller static inline struct rtable *ip_route_output_key(struct net *net, struct flowi4 *flp)
1205bfa787fSDavid S. Miller {
1215bfa787fSDavid S. Miller 	return ip_route_output_flow(net, flp, NULL);
1225bfa787fSDavid S. Miller }
1235bfa787fSDavid S. Miller 
12478fbfd8aSDavid S. Miller static inline struct rtable *ip_route_output(struct net *net, __be32 daddr,
12578fbfd8aSDavid S. Miller 					     __be32 saddr, u8 tos, int oif)
12678fbfd8aSDavid S. Miller {
1279d6ec938SDavid S. Miller 	struct flowi4 fl4 = {
1289d6ec938SDavid S. Miller 		.flowi4_oif = oif,
129c5d21c4bSRoland Dreier 		.flowi4_tos = tos,
1309d6ec938SDavid S. Miller 		.daddr = daddr,
1319d6ec938SDavid S. Miller 		.saddr = saddr,
13278fbfd8aSDavid S. Miller 	};
1339d6ec938SDavid S. Miller 	return ip_route_output_key(net, &fl4);
13478fbfd8aSDavid S. Miller }
13578fbfd8aSDavid S. Miller 
13631e4543dSDavid S. Miller static inline struct rtable *ip_route_output_ports(struct net *net, struct flowi4 *fl4,
13731e4543dSDavid S. Miller 						   struct sock *sk,
13878fbfd8aSDavid S. Miller 						   __be32 daddr, __be32 saddr,
13978fbfd8aSDavid S. Miller 						   __be16 dport, __be16 sport,
14078fbfd8aSDavid S. Miller 						   __u8 proto, __u8 tos, int oif)
14178fbfd8aSDavid S. Miller {
14231e4543dSDavid S. Miller 	flowi4_init_output(fl4, oif, sk ? sk->sk_mark : 0, tos,
14394b92b88SDavid S. Miller 			   RT_SCOPE_UNIVERSE, proto,
14494b92b88SDavid S. Miller 			   sk ? inet_sk_flowi_flags(sk) : 0,
14594b92b88SDavid S. Miller 			   daddr, saddr, dport, sport);
14678fbfd8aSDavid S. Miller 	if (sk)
14731e4543dSDavid S. Miller 		security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
14831e4543dSDavid S. Miller 	return ip_route_output_flow(net, fl4, sk);
14978fbfd8aSDavid S. Miller }
15078fbfd8aSDavid S. Miller 
151cbb1e85fSDavid S. Miller static inline struct rtable *ip_route_output_gre(struct net *net, struct flowi4 *fl4,
15278fbfd8aSDavid S. Miller 						 __be32 daddr, __be32 saddr,
15378fbfd8aSDavid S. Miller 						 __be32 gre_key, __u8 tos, int oif)
15478fbfd8aSDavid S. Miller {
155cbb1e85fSDavid S. Miller 	memset(fl4, 0, sizeof(*fl4));
156cbb1e85fSDavid S. Miller 	fl4->flowi4_oif = oif;
157cbb1e85fSDavid S. Miller 	fl4->daddr = daddr;
158cbb1e85fSDavid S. Miller 	fl4->saddr = saddr;
159cbb1e85fSDavid S. Miller 	fl4->flowi4_tos = tos;
160cbb1e85fSDavid S. Miller 	fl4->flowi4_proto = IPPROTO_GRE;
161cbb1e85fSDavid S. Miller 	fl4->fl4_gre_key = gre_key;
162cbb1e85fSDavid S. Miller 	return ip_route_output_key(net, fl4);
16378fbfd8aSDavid S. Miller }
16478fbfd8aSDavid S. Miller 
165407eadd9SEric Dumazet extern int ip_route_input_common(struct sk_buff *skb, __be32 dst, __be32 src,
166c10237e0SDavid S. Miller 				 u8 tos, struct net_device *devin, bool noref);
167407eadd9SEric Dumazet 
168407eadd9SEric Dumazet static inline int ip_route_input(struct sk_buff *skb, __be32 dst, __be32 src,
169407eadd9SEric Dumazet 				 u8 tos, struct net_device *devin)
170407eadd9SEric Dumazet {
171c10237e0SDavid S. Miller 	return ip_route_input_common(skb, dst, src, tos, devin, false);
172407eadd9SEric Dumazet }
173407eadd9SEric Dumazet 
174407eadd9SEric Dumazet static inline int ip_route_input_noref(struct sk_buff *skb, __be32 dst, __be32 src,
175c10237e0SDavid S. Miller 				       u8 tos, struct net_device *devin)
176407eadd9SEric Dumazet {
177c10237e0SDavid S. Miller 	return ip_route_input_common(skb, dst, src, tos, devin, true);
178407eadd9SEric Dumazet }
179407eadd9SEric Dumazet 
18036393395SDavid S. Miller extern void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu,
18136393395SDavid S. Miller 			     int oif, u32 mark, u8 protocol, int flow_flags);
18236393395SDavid S. Miller extern void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu);
183b42597e2SDavid S. Miller extern void ipv4_redirect(struct sk_buff *skb, struct net *net,
184b42597e2SDavid S. Miller 			  int oif, u32 mark, u8 protocol, int flow_flags);
185b42597e2SDavid S. Miller extern void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk);
1861da177e4SLinus Torvalds extern void ip_rt_send_redirect(struct sk_buff *skb);
1871da177e4SLinus Torvalds 
18895c96174SEric Dumazet extern unsigned int		inet_addr_type(struct net *net, __be32 addr);
18995c96174SEric Dumazet extern unsigned int		inet_dev_addr_type(struct net *net, const struct net_device *dev, __be32 addr);
1901da177e4SLinus Torvalds extern void		ip_rt_multicast_event(struct in_device *);
1911bad118aSDenis V. Lunev extern int		ip_rt_ioctl(struct net *, unsigned int cmd, void __user *arg);
1928e36360aSDavid S. Miller extern void		ip_rt_get_source(u8 *src, struct sk_buff *skb, struct rtable *rt);
1931da177e4SLinus Torvalds extern int		ip_rt_dump(struct sk_buff *skb,  struct netlink_callback *cb);
1941da177e4SLinus Torvalds 
1950ff60a45SJamal Hadi Salim struct in_ifaddr;
1960ff60a45SJamal Hadi Salim extern void fib_add_ifaddr(struct in_ifaddr *);
197e6abbaa2SJulian Anastasov extern void fib_del_ifaddr(struct in_ifaddr *, struct in_ifaddr *);
1980ff60a45SJamal Hadi Salim 
1991da177e4SLinus Torvalds static inline void ip_rt_put(struct rtable * rt)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	if (rt)
202d8d1f30bSChangli Gao 		dst_release(&rt->dst);
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds #define IPTOS_RT_MASK	(IPTOS_TOS_MASK & ~3)
2061da177e4SLinus Torvalds 
2074839c52bSPhilippe De Muyter extern const __u8 ip_tos2prio[16];
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds static inline char rt_tos2priority(u8 tos)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	return ip_tos2prio[IPTOS_TOS(tos)>>1];
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
2142d7192d6SDavid S. Miller /* ip_route_connect() and ip_route_newports() work in tandem whilst
2152d7192d6SDavid S. Miller  * binding a socket for a new outgoing connection.
2162d7192d6SDavid S. Miller  *
2172d7192d6SDavid S. Miller  * In order to use IPSEC properly, we must, in the end, have a
2182d7192d6SDavid S. Miller  * route that was looked up using all available keys including source
2192d7192d6SDavid S. Miller  * and destination ports.
2202d7192d6SDavid S. Miller  *
2212d7192d6SDavid S. Miller  * However, if a source port needs to be allocated (the user specified
2222d7192d6SDavid S. Miller  * a wildcard source port) we need to obtain addressing information
2232d7192d6SDavid S. Miller  * in order to perform that allocation.
2242d7192d6SDavid S. Miller  *
2252d7192d6SDavid S. Miller  * So ip_route_connect() looks up a route using wildcarded source and
2262d7192d6SDavid S. Miller  * destination ports in the key, simply so that we can get a pair of
2272d7192d6SDavid S. Miller  * addresses to use for port allocation.
2282d7192d6SDavid S. Miller  *
2292d7192d6SDavid S. Miller  * Later, once the ports are allocated, ip_route_newports() will make
2302d7192d6SDavid S. Miller  * another route lookup if needed to make sure we catch any IPSEC
2312d7192d6SDavid S. Miller  * rules keyed on the port information.
2322d7192d6SDavid S. Miller  *
2332d7192d6SDavid S. Miller  * The callers allocate the flow key on their stack, and must pass in
2342d7192d6SDavid S. Miller  * the same flowi4 object to both the ip_route_connect() and the
2352d7192d6SDavid S. Miller  * ip_route_newports() calls.
2362d7192d6SDavid S. Miller  */
2372d7192d6SDavid S. Miller 
2382d7192d6SDavid S. Miller static inline void ip_route_connect_init(struct flowi4 *fl4, __be32 dst, __be32 src,
2392d7192d6SDavid S. Miller 					 u32 tos, int oif, u8 protocol,
240b23dd4feSDavid S. Miller 					 __be16 sport, __be16 dport,
241b23dd4feSDavid S. Miller 					 struct sock *sk, bool can_sleep)
2421da177e4SLinus Torvalds {
2432d7192d6SDavid S. Miller 	__u8 flow_flags = 0;
24479876874SKOVACS Krisztian 
24579876874SKOVACS Krisztian 	if (inet_sk(sk)->transparent)
24694b92b88SDavid S. Miller 		flow_flags |= FLOWI_FLAG_ANYSRC;
2475df65e55SDavid S. Miller 	if (can_sleep)
24894b92b88SDavid S. Miller 		flow_flags |= FLOWI_FLAG_CAN_SLEEP;
24994b92b88SDavid S. Miller 
2502d7192d6SDavid S. Miller 	flowi4_init_output(fl4, oif, sk->sk_mark, tos, RT_SCOPE_UNIVERSE,
25194b92b88SDavid S. Miller 			   protocol, flow_flags, dst, src, dport, sport);
2522d7192d6SDavid S. Miller }
2532d7192d6SDavid S. Miller 
2542d7192d6SDavid S. Miller static inline struct rtable *ip_route_connect(struct flowi4 *fl4,
2552d7192d6SDavid S. Miller 					      __be32 dst, __be32 src, u32 tos,
2562d7192d6SDavid S. Miller 					      int oif, u8 protocol,
2572d7192d6SDavid S. Miller 					      __be16 sport, __be16 dport,
2582d7192d6SDavid S. Miller 					      struct sock *sk, bool can_sleep)
2592d7192d6SDavid S. Miller {
2602d7192d6SDavid S. Miller 	struct net *net = sock_net(sk);
2612d7192d6SDavid S. Miller 	struct rtable *rt;
2622d7192d6SDavid S. Miller 
2632d7192d6SDavid S. Miller 	ip_route_connect_init(fl4, dst, src, tos, oif, protocol,
2642d7192d6SDavid S. Miller 			      sport, dport, sk, can_sleep);
26579876874SKOVACS Krisztian 
2661da177e4SLinus Torvalds 	if (!dst || !src) {
2672d7192d6SDavid S. Miller 		rt = __ip_route_output_key(net, fl4);
268b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
269b23dd4feSDavid S. Miller 			return rt;
270b23dd4feSDavid S. Miller 		ip_rt_put(rt);
271e6b45241SJulian Anastasov 		flowi4_update_output(fl4, oif, tos, fl4->daddr, fl4->saddr);
2721da177e4SLinus Torvalds 	}
2732d7192d6SDavid S. Miller 	security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
2742d7192d6SDavid S. Miller 	return ip_route_output_flow(net, fl4, sk);
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
2772d7192d6SDavid S. Miller static inline struct rtable *ip_route_newports(struct flowi4 *fl4, struct rtable *rt,
2782d7192d6SDavid S. Miller 					       __be16 orig_sport, __be16 orig_dport,
2792d7192d6SDavid S. Miller 					       __be16 sport, __be16 dport,
2802d7192d6SDavid S. Miller 					       struct sock *sk)
2811da177e4SLinus Torvalds {
282dca8b089SDavid S. Miller 	if (sport != orig_sport || dport != orig_dport) {
2832d7192d6SDavid S. Miller 		fl4->fl4_dport = dport;
2842d7192d6SDavid S. Miller 		fl4->fl4_sport = sport;
285b23dd4feSDavid S. Miller 		ip_rt_put(rt);
286e6b45241SJulian Anastasov 		flowi4_update_output(fl4, sk->sk_bound_dev_if,
287e6b45241SJulian Anastasov 				     RT_CONN_FLAGS(sk), fl4->daddr,
288e6b45241SJulian Anastasov 				     fl4->saddr);
2892d7192d6SDavid S. Miller 		security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
2902d7192d6SDavid S. Miller 		return ip_route_output_flow(sock_net(sk), fl4, sk);
2911da177e4SLinus Torvalds 	}
292b23dd4feSDavid S. Miller 	return rt;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951668e010SKOVACS Krisztian static inline int inet_iif(const struct sk_buff *skb)
2961668e010SKOVACS Krisztian {
297511c3f92SEric Dumazet 	return skb_rtable(skb)->rt_iif;
2981668e010SKOVACS Krisztian }
2991668e010SKOVACS Krisztian 
300323e126fSDavid S. Miller extern int sysctl_ip_default_ttl;
301323e126fSDavid S. Miller 
302323e126fSDavid S. Miller static inline int ip4_dst_hoplimit(const struct dst_entry *dst)
303323e126fSDavid S. Miller {
304323e126fSDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
305323e126fSDavid S. Miller 
306323e126fSDavid S. Miller 	if (hoplimit == 0)
307323e126fSDavid S. Miller 		hoplimit = sysctl_ip_default_ttl;
308323e126fSDavid S. Miller 	return hoplimit;
309323e126fSDavid S. Miller }
310323e126fSDavid S. Miller 
3111da177e4SLinus Torvalds #endif	/* _ROUTE_H */
312