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 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)) 41aa661581SFrancesco Fusco #define RT_CONN_FLAGS_TOS(sk,tos) (RT_TOS(tos) | sock_flag(sk, SOCK_LOCALROUTE)) 421da177e4SLinus Torvalds 4367e1e2f4SGuillaume Nault static inline __u8 ip_sock_rt_scope(const struct sock *sk) 4467e1e2f4SGuillaume Nault { 4567e1e2f4SGuillaume Nault if (sock_flag(sk, SOCK_LOCALROUTE)) 4667e1e2f4SGuillaume Nault return RT_SCOPE_LINK; 4767e1e2f4SGuillaume Nault 4867e1e2f4SGuillaume Nault return RT_SCOPE_UNIVERSE; 4967e1e2f4SGuillaume Nault } 5067e1e2f4SGuillaume Nault 5167e1e2f4SGuillaume Nault static inline __u8 ip_sock_rt_tos(const struct sock *sk) 5267e1e2f4SGuillaume Nault { 5367e1e2f4SGuillaume Nault return RT_TOS(inet_sk(sk)->tos); 5467e1e2f4SGuillaume Nault } 5567e1e2f4SGuillaume Nault 56b6459415SJakub Kicinski struct ip_tunnel_info; 571da177e4SLinus Torvalds struct fib_nh; 5862fa8a84SDavid S. Miller struct fib_info; 595055c371SEric Dumazet struct uncached_list; 60fd2c3ef7SEric Dumazet struct rtable { 611da177e4SLinus Torvalds struct dst_entry dst; 621da177e4SLinus Torvalds 6329e75252SEric Dumazet int rt_genid; 6495c96174SEric Dumazet unsigned int rt_flags; 651da177e4SLinus Torvalds __u16 rt_type; 66155e8336SJulian Anastasov __u8 rt_is_input; 6777d5bc7eSDavid Ahern __u8 rt_uses_gateway; 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds int rt_iif; 701da177e4SLinus Torvalds 7177d5bc7eSDavid Ahern u8 rt_gw_family; 721da177e4SLinus Torvalds /* Info on neighbour */ 730f5f7d7bSDavid Ahern union { 741550c171SDavid Ahern __be32 rt_gw4; 750f5f7d7bSDavid Ahern struct in6_addr rt_gw6; 760f5f7d7bSDavid Ahern }; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds /* Miscellaneous cached information */ 79d52e5a7eSSabrina Dubroca u32 rt_mtu_locked:1, 80d52e5a7eSSabrina Dubroca rt_pmtu:31; 811da177e4SLinus Torvalds }; 821da177e4SLinus Torvalds 83b8400f37SSteffen Klassert static inline bool rt_is_input_route(const struct rtable *rt) 84c7537967SDavid S. Miller { 859917e1e8SDavid S. Miller return rt->rt_is_input != 0; 86c7537967SDavid S. Miller } 87c7537967SDavid S. Miller 88b8400f37SSteffen Klassert static inline bool rt_is_output_route(const struct rtable *rt) 89c7537967SDavid S. Miller { 909917e1e8SDavid S. Miller return rt->rt_is_input == 0; 91c7537967SDavid S. Miller } 92c7537967SDavid S. Miller 93f8126f1dSDavid S. Miller static inline __be32 rt_nexthop(const struct rtable *rt, __be32 daddr) 94f8126f1dSDavid S. Miller { 951550c171SDavid Ahern if (rt->rt_gw_family == AF_INET) 961550c171SDavid Ahern return rt->rt_gw4; 97f8126f1dSDavid S. Miller return daddr; 98f8126f1dSDavid S. Miller } 99f8126f1dSDavid S. Miller 100fd2c3ef7SEric Dumazet struct ip_rt_acct { 1011da177e4SLinus Torvalds __u32 o_bytes; 1021da177e4SLinus Torvalds __u32 o_packets; 1031da177e4SLinus Torvalds __u32 i_bytes; 1041da177e4SLinus Torvalds __u32 i_packets; 1051da177e4SLinus Torvalds }; 1061da177e4SLinus Torvalds 107fd2c3ef7SEric Dumazet struct rt_cache_stat { 1081da177e4SLinus Torvalds unsigned int in_slow_tot; 1091da177e4SLinus Torvalds unsigned int in_slow_mc; 1101da177e4SLinus Torvalds unsigned int in_no_route; 1111da177e4SLinus Torvalds unsigned int in_brd; 1121da177e4SLinus Torvalds unsigned int in_martian_dst; 1131da177e4SLinus Torvalds unsigned int in_martian_src; 1141da177e4SLinus Torvalds unsigned int out_slow_tot; 1151da177e4SLinus Torvalds unsigned int out_slow_mc; 1161da177e4SLinus Torvalds }; 1171da177e4SLinus Torvalds 1187d720c3eSTejun Heo extern struct ip_rt_acct __percpu *ip_rt_acct; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds struct in_device; 1212702c4bbSJoe Perches 1222702c4bbSJoe Perches int ip_rt_init(void); 1232702c4bbSJoe Perches void rt_cache_flush(struct net *net); 1242702c4bbSJoe Perches void rt_flush_dev(struct net_device *dev); 1253abd1adeSDavid Ahern struct rtable *ip_route_output_key_hash(struct net *net, struct flowi4 *flp, 1263abd1adeSDavid Ahern const struct sk_buff *skb); 1273abd1adeSDavid Ahern struct rtable *ip_route_output_key_hash_rcu(struct net *net, struct flowi4 *flp, 1283abd1adeSDavid Ahern struct fib_result *res, 129bf4e0a3dSNikolay Aleksandrov const struct sk_buff *skb); 13079a13159SPeter Nørlund 13179a13159SPeter Nørlund static inline struct rtable *__ip_route_output_key(struct net *net, 13279a13159SPeter Nørlund struct flowi4 *flp) 13379a13159SPeter Nørlund { 1343abd1adeSDavid Ahern return ip_route_output_key_hash(net, flp, NULL); 13579a13159SPeter Nørlund } 13679a13159SPeter Nørlund 1372702c4bbSJoe Perches struct rtable *ip_route_output_flow(struct net *, struct flowi4 *flp, 1386f9c9615SEric Dumazet const struct sock *sk); 139571912c6SMartin Varghese struct rtable *ip_route_output_tunnel(struct sk_buff *skb, 140571912c6SMartin Varghese struct net_device *dev, 141571912c6SMartin Varghese struct net *net, __be32 *saddr, 142571912c6SMartin Varghese const struct ip_tunnel_info *info, 143571912c6SMartin Varghese u8 protocol, bool use_cache); 144571912c6SMartin Varghese 1452702c4bbSJoe Perches struct dst_entry *ipv4_blackhole_route(struct net *net, 1462702c4bbSJoe Perches struct dst_entry *dst_orig); 147407eadd9SEric Dumazet 1489d6ec938SDavid S. Miller static inline struct rtable *ip_route_output_key(struct net *net, struct flowi4 *flp) 1495bfa787fSDavid S. Miller { 1505bfa787fSDavid S. Miller return ip_route_output_flow(net, flp, NULL); 1515bfa787fSDavid S. Miller } 1525bfa787fSDavid S. Miller 15378fbfd8aSDavid S. Miller static inline struct rtable *ip_route_output(struct net *net, __be32 daddr, 15478fbfd8aSDavid S. Miller __be32 saddr, u8 tos, int oif) 15578fbfd8aSDavid S. Miller { 1569d6ec938SDavid S. Miller struct flowi4 fl4 = { 1579d6ec938SDavid S. Miller .flowi4_oif = oif, 158c5d21c4bSRoland Dreier .flowi4_tos = tos, 1599d6ec938SDavid S. Miller .daddr = daddr, 1609d6ec938SDavid S. Miller .saddr = saddr, 16178fbfd8aSDavid S. Miller }; 1629d6ec938SDavid S. Miller return ip_route_output_key(net, &fl4); 16378fbfd8aSDavid S. Miller } 16478fbfd8aSDavid S. Miller 16531e4543dSDavid S. Miller static inline struct rtable *ip_route_output_ports(struct net *net, struct flowi4 *fl4, 166*8d6eba33SGuillaume Nault const struct sock *sk, 16778fbfd8aSDavid S. Miller __be32 daddr, __be32 saddr, 16878fbfd8aSDavid S. Miller __be16 dport, __be16 sport, 16978fbfd8aSDavid S. Miller __u8 proto, __u8 tos, int oif) 17078fbfd8aSDavid S. Miller { 17131e4543dSDavid S. Miller flowi4_init_output(fl4, oif, sk ? sk->sk_mark : 0, tos, 17294b92b88SDavid S. Miller RT_SCOPE_UNIVERSE, proto, 17394b92b88SDavid S. Miller sk ? inet_sk_flowi_flags(sk) : 0, 174e2d118a1SLorenzo Colitti daddr, saddr, dport, sport, sock_net_uid(net, sk)); 17578fbfd8aSDavid S. Miller if (sk) 1763df98d79SPaul Moore security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4)); 17731e4543dSDavid S. Miller return ip_route_output_flow(net, fl4, sk); 17878fbfd8aSDavid S. Miller } 17978fbfd8aSDavid S. Miller 180cbb1e85fSDavid S. Miller static inline struct rtable *ip_route_output_gre(struct net *net, struct flowi4 *fl4, 18178fbfd8aSDavid S. Miller __be32 daddr, __be32 saddr, 18278fbfd8aSDavid S. Miller __be32 gre_key, __u8 tos, int oif) 18378fbfd8aSDavid S. Miller { 184cbb1e85fSDavid S. Miller memset(fl4, 0, sizeof(*fl4)); 185cbb1e85fSDavid S. Miller fl4->flowi4_oif = oif; 186cbb1e85fSDavid S. Miller fl4->daddr = daddr; 187cbb1e85fSDavid S. Miller fl4->saddr = saddr; 188cbb1e85fSDavid S. Miller fl4->flowi4_tos = tos; 189cbb1e85fSDavid S. Miller fl4->flowi4_proto = IPPROTO_GRE; 190cbb1e85fSDavid S. Miller fl4->fl4_gre_key = gre_key; 191cbb1e85fSDavid S. Miller return ip_route_output_key(net, fl4); 19278fbfd8aSDavid S. Miller } 193bc044e8dSPaolo Abeni int ip_mc_validate_source(struct sk_buff *skb, __be32 daddr, __be32 saddr, 194bc044e8dSPaolo Abeni u8 tos, struct net_device *dev, 195bc044e8dSPaolo Abeni struct in_device *in_dev, u32 *itag); 1962702c4bbSJoe Perches int ip_route_input_noref(struct sk_buff *skb, __be32 dst, __be32 src, 19738a424e4SDavid Miller u8 tos, struct net_device *devin); 19802b24941SPaolo Abeni int ip_route_use_hint(struct sk_buff *skb, __be32 dst, __be32 src, 19902b24941SPaolo Abeni u8 tos, struct net_device *devin, 20002b24941SPaolo Abeni const struct sk_buff *hint); 20102b24941SPaolo Abeni 202c6cffba4SDavid S. Miller static inline int ip_route_input(struct sk_buff *skb, __be32 dst, __be32 src, 203c6cffba4SDavid S. Miller u8 tos, struct net_device *devin) 204c6cffba4SDavid S. Miller { 205c6cffba4SDavid S. Miller int err; 206c6cffba4SDavid S. Miller 207c6cffba4SDavid S. Miller rcu_read_lock(); 208c6cffba4SDavid S. Miller err = ip_route_input_noref(skb, dst, src, tos, devin); 20964327fc8SStefano Brivio if (!err) { 210222d7dbdSEric Dumazet skb_dst_force(skb); 2119df16efaSWei Wang if (!skb_dst(skb)) 2129df16efaSWei Wang err = -EINVAL; 21364327fc8SStefano Brivio } 214c6cffba4SDavid S. Miller rcu_read_unlock(); 215c6cffba4SDavid S. Miller 216c6cffba4SDavid S. Miller return err; 217c6cffba4SDavid S. Miller } 218c6cffba4SDavid S. Miller 2192702c4bbSJoe Perches void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu, int oif, 220d888f396SMaciej Żenczykowski u8 protocol); 2212702c4bbSJoe Perches void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu); 2221042caa7SMaciej Żenczykowski void ipv4_redirect(struct sk_buff *skb, struct net *net, int oif, u8 protocol); 2232702c4bbSJoe Perches void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk); 2242702c4bbSJoe Perches void ip_rt_send_redirect(struct sk_buff *skb); 2251da177e4SLinus Torvalds 2262702c4bbSJoe Perches unsigned int inet_addr_type(struct net *net, __be32 addr); 2279b8ff518SDavid Ahern unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id); 2282702c4bbSJoe Perches unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev, 2292702c4bbSJoe Perches __be32 addr); 23030bbaa19SDavid Ahern unsigned int inet_addr_type_dev_table(struct net *net, 23130bbaa19SDavid Ahern const struct net_device *dev, 23230bbaa19SDavid Ahern __be32 addr); 2332702c4bbSJoe Perches void ip_rt_multicast_event(struct in_device *); 234ca25c300SAl Viro int ip_rt_ioctl(struct net *, unsigned int cmd, struct rtentry *rt); 2352702c4bbSJoe Perches void ip_rt_get_source(u8 *src, struct sk_buff *skb, struct rtable *rt); 2369ab179d8SDavid Ahern struct rtable *rt_dst_alloc(struct net_device *dev, 237b5c8b3feSEyal Birger unsigned int flags, u16 type, bool noxfrm); 2385b18f128SStephen Suryaputra struct rtable *rt_dst_clone(struct net_device *dev, struct rtable *rt); 2391da177e4SLinus Torvalds 2400ff60a45SJamal Hadi Salim struct in_ifaddr; 2412702c4bbSJoe Perches void fib_add_ifaddr(struct in_ifaddr *); 2422702c4bbSJoe Perches void fib_del_ifaddr(struct in_ifaddr *, struct in_ifaddr *); 243af4d768aSDavid Ahern void fib_modify_prefix_metric(struct in_ifaddr *ifa, u32 new_metric); 2440ff60a45SJamal Hadi Salim 245510c321bSXin Long void rt_add_uncached_list(struct rtable *rt); 246510c321bSXin Long void rt_del_uncached_list(struct rtable *rt); 247510c321bSXin Long 248ee28906fSStefano Brivio int fib_dump_info_fnhe(struct sk_buff *skb, struct netlink_callback *cb, 249ee28906fSStefano Brivio u32 table_id, struct fib_info *fi, 250e93fb3e9SJohn Fastabend int *fa_index, int fa_start, unsigned int flags); 251ee28906fSStefano Brivio 2521da177e4SLinus Torvalds static inline void ip_rt_put(struct rtable *rt) 2531da177e4SLinus Torvalds { 2546da025faSEric Dumazet /* dst_release() accepts a NULL parameter. 2556da025faSEric Dumazet * We rely on dst being first structure in struct rtable 2566da025faSEric Dumazet */ 2576da025faSEric Dumazet BUILD_BUG_ON(offsetof(struct rtable, dst) != 0); 258d8d1f30bSChangli Gao dst_release(&rt->dst); 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds #define IPTOS_RT_MASK (IPTOS_TOS_MASK & ~3) 2621da177e4SLinus Torvalds 2634839c52bSPhilippe De Muyter extern const __u8 ip_tos2prio[16]; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds static inline char rt_tos2priority(u8 tos) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds return ip_tos2prio[IPTOS_TOS(tos)>>1]; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2702d7192d6SDavid S. Miller /* ip_route_connect() and ip_route_newports() work in tandem whilst 2712d7192d6SDavid S. Miller * binding a socket for a new outgoing connection. 2722d7192d6SDavid S. Miller * 2732d7192d6SDavid S. Miller * In order to use IPSEC properly, we must, in the end, have a 2742d7192d6SDavid S. Miller * route that was looked up using all available keys including source 2752d7192d6SDavid S. Miller * and destination ports. 2762d7192d6SDavid S. Miller * 2772d7192d6SDavid S. Miller * However, if a source port needs to be allocated (the user specified 2782d7192d6SDavid S. Miller * a wildcard source port) we need to obtain addressing information 2792d7192d6SDavid S. Miller * in order to perform that allocation. 2802d7192d6SDavid S. Miller * 2812d7192d6SDavid S. Miller * So ip_route_connect() looks up a route using wildcarded source and 2822d7192d6SDavid S. Miller * destination ports in the key, simply so that we can get a pair of 2832d7192d6SDavid S. Miller * addresses to use for port allocation. 2842d7192d6SDavid S. Miller * 2852d7192d6SDavid S. Miller * Later, once the ports are allocated, ip_route_newports() will make 2862d7192d6SDavid S. Miller * another route lookup if needed to make sure we catch any IPSEC 2872d7192d6SDavid S. Miller * rules keyed on the port information. 2882d7192d6SDavid S. Miller * 2892d7192d6SDavid S. Miller * The callers allocate the flow key on their stack, and must pass in 2902d7192d6SDavid S. Miller * the same flowi4 object to both the ip_route_connect() and the 2912d7192d6SDavid S. Miller * ip_route_newports() calls. 2922d7192d6SDavid S. Miller */ 2932d7192d6SDavid S. Miller 29467e1e2f4SGuillaume Nault static inline void ip_route_connect_init(struct flowi4 *fl4, __be32 dst, 29567e1e2f4SGuillaume Nault __be32 src, int oif, u8 protocol, 296b23dd4feSDavid S. Miller __be16 sport, __be16 dport, 29767e1e2f4SGuillaume Nault const struct sock *sk) 2981da177e4SLinus Torvalds { 2992d7192d6SDavid S. Miller __u8 flow_flags = 0; 30079876874SKOVACS Krisztian 30179876874SKOVACS Krisztian if (inet_sk(sk)->transparent) 30294b92b88SDavid S. Miller flow_flags |= FLOWI_FLAG_ANYSRC; 30394b92b88SDavid S. Miller 30467e1e2f4SGuillaume Nault flowi4_init_output(fl4, oif, sk->sk_mark, ip_sock_rt_tos(sk), 30567e1e2f4SGuillaume Nault ip_sock_rt_scope(sk), protocol, flow_flags, dst, 30667e1e2f4SGuillaume Nault src, dport, sport, sk->sk_uid); 3072d7192d6SDavid S. Miller } 3082d7192d6SDavid S. Miller 30967e1e2f4SGuillaume Nault static inline struct rtable *ip_route_connect(struct flowi4 *fl4, __be32 dst, 31067e1e2f4SGuillaume Nault __be32 src, int oif, u8 protocol, 3112d7192d6SDavid S. Miller __be16 sport, __be16 dport, 312*8d6eba33SGuillaume Nault const struct sock *sk) 3132d7192d6SDavid S. Miller { 3142d7192d6SDavid S. Miller struct net *net = sock_net(sk); 3152d7192d6SDavid S. Miller struct rtable *rt; 3162d7192d6SDavid S. Miller 31767e1e2f4SGuillaume Nault ip_route_connect_init(fl4, dst, src, oif, protocol, sport, dport, sk); 31879876874SKOVACS Krisztian 3191da177e4SLinus Torvalds if (!dst || !src) { 3202d7192d6SDavid S. Miller rt = __ip_route_output_key(net, fl4); 321b23dd4feSDavid S. Miller if (IS_ERR(rt)) 322b23dd4feSDavid S. Miller return rt; 323b23dd4feSDavid S. Miller ip_rt_put(rt); 3243f06760cSGuillaume Nault flowi4_update_output(fl4, oif, fl4->daddr, fl4->saddr); 3251da177e4SLinus Torvalds } 3263df98d79SPaul Moore security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4)); 3272d7192d6SDavid S. Miller return ip_route_output_flow(net, fl4, sk); 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds 3302d7192d6SDavid S. Miller static inline struct rtable *ip_route_newports(struct flowi4 *fl4, struct rtable *rt, 3312d7192d6SDavid S. Miller __be16 orig_sport, __be16 orig_dport, 3322d7192d6SDavid S. Miller __be16 sport, __be16 dport, 333*8d6eba33SGuillaume Nault const struct sock *sk) 3341da177e4SLinus Torvalds { 335dca8b089SDavid S. Miller if (sport != orig_sport || dport != orig_dport) { 3362d7192d6SDavid S. Miller fl4->fl4_dport = dport; 3372d7192d6SDavid S. Miller fl4->fl4_sport = sport; 338b23dd4feSDavid S. Miller ip_rt_put(rt); 3393f06760cSGuillaume Nault flowi4_update_output(fl4, sk->sk_bound_dev_if, fl4->daddr, 340e6b45241SJulian Anastasov fl4->saddr); 3413df98d79SPaul Moore security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4)); 3422d7192d6SDavid S. Miller return ip_route_output_flow(sock_net(sk), fl4, sk); 3431da177e4SLinus Torvalds } 344b23dd4feSDavid S. Miller return rt; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 3471668e010SKOVACS Krisztian static inline int inet_iif(const struct sk_buff *skb) 3481668e010SKOVACS Krisztian { 3490340d0b9STom Herbert struct rtable *rt = skb_rtable(skb); 35013378cadSDavid S. Miller 3510340d0b9STom Herbert if (rt && rt->rt_iif) 3520340d0b9STom Herbert return rt->rt_iif; 3530340d0b9STom Herbert 35413378cadSDavid S. Miller return skb->skb_iif; 3551668e010SKOVACS Krisztian } 3561668e010SKOVACS Krisztian 357323e126fSDavid S. Miller static inline int ip4_dst_hoplimit(const struct dst_entry *dst) 358323e126fSDavid S. Miller { 359323e126fSDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 360fa50d974SNikolay Borisov struct net *net = dev_net(dst->dev); 361323e126fSDavid S. Miller 362323e126fSDavid S. Miller if (hoplimit == 0) 3638281b7ecSKuniyuki Iwashima hoplimit = READ_ONCE(net->ipv4.sysctl_ip_default_ttl); 364323e126fSDavid S. Miller return hoplimit; 365323e126fSDavid S. Miller } 366323e126fSDavid S. Miller 3675c9f7c1dSDavid Ahern static inline struct neighbour *ip_neigh_gw4(struct net_device *dev, 3685c9f7c1dSDavid Ahern __be32 daddr) 3695c9f7c1dSDavid Ahern { 3705c9f7c1dSDavid Ahern struct neighbour *neigh; 3715c9f7c1dSDavid Ahern 3723c42b201SEric Dumazet neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)daddr); 3735c9f7c1dSDavid Ahern if (unlikely(!neigh)) 3745c9f7c1dSDavid Ahern neigh = __neigh_create(&arp_tbl, &daddr, dev, false); 3755c9f7c1dSDavid Ahern 3765c9f7c1dSDavid Ahern return neigh; 3775c9f7c1dSDavid Ahern } 3785c9f7c1dSDavid Ahern 3795c9f7c1dSDavid Ahern static inline struct neighbour *ip_neigh_for_gw(struct rtable *rt, 3805c9f7c1dSDavid Ahern struct sk_buff *skb, 3815c9f7c1dSDavid Ahern bool *is_v6gw) 3825c9f7c1dSDavid Ahern { 3835c9f7c1dSDavid Ahern struct net_device *dev = rt->dst.dev; 3845c9f7c1dSDavid Ahern struct neighbour *neigh; 3855c9f7c1dSDavid Ahern 3865c9f7c1dSDavid Ahern if (likely(rt->rt_gw_family == AF_INET)) { 3875c9f7c1dSDavid Ahern neigh = ip_neigh_gw4(dev, rt->rt_gw4); 3885c9f7c1dSDavid Ahern } else if (rt->rt_gw_family == AF_INET6) { 3895c9f7c1dSDavid Ahern neigh = ip_neigh_gw6(dev, &rt->rt_gw6); 3905c9f7c1dSDavid Ahern *is_v6gw = true; 3915c9f7c1dSDavid Ahern } else { 3925c9f7c1dSDavid Ahern neigh = ip_neigh_gw4(dev, ip_hdr(skb)->daddr); 3935c9f7c1dSDavid Ahern } 3945c9f7c1dSDavid Ahern return neigh; 3955c9f7c1dSDavid Ahern } 3965c9f7c1dSDavid Ahern 3971da177e4SLinus Torvalds #endif /* _ROUTE_H */ 398