6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * Kazunori MIYAZAWA @USAGI
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/kmod.h>
19 #include <linux/list.h>
20 #include <linux/spinlock.h>
21 #include <linux/workqueue.h>
22 #include <linux/notifier.h>
23 #include <linux/netdevice.h>
24 #include <linux/netfilter.h>
25 #include <linux/module.h>
26 #include <linux/cache.h>
27 #include <linux/audit.h>
32 #ifdef CONFIG_XFRM_STATISTICS
36 #include "xfrm_hash.h"
38 #define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
39 #define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
40 #define XFRM_MAX_QUEUE_LEN 100
43 struct dst_entry
*dst_orig
;
47 static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock
);
48 static struct xfrm_policy_afinfo __rcu
*xfrm_policy_afinfo
[NPROTO
]
51 static struct kmem_cache
*xfrm_dst_cache __read_mostly
;
52 static __read_mostly seqcount_t xfrm_policy_hash_generation
;
54 static void xfrm_init_pmtu(struct dst_entry
*dst
);
55 static int stale_bundle(struct dst_entry
*dst
);
56 static int xfrm_bundle_ok(struct xfrm_dst
*xdst
);
57 static void xfrm_policy_queue_process(unsigned long arg
);
59 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
);
60 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
63 static inline bool xfrm_pol_hold_rcu(struct xfrm_policy
*policy
)
65 return atomic_inc_not_zero(&policy
->refcnt
);
69 __xfrm4_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
71 const struct flowi4
*fl4
= &fl
->u
.ip4
;
73 return addr4_match(fl4
->daddr
, sel
->daddr
.a4
, sel
->prefixlen_d
) &&
74 addr4_match(fl4
->saddr
, sel
->saddr
.a4
, sel
->prefixlen_s
) &&
75 !((xfrm_flowi_dport(fl
, &fl4
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
76 !((xfrm_flowi_sport(fl
, &fl4
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
77 (fl4
->flowi4_proto
== sel
->proto
|| !sel
->proto
) &&
78 (fl4
->flowi4_oif
== sel
->ifindex
|| !sel
->ifindex
);
82 __xfrm6_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
84 const struct flowi6
*fl6
= &fl
->u
.ip6
;
86 return addr_match(&fl6
->daddr
, &sel
->daddr
, sel
->prefixlen_d
) &&
87 addr_match(&fl6
->saddr
, &sel
->saddr
, sel
->prefixlen_s
) &&
88 !((xfrm_flowi_dport(fl
, &fl6
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
89 !((xfrm_flowi_sport(fl
, &fl6
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
90 (fl6
->flowi6_proto
== sel
->proto
|| !sel
->proto
) &&
91 (fl6
->flowi6_oif
== sel
->ifindex
|| !sel
->ifindex
);
94 bool xfrm_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
,
95 unsigned short family
)
99 return __xfrm4_selector_match(sel
, fl
);
101 return __xfrm6_selector_match(sel
, fl
);
106 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
108 struct xfrm_policy_afinfo
*afinfo
;
110 if (unlikely(family
>= NPROTO
))
113 afinfo
= rcu_dereference(xfrm_policy_afinfo
[family
]);
114 if (unlikely(!afinfo
))
119 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
)
124 static inline struct dst_entry
*__xfrm_dst_lookup(struct net
*net
,
126 const xfrm_address_t
*saddr
,
127 const xfrm_address_t
*daddr
,
130 struct xfrm_policy_afinfo
*afinfo
;
131 struct dst_entry
*dst
;
133 afinfo
= xfrm_policy_get_afinfo(family
);
134 if (unlikely(afinfo
== NULL
))
135 return ERR_PTR(-EAFNOSUPPORT
);
137 dst
= afinfo
->dst_lookup(net
, tos
, oif
, saddr
, daddr
);
139 xfrm_policy_put_afinfo(afinfo
);
144 static inline struct dst_entry
*xfrm_dst_lookup(struct xfrm_state
*x
,
146 xfrm_address_t
*prev_saddr
,
147 xfrm_address_t
*prev_daddr
,
150 struct net
*net
= xs_net(x
);
151 xfrm_address_t
*saddr
= &x
->props
.saddr
;
152 xfrm_address_t
*daddr
= &x
->id
.daddr
;
153 struct dst_entry
*dst
;
155 if (x
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
) {
159 if (x
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
) {
164 dst
= __xfrm_dst_lookup(net
, tos
, oif
, saddr
, daddr
, family
);
167 if (prev_saddr
!= saddr
)
168 memcpy(prev_saddr
, saddr
, sizeof(*prev_saddr
));
169 if (prev_daddr
!= daddr
)
170 memcpy(prev_daddr
, daddr
, sizeof(*prev_daddr
));
176 static inline unsigned long make_jiffies(long secs
)
178 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
179 return MAX_SCHEDULE_TIMEOUT
-1;
184 static void xfrm_policy_timer(unsigned long data
)
186 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
187 unsigned long now
= get_seconds();
188 long next
= LONG_MAX
;
192 read_lock(&xp
->lock
);
194 if (unlikely(xp
->walk
.dead
))
197 dir
= xfrm_policy_id2dir(xp
->index
);
199 if (xp
->lft
.hard_add_expires_seconds
) {
200 long tmo
= xp
->lft
.hard_add_expires_seconds
+
201 xp
->curlft
.add_time
- now
;
207 if (xp
->lft
.hard_use_expires_seconds
) {
208 long tmo
= xp
->lft
.hard_use_expires_seconds
+
209 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
215 if (xp
->lft
.soft_add_expires_seconds
) {
216 long tmo
= xp
->lft
.soft_add_expires_seconds
+
217 xp
->curlft
.add_time
- now
;
220 tmo
= XFRM_KM_TIMEOUT
;
225 if (xp
->lft
.soft_use_expires_seconds
) {
226 long tmo
= xp
->lft
.soft_use_expires_seconds
+
227 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
230 tmo
= XFRM_KM_TIMEOUT
;
237 km_policy_expired(xp
, dir
, 0, 0);
238 if (next
!= LONG_MAX
&&
239 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
243 read_unlock(&xp
->lock
);
248 read_unlock(&xp
->lock
);
249 if (!xfrm_policy_delete(xp
, dir
))
250 km_policy_expired(xp
, dir
, 1, 0);
254 static struct flow_cache_object
*xfrm_policy_flo_get(struct flow_cache_object
*flo
)
256 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
258 if (unlikely(pol
->walk
.dead
))
266 static int xfrm_policy_flo_check(struct flow_cache_object
*flo
)
268 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
270 return !pol
->walk
.dead
;
273 static void xfrm_policy_flo_delete(struct flow_cache_object
*flo
)
275 xfrm_pol_put(container_of(flo
, struct xfrm_policy
, flo
));
278 static const struct flow_cache_ops xfrm_policy_fc_ops
= {
279 .get
= xfrm_policy_flo_get
,
280 .check
= xfrm_policy_flo_check
,
281 .delete = xfrm_policy_flo_delete
,
284 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
288 struct xfrm_policy
*xfrm_policy_alloc(struct net
*net
, gfp_t gfp
)
290 struct xfrm_policy
*policy
;
292 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
295 write_pnet(&policy
->xp_net
, net
);
296 INIT_LIST_HEAD(&policy
->walk
.all
);
297 INIT_HLIST_NODE(&policy
->bydst
);
298 INIT_HLIST_NODE(&policy
->byidx
);
299 rwlock_init(&policy
->lock
);
300 atomic_set(&policy
->refcnt
, 1);
301 skb_queue_head_init(&policy
->polq
.hold_queue
);
302 setup_timer(&policy
->timer
, xfrm_policy_timer
,
303 (unsigned long)policy
);
304 setup_timer(&policy
->polq
.hold_timer
, xfrm_policy_queue_process
,
305 (unsigned long)policy
);
306 policy
->flo
.ops
= &xfrm_policy_fc_ops
;
310 EXPORT_SYMBOL(xfrm_policy_alloc
);
312 static void xfrm_policy_destroy_rcu(struct rcu_head
*head
)
314 struct xfrm_policy
*policy
= container_of(head
, struct xfrm_policy
, rcu
);
316 security_xfrm_policy_free(policy
->security
);
320 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
322 void xfrm_policy_destroy(struct xfrm_policy
*policy
)
324 BUG_ON(!policy
->walk
.dead
);
326 if (del_timer(&policy
->timer
) || del_timer(&policy
->polq
.hold_timer
))
329 call_rcu(&policy
->rcu
, xfrm_policy_destroy_rcu
);
331 EXPORT_SYMBOL(xfrm_policy_destroy
);
333 /* Rule must be locked. Release descendant resources, announce
334 * entry dead. The rule must be unlinked from lists to the moment.
337 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
339 policy
->walk
.dead
= 1;
341 atomic_inc(&policy
->genid
);
343 if (del_timer(&policy
->polq
.hold_timer
))
344 xfrm_pol_put(policy
);
345 skb_queue_purge(&policy
->polq
.hold_queue
);
347 if (del_timer(&policy
->timer
))
348 xfrm_pol_put(policy
);
350 xfrm_pol_put(policy
);
353 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
355 static inline unsigned int idx_hash(struct net
*net
, u32 index
)
357 return __idx_hash(index
, net
->xfrm
.policy_idx_hmask
);
360 /* calculate policy hash thresholds */
361 static void __get_hash_thresh(struct net
*net
,
362 unsigned short family
, int dir
,
363 u8
*dbits
, u8
*sbits
)
367 *dbits
= net
->xfrm
.policy_bydst
[dir
].dbits4
;
368 *sbits
= net
->xfrm
.policy_bydst
[dir
].sbits4
;
372 *dbits
= net
->xfrm
.policy_bydst
[dir
].dbits6
;
373 *sbits
= net
->xfrm
.policy_bydst
[dir
].sbits6
;
382 static struct hlist_head
*policy_hash_bysel(struct net
*net
,
383 const struct xfrm_selector
*sel
,
384 unsigned short family
, int dir
)
386 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
391 __get_hash_thresh(net
, family
, dir
, &dbits
, &sbits
);
392 hash
= __sel_hash(sel
, family
, hmask
, dbits
, sbits
);
394 if (hash
== hmask
+ 1)
395 return &net
->xfrm
.policy_inexact
[dir
];
397 return rcu_dereference_check(net
->xfrm
.policy_bydst
[dir
].table
,
398 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
)) + hash
;
401 static struct hlist_head
*policy_hash_direct(struct net
*net
,
402 const xfrm_address_t
*daddr
,
403 const xfrm_address_t
*saddr
,
404 unsigned short family
, int dir
)
406 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
411 __get_hash_thresh(net
, family
, dir
, &dbits
, &sbits
);
412 hash
= __addr_hash(daddr
, saddr
, family
, hmask
, dbits
, sbits
);
414 return rcu_dereference_check(net
->xfrm
.policy_bydst
[dir
].table
,
415 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
)) + hash
;
418 static void xfrm_dst_hash_transfer(struct net
*net
,
419 struct hlist_head
*list
,
420 struct hlist_head
*ndsttable
,
421 unsigned int nhashmask
,
424 struct hlist_node
*tmp
, *entry0
= NULL
;
425 struct xfrm_policy
*pol
;
431 hlist_for_each_entry_safe(pol
, tmp
, list
, bydst
) {
434 __get_hash_thresh(net
, pol
->family
, dir
, &dbits
, &sbits
);
435 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
436 pol
->family
, nhashmask
, dbits
, sbits
);
438 hlist_del_rcu(&pol
->bydst
);
439 hlist_add_head_rcu(&pol
->bydst
, ndsttable
+ h
);
444 hlist_del_rcu(&pol
->bydst
);
445 hlist_add_behind_rcu(&pol
->bydst
, entry0
);
447 entry0
= &pol
->bydst
;
449 if (!hlist_empty(list
)) {
455 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
456 struct hlist_head
*nidxtable
,
457 unsigned int nhashmask
)
459 struct hlist_node
*tmp
;
460 struct xfrm_policy
*pol
;
462 hlist_for_each_entry_safe(pol
, tmp
, list
, byidx
) {
465 h
= __idx_hash(pol
->index
, nhashmask
);
466 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
470 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
472 return ((old_hmask
+ 1) << 1) - 1;
475 static void xfrm_bydst_resize(struct net
*net
, int dir
)
477 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
478 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
479 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
480 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
481 struct hlist_head
*odst
;
487 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
488 write_seqcount_begin(&xfrm_policy_hash_generation
);
490 odst
= rcu_dereference_protected(net
->xfrm
.policy_bydst
[dir
].table
,
491 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
493 odst
= rcu_dereference_protected(net
->xfrm
.policy_bydst
[dir
].table
,
494 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
496 for (i
= hmask
; i
>= 0; i
--)
497 xfrm_dst_hash_transfer(net
, odst
+ i
, ndst
, nhashmask
, dir
);
499 rcu_assign_pointer(net
->xfrm
.policy_bydst
[dir
].table
, ndst
);
500 net
->xfrm
.policy_bydst
[dir
].hmask
= nhashmask
;
502 write_seqcount_end(&xfrm_policy_hash_generation
);
503 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
507 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
510 static void xfrm_byidx_resize(struct net
*net
, int total
)
512 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
513 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
514 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
515 struct hlist_head
*oidx
= net
->xfrm
.policy_byidx
;
516 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
522 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
524 for (i
= hmask
; i
>= 0; i
--)
525 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
527 net
->xfrm
.policy_byidx
= nidx
;
528 net
->xfrm
.policy_idx_hmask
= nhashmask
;
530 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
532 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
535 static inline int xfrm_bydst_should_resize(struct net
*net
, int dir
, int *total
)
537 unsigned int cnt
= net
->xfrm
.policy_count
[dir
];
538 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
543 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
550 static inline int xfrm_byidx_should_resize(struct net
*net
, int total
)
552 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
554 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
561 void xfrm_spd_getinfo(struct net
*net
, struct xfrmk_spdinfo
*si
)
563 si
->incnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
];
564 si
->outcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
];
565 si
->fwdcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
];
566 si
->inscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
567 si
->outscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
568 si
->fwdscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
569 si
->spdhcnt
= net
->xfrm
.policy_idx_hmask
;
570 si
->spdhmcnt
= xfrm_policy_hashmax
;
572 EXPORT_SYMBOL(xfrm_spd_getinfo
);
574 static DEFINE_MUTEX(hash_resize_mutex
);
575 static void xfrm_hash_resize(struct work_struct
*work
)
577 struct net
*net
= container_of(work
, struct net
, xfrm
.policy_hash_work
);
580 mutex_lock(&hash_resize_mutex
);
583 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
584 if (xfrm_bydst_should_resize(net
, dir
, &total
))
585 xfrm_bydst_resize(net
, dir
);
587 if (xfrm_byidx_should_resize(net
, total
))
588 xfrm_byidx_resize(net
, total
);
590 mutex_unlock(&hash_resize_mutex
);
593 static void xfrm_hash_rebuild(struct work_struct
*work
)
595 struct net
*net
= container_of(work
, struct net
,
596 xfrm
.policy_hthresh
.work
);
598 struct xfrm_policy
*pol
;
599 struct xfrm_policy
*policy
;
600 struct hlist_head
*chain
;
601 struct hlist_head
*odst
;
602 struct hlist_node
*newpos
;
606 u8 lbits4
, rbits4
, lbits6
, rbits6
;
608 mutex_lock(&hash_resize_mutex
);
610 /* read selector prefixlen thresholds */
612 seq
= read_seqbegin(&net
->xfrm
.policy_hthresh
.lock
);
614 lbits4
= net
->xfrm
.policy_hthresh
.lbits4
;
615 rbits4
= net
->xfrm
.policy_hthresh
.rbits4
;
616 lbits6
= net
->xfrm
.policy_hthresh
.lbits6
;
617 rbits6
= net
->xfrm
.policy_hthresh
.rbits6
;
618 } while (read_seqretry(&net
->xfrm
.policy_hthresh
.lock
, seq
));
620 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
622 /* reset the bydst and inexact table in all directions */
623 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
624 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
625 hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
626 odst
= net
->xfrm
.policy_bydst
[dir
].table
;
627 for (i
= hmask
; i
>= 0; i
--)
628 INIT_HLIST_HEAD(odst
+ i
);
629 if ((dir
& XFRM_POLICY_MASK
) == XFRM_POLICY_OUT
) {
630 /* dir out => dst = remote, src = local */
631 net
->xfrm
.policy_bydst
[dir
].dbits4
= rbits4
;
632 net
->xfrm
.policy_bydst
[dir
].sbits4
= lbits4
;
633 net
->xfrm
.policy_bydst
[dir
].dbits6
= rbits6
;
634 net
->xfrm
.policy_bydst
[dir
].sbits6
= lbits6
;
636 /* dir in/fwd => dst = local, src = remote */
637 net
->xfrm
.policy_bydst
[dir
].dbits4
= lbits4
;
638 net
->xfrm
.policy_bydst
[dir
].sbits4
= rbits4
;
639 net
->xfrm
.policy_bydst
[dir
].dbits6
= lbits6
;
640 net
->xfrm
.policy_bydst
[dir
].sbits6
= rbits6
;
644 /* re-insert all policies by order of creation */
645 list_for_each_entry_reverse(policy
, &net
->xfrm
.policy_all
, walk
.all
) {
646 if (xfrm_policy_id2dir(policy
->index
) >= XFRM_POLICY_MAX
) {
647 /* skip socket policies */
651 chain
= policy_hash_bysel(net
, &policy
->selector
,
653 xfrm_policy_id2dir(policy
->index
));
654 hlist_for_each_entry(pol
, chain
, bydst
) {
655 if (policy
->priority
>= pol
->priority
)
656 newpos
= &pol
->bydst
;
661 hlist_add_behind(&policy
->bydst
, newpos
);
663 hlist_add_head(&policy
->bydst
, chain
);
666 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
668 mutex_unlock(&hash_resize_mutex
);
671 void xfrm_policy_hash_rebuild(struct net
*net
)
673 schedule_work(&net
->xfrm
.policy_hthresh
.work
);
675 EXPORT_SYMBOL(xfrm_policy_hash_rebuild
);
677 /* Generate new index... KAME seems to generate them ordered by cost
678 * of an absolute inpredictability of ordering of rules. This will not pass. */
679 static u32
xfrm_gen_index(struct net
*net
, int dir
, u32 index
)
681 static u32 idx_generator
;
684 struct hlist_head
*list
;
685 struct xfrm_policy
*p
;
690 idx
= (idx_generator
| dir
);
699 list
= net
->xfrm
.policy_byidx
+ idx_hash(net
, idx
);
701 hlist_for_each_entry(p
, list
, byidx
) {
702 if (p
->index
== idx
) {
712 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
714 u32
*p1
= (u32
*) s1
;
715 u32
*p2
= (u32
*) s2
;
716 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
719 for (i
= 0; i
< len
; i
++) {
727 static void xfrm_policy_requeue(struct xfrm_policy
*old
,
728 struct xfrm_policy
*new)
730 struct xfrm_policy_queue
*pq
= &old
->polq
;
731 struct sk_buff_head list
;
733 if (skb_queue_empty(&pq
->hold_queue
))
736 __skb_queue_head_init(&list
);
738 spin_lock_bh(&pq
->hold_queue
.lock
);
739 skb_queue_splice_init(&pq
->hold_queue
, &list
);
740 if (del_timer(&pq
->hold_timer
))
742 spin_unlock_bh(&pq
->hold_queue
.lock
);
746 spin_lock_bh(&pq
->hold_queue
.lock
);
747 skb_queue_splice(&list
, &pq
->hold_queue
);
748 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
749 if (!mod_timer(&pq
->hold_timer
, jiffies
))
751 spin_unlock_bh(&pq
->hold_queue
.lock
);
754 static bool xfrm_policy_mark_match(struct xfrm_policy
*policy
,
755 struct xfrm_policy
*pol
)
757 u32 mark
= policy
->mark
.v
& policy
->mark
.m
;
759 if (policy
->mark
.v
== pol
->mark
.v
&& policy
->mark
.m
== pol
->mark
.m
)
762 if ((mark
& pol
->mark
.m
) == pol
->mark
.v
&&
763 policy
->priority
== pol
->priority
)
769 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
771 struct net
*net
= xp_net(policy
);
772 struct xfrm_policy
*pol
;
773 struct xfrm_policy
*delpol
;
774 struct hlist_head
*chain
;
775 struct hlist_node
*newpos
;
777 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
778 chain
= policy_hash_bysel(net
, &policy
->selector
, policy
->family
, dir
);
781 hlist_for_each_entry(pol
, chain
, bydst
) {
782 if (pol
->type
== policy
->type
&&
783 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
784 xfrm_policy_mark_match(policy
, pol
) &&
785 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
788 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
792 if (policy
->priority
> pol
->priority
)
794 } else if (policy
->priority
>= pol
->priority
) {
795 newpos
= &pol
->bydst
;
802 hlist_add_behind(&policy
->bydst
, newpos
);
804 hlist_add_head(&policy
->bydst
, chain
);
805 __xfrm_policy_link(policy
, dir
);
806 atomic_inc(&net
->xfrm
.flow_cache_genid
);
808 /* After previous checking, family can either be AF_INET or AF_INET6 */
809 if (policy
->family
== AF_INET
)
810 rt_genid_bump_ipv4(net
);
812 rt_genid_bump_ipv6(net
);
815 xfrm_policy_requeue(delpol
, policy
);
816 __xfrm_policy_unlink(delpol
, dir
);
818 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(net
, dir
, policy
->index
);
819 hlist_add_head(&policy
->byidx
, net
->xfrm
.policy_byidx
+idx_hash(net
, policy
->index
));
820 policy
->curlft
.add_time
= get_seconds();
821 policy
->curlft
.use_time
= 0;
822 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
823 xfrm_pol_hold(policy
);
824 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
827 xfrm_policy_kill(delpol
);
828 else if (xfrm_bydst_should_resize(net
, dir
, NULL
))
829 schedule_work(&net
->xfrm
.policy_hash_work
);
833 EXPORT_SYMBOL(xfrm_policy_insert
);
835 struct xfrm_policy
*xfrm_policy_bysel_ctx(struct net
*net
, u32 mark
, u8 type
,
836 int dir
, struct xfrm_selector
*sel
,
837 struct xfrm_sec_ctx
*ctx
, int delete,
840 struct xfrm_policy
*pol
, *ret
;
841 struct hlist_head
*chain
;
844 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
845 chain
= policy_hash_bysel(net
, sel
, sel
->family
, dir
);
847 hlist_for_each_entry(pol
, chain
, bydst
) {
848 if (pol
->type
== type
&&
849 (mark
& pol
->mark
.m
) == pol
->mark
.v
&&
850 !selector_cmp(sel
, &pol
->selector
) &&
851 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
854 *err
= security_xfrm_policy_delete(
857 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
860 __xfrm_policy_unlink(pol
, dir
);
866 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
869 xfrm_policy_kill(ret
);
872 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
874 struct xfrm_policy
*xfrm_policy_byid(struct net
*net
, u32 mark
, u8 type
,
875 int dir
, u32 id
, int delete, int *err
)
877 struct xfrm_policy
*pol
, *ret
;
878 struct hlist_head
*chain
;
881 if (xfrm_policy_id2dir(id
) != dir
)
885 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
886 chain
= net
->xfrm
.policy_byidx
+ idx_hash(net
, id
);
888 hlist_for_each_entry(pol
, chain
, byidx
) {
889 if (pol
->type
== type
&& pol
->index
== id
&&
890 (mark
& pol
->mark
.m
) == pol
->mark
.v
) {
893 *err
= security_xfrm_policy_delete(
896 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
899 __xfrm_policy_unlink(pol
, dir
);
905 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
908 xfrm_policy_kill(ret
);
911 EXPORT_SYMBOL(xfrm_policy_byid
);
913 #ifdef CONFIG_SECURITY_NETWORK_XFRM
915 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
919 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
920 struct xfrm_policy
*pol
;
923 hlist_for_each_entry(pol
,
924 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
925 if (pol
->type
!= type
)
927 err
= security_xfrm_policy_delete(pol
->security
);
929 xfrm_audit_policy_delete(pol
, 0, task_valid
);
933 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
934 hlist_for_each_entry(pol
,
935 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
937 if (pol
->type
!= type
)
939 err
= security_xfrm_policy_delete(
942 xfrm_audit_policy_delete(pol
, 0,
953 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
959 int xfrm_policy_flush(struct net
*net
, u8 type
, bool task_valid
)
961 int dir
, err
= 0, cnt
= 0;
963 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
965 err
= xfrm_policy_flush_secctx_check(net
, type
, task_valid
);
969 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
970 struct xfrm_policy
*pol
;
974 hlist_for_each_entry(pol
,
975 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
976 if (pol
->type
!= type
)
978 __xfrm_policy_unlink(pol
, dir
);
979 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
982 xfrm_audit_policy_delete(pol
, 1, task_valid
);
984 xfrm_policy_kill(pol
);
986 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
990 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
992 hlist_for_each_entry(pol
,
993 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
995 if (pol
->type
!= type
)
997 __xfrm_policy_unlink(pol
, dir
);
998 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1001 xfrm_audit_policy_delete(pol
, 1, task_valid
);
1002 xfrm_policy_kill(pol
);
1004 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1013 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1016 EXPORT_SYMBOL(xfrm_policy_flush
);
1018 int xfrm_policy_walk(struct net
*net
, struct xfrm_policy_walk
*walk
,
1019 int (*func
)(struct xfrm_policy
*, int, int, void*),
1022 struct xfrm_policy
*pol
;
1023 struct xfrm_policy_walk_entry
*x
;
1026 if (walk
->type
>= XFRM_POLICY_TYPE_MAX
&&
1027 walk
->type
!= XFRM_POLICY_TYPE_ANY
)
1030 if (list_empty(&walk
->walk
.all
) && walk
->seq
!= 0)
1033 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1034 if (list_empty(&walk
->walk
.all
))
1035 x
= list_first_entry(&net
->xfrm
.policy_all
, struct xfrm_policy_walk_entry
, all
);
1037 x
= list_first_entry(&walk
->walk
.all
,
1038 struct xfrm_policy_walk_entry
, all
);
1040 list_for_each_entry_from(x
, &net
->xfrm
.policy_all
, all
) {
1043 pol
= container_of(x
, struct xfrm_policy
, walk
);
1044 if (walk
->type
!= XFRM_POLICY_TYPE_ANY
&&
1045 walk
->type
!= pol
->type
)
1047 error
= func(pol
, xfrm_policy_id2dir(pol
->index
),
1050 list_move_tail(&walk
->walk
.all
, &x
->all
);
1055 if (walk
->seq
== 0) {
1059 list_del_init(&walk
->walk
.all
);
1061 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1064 EXPORT_SYMBOL(xfrm_policy_walk
);
1066 void xfrm_policy_walk_init(struct xfrm_policy_walk
*walk
, u8 type
)
1068 INIT_LIST_HEAD(&walk
->walk
.all
);
1069 walk
->walk
.dead
= 1;
1073 EXPORT_SYMBOL(xfrm_policy_walk_init
);
1075 void xfrm_policy_walk_done(struct xfrm_policy_walk
*walk
, struct net
*net
)
1077 if (list_empty(&walk
->walk
.all
))
1080 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
); /*FIXME where is net? */
1081 list_del(&walk
->walk
.all
);
1082 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1084 EXPORT_SYMBOL(xfrm_policy_walk_done
);
1087 * Find policy to apply to this flow.
1089 * Returns 0 if policy found, else an -errno.
1091 static int xfrm_policy_match(const struct xfrm_policy
*pol
,
1092 const struct flowi
*fl
,
1093 u8 type
, u16 family
, int dir
)
1095 const struct xfrm_selector
*sel
= &pol
->selector
;
1099 if (pol
->family
!= family
||
1100 (fl
->flowi_mark
& pol
->mark
.m
) != pol
->mark
.v
||
1104 match
= xfrm_selector_match(sel
, fl
, family
);
1106 ret
= security_xfrm_policy_lookup(pol
->security
, fl
->flowi_secid
,
1112 static struct xfrm_policy
*xfrm_policy_lookup_bytype(struct net
*net
, u8 type
,
1113 const struct flowi
*fl
,
1117 struct xfrm_policy
*pol
, *ret
;
1118 const xfrm_address_t
*daddr
, *saddr
;
1119 struct hlist_head
*chain
;
1120 unsigned int sequence
;
1123 daddr
= xfrm_flowi_daddr(fl
, family
);
1124 saddr
= xfrm_flowi_saddr(fl
, family
);
1125 if (unlikely(!daddr
|| !saddr
))
1131 sequence
= read_seqcount_begin(&xfrm_policy_hash_generation
);
1132 chain
= policy_hash_direct(net
, daddr
, saddr
, family
, dir
);
1133 } while (read_seqcount_retry(&xfrm_policy_hash_generation
, sequence
));
1137 hlist_for_each_entry_rcu(pol
, chain
, bydst
) {
1138 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1148 priority
= ret
->priority
;
1152 chain
= &net
->xfrm
.policy_inexact
[dir
];
1153 hlist_for_each_entry_rcu(pol
, chain
, bydst
) {
1154 if ((pol
->priority
>= priority
) && ret
)
1157 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1171 if (read_seqcount_retry(&xfrm_policy_hash_generation
, sequence
))
1174 if (ret
&& !xfrm_pol_hold_rcu(ret
))
1182 static struct xfrm_policy
*
1183 __xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
)
1185 #ifdef CONFIG_XFRM_SUB_POLICY
1186 struct xfrm_policy
*pol
;
1188 pol
= xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
1192 return xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
1195 static int flow_to_policy_dir(int dir
)
1197 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1198 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1199 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1205 return XFRM_POLICY_IN
;
1207 return XFRM_POLICY_OUT
;
1209 return XFRM_POLICY_FWD
;
1213 static struct flow_cache_object
*
1214 xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
,
1215 u8 dir
, struct flow_cache_object
*old_obj
, void *ctx
)
1217 struct xfrm_policy
*pol
;
1220 xfrm_pol_put(container_of(old_obj
, struct xfrm_policy
, flo
));
1222 pol
= __xfrm_policy_lookup(net
, fl
, family
, flow_to_policy_dir(dir
));
1223 if (IS_ERR_OR_NULL(pol
))
1224 return ERR_CAST(pol
);
1226 /* Resolver returns two references:
1227 * one for cache and one for caller of flow_cache_lookup() */
1233 static inline int policy_to_flow_dir(int dir
)
1235 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1236 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1237 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1241 case XFRM_POLICY_IN
:
1243 case XFRM_POLICY_OUT
:
1244 return FLOW_DIR_OUT
;
1245 case XFRM_POLICY_FWD
:
1246 return FLOW_DIR_FWD
;
1250 static struct xfrm_policy
*xfrm_sk_policy_lookup(const struct sock
*sk
, int dir
,
1251 const struct flowi
*fl
)
1253 struct xfrm_policy
*pol
;
1257 pol
= rcu_dereference(sk
->sk_policy
[dir
]);
1259 bool match
= xfrm_selector_match(&pol
->selector
, fl
,
1264 if ((sk
->sk_mark
& pol
->mark
.m
) != pol
->mark
.v
) {
1268 err
= security_xfrm_policy_lookup(pol
->security
,
1270 policy_to_flow_dir(dir
));
1272 if (!xfrm_pol_hold_rcu(pol
))
1274 } else if (err
== -ESRCH
) {
1287 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1289 struct net
*net
= xp_net(pol
);
1291 list_add(&pol
->walk
.all
, &net
->xfrm
.policy_all
);
1292 net
->xfrm
.policy_count
[dir
]++;
1296 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1299 struct net
*net
= xp_net(pol
);
1301 if (list_empty(&pol
->walk
.all
))
1304 /* Socket policies are not hashed. */
1305 if (!hlist_unhashed(&pol
->bydst
)) {
1306 hlist_del_rcu(&pol
->bydst
);
1307 hlist_del(&pol
->byidx
);
1310 list_del_init(&pol
->walk
.all
);
1311 net
->xfrm
.policy_count
[dir
]--;
1316 static void xfrm_sk_policy_link(struct xfrm_policy
*pol
, int dir
)
1318 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+ dir
);
1321 static void xfrm_sk_policy_unlink(struct xfrm_policy
*pol
, int dir
)
1323 __xfrm_policy_unlink(pol
, XFRM_POLICY_MAX
+ dir
);
1326 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1328 struct net
*net
= xp_net(pol
);
1330 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1331 pol
= __xfrm_policy_unlink(pol
, dir
);
1332 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1334 xfrm_policy_kill(pol
);
1339 EXPORT_SYMBOL(xfrm_policy_delete
);
1341 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1343 struct net
*net
= xp_net(pol
);
1344 struct xfrm_policy
*old_pol
;
1346 #ifdef CONFIG_XFRM_SUB_POLICY
1347 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1351 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1352 old_pol
= rcu_dereference_protected(sk
->sk_policy
[dir
],
1353 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
1355 pol
->curlft
.add_time
= get_seconds();
1356 pol
->index
= xfrm_gen_index(net
, XFRM_POLICY_MAX
+dir
, 0);
1357 xfrm_sk_policy_link(pol
, dir
);
1359 rcu_assign_pointer(sk
->sk_policy
[dir
], pol
);
1362 xfrm_policy_requeue(old_pol
, pol
);
1364 /* Unlinking succeeds always. This is the only function
1365 * allowed to delete or replace socket policy.
1367 xfrm_sk_policy_unlink(old_pol
, dir
);
1369 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1372 xfrm_policy_kill(old_pol
);
1377 static struct xfrm_policy
*clone_policy(const struct xfrm_policy
*old
, int dir
)
1379 struct xfrm_policy
*newp
= xfrm_policy_alloc(xp_net(old
), GFP_ATOMIC
);
1380 struct net
*net
= xp_net(old
);
1383 newp
->selector
= old
->selector
;
1384 if (security_xfrm_policy_clone(old
->security
,
1387 return NULL
; /* ENOMEM */
1389 newp
->lft
= old
->lft
;
1390 newp
->curlft
= old
->curlft
;
1391 newp
->mark
= old
->mark
;
1392 newp
->action
= old
->action
;
1393 newp
->flags
= old
->flags
;
1394 newp
->xfrm_nr
= old
->xfrm_nr
;
1395 newp
->index
= old
->index
;
1396 newp
->type
= old
->type
;
1397 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1398 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1399 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1400 xfrm_sk_policy_link(newp
, dir
);
1401 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1407 int __xfrm_sk_clone_policy(struct sock
*sk
, const struct sock
*osk
)
1409 const struct xfrm_policy
*p
;
1410 struct xfrm_policy
*np
;
1414 for (i
= 0; i
< 2; i
++) {
1415 p
= rcu_dereference(osk
->sk_policy
[i
]);
1417 np
= clone_policy(p
, i
);
1418 if (unlikely(!np
)) {
1422 rcu_assign_pointer(sk
->sk_policy
[i
], np
);
1430 xfrm_get_saddr(struct net
*net
, int oif
, xfrm_address_t
*local
,
1431 xfrm_address_t
*remote
, unsigned short family
)
1434 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1436 if (unlikely(afinfo
== NULL
))
1438 err
= afinfo
->get_saddr(net
, oif
, local
, remote
);
1439 xfrm_policy_put_afinfo(afinfo
);
1443 /* Resolve list of templates for the flow, given policy. */
1446 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, const struct flowi
*fl
,
1447 struct xfrm_state
**xfrm
, unsigned short family
)
1449 struct net
*net
= xp_net(policy
);
1452 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1453 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1456 for (nx
= 0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1457 struct xfrm_state
*x
;
1458 xfrm_address_t
*remote
= daddr
;
1459 xfrm_address_t
*local
= saddr
;
1460 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1462 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1463 tmpl
->mode
== XFRM_MODE_BEET
) {
1464 remote
= &tmpl
->id
.daddr
;
1465 local
= &tmpl
->saddr
;
1466 if (xfrm_addr_any(local
, tmpl
->encap_family
)) {
1467 error
= xfrm_get_saddr(net
, fl
->flowi_oif
,
1469 tmpl
->encap_family
);
1476 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1478 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1485 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1488 } else if (error
== -ESRCH
) {
1492 if (!tmpl
->optional
)
1498 for (nx
--; nx
>= 0; nx
--)
1499 xfrm_state_put(xfrm
[nx
]);
1504 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, const struct flowi
*fl
,
1505 struct xfrm_state
**xfrm
, unsigned short family
)
1507 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1508 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1514 for (i
= 0; i
< npols
; i
++) {
1515 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1520 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1528 /* found states are sorted for outbound processing */
1530 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1535 for (cnx
--; cnx
>= 0; cnx
--)
1536 xfrm_state_put(tpp
[cnx
]);
1541 /* Check that the bundle accepts the flow and its components are
1545 static inline int xfrm_get_tos(const struct flowi
*fl
, int family
)
1547 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1553 tos
= afinfo
->get_tos(fl
);
1555 xfrm_policy_put_afinfo(afinfo
);
1560 static struct flow_cache_object
*xfrm_bundle_flo_get(struct flow_cache_object
*flo
)
1562 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1563 struct dst_entry
*dst
= &xdst
->u
.dst
;
1565 if (xdst
->route
== NULL
) {
1566 /* Dummy bundle - if it has xfrms we were not
1567 * able to build bundle as template resolution failed.
1568 * It means we need to try again resolving. */
1569 if (xdst
->num_xfrms
> 0)
1571 } else if (dst
->flags
& DST_XFRM_QUEUE
) {
1575 if (stale_bundle(dst
))
1583 static int xfrm_bundle_flo_check(struct flow_cache_object
*flo
)
1585 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1586 struct dst_entry
*dst
= &xdst
->u
.dst
;
1590 if (stale_bundle(dst
))
1596 static void xfrm_bundle_flo_delete(struct flow_cache_object
*flo
)
1598 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1599 struct dst_entry
*dst
= &xdst
->u
.dst
;
1604 static const struct flow_cache_ops xfrm_bundle_fc_ops
= {
1605 .get
= xfrm_bundle_flo_get
,
1606 .check
= xfrm_bundle_flo_check
,
1607 .delete = xfrm_bundle_flo_delete
,
1610 static inline struct xfrm_dst
*xfrm_alloc_dst(struct net
*net
, int family
)
1612 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1613 struct dst_ops
*dst_ops
;
1614 struct xfrm_dst
*xdst
;
1617 return ERR_PTR(-EINVAL
);
1621 dst_ops
= &net
->xfrm
.xfrm4_dst_ops
;
1623 #if IS_ENABLED(CONFIG_IPV6)
1625 dst_ops
= &net
->xfrm
.xfrm6_dst_ops
;
1631 xdst
= dst_alloc(dst_ops
, NULL
, 0, DST_OBSOLETE_NONE
, 0);
1634 struct dst_entry
*dst
= &xdst
->u
.dst
;
1636 memset(dst
+ 1, 0, sizeof(*xdst
) - sizeof(*dst
));
1637 xdst
->flo
.ops
= &xfrm_bundle_fc_ops
;
1639 xdst
= ERR_PTR(-ENOBUFS
);
1641 xfrm_policy_put_afinfo(afinfo
);
1646 static inline int xfrm_init_path(struct xfrm_dst
*path
, struct dst_entry
*dst
,
1649 struct xfrm_policy_afinfo
*afinfo
=
1650 xfrm_policy_get_afinfo(dst
->ops
->family
);
1656 err
= afinfo
->init_path(path
, dst
, nfheader_len
);
1658 xfrm_policy_put_afinfo(afinfo
);
1663 static inline int xfrm_fill_dst(struct xfrm_dst
*xdst
, struct net_device
*dev
,
1664 const struct flowi
*fl
)
1666 struct xfrm_policy_afinfo
*afinfo
=
1667 xfrm_policy_get_afinfo(xdst
->u
.dst
.ops
->family
);
1673 err
= afinfo
->fill_dst(xdst
, dev
, fl
);
1675 xfrm_policy_put_afinfo(afinfo
);
1681 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1682 * all the metrics... Shortly, bundle a bundle.
1685 static struct dst_entry
*xfrm_bundle_create(struct xfrm_policy
*policy
,
1686 struct xfrm_state
**xfrm
, int nx
,
1687 const struct flowi
*fl
,
1688 struct dst_entry
*dst
)
1690 struct net
*net
= xp_net(policy
);
1691 unsigned long now
= jiffies
;
1692 struct net_device
*dev
;
1693 struct xfrm_mode
*inner_mode
;
1694 struct dst_entry
*dst_prev
= NULL
;
1695 struct dst_entry
*dst0
= NULL
;
1699 int nfheader_len
= 0;
1700 int trailer_len
= 0;
1702 int family
= policy
->selector
.family
;
1703 xfrm_address_t saddr
, daddr
;
1705 xfrm_flowi_addr_get(fl
, &saddr
, &daddr
, family
);
1707 tos
= xfrm_get_tos(fl
, family
);
1714 for (; i
< nx
; i
++) {
1715 struct xfrm_dst
*xdst
= xfrm_alloc_dst(net
, family
);
1716 struct dst_entry
*dst1
= &xdst
->u
.dst
;
1718 err
= PTR_ERR(xdst
);
1724 if (xfrm
[i
]->sel
.family
== AF_UNSPEC
) {
1725 inner_mode
= xfrm_ip2inner_mode(xfrm
[i
],
1726 xfrm_af2proto(family
));
1728 err
= -EAFNOSUPPORT
;
1733 inner_mode
= xfrm
[i
]->inner_mode
;
1738 dst_prev
->child
= dst_clone(dst1
);
1739 dst1
->flags
|= DST_NOHASH
;
1743 dst_copy_metrics(dst1
, dst
);
1745 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1746 family
= xfrm
[i
]->props
.family
;
1747 dst
= xfrm_dst_lookup(xfrm
[i
], tos
, fl
->flowi_oif
,
1748 &saddr
, &daddr
, family
);
1755 dst1
->xfrm
= xfrm
[i
];
1756 xdst
->xfrm_genid
= xfrm
[i
]->genid
;
1758 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
1759 dst1
->flags
|= DST_HOST
;
1760 dst1
->lastuse
= now
;
1762 dst1
->input
= dst_discard
;
1763 dst1
->output
= inner_mode
->afinfo
->output
;
1765 dst1
->next
= dst_prev
;
1768 header_len
+= xfrm
[i
]->props
.header_len
;
1769 if (xfrm
[i
]->type
->flags
& XFRM_TYPE_NON_FRAGMENT
)
1770 nfheader_len
+= xfrm
[i
]->props
.header_len
;
1771 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
1774 dst_prev
->child
= dst
;
1782 xfrm_init_path((struct xfrm_dst
*)dst0
, dst
, nfheader_len
);
1783 xfrm_init_pmtu(dst_prev
);
1785 for (dst_prev
= dst0
; dst_prev
!= dst
; dst_prev
= dst_prev
->child
) {
1786 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst_prev
;
1788 err
= xfrm_fill_dst(xdst
, dev
, fl
);
1792 dst_prev
->header_len
= header_len
;
1793 dst_prev
->trailer_len
= trailer_len
;
1794 header_len
-= xdst
->u
.dst
.xfrm
->props
.header_len
;
1795 trailer_len
-= xdst
->u
.dst
.xfrm
->props
.trailer_len
;
1803 xfrm_state_put(xfrm
[i
]);
1807 dst0
= ERR_PTR(err
);
1811 #ifdef CONFIG_XFRM_SUB_POLICY
1812 static int xfrm_dst_alloc_copy(void **target
, const void *src
, int size
)
1815 *target
= kmalloc(size
, GFP_ATOMIC
);
1820 memcpy(*target
, src
, size
);
1825 static int xfrm_dst_update_parent(struct dst_entry
*dst
,
1826 const struct xfrm_selector
*sel
)
1828 #ifdef CONFIG_XFRM_SUB_POLICY
1829 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1830 return xfrm_dst_alloc_copy((void **)&(xdst
->partner
),
1837 static int xfrm_dst_update_origin(struct dst_entry
*dst
,
1838 const struct flowi
*fl
)
1840 #ifdef CONFIG_XFRM_SUB_POLICY
1841 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1842 return xfrm_dst_alloc_copy((void **)&(xdst
->origin
), fl
, sizeof(*fl
));
1848 static int xfrm_expand_policies(const struct flowi
*fl
, u16 family
,
1849 struct xfrm_policy
**pols
,
1850 int *num_pols
, int *num_xfrms
)
1854 if (*num_pols
== 0 || !pols
[0]) {
1859 if (IS_ERR(pols
[0]))
1860 return PTR_ERR(pols
[0]);
1862 *num_xfrms
= pols
[0]->xfrm_nr
;
1864 #ifdef CONFIG_XFRM_SUB_POLICY
1865 if (pols
[0] && pols
[0]->action
== XFRM_POLICY_ALLOW
&&
1866 pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1867 pols
[1] = xfrm_policy_lookup_bytype(xp_net(pols
[0]),
1868 XFRM_POLICY_TYPE_MAIN
,
1872 if (IS_ERR(pols
[1])) {
1873 xfrm_pols_put(pols
, *num_pols
);
1874 return PTR_ERR(pols
[1]);
1877 (*num_xfrms
) += pols
[1]->xfrm_nr
;
1881 for (i
= 0; i
< *num_pols
; i
++) {
1882 if (pols
[i
]->action
!= XFRM_POLICY_ALLOW
) {
1892 static struct xfrm_dst
*
1893 xfrm_resolve_and_create_bundle(struct xfrm_policy
**pols
, int num_pols
,
1894 const struct flowi
*fl
, u16 family
,
1895 struct dst_entry
*dst_orig
)
1897 struct net
*net
= xp_net(pols
[0]);
1898 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1899 struct dst_entry
*dst
;
1900 struct xfrm_dst
*xdst
;
1903 /* Try to instantiate a bundle */
1904 err
= xfrm_tmpl_resolve(pols
, num_pols
, fl
, xfrm
, family
);
1906 if (err
!= 0 && err
!= -EAGAIN
)
1907 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1908 return ERR_PTR(err
);
1911 dst
= xfrm_bundle_create(pols
[0], xfrm
, err
, fl
, dst_orig
);
1913 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLEGENERROR
);
1914 return ERR_CAST(dst
);
1917 xdst
= (struct xfrm_dst
*)dst
;
1918 xdst
->num_xfrms
= err
;
1920 err
= xfrm_dst_update_parent(dst
, &pols
[1]->selector
);
1922 err
= xfrm_dst_update_origin(dst
, fl
);
1923 if (unlikely(err
)) {
1925 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLECHECKERROR
);
1926 return ERR_PTR(err
);
1929 xdst
->num_pols
= num_pols
;
1930 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1931 xdst
->policy_genid
= atomic_read(&pols
[0]->genid
);
1936 static void xfrm_policy_queue_process(unsigned long arg
)
1938 struct sk_buff
*skb
;
1940 struct dst_entry
*dst
;
1941 struct xfrm_policy
*pol
= (struct xfrm_policy
*)arg
;
1942 struct net
*net
= xp_net(pol
);
1943 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1945 struct sk_buff_head list
;
1947 spin_lock(&pq
->hold_queue
.lock
);
1948 skb
= skb_peek(&pq
->hold_queue
);
1950 spin_unlock(&pq
->hold_queue
.lock
);
1955 xfrm_decode_session(skb
, &fl
, dst
->ops
->family
);
1956 spin_unlock(&pq
->hold_queue
.lock
);
1958 dst_hold(dst
->path
);
1959 dst
= xfrm_lookup(net
, dst
->path
, &fl
, sk
, 0);
1963 if (dst
->flags
& DST_XFRM_QUEUE
) {
1966 if (pq
->timeout
>= XFRM_QUEUE_TMO_MAX
)
1969 pq
->timeout
= pq
->timeout
<< 1;
1970 if (!mod_timer(&pq
->hold_timer
, jiffies
+ pq
->timeout
))
1977 __skb_queue_head_init(&list
);
1979 spin_lock(&pq
->hold_queue
.lock
);
1981 skb_queue_splice_init(&pq
->hold_queue
, &list
);
1982 spin_unlock(&pq
->hold_queue
.lock
);
1984 while (!skb_queue_empty(&list
)) {
1985 skb
= __skb_dequeue(&list
);
1987 xfrm_decode_session(skb
, &fl
, skb_dst(skb
)->ops
->family
);
1988 dst_hold(skb_dst(skb
)->path
);
1989 dst
= xfrm_lookup(net
, skb_dst(skb
)->path
, &fl
, skb
->sk
, 0);
1997 skb_dst_set(skb
, dst
);
1999 dst_output(net
, skb
->sk
, skb
);
2008 skb_queue_purge(&pq
->hold_queue
);
2012 static int xdst_queue_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
2014 unsigned long sched_next
;
2015 struct dst_entry
*dst
= skb_dst(skb
);
2016 struct xfrm_dst
*xdst
= (struct xfrm_dst
*) dst
;
2017 struct xfrm_policy
*pol
= xdst
->pols
[0];
2018 struct xfrm_policy_queue
*pq
= &pol
->polq
;
2020 if (unlikely(skb_fclone_busy(sk
, skb
))) {
2025 if (pq
->hold_queue
.qlen
> XFRM_MAX_QUEUE_LEN
) {
2032 spin_lock_bh(&pq
->hold_queue
.lock
);
2035 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
2037 sched_next
= jiffies
+ pq
->timeout
;
2039 if (del_timer(&pq
->hold_timer
)) {
2040 if (time_before(pq
->hold_timer
.expires
, sched_next
))
2041 sched_next
= pq
->hold_timer
.expires
;
2045 __skb_queue_tail(&pq
->hold_queue
, skb
);
2046 if (!mod_timer(&pq
->hold_timer
, sched_next
))
2049 spin_unlock_bh(&pq
->hold_queue
.lock
);
2054 static struct xfrm_dst
*xfrm_create_dummy_bundle(struct net
*net
,
2055 struct xfrm_flo
*xflo
,
2056 const struct flowi
*fl
,
2061 struct net_device
*dev
;
2062 struct dst_entry
*dst
;
2063 struct dst_entry
*dst1
;
2064 struct xfrm_dst
*xdst
;
2066 xdst
= xfrm_alloc_dst(net
, family
);
2070 if (!(xflo
->flags
& XFRM_LOOKUP_QUEUE
) ||
2071 net
->xfrm
.sysctl_larval_drop
||
2075 dst
= xflo
->dst_orig
;
2076 dst1
= &xdst
->u
.dst
;
2080 dst_copy_metrics(dst1
, dst
);
2082 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
2083 dst1
->flags
|= DST_HOST
| DST_XFRM_QUEUE
;
2084 dst1
->lastuse
= jiffies
;
2086 dst1
->input
= dst_discard
;
2087 dst1
->output
= xdst_queue_output
;
2093 xfrm_init_path((struct xfrm_dst
*)dst1
, dst
, 0);
2100 err
= xfrm_fill_dst(xdst
, dev
, fl
);
2109 xdst
= ERR_PTR(err
);
2113 static struct flow_cache_object
*
2114 xfrm_bundle_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
,
2115 struct flow_cache_object
*oldflo
, void *ctx
)
2117 struct xfrm_flo
*xflo
= (struct xfrm_flo
*)ctx
;
2118 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2119 struct xfrm_dst
*xdst
, *new_xdst
;
2120 int num_pols
= 0, num_xfrms
= 0, i
, err
, pol_dead
;
2122 /* Check if the policies from old bundle are usable */
2125 xdst
= container_of(oldflo
, struct xfrm_dst
, flo
);
2126 num_pols
= xdst
->num_pols
;
2127 num_xfrms
= xdst
->num_xfrms
;
2129 for (i
= 0; i
< num_pols
; i
++) {
2130 pols
[i
] = xdst
->pols
[i
];
2131 pol_dead
|= pols
[i
]->walk
.dead
;
2134 dst_free(&xdst
->u
.dst
);
2142 /* Resolve policies to use if we couldn't get them from
2143 * previous cache entry */
2146 pols
[0] = __xfrm_policy_lookup(net
, fl
, family
,
2147 flow_to_policy_dir(dir
));
2148 err
= xfrm_expand_policies(fl
, family
, pols
,
2149 &num_pols
, &num_xfrms
);
2155 goto make_dummy_bundle
;
2158 new_xdst
= xfrm_resolve_and_create_bundle(pols
, num_pols
, fl
, family
,
2160 if (IS_ERR(new_xdst
)) {
2161 err
= PTR_ERR(new_xdst
);
2165 goto make_dummy_bundle
;
2166 dst_hold(&xdst
->u
.dst
);
2168 } else if (new_xdst
== NULL
) {
2171 goto make_dummy_bundle
;
2172 xdst
->num_xfrms
= 0;
2173 dst_hold(&xdst
->u
.dst
);
2177 /* Kill the previous bundle */
2179 /* The policies were stolen for newly generated bundle */
2181 dst_free(&xdst
->u
.dst
);
2184 /* Flow cache does not have reference, it dst_free()'s,
2185 * but we do need to return one reference for original caller */
2186 dst_hold(&new_xdst
->u
.dst
);
2187 return &new_xdst
->flo
;
2190 /* We found policies, but there's no bundles to instantiate:
2191 * either because the policy blocks, has no transformations or
2192 * we could not build template (no xfrm_states).*/
2193 xdst
= xfrm_create_dummy_bundle(net
, xflo
, fl
, num_xfrms
, family
);
2195 xfrm_pols_put(pols
, num_pols
);
2196 return ERR_CAST(xdst
);
2198 xdst
->num_pols
= num_pols
;
2199 xdst
->num_xfrms
= num_xfrms
;
2200 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2202 dst_hold(&xdst
->u
.dst
);
2206 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
2209 dst_free(&xdst
->u
.dst
);
2211 xfrm_pols_put(pols
, num_pols
);
2212 return ERR_PTR(err
);
2215 static struct dst_entry
*make_blackhole(struct net
*net
, u16 family
,
2216 struct dst_entry
*dst_orig
)
2218 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2219 struct dst_entry
*ret
;
2222 dst_release(dst_orig
);
2223 return ERR_PTR(-EINVAL
);
2225 ret
= afinfo
->blackhole_route(net
, dst_orig
);
2227 xfrm_policy_put_afinfo(afinfo
);
2232 /* Main function: finds/creates a bundle for given flow.
2234 * At the moment we eat a raw IP route. Mostly to speed up lookups
2235 * on interfaces with disabled IPsec.
2237 struct dst_entry
*xfrm_lookup(struct net
*net
, struct dst_entry
*dst_orig
,
2238 const struct flowi
*fl
,
2239 const struct sock
*sk
, int flags
)
2241 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2242 struct flow_cache_object
*flo
;
2243 struct xfrm_dst
*xdst
;
2244 struct dst_entry
*dst
, *route
;
2245 u16 family
= dst_orig
->ops
->family
;
2246 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
2247 int i
, err
, num_pols
, num_xfrms
= 0, drop_pols
= 0;
2253 sk
= sk_const_to_full_sk(sk
);
2254 if (sk
&& sk
->sk_policy
[XFRM_POLICY_OUT
]) {
2256 pols
[0] = xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
);
2257 err
= xfrm_expand_policies(fl
, family
, pols
,
2258 &num_pols
, &num_xfrms
);
2263 if (num_xfrms
<= 0) {
2264 drop_pols
= num_pols
;
2268 xdst
= xfrm_resolve_and_create_bundle(
2272 xfrm_pols_put(pols
, num_pols
);
2273 err
= PTR_ERR(xdst
);
2275 } else if (xdst
== NULL
) {
2277 drop_pols
= num_pols
;
2281 dst_hold(&xdst
->u
.dst
);
2282 xdst
->u
.dst
.flags
|= DST_NOCACHE
;
2283 route
= xdst
->route
;
2288 struct xfrm_flo xflo
;
2290 xflo
.dst_orig
= dst_orig
;
2293 /* To accelerate a bit... */
2294 if ((dst_orig
->flags
& DST_NOXFRM
) ||
2295 !net
->xfrm
.policy_count
[XFRM_POLICY_OUT
])
2298 flo
= flow_cache_lookup(net
, fl
, family
, dir
,
2299 xfrm_bundle_lookup
, &xflo
);
2306 xdst
= container_of(flo
, struct xfrm_dst
, flo
);
2308 num_pols
= xdst
->num_pols
;
2309 num_xfrms
= xdst
->num_xfrms
;
2310 memcpy(pols
, xdst
->pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2311 route
= xdst
->route
;
2315 if (route
== NULL
&& num_xfrms
> 0) {
2316 /* The only case when xfrm_bundle_lookup() returns a
2317 * bundle with null route, is when the template could
2318 * not be resolved. It means policies are there, but
2319 * bundle could not be created, since we don't yet
2320 * have the xfrm_state's. We need to wait for KM to
2321 * negotiate new SA's or bail out with error.*/
2322 if (net
->xfrm
.sysctl_larval_drop
) {
2323 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2330 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2338 if ((flags
& XFRM_LOOKUP_ICMP
) &&
2339 !(pols
[0]->flags
& XFRM_POLICY_ICMP
)) {
2344 for (i
= 0; i
< num_pols
; i
++)
2345 pols
[i
]->curlft
.use_time
= get_seconds();
2347 if (num_xfrms
< 0) {
2348 /* Prohibit the flow */
2349 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLBLOCK
);
2352 } else if (num_xfrms
> 0) {
2353 /* Flow transformed */
2354 dst_release(dst_orig
);
2356 /* Flow passes untransformed */
2361 xfrm_pols_put(pols
, drop_pols
);
2362 if (dst
&& dst
->xfrm
&&
2363 dst
->xfrm
->props
.mode
== XFRM_MODE_TUNNEL
)
2364 dst
->flags
|= DST_XFRM_TUNNEL
;
2368 if (!(flags
& XFRM_LOOKUP_ICMP
)) {
2376 if (!(flags
& XFRM_LOOKUP_KEEP_DST_REF
))
2377 dst_release(dst_orig
);
2378 xfrm_pols_put(pols
, drop_pols
);
2379 return ERR_PTR(err
);
2381 EXPORT_SYMBOL(xfrm_lookup
);
2383 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2384 * Otherwise we may send out blackholed packets.
2386 struct dst_entry
*xfrm_lookup_route(struct net
*net
, struct dst_entry
*dst_orig
,
2387 const struct flowi
*fl
,
2388 const struct sock
*sk
, int flags
)
2390 struct dst_entry
*dst
= xfrm_lookup(net
, dst_orig
, fl
, sk
,
2391 flags
| XFRM_LOOKUP_QUEUE
|
2392 XFRM_LOOKUP_KEEP_DST_REF
);
2394 if (IS_ERR(dst
) && PTR_ERR(dst
) == -EREMOTE
)
2395 return make_blackhole(net
, dst_orig
->ops
->family
, dst_orig
);
2399 EXPORT_SYMBOL(xfrm_lookup_route
);
2402 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, const struct flowi
*fl
)
2404 struct xfrm_state
*x
;
2406 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
2408 x
= skb
->sp
->xvec
[idx
];
2409 if (!x
->type
->reject
)
2411 return x
->type
->reject(x
, skb
, fl
);
2414 /* When skb is transformed back to its "native" form, we have to
2415 * check policy restrictions. At the moment we make this in maximally
2416 * stupid way. Shame on me. :-) Of course, connected sockets must
2417 * have policy cached at them.
2421 xfrm_state_ok(const struct xfrm_tmpl
*tmpl
, const struct xfrm_state
*x
,
2422 unsigned short family
)
2424 if (xfrm_state_kern(x
))
2425 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
2426 return x
->id
.proto
== tmpl
->id
.proto
&&
2427 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
2428 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
2429 x
->props
.mode
== tmpl
->mode
&&
2430 (tmpl
->allalgs
|| (tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
2431 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
2432 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
2433 xfrm_state_addr_cmp(tmpl
, x
, family
));
2437 * 0 or more than 0 is returned when validation is succeeded (either bypass
2438 * because of optional transport mode, or next index of the mathced secpath
2439 * state with the template.
2440 * -1 is returned when no matching template is found.
2441 * Otherwise "-2 - errored_index" is returned.
2444 xfrm_policy_ok(const struct xfrm_tmpl
*tmpl
, const struct sec_path
*sp
, int start
,
2445 unsigned short family
)
2449 if (tmpl
->optional
) {
2450 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
2454 for (; idx
< sp
->len
; idx
++) {
2455 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
2457 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2466 int __xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
,
2467 unsigned int family
, int reverse
)
2469 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2472 if (unlikely(afinfo
== NULL
))
2473 return -EAFNOSUPPORT
;
2475 afinfo
->decode_session(skb
, fl
, reverse
);
2476 err
= security_xfrm_decode_session(skb
, &fl
->flowi_secid
);
2477 xfrm_policy_put_afinfo(afinfo
);
2480 EXPORT_SYMBOL(__xfrm_decode_session
);
2482 static inline int secpath_has_nontransport(const struct sec_path
*sp
, int k
, int *idxp
)
2484 for (; k
< sp
->len
; k
++) {
2485 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2494 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
2495 unsigned short family
)
2497 struct net
*net
= dev_net(skb
->dev
);
2498 struct xfrm_policy
*pol
;
2499 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2508 reverse
= dir
& ~XFRM_POLICY_MASK
;
2509 dir
&= XFRM_POLICY_MASK
;
2510 fl_dir
= policy_to_flow_dir(dir
);
2512 if (__xfrm_decode_session(skb
, &fl
, family
, reverse
) < 0) {
2513 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
2517 nf_nat_decode_session(skb
, &fl
, family
);
2519 /* First, check used SA against their selectors. */
2523 for (i
= skb
->sp
->len
-1; i
>= 0; i
--) {
2524 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
2525 if (!xfrm_selector_match(&x
->sel
, &fl
, family
)) {
2526 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMISMATCH
);
2533 sk
= sk_to_full_sk(sk
);
2534 if (sk
&& sk
->sk_policy
[dir
]) {
2535 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
);
2537 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2543 struct flow_cache_object
*flo
;
2545 flo
= flow_cache_lookup(net
, &fl
, family
, fl_dir
,
2546 xfrm_policy_lookup
, NULL
);
2547 if (IS_ERR_OR_NULL(flo
))
2548 pol
= ERR_CAST(flo
);
2550 pol
= container_of(flo
, struct xfrm_policy
, flo
);
2554 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2559 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
2560 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2561 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINNOPOLS
);
2567 pol
->curlft
.use_time
= get_seconds();
2571 #ifdef CONFIG_XFRM_SUB_POLICY
2572 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
2573 pols
[1] = xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
,
2577 if (IS_ERR(pols
[1])) {
2578 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2581 pols
[1]->curlft
.use_time
= get_seconds();
2587 if (pol
->action
== XFRM_POLICY_ALLOW
) {
2588 struct sec_path
*sp
;
2589 static struct sec_path dummy
;
2590 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
2591 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
2592 struct xfrm_tmpl
**tpp
= tp
;
2596 if ((sp
= skb
->sp
) == NULL
)
2599 for (pi
= 0; pi
< npols
; pi
++) {
2600 if (pols
[pi
] != pol
&&
2601 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
) {
2602 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2605 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
2606 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
2609 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
2610 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
2614 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
, net
);
2618 /* For each tunnel xfrm, find the first matching tmpl.
2619 * For each tmpl before that, find corresponding xfrm.
2620 * Order is _important_. Later we will implement
2621 * some barriers, but at the moment barriers
2622 * are implied between each two transformations.
2624 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
2625 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
2628 /* "-2 - errored_index" returned */
2630 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2635 if (secpath_has_nontransport(sp
, k
, &xerr_idx
)) {
2636 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2640 xfrm_pols_put(pols
, npols
);
2643 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2646 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2648 xfrm_pols_put(pols
, npols
);
2651 EXPORT_SYMBOL(__xfrm_policy_check
);
2653 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
2655 struct net
*net
= dev_net(skb
->dev
);
2657 struct dst_entry
*dst
;
2660 if (xfrm_decode_session(skb
, &fl
, family
) < 0) {
2661 XFRM_INC_STATS(net
, LINUX_MIB_XFRMFWDHDRERROR
);
2667 dst
= xfrm_lookup(net
, skb_dst(skb
), &fl
, NULL
, XFRM_LOOKUP_QUEUE
);
2672 skb_dst_set(skb
, dst
);
2675 EXPORT_SYMBOL(__xfrm_route_forward
);
2677 /* Optimize later using cookies and generation ids. */
2679 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
2681 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2682 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2683 * get validated by dst_ops->check on every use. We do this
2684 * because when a normal route referenced by an XFRM dst is
2685 * obsoleted we do not go looking around for all parent
2686 * referencing XFRM dsts so that we can invalidate them. It
2687 * is just too much work. Instead we make the checks here on
2688 * every use. For example:
2690 * XFRM dst A --> IPv4 dst X
2692 * X is the "xdst->route" of A (X is also the "dst->path" of A
2693 * in this example). If X is marked obsolete, "A" will not
2694 * notice. That's what we are validating here via the
2695 * stale_bundle() check.
2697 * When a policy's bundle is pruned, we dst_free() the XFRM
2698 * dst which causes it's ->obsolete field to be set to
2699 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2700 * this, we want to force a new route lookup.
2702 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
2708 static int stale_bundle(struct dst_entry
*dst
)
2710 return !xfrm_bundle_ok((struct xfrm_dst
*)dst
);
2713 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
2715 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
2716 dst
->dev
= dev_net(dev
)->loopback_dev
;
2721 EXPORT_SYMBOL(xfrm_dst_ifdown
);
2723 static void xfrm_link_failure(struct sk_buff
*skb
)
2725 /* Impossible. Such dst must be popped before reaches point of failure. */
2728 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
2731 if (dst
->obsolete
) {
2739 void xfrm_garbage_collect(struct net
*net
)
2741 flow_cache_flush(net
);
2743 EXPORT_SYMBOL(xfrm_garbage_collect
);
2745 static void xfrm_garbage_collect_deferred(struct net
*net
)
2747 flow_cache_flush_deferred(net
);
2750 static void xfrm_init_pmtu(struct dst_entry
*dst
)
2753 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2754 u32 pmtu
, route_mtu_cached
;
2756 pmtu
= dst_mtu(dst
->child
);
2757 xdst
->child_mtu_cached
= pmtu
;
2759 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2761 route_mtu_cached
= dst_mtu(xdst
->route
);
2762 xdst
->route_mtu_cached
= route_mtu_cached
;
2764 if (pmtu
> route_mtu_cached
)
2765 pmtu
= route_mtu_cached
;
2767 dst_metric_set(dst
, RTAX_MTU
, pmtu
);
2768 } while ((dst
= dst
->next
));
2771 /* Check that the bundle accepts the flow and its components are
2775 static int xfrm_bundle_ok(struct xfrm_dst
*first
)
2777 struct dst_entry
*dst
= &first
->u
.dst
;
2778 struct xfrm_dst
*last
;
2781 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2782 (dst
->dev
&& !netif_running(dst
->dev
)))
2785 if (dst
->flags
& DST_XFRM_QUEUE
)
2791 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2793 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2795 if (xdst
->xfrm_genid
!= dst
->xfrm
->genid
)
2797 if (xdst
->num_pols
> 0 &&
2798 xdst
->policy_genid
!= atomic_read(&xdst
->pols
[0]->genid
))
2801 mtu
= dst_mtu(dst
->child
);
2802 if (xdst
->child_mtu_cached
!= mtu
) {
2804 xdst
->child_mtu_cached
= mtu
;
2807 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2809 mtu
= dst_mtu(xdst
->route
);
2810 if (xdst
->route_mtu_cached
!= mtu
) {
2812 xdst
->route_mtu_cached
= mtu
;
2816 } while (dst
->xfrm
);
2821 mtu
= last
->child_mtu_cached
;
2825 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2826 if (mtu
> last
->route_mtu_cached
)
2827 mtu
= last
->route_mtu_cached
;
2828 dst_metric_set(dst
, RTAX_MTU
, mtu
);
2833 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2834 last
->child_mtu_cached
= mtu
;
2840 static unsigned int xfrm_default_advmss(const struct dst_entry
*dst
)
2842 return dst_metric_advmss(dst
->path
);
2845 static unsigned int xfrm_mtu(const struct dst_entry
*dst
)
2847 unsigned int mtu
= dst_metric_raw(dst
, RTAX_MTU
);
2849 return mtu
? : dst_mtu(dst
->path
);
2852 static struct neighbour
*xfrm_neigh_lookup(const struct dst_entry
*dst
,
2853 struct sk_buff
*skb
,
2856 return dst
->path
->ops
->neigh_lookup(dst
, skb
, daddr
);
2859 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2862 if (unlikely(afinfo
== NULL
))
2864 if (unlikely(afinfo
->family
>= NPROTO
))
2865 return -EAFNOSUPPORT
;
2866 spin_lock(&xfrm_policy_afinfo_lock
);
2867 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
2870 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2871 if (likely(dst_ops
->kmem_cachep
== NULL
))
2872 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2873 if (likely(dst_ops
->check
== NULL
))
2874 dst_ops
->check
= xfrm_dst_check
;
2875 if (likely(dst_ops
->default_advmss
== NULL
))
2876 dst_ops
->default_advmss
= xfrm_default_advmss
;
2877 if (likely(dst_ops
->mtu
== NULL
))
2878 dst_ops
->mtu
= xfrm_mtu
;
2879 if (likely(dst_ops
->negative_advice
== NULL
))
2880 dst_ops
->negative_advice
= xfrm_negative_advice
;
2881 if (likely(dst_ops
->link_failure
== NULL
))
2882 dst_ops
->link_failure
= xfrm_link_failure
;
2883 if (likely(dst_ops
->neigh_lookup
== NULL
))
2884 dst_ops
->neigh_lookup
= xfrm_neigh_lookup
;
2885 if (likely(afinfo
->garbage_collect
== NULL
))
2886 afinfo
->garbage_collect
= xfrm_garbage_collect_deferred
;
2887 rcu_assign_pointer(xfrm_policy_afinfo
[afinfo
->family
], afinfo
);
2889 spin_unlock(&xfrm_policy_afinfo_lock
);
2893 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2895 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2898 if (unlikely(afinfo
== NULL
))
2900 if (unlikely(afinfo
->family
>= NPROTO
))
2901 return -EAFNOSUPPORT
;
2902 spin_lock(&xfrm_policy_afinfo_lock
);
2903 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
2904 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
2907 RCU_INIT_POINTER(xfrm_policy_afinfo
[afinfo
->family
],
2910 spin_unlock(&xfrm_policy_afinfo_lock
);
2912 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2916 dst_ops
->kmem_cachep
= NULL
;
2917 dst_ops
->check
= NULL
;
2918 dst_ops
->negative_advice
= NULL
;
2919 dst_ops
->link_failure
= NULL
;
2920 afinfo
->garbage_collect
= NULL
;
2924 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2926 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
2928 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2932 xfrm_garbage_collect(dev_net(dev
));
2937 static struct notifier_block xfrm_dev_notifier
= {
2938 .notifier_call
= xfrm_dev_event
,
2941 #ifdef CONFIG_XFRM_STATISTICS
2942 static int __net_init
xfrm_statistics_init(struct net
*net
)
2945 net
->mib
.xfrm_statistics
= alloc_percpu(struct linux_xfrm_mib
);
2946 if (!net
->mib
.xfrm_statistics
)
2948 rv
= xfrm_proc_init(net
);
2950 free_percpu(net
->mib
.xfrm_statistics
);
2954 static void xfrm_statistics_fini(struct net
*net
)
2956 xfrm_proc_fini(net
);
2957 free_percpu(net
->mib
.xfrm_statistics
);
2960 static int __net_init
xfrm_statistics_init(struct net
*net
)
2965 static void xfrm_statistics_fini(struct net
*net
)
2970 static int __net_init
xfrm_policy_init(struct net
*net
)
2972 unsigned int hmask
, sz
;
2975 if (net_eq(net
, &init_net
))
2976 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2977 sizeof(struct xfrm_dst
),
2978 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2982 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2984 net
->xfrm
.policy_byidx
= xfrm_hash_alloc(sz
);
2985 if (!net
->xfrm
.policy_byidx
)
2987 net
->xfrm
.policy_idx_hmask
= hmask
;
2989 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
2990 struct xfrm_policy_hash
*htab
;
2992 net
->xfrm
.policy_count
[dir
] = 0;
2993 net
->xfrm
.policy_count
[XFRM_POLICY_MAX
+ dir
] = 0;
2994 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
2996 htab
= &net
->xfrm
.policy_bydst
[dir
];
2997 htab
->table
= xfrm_hash_alloc(sz
);
3000 htab
->hmask
= hmask
;
3006 net
->xfrm
.policy_hthresh
.lbits4
= 32;
3007 net
->xfrm
.policy_hthresh
.rbits4
= 32;
3008 net
->xfrm
.policy_hthresh
.lbits6
= 128;
3009 net
->xfrm
.policy_hthresh
.rbits6
= 128;
3011 seqlock_init(&net
->xfrm
.policy_hthresh
.lock
);
3013 INIT_LIST_HEAD(&net
->xfrm
.policy_all
);
3014 INIT_WORK(&net
->xfrm
.policy_hash_work
, xfrm_hash_resize
);
3015 INIT_WORK(&net
->xfrm
.policy_hthresh
.work
, xfrm_hash_rebuild
);
3016 if (net_eq(net
, &init_net
))
3017 register_netdevice_notifier(&xfrm_dev_notifier
);
3021 for (dir
--; dir
>= 0; dir
--) {
3022 struct xfrm_policy_hash
*htab
;
3024 htab
= &net
->xfrm
.policy_bydst
[dir
];
3025 xfrm_hash_free(htab
->table
, sz
);
3027 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
3032 static void xfrm_policy_fini(struct net
*net
)
3037 flush_work(&net
->xfrm
.policy_hash_work
);
3038 #ifdef CONFIG_XFRM_SUB_POLICY
3039 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_SUB
, false);
3041 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, false);
3043 WARN_ON(!list_empty(&net
->xfrm
.policy_all
));
3045 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
3046 struct xfrm_policy_hash
*htab
;
3048 WARN_ON(!hlist_empty(&net
->xfrm
.policy_inexact
[dir
]));
3050 htab
= &net
->xfrm
.policy_bydst
[dir
];
3051 sz
= (htab
->hmask
+ 1) * sizeof(struct hlist_head
);
3052 WARN_ON(!hlist_empty(htab
->table
));
3053 xfrm_hash_free(htab
->table
, sz
);
3056 sz
= (net
->xfrm
.policy_idx_hmask
+ 1) * sizeof(struct hlist_head
);
3057 WARN_ON(!hlist_empty(net
->xfrm
.policy_byidx
));
3058 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
3061 static int __net_init
xfrm_net_init(struct net
*net
)
3065 rv
= xfrm_statistics_init(net
);
3067 goto out_statistics
;
3068 rv
= xfrm_state_init(net
);
3071 rv
= xfrm_policy_init(net
);
3074 rv
= xfrm_sysctl_init(net
);
3077 rv
= flow_cache_init(net
);
3081 /* Initialize the per-net locks here */
3082 spin_lock_init(&net
->xfrm
.xfrm_state_lock
);
3083 spin_lock_init(&net
->xfrm
.xfrm_policy_lock
);
3084 mutex_init(&net
->xfrm
.xfrm_cfg_mutex
);
3089 xfrm_sysctl_fini(net
);
3091 xfrm_policy_fini(net
);
3093 xfrm_state_fini(net
);
3095 xfrm_statistics_fini(net
);
3100 static void __net_exit
xfrm_net_exit(struct net
*net
)
3102 flow_cache_fini(net
);
3103 xfrm_sysctl_fini(net
);
3104 xfrm_policy_fini(net
);
3105 xfrm_state_fini(net
);
3106 xfrm_statistics_fini(net
);
3109 static struct pernet_operations __net_initdata xfrm_net_ops
= {
3110 .init
= xfrm_net_init
,
3111 .exit
= xfrm_net_exit
,
3114 void __init
xfrm_init(void)
3116 flow_cache_hp_init();
3117 register_pernet_subsys(&xfrm_net_ops
);
3118 seqcount_init(&xfrm_policy_hash_generation
);
3122 #ifdef CONFIG_AUDITSYSCALL
3123 static void xfrm_audit_common_policyinfo(struct xfrm_policy
*xp
,
3124 struct audit_buffer
*audit_buf
)
3126 struct xfrm_sec_ctx
*ctx
= xp
->security
;
3127 struct xfrm_selector
*sel
= &xp
->selector
;
3130 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
3131 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
3133 switch (sel
->family
) {
3135 audit_log_format(audit_buf
, " src=%pI4", &sel
->saddr
.a4
);
3136 if (sel
->prefixlen_s
!= 32)
3137 audit_log_format(audit_buf
, " src_prefixlen=%d",
3139 audit_log_format(audit_buf
, " dst=%pI4", &sel
->daddr
.a4
);
3140 if (sel
->prefixlen_d
!= 32)
3141 audit_log_format(audit_buf
, " dst_prefixlen=%d",
3145 audit_log_format(audit_buf
, " src=%pI6", sel
->saddr
.a6
);
3146 if (sel
->prefixlen_s
!= 128)
3147 audit_log_format(audit_buf
, " src_prefixlen=%d",
3149 audit_log_format(audit_buf
, " dst=%pI6", sel
->daddr
.a6
);
3150 if (sel
->prefixlen_d
!= 128)
3151 audit_log_format(audit_buf
, " dst_prefixlen=%d",
3157 void xfrm_audit_policy_add(struct xfrm_policy
*xp
, int result
, bool task_valid
)
3159 struct audit_buffer
*audit_buf
;
3161 audit_buf
= xfrm_audit_start("SPD-add");
3162 if (audit_buf
== NULL
)
3164 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
3165 audit_log_format(audit_buf
, " res=%u", result
);
3166 xfrm_audit_common_policyinfo(xp
, audit_buf
);
3167 audit_log_end(audit_buf
);
3169 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add
);
3171 void xfrm_audit_policy_delete(struct xfrm_policy
*xp
, int result
,
3174 struct audit_buffer
*audit_buf
;
3176 audit_buf
= xfrm_audit_start("SPD-delete");
3177 if (audit_buf
== NULL
)
3179 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
3180 audit_log_format(audit_buf
, " res=%u", result
);
3181 xfrm_audit_common_policyinfo(xp
, audit_buf
);
3182 audit_log_end(audit_buf
);
3184 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete
);
3187 #ifdef CONFIG_XFRM_MIGRATE
3188 static bool xfrm_migrate_selector_match(const struct xfrm_selector
*sel_cmp
,
3189 const struct xfrm_selector
*sel_tgt
)
3191 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
3192 if (sel_tgt
->family
== sel_cmp
->family
&&
3193 xfrm_addr_equal(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
3195 xfrm_addr_equal(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
3197 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
3198 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
3202 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
3209 static struct xfrm_policy
*xfrm_migrate_policy_find(const struct xfrm_selector
*sel
,
3210 u8 dir
, u8 type
, struct net
*net
)
3212 struct xfrm_policy
*pol
, *ret
= NULL
;
3213 struct hlist_head
*chain
;
3216 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
3217 chain
= policy_hash_direct(net
, &sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
3218 hlist_for_each_entry(pol
, chain
, bydst
) {
3219 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3220 pol
->type
== type
) {
3222 priority
= ret
->priority
;
3226 chain
= &net
->xfrm
.policy_inexact
[dir
];
3227 hlist_for_each_entry(pol
, chain
, bydst
) {
3228 if ((pol
->priority
>= priority
) && ret
)
3231 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3232 pol
->type
== type
) {
3240 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
3245 static int migrate_tmpl_match(const struct xfrm_migrate
*m
, const struct xfrm_tmpl
*t
)
3249 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
3250 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
3252 case XFRM_MODE_TUNNEL
:
3253 case XFRM_MODE_BEET
:
3254 if (xfrm_addr_equal(&t
->id
.daddr
, &m
->old_daddr
,
3256 xfrm_addr_equal(&t
->saddr
, &m
->old_saddr
,
3261 case XFRM_MODE_TRANSPORT
:
3262 /* in case of transport mode, template does not store
3263 any IP addresses, hence we just compare mode and
3274 /* update endpoint address(es) of template(s) */
3275 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
3276 struct xfrm_migrate
*m
, int num_migrate
)
3278 struct xfrm_migrate
*mp
;
3281 write_lock_bh(&pol
->lock
);
3282 if (unlikely(pol
->walk
.dead
)) {
3283 /* target policy has been deleted */
3284 write_unlock_bh(&pol
->lock
);
3288 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
3289 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
3290 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
3293 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
&&
3294 pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_BEET
)
3296 /* update endpoints */
3297 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
3298 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
3299 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
3300 sizeof(pol
->xfrm_vec
[i
].saddr
));
3301 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
3303 atomic_inc(&pol
->genid
);
3307 write_unlock_bh(&pol
->lock
);
3315 static int xfrm_migrate_check(const struct xfrm_migrate
*m
, int num_migrate
)
3319 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
3322 for (i
= 0; i
< num_migrate
; i
++) {
3323 if (xfrm_addr_equal(&m
[i
].old_daddr
, &m
[i
].new_daddr
,
3325 xfrm_addr_equal(&m
[i
].old_saddr
, &m
[i
].new_saddr
,
3328 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
3329 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
3332 /* check if there is any duplicated entry */
3333 for (j
= i
+ 1; j
< num_migrate
; j
++) {
3334 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
3335 sizeof(m
[i
].old_daddr
)) &&
3336 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
3337 sizeof(m
[i
].old_saddr
)) &&
3338 m
[i
].proto
== m
[j
].proto
&&
3339 m
[i
].mode
== m
[j
].mode
&&
3340 m
[i
].reqid
== m
[j
].reqid
&&
3341 m
[i
].old_family
== m
[j
].old_family
)
3349 int xfrm_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3350 struct xfrm_migrate
*m
, int num_migrate
,
3351 struct xfrm_kmaddress
*k
, struct net
*net
)
3353 int i
, err
, nx_cur
= 0, nx_new
= 0;
3354 struct xfrm_policy
*pol
= NULL
;
3355 struct xfrm_state
*x
, *xc
;
3356 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
3357 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
3358 struct xfrm_migrate
*mp
;
3360 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
3363 /* Stage 1 - find policy */
3364 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
, net
)) == NULL
) {
3369 /* Stage 2 - find and update state(s) */
3370 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
3371 if ((x
= xfrm_migrate_state_find(mp
, net
))) {
3374 if ((xc
= xfrm_state_migrate(x
, mp
))) {
3384 /* Stage 3 - update policy */
3385 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
3388 /* Stage 4 - delete old state(s) */
3390 xfrm_states_put(x_cur
, nx_cur
);
3391 xfrm_states_delete(x_cur
, nx_cur
);
3394 /* Stage 5 - announce */
3395 km_migrate(sel
, dir
, type
, m
, num_migrate
, k
);
3407 xfrm_states_put(x_cur
, nx_cur
);
3409 xfrm_states_delete(x_new
, nx_new
);
3413 EXPORT_SYMBOL(xfrm_migrate
);