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 #ifndef __KERNEL__ 391da177e4SLinus Torvalds #warning This file is not supposed to be used outside of kernel. 401da177e4SLinus Torvalds #endif 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #define RTO_ONLINK 0x01 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #define RTO_CONN 0 451da177e4SLinus Torvalds /* RTO_CONN is not used (being alias for 0), but preserved not to break 461da177e4SLinus Torvalds * some modules referring to it. */ 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #define RT_CONN_FLAGS(sk) (RT_TOS(inet_sk(sk)->tos) | sock_flag(sk, SOCK_LOCALROUTE)) 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds struct fib_nh; 511da177e4SLinus Torvalds struct inet_peer; 5262fa8a84SDavid S. Miller struct fib_info; 53fd2c3ef7SEric Dumazet struct rtable { 541da177e4SLinus Torvalds struct dst_entry dst; 551da177e4SLinus Torvalds 565e2b61f7SDavid S. Miller /* Lookup key. */ 575e2b61f7SDavid S. Miller __be32 rt_key_dst; 585e2b61f7SDavid S. Miller __be32 rt_key_src; 59093c2ca4SEric Dumazet 6029e75252SEric Dumazet int rt_genid; 611da177e4SLinus Torvalds unsigned rt_flags; 621da177e4SLinus Torvalds __u16 rt_type; 635e2b61f7SDavid S. Miller __u8 rt_tos; 641da177e4SLinus Torvalds 65f2c3fe24SAl Viro __be32 rt_dst; /* Path destination */ 66f2c3fe24SAl Viro __be32 rt_src; /* Path source */ 671b86a58fSOGAWA Hirofumi int rt_route_iif; 681da177e4SLinus Torvalds int rt_iif; 695e2b61f7SDavid S. Miller int rt_oif; 705e2b61f7SDavid S. Miller __u32 rt_mark; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds /* Info on neighbour */ 73f2c3fe24SAl Viro __be32 rt_gateway; 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds /* Miscellaneous cached information */ 76f2c3fe24SAl Viro __be32 rt_spec_dst; /* RFC1122 specific destination */ 776431cbc2SDavid S. Miller u32 rt_peer_genid; 781da177e4SLinus Torvalds struct inet_peer *peer; /* long-living peer info */ 7962fa8a84SDavid S. Miller struct fib_info *fi; /* for client ref to shared metrics */ 801da177e4SLinus Torvalds }; 811da177e4SLinus Torvalds 82c7537967SDavid S. Miller static inline bool rt_is_input_route(struct rtable *rt) 83c7537967SDavid S. Miller { 841b86a58fSOGAWA Hirofumi return rt->rt_route_iif != 0; 85c7537967SDavid S. Miller } 86c7537967SDavid S. Miller 87c7537967SDavid S. Miller static inline bool rt_is_output_route(struct rtable *rt) 88c7537967SDavid S. Miller { 891b86a58fSOGAWA Hirofumi return rt->rt_route_iif == 0; 90c7537967SDavid S. Miller } 91c7537967SDavid S. Miller 92fd2c3ef7SEric Dumazet struct ip_rt_acct { 931da177e4SLinus Torvalds __u32 o_bytes; 941da177e4SLinus Torvalds __u32 o_packets; 951da177e4SLinus Torvalds __u32 i_bytes; 961da177e4SLinus Torvalds __u32 i_packets; 971da177e4SLinus Torvalds }; 981da177e4SLinus Torvalds 99fd2c3ef7SEric Dumazet struct rt_cache_stat { 1001da177e4SLinus Torvalds unsigned int in_hit; 1011da177e4SLinus Torvalds unsigned int in_slow_tot; 1021da177e4SLinus Torvalds unsigned int in_slow_mc; 1031da177e4SLinus Torvalds unsigned int in_no_route; 1041da177e4SLinus Torvalds unsigned int in_brd; 1051da177e4SLinus Torvalds unsigned int in_martian_dst; 1061da177e4SLinus Torvalds unsigned int in_martian_src; 1071da177e4SLinus Torvalds unsigned int out_hit; 1081da177e4SLinus Torvalds unsigned int out_slow_tot; 1091da177e4SLinus Torvalds unsigned int out_slow_mc; 1101da177e4SLinus Torvalds unsigned int gc_total; 1111da177e4SLinus Torvalds unsigned int gc_ignored; 1121da177e4SLinus Torvalds unsigned int gc_goal_miss; 1131da177e4SLinus Torvalds unsigned int gc_dst_overflow; 1141da177e4SLinus Torvalds unsigned int in_hlist_search; 1151da177e4SLinus Torvalds unsigned int out_hlist_search; 1161da177e4SLinus Torvalds }; 1171da177e4SLinus Torvalds 1187d720c3eSTejun Heo extern struct ip_rt_acct __percpu *ip_rt_acct; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds struct in_device; 1211da177e4SLinus Torvalds extern int ip_rt_init(void); 122f7655229SAl Viro extern void ip_rt_redirect(__be32 old_gw, __be32 dst, __be32 new_gw, 123f7655229SAl Viro __be32 src, struct net_device *dev); 12476e6ebfbSDenis V. Lunev extern void rt_cache_flush(struct net *net, int how); 1256561a3b1SDavid S. Miller extern void rt_cache_flush_batch(struct net *net); 1269d6ec938SDavid S. Miller extern struct rtable *__ip_route_output_key(struct net *, const struct flowi4 *flp); 1279d6ec938SDavid S. Miller extern struct rtable *ip_route_output_flow(struct net *, struct flowi4 *flp, 128b23dd4feSDavid S. Miller struct sock *sk); 1292774c131SDavid S. Miller extern struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig); 130407eadd9SEric Dumazet 1319d6ec938SDavid S. Miller static inline struct rtable *ip_route_output_key(struct net *net, struct flowi4 *flp) 1325bfa787fSDavid S. Miller { 1335bfa787fSDavid S. Miller return ip_route_output_flow(net, flp, NULL); 1345bfa787fSDavid S. Miller } 1355bfa787fSDavid S. Miller 13678fbfd8aSDavid S. Miller static inline struct rtable *ip_route_output(struct net *net, __be32 daddr, 13778fbfd8aSDavid S. Miller __be32 saddr, u8 tos, int oif) 13878fbfd8aSDavid S. Miller { 1399d6ec938SDavid S. Miller struct flowi4 fl4 = { 1409d6ec938SDavid S. Miller .flowi4_oif = oif, 1419d6ec938SDavid S. Miller .daddr = daddr, 1429d6ec938SDavid S. Miller .saddr = saddr, 1439d6ec938SDavid S. Miller .flowi4_tos = tos, 14478fbfd8aSDavid S. Miller }; 1459d6ec938SDavid S. Miller return ip_route_output_key(net, &fl4); 14678fbfd8aSDavid S. Miller } 14778fbfd8aSDavid S. Miller 14878fbfd8aSDavid S. Miller static inline struct rtable *ip_route_output_ports(struct net *net, struct sock *sk, 14978fbfd8aSDavid S. Miller __be32 daddr, __be32 saddr, 15078fbfd8aSDavid S. Miller __be16 dport, __be16 sport, 15178fbfd8aSDavid S. Miller __u8 proto, __u8 tos, int oif) 15278fbfd8aSDavid S. Miller { 1539d6ec938SDavid S. Miller struct flowi4 fl4 = { 1549d6ec938SDavid S. Miller .flowi4_oif = oif, 1559d6ec938SDavid S. Miller .flowi4_flags = sk ? inet_sk_flowi_flags(sk) : 0, 1569d6ec938SDavid S. Miller .flowi4_mark = sk ? sk->sk_mark : 0, 1579d6ec938SDavid S. Miller .daddr = daddr, 1589d6ec938SDavid S. Miller .saddr = saddr, 1599d6ec938SDavid S. Miller .flowi4_tos = tos, 1609d6ec938SDavid S. Miller .flowi4_proto = proto, 1619cce96dfSDavid S. Miller .fl4_dport = dport, 1629cce96dfSDavid S. Miller .fl4_sport = sport, 16378fbfd8aSDavid S. Miller }; 16478fbfd8aSDavid S. Miller if (sk) 1659d6ec938SDavid S. Miller security_sk_classify_flow(sk, flowi4_to_flowi(&fl4)); 1669d6ec938SDavid S. Miller return ip_route_output_flow(net, &fl4, sk); 16778fbfd8aSDavid S. Miller } 16878fbfd8aSDavid S. Miller 16978fbfd8aSDavid S. Miller static inline struct rtable *ip_route_output_gre(struct net *net, 17078fbfd8aSDavid S. Miller __be32 daddr, __be32 saddr, 17178fbfd8aSDavid S. Miller __be32 gre_key, __u8 tos, int oif) 17278fbfd8aSDavid S. Miller { 1739d6ec938SDavid S. Miller struct flowi4 fl4 = { 1749d6ec938SDavid S. Miller .flowi4_oif = oif, 1759d6ec938SDavid S. Miller .daddr = daddr, 1769d6ec938SDavid S. Miller .saddr = saddr, 1779d6ec938SDavid S. Miller .flowi4_tos = tos, 1789d6ec938SDavid S. Miller .flowi4_proto = IPPROTO_GRE, 1799cce96dfSDavid S. Miller .fl4_gre_key = gre_key, 18078fbfd8aSDavid S. Miller }; 1819d6ec938SDavid S. Miller return ip_route_output_key(net, &fl4); 18278fbfd8aSDavid S. Miller } 18378fbfd8aSDavid S. Miller 184407eadd9SEric Dumazet extern int ip_route_input_common(struct sk_buff *skb, __be32 dst, __be32 src, 185407eadd9SEric Dumazet u8 tos, struct net_device *devin, bool noref); 186407eadd9SEric Dumazet 187407eadd9SEric Dumazet static inline int ip_route_input(struct sk_buff *skb, __be32 dst, __be32 src, 188407eadd9SEric Dumazet u8 tos, struct net_device *devin) 189407eadd9SEric Dumazet { 190407eadd9SEric Dumazet return ip_route_input_common(skb, dst, src, tos, devin, false); 191407eadd9SEric Dumazet } 192407eadd9SEric Dumazet 193407eadd9SEric Dumazet static inline int ip_route_input_noref(struct sk_buff *skb, __be32 dst, __be32 src, 194407eadd9SEric Dumazet u8 tos, struct net_device *devin) 195407eadd9SEric Dumazet { 196407eadd9SEric Dumazet return ip_route_input_common(skb, dst, src, tos, devin, true); 197407eadd9SEric Dumazet } 198407eadd9SEric Dumazet 1990010e465STimo Teras extern unsigned short ip_rt_frag_needed(struct net *net, struct iphdr *iph, unsigned short new_mtu, struct net_device *dev); 2001da177e4SLinus Torvalds extern void ip_rt_send_redirect(struct sk_buff *skb); 2011da177e4SLinus Torvalds 2026b175b26SEric W. Biederman extern unsigned inet_addr_type(struct net *net, __be32 addr); 2036b175b26SEric W. Biederman extern unsigned inet_dev_addr_type(struct net *net, const struct net_device *dev, __be32 addr); 2041da177e4SLinus Torvalds extern void ip_rt_multicast_event(struct in_device *); 2051bad118aSDenis V. Lunev extern int ip_rt_ioctl(struct net *, unsigned int cmd, void __user *arg); 2061da177e4SLinus Torvalds extern void ip_rt_get_source(u8 *src, struct rtable *rt); 2071da177e4SLinus Torvalds extern int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb); 2081da177e4SLinus Torvalds 2090ff60a45SJamal Hadi Salim struct in_ifaddr; 2100ff60a45SJamal Hadi Salim extern void fib_add_ifaddr(struct in_ifaddr *); 211e6abbaa2SJulian Anastasov extern void fib_del_ifaddr(struct in_ifaddr *, struct in_ifaddr *); 2120ff60a45SJamal Hadi Salim 2131da177e4SLinus Torvalds static inline void ip_rt_put(struct rtable * rt) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds if (rt) 216d8d1f30bSChangli Gao dst_release(&rt->dst); 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds #define IPTOS_RT_MASK (IPTOS_TOS_MASK & ~3) 2201da177e4SLinus Torvalds 2214839c52bSPhilippe De Muyter extern const __u8 ip_tos2prio[16]; 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds static inline char rt_tos2priority(u8 tos) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds return ip_tos2prio[IPTOS_TOS(tos)>>1]; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 228b23dd4feSDavid S. Miller static inline struct rtable *ip_route_connect(__be32 dst, __be32 src, u32 tos, 229b23dd4feSDavid S. Miller int oif, u8 protocol, 230b23dd4feSDavid S. Miller __be16 sport, __be16 dport, 231b23dd4feSDavid S. Miller struct sock *sk, bool can_sleep) 2321da177e4SLinus Torvalds { 2339d6ec938SDavid S. Miller struct flowi4 fl4 = { 2349d6ec938SDavid S. Miller .flowi4_oif = oif, 2359d6ec938SDavid S. Miller .flowi4_mark = sk->sk_mark, 2369d6ec938SDavid S. Miller .daddr = dst, 2379d6ec938SDavid S. Miller .saddr = src, 2389d6ec938SDavid S. Miller .flowi4_tos = tos, 2399d6ec938SDavid S. Miller .flowi4_proto = protocol, 2409cce96dfSDavid S. Miller .fl4_sport = sport, 2419cce96dfSDavid S. Miller .fl4_dport = dport, 2426281dcc9SDavid S. Miller }; 2433b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 244b23dd4feSDavid S. Miller struct rtable *rt; 24579876874SKOVACS Krisztian 24679876874SKOVACS Krisztian if (inet_sk(sk)->transparent) 2479d6ec938SDavid S. Miller fl4.flowi4_flags |= FLOWI_FLAG_ANYSRC; 248a4daad6bSDavid S. Miller if (protocol == IPPROTO_TCP) 2499d6ec938SDavid S. Miller fl4.flowi4_flags |= FLOWI_FLAG_PRECOW_METRICS; 2505df65e55SDavid S. Miller if (can_sleep) 2519d6ec938SDavid S. Miller fl4.flowi4_flags |= FLOWI_FLAG_CAN_SLEEP; 25279876874SKOVACS Krisztian 2531da177e4SLinus Torvalds if (!dst || !src) { 2549d6ec938SDavid S. Miller rt = __ip_route_output_key(net, &fl4); 255b23dd4feSDavid S. Miller if (IS_ERR(rt)) 256b23dd4feSDavid S. Miller return rt; 2579d6ec938SDavid S. Miller fl4.daddr = rt->rt_dst; 2589d6ec938SDavid S. Miller fl4.saddr = rt->rt_src; 259b23dd4feSDavid S. Miller ip_rt_put(rt); 2601da177e4SLinus Torvalds } 2619d6ec938SDavid S. Miller security_sk_classify_flow(sk, flowi4_to_flowi(&fl4)); 2629d6ec938SDavid S. Miller return ip_route_output_flow(net, &fl4, sk); 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 265b23dd4feSDavid S. Miller static inline struct rtable *ip_route_newports(struct rtable *rt, 266b23dd4feSDavid S. Miller u8 protocol, __be16 orig_sport, 267b23dd4feSDavid S. Miller __be16 orig_dport, __be16 sport, 268b23dd4feSDavid S. Miller __be16 dport, struct sock *sk) 2691da177e4SLinus Torvalds { 270dca8b089SDavid S. Miller if (sport != orig_sport || dport != orig_dport) { 2719d6ec938SDavid S. Miller struct flowi4 fl4 = { 2729d6ec938SDavid S. Miller .flowi4_oif = rt->rt_oif, 2739d6ec938SDavid S. Miller .flowi4_mark = rt->rt_mark, 2746df59a84SSteffen Klassert .daddr = rt->rt_dst, 2756df59a84SSteffen Klassert .saddr = rt->rt_src, 2769d6ec938SDavid S. Miller .flowi4_tos = rt->rt_tos, 2779d6ec938SDavid S. Miller .flowi4_proto = protocol, 2789cce96dfSDavid S. Miller .fl4_sport = sport, 2799cce96dfSDavid S. Miller .fl4_dport = dport 2806281dcc9SDavid S. Miller }; 281fb0c5f0bSUlrich Weber if (inet_sk(sk)->transparent) 2829d6ec938SDavid S. Miller fl4.flowi4_flags |= FLOWI_FLAG_ANYSRC; 283a4daad6bSDavid S. Miller if (protocol == IPPROTO_TCP) 2849d6ec938SDavid S. Miller fl4.flowi4_flags |= FLOWI_FLAG_PRECOW_METRICS; 285b23dd4feSDavid S. Miller ip_rt_put(rt); 2869d6ec938SDavid S. Miller security_sk_classify_flow(sk, flowi4_to_flowi(&fl4)); 2879d6ec938SDavid S. Miller return ip_route_output_flow(sock_net(sk), &fl4, sk); 2881da177e4SLinus Torvalds } 289b23dd4feSDavid S. Miller return rt; 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds extern void rt_bind_peer(struct rtable *rt, int create); 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds static inline struct inet_peer *rt_get_peer(struct rtable *rt) 2951da177e4SLinus Torvalds { 2961da177e4SLinus Torvalds if (rt->peer) 2971da177e4SLinus Torvalds return rt->peer; 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds rt_bind_peer(rt, 0); 3001da177e4SLinus Torvalds return rt->peer; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031668e010SKOVACS Krisztian static inline int inet_iif(const struct sk_buff *skb) 3041668e010SKOVACS Krisztian { 305511c3f92SEric Dumazet return skb_rtable(skb)->rt_iif; 3061668e010SKOVACS Krisztian } 3071668e010SKOVACS Krisztian 308323e126fSDavid S. Miller extern int sysctl_ip_default_ttl; 309323e126fSDavid S. Miller 310323e126fSDavid S. Miller static inline int ip4_dst_hoplimit(const struct dst_entry *dst) 311323e126fSDavid S. Miller { 312323e126fSDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 313323e126fSDavid S. Miller 314323e126fSDavid S. Miller if (hoplimit == 0) 315323e126fSDavid S. Miller hoplimit = sysctl_ip_default_ttl; 316323e126fSDavid S. Miller return hoplimit; 317323e126fSDavid S. Miller } 318323e126fSDavid S. Miller 3191da177e4SLinus Torvalds #endif /* _ROUTE_H */ 320