1/* 2 * INET An implementation of the TCP/IP protocol suite for the LINUX 3 * operating system. INET is implemented using the BSD Socket 4 * interface as the means of communication with the user level. 5 * 6 * Definitions for the Forwarding Information Base. 7 * 8 * Authors: A.N.Kuznetsov, <kuznet@ms2.inr.ac.ru> 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 13 * 2 of the License, or (at your option) any later version. 14 */ 15 16#ifndef _NET_IP_FIB_H 17#define _NET_IP_FIB_H 18 19#include <net/flow.h> 20#include <linux/seq_file.h> 21#include <linux/rcupdate.h> 22#include <net/fib_rules.h> 23#include <net/inetpeer.h> 24#include <linux/percpu.h> 25 26struct fib_config { 27 u8 fc_dst_len; 28 u8 fc_tos; 29 u8 fc_protocol; 30 u8 fc_scope; 31 u8 fc_type; 32 /* 3 bytes unused */ 33 u32 fc_table; 34 __be32 fc_dst; 35 __be32 fc_gw; 36 int fc_oif; 37 u32 fc_flags; 38 u32 fc_priority; 39 __be32 fc_prefsrc; 40 struct nlattr *fc_mx; 41 struct rtnexthop *fc_mp; 42 int fc_mx_len; 43 int fc_mp_len; 44 u32 fc_flow; 45 u32 fc_nlflags; 46 struct nl_info fc_nlinfo; 47 }; 48 49struct fib_info; 50struct rtable; 51 52struct fib_nh_exception { 53 struct fib_nh_exception __rcu *fnhe_next; 54 int fnhe_genid; 55 __be32 fnhe_daddr; 56 u32 fnhe_pmtu; 57 __be32 fnhe_gw; 58 unsigned long fnhe_expires; 59 struct rtable __rcu *fnhe_rth_input; 60 struct rtable __rcu *fnhe_rth_output; 61 unsigned long fnhe_stamp; 62 struct rcu_head rcu; 63}; 64 65struct fnhe_hash_bucket { 66 struct fib_nh_exception __rcu *chain; 67}; 68 69#define FNHE_HASH_SHIFT 11 70#define FNHE_HASH_SIZE (1 << FNHE_HASH_SHIFT) 71#define FNHE_RECLAIM_DEPTH 5 72 73struct fib_nh { 74 struct net_device *nh_dev; 75 struct hlist_node nh_hash; 76 struct fib_info *nh_parent; 77 unsigned int nh_flags; 78 unsigned char nh_scope; 79#ifdef CONFIG_IP_ROUTE_MULTIPATH 80 int nh_weight; 81 int nh_power; 82#endif 83#ifdef CONFIG_IP_ROUTE_CLASSID 84 __u32 nh_tclassid; 85#endif 86 int nh_oif; 87 __be32 nh_gw; 88 __be32 nh_saddr; 89 int nh_saddr_genid; 90 struct rtable __rcu * __percpu *nh_pcpu_rth_output; 91 struct rtable __rcu *nh_rth_input; 92 struct fnhe_hash_bucket __rcu *nh_exceptions; 93}; 94 95/* 96 * This structure contains data shared by many of routes. 97 */ 98 99struct fib_info { 100 struct hlist_node fib_hash; 101 struct hlist_node fib_lhash; 102 struct net *fib_net; 103 int fib_treeref; 104 atomic_t fib_clntref; 105 unsigned int fib_flags; 106 unsigned char fib_dead; 107 unsigned char fib_protocol; 108 unsigned char fib_scope; 109 unsigned char fib_type; 110 __be32 fib_prefsrc; 111 u32 fib_priority; 112 u32 *fib_metrics; 113#define fib_mtu fib_metrics[RTAX_MTU-1] 114#define fib_window fib_metrics[RTAX_WINDOW-1] 115#define fib_rtt fib_metrics[RTAX_RTT-1] 116#define fib_advmss fib_metrics[RTAX_ADVMSS-1] 117 int fib_nhs; 118#ifdef CONFIG_IP_ROUTE_MULTIPATH 119 int fib_power; 120#endif 121 struct rcu_head rcu; 122 struct fib_nh fib_nh[0]; 123#define fib_dev fib_nh[0].nh_dev 124}; 125 126 127#ifdef CONFIG_IP_MULTIPLE_TABLES 128struct fib_rule; 129#endif 130 131struct fib_table; 132struct fib_result { 133 unsigned char prefixlen; 134 unsigned char nh_sel; 135 unsigned char type; 136 unsigned char scope; 137 u32 tclassid; 138 struct fib_info *fi; 139 struct fib_table *table; 140 struct hlist_head *fa_head; 141}; 142 143struct fib_result_nl { 144 __be32 fl_addr; /* To be looked up*/ 145 u32 fl_mark; 146 unsigned char fl_tos; 147 unsigned char fl_scope; 148 unsigned char tb_id_in; 149 150 unsigned char tb_id; /* Results */ 151 unsigned char prefixlen; 152 unsigned char nh_sel; 153 unsigned char type; 154 unsigned char scope; 155 int err; 156}; 157 158#ifdef CONFIG_IP_ROUTE_MULTIPATH 159#define FIB_RES_NH(res) ((res).fi->fib_nh[(res).nh_sel]) 160#else /* CONFIG_IP_ROUTE_MULTIPATH */ 161#define FIB_RES_NH(res) ((res).fi->fib_nh[0]) 162#endif /* CONFIG_IP_ROUTE_MULTIPATH */ 163 164#ifdef CONFIG_IP_MULTIPLE_TABLES 165#define FIB_TABLE_HASHSZ 256 166#else 167#define FIB_TABLE_HASHSZ 2 168#endif 169 170__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh); 171 172#define FIB_RES_SADDR(net, res) \ 173 ((FIB_RES_NH(res).nh_saddr_genid == \ 174 atomic_read(&(net)->ipv4.dev_addr_genid)) ? \ 175 FIB_RES_NH(res).nh_saddr : \ 176 fib_info_update_nh_saddr((net), &FIB_RES_NH(res))) 177#define FIB_RES_GW(res) (FIB_RES_NH(res).nh_gw) 178#define FIB_RES_DEV(res) (FIB_RES_NH(res).nh_dev) 179#define FIB_RES_OIF(res) (FIB_RES_NH(res).nh_oif) 180 181#define FIB_RES_PREFSRC(net, res) ((res).fi->fib_prefsrc ? : \ 182 FIB_RES_SADDR(net, res)) 183 184struct fib_table { 185 struct hlist_node tb_hlist; 186 u32 tb_id; 187 int tb_default; 188 int tb_num_default; 189 struct rcu_head rcu; 190 unsigned long *tb_data; 191 unsigned long __data[0]; 192}; 193 194int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp, 195 struct fib_result *res, int fib_flags); 196int fib_table_insert(struct fib_table *, struct fib_config *); 197int fib_table_delete(struct fib_table *, struct fib_config *); 198int fib_table_dump(struct fib_table *table, struct sk_buff *skb, 199 struct netlink_callback *cb); 200int fib_table_flush(struct fib_table *table); 201struct fib_table *fib_trie_unmerge(struct fib_table *main_tb); 202void fib_table_flush_external(struct fib_table *table); 203void fib_free_table(struct fib_table *tb); 204 205#ifndef CONFIG_IP_MULTIPLE_TABLES 206 207#define TABLE_LOCAL_INDEX (RT_TABLE_LOCAL & (FIB_TABLE_HASHSZ - 1)) 208#define TABLE_MAIN_INDEX (RT_TABLE_MAIN & (FIB_TABLE_HASHSZ - 1)) 209 210static inline struct fib_table *fib_get_table(struct net *net, u32 id) 211{ 212 struct hlist_node *tb_hlist; 213 struct hlist_head *ptr; 214 215 ptr = id == RT_TABLE_LOCAL ? 216 &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX] : 217 &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]; 218 219 tb_hlist = rcu_dereference_rtnl(hlist_first_rcu(ptr)); 220 221 return hlist_entry(tb_hlist, struct fib_table, tb_hlist); 222} 223 224static inline struct fib_table *fib_new_table(struct net *net, u32 id) 225{ 226 return fib_get_table(net, id); 227} 228 229static inline int fib_lookup(struct net *net, const struct flowi4 *flp, 230 struct fib_result *res) 231{ 232 struct fib_table *tb; 233 int err = -ENETUNREACH; 234 235 rcu_read_lock(); 236 237 tb = fib_get_table(net, RT_TABLE_MAIN); 238 if (tb && !fib_table_lookup(tb, flp, res, FIB_LOOKUP_NOREF)) 239 err = 0; 240 241 rcu_read_unlock(); 242 243 return err; 244} 245 246#else /* CONFIG_IP_MULTIPLE_TABLES */ 247int __net_init fib4_rules_init(struct net *net); 248void __net_exit fib4_rules_exit(struct net *net); 249 250struct fib_table *fib_new_table(struct net *net, u32 id); 251struct fib_table *fib_get_table(struct net *net, u32 id); 252 253int __fib_lookup(struct net *net, struct flowi4 *flp, struct fib_result *res); 254 255static inline int fib_lookup(struct net *net, struct flowi4 *flp, 256 struct fib_result *res) 257{ 258 struct fib_table *tb; 259 int err; 260 261 if (net->ipv4.fib_has_custom_rules) 262 return __fib_lookup(net, flp, res); 263 264 rcu_read_lock(); 265 266 res->tclassid = 0; 267 268 for (err = 0; !err; err = -ENETUNREACH) { 269 tb = rcu_dereference_rtnl(net->ipv4.fib_main); 270 if (tb && !fib_table_lookup(tb, flp, res, FIB_LOOKUP_NOREF)) 271 break; 272 273 tb = rcu_dereference_rtnl(net->ipv4.fib_default); 274 if (tb && !fib_table_lookup(tb, flp, res, FIB_LOOKUP_NOREF)) 275 break; 276 } 277 278 rcu_read_unlock(); 279 280 return err; 281} 282 283#endif /* CONFIG_IP_MULTIPLE_TABLES */ 284 285/* Exported by fib_frontend.c */ 286extern const struct nla_policy rtm_ipv4_policy[]; 287void ip_fib_init(void); 288__be32 fib_compute_spec_dst(struct sk_buff *skb); 289int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst, 290 u8 tos, int oif, struct net_device *dev, 291 struct in_device *idev, u32 *itag); 292void fib_select_default(struct fib_result *res); 293#ifdef CONFIG_IP_ROUTE_CLASSID 294static inline int fib_num_tclassid_users(struct net *net) 295{ 296 return net->ipv4.fib_num_tclassid_users; 297} 298#else 299static inline int fib_num_tclassid_users(struct net *net) 300{ 301 return 0; 302} 303#endif 304int fib_unmerge(struct net *net); 305void fib_flush_external(struct net *net); 306 307/* Exported by fib_semantics.c */ 308int ip_fib_check_default(__be32 gw, struct net_device *dev); 309int fib_sync_down_dev(struct net_device *dev, int force); 310int fib_sync_down_addr(struct net *net, __be32 local); 311int fib_sync_up(struct net_device *dev); 312void fib_select_multipath(struct fib_result *res); 313 314/* Exported by fib_trie.c */ 315void fib_trie_init(void); 316struct fib_table *fib_trie_table(u32 id, struct fib_table *alias); 317 318static inline void fib_combine_itag(u32 *itag, const struct fib_result *res) 319{ 320#ifdef CONFIG_IP_ROUTE_CLASSID 321#ifdef CONFIG_IP_MULTIPLE_TABLES 322 u32 rtag; 323#endif 324 *itag = FIB_RES_NH(*res).nh_tclassid<<16; 325#ifdef CONFIG_IP_MULTIPLE_TABLES 326 rtag = res->tclassid; 327 if (*itag == 0) 328 *itag = (rtag<<16); 329 *itag |= (rtag>>16); 330#endif 331#endif 332} 333 334void free_fib_info(struct fib_info *fi); 335 336static inline void fib_info_put(struct fib_info *fi) 337{ 338 if (atomic_dec_and_test(&fi->fib_clntref)) 339 free_fib_info(fi); 340} 341 342#ifdef CONFIG_PROC_FS 343int __net_init fib_proc_init(struct net *net); 344void __net_exit fib_proc_exit(struct net *net); 345#else 346static inline int fib_proc_init(struct net *net) 347{ 348 return 0; 349} 350static inline void fib_proc_exit(struct net *net) 351{ 352} 353#endif 354 355#endif /* _NET_FIB_H */ 356