Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec...
[firefly-linux-kernel-4.4.55.git] / include / net / dst.h
1 /*
2  * net/dst.h    Protocol independent destination cache definitions.
3  *
4  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
5  *
6  */
7
8 #ifndef _NET_DST_H
9 #define _NET_DST_H
10
11 #include <net/dst_ops.h>
12 #include <linux/netdevice.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/rcupdate.h>
15 #include <linux/bug.h>
16 #include <linux/jiffies.h>
17 #include <net/neighbour.h>
18 #include <asm/processor.h>
19
20 #define DST_GC_MIN      (HZ/10)
21 #define DST_GC_INC      (HZ/2)
22 #define DST_GC_MAX      (120*HZ)
23
24 /* Each dst_entry has reference count and sits in some parent list(s).
25  * When it is removed from parent list, it is "freed" (dst_free).
26  * After this it enters dead state (dst->obsolete > 0) and if its refcnt
27  * is zero, it can be destroyed immediately, otherwise it is added
28  * to gc list and garbage collector periodically checks the refcnt.
29  */
30
31 struct sk_buff;
32
33 struct dst_entry {
34         struct rcu_head         rcu_head;
35         struct dst_entry        *child;
36         struct net_device       *dev;
37         struct  dst_ops         *ops;
38         unsigned long           _metrics;
39         unsigned long           expires;
40         struct dst_entry        *path;
41         struct dst_entry        *from;
42 #ifdef CONFIG_XFRM
43         struct xfrm_state       *xfrm;
44 #else
45         void                    *__pad1;
46 #endif
47         int                     (*input)(struct sk_buff *);
48         int                     (*output)(struct sk_buff *);
49
50         unsigned short          flags;
51 #define DST_HOST                0x0001
52 #define DST_NOXFRM              0x0002
53 #define DST_NOPOLICY            0x0004
54 #define DST_NOHASH              0x0008
55 #define DST_NOCACHE             0x0010
56 #define DST_NOCOUNT             0x0020
57 #define DST_FAKE_RTABLE         0x0040
58 #define DST_XFRM_TUNNEL         0x0080
59 #define DST_XFRM_QUEUE          0x0100
60
61         unsigned short          pending_confirm;
62
63         short                   error;
64
65         /* A non-zero value of dst->obsolete forces by-hand validation
66          * of the route entry.  Positive values are set by the generic
67          * dst layer to indicate that the entry has been forcefully
68          * destroyed.
69          *
70          * Negative values are used by the implementation layer code to
71          * force invocation of the dst_ops->check() method.
72          */
73         short                   obsolete;
74 #define DST_OBSOLETE_NONE       0
75 #define DST_OBSOLETE_DEAD       2
76 #define DST_OBSOLETE_FORCE_CHK  -1
77 #define DST_OBSOLETE_KILL       -2
78         unsigned short          header_len;     /* more space at head required */
79         unsigned short          trailer_len;    /* space to reserve at tail */
80 #ifdef CONFIG_IP_ROUTE_CLASSID
81         __u32                   tclassid;
82 #else
83         __u32                   __pad2;
84 #endif
85
86         /*
87          * Align __refcnt to a 64 bytes alignment
88          * (L1_CACHE_SIZE would be too much)
89          */
90 #ifdef CONFIG_64BIT
91         long                    __pad_to_align_refcnt[2];
92 #endif
93         /*
94          * __refcnt wants to be on a different cache line from
95          * input/output/ops or performance tanks badly
96          */
97         atomic_t                __refcnt;       /* client references    */
98         int                     __use;
99         unsigned long           lastuse;
100         union {
101                 struct dst_entry        *next;
102                 struct rtable __rcu     *rt_next;
103                 struct rt6_info         *rt6_next;
104                 struct dn_route __rcu   *dn_next;
105         };
106 };
107
108 u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
109 extern const u32 dst_default_metrics[];
110
111 #define DST_METRICS_READ_ONLY   0x1UL
112 #define __DST_METRICS_PTR(Y)    \
113         ((u32 *)((Y) & ~DST_METRICS_READ_ONLY))
114 #define DST_METRICS_PTR(X)      __DST_METRICS_PTR((X)->_metrics)
115
116 static inline bool dst_metrics_read_only(const struct dst_entry *dst)
117 {
118         return dst->_metrics & DST_METRICS_READ_ONLY;
119 }
120
121 void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
122
123 static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
124 {
125         unsigned long val = dst->_metrics;
126         if (!(val & DST_METRICS_READ_ONLY))
127                 __dst_destroy_metrics_generic(dst, val);
128 }
129
130 static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
131 {
132         unsigned long p = dst->_metrics;
133
134         BUG_ON(!p);
135
136         if (p & DST_METRICS_READ_ONLY)
137                 return dst->ops->cow_metrics(dst, p);
138         return __DST_METRICS_PTR(p);
139 }
140
141 /* This may only be invoked before the entry has reached global
142  * visibility.
143  */
144 static inline void dst_init_metrics(struct dst_entry *dst,
145                                     const u32 *src_metrics,
146                                     bool read_only)
147 {
148         dst->_metrics = ((unsigned long) src_metrics) |
149                 (read_only ? DST_METRICS_READ_ONLY : 0);
150 }
151
152 static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
153 {
154         u32 *dst_metrics = dst_metrics_write_ptr(dest);
155
156         if (dst_metrics) {
157                 u32 *src_metrics = DST_METRICS_PTR(src);
158
159                 memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
160         }
161 }
162
163 static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
164 {
165         return DST_METRICS_PTR(dst);
166 }
167
168 static inline u32
169 dst_metric_raw(const struct dst_entry *dst, const int metric)
170 {
171         u32 *p = DST_METRICS_PTR(dst);
172
173         return p[metric-1];
174 }
175
176 static inline u32
177 dst_metric(const struct dst_entry *dst, const int metric)
178 {
179         WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
180                      metric == RTAX_ADVMSS ||
181                      metric == RTAX_MTU);
182         return dst_metric_raw(dst, metric);
183 }
184
185 static inline u32
186 dst_metric_advmss(const struct dst_entry *dst)
187 {
188         u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
189
190         if (!advmss)
191                 advmss = dst->ops->default_advmss(dst);
192
193         return advmss;
194 }
195
196 static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
197 {
198         u32 *p = dst_metrics_write_ptr(dst);
199
200         if (p)
201                 p[metric-1] = val;
202 }
203
204 static inline u32
205 dst_feature(const struct dst_entry *dst, u32 feature)
206 {
207         return dst_metric(dst, RTAX_FEATURES) & feature;
208 }
209
210 static inline u32 dst_mtu(const struct dst_entry *dst)
211 {
212         return dst->ops->mtu(dst);
213 }
214
215 /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
216 static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
217 {
218         return msecs_to_jiffies(dst_metric(dst, metric));
219 }
220
221 static inline u32
222 dst_allfrag(const struct dst_entry *dst)
223 {
224         int ret = dst_feature(dst,  RTAX_FEATURE_ALLFRAG);
225         return ret;
226 }
227
228 static inline int
229 dst_metric_locked(const struct dst_entry *dst, int metric)
230 {
231         return dst_metric(dst, RTAX_LOCK) & (1<<metric);
232 }
233
234 static inline void dst_hold(struct dst_entry *dst)
235 {
236         /*
237          * If your kernel compilation stops here, please check
238          * __pad_to_align_refcnt declaration in struct dst_entry
239          */
240         BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
241         atomic_inc(&dst->__refcnt);
242 }
243
244 static inline void dst_use(struct dst_entry *dst, unsigned long time)
245 {
246         dst_hold(dst);
247         dst->__use++;
248         dst->lastuse = time;
249 }
250
251 static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
252 {
253         dst->__use++;
254         dst->lastuse = time;
255 }
256
257 static inline struct dst_entry *dst_clone(struct dst_entry *dst)
258 {
259         if (dst)
260                 atomic_inc(&dst->__refcnt);
261         return dst;
262 }
263
264 void dst_release(struct dst_entry *dst);
265
266 static inline void refdst_drop(unsigned long refdst)
267 {
268         if (!(refdst & SKB_DST_NOREF))
269                 dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
270 }
271
272 /**
273  * skb_dst_drop - drops skb dst
274  * @skb: buffer
275  *
276  * Drops dst reference count if a reference was taken.
277  */
278 static inline void skb_dst_drop(struct sk_buff *skb)
279 {
280         if (skb->_skb_refdst) {
281                 refdst_drop(skb->_skb_refdst);
282                 skb->_skb_refdst = 0UL;
283         }
284 }
285
286 static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
287 {
288         nskb->_skb_refdst = oskb->_skb_refdst;
289         if (!(nskb->_skb_refdst & SKB_DST_NOREF))
290                 dst_clone(skb_dst(nskb));
291 }
292
293 /**
294  * skb_dst_force - makes sure skb dst is refcounted
295  * @skb: buffer
296  *
297  * If dst is not yet refcounted, let's do it
298  */
299 static inline void skb_dst_force(struct sk_buff *skb)
300 {
301         if (skb_dst_is_noref(skb)) {
302                 WARN_ON(!rcu_read_lock_held());
303                 skb->_skb_refdst &= ~SKB_DST_NOREF;
304                 dst_clone(skb_dst(skb));
305         }
306 }
307
308
309 /**
310  *      __skb_tunnel_rx - prepare skb for rx reinsert
311  *      @skb: buffer
312  *      @dev: tunnel device
313  *      @net: netns for packet i/o
314  *
315  *      After decapsulation, packet is going to re-enter (netif_rx()) our stack,
316  *      so make some cleanups. (no accounting done)
317  */
318 static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
319                                    struct net *net)
320 {
321         skb->dev = dev;
322
323         /*
324          * Clear hash so that we can recalulate the hash for the
325          * encapsulated packet, unless we have already determine the hash
326          * over the L4 4-tuple.
327          */
328         skb_clear_hash_if_not_l4(skb);
329         skb_set_queue_mapping(skb, 0);
330         skb_scrub_packet(skb, !net_eq(net, dev_net(dev)));
331 }
332
333 /**
334  *      skb_tunnel_rx - prepare skb for rx reinsert
335  *      @skb: buffer
336  *      @dev: tunnel device
337  *
338  *      After decapsulation, packet is going to re-enter (netif_rx()) our stack,
339  *      so make some cleanups, and perform accounting.
340  *      Note: this accounting is not SMP safe.
341  */
342 static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
343                                  struct net *net)
344 {
345         /* TODO : stats should be SMP safe */
346         dev->stats.rx_packets++;
347         dev->stats.rx_bytes += skb->len;
348         __skb_tunnel_rx(skb, dev, net);
349 }
350
351 /* Children define the path of the packet through the
352  * Linux networking.  Thus, destinations are stackable.
353  */
354
355 static inline struct dst_entry *skb_dst_pop(struct sk_buff *skb)
356 {
357         struct dst_entry *child = dst_clone(skb_dst(skb)->child);
358
359         skb_dst_drop(skb);
360         return child;
361 }
362
363 int dst_discard(struct sk_buff *skb);
364 void *dst_alloc(struct dst_ops *ops, struct net_device *dev, int initial_ref,
365                 int initial_obsolete, unsigned short flags);
366 void __dst_free(struct dst_entry *dst);
367 struct dst_entry *dst_destroy(struct dst_entry *dst);
368
369 static inline void dst_free(struct dst_entry *dst)
370 {
371         if (dst->obsolete > 0)
372                 return;
373         if (!atomic_read(&dst->__refcnt)) {
374                 dst = dst_destroy(dst);
375                 if (!dst)
376                         return;
377         }
378         __dst_free(dst);
379 }
380
381 static inline void dst_rcu_free(struct rcu_head *head)
382 {
383         struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
384         dst_free(dst);
385 }
386
387 static inline void dst_confirm(struct dst_entry *dst)
388 {
389         dst->pending_confirm = 1;
390 }
391
392 static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
393                                    struct sk_buff *skb)
394 {
395         const struct hh_cache *hh;
396
397         if (dst->pending_confirm) {
398                 unsigned long now = jiffies;
399
400                 dst->pending_confirm = 0;
401                 /* avoid dirtying neighbour */
402                 if (n->confirmed != now)
403                         n->confirmed = now;
404         }
405
406         hh = &n->hh;
407         if ((n->nud_state & NUD_CONNECTED) && hh->hh_len)
408                 return neigh_hh_output(hh, skb);
409         else
410                 return n->output(n, skb);
411 }
412
413 static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
414 {
415         struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr);
416         return IS_ERR(n) ? NULL : n;
417 }
418
419 static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
420                                                      struct sk_buff *skb)
421 {
422         struct neighbour *n =  dst->ops->neigh_lookup(dst, skb, NULL);
423         return IS_ERR(n) ? NULL : n;
424 }
425
426 static inline void dst_link_failure(struct sk_buff *skb)
427 {
428         struct dst_entry *dst = skb_dst(skb);
429         if (dst && dst->ops && dst->ops->link_failure)
430                 dst->ops->link_failure(skb);
431 }
432
433 static inline void dst_set_expires(struct dst_entry *dst, int timeout)
434 {
435         unsigned long expires = jiffies + timeout;
436
437         if (expires == 0)
438                 expires = 1;
439
440         if (dst->expires == 0 || time_before(expires, dst->expires))
441                 dst->expires = expires;
442 }
443
444 /* Output packet to network from transport.  */
445 static inline int dst_output(struct sk_buff *skb)
446 {
447         return skb_dst(skb)->output(skb);
448 }
449
450 /* Input packet from network to transport.  */
451 static inline int dst_input(struct sk_buff *skb)
452 {
453         return skb_dst(skb)->input(skb);
454 }
455
456 static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
457 {
458         if (dst->obsolete)
459                 dst = dst->ops->check(dst, cookie);
460         return dst;
461 }
462
463 void dst_init(void);
464
465 /* Flags for xfrm_lookup flags argument. */
466 enum {
467         XFRM_LOOKUP_ICMP = 1 << 0,
468 };
469
470 struct flowi;
471 #ifndef CONFIG_XFRM
472 static inline struct dst_entry *xfrm_lookup(struct net *net,
473                                             struct dst_entry *dst_orig,
474                                             const struct flowi *fl, struct sock *sk,
475                                             int flags)
476 {
477         return dst_orig;
478
479
480 static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
481 {
482         return NULL;
483 }
484
485 #else
486 struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
487                               const struct flowi *fl, struct sock *sk,
488                               int flags);
489
490 /* skb attached with this dst needs transformation if dst->xfrm is valid */
491 static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
492 {
493         return dst->xfrm;
494 }
495 #endif
496
497 #endif /* _NET_DST_H */