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 */ 671da177e4SLinus Torvalds int rt_iif; 685e2b61f7SDavid S. Miller int rt_oif; 695e2b61f7SDavid S. Miller __u32 rt_mark; 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds /* Info on neighbour */ 72f2c3fe24SAl Viro __be32 rt_gateway; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds /* Miscellaneous cached information */ 75f2c3fe24SAl Viro __be32 rt_spec_dst; /* RFC1122 specific destination */ 766431cbc2SDavid S. Miller u32 rt_peer_genid; 771da177e4SLinus Torvalds struct inet_peer *peer; /* long-living peer info */ 7862fa8a84SDavid S. Miller struct fib_info *fi; /* for client ref to shared metrics */ 791da177e4SLinus Torvalds }; 801da177e4SLinus Torvalds 81c7537967SDavid S. Miller static inline bool rt_is_input_route(struct rtable *rt) 82c7537967SDavid S. Miller { 835e2b61f7SDavid S. Miller return rt->rt_iif != 0; 84c7537967SDavid S. Miller } 85c7537967SDavid S. Miller 86c7537967SDavid S. Miller static inline bool rt_is_output_route(struct rtable *rt) 87c7537967SDavid S. Miller { 885e2b61f7SDavid S. Miller return rt->rt_iif == 0; 89c7537967SDavid S. Miller } 90c7537967SDavid S. Miller 91fd2c3ef7SEric Dumazet struct ip_rt_acct { 921da177e4SLinus Torvalds __u32 o_bytes; 931da177e4SLinus Torvalds __u32 o_packets; 941da177e4SLinus Torvalds __u32 i_bytes; 951da177e4SLinus Torvalds __u32 i_packets; 961da177e4SLinus Torvalds }; 971da177e4SLinus Torvalds 98fd2c3ef7SEric Dumazet struct rt_cache_stat { 991da177e4SLinus Torvalds unsigned int in_hit; 1001da177e4SLinus Torvalds unsigned int in_slow_tot; 1011da177e4SLinus Torvalds unsigned int in_slow_mc; 1021da177e4SLinus Torvalds unsigned int in_no_route; 1031da177e4SLinus Torvalds unsigned int in_brd; 1041da177e4SLinus Torvalds unsigned int in_martian_dst; 1051da177e4SLinus Torvalds unsigned int in_martian_src; 1061da177e4SLinus Torvalds unsigned int out_hit; 1071da177e4SLinus Torvalds unsigned int out_slow_tot; 1081da177e4SLinus Torvalds unsigned int out_slow_mc; 1091da177e4SLinus Torvalds unsigned int gc_total; 1101da177e4SLinus Torvalds unsigned int gc_ignored; 1111da177e4SLinus Torvalds unsigned int gc_goal_miss; 1121da177e4SLinus Torvalds unsigned int gc_dst_overflow; 1131da177e4SLinus Torvalds unsigned int in_hlist_search; 1141da177e4SLinus Torvalds unsigned int out_hlist_search; 1151da177e4SLinus Torvalds }; 1161da177e4SLinus Torvalds 1177d720c3eSTejun Heo extern struct ip_rt_acct __percpu *ip_rt_acct; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds struct in_device; 1201da177e4SLinus Torvalds extern int ip_rt_init(void); 121f7655229SAl Viro extern void ip_rt_redirect(__be32 old_gw, __be32 dst, __be32 new_gw, 122f7655229SAl Viro __be32 src, struct net_device *dev); 12376e6ebfbSDenis V. Lunev extern void rt_cache_flush(struct net *net, int how); 1246561a3b1SDavid S. Miller extern void rt_cache_flush_batch(struct net *net); 1259d6ec938SDavid S. Miller extern struct rtable *__ip_route_output_key(struct net *, const struct flowi4 *flp); 1269d6ec938SDavid S. Miller extern struct rtable *ip_route_output_flow(struct net *, struct flowi4 *flp, 127b23dd4feSDavid S. Miller struct sock *sk); 1282774c131SDavid S. Miller extern struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orig); 129407eadd9SEric Dumazet 1309d6ec938SDavid S. Miller static inline struct rtable *ip_route_output_key(struct net *net, struct flowi4 *flp) 1315bfa787fSDavid S. Miller { 1325bfa787fSDavid S. Miller return ip_route_output_flow(net, flp, NULL); 1335bfa787fSDavid S. Miller } 1345bfa787fSDavid S. Miller 13578fbfd8aSDavid S. Miller static inline struct rtable *ip_route_output(struct net *net, __be32 daddr, 13678fbfd8aSDavid S. Miller __be32 saddr, u8 tos, int oif) 13778fbfd8aSDavid S. Miller { 1389d6ec938SDavid S. Miller struct flowi4 fl4 = { 1399d6ec938SDavid S. Miller .flowi4_oif = oif, 1409d6ec938SDavid S. Miller .daddr = daddr, 1419d6ec938SDavid S. Miller .saddr = saddr, 1429d6ec938SDavid S. Miller .flowi4_tos = tos, 14378fbfd8aSDavid S. Miller }; 1449d6ec938SDavid S. Miller return ip_route_output_key(net, &fl4); 14578fbfd8aSDavid S. Miller } 14678fbfd8aSDavid S. Miller 14778fbfd8aSDavid S. Miller static inline struct rtable *ip_route_output_ports(struct net *net, struct sock *sk, 14878fbfd8aSDavid S. Miller __be32 daddr, __be32 saddr, 14978fbfd8aSDavid S. Miller __be16 dport, __be16 sport, 15078fbfd8aSDavid S. Miller __u8 proto, __u8 tos, int oif) 15178fbfd8aSDavid S. Miller { 1529d6ec938SDavid S. Miller struct flowi4 fl4 = { 1539d6ec938SDavid S. Miller .flowi4_oif = oif, 1549d6ec938SDavid S. Miller .flowi4_flags = sk ? inet_sk_flowi_flags(sk) : 0, 1559d6ec938SDavid S. Miller .flowi4_mark = sk ? sk->sk_mark : 0, 1569d6ec938SDavid S. Miller .daddr = daddr, 1579d6ec938SDavid S. Miller .saddr = saddr, 1589d6ec938SDavid S. Miller .flowi4_tos = tos, 1599d6ec938SDavid S. Miller .flowi4_proto = proto, 1609d6ec938SDavid S. Miller .uli.ports.dport = dport, 1619d6ec938SDavid S. Miller .uli.ports.sport = sport, 16278fbfd8aSDavid S. Miller }; 16378fbfd8aSDavid S. Miller if (sk) 1649d6ec938SDavid S. Miller security_sk_classify_flow(sk, flowi4_to_flowi(&fl4)); 1659d6ec938SDavid S. Miller return ip_route_output_flow(net, &fl4, sk); 16678fbfd8aSDavid S. Miller } 16778fbfd8aSDavid S. Miller 16878fbfd8aSDavid S. Miller static inline struct rtable *ip_route_output_gre(struct net *net, 16978fbfd8aSDavid S. Miller __be32 daddr, __be32 saddr, 17078fbfd8aSDavid S. Miller __be32 gre_key, __u8 tos, int oif) 17178fbfd8aSDavid S. Miller { 1729d6ec938SDavid S. Miller struct flowi4 fl4 = { 1739d6ec938SDavid S. Miller .flowi4_oif = oif, 1749d6ec938SDavid S. Miller .daddr = daddr, 1759d6ec938SDavid S. Miller .saddr = saddr, 1769d6ec938SDavid S. Miller .flowi4_tos = tos, 1779d6ec938SDavid S. Miller .flowi4_proto = IPPROTO_GRE, 1789d6ec938SDavid S. Miller .uli.gre_key = gre_key, 17978fbfd8aSDavid S. Miller }; 1809d6ec938SDavid S. Miller return ip_route_output_key(net, &fl4); 18178fbfd8aSDavid S. Miller } 18278fbfd8aSDavid S. Miller 183407eadd9SEric Dumazet extern int ip_route_input_common(struct sk_buff *skb, __be32 dst, __be32 src, 184407eadd9SEric Dumazet u8 tos, struct net_device *devin, bool noref); 185407eadd9SEric Dumazet 186407eadd9SEric Dumazet static inline int ip_route_input(struct sk_buff *skb, __be32 dst, __be32 src, 187407eadd9SEric Dumazet u8 tos, struct net_device *devin) 188407eadd9SEric Dumazet { 189407eadd9SEric Dumazet return ip_route_input_common(skb, dst, src, tos, devin, false); 190407eadd9SEric Dumazet } 191407eadd9SEric Dumazet 192407eadd9SEric Dumazet static inline int ip_route_input_noref(struct sk_buff *skb, __be32 dst, __be32 src, 193407eadd9SEric Dumazet u8 tos, struct net_device *devin) 194407eadd9SEric Dumazet { 195407eadd9SEric Dumazet return ip_route_input_common(skb, dst, src, tos, devin, true); 196407eadd9SEric Dumazet } 197407eadd9SEric Dumazet 1980010e465STimo Teras extern unsigned short ip_rt_frag_needed(struct net *net, struct iphdr *iph, unsigned short new_mtu, struct net_device *dev); 1991da177e4SLinus Torvalds extern void ip_rt_send_redirect(struct sk_buff *skb); 2001da177e4SLinus Torvalds 2016b175b26SEric W. Biederman extern unsigned inet_addr_type(struct net *net, __be32 addr); 2026b175b26SEric W. Biederman extern unsigned inet_dev_addr_type(struct net *net, const struct net_device *dev, __be32 addr); 2031da177e4SLinus Torvalds extern void ip_rt_multicast_event(struct in_device *); 2041bad118aSDenis V. Lunev extern int ip_rt_ioctl(struct net *, unsigned int cmd, void __user *arg); 2051da177e4SLinus Torvalds extern void ip_rt_get_source(u8 *src, struct rtable *rt); 2061da177e4SLinus Torvalds extern int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb); 2071da177e4SLinus Torvalds 2080ff60a45SJamal Hadi Salim struct in_ifaddr; 2090ff60a45SJamal Hadi Salim extern void fib_add_ifaddr(struct in_ifaddr *); 2100ff60a45SJamal Hadi Salim 2111da177e4SLinus Torvalds static inline void ip_rt_put(struct rtable * rt) 2121da177e4SLinus Torvalds { 2131da177e4SLinus Torvalds if (rt) 214d8d1f30bSChangli Gao dst_release(&rt->dst); 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds #define IPTOS_RT_MASK (IPTOS_TOS_MASK & ~3) 2181da177e4SLinus Torvalds 2194839c52bSPhilippe De Muyter extern const __u8 ip_tos2prio[16]; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds static inline char rt_tos2priority(u8 tos) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds return ip_tos2prio[IPTOS_TOS(tos)>>1]; 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 226b23dd4feSDavid S. Miller static inline struct rtable *ip_route_connect(__be32 dst, __be32 src, u32 tos, 227b23dd4feSDavid S. Miller int oif, u8 protocol, 228b23dd4feSDavid S. Miller __be16 sport, __be16 dport, 229b23dd4feSDavid S. Miller struct sock *sk, bool can_sleep) 2301da177e4SLinus Torvalds { 2319d6ec938SDavid S. Miller struct flowi4 fl4 = { 2329d6ec938SDavid S. Miller .flowi4_oif = oif, 2339d6ec938SDavid S. Miller .flowi4_mark = sk->sk_mark, 2349d6ec938SDavid S. Miller .daddr = dst, 2359d6ec938SDavid S. Miller .saddr = src, 2369d6ec938SDavid S. Miller .flowi4_tos = tos, 2379d6ec938SDavid S. Miller .flowi4_proto = protocol, 2389d6ec938SDavid S. Miller .uli.ports.sport = sport, 2399d6ec938SDavid S. Miller .uli.ports.dport = dport, 2406281dcc9SDavid S. Miller }; 2413b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 242b23dd4feSDavid S. Miller struct rtable *rt; 24379876874SKOVACS Krisztian 24479876874SKOVACS Krisztian if (inet_sk(sk)->transparent) 2459d6ec938SDavid S. Miller fl4.flowi4_flags |= FLOWI_FLAG_ANYSRC; 246a4daad6bSDavid S. Miller if (protocol == IPPROTO_TCP) 2479d6ec938SDavid S. Miller fl4.flowi4_flags |= FLOWI_FLAG_PRECOW_METRICS; 2485df65e55SDavid S. Miller if (can_sleep) 2499d6ec938SDavid S. Miller fl4.flowi4_flags |= FLOWI_FLAG_CAN_SLEEP; 25079876874SKOVACS Krisztian 2511da177e4SLinus Torvalds if (!dst || !src) { 2529d6ec938SDavid S. Miller rt = __ip_route_output_key(net, &fl4); 253b23dd4feSDavid S. Miller if (IS_ERR(rt)) 254b23dd4feSDavid S. Miller return rt; 2559d6ec938SDavid S. Miller fl4.daddr = rt->rt_dst; 2569d6ec938SDavid S. Miller fl4.saddr = rt->rt_src; 257b23dd4feSDavid S. Miller ip_rt_put(rt); 2581da177e4SLinus Torvalds } 2599d6ec938SDavid S. Miller security_sk_classify_flow(sk, flowi4_to_flowi(&fl4)); 2609d6ec938SDavid S. Miller return ip_route_output_flow(net, &fl4, sk); 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds 263b23dd4feSDavid S. Miller static inline struct rtable *ip_route_newports(struct rtable *rt, 264b23dd4feSDavid S. Miller u8 protocol, __be16 orig_sport, 265b23dd4feSDavid S. Miller __be16 orig_dport, __be16 sport, 266b23dd4feSDavid S. Miller __be16 dport, struct sock *sk) 2671da177e4SLinus Torvalds { 268dca8b089SDavid S. Miller if (sport != orig_sport || dport != orig_dport) { 2699d6ec938SDavid S. Miller struct flowi4 fl4 = { 2709d6ec938SDavid S. Miller .flowi4_oif = rt->rt_oif, 2719d6ec938SDavid S. Miller .flowi4_mark = rt->rt_mark, 2729d6ec938SDavid S. Miller .daddr = rt->rt_key_dst, 2739d6ec938SDavid S. Miller .saddr = rt->rt_key_src, 2749d6ec938SDavid S. Miller .flowi4_tos = rt->rt_tos, 2759d6ec938SDavid S. Miller .flowi4_proto = protocol, 2769d6ec938SDavid S. Miller .uli.ports.sport = sport, 2779d6ec938SDavid S. Miller .uli.ports.dport = dport 2786281dcc9SDavid S. Miller }; 279fb0c5f0bSUlrich Weber if (inet_sk(sk)->transparent) 2809d6ec938SDavid S. Miller fl4.flowi4_flags |= FLOWI_FLAG_ANYSRC; 281a4daad6bSDavid S. Miller if (protocol == IPPROTO_TCP) 2829d6ec938SDavid S. Miller fl4.flowi4_flags |= FLOWI_FLAG_PRECOW_METRICS; 283b23dd4feSDavid S. Miller ip_rt_put(rt); 2849d6ec938SDavid S. Miller security_sk_classify_flow(sk, flowi4_to_flowi(&fl4)); 2859d6ec938SDavid S. Miller return ip_route_output_flow(sock_net(sk), &fl4, sk); 2861da177e4SLinus Torvalds } 287b23dd4feSDavid S. Miller return rt; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds extern void rt_bind_peer(struct rtable *rt, int create); 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds static inline struct inet_peer *rt_get_peer(struct rtable *rt) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds if (rt->peer) 2951da177e4SLinus Torvalds return rt->peer; 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds rt_bind_peer(rt, 0); 2981da177e4SLinus Torvalds return rt->peer; 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 3011668e010SKOVACS Krisztian static inline int inet_iif(const struct sk_buff *skb) 3021668e010SKOVACS Krisztian { 303511c3f92SEric Dumazet return skb_rtable(skb)->rt_iif; 3041668e010SKOVACS Krisztian } 3051668e010SKOVACS Krisztian 306323e126fSDavid S. Miller extern int sysctl_ip_default_ttl; 307323e126fSDavid S. Miller 308323e126fSDavid S. Miller static inline int ip4_dst_hoplimit(const struct dst_entry *dst) 309323e126fSDavid S. Miller { 310323e126fSDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 311323e126fSDavid S. Miller 312323e126fSDavid S. Miller if (hoplimit == 0) 313323e126fSDavid S. Miller hoplimit = sysctl_ip_default_ttl; 314323e126fSDavid S. Miller return hoplimit; 315323e126fSDavid S. Miller } 316323e126fSDavid S. Miller 3171da177e4SLinus Torvalds #endif /* _ROUTE_H */ 318