xref: /openbmc/linux/include/net/route.h (revision 2874c5fd)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds  *		operating system.  INET  is implemented using the  BSD Socket
51da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *		Definitions for the IP router.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Version:	@(#)route.h	1.0.4	05/27/93
101da177e4SLinus Torvalds  *
1102c30a84SJesper Juhl  * Authors:	Ross Biro
121da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
131da177e4SLinus Torvalds  * Fixes:
141da177e4SLinus Torvalds  *		Alan Cox	:	Reformatted. Added ip_rt_local()
151da177e4SLinus Torvalds  *		Alan Cox	:	Support for TCP parameters.
161da177e4SLinus Torvalds  *		Alexey Kuznetsov:	Major changes for new routing code.
171da177e4SLinus Torvalds  *		Mike McLagan    :	Routing by source
181da177e4SLinus Torvalds  *		Robert Olsson   :	Added rt_cache statistics
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds #ifndef _ROUTE_H
211da177e4SLinus Torvalds #define _ROUTE_H
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <net/dst.h>
241da177e4SLinus Torvalds #include <net/inetpeer.h>
251da177e4SLinus Torvalds #include <net/flow.h>
2679876874SKOVACS Krisztian #include <net/inet_sock.h>
2779a13159SPeter Nørlund #include <net/ip_fib.h>
285c9f7c1dSDavid Ahern #include <net/arp.h>
295c9f7c1dSDavid Ahern #include <net/ndisc.h>
301da177e4SLinus Torvalds #include <linux/in_route.h>
311da177e4SLinus Torvalds #include <linux/rtnetlink.h>
32c6cffba4SDavid S. Miller #include <linux/rcupdate.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 
38f87c10a8SHannes Frederic Sowa /* IPv4 datagram length is stored into 16bit field (tot_len) */
39f87c10a8SHannes Frederic Sowa #define IP_MAX_MTU	0xFFFFU
40f87c10a8SHannes Frederic Sowa 
411da177e4SLinus Torvalds #define RTO_ONLINK	0x01
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #define RT_CONN_FLAGS(sk)   (RT_TOS(inet_sk(sk)->tos) | sock_flag(sk, SOCK_LOCALROUTE))
44aa661581SFrancesco Fusco #define RT_CONN_FLAGS_TOS(sk,tos)   (RT_TOS(tos) | sock_flag(sk, SOCK_LOCALROUTE))
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds struct fib_nh;
4762fa8a84SDavid S. Miller struct fib_info;
485055c371SEric Dumazet struct uncached_list;
49fd2c3ef7SEric Dumazet struct rtable {
501da177e4SLinus Torvalds 	struct dst_entry	dst;
511da177e4SLinus Torvalds 
5229e75252SEric Dumazet 	int			rt_genid;
5395c96174SEric Dumazet 	unsigned int		rt_flags;
541da177e4SLinus Torvalds 	__u16			rt_type;
55155e8336SJulian Anastasov 	__u8			rt_is_input;
561550c171SDavid Ahern 	u8			rt_gw_family;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	int			rt_iif;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	/* Info on neighbour */
610f5f7d7bSDavid Ahern 	union {
621550c171SDavid Ahern 		__be32		rt_gw4;
630f5f7d7bSDavid Ahern 		struct in6_addr	rt_gw6;
640f5f7d7bSDavid Ahern 	};
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	/* Miscellaneous cached information */
67d52e5a7eSSabrina Dubroca 	u32			rt_mtu_locked:1,
68d52e5a7eSSabrina Dubroca 				rt_pmtu:31;
69caacf05eSDavid S. Miller 
70caacf05eSDavid S. Miller 	struct list_head	rt_uncached;
715055c371SEric Dumazet 	struct uncached_list	*rt_uncached_list;
721da177e4SLinus Torvalds };
731da177e4SLinus Torvalds 
74b8400f37SSteffen Klassert static inline bool rt_is_input_route(const struct rtable *rt)
75c7537967SDavid S. Miller {
769917e1e8SDavid S. Miller 	return rt->rt_is_input != 0;
77c7537967SDavid S. Miller }
78c7537967SDavid S. Miller 
79b8400f37SSteffen Klassert static inline bool rt_is_output_route(const struct rtable *rt)
80c7537967SDavid S. Miller {
819917e1e8SDavid S. Miller 	return rt->rt_is_input == 0;
82c7537967SDavid S. Miller }
83c7537967SDavid S. Miller 
84f8126f1dSDavid S. Miller static inline __be32 rt_nexthop(const struct rtable *rt, __be32 daddr)
85f8126f1dSDavid S. Miller {
861550c171SDavid Ahern 	if (rt->rt_gw_family == AF_INET)
871550c171SDavid Ahern 		return rt->rt_gw4;
88f8126f1dSDavid S. Miller 	return daddr;
89f8126f1dSDavid S. Miller }
90f8126f1dSDavid S. Miller 
91fd2c3ef7SEric Dumazet struct ip_rt_acct {
921da177e4SLinus Torvalds 	__u32 	o_bytes;
931da177e4SLinus Torvalds 	__u32 	o_packets;
941da177e4SLinus Torvalds 	__u32 	i_bytes;
951da177e4SLinus Torvalds 	__u32 	i_packets;
961da177e4SLinus Torvalds };
971da177e4SLinus Torvalds 
98fd2c3ef7SEric Dumazet struct rt_cache_stat {
991da177e4SLinus Torvalds         unsigned int in_slow_tot;
1001da177e4SLinus Torvalds         unsigned int in_slow_mc;
1011da177e4SLinus Torvalds         unsigned int in_no_route;
1021da177e4SLinus Torvalds         unsigned int in_brd;
1031da177e4SLinus Torvalds         unsigned int in_martian_dst;
1041da177e4SLinus Torvalds         unsigned int in_martian_src;
1051da177e4SLinus Torvalds         unsigned int out_slow_tot;
1061da177e4SLinus Torvalds         unsigned int out_slow_mc;
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds 
1097d720c3eSTejun Heo extern struct ip_rt_acct __percpu *ip_rt_acct;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds struct in_device;
1122702c4bbSJoe Perches 
1132702c4bbSJoe Perches int ip_rt_init(void);
1142702c4bbSJoe Perches void rt_cache_flush(struct net *net);
1152702c4bbSJoe Perches void rt_flush_dev(struct net_device *dev);
1163abd1adeSDavid Ahern struct rtable *ip_route_output_key_hash(struct net *net, struct flowi4 *flp,
1173abd1adeSDavid Ahern 					const struct sk_buff *skb);
1183abd1adeSDavid Ahern struct rtable *ip_route_output_key_hash_rcu(struct net *net, struct flowi4 *flp,
1193abd1adeSDavid Ahern 					    struct fib_result *res,
120bf4e0a3dSNikolay Aleksandrov 					    const struct sk_buff *skb);
12179a13159SPeter Nørlund 
12279a13159SPeter Nørlund static inline struct rtable *__ip_route_output_key(struct net *net,
12379a13159SPeter Nørlund 						   struct flowi4 *flp)
12479a13159SPeter Nørlund {
1253abd1adeSDavid Ahern 	return ip_route_output_key_hash(net, flp, NULL);
12679a13159SPeter Nørlund }
12779a13159SPeter Nørlund 
1282702c4bbSJoe Perches struct rtable *ip_route_output_flow(struct net *, struct flowi4 *flp,
1296f9c9615SEric Dumazet 				    const struct sock *sk);
1302702c4bbSJoe Perches struct dst_entry *ipv4_blackhole_route(struct net *net,
1312702c4bbSJoe Perches 				       struct dst_entry *dst_orig);
132407eadd9SEric Dumazet 
1339d6ec938SDavid S. Miller static inline struct rtable *ip_route_output_key(struct net *net, struct flowi4 *flp)
1345bfa787fSDavid S. Miller {
1355bfa787fSDavid S. Miller 	return ip_route_output_flow(net, flp, NULL);
1365bfa787fSDavid S. Miller }
1375bfa787fSDavid S. Miller 
13878fbfd8aSDavid S. Miller static inline struct rtable *ip_route_output(struct net *net, __be32 daddr,
13978fbfd8aSDavid S. Miller 					     __be32 saddr, u8 tos, int oif)
14078fbfd8aSDavid S. Miller {
1419d6ec938SDavid S. Miller 	struct flowi4 fl4 = {
1429d6ec938SDavid S. Miller 		.flowi4_oif = oif,
143c5d21c4bSRoland Dreier 		.flowi4_tos = tos,
1449d6ec938SDavid S. Miller 		.daddr = daddr,
1459d6ec938SDavid S. Miller 		.saddr = saddr,
14678fbfd8aSDavid S. Miller 	};
1479d6ec938SDavid S. Miller 	return ip_route_output_key(net, &fl4);
14878fbfd8aSDavid S. Miller }
14978fbfd8aSDavid S. Miller 
15031e4543dSDavid S. Miller static inline struct rtable *ip_route_output_ports(struct net *net, struct flowi4 *fl4,
15131e4543dSDavid S. Miller 						   struct sock *sk,
15278fbfd8aSDavid S. Miller 						   __be32 daddr, __be32 saddr,
15378fbfd8aSDavid S. Miller 						   __be16 dport, __be16 sport,
15478fbfd8aSDavid S. Miller 						   __u8 proto, __u8 tos, int oif)
15578fbfd8aSDavid S. Miller {
15631e4543dSDavid S. Miller 	flowi4_init_output(fl4, oif, sk ? sk->sk_mark : 0, tos,
15794b92b88SDavid S. Miller 			   RT_SCOPE_UNIVERSE, proto,
15894b92b88SDavid S. Miller 			   sk ? inet_sk_flowi_flags(sk) : 0,
159e2d118a1SLorenzo Colitti 			   daddr, saddr, dport, sport, sock_net_uid(net, sk));
16078fbfd8aSDavid S. Miller 	if (sk)
16131e4543dSDavid S. Miller 		security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
16231e4543dSDavid S. Miller 	return ip_route_output_flow(net, fl4, sk);
16378fbfd8aSDavid S. Miller }
16478fbfd8aSDavid S. Miller 
165cbb1e85fSDavid S. Miller static inline struct rtable *ip_route_output_gre(struct net *net, struct flowi4 *fl4,
16678fbfd8aSDavid S. Miller 						 __be32 daddr, __be32 saddr,
16778fbfd8aSDavid S. Miller 						 __be32 gre_key, __u8 tos, int oif)
16878fbfd8aSDavid S. Miller {
169cbb1e85fSDavid S. Miller 	memset(fl4, 0, sizeof(*fl4));
170cbb1e85fSDavid S. Miller 	fl4->flowi4_oif = oif;
171cbb1e85fSDavid S. Miller 	fl4->daddr = daddr;
172cbb1e85fSDavid S. Miller 	fl4->saddr = saddr;
173cbb1e85fSDavid S. Miller 	fl4->flowi4_tos = tos;
174cbb1e85fSDavid S. Miller 	fl4->flowi4_proto = IPPROTO_GRE;
175cbb1e85fSDavid S. Miller 	fl4->fl4_gre_key = gre_key;
176cbb1e85fSDavid S. Miller 	return ip_route_output_key(net, fl4);
17778fbfd8aSDavid S. Miller }
178bc044e8dSPaolo Abeni int ip_mc_validate_source(struct sk_buff *skb, __be32 daddr, __be32 saddr,
179bc044e8dSPaolo Abeni 			  u8 tos, struct net_device *dev,
180bc044e8dSPaolo Abeni 			  struct in_device *in_dev, u32 *itag);
1812702c4bbSJoe Perches int ip_route_input_noref(struct sk_buff *skb, __be32 dst, __be32 src,
18238a424e4SDavid Miller 			 u8 tos, struct net_device *devin);
1835510cdf7SDavid Ahern int ip_route_input_rcu(struct sk_buff *skb, __be32 dst, __be32 src,
1845510cdf7SDavid Ahern 		       u8 tos, struct net_device *devin,
1855510cdf7SDavid Ahern 		       struct fib_result *res);
186407eadd9SEric Dumazet 
187c6cffba4SDavid S. Miller static inline int ip_route_input(struct sk_buff *skb, __be32 dst, __be32 src,
188c6cffba4SDavid S. Miller 				 u8 tos, struct net_device *devin)
189c6cffba4SDavid S. Miller {
190c6cffba4SDavid S. Miller 	int err;
191c6cffba4SDavid S. Miller 
192c6cffba4SDavid S. Miller 	rcu_read_lock();
193c6cffba4SDavid S. Miller 	err = ip_route_input_noref(skb, dst, src, tos, devin);
19464327fc8SStefano Brivio 	if (!err) {
195222d7dbdSEric Dumazet 		skb_dst_force(skb);
1969df16efaSWei Wang 		if (!skb_dst(skb))
1979df16efaSWei Wang 			err = -EINVAL;
19864327fc8SStefano Brivio 	}
199c6cffba4SDavid S. Miller 	rcu_read_unlock();
200c6cffba4SDavid S. Miller 
201c6cffba4SDavid S. Miller 	return err;
202c6cffba4SDavid S. Miller }
203c6cffba4SDavid S. Miller 
2042702c4bbSJoe Perches void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu, int oif,
205d888f396SMaciej Żenczykowski 		      u8 protocol);
2062702c4bbSJoe Perches void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu);
2071042caa7SMaciej Żenczykowski void ipv4_redirect(struct sk_buff *skb, struct net *net, int oif, u8 protocol);
2082702c4bbSJoe Perches void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk);
2092702c4bbSJoe Perches void ip_rt_send_redirect(struct sk_buff *skb);
2101da177e4SLinus Torvalds 
2112702c4bbSJoe Perches unsigned int inet_addr_type(struct net *net, __be32 addr);
2129b8ff518SDavid Ahern unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id);
2132702c4bbSJoe Perches unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
2142702c4bbSJoe Perches 				__be32 addr);
21530bbaa19SDavid Ahern unsigned int inet_addr_type_dev_table(struct net *net,
21630bbaa19SDavid Ahern 				      const struct net_device *dev,
21730bbaa19SDavid Ahern 				      __be32 addr);
2182702c4bbSJoe Perches void ip_rt_multicast_event(struct in_device *);
219ca25c300SAl Viro int ip_rt_ioctl(struct net *, unsigned int cmd, struct rtentry *rt);
2202702c4bbSJoe Perches void ip_rt_get_source(u8 *src, struct sk_buff *skb, struct rtable *rt);
2219ab179d8SDavid Ahern struct rtable *rt_dst_alloc(struct net_device *dev,
2229ab179d8SDavid Ahern 			     unsigned int flags, u16 type,
2239ab179d8SDavid Ahern 			     bool nopolicy, bool noxfrm, bool will_cache);
2241da177e4SLinus Torvalds 
2250ff60a45SJamal Hadi Salim struct in_ifaddr;
2262702c4bbSJoe Perches void fib_add_ifaddr(struct in_ifaddr *);
2272702c4bbSJoe Perches void fib_del_ifaddr(struct in_ifaddr *, struct in_ifaddr *);
228af4d768aSDavid Ahern void fib_modify_prefix_metric(struct in_ifaddr *ifa, u32 new_metric);
2290ff60a45SJamal Hadi Salim 
230510c321bSXin Long void rt_add_uncached_list(struct rtable *rt);
231510c321bSXin Long void rt_del_uncached_list(struct rtable *rt);
232510c321bSXin Long 
2331da177e4SLinus Torvalds static inline void ip_rt_put(struct rtable *rt)
2341da177e4SLinus Torvalds {
2356da025faSEric Dumazet 	/* dst_release() accepts a NULL parameter.
2366da025faSEric Dumazet 	 * We rely on dst being first structure in struct rtable
2376da025faSEric Dumazet 	 */
2386da025faSEric Dumazet 	BUILD_BUG_ON(offsetof(struct rtable, dst) != 0);
239d8d1f30bSChangli Gao 	dst_release(&rt->dst);
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds #define IPTOS_RT_MASK	(IPTOS_TOS_MASK & ~3)
2431da177e4SLinus Torvalds 
2444839c52bSPhilippe De Muyter extern const __u8 ip_tos2prio[16];
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds static inline char rt_tos2priority(u8 tos)
2471da177e4SLinus Torvalds {
2481da177e4SLinus Torvalds 	return ip_tos2prio[IPTOS_TOS(tos)>>1];
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds 
2512d7192d6SDavid S. Miller /* ip_route_connect() and ip_route_newports() work in tandem whilst
2522d7192d6SDavid S. Miller  * binding a socket for a new outgoing connection.
2532d7192d6SDavid S. Miller  *
2542d7192d6SDavid S. Miller  * In order to use IPSEC properly, we must, in the end, have a
2552d7192d6SDavid S. Miller  * route that was looked up using all available keys including source
2562d7192d6SDavid S. Miller  * and destination ports.
2572d7192d6SDavid S. Miller  *
2582d7192d6SDavid S. Miller  * However, if a source port needs to be allocated (the user specified
2592d7192d6SDavid S. Miller  * a wildcard source port) we need to obtain addressing information
2602d7192d6SDavid S. Miller  * in order to perform that allocation.
2612d7192d6SDavid S. Miller  *
2622d7192d6SDavid S. Miller  * So ip_route_connect() looks up a route using wildcarded source and
2632d7192d6SDavid S. Miller  * destination ports in the key, simply so that we can get a pair of
2642d7192d6SDavid S. Miller  * addresses to use for port allocation.
2652d7192d6SDavid S. Miller  *
2662d7192d6SDavid S. Miller  * Later, once the ports are allocated, ip_route_newports() will make
2672d7192d6SDavid S. Miller  * another route lookup if needed to make sure we catch any IPSEC
2682d7192d6SDavid S. Miller  * rules keyed on the port information.
2692d7192d6SDavid S. Miller  *
2702d7192d6SDavid S. Miller  * The callers allocate the flow key on their stack, and must pass in
2712d7192d6SDavid S. Miller  * the same flowi4 object to both the ip_route_connect() and the
2722d7192d6SDavid S. Miller  * ip_route_newports() calls.
2732d7192d6SDavid S. Miller  */
2742d7192d6SDavid S. Miller 
2752d7192d6SDavid S. Miller static inline void ip_route_connect_init(struct flowi4 *fl4, __be32 dst, __be32 src,
2762d7192d6SDavid S. Miller 					 u32 tos, int oif, u8 protocol,
277b23dd4feSDavid S. Miller 					 __be16 sport, __be16 dport,
2780e0d44abSSteffen Klassert 					 struct sock *sk)
2791da177e4SLinus Torvalds {
2802d7192d6SDavid S. Miller 	__u8 flow_flags = 0;
28179876874SKOVACS Krisztian 
28279876874SKOVACS Krisztian 	if (inet_sk(sk)->transparent)
28394b92b88SDavid S. Miller 		flow_flags |= FLOWI_FLAG_ANYSRC;
28494b92b88SDavid S. Miller 
2852d7192d6SDavid S. Miller 	flowi4_init_output(fl4, oif, sk->sk_mark, tos, RT_SCOPE_UNIVERSE,
286e2d118a1SLorenzo Colitti 			   protocol, flow_flags, dst, src, dport, sport,
287e2d118a1SLorenzo Colitti 			   sk->sk_uid);
2882d7192d6SDavid S. Miller }
2892d7192d6SDavid S. Miller 
2902d7192d6SDavid S. Miller static inline struct rtable *ip_route_connect(struct flowi4 *fl4,
2912d7192d6SDavid S. Miller 					      __be32 dst, __be32 src, u32 tos,
2922d7192d6SDavid S. Miller 					      int oif, u8 protocol,
2932d7192d6SDavid S. Miller 					      __be16 sport, __be16 dport,
2940e0d44abSSteffen Klassert 					      struct sock *sk)
2952d7192d6SDavid S. Miller {
2962d7192d6SDavid S. Miller 	struct net *net = sock_net(sk);
2972d7192d6SDavid S. Miller 	struct rtable *rt;
2982d7192d6SDavid S. Miller 
2992d7192d6SDavid S. Miller 	ip_route_connect_init(fl4, dst, src, tos, oif, protocol,
3000e0d44abSSteffen Klassert 			      sport, dport, sk);
30179876874SKOVACS Krisztian 
3021da177e4SLinus Torvalds 	if (!dst || !src) {
3032d7192d6SDavid S. Miller 		rt = __ip_route_output_key(net, fl4);
304b23dd4feSDavid S. Miller 		if (IS_ERR(rt))
305b23dd4feSDavid S. Miller 			return rt;
306b23dd4feSDavid S. Miller 		ip_rt_put(rt);
307e6b45241SJulian Anastasov 		flowi4_update_output(fl4, oif, tos, fl4->daddr, fl4->saddr);
3081da177e4SLinus Torvalds 	}
3092d7192d6SDavid S. Miller 	security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
3102d7192d6SDavid S. Miller 	return ip_route_output_flow(net, fl4, sk);
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
3132d7192d6SDavid S. Miller static inline struct rtable *ip_route_newports(struct flowi4 *fl4, struct rtable *rt,
3142d7192d6SDavid S. Miller 					       __be16 orig_sport, __be16 orig_dport,
3152d7192d6SDavid S. Miller 					       __be16 sport, __be16 dport,
3162d7192d6SDavid S. Miller 					       struct sock *sk)
3171da177e4SLinus Torvalds {
318dca8b089SDavid S. Miller 	if (sport != orig_sport || dport != orig_dport) {
3192d7192d6SDavid S. Miller 		fl4->fl4_dport = dport;
3202d7192d6SDavid S. Miller 		fl4->fl4_sport = sport;
321b23dd4feSDavid S. Miller 		ip_rt_put(rt);
322e6b45241SJulian Anastasov 		flowi4_update_output(fl4, sk->sk_bound_dev_if,
323e6b45241SJulian Anastasov 				     RT_CONN_FLAGS(sk), fl4->daddr,
324e6b45241SJulian Anastasov 				     fl4->saddr);
3252d7192d6SDavid S. Miller 		security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
3262d7192d6SDavid S. Miller 		return ip_route_output_flow(sock_net(sk), fl4, sk);
3271da177e4SLinus Torvalds 	}
328b23dd4feSDavid S. Miller 	return rt;
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
3311668e010SKOVACS Krisztian static inline int inet_iif(const struct sk_buff *skb)
3321668e010SKOVACS Krisztian {
3330340d0b9STom Herbert 	struct rtable *rt = skb_rtable(skb);
33413378cadSDavid S. Miller 
3350340d0b9STom Herbert 	if (rt && rt->rt_iif)
3360340d0b9STom Herbert 		return rt->rt_iif;
3370340d0b9STom Herbert 
33813378cadSDavid S. Miller 	return skb->skb_iif;
3391668e010SKOVACS Krisztian }
3401668e010SKOVACS Krisztian 
341323e126fSDavid S. Miller static inline int ip4_dst_hoplimit(const struct dst_entry *dst)
342323e126fSDavid S. Miller {
343323e126fSDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
344fa50d974SNikolay Borisov 	struct net *net = dev_net(dst->dev);
345323e126fSDavid S. Miller 
346323e126fSDavid S. Miller 	if (hoplimit == 0)
347fa50d974SNikolay Borisov 		hoplimit = net->ipv4.sysctl_ip_default_ttl;
348323e126fSDavid S. Miller 	return hoplimit;
349323e126fSDavid S. Miller }
350323e126fSDavid S. Miller 
3515c9f7c1dSDavid Ahern static inline struct neighbour *ip_neigh_gw4(struct net_device *dev,
3525c9f7c1dSDavid Ahern 					     __be32 daddr)
3535c9f7c1dSDavid Ahern {
3545c9f7c1dSDavid Ahern 	struct neighbour *neigh;
3555c9f7c1dSDavid Ahern 
3565c9f7c1dSDavid Ahern 	neigh = __ipv4_neigh_lookup_noref(dev, daddr);
3575c9f7c1dSDavid Ahern 	if (unlikely(!neigh))
3585c9f7c1dSDavid Ahern 		neigh = __neigh_create(&arp_tbl, &daddr, dev, false);
3595c9f7c1dSDavid Ahern 
3605c9f7c1dSDavid Ahern 	return neigh;
3615c9f7c1dSDavid Ahern }
3625c9f7c1dSDavid Ahern 
3635c9f7c1dSDavid Ahern static inline struct neighbour *ip_neigh_for_gw(struct rtable *rt,
3645c9f7c1dSDavid Ahern 						struct sk_buff *skb,
3655c9f7c1dSDavid Ahern 						bool *is_v6gw)
3665c9f7c1dSDavid Ahern {
3675c9f7c1dSDavid Ahern 	struct net_device *dev = rt->dst.dev;
3685c9f7c1dSDavid Ahern 	struct neighbour *neigh;
3695c9f7c1dSDavid Ahern 
3705c9f7c1dSDavid Ahern 	if (likely(rt->rt_gw_family == AF_INET)) {
3715c9f7c1dSDavid Ahern 		neigh = ip_neigh_gw4(dev, rt->rt_gw4);
3725c9f7c1dSDavid Ahern 	} else if (rt->rt_gw_family == AF_INET6) {
3735c9f7c1dSDavid Ahern 		neigh = ip_neigh_gw6(dev, &rt->rt_gw6);
3745c9f7c1dSDavid Ahern 		*is_v6gw = true;
3755c9f7c1dSDavid Ahern 	} else {
3765c9f7c1dSDavid Ahern 		neigh = ip_neigh_gw4(dev, ip_hdr(skb)->daddr);
3775c9f7c1dSDavid Ahern 	}
3785c9f7c1dSDavid Ahern 	return neigh;
3795c9f7c1dSDavid Ahern }
3805c9f7c1dSDavid Ahern 
3811da177e4SLinus Torvalds #endif	/* _ROUTE_H */
382