1 /* 2 * Linux INET6 implementation 3 * 4 * Authors: 5 * Pedro Roque <roque@di.fc.ul.pt> 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 10 * 2 of the License, or (at your option) any later version. 11 */ 12 13 #ifndef _IP6_FIB_H 14 #define _IP6_FIB_H 15 16 #include <linux/ipv6_route.h> 17 #include <linux/rtnetlink.h> 18 #include <linux/spinlock.h> 19 #include <linux/notifier.h> 20 #include <net/dst.h> 21 #include <net/flow.h> 22 #include <net/netlink.h> 23 #include <net/inetpeer.h> 24 #include <net/fib_notifier.h> 25 26 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 27 #define FIB6_TABLE_HASHSZ 256 28 #else 29 #define FIB6_TABLE_HASHSZ 1 30 #endif 31 32 #define RT6_DEBUG 2 33 34 #if RT6_DEBUG >= 3 35 #define RT6_TRACE(x...) pr_debug(x) 36 #else 37 #define RT6_TRACE(x...) do { ; } while (0) 38 #endif 39 40 struct rt6_info; 41 42 struct fib6_config { 43 u32 fc_table; 44 u32 fc_metric; 45 int fc_dst_len; 46 int fc_src_len; 47 int fc_ifindex; 48 u32 fc_flags; 49 u32 fc_protocol; 50 u16 fc_type; /* only 8 bits are used */ 51 u16 fc_delete_all_nh : 1, 52 __unused : 15; 53 54 struct in6_addr fc_dst; 55 struct in6_addr fc_src; 56 struct in6_addr fc_prefsrc; 57 struct in6_addr fc_gateway; 58 59 unsigned long fc_expires; 60 struct nlattr *fc_mx; 61 int fc_mx_len; 62 int fc_mp_len; 63 struct nlattr *fc_mp; 64 65 struct nl_info fc_nlinfo; 66 struct nlattr *fc_encap; 67 u16 fc_encap_type; 68 }; 69 70 struct fib6_node { 71 struct fib6_node __rcu *parent; 72 struct fib6_node __rcu *left; 73 struct fib6_node __rcu *right; 74 #ifdef CONFIG_IPV6_SUBTREES 75 struct fib6_node __rcu *subtree; 76 #endif 77 struct rt6_info __rcu *leaf; 78 79 __u16 fn_bit; /* bit key */ 80 __u16 fn_flags; 81 int fn_sernum; 82 struct rt6_info __rcu *rr_ptr; 83 struct rcu_head rcu; 84 }; 85 86 struct fib6_gc_args { 87 int timeout; 88 int more; 89 }; 90 91 #ifndef CONFIG_IPV6_SUBTREES 92 #define FIB6_SUBTREE(fn) NULL 93 #else 94 #define FIB6_SUBTREE(fn) (rcu_dereference_protected((fn)->subtree, 1)) 95 #endif 96 97 struct mx6_config { 98 const u32 *mx; 99 DECLARE_BITMAP(mx_valid, RTAX_MAX); 100 }; 101 102 /* 103 * routing information 104 * 105 */ 106 107 struct rt6key { 108 struct in6_addr addr; 109 int plen; 110 }; 111 112 struct fib6_table; 113 114 struct rt6_exception_bucket { 115 struct hlist_head chain; 116 int depth; 117 }; 118 119 struct rt6_exception { 120 struct hlist_node hlist; 121 struct rt6_info *rt6i; 122 unsigned long stamp; 123 struct rcu_head rcu; 124 }; 125 126 #define FIB6_EXCEPTION_BUCKET_SIZE_SHIFT 10 127 #define FIB6_EXCEPTION_BUCKET_SIZE (1 << FIB6_EXCEPTION_BUCKET_SIZE_SHIFT) 128 #define FIB6_MAX_DEPTH 5 129 130 struct rt6_info { 131 struct dst_entry dst; 132 struct rt6_info __rcu *rt6_next; 133 struct rt6_info *from; 134 135 /* 136 * Tail elements of dst_entry (__refcnt etc.) 137 * and these elements (rarely used in hot path) are in 138 * the same cache line. 139 */ 140 struct fib6_table *rt6i_table; 141 struct fib6_node __rcu *rt6i_node; 142 143 struct in6_addr rt6i_gateway; 144 145 /* Multipath routes: 146 * siblings is a list of rt6_info that have the the same metric/weight, 147 * destination, but not the same gateway. nsiblings is just a cache 148 * to speed up lookup. 149 */ 150 struct list_head rt6i_siblings; 151 unsigned int rt6i_nsiblings; 152 153 atomic_t rt6i_ref; 154 155 unsigned int rt6i_nh_flags; 156 157 /* These are in a separate cache line. */ 158 struct rt6key rt6i_dst ____cacheline_aligned_in_smp; 159 u32 rt6i_flags; 160 struct rt6key rt6i_src; 161 struct rt6key rt6i_prefsrc; 162 163 struct list_head rt6i_uncached; 164 struct uncached_list *rt6i_uncached_list; 165 166 struct inet6_dev *rt6i_idev; 167 struct rt6_info * __percpu *rt6i_pcpu; 168 struct rt6_exception_bucket __rcu *rt6i_exception_bucket; 169 170 u32 rt6i_metric; 171 u32 rt6i_pmtu; 172 /* more non-fragment space at head required */ 173 unsigned short rt6i_nfheader_len; 174 u8 rt6i_protocol; 175 u8 exception_bucket_flushed:1, 176 should_flush:1, 177 unused:6; 178 }; 179 180 #define for_each_fib6_node_rt_rcu(fn) \ 181 for (rt = rcu_dereference((fn)->leaf); rt; \ 182 rt = rcu_dereference(rt->rt6_next)) 183 184 #define for_each_fib6_walker_rt(w) \ 185 for (rt = (w)->leaf; rt; \ 186 rt = rcu_dereference_protected(rt->rt6_next, 1)) 187 188 static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst) 189 { 190 return ((struct rt6_info *)dst)->rt6i_idev; 191 } 192 193 static inline void rt6_clean_expires(struct rt6_info *rt) 194 { 195 rt->rt6i_flags &= ~RTF_EXPIRES; 196 rt->dst.expires = 0; 197 } 198 199 static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires) 200 { 201 rt->dst.expires = expires; 202 rt->rt6i_flags |= RTF_EXPIRES; 203 } 204 205 static inline void rt6_update_expires(struct rt6_info *rt0, int timeout) 206 { 207 struct rt6_info *rt; 208 209 for (rt = rt0; rt && !(rt->rt6i_flags & RTF_EXPIRES); rt = rt->from); 210 if (rt && rt != rt0) 211 rt0->dst.expires = rt->dst.expires; 212 dst_set_expires(&rt0->dst, timeout); 213 rt0->rt6i_flags |= RTF_EXPIRES; 214 } 215 216 /* Function to safely get fn->sernum for passed in rt 217 * and store result in passed in cookie. 218 * Return true if we can get cookie safely 219 * Return false if not 220 */ 221 static inline bool rt6_get_cookie_safe(const struct rt6_info *rt, 222 u32 *cookie) 223 { 224 struct fib6_node *fn; 225 bool status = false; 226 227 rcu_read_lock(); 228 fn = rcu_dereference(rt->rt6i_node); 229 230 if (fn) { 231 *cookie = fn->fn_sernum; 232 /* pairs with smp_wmb() in fib6_update_sernum_upto_root() */ 233 smp_rmb(); 234 status = true; 235 } 236 237 rcu_read_unlock(); 238 return status; 239 } 240 241 static inline u32 rt6_get_cookie(const struct rt6_info *rt) 242 { 243 u32 cookie = 0; 244 245 if (rt->rt6i_flags & RTF_PCPU || 246 (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from)) 247 rt = rt->from; 248 249 rt6_get_cookie_safe(rt, &cookie); 250 251 return cookie; 252 } 253 254 static inline void ip6_rt_put(struct rt6_info *rt) 255 { 256 /* dst_release() accepts a NULL parameter. 257 * We rely on dst being first structure in struct rt6_info 258 */ 259 BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0); 260 dst_release(&rt->dst); 261 } 262 263 void rt6_free_pcpu(struct rt6_info *non_pcpu_rt); 264 265 static inline void rt6_hold(struct rt6_info *rt) 266 { 267 atomic_inc(&rt->rt6i_ref); 268 } 269 270 static inline void rt6_release(struct rt6_info *rt) 271 { 272 if (atomic_dec_and_test(&rt->rt6i_ref)) { 273 rt6_free_pcpu(rt); 274 dst_dev_put(&rt->dst); 275 dst_release(&rt->dst); 276 } 277 } 278 279 enum fib6_walk_state { 280 #ifdef CONFIG_IPV6_SUBTREES 281 FWS_S, 282 #endif 283 FWS_L, 284 FWS_R, 285 FWS_C, 286 FWS_U 287 }; 288 289 struct fib6_walker { 290 struct list_head lh; 291 struct fib6_node *root, *node; 292 struct rt6_info *leaf; 293 enum fib6_walk_state state; 294 unsigned int skip; 295 unsigned int count; 296 int (*func)(struct fib6_walker *); 297 void *args; 298 }; 299 300 struct rt6_statistics { 301 __u32 fib_nodes; /* all fib6 nodes */ 302 __u32 fib_route_nodes; /* intermediate nodes */ 303 __u32 fib_rt_entries; /* rt entries in fib table */ 304 __u32 fib_rt_cache; /* cached rt entries in exception table */ 305 __u32 fib_discarded_routes; /* total number of routes delete */ 306 307 /* The following stats are not protected by any lock */ 308 atomic_t fib_rt_alloc; /* total number of routes alloced */ 309 atomic_t fib_rt_uncache; /* rt entries in uncached list */ 310 }; 311 312 #define RTN_TL_ROOT 0x0001 313 #define RTN_ROOT 0x0002 /* tree root node */ 314 #define RTN_RTINFO 0x0004 /* node with valid routing info */ 315 316 /* 317 * priority levels (or metrics) 318 * 319 */ 320 321 322 struct fib6_table { 323 struct hlist_node tb6_hlist; 324 u32 tb6_id; 325 spinlock_t tb6_lock; 326 struct fib6_node tb6_root; 327 struct inet_peer_base tb6_peers; 328 unsigned int flags; 329 unsigned int fib_seq; 330 #define RT6_TABLE_HAS_DFLT_ROUTER BIT(0) 331 }; 332 333 #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC 334 #define RT6_TABLE_MAIN RT_TABLE_MAIN 335 #define RT6_TABLE_DFLT RT6_TABLE_MAIN 336 #define RT6_TABLE_INFO RT6_TABLE_MAIN 337 #define RT6_TABLE_PREFIX RT6_TABLE_MAIN 338 339 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 340 #define FIB6_TABLE_MIN 1 341 #define FIB6_TABLE_MAX RT_TABLE_MAX 342 #define RT6_TABLE_LOCAL RT_TABLE_LOCAL 343 #else 344 #define FIB6_TABLE_MIN RT_TABLE_MAIN 345 #define FIB6_TABLE_MAX FIB6_TABLE_MIN 346 #define RT6_TABLE_LOCAL RT6_TABLE_MAIN 347 #endif 348 349 typedef struct rt6_info *(*pol_lookup_t)(struct net *, 350 struct fib6_table *, 351 struct flowi6 *, int); 352 353 struct fib6_entry_notifier_info { 354 struct fib_notifier_info info; /* must be first */ 355 struct rt6_info *rt; 356 }; 357 358 /* 359 * exported functions 360 */ 361 362 struct fib6_table *fib6_get_table(struct net *net, u32 id); 363 struct fib6_table *fib6_new_table(struct net *net, u32 id); 364 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6, 365 int flags, pol_lookup_t lookup); 366 367 struct fib6_node *fib6_lookup(struct fib6_node *root, 368 const struct in6_addr *daddr, 369 const struct in6_addr *saddr); 370 371 struct fib6_node *fib6_locate(struct fib6_node *root, 372 const struct in6_addr *daddr, int dst_len, 373 const struct in6_addr *saddr, int src_len, 374 bool exact_match); 375 376 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg), 377 void *arg); 378 379 int fib6_add(struct fib6_node *root, struct rt6_info *rt, 380 struct nl_info *info, struct mx6_config *mxc, 381 struct netlink_ext_ack *extack); 382 int fib6_del(struct rt6_info *rt, struct nl_info *info); 383 384 void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 385 unsigned int flags); 386 387 void fib6_run_gc(unsigned long expires, struct net *net, bool force); 388 389 void fib6_gc_cleanup(void); 390 391 int fib6_init(void); 392 393 int ipv6_route_open(struct inode *inode, struct file *file); 394 395 int call_fib6_notifier(struct notifier_block *nb, struct net *net, 396 enum fib_event_type event_type, 397 struct fib_notifier_info *info); 398 int call_fib6_notifiers(struct net *net, enum fib_event_type event_type, 399 struct fib_notifier_info *info); 400 401 int __net_init fib6_notifier_init(struct net *net); 402 void __net_exit fib6_notifier_exit(struct net *net); 403 404 unsigned int fib6_tables_seq_read(struct net *net); 405 int fib6_tables_dump(struct net *net, struct notifier_block *nb); 406 407 void fib6_update_sernum(struct rt6_info *rt); 408 void fib6_update_sernum_upto_root(struct net *net, struct rt6_info *rt); 409 410 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 411 int fib6_rules_init(void); 412 void fib6_rules_cleanup(void); 413 bool fib6_rule_default(const struct fib_rule *rule); 414 int fib6_rules_dump(struct net *net, struct notifier_block *nb); 415 unsigned int fib6_rules_seq_read(struct net *net); 416 #else 417 static inline int fib6_rules_init(void) 418 { 419 return 0; 420 } 421 static inline void fib6_rules_cleanup(void) 422 { 423 return ; 424 } 425 static inline bool fib6_rule_default(const struct fib_rule *rule) 426 { 427 return true; 428 } 429 static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb) 430 { 431 return 0; 432 } 433 static inline unsigned int fib6_rules_seq_read(struct net *net) 434 { 435 return 0; 436 } 437 #endif 438 #endif 439