GNU Linux-libre 4.4.285-gnu1
[releases.git] / include / net / ip6_fib.h
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 <net/dst.h>
20 #include <net/flow.h>
21 #include <net/netlink.h>
22 #include <net/inetpeer.h>
23
24 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
25 #define FIB6_TABLE_HASHSZ 256
26 #else
27 #define FIB6_TABLE_HASHSZ 1
28 #endif
29
30 struct rt6_info;
31
32 struct fib6_config {
33         u32             fc_table;
34         u32             fc_metric;
35         int             fc_dst_len;
36         int             fc_src_len;
37         int             fc_ifindex;
38         u32             fc_flags;
39         u32             fc_protocol;
40         u32             fc_type;        /* only 8 bits are used */
41
42         struct in6_addr fc_dst;
43         struct in6_addr fc_src;
44         struct in6_addr fc_prefsrc;
45         struct in6_addr fc_gateway;
46
47         unsigned long   fc_expires;
48         struct nlattr   *fc_mx;
49         int             fc_mx_len;
50         int             fc_mp_len;
51         struct nlattr   *fc_mp;
52
53         struct nl_info  fc_nlinfo;
54         struct nlattr   *fc_encap;
55         u16             fc_encap_type;
56 };
57
58 struct fib6_node {
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;
64 #endif
65         struct rt6_info         *leaf;
66
67         __u16                   fn_bit;         /* bit key */
68         __u16                   fn_flags;
69         int                     fn_sernum;
70         struct rt6_info         *rr_ptr;
71         struct rcu_head         rcu;
72 };
73
74 #ifndef CONFIG_IPV6_SUBTREES
75 #define FIB6_SUBTREE(fn)        NULL
76 #else
77 #define FIB6_SUBTREE(fn)        ((fn)->subtree)
78 #endif
79
80 struct mx6_config {
81         const u32 *mx;
82         DECLARE_BITMAP(mx_valid, RTAX_MAX);
83 };
84
85 /*
86  *      routing information
87  *
88  */
89
90 struct rt6key {
91         struct in6_addr addr;
92         int             plen;
93 };
94
95 struct fib6_table;
96
97 struct rt6_info {
98         struct dst_entry                dst;
99
100         /*
101          * Tail elements of dst_entry (__refcnt etc.)
102          * and these elements (rarely used in hot path) are in
103          * the same cache line.
104          */
105         struct fib6_table               *rt6i_table;
106         struct fib6_node __rcu          *rt6i_node;
107
108         struct in6_addr                 rt6i_gateway;
109
110         /* Multipath routes:
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.
114          */
115         struct list_head                rt6i_siblings;
116         unsigned int                    rt6i_nsiblings;
117
118         atomic_t                        rt6i_ref;
119
120         /* These are in a separate cache line. */
121         struct rt6key                   rt6i_dst ____cacheline_aligned_in_smp;
122         u32                             rt6i_flags;
123         struct rt6key                   rt6i_src;
124         struct rt6key                   rt6i_prefsrc;
125
126         struct list_head                rt6i_uncached;
127         struct uncached_list            *rt6i_uncached_list;
128
129         struct inet6_dev                *rt6i_idev;
130         struct rt6_info * __percpu      *rt6i_pcpu;
131
132         u32                             rt6i_metric;
133         u32                             rt6i_pmtu;
134         /* more non-fragment space at head required */
135         unsigned short                  rt6i_nfheader_len;
136         u8                              rt6i_protocol;
137 };
138
139 static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
140 {
141         return ((struct rt6_info *)dst)->rt6i_idev;
142 }
143
144 static inline void rt6_clean_expires(struct rt6_info *rt)
145 {
146         rt->rt6i_flags &= ~RTF_EXPIRES;
147         rt->dst.expires = 0;
148 }
149
150 static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires)
151 {
152         rt->dst.expires = expires;
153         rt->rt6i_flags |= RTF_EXPIRES;
154 }
155
156 static inline void rt6_update_expires(struct rt6_info *rt0, int timeout)
157 {
158         struct rt6_info *rt;
159
160         for (rt = rt0; rt && !(rt->rt6i_flags & RTF_EXPIRES);
161              rt = (struct rt6_info *)rt->dst.from);
162         if (rt && rt != rt0)
163                 rt0->dst.expires = rt->dst.expires;
164
165         dst_set_expires(&rt0->dst, timeout);
166         rt0->rt6i_flags |= RTF_EXPIRES;
167 }
168
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
173  */
174 static inline bool rt6_get_cookie_safe(const struct rt6_info *rt,
175                                        u32 *cookie)
176 {
177         struct fib6_node *fn;
178         bool status = false;
179
180         rcu_read_lock();
181         fn = rcu_dereference(rt->rt6i_node);
182
183         if (fn) {
184                 *cookie = fn->fn_sernum;
185                 status = true;
186         }
187
188         rcu_read_unlock();
189         return status;
190 }
191
192 static inline u32 rt6_get_cookie(const struct rt6_info *rt)
193 {
194         u32 cookie = 0;
195
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);
199
200         rt6_get_cookie_safe(rt, &cookie);
201
202         return cookie;
203 }
204
205 static inline void ip6_rt_put(struct rt6_info *rt)
206 {
207         /* dst_release() accepts a NULL parameter.
208          * We rely on dst being first structure in struct rt6_info
209          */
210         BUILD_BUG_ON(offsetof(struct rt6_info, dst) != 0);
211         dst_release(&rt->dst);
212 }
213
214 enum fib6_walk_state {
215 #ifdef CONFIG_IPV6_SUBTREES
216         FWS_S,
217 #endif
218         FWS_L,
219         FWS_R,
220         FWS_C,
221         FWS_U
222 };
223
224 struct fib6_walker {
225         struct list_head lh;
226         struct fib6_node *root, *node;
227         struct rt6_info *leaf;
228         enum fib6_walk_state state;
229         bool prune;
230         unsigned int skip;
231         unsigned int count;
232         int (*func)(struct fib6_walker *);
233         void *args;
234 };
235
236 struct rt6_statistics {
237         __u32           fib_nodes;
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;
243 };
244
245 #define RTN_TL_ROOT     0x0001
246 #define RTN_ROOT        0x0002          /* tree root node               */
247 #define RTN_RTINFO      0x0004          /* node with valid routing info */
248
249 /*
250  *      priority levels (or metrics)
251  *
252  */
253
254
255 struct fib6_table {
256         struct hlist_node       tb6_hlist;
257         u32                     tb6_id;
258         rwlock_t                tb6_lock;
259         struct fib6_node        tb6_root;
260         struct inet_peer_base   tb6_peers;
261         unsigned int            flags;
262 #define RT6_TABLE_HAS_DFLT_ROUTER       BIT(0)
263 };
264
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
270
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
275 #else
276 #define FIB6_TABLE_MIN          RT_TABLE_MAIN
277 #define FIB6_TABLE_MAX          FIB6_TABLE_MIN
278 #define RT6_TABLE_LOCAL         RT6_TABLE_MAIN
279 #endif
280
281 typedef struct rt6_info *(*pol_lookup_t)(struct net *,
282                                          struct fib6_table *,
283                                          struct flowi6 *, int);
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 int fib6_del(struct rt6_info *rt, struct nl_info *info);
308
309 void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
310                      unsigned int flags);
311
312 void fib6_run_gc(unsigned long expires, struct net *net, bool force);
313
314 void fib6_gc_cleanup(void);
315
316 int fib6_init(void);
317
318 int ipv6_route_open(struct inode *inode, struct file *file);
319
320 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
321 int fib6_rules_init(void);
322 void fib6_rules_cleanup(void);
323 #else
324 static inline int               fib6_rules_init(void)
325 {
326         return 0;
327 }
328 static inline void              fib6_rules_cleanup(void)
329 {
330         return ;
331 }
332 #endif
333 #endif