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/cpu.h>
28 #include <linux/audit.h>
33 #ifdef CONFIG_XFRM_STATISTICS
37 #include "xfrm_hash.h"
39 #define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
40 #define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
41 #define XFRM_MAX_QUEUE_LEN 100
44 struct dst_entry
*dst_orig
;
48 static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock
);
49 static struct xfrm_policy_afinfo
const __rcu
*xfrm_policy_afinfo
[AF_INET6
+ 1]
52 static struct kmem_cache
*xfrm_dst_cache __read_mostly
;
53 static __read_mostly seqcount_t xfrm_policy_hash_generation
;
55 static void xfrm_init_pmtu(struct dst_entry
*dst
);
56 static int stale_bundle(struct dst_entry
*dst
);
57 static int xfrm_bundle_ok(struct xfrm_dst
*xdst
);
58 static void xfrm_policy_queue_process(struct timer_list
*t
);
60 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
);
61 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
64 static inline bool xfrm_pol_hold_rcu(struct xfrm_policy
*policy
)
66 return refcount_inc_not_zero(&policy
->refcnt
);
70 __xfrm4_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
72 const struct flowi4
*fl4
= &fl
->u
.ip4
;
74 return addr4_match(fl4
->daddr
, sel
->daddr
.a4
, sel
->prefixlen_d
) &&
75 addr4_match(fl4
->saddr
, sel
->saddr
.a4
, sel
->prefixlen_s
) &&
76 !((xfrm_flowi_dport(fl
, &fl4
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
77 !((xfrm_flowi_sport(fl
, &fl4
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
78 (fl4
->flowi4_proto
== sel
->proto
|| !sel
->proto
) &&
79 (fl4
->flowi4_oif
== sel
->ifindex
|| !sel
->ifindex
);
83 __xfrm6_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
85 const struct flowi6
*fl6
= &fl
->u
.ip6
;
87 return addr_match(&fl6
->daddr
, &sel
->daddr
, sel
->prefixlen_d
) &&
88 addr_match(&fl6
->saddr
, &sel
->saddr
, sel
->prefixlen_s
) &&
89 !((xfrm_flowi_dport(fl
, &fl6
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
90 !((xfrm_flowi_sport(fl
, &fl6
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
91 (fl6
->flowi6_proto
== sel
->proto
|| !sel
->proto
) &&
92 (fl6
->flowi6_oif
== sel
->ifindex
|| !sel
->ifindex
);
95 bool xfrm_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
,
96 unsigned short family
)
100 return __xfrm4_selector_match(sel
, fl
);
102 return __xfrm6_selector_match(sel
, fl
);
107 static const struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
109 const struct xfrm_policy_afinfo
*afinfo
;
111 if (unlikely(family
>= ARRAY_SIZE(xfrm_policy_afinfo
)))
114 afinfo
= rcu_dereference(xfrm_policy_afinfo
[family
]);
115 if (unlikely(!afinfo
))
120 struct dst_entry
*__xfrm_dst_lookup(struct net
*net
, int tos
, int oif
,
121 const xfrm_address_t
*saddr
,
122 const xfrm_address_t
*daddr
,
123 int family
, u32 mark
)
125 const struct xfrm_policy_afinfo
*afinfo
;
126 struct dst_entry
*dst
;
128 afinfo
= xfrm_policy_get_afinfo(family
);
129 if (unlikely(afinfo
== NULL
))
130 return ERR_PTR(-EAFNOSUPPORT
);
132 dst
= afinfo
->dst_lookup(net
, tos
, oif
, saddr
, daddr
, mark
);
138 EXPORT_SYMBOL(__xfrm_dst_lookup
);
140 static inline struct dst_entry
*xfrm_dst_lookup(struct xfrm_state
*x
,
142 xfrm_address_t
*prev_saddr
,
143 xfrm_address_t
*prev_daddr
,
144 int family
, u32 mark
)
146 struct net
*net
= xs_net(x
);
147 xfrm_address_t
*saddr
= &x
->props
.saddr
;
148 xfrm_address_t
*daddr
= &x
->id
.daddr
;
149 struct dst_entry
*dst
;
151 if (x
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
) {
155 if (x
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
) {
160 dst
= __xfrm_dst_lookup(net
, tos
, oif
, saddr
, daddr
, family
, mark
);
163 if (prev_saddr
!= saddr
)
164 memcpy(prev_saddr
, saddr
, sizeof(*prev_saddr
));
165 if (prev_daddr
!= daddr
)
166 memcpy(prev_daddr
, daddr
, sizeof(*prev_daddr
));
172 static inline unsigned long make_jiffies(long secs
)
174 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
175 return MAX_SCHEDULE_TIMEOUT
-1;
180 static void xfrm_policy_timer(struct timer_list
*t
)
182 struct xfrm_policy
*xp
= from_timer(xp
, t
, timer
);
183 unsigned long now
= get_seconds();
184 long next
= LONG_MAX
;
188 read_lock(&xp
->lock
);
190 if (unlikely(xp
->walk
.dead
))
193 dir
= xfrm_policy_id2dir(xp
->index
);
195 if (xp
->lft
.hard_add_expires_seconds
) {
196 long tmo
= xp
->lft
.hard_add_expires_seconds
+
197 xp
->curlft
.add_time
- now
;
203 if (xp
->lft
.hard_use_expires_seconds
) {
204 long tmo
= xp
->lft
.hard_use_expires_seconds
+
205 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
211 if (xp
->lft
.soft_add_expires_seconds
) {
212 long tmo
= xp
->lft
.soft_add_expires_seconds
+
213 xp
->curlft
.add_time
- now
;
216 tmo
= XFRM_KM_TIMEOUT
;
221 if (xp
->lft
.soft_use_expires_seconds
) {
222 long tmo
= xp
->lft
.soft_use_expires_seconds
+
223 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
226 tmo
= XFRM_KM_TIMEOUT
;
233 km_policy_expired(xp
, dir
, 0, 0);
234 if (next
!= LONG_MAX
&&
235 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
239 read_unlock(&xp
->lock
);
244 read_unlock(&xp
->lock
);
245 if (!xfrm_policy_delete(xp
, dir
))
246 km_policy_expired(xp
, dir
, 1, 0);
250 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
254 struct xfrm_policy
*xfrm_policy_alloc(struct net
*net
, gfp_t gfp
)
256 struct xfrm_policy
*policy
;
258 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
261 write_pnet(&policy
->xp_net
, net
);
262 INIT_LIST_HEAD(&policy
->walk
.all
);
263 INIT_HLIST_NODE(&policy
->bydst
);
264 INIT_HLIST_NODE(&policy
->byidx
);
265 rwlock_init(&policy
->lock
);
266 refcount_set(&policy
->refcnt
, 1);
267 skb_queue_head_init(&policy
->polq
.hold_queue
);
268 timer_setup(&policy
->timer
, xfrm_policy_timer
, 0);
269 timer_setup(&policy
->polq
.hold_timer
,
270 xfrm_policy_queue_process
, 0);
274 EXPORT_SYMBOL(xfrm_policy_alloc
);
276 static void xfrm_policy_destroy_rcu(struct rcu_head
*head
)
278 struct xfrm_policy
*policy
= container_of(head
, struct xfrm_policy
, rcu
);
280 security_xfrm_policy_free(policy
->security
);
284 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
286 void xfrm_policy_destroy(struct xfrm_policy
*policy
)
288 BUG_ON(!policy
->walk
.dead
);
290 if (del_timer(&policy
->timer
) || del_timer(&policy
->polq
.hold_timer
))
293 call_rcu(&policy
->rcu
, xfrm_policy_destroy_rcu
);
295 EXPORT_SYMBOL(xfrm_policy_destroy
);
297 /* Rule must be locked. Release descendant resources, announce
298 * entry dead. The rule must be unlinked from lists to the moment.
301 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
303 policy
->walk
.dead
= 1;
305 atomic_inc(&policy
->genid
);
307 if (del_timer(&policy
->polq
.hold_timer
))
308 xfrm_pol_put(policy
);
309 skb_queue_purge(&policy
->polq
.hold_queue
);
311 if (del_timer(&policy
->timer
))
312 xfrm_pol_put(policy
);
314 xfrm_pol_put(policy
);
317 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
319 static inline unsigned int idx_hash(struct net
*net
, u32 index
)
321 return __idx_hash(index
, net
->xfrm
.policy_idx_hmask
);
324 /* calculate policy hash thresholds */
325 static void __get_hash_thresh(struct net
*net
,
326 unsigned short family
, int dir
,
327 u8
*dbits
, u8
*sbits
)
331 *dbits
= net
->xfrm
.policy_bydst
[dir
].dbits4
;
332 *sbits
= net
->xfrm
.policy_bydst
[dir
].sbits4
;
336 *dbits
= net
->xfrm
.policy_bydst
[dir
].dbits6
;
337 *sbits
= net
->xfrm
.policy_bydst
[dir
].sbits6
;
346 static struct hlist_head
*policy_hash_bysel(struct net
*net
,
347 const struct xfrm_selector
*sel
,
348 unsigned short family
, int dir
)
350 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
355 __get_hash_thresh(net
, family
, dir
, &dbits
, &sbits
);
356 hash
= __sel_hash(sel
, family
, hmask
, dbits
, sbits
);
358 if (hash
== hmask
+ 1)
359 return &net
->xfrm
.policy_inexact
[dir
];
361 return rcu_dereference_check(net
->xfrm
.policy_bydst
[dir
].table
,
362 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
)) + hash
;
365 static struct hlist_head
*policy_hash_direct(struct net
*net
,
366 const xfrm_address_t
*daddr
,
367 const xfrm_address_t
*saddr
,
368 unsigned short family
, int dir
)
370 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
375 __get_hash_thresh(net
, family
, dir
, &dbits
, &sbits
);
376 hash
= __addr_hash(daddr
, saddr
, family
, hmask
, dbits
, sbits
);
378 return rcu_dereference_check(net
->xfrm
.policy_bydst
[dir
].table
,
379 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
)) + hash
;
382 static void xfrm_dst_hash_transfer(struct net
*net
,
383 struct hlist_head
*list
,
384 struct hlist_head
*ndsttable
,
385 unsigned int nhashmask
,
388 struct hlist_node
*tmp
, *entry0
= NULL
;
389 struct xfrm_policy
*pol
;
395 hlist_for_each_entry_safe(pol
, tmp
, list
, bydst
) {
398 __get_hash_thresh(net
, pol
->family
, dir
, &dbits
, &sbits
);
399 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
400 pol
->family
, nhashmask
, dbits
, sbits
);
402 hlist_del_rcu(&pol
->bydst
);
403 hlist_add_head_rcu(&pol
->bydst
, ndsttable
+ h
);
408 hlist_del_rcu(&pol
->bydst
);
409 hlist_add_behind_rcu(&pol
->bydst
, entry0
);
411 entry0
= &pol
->bydst
;
413 if (!hlist_empty(list
)) {
419 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
420 struct hlist_head
*nidxtable
,
421 unsigned int nhashmask
)
423 struct hlist_node
*tmp
;
424 struct xfrm_policy
*pol
;
426 hlist_for_each_entry_safe(pol
, tmp
, list
, byidx
) {
429 h
= __idx_hash(pol
->index
, nhashmask
);
430 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
434 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
436 return ((old_hmask
+ 1) << 1) - 1;
439 static void xfrm_bydst_resize(struct net
*net
, int dir
)
441 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
442 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
443 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
444 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
445 struct hlist_head
*odst
;
451 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
452 write_seqcount_begin(&xfrm_policy_hash_generation
);
454 odst
= rcu_dereference_protected(net
->xfrm
.policy_bydst
[dir
].table
,
455 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
457 odst
= rcu_dereference_protected(net
->xfrm
.policy_bydst
[dir
].table
,
458 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
460 for (i
= hmask
; i
>= 0; i
--)
461 xfrm_dst_hash_transfer(net
, odst
+ i
, ndst
, nhashmask
, dir
);
463 rcu_assign_pointer(net
->xfrm
.policy_bydst
[dir
].table
, ndst
);
464 net
->xfrm
.policy_bydst
[dir
].hmask
= nhashmask
;
466 write_seqcount_end(&xfrm_policy_hash_generation
);
467 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
471 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
474 static void xfrm_byidx_resize(struct net
*net
, int total
)
476 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
477 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
478 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
479 struct hlist_head
*oidx
= net
->xfrm
.policy_byidx
;
480 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
486 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
488 for (i
= hmask
; i
>= 0; i
--)
489 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
491 net
->xfrm
.policy_byidx
= nidx
;
492 net
->xfrm
.policy_idx_hmask
= nhashmask
;
494 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
496 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
499 static inline int xfrm_bydst_should_resize(struct net
*net
, int dir
, int *total
)
501 unsigned int cnt
= net
->xfrm
.policy_count
[dir
];
502 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
507 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
514 static inline int xfrm_byidx_should_resize(struct net
*net
, int total
)
516 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
518 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
525 void xfrm_spd_getinfo(struct net
*net
, struct xfrmk_spdinfo
*si
)
527 si
->incnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
];
528 si
->outcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
];
529 si
->fwdcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
];
530 si
->inscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
531 si
->outscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
532 si
->fwdscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
533 si
->spdhcnt
= net
->xfrm
.policy_idx_hmask
;
534 si
->spdhmcnt
= xfrm_policy_hashmax
;
536 EXPORT_SYMBOL(xfrm_spd_getinfo
);
538 static DEFINE_MUTEX(hash_resize_mutex
);
539 static void xfrm_hash_resize(struct work_struct
*work
)
541 struct net
*net
= container_of(work
, struct net
, xfrm
.policy_hash_work
);
544 mutex_lock(&hash_resize_mutex
);
547 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
548 if (xfrm_bydst_should_resize(net
, dir
, &total
))
549 xfrm_bydst_resize(net
, dir
);
551 if (xfrm_byidx_should_resize(net
, total
))
552 xfrm_byidx_resize(net
, total
);
554 mutex_unlock(&hash_resize_mutex
);
557 static void xfrm_hash_rebuild(struct work_struct
*work
)
559 struct net
*net
= container_of(work
, struct net
,
560 xfrm
.policy_hthresh
.work
);
562 struct xfrm_policy
*pol
;
563 struct xfrm_policy
*policy
;
564 struct hlist_head
*chain
;
565 struct hlist_head
*odst
;
566 struct hlist_node
*newpos
;
570 u8 lbits4
, rbits4
, lbits6
, rbits6
;
572 mutex_lock(&hash_resize_mutex
);
574 /* read selector prefixlen thresholds */
576 seq
= read_seqbegin(&net
->xfrm
.policy_hthresh
.lock
);
578 lbits4
= net
->xfrm
.policy_hthresh
.lbits4
;
579 rbits4
= net
->xfrm
.policy_hthresh
.rbits4
;
580 lbits6
= net
->xfrm
.policy_hthresh
.lbits6
;
581 rbits6
= net
->xfrm
.policy_hthresh
.rbits6
;
582 } while (read_seqretry(&net
->xfrm
.policy_hthresh
.lock
, seq
));
584 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
586 /* reset the bydst and inexact table in all directions */
587 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
588 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
589 hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
590 odst
= net
->xfrm
.policy_bydst
[dir
].table
;
591 for (i
= hmask
; i
>= 0; i
--)
592 INIT_HLIST_HEAD(odst
+ i
);
593 if ((dir
& XFRM_POLICY_MASK
) == XFRM_POLICY_OUT
) {
594 /* dir out => dst = remote, src = local */
595 net
->xfrm
.policy_bydst
[dir
].dbits4
= rbits4
;
596 net
->xfrm
.policy_bydst
[dir
].sbits4
= lbits4
;
597 net
->xfrm
.policy_bydst
[dir
].dbits6
= rbits6
;
598 net
->xfrm
.policy_bydst
[dir
].sbits6
= lbits6
;
600 /* dir in/fwd => dst = local, src = remote */
601 net
->xfrm
.policy_bydst
[dir
].dbits4
= lbits4
;
602 net
->xfrm
.policy_bydst
[dir
].sbits4
= rbits4
;
603 net
->xfrm
.policy_bydst
[dir
].dbits6
= lbits6
;
604 net
->xfrm
.policy_bydst
[dir
].sbits6
= rbits6
;
608 /* re-insert all policies by order of creation */
609 list_for_each_entry_reverse(policy
, &net
->xfrm
.policy_all
, walk
.all
) {
610 if (policy
->walk
.dead
||
611 xfrm_policy_id2dir(policy
->index
) >= XFRM_POLICY_MAX
) {
612 /* skip socket policies */
616 chain
= policy_hash_bysel(net
, &policy
->selector
,
618 xfrm_policy_id2dir(policy
->index
));
619 hlist_for_each_entry(pol
, chain
, bydst
) {
620 if (policy
->priority
>= pol
->priority
)
621 newpos
= &pol
->bydst
;
626 hlist_add_behind_rcu(&policy
->bydst
, newpos
);
628 hlist_add_head_rcu(&policy
->bydst
, chain
);
631 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
633 mutex_unlock(&hash_resize_mutex
);
636 void xfrm_policy_hash_rebuild(struct net
*net
)
638 schedule_work(&net
->xfrm
.policy_hthresh
.work
);
640 EXPORT_SYMBOL(xfrm_policy_hash_rebuild
);
642 /* Generate new index... KAME seems to generate them ordered by cost
643 * of an absolute inpredictability of ordering of rules. This will not pass. */
644 static u32
xfrm_gen_index(struct net
*net
, int dir
, u32 index
)
646 static u32 idx_generator
;
649 struct hlist_head
*list
;
650 struct xfrm_policy
*p
;
655 idx
= (idx_generator
| dir
);
664 list
= net
->xfrm
.policy_byidx
+ idx_hash(net
, idx
);
666 hlist_for_each_entry(p
, list
, byidx
) {
667 if (p
->index
== idx
) {
677 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
679 u32
*p1
= (u32
*) s1
;
680 u32
*p2
= (u32
*) s2
;
681 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
684 for (i
= 0; i
< len
; i
++) {
692 static void xfrm_policy_requeue(struct xfrm_policy
*old
,
693 struct xfrm_policy
*new)
695 struct xfrm_policy_queue
*pq
= &old
->polq
;
696 struct sk_buff_head list
;
698 if (skb_queue_empty(&pq
->hold_queue
))
701 __skb_queue_head_init(&list
);
703 spin_lock_bh(&pq
->hold_queue
.lock
);
704 skb_queue_splice_init(&pq
->hold_queue
, &list
);
705 if (del_timer(&pq
->hold_timer
))
707 spin_unlock_bh(&pq
->hold_queue
.lock
);
711 spin_lock_bh(&pq
->hold_queue
.lock
);
712 skb_queue_splice(&list
, &pq
->hold_queue
);
713 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
714 if (!mod_timer(&pq
->hold_timer
, jiffies
))
716 spin_unlock_bh(&pq
->hold_queue
.lock
);
719 static bool xfrm_policy_mark_match(struct xfrm_policy
*policy
,
720 struct xfrm_policy
*pol
)
722 u32 mark
= policy
->mark
.v
& policy
->mark
.m
;
724 if (policy
->mark
.v
== pol
->mark
.v
&& policy
->mark
.m
== pol
->mark
.m
)
727 if ((mark
& pol
->mark
.m
) == pol
->mark
.v
&&
728 policy
->priority
== pol
->priority
)
734 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
736 struct net
*net
= xp_net(policy
);
737 struct xfrm_policy
*pol
;
738 struct xfrm_policy
*delpol
;
739 struct hlist_head
*chain
;
740 struct hlist_node
*newpos
;
742 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
743 chain
= policy_hash_bysel(net
, &policy
->selector
, policy
->family
, dir
);
746 hlist_for_each_entry(pol
, chain
, bydst
) {
747 if (pol
->type
== policy
->type
&&
748 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
749 xfrm_policy_mark_match(policy
, pol
) &&
750 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
753 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
757 if (policy
->priority
> pol
->priority
)
759 } else if (policy
->priority
>= pol
->priority
) {
760 newpos
= &pol
->bydst
;
767 hlist_add_behind_rcu(&policy
->bydst
, newpos
);
769 hlist_add_head_rcu(&policy
->bydst
, chain
);
770 __xfrm_policy_link(policy
, dir
);
772 /* After previous checking, family can either be AF_INET or AF_INET6 */
773 if (policy
->family
== AF_INET
)
774 rt_genid_bump_ipv4(net
);
776 rt_genid_bump_ipv6(net
);
779 xfrm_policy_requeue(delpol
, policy
);
780 __xfrm_policy_unlink(delpol
, dir
);
782 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(net
, dir
, policy
->index
);
783 hlist_add_head(&policy
->byidx
, net
->xfrm
.policy_byidx
+idx_hash(net
, policy
->index
));
784 policy
->curlft
.add_time
= get_seconds();
785 policy
->curlft
.use_time
= 0;
786 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
787 xfrm_pol_hold(policy
);
788 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
791 xfrm_policy_kill(delpol
);
792 else if (xfrm_bydst_should_resize(net
, dir
, NULL
))
793 schedule_work(&net
->xfrm
.policy_hash_work
);
797 EXPORT_SYMBOL(xfrm_policy_insert
);
799 struct xfrm_policy
*xfrm_policy_bysel_ctx(struct net
*net
, u32 mark
, u8 type
,
800 int dir
, struct xfrm_selector
*sel
,
801 struct xfrm_sec_ctx
*ctx
, int delete,
804 struct xfrm_policy
*pol
, *ret
;
805 struct hlist_head
*chain
;
808 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
809 chain
= policy_hash_bysel(net
, sel
, sel
->family
, dir
);
811 hlist_for_each_entry(pol
, chain
, bydst
) {
812 if (pol
->type
== type
&&
813 (mark
& pol
->mark
.m
) == pol
->mark
.v
&&
814 !selector_cmp(sel
, &pol
->selector
) &&
815 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
818 *err
= security_xfrm_policy_delete(
821 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
824 __xfrm_policy_unlink(pol
, dir
);
830 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
833 xfrm_policy_kill(ret
);
836 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
838 struct xfrm_policy
*xfrm_policy_byid(struct net
*net
, u32 mark
, u8 type
,
839 int dir
, u32 id
, int delete, int *err
)
841 struct xfrm_policy
*pol
, *ret
;
842 struct hlist_head
*chain
;
845 if (xfrm_policy_id2dir(id
) != dir
)
849 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
850 chain
= net
->xfrm
.policy_byidx
+ idx_hash(net
, id
);
852 hlist_for_each_entry(pol
, chain
, byidx
) {
853 if (pol
->type
== type
&& pol
->index
== id
&&
854 (mark
& pol
->mark
.m
) == pol
->mark
.v
) {
857 *err
= security_xfrm_policy_delete(
860 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
863 __xfrm_policy_unlink(pol
, dir
);
869 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
872 xfrm_policy_kill(ret
);
875 EXPORT_SYMBOL(xfrm_policy_byid
);
877 #ifdef CONFIG_SECURITY_NETWORK_XFRM
879 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
883 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
884 struct xfrm_policy
*pol
;
887 hlist_for_each_entry(pol
,
888 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
889 if (pol
->type
!= type
)
891 err
= security_xfrm_policy_delete(pol
->security
);
893 xfrm_audit_policy_delete(pol
, 0, task_valid
);
897 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
898 hlist_for_each_entry(pol
,
899 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
901 if (pol
->type
!= type
)
903 err
= security_xfrm_policy_delete(
906 xfrm_audit_policy_delete(pol
, 0,
917 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
923 int xfrm_policy_flush(struct net
*net
, u8 type
, bool task_valid
)
925 int dir
, err
= 0, cnt
= 0;
927 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
929 err
= xfrm_policy_flush_secctx_check(net
, type
, task_valid
);
933 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
934 struct xfrm_policy
*pol
;
938 hlist_for_each_entry(pol
,
939 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
940 if (pol
->type
!= type
)
942 __xfrm_policy_unlink(pol
, dir
);
943 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
946 xfrm_audit_policy_delete(pol
, 1, task_valid
);
948 xfrm_policy_kill(pol
);
950 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
954 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
956 hlist_for_each_entry(pol
,
957 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
959 if (pol
->type
!= type
)
961 __xfrm_policy_unlink(pol
, dir
);
962 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
965 xfrm_audit_policy_delete(pol
, 1, task_valid
);
966 xfrm_policy_kill(pol
);
968 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
977 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
980 EXPORT_SYMBOL(xfrm_policy_flush
);
982 int xfrm_policy_walk(struct net
*net
, struct xfrm_policy_walk
*walk
,
983 int (*func
)(struct xfrm_policy
*, int, int, void*),
986 struct xfrm_policy
*pol
;
987 struct xfrm_policy_walk_entry
*x
;
990 if (walk
->type
>= XFRM_POLICY_TYPE_MAX
&&
991 walk
->type
!= XFRM_POLICY_TYPE_ANY
)
994 if (list_empty(&walk
->walk
.all
) && walk
->seq
!= 0)
997 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
998 if (list_empty(&walk
->walk
.all
))
999 x
= list_first_entry(&net
->xfrm
.policy_all
, struct xfrm_policy_walk_entry
, all
);
1001 x
= list_first_entry(&walk
->walk
.all
,
1002 struct xfrm_policy_walk_entry
, all
);
1004 list_for_each_entry_from(x
, &net
->xfrm
.policy_all
, all
) {
1007 pol
= container_of(x
, struct xfrm_policy
, walk
);
1008 if (walk
->type
!= XFRM_POLICY_TYPE_ANY
&&
1009 walk
->type
!= pol
->type
)
1011 error
= func(pol
, xfrm_policy_id2dir(pol
->index
),
1014 list_move_tail(&walk
->walk
.all
, &x
->all
);
1019 if (walk
->seq
== 0) {
1023 list_del_init(&walk
->walk
.all
);
1025 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1028 EXPORT_SYMBOL(xfrm_policy_walk
);
1030 void xfrm_policy_walk_init(struct xfrm_policy_walk
*walk
, u8 type
)
1032 INIT_LIST_HEAD(&walk
->walk
.all
);
1033 walk
->walk
.dead
= 1;
1037 EXPORT_SYMBOL(xfrm_policy_walk_init
);
1039 void xfrm_policy_walk_done(struct xfrm_policy_walk
*walk
, struct net
*net
)
1041 if (list_empty(&walk
->walk
.all
))
1044 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
); /*FIXME where is net? */
1045 list_del(&walk
->walk
.all
);
1046 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1048 EXPORT_SYMBOL(xfrm_policy_walk_done
);
1051 * Find policy to apply to this flow.
1053 * Returns 0 if policy found, else an -errno.
1055 static int xfrm_policy_match(const struct xfrm_policy
*pol
,
1056 const struct flowi
*fl
,
1057 u8 type
, u16 family
, int dir
)
1059 const struct xfrm_selector
*sel
= &pol
->selector
;
1063 if (pol
->family
!= family
||
1064 (fl
->flowi_mark
& pol
->mark
.m
) != pol
->mark
.v
||
1068 match
= xfrm_selector_match(sel
, fl
, family
);
1070 ret
= security_xfrm_policy_lookup(pol
->security
, fl
->flowi_secid
,
1076 static struct xfrm_policy
*xfrm_policy_lookup_bytype(struct net
*net
, u8 type
,
1077 const struct flowi
*fl
,
1081 struct xfrm_policy
*pol
, *ret
;
1082 const xfrm_address_t
*daddr
, *saddr
;
1083 struct hlist_head
*chain
;
1084 unsigned int sequence
;
1087 daddr
= xfrm_flowi_daddr(fl
, family
);
1088 saddr
= xfrm_flowi_saddr(fl
, family
);
1089 if (unlikely(!daddr
|| !saddr
))
1095 sequence
= read_seqcount_begin(&xfrm_policy_hash_generation
);
1096 chain
= policy_hash_direct(net
, daddr
, saddr
, family
, dir
);
1097 } while (read_seqcount_retry(&xfrm_policy_hash_generation
, sequence
));
1101 hlist_for_each_entry_rcu(pol
, chain
, bydst
) {
1102 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1112 priority
= ret
->priority
;
1116 chain
= &net
->xfrm
.policy_inexact
[dir
];
1117 hlist_for_each_entry_rcu(pol
, chain
, bydst
) {
1118 if ((pol
->priority
>= priority
) && ret
)
1121 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1135 if (read_seqcount_retry(&xfrm_policy_hash_generation
, sequence
))
1138 if (ret
&& !xfrm_pol_hold_rcu(ret
))
1146 static struct xfrm_policy
*
1147 xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
)
1149 #ifdef CONFIG_XFRM_SUB_POLICY
1150 struct xfrm_policy
*pol
;
1152 pol
= xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
1156 return xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
1159 static struct xfrm_policy
*xfrm_sk_policy_lookup(const struct sock
*sk
, int dir
,
1160 const struct flowi
*fl
, u16 family
)
1162 struct xfrm_policy
*pol
;
1166 pol
= rcu_dereference(sk
->sk_policy
[dir
]);
1171 if (pol
->family
!= family
) {
1176 match
= xfrm_selector_match(&pol
->selector
, fl
, family
);
1178 if ((sk
->sk_mark
& pol
->mark
.m
) != pol
->mark
.v
) {
1182 err
= security_xfrm_policy_lookup(pol
->security
,
1186 if (!xfrm_pol_hold_rcu(pol
))
1188 } else if (err
== -ESRCH
) {
1201 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1203 struct net
*net
= xp_net(pol
);
1205 list_add(&pol
->walk
.all
, &net
->xfrm
.policy_all
);
1206 net
->xfrm
.policy_count
[dir
]++;
1210 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1213 struct net
*net
= xp_net(pol
);
1215 if (list_empty(&pol
->walk
.all
))
1218 /* Socket policies are not hashed. */
1219 if (!hlist_unhashed(&pol
->bydst
)) {
1220 hlist_del_rcu(&pol
->bydst
);
1221 hlist_del(&pol
->byidx
);
1224 list_del_init(&pol
->walk
.all
);
1225 net
->xfrm
.policy_count
[dir
]--;
1230 static void xfrm_sk_policy_link(struct xfrm_policy
*pol
, int dir
)
1232 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+ dir
);
1235 static void xfrm_sk_policy_unlink(struct xfrm_policy
*pol
, int dir
)
1237 __xfrm_policy_unlink(pol
, XFRM_POLICY_MAX
+ dir
);
1240 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1242 struct net
*net
= xp_net(pol
);
1244 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1245 pol
= __xfrm_policy_unlink(pol
, dir
);
1246 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1248 xfrm_policy_kill(pol
);
1253 EXPORT_SYMBOL(xfrm_policy_delete
);
1255 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1257 struct net
*net
= sock_net(sk
);
1258 struct xfrm_policy
*old_pol
;
1260 #ifdef CONFIG_XFRM_SUB_POLICY
1261 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1265 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1266 old_pol
= rcu_dereference_protected(sk
->sk_policy
[dir
],
1267 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
1269 pol
->curlft
.add_time
= get_seconds();
1270 pol
->index
= xfrm_gen_index(net
, XFRM_POLICY_MAX
+dir
, 0);
1271 xfrm_sk_policy_link(pol
, dir
);
1273 rcu_assign_pointer(sk
->sk_policy
[dir
], pol
);
1276 xfrm_policy_requeue(old_pol
, pol
);
1278 /* Unlinking succeeds always. This is the only function
1279 * allowed to delete or replace socket policy.
1281 xfrm_sk_policy_unlink(old_pol
, dir
);
1283 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1286 xfrm_policy_kill(old_pol
);
1291 static struct xfrm_policy
*clone_policy(const struct xfrm_policy
*old
, int dir
)
1293 struct xfrm_policy
*newp
= xfrm_policy_alloc(xp_net(old
), GFP_ATOMIC
);
1294 struct net
*net
= xp_net(old
);
1297 newp
->selector
= old
->selector
;
1298 if (security_xfrm_policy_clone(old
->security
,
1301 return NULL
; /* ENOMEM */
1303 newp
->lft
= old
->lft
;
1304 newp
->curlft
= old
->curlft
;
1305 newp
->mark
= old
->mark
;
1306 newp
->action
= old
->action
;
1307 newp
->flags
= old
->flags
;
1308 newp
->xfrm_nr
= old
->xfrm_nr
;
1309 newp
->index
= old
->index
;
1310 newp
->type
= old
->type
;
1311 newp
->family
= old
->family
;
1312 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1313 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1314 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1315 xfrm_sk_policy_link(newp
, dir
);
1316 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1322 int __xfrm_sk_clone_policy(struct sock
*sk
, const struct sock
*osk
)
1324 const struct xfrm_policy
*p
;
1325 struct xfrm_policy
*np
;
1329 for (i
= 0; i
< 2; i
++) {
1330 p
= rcu_dereference(osk
->sk_policy
[i
]);
1332 np
= clone_policy(p
, i
);
1333 if (unlikely(!np
)) {
1337 rcu_assign_pointer(sk
->sk_policy
[i
], np
);
1345 xfrm_get_saddr(struct net
*net
, int oif
, xfrm_address_t
*local
,
1346 xfrm_address_t
*remote
, unsigned short family
, u32 mark
)
1349 const struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1351 if (unlikely(afinfo
== NULL
))
1353 err
= afinfo
->get_saddr(net
, oif
, local
, remote
, mark
);
1358 /* Resolve list of templates for the flow, given policy. */
1361 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, const struct flowi
*fl
,
1362 struct xfrm_state
**xfrm
, unsigned short family
)
1364 struct net
*net
= xp_net(policy
);
1367 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1368 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1371 for (nx
= 0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1372 struct xfrm_state
*x
;
1373 xfrm_address_t
*remote
= daddr
;
1374 xfrm_address_t
*local
= saddr
;
1375 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1377 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1378 tmpl
->mode
== XFRM_MODE_BEET
) {
1379 remote
= &tmpl
->id
.daddr
;
1380 local
= &tmpl
->saddr
;
1381 if (xfrm_addr_any(local
, tmpl
->encap_family
)) {
1382 error
= xfrm_get_saddr(net
, fl
->flowi_oif
,
1384 tmpl
->encap_family
, 0);
1391 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1393 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1400 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1403 } else if (error
== -ESRCH
) {
1407 if (!tmpl
->optional
)
1413 for (nx
--; nx
>= 0; nx
--)
1414 xfrm_state_put(xfrm
[nx
]);
1419 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, const struct flowi
*fl
,
1420 struct xfrm_state
**xfrm
, unsigned short family
)
1422 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1423 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1429 for (i
= 0; i
< npols
; i
++) {
1430 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1435 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1443 /* found states are sorted for outbound processing */
1445 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1450 for (cnx
--; cnx
>= 0; cnx
--)
1451 xfrm_state_put(tpp
[cnx
]);
1456 static int xfrm_get_tos(const struct flowi
*fl
, int family
)
1458 const struct xfrm_policy_afinfo
*afinfo
;
1461 afinfo
= xfrm_policy_get_afinfo(family
);
1465 tos
= afinfo
->get_tos(fl
);
1472 static inline struct xfrm_dst
*xfrm_alloc_dst(struct net
*net
, int family
)
1474 const struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1475 struct dst_ops
*dst_ops
;
1476 struct xfrm_dst
*xdst
;
1479 return ERR_PTR(-EINVAL
);
1483 dst_ops
= &net
->xfrm
.xfrm4_dst_ops
;
1485 #if IS_ENABLED(CONFIG_IPV6)
1487 dst_ops
= &net
->xfrm
.xfrm6_dst_ops
;
1493 xdst
= dst_alloc(dst_ops
, NULL
, 1, DST_OBSOLETE_NONE
, 0);
1496 struct dst_entry
*dst
= &xdst
->u
.dst
;
1498 memset(dst
+ 1, 0, sizeof(*xdst
) - sizeof(*dst
));
1500 xdst
= ERR_PTR(-ENOBUFS
);
1507 static inline int xfrm_init_path(struct xfrm_dst
*path
, struct dst_entry
*dst
,
1510 const struct xfrm_policy_afinfo
*afinfo
=
1511 xfrm_policy_get_afinfo(dst
->ops
->family
);
1517 err
= afinfo
->init_path(path
, dst
, nfheader_len
);
1524 static inline int xfrm_fill_dst(struct xfrm_dst
*xdst
, struct net_device
*dev
,
1525 const struct flowi
*fl
)
1527 const struct xfrm_policy_afinfo
*afinfo
=
1528 xfrm_policy_get_afinfo(xdst
->u
.dst
.ops
->family
);
1534 err
= afinfo
->fill_dst(xdst
, dev
, fl
);
1542 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1543 * all the metrics... Shortly, bundle a bundle.
1546 static struct dst_entry
*xfrm_bundle_create(struct xfrm_policy
*policy
,
1547 struct xfrm_state
**xfrm
, int nx
,
1548 const struct flowi
*fl
,
1549 struct dst_entry
*dst
)
1551 struct net
*net
= xp_net(policy
);
1552 unsigned long now
= jiffies
;
1553 struct net_device
*dev
;
1554 struct xfrm_mode
*inner_mode
;
1555 struct dst_entry
*dst_prev
= NULL
;
1556 struct dst_entry
*dst0
= NULL
;
1560 int nfheader_len
= 0;
1561 int trailer_len
= 0;
1563 int family
= policy
->selector
.family
;
1564 xfrm_address_t saddr
, daddr
;
1566 xfrm_flowi_addr_get(fl
, &saddr
, &daddr
, family
);
1568 tos
= xfrm_get_tos(fl
, family
);
1572 for (; i
< nx
; i
++) {
1573 struct xfrm_dst
*xdst
= xfrm_alloc_dst(net
, family
);
1574 struct dst_entry
*dst1
= &xdst
->u
.dst
;
1576 err
= PTR_ERR(xdst
);
1585 /* Ref count is taken during xfrm_alloc_dst()
1586 * No need to do dst_clone() on dst1
1588 dst_prev
->child
= dst1
;
1590 if (xfrm
[i
]->sel
.family
== AF_UNSPEC
) {
1591 inner_mode
= xfrm_ip2inner_mode(xfrm
[i
],
1592 xfrm_af2proto(family
));
1594 err
= -EAFNOSUPPORT
;
1599 inner_mode
= xfrm
[i
]->inner_mode
;
1602 dst_copy_metrics(dst1
, dst
);
1604 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1605 family
= xfrm
[i
]->props
.family
;
1606 dst
= xfrm_dst_lookup(xfrm
[i
], tos
, fl
->flowi_oif
,
1607 &saddr
, &daddr
, family
,
1608 xfrm
[i
]->props
.output_mark
);
1615 dst1
->xfrm
= xfrm
[i
];
1616 xdst
->xfrm_genid
= xfrm
[i
]->genid
;
1618 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
1619 dst1
->flags
|= DST_HOST
;
1620 dst1
->lastuse
= now
;
1622 dst1
->input
= dst_discard
;
1623 dst1
->output
= inner_mode
->afinfo
->output
;
1625 dst1
->next
= dst_prev
;
1628 header_len
+= xfrm
[i
]->props
.header_len
;
1629 if (xfrm
[i
]->type
->flags
& XFRM_TYPE_NON_FRAGMENT
)
1630 nfheader_len
+= xfrm
[i
]->props
.header_len
;
1631 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
1634 dst_prev
->child
= dst
;
1642 xfrm_init_path((struct xfrm_dst
*)dst0
, dst
, nfheader_len
);
1643 xfrm_init_pmtu(dst_prev
);
1645 for (dst_prev
= dst0
; dst_prev
!= dst
; dst_prev
= dst_prev
->child
) {
1646 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst_prev
;
1648 err
= xfrm_fill_dst(xdst
, dev
, fl
);
1652 dst_prev
->header_len
= header_len
;
1653 dst_prev
->trailer_len
= trailer_len
;
1654 header_len
-= xdst
->u
.dst
.xfrm
->props
.header_len
;
1655 trailer_len
-= xdst
->u
.dst
.xfrm
->props
.trailer_len
;
1663 xfrm_state_put(xfrm
[i
]);
1666 dst_release_immediate(dst0
);
1667 dst0
= ERR_PTR(err
);
1671 static int xfrm_expand_policies(const struct flowi
*fl
, u16 family
,
1672 struct xfrm_policy
**pols
,
1673 int *num_pols
, int *num_xfrms
)
1677 if (*num_pols
== 0 || !pols
[0]) {
1682 if (IS_ERR(pols
[0]))
1683 return PTR_ERR(pols
[0]);
1685 *num_xfrms
= pols
[0]->xfrm_nr
;
1687 #ifdef CONFIG_XFRM_SUB_POLICY
1688 if (pols
[0] && pols
[0]->action
== XFRM_POLICY_ALLOW
&&
1689 pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1690 pols
[1] = xfrm_policy_lookup_bytype(xp_net(pols
[0]),
1691 XFRM_POLICY_TYPE_MAIN
,
1695 if (IS_ERR(pols
[1])) {
1696 xfrm_pols_put(pols
, *num_pols
);
1697 return PTR_ERR(pols
[1]);
1700 (*num_xfrms
) += pols
[1]->xfrm_nr
;
1704 for (i
= 0; i
< *num_pols
; i
++) {
1705 if (pols
[i
]->action
!= XFRM_POLICY_ALLOW
) {
1715 static struct xfrm_dst
*
1716 xfrm_resolve_and_create_bundle(struct xfrm_policy
**pols
, int num_pols
,
1717 const struct flowi
*fl
, u16 family
,
1718 struct dst_entry
*dst_orig
)
1720 struct net
*net
= xp_net(pols
[0]);
1721 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1722 struct xfrm_dst
*xdst
;
1723 struct dst_entry
*dst
;
1726 /* Try to instantiate a bundle */
1727 err
= xfrm_tmpl_resolve(pols
, num_pols
, fl
, xfrm
, family
);
1733 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1734 return ERR_PTR(err
);
1737 dst
= xfrm_bundle_create(pols
[0], xfrm
, err
, fl
, dst_orig
);
1739 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLEGENERROR
);
1740 return ERR_CAST(dst
);
1743 xdst
= (struct xfrm_dst
*)dst
;
1744 xdst
->num_xfrms
= err
;
1745 xdst
->num_pols
= num_pols
;
1746 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1747 xdst
->policy_genid
= atomic_read(&pols
[0]->genid
);
1752 static void xfrm_policy_queue_process(struct timer_list
*t
)
1754 struct sk_buff
*skb
;
1756 struct dst_entry
*dst
;
1757 struct xfrm_policy
*pol
= from_timer(pol
, t
, polq
.hold_timer
);
1758 struct net
*net
= xp_net(pol
);
1759 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1761 struct sk_buff_head list
;
1763 spin_lock(&pq
->hold_queue
.lock
);
1764 skb
= skb_peek(&pq
->hold_queue
);
1766 spin_unlock(&pq
->hold_queue
.lock
);
1771 xfrm_decode_session(skb
, &fl
, dst
->ops
->family
);
1772 spin_unlock(&pq
->hold_queue
.lock
);
1774 dst_hold(dst
->path
);
1775 dst
= xfrm_lookup(net
, dst
->path
, &fl
, sk
, 0);
1779 if (dst
->flags
& DST_XFRM_QUEUE
) {
1782 if (pq
->timeout
>= XFRM_QUEUE_TMO_MAX
)
1785 pq
->timeout
= pq
->timeout
<< 1;
1786 if (!mod_timer(&pq
->hold_timer
, jiffies
+ pq
->timeout
))
1793 __skb_queue_head_init(&list
);
1795 spin_lock(&pq
->hold_queue
.lock
);
1797 skb_queue_splice_init(&pq
->hold_queue
, &list
);
1798 spin_unlock(&pq
->hold_queue
.lock
);
1800 while (!skb_queue_empty(&list
)) {
1801 skb
= __skb_dequeue(&list
);
1803 xfrm_decode_session(skb
, &fl
, skb_dst(skb
)->ops
->family
);
1804 dst_hold(skb_dst(skb
)->path
);
1805 dst
= xfrm_lookup(net
, skb_dst(skb
)->path
, &fl
, skb
->sk
, 0);
1813 skb_dst_set(skb
, dst
);
1815 dst_output(net
, skb
->sk
, skb
);
1824 skb_queue_purge(&pq
->hold_queue
);
1828 static int xdst_queue_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
1830 unsigned long sched_next
;
1831 struct dst_entry
*dst
= skb_dst(skb
);
1832 struct xfrm_dst
*xdst
= (struct xfrm_dst
*) dst
;
1833 struct xfrm_policy
*pol
= xdst
->pols
[0];
1834 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1836 if (unlikely(skb_fclone_busy(sk
, skb
))) {
1841 if (pq
->hold_queue
.qlen
> XFRM_MAX_QUEUE_LEN
) {
1848 spin_lock_bh(&pq
->hold_queue
.lock
);
1851 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
1853 sched_next
= jiffies
+ pq
->timeout
;
1855 if (del_timer(&pq
->hold_timer
)) {
1856 if (time_before(pq
->hold_timer
.expires
, sched_next
))
1857 sched_next
= pq
->hold_timer
.expires
;
1861 __skb_queue_tail(&pq
->hold_queue
, skb
);
1862 if (!mod_timer(&pq
->hold_timer
, sched_next
))
1865 spin_unlock_bh(&pq
->hold_queue
.lock
);
1870 static struct xfrm_dst
*xfrm_create_dummy_bundle(struct net
*net
,
1871 struct xfrm_flo
*xflo
,
1872 const struct flowi
*fl
,
1877 struct net_device
*dev
;
1878 struct dst_entry
*dst
;
1879 struct dst_entry
*dst1
;
1880 struct xfrm_dst
*xdst
;
1882 xdst
= xfrm_alloc_dst(net
, family
);
1886 if (!(xflo
->flags
& XFRM_LOOKUP_QUEUE
) ||
1887 net
->xfrm
.sysctl_larval_drop
||
1891 dst
= xflo
->dst_orig
;
1892 dst1
= &xdst
->u
.dst
;
1896 dst_copy_metrics(dst1
, dst
);
1898 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
1899 dst1
->flags
|= DST_HOST
| DST_XFRM_QUEUE
;
1900 dst1
->lastuse
= jiffies
;
1902 dst1
->input
= dst_discard
;
1903 dst1
->output
= xdst_queue_output
;
1909 xfrm_init_path((struct xfrm_dst
*)dst1
, dst
, 0);
1916 err
= xfrm_fill_dst(xdst
, dev
, fl
);
1925 xdst
= ERR_PTR(err
);
1929 static struct xfrm_dst
*
1930 xfrm_bundle_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
, struct xfrm_flo
*xflo
)
1932 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1933 int num_pols
= 0, num_xfrms
= 0, err
;
1934 struct xfrm_dst
*xdst
;
1936 /* Resolve policies to use if we couldn't get them from
1937 * previous cache entry */
1939 pols
[0] = xfrm_policy_lookup(net
, fl
, family
, dir
);
1940 err
= xfrm_expand_policies(fl
, family
, pols
,
1941 &num_pols
, &num_xfrms
);
1947 goto make_dummy_bundle
;
1949 xdst
= xfrm_resolve_and_create_bundle(pols
, num_pols
, fl
, family
,
1952 err
= PTR_ERR(xdst
);
1955 goto make_dummy_bundle
;
1956 } else if (xdst
== NULL
) {
1958 goto make_dummy_bundle
;
1964 /* We found policies, but there's no bundles to instantiate:
1965 * either because the policy blocks, has no transformations or
1966 * we could not build template (no xfrm_states).*/
1967 xdst
= xfrm_create_dummy_bundle(net
, xflo
, fl
, num_xfrms
, family
);
1969 xfrm_pols_put(pols
, num_pols
);
1970 return ERR_CAST(xdst
);
1972 xdst
->num_pols
= num_pols
;
1973 xdst
->num_xfrms
= num_xfrms
;
1974 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1979 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1981 xfrm_pols_put(pols
, num_pols
);
1982 return ERR_PTR(err
);
1985 static struct dst_entry
*make_blackhole(struct net
*net
, u16 family
,
1986 struct dst_entry
*dst_orig
)
1988 const struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1989 struct dst_entry
*ret
;
1992 dst_release(dst_orig
);
1993 return ERR_PTR(-EINVAL
);
1995 ret
= afinfo
->blackhole_route(net
, dst_orig
);
2002 /* Main function: finds/creates a bundle for given flow.
2004 * At the moment we eat a raw IP route. Mostly to speed up lookups
2005 * on interfaces with disabled IPsec.
2007 struct dst_entry
*xfrm_lookup(struct net
*net
, struct dst_entry
*dst_orig
,
2008 const struct flowi
*fl
,
2009 const struct sock
*sk
, int flags
)
2011 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2012 struct xfrm_dst
*xdst
;
2013 struct dst_entry
*dst
, *route
;
2014 u16 family
= dst_orig
->ops
->family
;
2015 u8 dir
= XFRM_POLICY_OUT
;
2016 int i
, err
, num_pols
, num_xfrms
= 0, drop_pols
= 0;
2022 sk
= sk_const_to_full_sk(sk
);
2023 if (sk
&& sk
->sk_policy
[XFRM_POLICY_OUT
]) {
2025 pols
[0] = xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
, family
);
2026 err
= xfrm_expand_policies(fl
, family
, pols
,
2027 &num_pols
, &num_xfrms
);
2032 if (num_xfrms
<= 0) {
2033 drop_pols
= num_pols
;
2037 xdst
= xfrm_resolve_and_create_bundle(
2042 xfrm_pols_put(pols
, num_pols
);
2043 err
= PTR_ERR(xdst
);
2045 } else if (xdst
== NULL
) {
2047 drop_pols
= num_pols
;
2051 route
= xdst
->route
;
2056 struct xfrm_flo xflo
;
2058 xflo
.dst_orig
= dst_orig
;
2061 /* To accelerate a bit... */
2062 if ((dst_orig
->flags
& DST_NOXFRM
) ||
2063 !net
->xfrm
.policy_count
[XFRM_POLICY_OUT
])
2066 xdst
= xfrm_bundle_lookup(net
, fl
, family
, dir
, &xflo
);
2070 err
= PTR_ERR(xdst
);
2074 num_pols
= xdst
->num_pols
;
2075 num_xfrms
= xdst
->num_xfrms
;
2076 memcpy(pols
, xdst
->pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2077 route
= xdst
->route
;
2081 if (route
== NULL
&& num_xfrms
> 0) {
2082 /* The only case when xfrm_bundle_lookup() returns a
2083 * bundle with null route, is when the template could
2084 * not be resolved. It means policies are there, but
2085 * bundle could not be created, since we don't yet
2086 * have the xfrm_state's. We need to wait for KM to
2087 * negotiate new SA's or bail out with error.*/
2088 if (net
->xfrm
.sysctl_larval_drop
) {
2089 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2096 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2104 if ((flags
& XFRM_LOOKUP_ICMP
) &&
2105 !(pols
[0]->flags
& XFRM_POLICY_ICMP
)) {
2110 for (i
= 0; i
< num_pols
; i
++)
2111 pols
[i
]->curlft
.use_time
= get_seconds();
2113 if (num_xfrms
< 0) {
2114 /* Prohibit the flow */
2115 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLBLOCK
);
2118 } else if (num_xfrms
> 0) {
2119 /* Flow transformed */
2120 dst_release(dst_orig
);
2122 /* Flow passes untransformed */
2127 xfrm_pols_put(pols
, drop_pols
);
2128 if (dst
&& dst
->xfrm
&&
2129 dst
->xfrm
->props
.mode
== XFRM_MODE_TUNNEL
)
2130 dst
->flags
|= DST_XFRM_TUNNEL
;
2134 if (!(flags
& XFRM_LOOKUP_ICMP
)) {
2142 if (!(flags
& XFRM_LOOKUP_KEEP_DST_REF
))
2143 dst_release(dst_orig
);
2144 xfrm_pols_put(pols
, drop_pols
);
2145 return ERR_PTR(err
);
2147 EXPORT_SYMBOL(xfrm_lookup
);
2149 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2150 * Otherwise we may send out blackholed packets.
2152 struct dst_entry
*xfrm_lookup_route(struct net
*net
, struct dst_entry
*dst_orig
,
2153 const struct flowi
*fl
,
2154 const struct sock
*sk
, int flags
)
2156 struct dst_entry
*dst
= xfrm_lookup(net
, dst_orig
, fl
, sk
,
2157 flags
| XFRM_LOOKUP_QUEUE
|
2158 XFRM_LOOKUP_KEEP_DST_REF
);
2160 if (IS_ERR(dst
) && PTR_ERR(dst
) == -EREMOTE
)
2161 return make_blackhole(net
, dst_orig
->ops
->family
, dst_orig
);
2164 dst_release(dst_orig
);
2168 EXPORT_SYMBOL(xfrm_lookup_route
);
2171 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, const struct flowi
*fl
)
2173 struct xfrm_state
*x
;
2175 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
2177 x
= skb
->sp
->xvec
[idx
];
2178 if (!x
->type
->reject
)
2180 return x
->type
->reject(x
, skb
, fl
);
2183 /* When skb is transformed back to its "native" form, we have to
2184 * check policy restrictions. At the moment we make this in maximally
2185 * stupid way. Shame on me. :-) Of course, connected sockets must
2186 * have policy cached at them.
2190 xfrm_state_ok(const struct xfrm_tmpl
*tmpl
, const struct xfrm_state
*x
,
2191 unsigned short family
)
2193 if (xfrm_state_kern(x
))
2194 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
2195 return x
->id
.proto
== tmpl
->id
.proto
&&
2196 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
2197 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
2198 x
->props
.mode
== tmpl
->mode
&&
2199 (tmpl
->allalgs
|| (tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
2200 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
2201 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
2202 xfrm_state_addr_cmp(tmpl
, x
, family
));
2206 * 0 or more than 0 is returned when validation is succeeded (either bypass
2207 * because of optional transport mode, or next index of the mathced secpath
2208 * state with the template.
2209 * -1 is returned when no matching template is found.
2210 * Otherwise "-2 - errored_index" is returned.
2213 xfrm_policy_ok(const struct xfrm_tmpl
*tmpl
, const struct sec_path
*sp
, int start
,
2214 unsigned short family
)
2218 if (tmpl
->optional
) {
2219 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
2223 for (; idx
< sp
->len
; idx
++) {
2224 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
2226 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2235 int __xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
,
2236 unsigned int family
, int reverse
)
2238 const struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2241 if (unlikely(afinfo
== NULL
))
2242 return -EAFNOSUPPORT
;
2244 afinfo
->decode_session(skb
, fl
, reverse
);
2245 err
= security_xfrm_decode_session(skb
, &fl
->flowi_secid
);
2249 EXPORT_SYMBOL(__xfrm_decode_session
);
2251 static inline int secpath_has_nontransport(const struct sec_path
*sp
, int k
, int *idxp
)
2253 for (; k
< sp
->len
; k
++) {
2254 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2263 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
2264 unsigned short family
)
2266 struct net
*net
= dev_net(skb
->dev
);
2267 struct xfrm_policy
*pol
;
2268 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2276 reverse
= dir
& ~XFRM_POLICY_MASK
;
2277 dir
&= XFRM_POLICY_MASK
;
2279 if (__xfrm_decode_session(skb
, &fl
, family
, reverse
) < 0) {
2280 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
2284 nf_nat_decode_session(skb
, &fl
, family
);
2286 /* First, check used SA against their selectors. */
2290 for (i
= skb
->sp
->len
-1; i
>= 0; i
--) {
2291 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
2292 if (!xfrm_selector_match(&x
->sel
, &fl
, family
)) {
2293 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMISMATCH
);
2300 sk
= sk_to_full_sk(sk
);
2301 if (sk
&& sk
->sk_policy
[dir
]) {
2302 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
, family
);
2304 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2310 pol
= xfrm_policy_lookup(net
, &fl
, family
, dir
);
2313 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2318 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
2319 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2320 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINNOPOLS
);
2326 pol
->curlft
.use_time
= get_seconds();
2330 #ifdef CONFIG_XFRM_SUB_POLICY
2331 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
2332 pols
[1] = xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
,
2336 if (IS_ERR(pols
[1])) {
2337 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2340 pols
[1]->curlft
.use_time
= get_seconds();
2346 if (pol
->action
== XFRM_POLICY_ALLOW
) {
2347 struct sec_path
*sp
;
2348 static struct sec_path dummy
;
2349 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
2350 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
2351 struct xfrm_tmpl
**tpp
= tp
;
2355 if ((sp
= skb
->sp
) == NULL
)
2358 for (pi
= 0; pi
< npols
; pi
++) {
2359 if (pols
[pi
] != pol
&&
2360 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
) {
2361 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2364 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
2365 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
2368 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
2369 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
2373 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
, net
);
2377 /* For each tunnel xfrm, find the first matching tmpl.
2378 * For each tmpl before that, find corresponding xfrm.
2379 * Order is _important_. Later we will implement
2380 * some barriers, but at the moment barriers
2381 * are implied between each two transformations.
2383 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
2384 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
2387 /* "-2 - errored_index" returned */
2389 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2394 if (secpath_has_nontransport(sp
, k
, &xerr_idx
)) {
2395 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2399 xfrm_pols_put(pols
, npols
);
2402 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2405 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2407 xfrm_pols_put(pols
, npols
);
2410 EXPORT_SYMBOL(__xfrm_policy_check
);
2412 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
2414 struct net
*net
= dev_net(skb
->dev
);
2416 struct dst_entry
*dst
;
2419 if (xfrm_decode_session(skb
, &fl
, family
) < 0) {
2420 XFRM_INC_STATS(net
, LINUX_MIB_XFRMFWDHDRERROR
);
2425 if (!skb_dst(skb
)) {
2426 XFRM_INC_STATS(net
, LINUX_MIB_XFRMFWDHDRERROR
);
2430 dst
= xfrm_lookup(net
, skb_dst(skb
), &fl
, NULL
, XFRM_LOOKUP_QUEUE
);
2435 skb_dst_set(skb
, dst
);
2438 EXPORT_SYMBOL(__xfrm_route_forward
);
2440 /* Optimize later using cookies and generation ids. */
2442 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
2444 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2445 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2446 * get validated by dst_ops->check on every use. We do this
2447 * because when a normal route referenced by an XFRM dst is
2448 * obsoleted we do not go looking around for all parent
2449 * referencing XFRM dsts so that we can invalidate them. It
2450 * is just too much work. Instead we make the checks here on
2451 * every use. For example:
2453 * XFRM dst A --> IPv4 dst X
2455 * X is the "xdst->route" of A (X is also the "dst->path" of A
2456 * in this example). If X is marked obsolete, "A" will not
2457 * notice. That's what we are validating here via the
2458 * stale_bundle() check.
2460 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
2462 * This will force stale_bundle() to fail on any xdst bundle with
2463 * this dst linked in it.
2465 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
2471 static int stale_bundle(struct dst_entry
*dst
)
2473 return !xfrm_bundle_ok((struct xfrm_dst
*)dst
);
2476 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
2478 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
2479 dst
->dev
= dev_net(dev
)->loopback_dev
;
2484 EXPORT_SYMBOL(xfrm_dst_ifdown
);
2486 static void xfrm_link_failure(struct sk_buff
*skb
)
2488 /* Impossible. Such dst must be popped before reaches point of failure. */
2491 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
2494 if (dst
->obsolete
) {
2502 static void xfrm_init_pmtu(struct dst_entry
*dst
)
2505 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2506 u32 pmtu
, route_mtu_cached
;
2508 pmtu
= dst_mtu(dst
->child
);
2509 xdst
->child_mtu_cached
= pmtu
;
2511 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2513 route_mtu_cached
= dst_mtu(xdst
->route
);
2514 xdst
->route_mtu_cached
= route_mtu_cached
;
2516 if (pmtu
> route_mtu_cached
)
2517 pmtu
= route_mtu_cached
;
2519 dst_metric_set(dst
, RTAX_MTU
, pmtu
);
2520 } while ((dst
= dst
->next
));
2523 /* Check that the bundle accepts the flow and its components are
2527 static int xfrm_bundle_ok(struct xfrm_dst
*first
)
2529 struct dst_entry
*dst
= &first
->u
.dst
;
2530 struct xfrm_dst
*last
;
2533 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2534 (dst
->dev
&& !netif_running(dst
->dev
)))
2537 if (dst
->flags
& DST_XFRM_QUEUE
)
2543 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2545 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2547 if (xdst
->xfrm_genid
!= dst
->xfrm
->genid
)
2549 if (xdst
->num_pols
> 0 &&
2550 xdst
->policy_genid
!= atomic_read(&xdst
->pols
[0]->genid
))
2553 mtu
= dst_mtu(dst
->child
);
2554 if (xdst
->child_mtu_cached
!= mtu
) {
2556 xdst
->child_mtu_cached
= mtu
;
2559 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2561 mtu
= dst_mtu(xdst
->route
);
2562 if (xdst
->route_mtu_cached
!= mtu
) {
2564 xdst
->route_mtu_cached
= mtu
;
2568 } while (dst
->xfrm
);
2573 mtu
= last
->child_mtu_cached
;
2577 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2578 if (mtu
> last
->route_mtu_cached
)
2579 mtu
= last
->route_mtu_cached
;
2580 dst_metric_set(dst
, RTAX_MTU
, mtu
);
2585 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2586 last
->child_mtu_cached
= mtu
;
2592 static unsigned int xfrm_default_advmss(const struct dst_entry
*dst
)
2594 return dst_metric_advmss(dst
->path
);
2597 static unsigned int xfrm_mtu(const struct dst_entry
*dst
)
2599 unsigned int mtu
= dst_metric_raw(dst
, RTAX_MTU
);
2601 return mtu
? : dst_mtu(dst
->path
);
2604 static const void *xfrm_get_dst_nexthop(const struct dst_entry
*dst
,
2607 const struct dst_entry
*path
= dst
->path
;
2609 for (; dst
!= path
; dst
= dst
->child
) {
2610 const struct xfrm_state
*xfrm
= dst
->xfrm
;
2612 if (xfrm
->props
.mode
== XFRM_MODE_TRANSPORT
)
2614 if (xfrm
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
)
2615 daddr
= xfrm
->coaddr
;
2616 else if (!(xfrm
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
))
2617 daddr
= &xfrm
->id
.daddr
;
2622 static struct neighbour
*xfrm_neigh_lookup(const struct dst_entry
*dst
,
2623 struct sk_buff
*skb
,
2626 const struct dst_entry
*path
= dst
->path
;
2629 daddr
= xfrm_get_dst_nexthop(dst
, daddr
);
2630 return path
->ops
->neigh_lookup(path
, skb
, daddr
);
2633 static void xfrm_confirm_neigh(const struct dst_entry
*dst
, const void *daddr
)
2635 const struct dst_entry
*path
= dst
->path
;
2637 daddr
= xfrm_get_dst_nexthop(dst
, daddr
);
2638 path
->ops
->confirm_neigh(path
, daddr
);
2641 int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo
*afinfo
, int family
)
2645 if (WARN_ON(family
>= ARRAY_SIZE(xfrm_policy_afinfo
)))
2646 return -EAFNOSUPPORT
;
2648 spin_lock(&xfrm_policy_afinfo_lock
);
2649 if (unlikely(xfrm_policy_afinfo
[family
] != NULL
))
2652 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2653 if (likely(dst_ops
->kmem_cachep
== NULL
))
2654 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2655 if (likely(dst_ops
->check
== NULL
))
2656 dst_ops
->check
= xfrm_dst_check
;
2657 if (likely(dst_ops
->default_advmss
== NULL
))
2658 dst_ops
->default_advmss
= xfrm_default_advmss
;
2659 if (likely(dst_ops
->mtu
== NULL
))
2660 dst_ops
->mtu
= xfrm_mtu
;
2661 if (likely(dst_ops
->negative_advice
== NULL
))
2662 dst_ops
->negative_advice
= xfrm_negative_advice
;
2663 if (likely(dst_ops
->link_failure
== NULL
))
2664 dst_ops
->link_failure
= xfrm_link_failure
;
2665 if (likely(dst_ops
->neigh_lookup
== NULL
))
2666 dst_ops
->neigh_lookup
= xfrm_neigh_lookup
;
2667 if (likely(!dst_ops
->confirm_neigh
))
2668 dst_ops
->confirm_neigh
= xfrm_confirm_neigh
;
2669 rcu_assign_pointer(xfrm_policy_afinfo
[family
], afinfo
);
2671 spin_unlock(&xfrm_policy_afinfo_lock
);
2675 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2677 void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo
*afinfo
)
2679 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2682 for (i
= 0; i
< ARRAY_SIZE(xfrm_policy_afinfo
); i
++) {
2683 if (xfrm_policy_afinfo
[i
] != afinfo
)
2685 RCU_INIT_POINTER(xfrm_policy_afinfo
[i
], NULL
);
2691 dst_ops
->kmem_cachep
= NULL
;
2692 dst_ops
->check
= NULL
;
2693 dst_ops
->negative_advice
= NULL
;
2694 dst_ops
->link_failure
= NULL
;
2696 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2698 #ifdef CONFIG_XFRM_STATISTICS
2699 static int __net_init
xfrm_statistics_init(struct net
*net
)
2702 net
->mib
.xfrm_statistics
= alloc_percpu(struct linux_xfrm_mib
);
2703 if (!net
->mib
.xfrm_statistics
)
2705 rv
= xfrm_proc_init(net
);
2707 free_percpu(net
->mib
.xfrm_statistics
);
2711 static void xfrm_statistics_fini(struct net
*net
)
2713 xfrm_proc_fini(net
);
2714 free_percpu(net
->mib
.xfrm_statistics
);
2717 static int __net_init
xfrm_statistics_init(struct net
*net
)
2722 static void xfrm_statistics_fini(struct net
*net
)
2727 static int __net_init
xfrm_policy_init(struct net
*net
)
2729 unsigned int hmask
, sz
;
2732 if (net_eq(net
, &init_net
))
2733 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2734 sizeof(struct xfrm_dst
),
2735 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2739 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2741 net
->xfrm
.policy_byidx
= xfrm_hash_alloc(sz
);
2742 if (!net
->xfrm
.policy_byidx
)
2744 net
->xfrm
.policy_idx_hmask
= hmask
;
2746 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
2747 struct xfrm_policy_hash
*htab
;
2749 net
->xfrm
.policy_count
[dir
] = 0;
2750 net
->xfrm
.policy_count
[XFRM_POLICY_MAX
+ dir
] = 0;
2751 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
2753 htab
= &net
->xfrm
.policy_bydst
[dir
];
2754 htab
->table
= xfrm_hash_alloc(sz
);
2757 htab
->hmask
= hmask
;
2763 net
->xfrm
.policy_hthresh
.lbits4
= 32;
2764 net
->xfrm
.policy_hthresh
.rbits4
= 32;
2765 net
->xfrm
.policy_hthresh
.lbits6
= 128;
2766 net
->xfrm
.policy_hthresh
.rbits6
= 128;
2768 seqlock_init(&net
->xfrm
.policy_hthresh
.lock
);
2770 INIT_LIST_HEAD(&net
->xfrm
.policy_all
);
2771 INIT_WORK(&net
->xfrm
.policy_hash_work
, xfrm_hash_resize
);
2772 INIT_WORK(&net
->xfrm
.policy_hthresh
.work
, xfrm_hash_rebuild
);
2773 if (net_eq(net
, &init_net
))
2778 for (dir
--; dir
>= 0; dir
--) {
2779 struct xfrm_policy_hash
*htab
;
2781 htab
= &net
->xfrm
.policy_bydst
[dir
];
2782 xfrm_hash_free(htab
->table
, sz
);
2784 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
2789 static void xfrm_policy_fini(struct net
*net
)
2794 flush_work(&net
->xfrm
.policy_hash_work
);
2795 #ifdef CONFIG_XFRM_SUB_POLICY
2796 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_SUB
, false);
2798 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, false);
2800 WARN_ON(!list_empty(&net
->xfrm
.policy_all
));
2802 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
2803 struct xfrm_policy_hash
*htab
;
2805 WARN_ON(!hlist_empty(&net
->xfrm
.policy_inexact
[dir
]));
2807 htab
= &net
->xfrm
.policy_bydst
[dir
];
2808 sz
= (htab
->hmask
+ 1) * sizeof(struct hlist_head
);
2809 WARN_ON(!hlist_empty(htab
->table
));
2810 xfrm_hash_free(htab
->table
, sz
);
2813 sz
= (net
->xfrm
.policy_idx_hmask
+ 1) * sizeof(struct hlist_head
);
2814 WARN_ON(!hlist_empty(net
->xfrm
.policy_byidx
));
2815 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
2818 static int __net_init
xfrm_net_init(struct net
*net
)
2822 /* Initialize the per-net locks here */
2823 spin_lock_init(&net
->xfrm
.xfrm_state_lock
);
2824 spin_lock_init(&net
->xfrm
.xfrm_policy_lock
);
2825 mutex_init(&net
->xfrm
.xfrm_cfg_mutex
);
2827 rv
= xfrm_statistics_init(net
);
2829 goto out_statistics
;
2830 rv
= xfrm_state_init(net
);
2833 rv
= xfrm_policy_init(net
);
2836 rv
= xfrm_sysctl_init(net
);
2843 xfrm_policy_fini(net
);
2845 xfrm_state_fini(net
);
2847 xfrm_statistics_fini(net
);
2852 static void __net_exit
xfrm_net_exit(struct net
*net
)
2854 xfrm_sysctl_fini(net
);
2855 xfrm_policy_fini(net
);
2856 xfrm_state_fini(net
);
2857 xfrm_statistics_fini(net
);
2860 static struct pernet_operations __net_initdata xfrm_net_ops
= {
2861 .init
= xfrm_net_init
,
2862 .exit
= xfrm_net_exit
,
2865 void __init
xfrm_init(void)
2867 register_pernet_subsys(&xfrm_net_ops
);
2868 seqcount_init(&xfrm_policy_hash_generation
);
2872 #ifdef CONFIG_AUDITSYSCALL
2873 static void xfrm_audit_common_policyinfo(struct xfrm_policy
*xp
,
2874 struct audit_buffer
*audit_buf
)
2876 struct xfrm_sec_ctx
*ctx
= xp
->security
;
2877 struct xfrm_selector
*sel
= &xp
->selector
;
2880 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2881 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
2883 switch (sel
->family
) {
2885 audit_log_format(audit_buf
, " src=%pI4", &sel
->saddr
.a4
);
2886 if (sel
->prefixlen_s
!= 32)
2887 audit_log_format(audit_buf
, " src_prefixlen=%d",
2889 audit_log_format(audit_buf
, " dst=%pI4", &sel
->daddr
.a4
);
2890 if (sel
->prefixlen_d
!= 32)
2891 audit_log_format(audit_buf
, " dst_prefixlen=%d",
2895 audit_log_format(audit_buf
, " src=%pI6", sel
->saddr
.a6
);
2896 if (sel
->prefixlen_s
!= 128)
2897 audit_log_format(audit_buf
, " src_prefixlen=%d",
2899 audit_log_format(audit_buf
, " dst=%pI6", sel
->daddr
.a6
);
2900 if (sel
->prefixlen_d
!= 128)
2901 audit_log_format(audit_buf
, " dst_prefixlen=%d",
2907 void xfrm_audit_policy_add(struct xfrm_policy
*xp
, int result
, bool task_valid
)
2909 struct audit_buffer
*audit_buf
;
2911 audit_buf
= xfrm_audit_start("SPD-add");
2912 if (audit_buf
== NULL
)
2914 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
2915 audit_log_format(audit_buf
, " res=%u", result
);
2916 xfrm_audit_common_policyinfo(xp
, audit_buf
);
2917 audit_log_end(audit_buf
);
2919 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add
);
2921 void xfrm_audit_policy_delete(struct xfrm_policy
*xp
, int result
,
2924 struct audit_buffer
*audit_buf
;
2926 audit_buf
= xfrm_audit_start("SPD-delete");
2927 if (audit_buf
== NULL
)
2929 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
2930 audit_log_format(audit_buf
, " res=%u", result
);
2931 xfrm_audit_common_policyinfo(xp
, audit_buf
);
2932 audit_log_end(audit_buf
);
2934 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete
);
2937 #ifdef CONFIG_XFRM_MIGRATE
2938 static bool xfrm_migrate_selector_match(const struct xfrm_selector
*sel_cmp
,
2939 const struct xfrm_selector
*sel_tgt
)
2941 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
2942 if (sel_tgt
->family
== sel_cmp
->family
&&
2943 xfrm_addr_equal(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
2945 xfrm_addr_equal(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
2947 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
2948 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
2952 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
2959 static struct xfrm_policy
*xfrm_migrate_policy_find(const struct xfrm_selector
*sel
,
2960 u8 dir
, u8 type
, struct net
*net
)
2962 struct xfrm_policy
*pol
, *ret
= NULL
;
2963 struct hlist_head
*chain
;
2966 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
2967 chain
= policy_hash_direct(net
, &sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
2968 hlist_for_each_entry(pol
, chain
, bydst
) {
2969 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2970 pol
->type
== type
) {
2972 priority
= ret
->priority
;
2976 chain
= &net
->xfrm
.policy_inexact
[dir
];
2977 hlist_for_each_entry(pol
, chain
, bydst
) {
2978 if ((pol
->priority
>= priority
) && ret
)
2981 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2982 pol
->type
== type
) {
2990 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
2995 static int migrate_tmpl_match(const struct xfrm_migrate
*m
, const struct xfrm_tmpl
*t
)
2999 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
3000 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
3002 case XFRM_MODE_TUNNEL
:
3003 case XFRM_MODE_BEET
:
3004 if (xfrm_addr_equal(&t
->id
.daddr
, &m
->old_daddr
,
3006 xfrm_addr_equal(&t
->saddr
, &m
->old_saddr
,
3011 case XFRM_MODE_TRANSPORT
:
3012 /* in case of transport mode, template does not store
3013 any IP addresses, hence we just compare mode and
3024 /* update endpoint address(es) of template(s) */
3025 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
3026 struct xfrm_migrate
*m
, int num_migrate
)
3028 struct xfrm_migrate
*mp
;
3031 write_lock_bh(&pol
->lock
);
3032 if (unlikely(pol
->walk
.dead
)) {
3033 /* target policy has been deleted */
3034 write_unlock_bh(&pol
->lock
);
3038 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
3039 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
3040 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
3043 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
&&
3044 pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_BEET
)
3046 /* update endpoints */
3047 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
3048 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
3049 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
3050 sizeof(pol
->xfrm_vec
[i
].saddr
));
3051 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
3053 atomic_inc(&pol
->genid
);
3057 write_unlock_bh(&pol
->lock
);
3065 static int xfrm_migrate_check(const struct xfrm_migrate
*m
, int num_migrate
)
3069 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
3072 for (i
= 0; i
< num_migrate
; i
++) {
3073 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
3074 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
3077 /* check if there is any duplicated entry */
3078 for (j
= i
+ 1; j
< num_migrate
; j
++) {
3079 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
3080 sizeof(m
[i
].old_daddr
)) &&
3081 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
3082 sizeof(m
[i
].old_saddr
)) &&
3083 m
[i
].proto
== m
[j
].proto
&&
3084 m
[i
].mode
== m
[j
].mode
&&
3085 m
[i
].reqid
== m
[j
].reqid
&&
3086 m
[i
].old_family
== m
[j
].old_family
)
3094 int xfrm_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3095 struct xfrm_migrate
*m
, int num_migrate
,
3096 struct xfrm_kmaddress
*k
, struct net
*net
,
3097 struct xfrm_encap_tmpl
*encap
)
3099 int i
, err
, nx_cur
= 0, nx_new
= 0;
3100 struct xfrm_policy
*pol
= NULL
;
3101 struct xfrm_state
*x
, *xc
;
3102 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
3103 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
3104 struct xfrm_migrate
*mp
;
3106 /* Stage 0 - sanity checks */
3107 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
3110 if (dir
>= XFRM_POLICY_MAX
) {
3115 /* Stage 1 - find policy */
3116 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
, net
)) == NULL
) {
3121 /* Stage 2 - find and update state(s) */
3122 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
3123 if ((x
= xfrm_migrate_state_find(mp
, net
))) {
3126 xc
= xfrm_state_migrate(x
, mp
, encap
);
3137 /* Stage 3 - update policy */
3138 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
3141 /* Stage 4 - delete old state(s) */
3143 xfrm_states_put(x_cur
, nx_cur
);
3144 xfrm_states_delete(x_cur
, nx_cur
);
3147 /* Stage 5 - announce */
3148 km_migrate(sel
, dir
, type
, m
, num_migrate
, k
, encap
);
3160 xfrm_states_put(x_cur
, nx_cur
);
3162 xfrm_states_delete(x_new
, nx_new
);
3166 EXPORT_SYMBOL(xfrm_migrate
);