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 56093c2ca4SEric Dumazet /* Cache lookup keys */ 57093c2ca4SEric Dumazet struct flowi fl; 58093c2ca4SEric Dumazet 5929e75252SEric Dumazet int rt_genid; 601da177e4SLinus Torvalds unsigned rt_flags; 611da177e4SLinus Torvalds __u16 rt_type; 621da177e4SLinus Torvalds 63f2c3fe24SAl Viro __be32 rt_dst; /* Path destination */ 64f2c3fe24SAl Viro __be32 rt_src; /* Path source */ 651da177e4SLinus Torvalds int rt_iif; 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* Info on neighbour */ 68f2c3fe24SAl Viro __be32 rt_gateway; 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds /* Miscellaneous cached information */ 71f2c3fe24SAl Viro __be32 rt_spec_dst; /* RFC1122 specific destination */ 726431cbc2SDavid S. Miller u32 rt_peer_genid; 731da177e4SLinus Torvalds struct inet_peer *peer; /* long-living peer info */ 7462fa8a84SDavid S. Miller struct fib_info *fi; /* for client ref to shared metrics */ 751da177e4SLinus Torvalds }; 761da177e4SLinus Torvalds 77c7537967SDavid S. Miller static inline bool rt_is_input_route(struct rtable *rt) 78c7537967SDavid S. Miller { 79c7537967SDavid S. Miller return rt->fl.iif != 0; 80c7537967SDavid S. Miller } 81c7537967SDavid S. Miller 82c7537967SDavid S. Miller static inline bool rt_is_output_route(struct rtable *rt) 83c7537967SDavid S. Miller { 84c7537967SDavid S. Miller return rt->fl.iif == 0; 85c7537967SDavid S. Miller } 86c7537967SDavid S. Miller 87fd2c3ef7SEric Dumazet struct ip_rt_acct { 881da177e4SLinus Torvalds __u32 o_bytes; 891da177e4SLinus Torvalds __u32 o_packets; 901da177e4SLinus Torvalds __u32 i_bytes; 911da177e4SLinus Torvalds __u32 i_packets; 921da177e4SLinus Torvalds }; 931da177e4SLinus Torvalds 94fd2c3ef7SEric Dumazet struct rt_cache_stat { 951da177e4SLinus Torvalds unsigned int in_hit; 961da177e4SLinus Torvalds unsigned int in_slow_tot; 971da177e4SLinus Torvalds unsigned int in_slow_mc; 981da177e4SLinus Torvalds unsigned int in_no_route; 991da177e4SLinus Torvalds unsigned int in_brd; 1001da177e4SLinus Torvalds unsigned int in_martian_dst; 1011da177e4SLinus Torvalds unsigned int in_martian_src; 1021da177e4SLinus Torvalds unsigned int out_hit; 1031da177e4SLinus Torvalds unsigned int out_slow_tot; 1041da177e4SLinus Torvalds unsigned int out_slow_mc; 1051da177e4SLinus Torvalds unsigned int gc_total; 1061da177e4SLinus Torvalds unsigned int gc_ignored; 1071da177e4SLinus Torvalds unsigned int gc_goal_miss; 1081da177e4SLinus Torvalds unsigned int gc_dst_overflow; 1091da177e4SLinus Torvalds unsigned int in_hlist_search; 1101da177e4SLinus Torvalds unsigned int out_hlist_search; 1111da177e4SLinus Torvalds }; 1121da177e4SLinus Torvalds 1137d720c3eSTejun Heo extern struct ip_rt_acct __percpu *ip_rt_acct; 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds struct in_device; 1161da177e4SLinus Torvalds extern int ip_rt_init(void); 117f7655229SAl Viro extern void ip_rt_redirect(__be32 old_gw, __be32 dst, __be32 new_gw, 118f7655229SAl Viro __be32 src, struct net_device *dev); 11976e6ebfbSDenis V. Lunev extern void rt_cache_flush(struct net *net, int how); 1206561a3b1SDavid S. Miller extern void rt_cache_flush_batch(struct net *net); 121b23dd4feSDavid S. Miller extern struct rtable *__ip_route_output_key(struct net *, const struct flowi *flp); 122b23dd4feSDavid S. Miller extern struct rtable *ip_route_output_flow(struct net *, struct flowi *flp, 123b23dd4feSDavid S. Miller struct sock *sk); 1242774c131SDavid S. Miller extern struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig); 125407eadd9SEric Dumazet 1265bfa787fSDavid S. Miller static inline struct rtable *ip_route_output_key(struct net *net, struct flowi *flp) 1275bfa787fSDavid S. Miller { 1285bfa787fSDavid S. Miller return ip_route_output_flow(net, flp, NULL); 1295bfa787fSDavid S. Miller } 1305bfa787fSDavid S. Miller 131407eadd9SEric Dumazet extern int ip_route_input_common(struct sk_buff *skb, __be32 dst, __be32 src, 132407eadd9SEric Dumazet u8 tos, struct net_device *devin, bool noref); 133407eadd9SEric Dumazet 134407eadd9SEric Dumazet static inline int ip_route_input(struct sk_buff *skb, __be32 dst, __be32 src, 135407eadd9SEric Dumazet u8 tos, struct net_device *devin) 136407eadd9SEric Dumazet { 137407eadd9SEric Dumazet return ip_route_input_common(skb, dst, src, tos, devin, false); 138407eadd9SEric Dumazet } 139407eadd9SEric Dumazet 140407eadd9SEric Dumazet static inline int ip_route_input_noref(struct sk_buff *skb, __be32 dst, __be32 src, 141407eadd9SEric Dumazet u8 tos, struct net_device *devin) 142407eadd9SEric Dumazet { 143407eadd9SEric Dumazet return ip_route_input_common(skb, dst, src, tos, devin, true); 144407eadd9SEric Dumazet } 145407eadd9SEric Dumazet 1460010e465STimo Teras extern unsigned short ip_rt_frag_needed(struct net *net, struct iphdr *iph, unsigned short new_mtu, struct net_device *dev); 1471da177e4SLinus Torvalds extern void ip_rt_send_redirect(struct sk_buff *skb); 1481da177e4SLinus Torvalds 1496b175b26SEric W. Biederman extern unsigned inet_addr_type(struct net *net, __be32 addr); 1506b175b26SEric W. Biederman extern unsigned inet_dev_addr_type(struct net *net, const struct net_device *dev, __be32 addr); 1511da177e4SLinus Torvalds extern void ip_rt_multicast_event(struct in_device *); 1521bad118aSDenis V. Lunev extern int ip_rt_ioctl(struct net *, unsigned int cmd, void __user *arg); 1531da177e4SLinus Torvalds extern void ip_rt_get_source(u8 *src, struct rtable *rt); 1541da177e4SLinus Torvalds extern int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb); 1551da177e4SLinus Torvalds 1560ff60a45SJamal Hadi Salim struct in_ifaddr; 1570ff60a45SJamal Hadi Salim extern void fib_add_ifaddr(struct in_ifaddr *); 1580ff60a45SJamal Hadi Salim 1591da177e4SLinus Torvalds static inline void ip_rt_put(struct rtable * rt) 1601da177e4SLinus Torvalds { 1611da177e4SLinus Torvalds if (rt) 162d8d1f30bSChangli Gao dst_release(&rt->dst); 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds #define IPTOS_RT_MASK (IPTOS_TOS_MASK & ~3) 1661da177e4SLinus Torvalds 1674839c52bSPhilippe De Muyter extern const __u8 ip_tos2prio[16]; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds static inline char rt_tos2priority(u8 tos) 1701da177e4SLinus Torvalds { 1711da177e4SLinus Torvalds return ip_tos2prio[IPTOS_TOS(tos)>>1]; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 174b23dd4feSDavid S. Miller static inline struct rtable *ip_route_connect(__be32 dst, __be32 src, u32 tos, 175b23dd4feSDavid S. Miller int oif, u8 protocol, 176b23dd4feSDavid S. Miller __be16 sport, __be16 dport, 177b23dd4feSDavid S. Miller struct sock *sk, bool can_sleep) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds struct flowi fl = { .oif = oif, 1804a19ec58SLaszlo Attila Toth .mark = sk->sk_mark, 1815811662bSChangli Gao .fl4_dst = dst, 1825811662bSChangli Gao .fl4_src = src, 1835811662bSChangli Gao .fl4_tos = tos, 1841da177e4SLinus Torvalds .proto = protocol, 1855811662bSChangli Gao .fl_ip_sport = sport, 1865811662bSChangli Gao .fl_ip_dport = dport }; 1873b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 188b23dd4feSDavid S. Miller struct rtable *rt; 18979876874SKOVACS Krisztian 19079876874SKOVACS Krisztian if (inet_sk(sk)->transparent) 19179876874SKOVACS Krisztian fl.flags |= FLOWI_FLAG_ANYSRC; 192a4daad6bSDavid S. Miller if (protocol == IPPROTO_TCP) 193a4daad6bSDavid S. Miller fl.flags |= FLOWI_FLAG_PRECOW_METRICS; 1945df65e55SDavid S. Miller if (can_sleep) 1955df65e55SDavid S. Miller fl.flags |= FLOWI_FLAG_CAN_SLEEP; 19679876874SKOVACS Krisztian 1971da177e4SLinus Torvalds if (!dst || !src) { 198b23dd4feSDavid S. Miller rt = __ip_route_output_key(net, &fl); 199b23dd4feSDavid S. Miller if (IS_ERR(rt)) 200b23dd4feSDavid S. Miller return rt; 201b23dd4feSDavid S. Miller fl.fl4_dst = rt->rt_dst; 202b23dd4feSDavid S. Miller fl.fl4_src = rt->rt_src; 203b23dd4feSDavid S. Miller ip_rt_put(rt); 2041da177e4SLinus Torvalds } 205beb8d13bSVenkat Yekkirala security_sk_classify_flow(sk, &fl); 206b23dd4feSDavid S. Miller return ip_route_output_flow(net, &fl, sk); 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 209b23dd4feSDavid S. Miller static inline struct rtable *ip_route_newports(struct rtable *rt, 210b23dd4feSDavid S. Miller u8 protocol, __be16 orig_sport, 211b23dd4feSDavid S. Miller __be16 orig_dport, __be16 sport, 212b23dd4feSDavid S. Miller __be16 dport, struct sock *sk) 2131da177e4SLinus Torvalds { 214dca8b089SDavid S. Miller if (sport != orig_sport || dport != orig_dport) { 215b23dd4feSDavid S. Miller struct flowi fl = { .oif = rt->fl.oif, 216b23dd4feSDavid S. Miller .mark = rt->fl.mark, 217b23dd4feSDavid S. Miller .fl4_dst = rt->fl.fl4_dst, 218b23dd4feSDavid S. Miller .fl4_src = rt->fl.fl4_src, 219b23dd4feSDavid S. Miller .fl4_tos = rt->fl.fl4_tos, 220b23dd4feSDavid S. Miller .proto = rt->fl.proto, 221dca8b089SDavid S. Miller .fl_ip_sport = sport, 222dca8b089SDavid S. Miller .fl_ip_dport = dport }; 2231da177e4SLinus Torvalds 224fb0c5f0bSUlrich Weber if (inet_sk(sk)->transparent) 225fb0c5f0bSUlrich Weber fl.flags |= FLOWI_FLAG_ANYSRC; 226a4daad6bSDavid S. Miller if (protocol == IPPROTO_TCP) 227a4daad6bSDavid S. Miller fl.flags |= FLOWI_FLAG_PRECOW_METRICS; 228b23dd4feSDavid S. Miller ip_rt_put(rt); 229beb8d13bSVenkat Yekkirala security_sk_classify_flow(sk, &fl); 230b23dd4feSDavid S. Miller return ip_route_output_flow(sock_net(sk), &fl, sk); 2311da177e4SLinus Torvalds } 232b23dd4feSDavid S. Miller return rt; 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds extern void rt_bind_peer(struct rtable *rt, int create); 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds static inline struct inet_peer *rt_get_peer(struct rtable *rt) 2381da177e4SLinus Torvalds { 2391da177e4SLinus Torvalds if (rt->peer) 2401da177e4SLinus Torvalds return rt->peer; 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds rt_bind_peer(rt, 0); 2431da177e4SLinus Torvalds return rt->peer; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 2461668e010SKOVACS Krisztian static inline int inet_iif(const struct sk_buff *skb) 2471668e010SKOVACS Krisztian { 248511c3f92SEric Dumazet return skb_rtable(skb)->rt_iif; 2491668e010SKOVACS Krisztian } 2501668e010SKOVACS Krisztian 251323e126fSDavid S. Miller extern int sysctl_ip_default_ttl; 252323e126fSDavid S. Miller 253323e126fSDavid S. Miller static inline int ip4_dst_hoplimit(const struct dst_entry *dst) 254323e126fSDavid S. Miller { 255323e126fSDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 256323e126fSDavid S. Miller 257323e126fSDavid S. Miller if (hoplimit == 0) 258323e126fSDavid S. Miller hoplimit = sysctl_ip_default_ttl; 259323e126fSDavid S. Miller return hoplimit; 260323e126fSDavid S. Miller } 261323e126fSDavid S. Miller 2621da177e4SLinus Torvalds #endif /* _ROUTE_H */ 263