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 struct rt6_info; 33 34 struct fib6_config { 35 u32 fc_table; 36 u32 fc_metric; 37 int fc_dst_len; 38 int fc_src_len; 39 int fc_ifindex; 40 u32 fc_flags; 41 u32 fc_protocol; 42 u16 fc_type; /* only 8 bits are used */ 43 u16 fc_delete_all_nh : 1, 44 __unused : 15; 45 46 struct in6_addr fc_dst; 47 struct in6_addr fc_src; 48 struct in6_addr fc_prefsrc; 49 struct in6_addr fc_gateway; 50 51 unsigned long fc_expires; 52 struct nlattr *fc_mx; 53 int fc_mx_len; 54 int fc_mp_len; 55 struct nlattr *fc_mp; 56 57 struct nl_info fc_nlinfo; 58 struct nlattr *fc_encap; 59 u16 fc_encap_type; 60 }; 61 62 struct fib6_node { 63 struct fib6_node *parent; 64 struct fib6_node *left; 65 struct fib6_node *right; 66 #ifdef CONFIG_IPV6_SUBTREES 67 struct fib6_node *subtree; 68 #endif 69 struct rt6_info *leaf; 70 71 __u16 fn_bit; /* bit key */ 72 __u16 fn_flags; 73 int fn_sernum; 74 struct rt6_info *rr_ptr; 75 }; 76 77 #ifndef CONFIG_IPV6_SUBTREES 78 #define FIB6_SUBTREE(fn) NULL 79 #else 80 #define FIB6_SUBTREE(fn) ((fn)->subtree) 81 #endif 82 83 struct mx6_config { 84 const u32 *mx; 85 DECLARE_BITMAP(mx_valid, RTAX_MAX); 86 }; 87 88 /* 89 * routing information 90 * 91 */ 92 93 struct rt6key { 94 struct in6_addr addr; 95 int plen; 96 }; 97 98 struct fib6_table; 99 100 struct rt6_info { 101 struct dst_entry dst; 102 103 /* 104 * Tail elements of dst_entry (__refcnt etc.) 105 * and these elements (rarely used in hot path) are in 106 * the same cache line. 107 */ 108 struct fib6_table *rt6i_table; 109 struct fib6_node *rt6i_node; 110 111 struct in6_addr rt6i_gateway; 112 113 /* Multipath routes: 114 * siblings is a list of rt6_info that have the the same metric/weight, 115 * destination, but not the same gateway. nsiblings is just a cache 116 * to speed up lookup. 117 */ 118 struct list_head rt6i_siblings; 119 unsigned int rt6i_nsiblings; 120 121 atomic_t rt6i_ref; 122 123 unsigned int rt6i_nh_flags; 124 125 /* These are in a separate cache line. */ 126 struct rt6key rt6i_dst ____cacheline_aligned_in_smp; 127 u32 rt6i_flags; 128 struct rt6key rt6i_src; 129 struct rt6key rt6i_prefsrc; 130 131 struct list_head rt6i_uncached; 132 struct uncached_list *rt6i_uncached_list; 133 134 struct inet6_dev *rt6i_idev; 135 struct rt6_info * __percpu *rt6i_pcpu; 136 137 u32 rt6i_metric; 138 u32 rt6i_pmtu; 139 /* more non-fragment space at head required */ 140 unsigned short rt6i_nfheader_len; 141 u8 rt6i_protocol; 142 }; 143 144 static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst) 145 { 146 return ((struct rt6_info *)dst)->rt6i_idev; 147 } 148 149 static inline void rt6_clean_expires(struct rt6_info *rt) 150 { 151 rt->rt6i_flags &= ~RTF_EXPIRES; 152 rt->dst.expires = 0; 153 } 154 155 static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires) 156 { 157 rt->dst.expires = expires; 158 rt->rt6i_flags |= RTF_EXPIRES; 159 } 160 161 static inline void rt6_update_expires(struct rt6_info *rt0, int timeout) 162 { 163 struct rt6_info *rt; 164 165 for (rt = rt0; rt && !(rt->rt6i_flags & RTF_EXPIRES); 166 rt = (struct rt6_info *)rt->dst.from); 167 if (rt && rt != rt0) 168 rt0->dst.expires = rt->dst.expires; 169 170 dst_set_expires(&rt0->dst, timeout); 171 rt0->rt6i_flags |= RTF_EXPIRES; 172 } 173 174 static inline u32 rt6_get_cookie(const struct rt6_info *rt) 175 { 176 if (rt->rt6i_flags & RTF_PCPU || 177 (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from)) 178 rt = (struct rt6_info *)(rt->dst.from); 179 180 return rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0; 181 } 182 183 static inline void ip6_rt_put(struct rt6_info *rt) 184 { 185 /* dst_release() accepts a NULL parameter. 186 * We rely on dst being first structure in struct rt6_info 187 */ 188 BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0); 189 dst_release(&rt->dst); 190 } 191 192 void rt6_free_pcpu(struct rt6_info *non_pcpu_rt); 193 194 static inline void rt6_hold(struct rt6_info *rt) 195 { 196 atomic_inc(&rt->rt6i_ref); 197 } 198 199 static inline void rt6_release(struct rt6_info *rt) 200 { 201 if (atomic_dec_and_test(&rt->rt6i_ref)) { 202 rt6_free_pcpu(rt); 203 dst_dev_put(&rt->dst); 204 dst_release(&rt->dst); 205 } 206 } 207 208 enum fib6_walk_state { 209 #ifdef CONFIG_IPV6_SUBTREES 210 FWS_S, 211 #endif 212 FWS_L, 213 FWS_R, 214 FWS_C, 215 FWS_U 216 }; 217 218 struct fib6_walker { 219 struct list_head lh; 220 struct fib6_node *root, *node; 221 struct rt6_info *leaf; 222 enum fib6_walk_state state; 223 bool prune; 224 unsigned int skip; 225 unsigned int count; 226 int (*func)(struct fib6_walker *); 227 void *args; 228 }; 229 230 struct rt6_statistics { 231 __u32 fib_nodes; 232 __u32 fib_route_nodes; 233 __u32 fib_rt_alloc; /* permanent routes */ 234 __u32 fib_rt_entries; /* rt entries in table */ 235 __u32 fib_rt_cache; /* cache routes */ 236 __u32 fib_discarded_routes; 237 }; 238 239 #define RTN_TL_ROOT 0x0001 240 #define RTN_ROOT 0x0002 /* tree root node */ 241 #define RTN_RTINFO 0x0004 /* node with valid routing info */ 242 243 /* 244 * priority levels (or metrics) 245 * 246 */ 247 248 249 struct fib6_table { 250 struct hlist_node tb6_hlist; 251 u32 tb6_id; 252 rwlock_t tb6_lock; 253 struct fib6_node tb6_root; 254 struct inet_peer_base tb6_peers; 255 unsigned int flags; 256 unsigned int fib_seq; 257 #define RT6_TABLE_HAS_DFLT_ROUTER BIT(0) 258 }; 259 260 #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC 261 #define RT6_TABLE_MAIN RT_TABLE_MAIN 262 #define RT6_TABLE_DFLT RT6_TABLE_MAIN 263 #define RT6_TABLE_INFO RT6_TABLE_MAIN 264 #define RT6_TABLE_PREFIX RT6_TABLE_MAIN 265 266 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 267 #define FIB6_TABLE_MIN 1 268 #define FIB6_TABLE_MAX RT_TABLE_MAX 269 #define RT6_TABLE_LOCAL RT_TABLE_LOCAL 270 #else 271 #define FIB6_TABLE_MIN RT_TABLE_MAIN 272 #define FIB6_TABLE_MAX FIB6_TABLE_MIN 273 #define RT6_TABLE_LOCAL RT6_TABLE_MAIN 274 #endif 275 276 typedef struct rt6_info *(*pol_lookup_t)(struct net *, 277 struct fib6_table *, 278 struct flowi6 *, int); 279 280 struct fib6_entry_notifier_info { 281 struct fib_notifier_info info; /* must be first */ 282 struct rt6_info *rt; 283 }; 284 285 /* 286 * exported functions 287 */ 288 289 struct fib6_table *fib6_get_table(struct net *net, u32 id); 290 struct fib6_table *fib6_new_table(struct net *net, u32 id); 291 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6, 292 int flags, pol_lookup_t lookup); 293 294 struct fib6_node *fib6_lookup(struct fib6_node *root, 295 const struct in6_addr *daddr, 296 const struct in6_addr *saddr); 297 298 struct fib6_node *fib6_locate(struct fib6_node *root, 299 const struct in6_addr *daddr, int dst_len, 300 const struct in6_addr *saddr, int src_len); 301 302 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg), 303 void *arg); 304 305 int fib6_add(struct fib6_node *root, struct rt6_info *rt, 306 struct nl_info *info, struct mx6_config *mxc, 307 struct netlink_ext_ack *extack); 308 int fib6_del(struct rt6_info *rt, struct nl_info *info); 309 310 void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 311 unsigned int flags); 312 313 void fib6_run_gc(unsigned long expires, struct net *net, bool force); 314 315 void fib6_gc_cleanup(void); 316 317 int fib6_init(void); 318 319 int ipv6_route_open(struct inode *inode, struct file *file); 320 321 int call_fib6_notifier(struct notifier_block *nb, struct net *net, 322 enum fib_event_type event_type, 323 struct fib_notifier_info *info); 324 int call_fib6_notifiers(struct net *net, enum fib_event_type event_type, 325 struct fib_notifier_info *info); 326 327 int __net_init fib6_notifier_init(struct net *net); 328 void __net_exit fib6_notifier_exit(struct net *net); 329 330 unsigned int fib6_tables_seq_read(struct net *net); 331 int fib6_tables_dump(struct net *net, struct notifier_block *nb); 332 333 #ifdef CONFIG_IPV6_MULTIPLE_TABLES 334 int fib6_rules_init(void); 335 void fib6_rules_cleanup(void); 336 bool fib6_rule_default(const struct fib_rule *rule); 337 int fib6_rules_dump(struct net *net, struct notifier_block *nb); 338 unsigned int fib6_rules_seq_read(struct net *net); 339 #else 340 static inline int fib6_rules_init(void) 341 { 342 return 0; 343 } 344 static inline void fib6_rules_cleanup(void) 345 { 346 return ; 347 } 348 static inline bool fib6_rule_default(const struct fib_rule *rule) 349 { 350 return true; 351 } 352 static inline int fib6_rules_dump(struct net *net, struct notifier_block *nb) 353 { 354 return 0; 355 } 356 static inline unsigned int fib6_rules_seq_read(struct net *net) 357 { 358 return 0; 359 } 360 #endif 361 #endif 362