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_key(struct net *, struct flowi *flp); 123b23dd4feSDavid S. Miller extern struct rtable *ip_route_output_flow(struct net *, struct flowi *flp, 124b23dd4feSDavid S. Miller struct sock *sk); 1252774c131SDavid S. Miller extern struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig); 126407eadd9SEric Dumazet 127407eadd9SEric Dumazet extern int ip_route_input_common(struct sk_buff *skb, __be32 dst, __be32 src, 128407eadd9SEric Dumazet u8 tos, struct net_device *devin, bool noref); 129407eadd9SEric Dumazet 130407eadd9SEric Dumazet static inline int ip_route_input(struct sk_buff *skb, __be32 dst, __be32 src, 131407eadd9SEric Dumazet u8 tos, struct net_device *devin) 132407eadd9SEric Dumazet { 133407eadd9SEric Dumazet return ip_route_input_common(skb, dst, src, tos, devin, false); 134407eadd9SEric Dumazet } 135407eadd9SEric Dumazet 136407eadd9SEric Dumazet static inline int ip_route_input_noref(struct sk_buff *skb, __be32 dst, __be32 src, 137407eadd9SEric Dumazet u8 tos, struct net_device *devin) 138407eadd9SEric Dumazet { 139407eadd9SEric Dumazet return ip_route_input_common(skb, dst, src, tos, devin, true); 140407eadd9SEric Dumazet } 141407eadd9SEric Dumazet 1420010e465STimo Teras extern unsigned short ip_rt_frag_needed(struct net *net, struct iphdr *iph, unsigned short new_mtu, struct net_device *dev); 1431da177e4SLinus Torvalds extern void ip_rt_send_redirect(struct sk_buff *skb); 1441da177e4SLinus Torvalds 1456b175b26SEric W. Biederman extern unsigned inet_addr_type(struct net *net, __be32 addr); 1466b175b26SEric W. Biederman extern unsigned inet_dev_addr_type(struct net *net, const struct net_device *dev, __be32 addr); 1471da177e4SLinus Torvalds extern void ip_rt_multicast_event(struct in_device *); 1481bad118aSDenis V. Lunev extern int ip_rt_ioctl(struct net *, unsigned int cmd, void __user *arg); 1491da177e4SLinus Torvalds extern void ip_rt_get_source(u8 *src, struct rtable *rt); 1501da177e4SLinus Torvalds extern int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb); 1511da177e4SLinus Torvalds 1520ff60a45SJamal Hadi Salim struct in_ifaddr; 1530ff60a45SJamal Hadi Salim extern void fib_add_ifaddr(struct in_ifaddr *); 1540ff60a45SJamal Hadi Salim 1551da177e4SLinus Torvalds static inline void ip_rt_put(struct rtable * rt) 1561da177e4SLinus Torvalds { 1571da177e4SLinus Torvalds if (rt) 158d8d1f30bSChangli Gao dst_release(&rt->dst); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds #define IPTOS_RT_MASK (IPTOS_TOS_MASK & ~3) 1621da177e4SLinus Torvalds 1634839c52bSPhilippe De Muyter extern const __u8 ip_tos2prio[16]; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds static inline char rt_tos2priority(u8 tos) 1661da177e4SLinus Torvalds { 1671da177e4SLinus Torvalds return ip_tos2prio[IPTOS_TOS(tos)>>1]; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 170b23dd4feSDavid S. Miller static inline struct rtable *ip_route_connect(__be32 dst, __be32 src, u32 tos, 171b23dd4feSDavid S. Miller int oif, u8 protocol, 172b23dd4feSDavid S. Miller __be16 sport, __be16 dport, 173b23dd4feSDavid S. Miller struct sock *sk, bool can_sleep) 1741da177e4SLinus Torvalds { 1751da177e4SLinus Torvalds struct flowi fl = { .oif = oif, 1764a19ec58SLaszlo Attila Toth .mark = sk->sk_mark, 1775811662bSChangli Gao .fl4_dst = dst, 1785811662bSChangli Gao .fl4_src = src, 1795811662bSChangli Gao .fl4_tos = tos, 1801da177e4SLinus Torvalds .proto = protocol, 1815811662bSChangli Gao .fl_ip_sport = sport, 1825811662bSChangli Gao .fl_ip_dport = dport }; 1833b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 184b23dd4feSDavid S. Miller struct rtable *rt; 18579876874SKOVACS Krisztian 18679876874SKOVACS Krisztian if (inet_sk(sk)->transparent) 18779876874SKOVACS Krisztian fl.flags |= FLOWI_FLAG_ANYSRC; 188a4daad6bSDavid S. Miller if (protocol == IPPROTO_TCP) 189a4daad6bSDavid S. Miller fl.flags |= FLOWI_FLAG_PRECOW_METRICS; 1905df65e55SDavid S. Miller if (can_sleep) 1915df65e55SDavid S. Miller fl.flags |= FLOWI_FLAG_CAN_SLEEP; 19279876874SKOVACS Krisztian 1931da177e4SLinus Torvalds if (!dst || !src) { 194b23dd4feSDavid S. Miller rt = __ip_route_output_key(net, &fl); 195b23dd4feSDavid S. Miller if (IS_ERR(rt)) 196b23dd4feSDavid S. Miller return rt; 197b23dd4feSDavid S. Miller fl.fl4_dst = rt->rt_dst; 198b23dd4feSDavid S. Miller fl.fl4_src = rt->rt_src; 199b23dd4feSDavid S. Miller ip_rt_put(rt); 2001da177e4SLinus Torvalds } 201beb8d13bSVenkat Yekkirala security_sk_classify_flow(sk, &fl); 202b23dd4feSDavid S. Miller return ip_route_output_flow(net, &fl, sk); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 205b23dd4feSDavid S. Miller static inline struct rtable *ip_route_newports(struct rtable *rt, 206b23dd4feSDavid S. Miller u8 protocol, __be16 orig_sport, 207b23dd4feSDavid S. Miller __be16 orig_dport, __be16 sport, 208b23dd4feSDavid S. Miller __be16 dport, struct sock *sk) 2091da177e4SLinus Torvalds { 210dca8b089SDavid S. Miller if (sport != orig_sport || dport != orig_dport) { 211b23dd4feSDavid S. Miller struct flowi fl = { .oif = rt->fl.oif, 212b23dd4feSDavid S. Miller .mark = rt->fl.mark, 213b23dd4feSDavid S. Miller .fl4_dst = rt->fl.fl4_dst, 214b23dd4feSDavid S. Miller .fl4_src = rt->fl.fl4_src, 215b23dd4feSDavid S. Miller .fl4_tos = rt->fl.fl4_tos, 216b23dd4feSDavid S. Miller .proto = rt->fl.proto, 217dca8b089SDavid S. Miller .fl_ip_sport = sport, 218dca8b089SDavid S. Miller .fl_ip_dport = dport }; 2191da177e4SLinus Torvalds 220fb0c5f0bSUlrich Weber if (inet_sk(sk)->transparent) 221fb0c5f0bSUlrich Weber fl.flags |= FLOWI_FLAG_ANYSRC; 222a4daad6bSDavid S. Miller if (protocol == IPPROTO_TCP) 223a4daad6bSDavid S. Miller fl.flags |= FLOWI_FLAG_PRECOW_METRICS; 224b23dd4feSDavid S. Miller ip_rt_put(rt); 225beb8d13bSVenkat Yekkirala security_sk_classify_flow(sk, &fl); 226b23dd4feSDavid S. Miller return ip_route_output_flow(sock_net(sk), &fl, sk); 2271da177e4SLinus Torvalds } 228b23dd4feSDavid S. Miller return rt; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds extern void rt_bind_peer(struct rtable *rt, int create); 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds static inline struct inet_peer *rt_get_peer(struct rtable *rt) 2341da177e4SLinus Torvalds { 2351da177e4SLinus Torvalds if (rt->peer) 2361da177e4SLinus Torvalds return rt->peer; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds rt_bind_peer(rt, 0); 2391da177e4SLinus Torvalds return rt->peer; 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 2421668e010SKOVACS Krisztian static inline int inet_iif(const struct sk_buff *skb) 2431668e010SKOVACS Krisztian { 244511c3f92SEric Dumazet return skb_rtable(skb)->rt_iif; 2451668e010SKOVACS Krisztian } 2461668e010SKOVACS Krisztian 247323e126fSDavid S. Miller extern int sysctl_ip_default_ttl; 248323e126fSDavid S. Miller 249323e126fSDavid S. Miller static inline int ip4_dst_hoplimit(const struct dst_entry *dst) 250323e126fSDavid S. Miller { 251323e126fSDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 252323e126fSDavid S. Miller 253323e126fSDavid S. Miller if (hoplimit == 0) 254323e126fSDavid S. Miller hoplimit = sysctl_ip_default_ttl; 255323e126fSDavid S. Miller return hoplimit; 256323e126fSDavid S. Miller } 257323e126fSDavid S. Miller 2581da177e4SLinus Torvalds #endif /* _ROUTE_H */ 259