2 * Linux INET6 implementation
5 * Pedro Roque <roque@di.fc.ul.pt>
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.
16 #include <linux/ipv6_route.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/spinlock.h>
21 #include <net/netlink.h>
22 #include <net/inetpeer.h>
24 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
25 #define FIB6_TABLE_HASHSZ 256
27 #define FIB6_TABLE_HASHSZ 1
40 u32 fc_type; /* only 8 bits are used */
42 struct in6_addr fc_dst;
43 struct in6_addr fc_src;
44 struct in6_addr fc_prefsrc;
45 struct in6_addr fc_gateway;
47 unsigned long fc_expires;
53 struct nl_info fc_nlinfo;
54 struct nlattr *fc_encap;
59 struct fib6_node *parent;
60 struct fib6_node *left;
61 struct fib6_node *right;
62 #ifdef CONFIG_IPV6_SUBTREES
63 struct fib6_node *subtree;
65 struct rt6_info *leaf;
67 __u16 fn_bit; /* bit key */
70 struct rt6_info *rr_ptr;
74 #ifndef CONFIG_IPV6_SUBTREES
75 #define FIB6_SUBTREE(fn) NULL
77 #define FIB6_SUBTREE(fn) ((fn)->subtree)
82 DECLARE_BITMAP(mx_valid, RTAX_MAX);
101 * Tail elements of dst_entry (__refcnt etc.)
102 * and these elements (rarely used in hot path) are in
103 * the same cache line.
105 struct fib6_table *rt6i_table;
106 struct fib6_node __rcu *rt6i_node;
108 struct in6_addr rt6i_gateway;
111 * siblings is a list of rt6_info that have the the same metric/weight,
112 * destination, but not the same gateway. nsiblings is just a cache
113 * to speed up lookup.
115 struct list_head rt6i_siblings;
116 unsigned int rt6i_nsiblings;
120 /* These are in a separate cache line. */
121 struct rt6key rt6i_dst ____cacheline_aligned_in_smp;
123 struct rt6key rt6i_src;
124 struct rt6key rt6i_prefsrc;
126 struct list_head rt6i_uncached;
127 struct uncached_list *rt6i_uncached_list;
129 struct inet6_dev *rt6i_idev;
130 struct rt6_info * __percpu *rt6i_pcpu;
134 /* more non-fragment space at head required */
135 unsigned short rt6i_nfheader_len;
139 static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
141 return ((struct rt6_info *)dst)->rt6i_idev;
144 static inline void rt6_clean_expires(struct rt6_info *rt)
146 rt->rt6i_flags &= ~RTF_EXPIRES;
150 static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires)
152 rt->dst.expires = expires;
153 rt->rt6i_flags |= RTF_EXPIRES;
156 static inline void rt6_update_expires(struct rt6_info *rt0, int timeout)
160 for (rt = rt0; rt && !(rt->rt6i_flags & RTF_EXPIRES);
161 rt = (struct rt6_info *)rt->dst.from);
163 rt0->dst.expires = rt->dst.expires;
165 dst_set_expires(&rt0->dst, timeout);
166 rt0->rt6i_flags |= RTF_EXPIRES;
169 /* Function to safely get fn->sernum for passed in rt
170 * and store result in passed in cookie.
171 * Return true if we can get cookie safely
172 * Return false if not
174 static inline bool rt6_get_cookie_safe(const struct rt6_info *rt,
177 struct fib6_node *fn;
181 fn = rcu_dereference(rt->rt6i_node);
184 *cookie = fn->fn_sernum;
192 static inline u32 rt6_get_cookie(const struct rt6_info *rt)
196 if (rt->rt6i_flags & RTF_PCPU ||
197 (unlikely(rt->dst.flags & DST_NOCACHE) && rt->dst.from))
198 rt = (struct rt6_info *)(rt->dst.from);
200 rt6_get_cookie_safe(rt, &cookie);
205 static inline void ip6_rt_put(struct rt6_info *rt)
207 /* dst_release() accepts a NULL parameter.
208 * We rely on dst being first structure in struct rt6_info
210 BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
211 dst_release(&rt->dst);
214 enum fib6_walk_state {
215 #ifdef CONFIG_IPV6_SUBTREES
226 struct fib6_node *root, *node;
227 struct rt6_info *leaf;
228 enum fib6_walk_state state;
232 int (*func)(struct fib6_walker *);
236 struct rt6_statistics {
238 __u32 fib_route_nodes;
239 __u32 fib_rt_alloc; /* permanent routes */
240 __u32 fib_rt_entries; /* rt entries in table */
241 __u32 fib_rt_cache; /* cache routes */
242 __u32 fib_discarded_routes;
245 #define RTN_TL_ROOT 0x0001
246 #define RTN_ROOT 0x0002 /* tree root node */
247 #define RTN_RTINFO 0x0004 /* node with valid routing info */
250 * priority levels (or metrics)
256 struct hlist_node tb6_hlist;
259 struct fib6_node tb6_root;
260 struct inet_peer_base tb6_peers;
262 #define RT6_TABLE_HAS_DFLT_ROUTER BIT(0)
265 #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
266 #define RT6_TABLE_MAIN RT_TABLE_MAIN
267 #define RT6_TABLE_DFLT RT6_TABLE_MAIN
268 #define RT6_TABLE_INFO RT6_TABLE_MAIN
269 #define RT6_TABLE_PREFIX RT6_TABLE_MAIN
271 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
272 #define FIB6_TABLE_MIN 1
273 #define FIB6_TABLE_MAX RT_TABLE_MAX
274 #define RT6_TABLE_LOCAL RT_TABLE_LOCAL
276 #define FIB6_TABLE_MIN RT_TABLE_MAIN
277 #define FIB6_TABLE_MAX FIB6_TABLE_MIN
278 #define RT6_TABLE_LOCAL RT6_TABLE_MAIN
281 typedef struct rt6_info *(*pol_lookup_t)(struct net *,
283 struct flowi6 *, int);
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);
294 struct fib6_node *fib6_lookup(struct fib6_node *root,
295 const struct in6_addr *daddr,
296 const struct in6_addr *saddr);
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);
302 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
305 int fib6_add(struct fib6_node *root, struct rt6_info *rt,
306 struct nl_info *info, struct mx6_config *mxc);
307 int fib6_del(struct rt6_info *rt, struct nl_info *info);
309 void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
312 void fib6_run_gc(unsigned long expires, struct net *net, bool force);
314 void fib6_gc_cleanup(void);
318 int ipv6_route_open(struct inode *inode, struct file *file);
320 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
321 int fib6_rules_init(void);
322 void fib6_rules_cleanup(void);
324 static inline int fib6_rules_init(void)
328 static inline void fib6_rules_cleanup(void)