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>
19 #include <linux/notifier.h>
22 #include <net/netlink.h>
23 #include <net/inetpeer.h>
24 #include <net/fib_notifier.h>
26 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
27 #define FIB6_TABLE_HASHSZ 256
29 #define FIB6_TABLE_HASHSZ 1
42 u16 fc_type
; /* only 8 bits are used */
43 u16 fc_delete_all_nh
: 1,
46 struct in6_addr fc_dst
;
47 struct in6_addr fc_src
;
48 struct in6_addr fc_prefsrc
;
49 struct in6_addr fc_gateway
;
51 unsigned long fc_expires
;
57 struct nl_info fc_nlinfo
;
58 struct nlattr
*fc_encap
;
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
;
69 struct rt6_info
*leaf
;
71 __u16 fn_bit
; /* bit key */
74 struct rt6_info
*rr_ptr
;
78 #ifndef CONFIG_IPV6_SUBTREES
79 #define FIB6_SUBTREE(fn) NULL
81 #define FIB6_SUBTREE(fn) ((fn)->subtree)
86 DECLARE_BITMAP(mx_valid
, RTAX_MAX
);
102 struct dst_entry dst
;
105 * Tail elements of dst_entry (__refcnt etc.)
106 * and these elements (rarely used in hot path) are in
107 * the same cache line.
109 struct fib6_table
*rt6i_table
;
110 struct fib6_node __rcu
*rt6i_node
;
112 struct in6_addr rt6i_gateway
;
115 * siblings is a list of rt6_info that have the the same metric/weight,
116 * destination, but not the same gateway. nsiblings is just a cache
117 * to speed up lookup.
119 struct list_head rt6i_siblings
;
120 unsigned int rt6i_nsiblings
;
124 unsigned int rt6i_nh_flags
;
126 /* These are in a separate cache line. */
127 struct rt6key rt6i_dst ____cacheline_aligned_in_smp
;
129 struct rt6key rt6i_src
;
130 struct rt6key rt6i_prefsrc
;
132 struct list_head rt6i_uncached
;
133 struct uncached_list
*rt6i_uncached_list
;
135 struct inet6_dev
*rt6i_idev
;
136 struct rt6_info
* __percpu
*rt6i_pcpu
;
140 /* more non-fragment space at head required */
141 unsigned short rt6i_nfheader_len
;
145 static inline struct inet6_dev
*ip6_dst_idev(struct dst_entry
*dst
)
147 return ((struct rt6_info
*)dst
)->rt6i_idev
;
150 static inline void rt6_clean_expires(struct rt6_info
*rt
)
152 rt
->rt6i_flags
&= ~RTF_EXPIRES
;
156 static inline void rt6_set_expires(struct rt6_info
*rt
, unsigned long expires
)
158 rt
->dst
.expires
= expires
;
159 rt
->rt6i_flags
|= RTF_EXPIRES
;
162 static inline void rt6_update_expires(struct rt6_info
*rt0
, int timeout
)
166 for (rt
= rt0
; rt
&& !(rt
->rt6i_flags
& RTF_EXPIRES
);
167 rt
= (struct rt6_info
*)rt
->dst
.from
);
169 rt0
->dst
.expires
= rt
->dst
.expires
;
171 dst_set_expires(&rt0
->dst
, timeout
);
172 rt0
->rt6i_flags
|= RTF_EXPIRES
;
175 /* Function to safely get fn->sernum for passed in rt
176 * and store result in passed in cookie.
177 * Return true if we can get cookie safely
178 * Return false if not
180 static inline bool rt6_get_cookie_safe(const struct rt6_info
*rt
,
183 struct fib6_node
*fn
;
187 fn
= rcu_dereference(rt
->rt6i_node
);
190 *cookie
= fn
->fn_sernum
;
198 static inline u32
rt6_get_cookie(const struct rt6_info
*rt
)
202 if (rt
->rt6i_flags
& RTF_PCPU
||
203 (unlikely(!list_empty(&rt
->rt6i_uncached
)) && rt
->dst
.from
))
204 rt
= (struct rt6_info
*)(rt
->dst
.from
);
206 rt6_get_cookie_safe(rt
, &cookie
);
211 static inline void ip6_rt_put(struct rt6_info
*rt
)
213 /* dst_release() accepts a NULL parameter.
214 * We rely on dst being first structure in struct rt6_info
216 BUILD_BUG_ON(offsetof(struct rt6_info
, dst
) != 0);
217 dst_release(&rt
->dst
);
220 void rt6_free_pcpu(struct rt6_info
*non_pcpu_rt
);
222 static inline void rt6_hold(struct rt6_info
*rt
)
224 atomic_inc(&rt
->rt6i_ref
);
227 static inline void rt6_release(struct rt6_info
*rt
)
229 if (atomic_dec_and_test(&rt
->rt6i_ref
)) {
231 dst_dev_put(&rt
->dst
);
232 dst_release(&rt
->dst
);
236 enum fib6_walk_state
{
237 #ifdef CONFIG_IPV6_SUBTREES
248 struct fib6_node
*root
, *node
;
249 struct rt6_info
*leaf
;
250 enum fib6_walk_state state
;
254 int (*func
)(struct fib6_walker
*);
258 struct rt6_statistics
{
260 __u32 fib_route_nodes
;
261 __u32 fib_rt_alloc
; /* permanent routes */
262 __u32 fib_rt_entries
; /* rt entries in table */
263 __u32 fib_rt_cache
; /* cache routes */
264 __u32 fib_discarded_routes
;
267 #define RTN_TL_ROOT 0x0001
268 #define RTN_ROOT 0x0002 /* tree root node */
269 #define RTN_RTINFO 0x0004 /* node with valid routing info */
272 * priority levels (or metrics)
278 struct hlist_node tb6_hlist
;
281 struct fib6_node tb6_root
;
282 struct inet_peer_base tb6_peers
;
284 unsigned int fib_seq
;
285 #define RT6_TABLE_HAS_DFLT_ROUTER BIT(0)
288 #define RT6_TABLE_UNSPEC RT_TABLE_UNSPEC
289 #define RT6_TABLE_MAIN RT_TABLE_MAIN
290 #define RT6_TABLE_DFLT RT6_TABLE_MAIN
291 #define RT6_TABLE_INFO RT6_TABLE_MAIN
292 #define RT6_TABLE_PREFIX RT6_TABLE_MAIN
294 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
295 #define FIB6_TABLE_MIN 1
296 #define FIB6_TABLE_MAX RT_TABLE_MAX
297 #define RT6_TABLE_LOCAL RT_TABLE_LOCAL
299 #define FIB6_TABLE_MIN RT_TABLE_MAIN
300 #define FIB6_TABLE_MAX FIB6_TABLE_MIN
301 #define RT6_TABLE_LOCAL RT6_TABLE_MAIN
304 typedef struct rt6_info
*(*pol_lookup_t
)(struct net
*,
306 struct flowi6
*, int);
308 struct fib6_entry_notifier_info
{
309 struct fib_notifier_info info
; /* must be first */
317 struct fib6_table
*fib6_get_table(struct net
*net
, u32 id
);
318 struct fib6_table
*fib6_new_table(struct net
*net
, u32 id
);
319 struct dst_entry
*fib6_rule_lookup(struct net
*net
, struct flowi6
*fl6
,
320 int flags
, pol_lookup_t lookup
);
322 struct fib6_node
*fib6_lookup(struct fib6_node
*root
,
323 const struct in6_addr
*daddr
,
324 const struct in6_addr
*saddr
);
326 struct fib6_node
*fib6_locate(struct fib6_node
*root
,
327 const struct in6_addr
*daddr
, int dst_len
,
328 const struct in6_addr
*saddr
, int src_len
);
330 void fib6_clean_all(struct net
*net
, int (*func
)(struct rt6_info
*, void *arg
),
333 int fib6_add(struct fib6_node
*root
, struct rt6_info
*rt
,
334 struct nl_info
*info
, struct mx6_config
*mxc
,
335 struct netlink_ext_ack
*extack
);
336 int fib6_del(struct rt6_info
*rt
, struct nl_info
*info
);
338 void inet6_rt_notify(int event
, struct rt6_info
*rt
, struct nl_info
*info
,
341 void fib6_run_gc(unsigned long expires
, struct net
*net
, bool force
);
343 void fib6_gc_cleanup(void);
347 int ipv6_route_open(struct inode
*inode
, struct file
*file
);
349 int call_fib6_notifier(struct notifier_block
*nb
, struct net
*net
,
350 enum fib_event_type event_type
,
351 struct fib_notifier_info
*info
);
352 int call_fib6_notifiers(struct net
*net
, enum fib_event_type event_type
,
353 struct fib_notifier_info
*info
);
355 int __net_init
fib6_notifier_init(struct net
*net
);
356 void __net_exit
fib6_notifier_exit(struct net
*net
);
358 unsigned int fib6_tables_seq_read(struct net
*net
);
359 int fib6_tables_dump(struct net
*net
, struct notifier_block
*nb
);
361 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
362 int fib6_rules_init(void);
363 void fib6_rules_cleanup(void);
364 bool fib6_rule_default(const struct fib_rule
*rule
);
365 int fib6_rules_dump(struct net
*net
, struct notifier_block
*nb
);
366 unsigned int fib6_rules_seq_read(struct net
*net
);
368 static inline int fib6_rules_init(void)
372 static inline void fib6_rules_cleanup(void)
376 static inline bool fib6_rule_default(const struct fib_rule
*rule
)
380 static inline int fib6_rules_dump(struct net
*net
, struct notifier_block
*nb
)
384 static inline unsigned int fib6_rules_seq_read(struct net
*net
)