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> 33c6cffba4SDavid S. Miller #include <linux/rcupdate.h> 341da177e4SLinus Torvalds #include <linux/route.h> 351da177e4SLinus Torvalds #include <linux/ip.h> 361da177e4SLinus Torvalds #include <linux/cache.h> 37beb8d13bSVenkat Yekkirala #include <linux/security.h> 381da177e4SLinus Torvalds 39f87c10a8SHannes Frederic Sowa /* IPv4 datagram length is stored into 16bit field (tot_len) */ 40f87c10a8SHannes Frederic Sowa #define IP_MAX_MTU 0xFFFFU 41f87c10a8SHannes Frederic Sowa 421da177e4SLinus Torvalds #define RTO_ONLINK 0x01 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #define RT_CONN_FLAGS(sk) (RT_TOS(inet_sk(sk)->tos) | sock_flag(sk, SOCK_LOCALROUTE)) 45aa661581SFrancesco Fusco #define RT_CONN_FLAGS_TOS(sk,tos) (RT_TOS(tos) | sock_flag(sk, SOCK_LOCALROUTE)) 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds struct fib_nh; 4862fa8a84SDavid S. Miller struct fib_info; 495055c371SEric Dumazet struct uncached_list; 50fd2c3ef7SEric Dumazet struct rtable { 511da177e4SLinus Torvalds struct dst_entry dst; 521da177e4SLinus Torvalds 5329e75252SEric Dumazet int rt_genid; 5495c96174SEric Dumazet unsigned int rt_flags; 551da177e4SLinus Torvalds __u16 rt_type; 56155e8336SJulian Anastasov __u8 rt_is_input; 57155e8336SJulian Anastasov __u8 rt_uses_gateway; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds int rt_iif; 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds /* Info on neighbour */ 62f2c3fe24SAl Viro __be32 rt_gateway; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* Miscellaneous cached information */ 655943634fSDavid S. Miller u32 rt_pmtu; 66caacf05eSDavid S. Miller 67b7503e0cSDavid Ahern u32 rt_table_id; 68b7503e0cSDavid Ahern 69caacf05eSDavid S. Miller struct list_head rt_uncached; 705055c371SEric Dumazet struct uncached_list *rt_uncached_list; 711da177e4SLinus Torvalds }; 721da177e4SLinus Torvalds 73b8400f37SSteffen Klassert static inline bool rt_is_input_route(const struct rtable *rt) 74c7537967SDavid S. Miller { 759917e1e8SDavid S. Miller return rt->rt_is_input != 0; 76c7537967SDavid S. Miller } 77c7537967SDavid S. Miller 78b8400f37SSteffen Klassert static inline bool rt_is_output_route(const struct rtable *rt) 79c7537967SDavid S. Miller { 809917e1e8SDavid S. Miller return rt->rt_is_input == 0; 81c7537967SDavid S. Miller } 82c7537967SDavid S. Miller 83f8126f1dSDavid S. Miller static inline __be32 rt_nexthop(const struct rtable *rt, __be32 daddr) 84f8126f1dSDavid S. Miller { 85f8126f1dSDavid S. Miller if (rt->rt_gateway) 86f8126f1dSDavid S. Miller return rt->rt_gateway; 87f8126f1dSDavid S. Miller return daddr; 88f8126f1dSDavid S. Miller } 89f8126f1dSDavid S. Miller 90fd2c3ef7SEric Dumazet struct ip_rt_acct { 911da177e4SLinus Torvalds __u32 o_bytes; 921da177e4SLinus Torvalds __u32 o_packets; 931da177e4SLinus Torvalds __u32 i_bytes; 941da177e4SLinus Torvalds __u32 i_packets; 951da177e4SLinus Torvalds }; 961da177e4SLinus Torvalds 97fd2c3ef7SEric Dumazet struct rt_cache_stat { 981da177e4SLinus Torvalds unsigned int in_slow_tot; 991da177e4SLinus Torvalds unsigned int in_slow_mc; 1001da177e4SLinus Torvalds unsigned int in_no_route; 1011da177e4SLinus Torvalds unsigned int in_brd; 1021da177e4SLinus Torvalds unsigned int in_martian_dst; 1031da177e4SLinus Torvalds unsigned int in_martian_src; 1041da177e4SLinus Torvalds unsigned int out_slow_tot; 1051da177e4SLinus Torvalds unsigned int out_slow_mc; 1061da177e4SLinus Torvalds }; 1071da177e4SLinus Torvalds 1087d720c3eSTejun Heo extern struct ip_rt_acct __percpu *ip_rt_acct; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds struct in_device; 1112702c4bbSJoe Perches 1122702c4bbSJoe Perches int ip_rt_init(void); 1132702c4bbSJoe Perches void rt_cache_flush(struct net *net); 1142702c4bbSJoe Perches void rt_flush_dev(struct net_device *dev); 1152702c4bbSJoe Perches struct rtable *__ip_route_output_key(struct net *, struct flowi4 *flp); 1162702c4bbSJoe Perches struct rtable *ip_route_output_flow(struct net *, struct flowi4 *flp, 1176f9c9615SEric Dumazet const struct sock *sk); 1182702c4bbSJoe Perches struct dst_entry *ipv4_blackhole_route(struct net *net, 1192702c4bbSJoe Perches struct dst_entry *dst_orig); 120407eadd9SEric Dumazet 1219d6ec938SDavid S. Miller static inline struct rtable *ip_route_output_key(struct net *net, struct flowi4 *flp) 1225bfa787fSDavid S. Miller { 1235bfa787fSDavid S. Miller return ip_route_output_flow(net, flp, NULL); 1245bfa787fSDavid S. Miller } 1255bfa787fSDavid S. Miller 12678fbfd8aSDavid S. Miller static inline struct rtable *ip_route_output(struct net *net, __be32 daddr, 12778fbfd8aSDavid S. Miller __be32 saddr, u8 tos, int oif) 12878fbfd8aSDavid S. Miller { 1299d6ec938SDavid S. Miller struct flowi4 fl4 = { 1309d6ec938SDavid S. Miller .flowi4_oif = oif, 131c5d21c4bSRoland Dreier .flowi4_tos = tos, 1329d6ec938SDavid S. Miller .daddr = daddr, 1339d6ec938SDavid S. Miller .saddr = saddr, 13478fbfd8aSDavid S. Miller }; 1359d6ec938SDavid S. Miller return ip_route_output_key(net, &fl4); 13678fbfd8aSDavid S. Miller } 13778fbfd8aSDavid S. Miller 13831e4543dSDavid S. Miller static inline struct rtable *ip_route_output_ports(struct net *net, struct flowi4 *fl4, 13931e4543dSDavid S. Miller struct sock *sk, 14078fbfd8aSDavid S. Miller __be32 daddr, __be32 saddr, 14178fbfd8aSDavid S. Miller __be16 dport, __be16 sport, 14278fbfd8aSDavid S. Miller __u8 proto, __u8 tos, int oif) 14378fbfd8aSDavid S. Miller { 14431e4543dSDavid S. Miller flowi4_init_output(fl4, oif, sk ? sk->sk_mark : 0, tos, 14594b92b88SDavid S. Miller RT_SCOPE_UNIVERSE, proto, 14694b92b88SDavid S. Miller sk ? inet_sk_flowi_flags(sk) : 0, 14794b92b88SDavid S. Miller daddr, saddr, dport, sport); 14878fbfd8aSDavid S. Miller if (sk) 14931e4543dSDavid S. Miller security_sk_classify_flow(sk, flowi4_to_flowi(fl4)); 15031e4543dSDavid S. Miller return ip_route_output_flow(net, fl4, sk); 15178fbfd8aSDavid S. Miller } 15278fbfd8aSDavid S. Miller 153cbb1e85fSDavid S. Miller static inline struct rtable *ip_route_output_gre(struct net *net, struct flowi4 *fl4, 15478fbfd8aSDavid S. Miller __be32 daddr, __be32 saddr, 15578fbfd8aSDavid S. Miller __be32 gre_key, __u8 tos, int oif) 15678fbfd8aSDavid S. Miller { 157cbb1e85fSDavid S. Miller memset(fl4, 0, sizeof(*fl4)); 158cbb1e85fSDavid S. Miller fl4->flowi4_oif = oif; 159cbb1e85fSDavid S. Miller fl4->daddr = daddr; 160cbb1e85fSDavid S. Miller fl4->saddr = saddr; 161cbb1e85fSDavid S. Miller fl4->flowi4_tos = tos; 162cbb1e85fSDavid S. Miller fl4->flowi4_proto = IPPROTO_GRE; 163cbb1e85fSDavid S. Miller fl4->fl4_gre_key = gre_key; 164cbb1e85fSDavid S. Miller return ip_route_output_key(net, fl4); 16578fbfd8aSDavid S. Miller } 16678fbfd8aSDavid S. Miller 1672702c4bbSJoe Perches int ip_route_input_noref(struct sk_buff *skb, __be32 dst, __be32 src, 16838a424e4SDavid Miller u8 tos, struct net_device *devin); 169407eadd9SEric Dumazet 170c6cffba4SDavid S. Miller static inline int ip_route_input(struct sk_buff *skb, __be32 dst, __be32 src, 171c6cffba4SDavid S. Miller u8 tos, struct net_device *devin) 172c6cffba4SDavid S. Miller { 173c6cffba4SDavid S. Miller int err; 174c6cffba4SDavid S. Miller 175c6cffba4SDavid S. Miller rcu_read_lock(); 176c6cffba4SDavid S. Miller err = ip_route_input_noref(skb, dst, src, tos, devin); 177c6cffba4SDavid S. Miller if (!err) 178c6cffba4SDavid S. Miller skb_dst_force(skb); 179c6cffba4SDavid S. Miller rcu_read_unlock(); 180c6cffba4SDavid S. Miller 181c6cffba4SDavid S. Miller return err; 182c6cffba4SDavid S. Miller } 183c6cffba4SDavid S. Miller 1842702c4bbSJoe Perches void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu, int oif, 1852702c4bbSJoe Perches u32 mark, u8 protocol, int flow_flags); 1862702c4bbSJoe Perches void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu); 1872702c4bbSJoe Perches void ipv4_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 1882702c4bbSJoe Perches u8 protocol, int flow_flags); 1892702c4bbSJoe Perches void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk); 1902702c4bbSJoe Perches void ip_rt_send_redirect(struct sk_buff *skb); 1911da177e4SLinus Torvalds 1922702c4bbSJoe Perches unsigned int inet_addr_type(struct net *net, __be32 addr); 1939b8ff518SDavid Ahern unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id); 1942702c4bbSJoe Perches unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev, 1952702c4bbSJoe Perches __be32 addr); 19630bbaa19SDavid Ahern unsigned int inet_addr_type_dev_table(struct net *net, 19730bbaa19SDavid Ahern const struct net_device *dev, 19830bbaa19SDavid Ahern __be32 addr); 1992702c4bbSJoe Perches void ip_rt_multicast_event(struct in_device *); 2002702c4bbSJoe Perches int ip_rt_ioctl(struct net *, unsigned int cmd, void __user *arg); 2012702c4bbSJoe Perches void ip_rt_get_source(u8 *src, struct sk_buff *skb, struct rtable *rt); 2021da177e4SLinus Torvalds 2030ff60a45SJamal Hadi Salim struct in_ifaddr; 2042702c4bbSJoe Perches void fib_add_ifaddr(struct in_ifaddr *); 2052702c4bbSJoe Perches void fib_del_ifaddr(struct in_ifaddr *, struct in_ifaddr *); 2060ff60a45SJamal Hadi Salim 2071da177e4SLinus Torvalds static inline void ip_rt_put(struct rtable *rt) 2081da177e4SLinus Torvalds { 2096da025faSEric Dumazet /* dst_release() accepts a NULL parameter. 2106da025faSEric Dumazet * We rely on dst being first structure in struct rtable 2116da025faSEric Dumazet */ 2126da025faSEric Dumazet BUILD_BUG_ON(offsetof(struct rtable, dst) != 0); 213d8d1f30bSChangli Gao dst_release(&rt->dst); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds #define IPTOS_RT_MASK (IPTOS_TOS_MASK & ~3) 2171da177e4SLinus Torvalds 2184839c52bSPhilippe De Muyter extern const __u8 ip_tos2prio[16]; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds static inline char rt_tos2priority(u8 tos) 2211da177e4SLinus Torvalds { 2221da177e4SLinus Torvalds return ip_tos2prio[IPTOS_TOS(tos)>>1]; 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 2252d7192d6SDavid S. Miller /* ip_route_connect() and ip_route_newports() work in tandem whilst 2262d7192d6SDavid S. Miller * binding a socket for a new outgoing connection. 2272d7192d6SDavid S. Miller * 2282d7192d6SDavid S. Miller * In order to use IPSEC properly, we must, in the end, have a 2292d7192d6SDavid S. Miller * route that was looked up using all available keys including source 2302d7192d6SDavid S. Miller * and destination ports. 2312d7192d6SDavid S. Miller * 2322d7192d6SDavid S. Miller * However, if a source port needs to be allocated (the user specified 2332d7192d6SDavid S. Miller * a wildcard source port) we need to obtain addressing information 2342d7192d6SDavid S. Miller * in order to perform that allocation. 2352d7192d6SDavid S. Miller * 2362d7192d6SDavid S. Miller * So ip_route_connect() looks up a route using wildcarded source and 2372d7192d6SDavid S. Miller * destination ports in the key, simply so that we can get a pair of 2382d7192d6SDavid S. Miller * addresses to use for port allocation. 2392d7192d6SDavid S. Miller * 2402d7192d6SDavid S. Miller * Later, once the ports are allocated, ip_route_newports() will make 2412d7192d6SDavid S. Miller * another route lookup if needed to make sure we catch any IPSEC 2422d7192d6SDavid S. Miller * rules keyed on the port information. 2432d7192d6SDavid S. Miller * 2442d7192d6SDavid S. Miller * The callers allocate the flow key on their stack, and must pass in 2452d7192d6SDavid S. Miller * the same flowi4 object to both the ip_route_connect() and the 2462d7192d6SDavid S. Miller * ip_route_newports() calls. 2472d7192d6SDavid S. Miller */ 2482d7192d6SDavid S. Miller 2492d7192d6SDavid S. Miller static inline void ip_route_connect_init(struct flowi4 *fl4, __be32 dst, __be32 src, 2502d7192d6SDavid S. Miller u32 tos, int oif, u8 protocol, 251b23dd4feSDavid S. Miller __be16 sport, __be16 dport, 2520e0d44abSSteffen Klassert struct sock *sk) 2531da177e4SLinus Torvalds { 2542d7192d6SDavid S. Miller __u8 flow_flags = 0; 25579876874SKOVACS Krisztian 25679876874SKOVACS Krisztian if (inet_sk(sk)->transparent) 25794b92b88SDavid S. Miller flow_flags |= FLOWI_FLAG_ANYSRC; 25894b92b88SDavid S. Miller 259613d09b3SDavid Ahern if (netif_index_is_vrf(sock_net(sk), oif)) 260613d09b3SDavid Ahern flow_flags |= FLOWI_FLAG_VRFSRC; 261613d09b3SDavid Ahern 2622d7192d6SDavid S. Miller flowi4_init_output(fl4, oif, sk->sk_mark, tos, RT_SCOPE_UNIVERSE, 26394b92b88SDavid S. Miller protocol, flow_flags, dst, src, dport, sport); 2642d7192d6SDavid S. Miller } 2652d7192d6SDavid S. Miller 2662d7192d6SDavid S. Miller static inline struct rtable *ip_route_connect(struct flowi4 *fl4, 2672d7192d6SDavid S. Miller __be32 dst, __be32 src, u32 tos, 2682d7192d6SDavid S. Miller int oif, u8 protocol, 2692d7192d6SDavid S. Miller __be16 sport, __be16 dport, 2700e0d44abSSteffen Klassert struct sock *sk) 2712d7192d6SDavid S. Miller { 2722d7192d6SDavid S. Miller struct net *net = sock_net(sk); 2732d7192d6SDavid S. Miller struct rtable *rt; 2742d7192d6SDavid S. Miller 2752d7192d6SDavid S. Miller ip_route_connect_init(fl4, dst, src, tos, oif, protocol, 2760e0d44abSSteffen Klassert sport, dport, sk); 27779876874SKOVACS Krisztian 2781da177e4SLinus Torvalds if (!dst || !src) { 2792d7192d6SDavid S. Miller rt = __ip_route_output_key(net, fl4); 280b23dd4feSDavid S. Miller if (IS_ERR(rt)) 281b23dd4feSDavid S. Miller return rt; 282b23dd4feSDavid S. Miller ip_rt_put(rt); 283e6b45241SJulian Anastasov flowi4_update_output(fl4, oif, tos, fl4->daddr, fl4->saddr); 2841da177e4SLinus Torvalds } 2852d7192d6SDavid S. Miller security_sk_classify_flow(sk, flowi4_to_flowi(fl4)); 2862d7192d6SDavid S. Miller return ip_route_output_flow(net, fl4, sk); 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 2892d7192d6SDavid S. Miller static inline struct rtable *ip_route_newports(struct flowi4 *fl4, struct rtable *rt, 2902d7192d6SDavid S. Miller __be16 orig_sport, __be16 orig_dport, 2912d7192d6SDavid S. Miller __be16 sport, __be16 dport, 2922d7192d6SDavid S. Miller struct sock *sk) 2931da177e4SLinus Torvalds { 294dca8b089SDavid S. Miller if (sport != orig_sport || dport != orig_dport) { 2952d7192d6SDavid S. Miller fl4->fl4_dport = dport; 2962d7192d6SDavid S. Miller fl4->fl4_sport = sport; 297b23dd4feSDavid S. Miller ip_rt_put(rt); 298e6b45241SJulian Anastasov flowi4_update_output(fl4, sk->sk_bound_dev_if, 299e6b45241SJulian Anastasov RT_CONN_FLAGS(sk), fl4->daddr, 300e6b45241SJulian Anastasov fl4->saddr); 3012d7192d6SDavid S. Miller security_sk_classify_flow(sk, flowi4_to_flowi(fl4)); 3022d7192d6SDavid S. Miller return ip_route_output_flow(sock_net(sk), fl4, sk); 3031da177e4SLinus Torvalds } 304b23dd4feSDavid S. Miller return rt; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071668e010SKOVACS Krisztian static inline int inet_iif(const struct sk_buff *skb) 3081668e010SKOVACS Krisztian { 30913378cadSDavid S. Miller int iif = skb_rtable(skb)->rt_iif; 31013378cadSDavid S. Miller 31113378cadSDavid S. Miller if (iif) 31213378cadSDavid S. Miller return iif; 31313378cadSDavid S. Miller return skb->skb_iif; 3141668e010SKOVACS Krisztian } 3151668e010SKOVACS Krisztian 316323e126fSDavid S. Miller extern int sysctl_ip_default_ttl; 317323e126fSDavid S. Miller 318323e126fSDavid S. Miller static inline int ip4_dst_hoplimit(const struct dst_entry *dst) 319323e126fSDavid S. Miller { 320323e126fSDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 321323e126fSDavid S. Miller 322323e126fSDavid S. Miller if (hoplimit == 0) 323323e126fSDavid S. Miller hoplimit = sysctl_ip_default_ttl; 324323e126fSDavid S. Miller return hoplimit; 325323e126fSDavid S. Miller } 326323e126fSDavid S. Miller 3271da177e4SLinus Torvalds #endif /* _ROUTE_H */ 328