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
;
53 static void xfrm_init_pmtu(struct dst_entry
*dst
);
54 static int stale_bundle(struct dst_entry
*dst
);
55 static int xfrm_bundle_ok(struct xfrm_dst
*xdst
);
56 static void xfrm_policy_queue_process(unsigned long arg
);
58 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
62 __xfrm4_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
64 const struct flowi4
*fl4
= &fl
->u
.ip4
;
66 return addr4_match(fl4
->daddr
, sel
->daddr
.a4
, sel
->prefixlen_d
) &&
67 addr4_match(fl4
->saddr
, sel
->saddr
.a4
, sel
->prefixlen_s
) &&
68 !((xfrm_flowi_dport(fl
, &fl4
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
69 !((xfrm_flowi_sport(fl
, &fl4
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
70 (fl4
->flowi4_proto
== sel
->proto
|| !sel
->proto
) &&
71 (fl4
->flowi4_oif
== sel
->ifindex
|| !sel
->ifindex
);
75 __xfrm6_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
77 const struct flowi6
*fl6
= &fl
->u
.ip6
;
79 return addr_match(&fl6
->daddr
, &sel
->daddr
, sel
->prefixlen_d
) &&
80 addr_match(&fl6
->saddr
, &sel
->saddr
, sel
->prefixlen_s
) &&
81 !((xfrm_flowi_dport(fl
, &fl6
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
82 !((xfrm_flowi_sport(fl
, &fl6
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
83 (fl6
->flowi6_proto
== sel
->proto
|| !sel
->proto
) &&
84 (fl6
->flowi6_oif
== sel
->ifindex
|| !sel
->ifindex
);
87 bool xfrm_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
,
88 unsigned short family
)
92 return __xfrm4_selector_match(sel
, fl
);
94 return __xfrm6_selector_match(sel
, fl
);
99 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
101 struct xfrm_policy_afinfo
*afinfo
;
103 if (unlikely(family
>= NPROTO
))
106 afinfo
= rcu_dereference(xfrm_policy_afinfo
[family
]);
107 if (unlikely(!afinfo
))
112 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
)
117 static inline struct dst_entry
*__xfrm_dst_lookup(struct net
*net
, int tos
,
118 const xfrm_address_t
*saddr
,
119 const xfrm_address_t
*daddr
,
122 struct xfrm_policy_afinfo
*afinfo
;
123 struct dst_entry
*dst
;
125 afinfo
= xfrm_policy_get_afinfo(family
);
126 if (unlikely(afinfo
== NULL
))
127 return ERR_PTR(-EAFNOSUPPORT
);
129 dst
= afinfo
->dst_lookup(net
, tos
, saddr
, daddr
);
131 xfrm_policy_put_afinfo(afinfo
);
136 static inline struct dst_entry
*xfrm_dst_lookup(struct xfrm_state
*x
, int tos
,
137 xfrm_address_t
*prev_saddr
,
138 xfrm_address_t
*prev_daddr
,
141 struct net
*net
= xs_net(x
);
142 xfrm_address_t
*saddr
= &x
->props
.saddr
;
143 xfrm_address_t
*daddr
= &x
->id
.daddr
;
144 struct dst_entry
*dst
;
146 if (x
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
) {
150 if (x
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
) {
155 dst
= __xfrm_dst_lookup(net
, tos
, saddr
, daddr
, family
);
158 if (prev_saddr
!= saddr
)
159 memcpy(prev_saddr
, saddr
, sizeof(*prev_saddr
));
160 if (prev_daddr
!= daddr
)
161 memcpy(prev_daddr
, daddr
, sizeof(*prev_daddr
));
167 static inline unsigned long make_jiffies(long secs
)
169 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
170 return MAX_SCHEDULE_TIMEOUT
-1;
175 static void xfrm_policy_timer(unsigned long data
)
177 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
178 unsigned long now
= get_seconds();
179 long next
= LONG_MAX
;
183 read_lock(&xp
->lock
);
185 if (unlikely(xp
->walk
.dead
))
188 dir
= xfrm_policy_id2dir(xp
->index
);
190 if (xp
->lft
.hard_add_expires_seconds
) {
191 long tmo
= xp
->lft
.hard_add_expires_seconds
+
192 xp
->curlft
.add_time
- now
;
198 if (xp
->lft
.hard_use_expires_seconds
) {
199 long tmo
= xp
->lft
.hard_use_expires_seconds
+
200 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
206 if (xp
->lft
.soft_add_expires_seconds
) {
207 long tmo
= xp
->lft
.soft_add_expires_seconds
+
208 xp
->curlft
.add_time
- now
;
211 tmo
= XFRM_KM_TIMEOUT
;
216 if (xp
->lft
.soft_use_expires_seconds
) {
217 long tmo
= xp
->lft
.soft_use_expires_seconds
+
218 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
221 tmo
= XFRM_KM_TIMEOUT
;
228 km_policy_expired(xp
, dir
, 0, 0);
229 if (next
!= LONG_MAX
&&
230 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
234 read_unlock(&xp
->lock
);
239 read_unlock(&xp
->lock
);
240 if (!xfrm_policy_delete(xp
, dir
))
241 km_policy_expired(xp
, dir
, 1, 0);
245 static struct flow_cache_object
*xfrm_policy_flo_get(struct flow_cache_object
*flo
)
247 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
249 if (unlikely(pol
->walk
.dead
))
257 static int xfrm_policy_flo_check(struct flow_cache_object
*flo
)
259 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
261 return !pol
->walk
.dead
;
264 static void xfrm_policy_flo_delete(struct flow_cache_object
*flo
)
266 xfrm_pol_put(container_of(flo
, struct xfrm_policy
, flo
));
269 static const struct flow_cache_ops xfrm_policy_fc_ops
= {
270 .get
= xfrm_policy_flo_get
,
271 .check
= xfrm_policy_flo_check
,
272 .delete = xfrm_policy_flo_delete
,
275 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
279 struct xfrm_policy
*xfrm_policy_alloc(struct net
*net
, gfp_t gfp
)
281 struct xfrm_policy
*policy
;
283 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
286 write_pnet(&policy
->xp_net
, net
);
287 INIT_LIST_HEAD(&policy
->walk
.all
);
288 INIT_HLIST_NODE(&policy
->bydst
);
289 INIT_HLIST_NODE(&policy
->byidx
);
290 rwlock_init(&policy
->lock
);
291 atomic_set(&policy
->refcnt
, 1);
292 skb_queue_head_init(&policy
->polq
.hold_queue
);
293 setup_timer(&policy
->timer
, xfrm_policy_timer
,
294 (unsigned long)policy
);
295 setup_timer(&policy
->polq
.hold_timer
, xfrm_policy_queue_process
,
296 (unsigned long)policy
);
297 policy
->flo
.ops
= &xfrm_policy_fc_ops
;
301 EXPORT_SYMBOL(xfrm_policy_alloc
);
303 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
305 void xfrm_policy_destroy(struct xfrm_policy
*policy
)
307 BUG_ON(!policy
->walk
.dead
);
309 if (del_timer(&policy
->timer
) || del_timer(&policy
->polq
.hold_timer
))
312 security_xfrm_policy_free(policy
->security
);
315 EXPORT_SYMBOL(xfrm_policy_destroy
);
317 static void xfrm_queue_purge(struct sk_buff_head
*list
)
321 while ((skb
= skb_dequeue(list
)) != NULL
)
325 /* Rule must be locked. Release descentant resources, announce
326 * entry dead. The rule must be unlinked from lists to the moment.
329 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
331 policy
->walk
.dead
= 1;
333 atomic_inc(&policy
->genid
);
335 if (del_timer(&policy
->polq
.hold_timer
))
336 xfrm_pol_put(policy
);
337 xfrm_queue_purge(&policy
->polq
.hold_queue
);
339 if (del_timer(&policy
->timer
))
340 xfrm_pol_put(policy
);
342 xfrm_pol_put(policy
);
345 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
347 static inline unsigned int idx_hash(struct net
*net
, u32 index
)
349 return __idx_hash(index
, net
->xfrm
.policy_idx_hmask
);
352 static struct hlist_head
*policy_hash_bysel(struct net
*net
,
353 const struct xfrm_selector
*sel
,
354 unsigned short family
, int dir
)
356 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
357 unsigned int hash
= __sel_hash(sel
, family
, hmask
);
359 return (hash
== hmask
+ 1 ?
360 &net
->xfrm
.policy_inexact
[dir
] :
361 net
->xfrm
.policy_bydst
[dir
].table
+ hash
);
364 static struct hlist_head
*policy_hash_direct(struct net
*net
,
365 const xfrm_address_t
*daddr
,
366 const xfrm_address_t
*saddr
,
367 unsigned short family
, int dir
)
369 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
370 unsigned int hash
= __addr_hash(daddr
, saddr
, family
, hmask
);
372 return net
->xfrm
.policy_bydst
[dir
].table
+ hash
;
375 static void xfrm_dst_hash_transfer(struct hlist_head
*list
,
376 struct hlist_head
*ndsttable
,
377 unsigned int nhashmask
)
379 struct hlist_node
*tmp
, *entry0
= NULL
;
380 struct xfrm_policy
*pol
;
384 hlist_for_each_entry_safe(pol
, tmp
, list
, bydst
) {
387 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
388 pol
->family
, nhashmask
);
390 hlist_del(&pol
->bydst
);
391 hlist_add_head(&pol
->bydst
, ndsttable
+h
);
396 hlist_del(&pol
->bydst
);
397 hlist_add_after(entry0
, &pol
->bydst
);
399 entry0
= &pol
->bydst
;
401 if (!hlist_empty(list
)) {
407 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
408 struct hlist_head
*nidxtable
,
409 unsigned int nhashmask
)
411 struct hlist_node
*tmp
;
412 struct xfrm_policy
*pol
;
414 hlist_for_each_entry_safe(pol
, tmp
, list
, byidx
) {
417 h
= __idx_hash(pol
->index
, nhashmask
);
418 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
422 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
424 return ((old_hmask
+ 1) << 1) - 1;
427 static void xfrm_bydst_resize(struct net
*net
, int dir
)
429 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
430 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
431 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
432 struct hlist_head
*odst
= net
->xfrm
.policy_bydst
[dir
].table
;
433 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
439 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
441 for (i
= hmask
; i
>= 0; i
--)
442 xfrm_dst_hash_transfer(odst
+ i
, ndst
, nhashmask
);
444 net
->xfrm
.policy_bydst
[dir
].table
= ndst
;
445 net
->xfrm
.policy_bydst
[dir
].hmask
= nhashmask
;
447 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
449 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
452 static void xfrm_byidx_resize(struct net
*net
, int total
)
454 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
455 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
456 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
457 struct hlist_head
*oidx
= net
->xfrm
.policy_byidx
;
458 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
464 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
466 for (i
= hmask
; i
>= 0; i
--)
467 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
469 net
->xfrm
.policy_byidx
= nidx
;
470 net
->xfrm
.policy_idx_hmask
= nhashmask
;
472 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
474 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
477 static inline int xfrm_bydst_should_resize(struct net
*net
, int dir
, int *total
)
479 unsigned int cnt
= net
->xfrm
.policy_count
[dir
];
480 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
485 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
492 static inline int xfrm_byidx_should_resize(struct net
*net
, int total
)
494 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
496 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
503 void xfrm_spd_getinfo(struct net
*net
, struct xfrmk_spdinfo
*si
)
505 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
506 si
->incnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
];
507 si
->outcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
];
508 si
->fwdcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
];
509 si
->inscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
510 si
->outscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
511 si
->fwdscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
512 si
->spdhcnt
= net
->xfrm
.policy_idx_hmask
;
513 si
->spdhmcnt
= xfrm_policy_hashmax
;
514 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
516 EXPORT_SYMBOL(xfrm_spd_getinfo
);
518 static DEFINE_MUTEX(hash_resize_mutex
);
519 static void xfrm_hash_resize(struct work_struct
*work
)
521 struct net
*net
= container_of(work
, struct net
, xfrm
.policy_hash_work
);
524 mutex_lock(&hash_resize_mutex
);
527 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
528 if (xfrm_bydst_should_resize(net
, dir
, &total
))
529 xfrm_bydst_resize(net
, dir
);
531 if (xfrm_byidx_should_resize(net
, total
))
532 xfrm_byidx_resize(net
, total
);
534 mutex_unlock(&hash_resize_mutex
);
537 /* Generate new index... KAME seems to generate them ordered by cost
538 * of an absolute inpredictability of ordering of rules. This will not pass. */
539 static u32
xfrm_gen_index(struct net
*net
, int dir
, u32 index
)
541 static u32 idx_generator
;
544 struct hlist_head
*list
;
545 struct xfrm_policy
*p
;
550 idx
= (idx_generator
| dir
);
559 list
= net
->xfrm
.policy_byidx
+ idx_hash(net
, idx
);
561 hlist_for_each_entry(p
, list
, byidx
) {
562 if (p
->index
== idx
) {
572 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
574 u32
*p1
= (u32
*) s1
;
575 u32
*p2
= (u32
*) s2
;
576 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
579 for (i
= 0; i
< len
; i
++) {
587 static void xfrm_policy_requeue(struct xfrm_policy
*old
,
588 struct xfrm_policy
*new)
590 struct xfrm_policy_queue
*pq
= &old
->polq
;
591 struct sk_buff_head list
;
593 __skb_queue_head_init(&list
);
595 spin_lock_bh(&pq
->hold_queue
.lock
);
596 skb_queue_splice_init(&pq
->hold_queue
, &list
);
597 if (del_timer(&pq
->hold_timer
))
599 spin_unlock_bh(&pq
->hold_queue
.lock
);
601 if (skb_queue_empty(&list
))
606 spin_lock_bh(&pq
->hold_queue
.lock
);
607 skb_queue_splice(&list
, &pq
->hold_queue
);
608 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
609 if (!mod_timer(&pq
->hold_timer
, jiffies
))
611 spin_unlock_bh(&pq
->hold_queue
.lock
);
614 static bool xfrm_policy_mark_match(struct xfrm_policy
*policy
,
615 struct xfrm_policy
*pol
)
617 u32 mark
= policy
->mark
.v
& policy
->mark
.m
;
619 if (policy
->mark
.v
== pol
->mark
.v
&& policy
->mark
.m
== pol
->mark
.m
)
622 if ((mark
& pol
->mark
.m
) == pol
->mark
.v
&&
623 policy
->priority
== pol
->priority
)
629 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
631 struct net
*net
= xp_net(policy
);
632 struct xfrm_policy
*pol
;
633 struct xfrm_policy
*delpol
;
634 struct hlist_head
*chain
;
635 struct hlist_node
*newpos
;
637 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
638 chain
= policy_hash_bysel(net
, &policy
->selector
, policy
->family
, dir
);
641 hlist_for_each_entry(pol
, chain
, bydst
) {
642 if (pol
->type
== policy
->type
&&
643 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
644 xfrm_policy_mark_match(policy
, pol
) &&
645 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
648 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
652 if (policy
->priority
> pol
->priority
)
654 } else if (policy
->priority
>= pol
->priority
) {
655 newpos
= &pol
->bydst
;
662 hlist_add_after(newpos
, &policy
->bydst
);
664 hlist_add_head(&policy
->bydst
, chain
);
665 xfrm_pol_hold(policy
);
666 net
->xfrm
.policy_count
[dir
]++;
667 atomic_inc(&net
->xfrm
.flow_cache_genid
);
669 /* After previous checking, family can either be AF_INET or AF_INET6 */
670 if (policy
->family
== AF_INET
)
671 rt_genid_bump_ipv4(net
);
673 rt_genid_bump_ipv6(net
);
676 xfrm_policy_requeue(delpol
, policy
);
677 __xfrm_policy_unlink(delpol
, dir
);
679 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(net
, dir
, policy
->index
);
680 hlist_add_head(&policy
->byidx
, net
->xfrm
.policy_byidx
+idx_hash(net
, policy
->index
));
681 policy
->curlft
.add_time
= get_seconds();
682 policy
->curlft
.use_time
= 0;
683 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
684 xfrm_pol_hold(policy
);
685 list_add(&policy
->walk
.all
, &net
->xfrm
.policy_all
);
686 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
689 xfrm_policy_kill(delpol
);
690 else if (xfrm_bydst_should_resize(net
, dir
, NULL
))
691 schedule_work(&net
->xfrm
.policy_hash_work
);
695 EXPORT_SYMBOL(xfrm_policy_insert
);
697 struct xfrm_policy
*xfrm_policy_bysel_ctx(struct net
*net
, u32 mark
, u8 type
,
698 int dir
, struct xfrm_selector
*sel
,
699 struct xfrm_sec_ctx
*ctx
, int delete,
702 struct xfrm_policy
*pol
, *ret
;
703 struct hlist_head
*chain
;
706 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
707 chain
= policy_hash_bysel(net
, sel
, sel
->family
, dir
);
709 hlist_for_each_entry(pol
, chain
, bydst
) {
710 if (pol
->type
== type
&&
711 (mark
& pol
->mark
.m
) == pol
->mark
.v
&&
712 !selector_cmp(sel
, &pol
->selector
) &&
713 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
716 *err
= security_xfrm_policy_delete(
719 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
722 __xfrm_policy_unlink(pol
, dir
);
728 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
731 xfrm_policy_kill(ret
);
734 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
736 struct xfrm_policy
*xfrm_policy_byid(struct net
*net
, u32 mark
, u8 type
,
737 int dir
, u32 id
, int delete, int *err
)
739 struct xfrm_policy
*pol
, *ret
;
740 struct hlist_head
*chain
;
743 if (xfrm_policy_id2dir(id
) != dir
)
747 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
748 chain
= net
->xfrm
.policy_byidx
+ idx_hash(net
, id
);
750 hlist_for_each_entry(pol
, chain
, byidx
) {
751 if (pol
->type
== type
&& pol
->index
== id
&&
752 (mark
& pol
->mark
.m
) == pol
->mark
.v
) {
755 *err
= security_xfrm_policy_delete(
758 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
761 __xfrm_policy_unlink(pol
, dir
);
767 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
770 xfrm_policy_kill(ret
);
773 EXPORT_SYMBOL(xfrm_policy_byid
);
775 #ifdef CONFIG_SECURITY_NETWORK_XFRM
777 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
781 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
782 struct xfrm_policy
*pol
;
785 hlist_for_each_entry(pol
,
786 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
787 if (pol
->type
!= type
)
789 err
= security_xfrm_policy_delete(pol
->security
);
791 xfrm_audit_policy_delete(pol
, 0, task_valid
);
795 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
796 hlist_for_each_entry(pol
,
797 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
799 if (pol
->type
!= type
)
801 err
= security_xfrm_policy_delete(
804 xfrm_audit_policy_delete(pol
, 0,
815 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
821 int xfrm_policy_flush(struct net
*net
, u8 type
, bool task_valid
)
823 int dir
, err
= 0, cnt
= 0;
825 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
827 err
= xfrm_policy_flush_secctx_check(net
, type
, task_valid
);
831 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
832 struct xfrm_policy
*pol
;
836 hlist_for_each_entry(pol
,
837 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
838 if (pol
->type
!= type
)
840 __xfrm_policy_unlink(pol
, dir
);
841 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
844 xfrm_audit_policy_delete(pol
, 1, task_valid
);
846 xfrm_policy_kill(pol
);
848 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
852 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
854 hlist_for_each_entry(pol
,
855 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
857 if (pol
->type
!= type
)
859 __xfrm_policy_unlink(pol
, dir
);
860 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
863 xfrm_audit_policy_delete(pol
, 1, task_valid
);
864 xfrm_policy_kill(pol
);
866 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
875 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
878 EXPORT_SYMBOL(xfrm_policy_flush
);
880 int xfrm_policy_walk(struct net
*net
, struct xfrm_policy_walk
*walk
,
881 int (*func
)(struct xfrm_policy
*, int, int, void*),
884 struct xfrm_policy
*pol
;
885 struct xfrm_policy_walk_entry
*x
;
888 if (walk
->type
>= XFRM_POLICY_TYPE_MAX
&&
889 walk
->type
!= XFRM_POLICY_TYPE_ANY
)
892 if (list_empty(&walk
->walk
.all
) && walk
->seq
!= 0)
895 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
896 if (list_empty(&walk
->walk
.all
))
897 x
= list_first_entry(&net
->xfrm
.policy_all
, struct xfrm_policy_walk_entry
, all
);
899 x
= list_entry(&walk
->walk
.all
, struct xfrm_policy_walk_entry
, all
);
900 list_for_each_entry_from(x
, &net
->xfrm
.policy_all
, all
) {
903 pol
= container_of(x
, struct xfrm_policy
, walk
);
904 if (walk
->type
!= XFRM_POLICY_TYPE_ANY
&&
905 walk
->type
!= pol
->type
)
907 error
= func(pol
, xfrm_policy_id2dir(pol
->index
),
910 list_move_tail(&walk
->walk
.all
, &x
->all
);
915 if (walk
->seq
== 0) {
919 list_del_init(&walk
->walk
.all
);
921 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
924 EXPORT_SYMBOL(xfrm_policy_walk
);
926 void xfrm_policy_walk_init(struct xfrm_policy_walk
*walk
, u8 type
)
928 INIT_LIST_HEAD(&walk
->walk
.all
);
933 EXPORT_SYMBOL(xfrm_policy_walk_init
);
935 void xfrm_policy_walk_done(struct xfrm_policy_walk
*walk
, struct net
*net
)
937 if (list_empty(&walk
->walk
.all
))
940 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
); /*FIXME where is net? */
941 list_del(&walk
->walk
.all
);
942 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
944 EXPORT_SYMBOL(xfrm_policy_walk_done
);
947 * Find policy to apply to this flow.
949 * Returns 0 if policy found, else an -errno.
951 static int xfrm_policy_match(const struct xfrm_policy
*pol
,
952 const struct flowi
*fl
,
953 u8 type
, u16 family
, int dir
)
955 const struct xfrm_selector
*sel
= &pol
->selector
;
959 if (pol
->family
!= family
||
960 (fl
->flowi_mark
& pol
->mark
.m
) != pol
->mark
.v
||
964 match
= xfrm_selector_match(sel
, fl
, family
);
966 ret
= security_xfrm_policy_lookup(pol
->security
, fl
->flowi_secid
,
972 static struct xfrm_policy
*xfrm_policy_lookup_bytype(struct net
*net
, u8 type
,
973 const struct flowi
*fl
,
977 struct xfrm_policy
*pol
, *ret
;
978 const xfrm_address_t
*daddr
, *saddr
;
979 struct hlist_head
*chain
;
982 daddr
= xfrm_flowi_daddr(fl
, family
);
983 saddr
= xfrm_flowi_saddr(fl
, family
);
984 if (unlikely(!daddr
|| !saddr
))
987 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
988 chain
= policy_hash_direct(net
, daddr
, saddr
, family
, dir
);
990 hlist_for_each_entry(pol
, chain
, bydst
) {
991 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1001 priority
= ret
->priority
;
1005 chain
= &net
->xfrm
.policy_inexact
[dir
];
1006 hlist_for_each_entry(pol
, chain
, bydst
) {
1007 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1015 } else if (pol
->priority
< priority
) {
1023 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1028 static struct xfrm_policy
*
1029 __xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
)
1031 #ifdef CONFIG_XFRM_SUB_POLICY
1032 struct xfrm_policy
*pol
;
1034 pol
= xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
1038 return xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
1041 static int flow_to_policy_dir(int dir
)
1043 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1044 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1045 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1051 return XFRM_POLICY_IN
;
1053 return XFRM_POLICY_OUT
;
1055 return XFRM_POLICY_FWD
;
1059 static struct flow_cache_object
*
1060 xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
,
1061 u8 dir
, struct flow_cache_object
*old_obj
, void *ctx
)
1063 struct xfrm_policy
*pol
;
1066 xfrm_pol_put(container_of(old_obj
, struct xfrm_policy
, flo
));
1068 pol
= __xfrm_policy_lookup(net
, fl
, family
, flow_to_policy_dir(dir
));
1069 if (IS_ERR_OR_NULL(pol
))
1070 return ERR_CAST(pol
);
1072 /* Resolver returns two references:
1073 * one for cache and one for caller of flow_cache_lookup() */
1079 static inline int policy_to_flow_dir(int dir
)
1081 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1082 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1083 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1087 case XFRM_POLICY_IN
:
1089 case XFRM_POLICY_OUT
:
1090 return FLOW_DIR_OUT
;
1091 case XFRM_POLICY_FWD
:
1092 return FLOW_DIR_FWD
;
1096 static struct xfrm_policy
*xfrm_sk_policy_lookup(struct sock
*sk
, int dir
,
1097 const struct flowi
*fl
)
1099 struct xfrm_policy
*pol
;
1100 struct net
*net
= sock_net(sk
);
1102 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1103 if ((pol
= sk
->sk_policy
[dir
]) != NULL
) {
1104 bool match
= xfrm_selector_match(&pol
->selector
, fl
,
1109 if ((sk
->sk_mark
& pol
->mark
.m
) != pol
->mark
.v
) {
1113 err
= security_xfrm_policy_lookup(pol
->security
,
1115 policy_to_flow_dir(dir
));
1118 else if (err
== -ESRCH
)
1126 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1130 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1132 struct net
*net
= xp_net(pol
);
1133 struct hlist_head
*chain
= policy_hash_bysel(net
, &pol
->selector
,
1136 list_add(&pol
->walk
.all
, &net
->xfrm
.policy_all
);
1137 hlist_add_head(&pol
->bydst
, chain
);
1138 hlist_add_head(&pol
->byidx
, net
->xfrm
.policy_byidx
+idx_hash(net
, pol
->index
));
1139 net
->xfrm
.policy_count
[dir
]++;
1142 if (xfrm_bydst_should_resize(net
, dir
, NULL
))
1143 schedule_work(&net
->xfrm
.policy_hash_work
);
1146 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1149 struct net
*net
= xp_net(pol
);
1151 if (hlist_unhashed(&pol
->bydst
))
1154 hlist_del_init(&pol
->bydst
);
1155 hlist_del(&pol
->byidx
);
1156 list_del(&pol
->walk
.all
);
1157 net
->xfrm
.policy_count
[dir
]--;
1162 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1164 struct net
*net
= xp_net(pol
);
1166 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1167 pol
= __xfrm_policy_unlink(pol
, dir
);
1168 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1170 xfrm_policy_kill(pol
);
1175 EXPORT_SYMBOL(xfrm_policy_delete
);
1177 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1179 struct net
*net
= xp_net(pol
);
1180 struct xfrm_policy
*old_pol
;
1182 #ifdef CONFIG_XFRM_SUB_POLICY
1183 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1187 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1188 old_pol
= sk
->sk_policy
[dir
];
1189 sk
->sk_policy
[dir
] = pol
;
1191 pol
->curlft
.add_time
= get_seconds();
1192 pol
->index
= xfrm_gen_index(net
, XFRM_POLICY_MAX
+dir
, 0);
1193 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+dir
);
1197 xfrm_policy_requeue(old_pol
, pol
);
1199 /* Unlinking succeeds always. This is the only function
1200 * allowed to delete or replace socket policy.
1202 __xfrm_policy_unlink(old_pol
, XFRM_POLICY_MAX
+dir
);
1204 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1207 xfrm_policy_kill(old_pol
);
1212 static struct xfrm_policy
*clone_policy(const struct xfrm_policy
*old
, int dir
)
1214 struct xfrm_policy
*newp
= xfrm_policy_alloc(xp_net(old
), GFP_ATOMIC
);
1215 struct net
*net
= xp_net(old
);
1218 newp
->selector
= old
->selector
;
1219 if (security_xfrm_policy_clone(old
->security
,
1222 return NULL
; /* ENOMEM */
1224 newp
->lft
= old
->lft
;
1225 newp
->curlft
= old
->curlft
;
1226 newp
->mark
= old
->mark
;
1227 newp
->action
= old
->action
;
1228 newp
->flags
= old
->flags
;
1229 newp
->xfrm_nr
= old
->xfrm_nr
;
1230 newp
->index
= old
->index
;
1231 newp
->type
= old
->type
;
1232 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1233 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1234 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1235 __xfrm_policy_link(newp
, XFRM_POLICY_MAX
+dir
);
1236 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1242 int __xfrm_sk_clone_policy(struct sock
*sk
)
1244 struct xfrm_policy
*p0
= sk
->sk_policy
[0],
1245 *p1
= sk
->sk_policy
[1];
1247 sk
->sk_policy
[0] = sk
->sk_policy
[1] = NULL
;
1248 if (p0
&& (sk
->sk_policy
[0] = clone_policy(p0
, 0)) == NULL
)
1250 if (p1
&& (sk
->sk_policy
[1] = clone_policy(p1
, 1)) == NULL
)
1256 xfrm_get_saddr(struct net
*net
, xfrm_address_t
*local
, xfrm_address_t
*remote
,
1257 unsigned short family
)
1260 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1262 if (unlikely(afinfo
== NULL
))
1264 err
= afinfo
->get_saddr(net
, local
, remote
);
1265 xfrm_policy_put_afinfo(afinfo
);
1269 /* Resolve list of templates for the flow, given policy. */
1272 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, const struct flowi
*fl
,
1273 struct xfrm_state
**xfrm
, unsigned short family
)
1275 struct net
*net
= xp_net(policy
);
1278 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1279 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1282 for (nx
= 0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1283 struct xfrm_state
*x
;
1284 xfrm_address_t
*remote
= daddr
;
1285 xfrm_address_t
*local
= saddr
;
1286 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1288 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1289 tmpl
->mode
== XFRM_MODE_BEET
) {
1290 remote
= &tmpl
->id
.daddr
;
1291 local
= &tmpl
->saddr
;
1292 if (xfrm_addr_any(local
, tmpl
->encap_family
)) {
1293 error
= xfrm_get_saddr(net
, &tmp
, remote
, tmpl
->encap_family
);
1300 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1302 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1309 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1312 } else if (error
== -ESRCH
) {
1316 if (!tmpl
->optional
)
1322 for (nx
--; nx
>= 0; nx
--)
1323 xfrm_state_put(xfrm
[nx
]);
1328 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, const struct flowi
*fl
,
1329 struct xfrm_state
**xfrm
, unsigned short family
)
1331 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1332 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1338 for (i
= 0; i
< npols
; i
++) {
1339 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1344 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1352 /* found states are sorted for outbound processing */
1354 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1359 for (cnx
--; cnx
>= 0; cnx
--)
1360 xfrm_state_put(tpp
[cnx
]);
1365 /* Check that the bundle accepts the flow and its components are
1369 static inline int xfrm_get_tos(const struct flowi
*fl
, int family
)
1371 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1377 tos
= afinfo
->get_tos(fl
);
1379 xfrm_policy_put_afinfo(afinfo
);
1384 static struct flow_cache_object
*xfrm_bundle_flo_get(struct flow_cache_object
*flo
)
1386 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1387 struct dst_entry
*dst
= &xdst
->u
.dst
;
1389 if (xdst
->route
== NULL
) {
1390 /* Dummy bundle - if it has xfrms we were not
1391 * able to build bundle as template resolution failed.
1392 * It means we need to try again resolving. */
1393 if (xdst
->num_xfrms
> 0)
1395 } else if (dst
->flags
& DST_XFRM_QUEUE
) {
1399 if (stale_bundle(dst
))
1407 static int xfrm_bundle_flo_check(struct flow_cache_object
*flo
)
1409 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1410 struct dst_entry
*dst
= &xdst
->u
.dst
;
1414 if (stale_bundle(dst
))
1420 static void xfrm_bundle_flo_delete(struct flow_cache_object
*flo
)
1422 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1423 struct dst_entry
*dst
= &xdst
->u
.dst
;
1428 static const struct flow_cache_ops xfrm_bundle_fc_ops
= {
1429 .get
= xfrm_bundle_flo_get
,
1430 .check
= xfrm_bundle_flo_check
,
1431 .delete = xfrm_bundle_flo_delete
,
1434 static inline struct xfrm_dst
*xfrm_alloc_dst(struct net
*net
, int family
)
1436 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1437 struct dst_ops
*dst_ops
;
1438 struct xfrm_dst
*xdst
;
1441 return ERR_PTR(-EINVAL
);
1445 dst_ops
= &net
->xfrm
.xfrm4_dst_ops
;
1447 #if IS_ENABLED(CONFIG_IPV6)
1449 dst_ops
= &net
->xfrm
.xfrm6_dst_ops
;
1455 xdst
= dst_alloc(dst_ops
, NULL
, 0, DST_OBSOLETE_NONE
, 0);
1458 struct dst_entry
*dst
= &xdst
->u
.dst
;
1460 memset(dst
+ 1, 0, sizeof(*xdst
) - sizeof(*dst
));
1461 xdst
->flo
.ops
= &xfrm_bundle_fc_ops
;
1462 if (afinfo
->init_dst
)
1463 afinfo
->init_dst(net
, xdst
);
1465 xdst
= ERR_PTR(-ENOBUFS
);
1467 xfrm_policy_put_afinfo(afinfo
);
1472 static inline int xfrm_init_path(struct xfrm_dst
*path
, struct dst_entry
*dst
,
1475 struct xfrm_policy_afinfo
*afinfo
=
1476 xfrm_policy_get_afinfo(dst
->ops
->family
);
1482 err
= afinfo
->init_path(path
, dst
, nfheader_len
);
1484 xfrm_policy_put_afinfo(afinfo
);
1489 static inline int xfrm_fill_dst(struct xfrm_dst
*xdst
, struct net_device
*dev
,
1490 const struct flowi
*fl
)
1492 struct xfrm_policy_afinfo
*afinfo
=
1493 xfrm_policy_get_afinfo(xdst
->u
.dst
.ops
->family
);
1499 err
= afinfo
->fill_dst(xdst
, dev
, fl
);
1501 xfrm_policy_put_afinfo(afinfo
);
1507 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1508 * all the metrics... Shortly, bundle a bundle.
1511 static struct dst_entry
*xfrm_bundle_create(struct xfrm_policy
*policy
,
1512 struct xfrm_state
**xfrm
, int nx
,
1513 const struct flowi
*fl
,
1514 struct dst_entry
*dst
)
1516 struct net
*net
= xp_net(policy
);
1517 unsigned long now
= jiffies
;
1518 struct net_device
*dev
;
1519 struct xfrm_mode
*inner_mode
;
1520 struct dst_entry
*dst_prev
= NULL
;
1521 struct dst_entry
*dst0
= NULL
;
1525 int nfheader_len
= 0;
1526 int trailer_len
= 0;
1528 int family
= policy
->selector
.family
;
1529 xfrm_address_t saddr
, daddr
;
1531 xfrm_flowi_addr_get(fl
, &saddr
, &daddr
, family
);
1533 tos
= xfrm_get_tos(fl
, family
);
1540 for (; i
< nx
; i
++) {
1541 struct xfrm_dst
*xdst
= xfrm_alloc_dst(net
, family
);
1542 struct dst_entry
*dst1
= &xdst
->u
.dst
;
1544 err
= PTR_ERR(xdst
);
1550 if (xfrm
[i
]->sel
.family
== AF_UNSPEC
) {
1551 inner_mode
= xfrm_ip2inner_mode(xfrm
[i
],
1552 xfrm_af2proto(family
));
1554 err
= -EAFNOSUPPORT
;
1559 inner_mode
= xfrm
[i
]->inner_mode
;
1564 dst_prev
->child
= dst_clone(dst1
);
1565 dst1
->flags
|= DST_NOHASH
;
1569 dst_copy_metrics(dst1
, dst
);
1571 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1572 family
= xfrm
[i
]->props
.family
;
1573 dst
= xfrm_dst_lookup(xfrm
[i
], tos
, &saddr
, &daddr
,
1581 dst1
->xfrm
= xfrm
[i
];
1582 xdst
->xfrm_genid
= xfrm
[i
]->genid
;
1584 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
1585 dst1
->flags
|= DST_HOST
;
1586 dst1
->lastuse
= now
;
1588 dst1
->input
= dst_discard
;
1589 dst1
->output
= inner_mode
->afinfo
->output
;
1591 dst1
->next
= dst_prev
;
1594 header_len
+= xfrm
[i
]->props
.header_len
;
1595 if (xfrm
[i
]->type
->flags
& XFRM_TYPE_NON_FRAGMENT
)
1596 nfheader_len
+= xfrm
[i
]->props
.header_len
;
1597 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
1600 dst_prev
->child
= dst
;
1608 xfrm_init_path((struct xfrm_dst
*)dst0
, dst
, nfheader_len
);
1609 xfrm_init_pmtu(dst_prev
);
1611 for (dst_prev
= dst0
; dst_prev
!= dst
; dst_prev
= dst_prev
->child
) {
1612 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst_prev
;
1614 err
= xfrm_fill_dst(xdst
, dev
, fl
);
1618 dst_prev
->header_len
= header_len
;
1619 dst_prev
->trailer_len
= trailer_len
;
1620 header_len
-= xdst
->u
.dst
.xfrm
->props
.header_len
;
1621 trailer_len
-= xdst
->u
.dst
.xfrm
->props
.trailer_len
;
1629 xfrm_state_put(xfrm
[i
]);
1633 dst0
= ERR_PTR(err
);
1637 static int xfrm_expand_policies(const struct flowi
*fl
, u16 family
,
1638 struct xfrm_policy
**pols
,
1639 int *num_pols
, int *num_xfrms
)
1643 if (*num_pols
== 0 || !pols
[0]) {
1648 if (IS_ERR(pols
[0]))
1649 return PTR_ERR(pols
[0]);
1651 *num_xfrms
= pols
[0]->xfrm_nr
;
1653 #ifdef CONFIG_XFRM_SUB_POLICY
1654 if (pols
[0] && pols
[0]->action
== XFRM_POLICY_ALLOW
&&
1655 pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1656 pols
[1] = xfrm_policy_lookup_bytype(xp_net(pols
[0]),
1657 XFRM_POLICY_TYPE_MAIN
,
1661 if (IS_ERR(pols
[1])) {
1662 xfrm_pols_put(pols
, *num_pols
);
1663 return PTR_ERR(pols
[1]);
1666 (*num_xfrms
) += pols
[1]->xfrm_nr
;
1670 for (i
= 0; i
< *num_pols
; i
++) {
1671 if (pols
[i
]->action
!= XFRM_POLICY_ALLOW
) {
1681 static struct xfrm_dst
*
1682 xfrm_resolve_and_create_bundle(struct xfrm_policy
**pols
, int num_pols
,
1683 const struct flowi
*fl
, u16 family
,
1684 struct dst_entry
*dst_orig
)
1686 struct net
*net
= xp_net(pols
[0]);
1687 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1688 struct dst_entry
*dst
;
1689 struct xfrm_dst
*xdst
;
1692 /* Try to instantiate a bundle */
1693 err
= xfrm_tmpl_resolve(pols
, num_pols
, fl
, xfrm
, family
);
1695 if (err
!= 0 && err
!= -EAGAIN
)
1696 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1697 return ERR_PTR(err
);
1700 dst
= xfrm_bundle_create(pols
[0], xfrm
, err
, fl
, dst_orig
);
1702 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLEGENERROR
);
1703 return ERR_CAST(dst
);
1706 xdst
= (struct xfrm_dst
*)dst
;
1707 xdst
->num_xfrms
= err
;
1708 xdst
->num_pols
= num_pols
;
1709 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1710 xdst
->policy_genid
= atomic_read(&pols
[0]->genid
);
1715 static void xfrm_policy_queue_process(unsigned long arg
)
1718 struct sk_buff
*skb
;
1720 struct dst_entry
*dst
;
1721 struct xfrm_policy
*pol
= (struct xfrm_policy
*)arg
;
1722 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1724 struct sk_buff_head list
;
1726 spin_lock(&pq
->hold_queue
.lock
);
1727 skb
= skb_peek(&pq
->hold_queue
);
1729 spin_unlock(&pq
->hold_queue
.lock
);
1734 xfrm_decode_session(skb
, &fl
, dst
->ops
->family
);
1735 spin_unlock(&pq
->hold_queue
.lock
);
1737 dst_hold(dst
->path
);
1738 dst
= xfrm_lookup(xp_net(pol
), dst
->path
, &fl
,
1743 if (dst
->flags
& DST_XFRM_QUEUE
) {
1746 if (pq
->timeout
>= XFRM_QUEUE_TMO_MAX
)
1749 pq
->timeout
= pq
->timeout
<< 1;
1750 if (!mod_timer(&pq
->hold_timer
, jiffies
+ pq
->timeout
))
1757 __skb_queue_head_init(&list
);
1759 spin_lock(&pq
->hold_queue
.lock
);
1761 skb_queue_splice_init(&pq
->hold_queue
, &list
);
1762 spin_unlock(&pq
->hold_queue
.lock
);
1764 while (!skb_queue_empty(&list
)) {
1765 skb
= __skb_dequeue(&list
);
1767 xfrm_decode_session(skb
, &fl
, skb_dst(skb
)->ops
->family
);
1768 dst_hold(skb_dst(skb
)->path
);
1769 dst
= xfrm_lookup(xp_net(pol
), skb_dst(skb
)->path
,
1778 skb_dst_set(skb
, dst
);
1780 err
= dst_output(skb
);
1789 xfrm_queue_purge(&pq
->hold_queue
);
1793 static int xdst_queue_output(struct sock
*sk
, struct sk_buff
*skb
)
1795 unsigned long sched_next
;
1796 struct dst_entry
*dst
= skb_dst(skb
);
1797 struct xfrm_dst
*xdst
= (struct xfrm_dst
*) dst
;
1798 struct xfrm_policy
*pol
= xdst
->pols
[0];
1799 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1800 const struct sk_buff
*fclone
= skb
+ 1;
1802 if (unlikely(skb
->fclone
== SKB_FCLONE_ORIG
&&
1803 fclone
->fclone
== SKB_FCLONE_CLONE
)) {
1808 if (pq
->hold_queue
.qlen
> XFRM_MAX_QUEUE_LEN
) {
1815 spin_lock_bh(&pq
->hold_queue
.lock
);
1818 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
1820 sched_next
= jiffies
+ pq
->timeout
;
1822 if (del_timer(&pq
->hold_timer
)) {
1823 if (time_before(pq
->hold_timer
.expires
, sched_next
))
1824 sched_next
= pq
->hold_timer
.expires
;
1828 __skb_queue_tail(&pq
->hold_queue
, skb
);
1829 if (!mod_timer(&pq
->hold_timer
, sched_next
))
1832 spin_unlock_bh(&pq
->hold_queue
.lock
);
1837 static struct xfrm_dst
*xfrm_create_dummy_bundle(struct net
*net
,
1838 struct xfrm_flo
*xflo
,
1839 const struct flowi
*fl
,
1844 struct net_device
*dev
;
1845 struct dst_entry
*dst
;
1846 struct dst_entry
*dst1
;
1847 struct xfrm_dst
*xdst
;
1849 xdst
= xfrm_alloc_dst(net
, family
);
1853 if (!(xflo
->flags
& XFRM_LOOKUP_QUEUE
) ||
1854 net
->xfrm
.sysctl_larval_drop
||
1858 dst
= xflo
->dst_orig
;
1859 dst1
= &xdst
->u
.dst
;
1863 dst_copy_metrics(dst1
, dst
);
1865 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
1866 dst1
->flags
|= DST_HOST
| DST_XFRM_QUEUE
;
1867 dst1
->lastuse
= jiffies
;
1869 dst1
->input
= dst_discard
;
1870 dst1
->output
= xdst_queue_output
;
1876 xfrm_init_path((struct xfrm_dst
*)dst1
, dst
, 0);
1883 err
= xfrm_fill_dst(xdst
, dev
, fl
);
1892 xdst
= ERR_PTR(err
);
1896 static struct flow_cache_object
*
1897 xfrm_bundle_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
,
1898 struct flow_cache_object
*oldflo
, void *ctx
)
1900 struct xfrm_flo
*xflo
= (struct xfrm_flo
*)ctx
;
1901 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1902 struct xfrm_dst
*xdst
, *new_xdst
;
1903 int num_pols
= 0, num_xfrms
= 0, i
, err
, pol_dead
;
1905 /* Check if the policies from old bundle are usable */
1908 xdst
= container_of(oldflo
, struct xfrm_dst
, flo
);
1909 num_pols
= xdst
->num_pols
;
1910 num_xfrms
= xdst
->num_xfrms
;
1912 for (i
= 0; i
< num_pols
; i
++) {
1913 pols
[i
] = xdst
->pols
[i
];
1914 pol_dead
|= pols
[i
]->walk
.dead
;
1917 dst_free(&xdst
->u
.dst
);
1925 /* Resolve policies to use if we couldn't get them from
1926 * previous cache entry */
1929 pols
[0] = __xfrm_policy_lookup(net
, fl
, family
,
1930 flow_to_policy_dir(dir
));
1931 err
= xfrm_expand_policies(fl
, family
, pols
,
1932 &num_pols
, &num_xfrms
);
1938 goto make_dummy_bundle
;
1941 new_xdst
= xfrm_resolve_and_create_bundle(pols
, num_pols
, fl
, family
,
1943 if (IS_ERR(new_xdst
)) {
1944 err
= PTR_ERR(new_xdst
);
1948 goto make_dummy_bundle
;
1949 dst_hold(&xdst
->u
.dst
);
1951 } else if (new_xdst
== NULL
) {
1954 goto make_dummy_bundle
;
1955 xdst
->num_xfrms
= 0;
1956 dst_hold(&xdst
->u
.dst
);
1960 /* Kill the previous bundle */
1962 /* The policies were stolen for newly generated bundle */
1964 dst_free(&xdst
->u
.dst
);
1967 /* Flow cache does not have reference, it dst_free()'s,
1968 * but we do need to return one reference for original caller */
1969 dst_hold(&new_xdst
->u
.dst
);
1970 return &new_xdst
->flo
;
1973 /* We found policies, but there's no bundles to instantiate:
1974 * either because the policy blocks, has no transformations or
1975 * we could not build template (no xfrm_states).*/
1976 xdst
= xfrm_create_dummy_bundle(net
, xflo
, fl
, num_xfrms
, family
);
1978 xfrm_pols_put(pols
, num_pols
);
1979 return ERR_CAST(xdst
);
1981 xdst
->num_pols
= num_pols
;
1982 xdst
->num_xfrms
= num_xfrms
;
1983 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1985 dst_hold(&xdst
->u
.dst
);
1989 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1992 dst_free(&xdst
->u
.dst
);
1994 xfrm_pols_put(pols
, num_pols
);
1995 return ERR_PTR(err
);
1998 static struct dst_entry
*make_blackhole(struct net
*net
, u16 family
,
1999 struct dst_entry
*dst_orig
)
2001 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2002 struct dst_entry
*ret
;
2005 dst_release(dst_orig
);
2006 return ERR_PTR(-EINVAL
);
2008 ret
= afinfo
->blackhole_route(net
, dst_orig
);
2010 xfrm_policy_put_afinfo(afinfo
);
2015 /* Main function: finds/creates a bundle for given flow.
2017 * At the moment we eat a raw IP route. Mostly to speed up lookups
2018 * on interfaces with disabled IPsec.
2020 struct dst_entry
*xfrm_lookup(struct net
*net
, struct dst_entry
*dst_orig
,
2021 const struct flowi
*fl
,
2022 struct sock
*sk
, int flags
)
2024 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2025 struct flow_cache_object
*flo
;
2026 struct xfrm_dst
*xdst
;
2027 struct dst_entry
*dst
, *route
;
2028 u16 family
= dst_orig
->ops
->family
;
2029 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
2030 int i
, err
, num_pols
, num_xfrms
= 0, drop_pols
= 0;
2036 if (sk
&& sk
->sk_policy
[XFRM_POLICY_OUT
]) {
2038 pols
[0] = xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
);
2039 err
= xfrm_expand_policies(fl
, family
, pols
,
2040 &num_pols
, &num_xfrms
);
2045 if (num_xfrms
<= 0) {
2046 drop_pols
= num_pols
;
2050 xdst
= xfrm_resolve_and_create_bundle(
2054 xfrm_pols_put(pols
, num_pols
);
2055 err
= PTR_ERR(xdst
);
2057 } else if (xdst
== NULL
) {
2059 drop_pols
= num_pols
;
2063 dst_hold(&xdst
->u
.dst
);
2064 xdst
->u
.dst
.flags
|= DST_NOCACHE
;
2065 route
= xdst
->route
;
2070 struct xfrm_flo xflo
;
2072 xflo
.dst_orig
= dst_orig
;
2075 /* To accelerate a bit... */
2076 if ((dst_orig
->flags
& DST_NOXFRM
) ||
2077 !net
->xfrm
.policy_count
[XFRM_POLICY_OUT
])
2080 flo
= flow_cache_lookup(net
, fl
, family
, dir
,
2081 xfrm_bundle_lookup
, &xflo
);
2088 xdst
= container_of(flo
, struct xfrm_dst
, flo
);
2090 num_pols
= xdst
->num_pols
;
2091 num_xfrms
= xdst
->num_xfrms
;
2092 memcpy(pols
, xdst
->pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2093 route
= xdst
->route
;
2097 if (route
== NULL
&& num_xfrms
> 0) {
2098 /* The only case when xfrm_bundle_lookup() returns a
2099 * bundle with null route, is when the template could
2100 * not be resolved. It means policies are there, but
2101 * bundle could not be created, since we don't yet
2102 * have the xfrm_state's. We need to wait for KM to
2103 * negotiate new SA's or bail out with error.*/
2104 if (net
->xfrm
.sysctl_larval_drop
) {
2105 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2112 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2120 if ((flags
& XFRM_LOOKUP_ICMP
) &&
2121 !(pols
[0]->flags
& XFRM_POLICY_ICMP
)) {
2126 for (i
= 0; i
< num_pols
; i
++)
2127 pols
[i
]->curlft
.use_time
= get_seconds();
2129 if (num_xfrms
< 0) {
2130 /* Prohibit the flow */
2131 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLBLOCK
);
2134 } else if (num_xfrms
> 0) {
2135 /* Flow transformed */
2136 dst_release(dst_orig
);
2138 /* Flow passes untransformed */
2143 xfrm_pols_put(pols
, drop_pols
);
2144 if (dst
&& dst
->xfrm
&&
2145 dst
->xfrm
->props
.mode
== XFRM_MODE_TUNNEL
)
2146 dst
->flags
|= DST_XFRM_TUNNEL
;
2150 if (!(flags
& XFRM_LOOKUP_ICMP
)) {
2158 if (!(flags
& XFRM_LOOKUP_KEEP_DST_REF
))
2159 dst_release(dst_orig
);
2160 xfrm_pols_put(pols
, drop_pols
);
2161 return ERR_PTR(err
);
2163 EXPORT_SYMBOL(xfrm_lookup
);
2165 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2166 * Otherwise we may send out blackholed packets.
2168 struct dst_entry
*xfrm_lookup_route(struct net
*net
, struct dst_entry
*dst_orig
,
2169 const struct flowi
*fl
,
2170 struct sock
*sk
, int flags
)
2172 struct dst_entry
*dst
= xfrm_lookup(net
, dst_orig
, fl
, sk
,
2173 flags
| XFRM_LOOKUP_QUEUE
|
2174 XFRM_LOOKUP_KEEP_DST_REF
);
2176 if (IS_ERR(dst
) && PTR_ERR(dst
) == -EREMOTE
)
2177 return make_blackhole(net
, dst_orig
->ops
->family
, dst_orig
);
2181 EXPORT_SYMBOL(xfrm_lookup_route
);
2184 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, const struct flowi
*fl
)
2186 struct xfrm_state
*x
;
2188 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
2190 x
= skb
->sp
->xvec
[idx
];
2191 if (!x
->type
->reject
)
2193 return x
->type
->reject(x
, skb
, fl
);
2196 /* When skb is transformed back to its "native" form, we have to
2197 * check policy restrictions. At the moment we make this in maximally
2198 * stupid way. Shame on me. :-) Of course, connected sockets must
2199 * have policy cached at them.
2203 xfrm_state_ok(const struct xfrm_tmpl
*tmpl
, const struct xfrm_state
*x
,
2204 unsigned short family
)
2206 if (xfrm_state_kern(x
))
2207 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
2208 return x
->id
.proto
== tmpl
->id
.proto
&&
2209 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
2210 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
2211 x
->props
.mode
== tmpl
->mode
&&
2212 (tmpl
->allalgs
|| (tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
2213 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
2214 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
2215 xfrm_state_addr_cmp(tmpl
, x
, family
));
2219 * 0 or more than 0 is returned when validation is succeeded (either bypass
2220 * because of optional transport mode, or next index of the mathced secpath
2221 * state with the template.
2222 * -1 is returned when no matching template is found.
2223 * Otherwise "-2 - errored_index" is returned.
2226 xfrm_policy_ok(const struct xfrm_tmpl
*tmpl
, const struct sec_path
*sp
, int start
,
2227 unsigned short family
)
2231 if (tmpl
->optional
) {
2232 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
2236 for (; idx
< sp
->len
; idx
++) {
2237 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
2239 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2248 int __xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
,
2249 unsigned int family
, int reverse
)
2251 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2254 if (unlikely(afinfo
== NULL
))
2255 return -EAFNOSUPPORT
;
2257 afinfo
->decode_session(skb
, fl
, reverse
);
2258 err
= security_xfrm_decode_session(skb
, &fl
->flowi_secid
);
2259 xfrm_policy_put_afinfo(afinfo
);
2262 EXPORT_SYMBOL(__xfrm_decode_session
);
2264 static inline int secpath_has_nontransport(const struct sec_path
*sp
, int k
, int *idxp
)
2266 for (; k
< sp
->len
; k
++) {
2267 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2276 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
2277 unsigned short family
)
2279 struct net
*net
= dev_net(skb
->dev
);
2280 struct xfrm_policy
*pol
;
2281 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2290 reverse
= dir
& ~XFRM_POLICY_MASK
;
2291 dir
&= XFRM_POLICY_MASK
;
2292 fl_dir
= policy_to_flow_dir(dir
);
2294 if (__xfrm_decode_session(skb
, &fl
, family
, reverse
) < 0) {
2295 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
2299 nf_nat_decode_session(skb
, &fl
, family
);
2301 /* First, check used SA against their selectors. */
2305 for (i
= skb
->sp
->len
-1; i
>= 0; i
--) {
2306 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
2307 if (!xfrm_selector_match(&x
->sel
, &fl
, family
)) {
2308 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMISMATCH
);
2315 if (sk
&& sk
->sk_policy
[dir
]) {
2316 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
);
2318 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2324 struct flow_cache_object
*flo
;
2326 flo
= flow_cache_lookup(net
, &fl
, family
, fl_dir
,
2327 xfrm_policy_lookup
, NULL
);
2328 if (IS_ERR_OR_NULL(flo
))
2329 pol
= ERR_CAST(flo
);
2331 pol
= container_of(flo
, struct xfrm_policy
, flo
);
2335 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2340 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
2341 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2342 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINNOPOLS
);
2348 pol
->curlft
.use_time
= get_seconds();
2352 #ifdef CONFIG_XFRM_SUB_POLICY
2353 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
2354 pols
[1] = xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
,
2358 if (IS_ERR(pols
[1])) {
2359 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2362 pols
[1]->curlft
.use_time
= get_seconds();
2368 if (pol
->action
== XFRM_POLICY_ALLOW
) {
2369 struct sec_path
*sp
;
2370 static struct sec_path dummy
;
2371 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
2372 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
2373 struct xfrm_tmpl
**tpp
= tp
;
2377 if ((sp
= skb
->sp
) == NULL
)
2380 for (pi
= 0; pi
< npols
; pi
++) {
2381 if (pols
[pi
] != pol
&&
2382 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
) {
2383 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2386 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
2387 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
2390 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
2391 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
2395 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
, net
);
2399 /* For each tunnel xfrm, find the first matching tmpl.
2400 * For each tmpl before that, find corresponding xfrm.
2401 * Order is _important_. Later we will implement
2402 * some barriers, but at the moment barriers
2403 * are implied between each two transformations.
2405 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
2406 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
2409 /* "-2 - errored_index" returned */
2411 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2416 if (secpath_has_nontransport(sp
, k
, &xerr_idx
)) {
2417 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2421 xfrm_pols_put(pols
, npols
);
2424 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2427 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2429 xfrm_pols_put(pols
, npols
);
2432 EXPORT_SYMBOL(__xfrm_policy_check
);
2434 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
2436 struct net
*net
= dev_net(skb
->dev
);
2438 struct dst_entry
*dst
;
2441 if (xfrm_decode_session(skb
, &fl
, family
) < 0) {
2442 XFRM_INC_STATS(net
, LINUX_MIB_XFRMFWDHDRERROR
);
2448 dst
= xfrm_lookup(net
, skb_dst(skb
), &fl
, NULL
, XFRM_LOOKUP_QUEUE
);
2453 skb_dst_set(skb
, dst
);
2456 EXPORT_SYMBOL(__xfrm_route_forward
);
2458 /* Optimize later using cookies and generation ids. */
2460 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
2462 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2463 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2464 * get validated by dst_ops->check on every use. We do this
2465 * because when a normal route referenced by an XFRM dst is
2466 * obsoleted we do not go looking around for all parent
2467 * referencing XFRM dsts so that we can invalidate them. It
2468 * is just too much work. Instead we make the checks here on
2469 * every use. For example:
2471 * XFRM dst A --> IPv4 dst X
2473 * X is the "xdst->route" of A (X is also the "dst->path" of A
2474 * in this example). If X is marked obsolete, "A" will not
2475 * notice. That's what we are validating here via the
2476 * stale_bundle() check.
2478 * When a policy's bundle is pruned, we dst_free() the XFRM
2479 * dst which causes it's ->obsolete field to be set to
2480 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2481 * this, we want to force a new route lookup.
2483 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
2489 static int stale_bundle(struct dst_entry
*dst
)
2491 return !xfrm_bundle_ok((struct xfrm_dst
*)dst
);
2494 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
2496 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
2497 dst
->dev
= dev_net(dev
)->loopback_dev
;
2502 EXPORT_SYMBOL(xfrm_dst_ifdown
);
2504 static void xfrm_link_failure(struct sk_buff
*skb
)
2506 /* Impossible. Such dst must be popped before reaches point of failure. */
2509 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
2512 if (dst
->obsolete
) {
2520 void xfrm_garbage_collect(struct net
*net
)
2522 flow_cache_flush(net
);
2524 EXPORT_SYMBOL(xfrm_garbage_collect
);
2526 static void xfrm_garbage_collect_deferred(struct net
*net
)
2528 flow_cache_flush_deferred(net
);
2531 static void xfrm_init_pmtu(struct dst_entry
*dst
)
2534 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2535 u32 pmtu
, route_mtu_cached
;
2537 pmtu
= dst_mtu(dst
->child
);
2538 xdst
->child_mtu_cached
= pmtu
;
2540 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2542 route_mtu_cached
= dst_mtu(xdst
->route
);
2543 xdst
->route_mtu_cached
= route_mtu_cached
;
2545 if (pmtu
> route_mtu_cached
)
2546 pmtu
= route_mtu_cached
;
2548 dst_metric_set(dst
, RTAX_MTU
, pmtu
);
2549 } while ((dst
= dst
->next
));
2552 /* Check that the bundle accepts the flow and its components are
2556 static int xfrm_bundle_ok(struct xfrm_dst
*first
)
2558 struct dst_entry
*dst
= &first
->u
.dst
;
2559 struct xfrm_dst
*last
;
2562 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2563 (dst
->dev
&& !netif_running(dst
->dev
)))
2566 if (dst
->flags
& DST_XFRM_QUEUE
)
2572 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2574 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2576 if (xdst
->xfrm_genid
!= dst
->xfrm
->genid
)
2578 if (xdst
->num_pols
> 0 &&
2579 xdst
->policy_genid
!= atomic_read(&xdst
->pols
[0]->genid
))
2582 mtu
= dst_mtu(dst
->child
);
2583 if (xdst
->child_mtu_cached
!= mtu
) {
2585 xdst
->child_mtu_cached
= mtu
;
2588 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2590 mtu
= dst_mtu(xdst
->route
);
2591 if (xdst
->route_mtu_cached
!= mtu
) {
2593 xdst
->route_mtu_cached
= mtu
;
2597 } while (dst
->xfrm
);
2602 mtu
= last
->child_mtu_cached
;
2606 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2607 if (mtu
> last
->route_mtu_cached
)
2608 mtu
= last
->route_mtu_cached
;
2609 dst_metric_set(dst
, RTAX_MTU
, mtu
);
2614 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2615 last
->child_mtu_cached
= mtu
;
2621 static unsigned int xfrm_default_advmss(const struct dst_entry
*dst
)
2623 return dst_metric_advmss(dst
->path
);
2626 static unsigned int xfrm_mtu(const struct dst_entry
*dst
)
2628 unsigned int mtu
= dst_metric_raw(dst
, RTAX_MTU
);
2630 return mtu
? : dst_mtu(dst
->path
);
2633 static struct neighbour
*xfrm_neigh_lookup(const struct dst_entry
*dst
,
2634 struct sk_buff
*skb
,
2637 return dst
->path
->ops
->neigh_lookup(dst
, skb
, daddr
);
2640 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2643 if (unlikely(afinfo
== NULL
))
2645 if (unlikely(afinfo
->family
>= NPROTO
))
2646 return -EAFNOSUPPORT
;
2647 spin_lock(&xfrm_policy_afinfo_lock
);
2648 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
2651 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2652 if (likely(dst_ops
->kmem_cachep
== NULL
))
2653 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2654 if (likely(dst_ops
->check
== NULL
))
2655 dst_ops
->check
= xfrm_dst_check
;
2656 if (likely(dst_ops
->default_advmss
== NULL
))
2657 dst_ops
->default_advmss
= xfrm_default_advmss
;
2658 if (likely(dst_ops
->mtu
== NULL
))
2659 dst_ops
->mtu
= xfrm_mtu
;
2660 if (likely(dst_ops
->negative_advice
== NULL
))
2661 dst_ops
->negative_advice
= xfrm_negative_advice
;
2662 if (likely(dst_ops
->link_failure
== NULL
))
2663 dst_ops
->link_failure
= xfrm_link_failure
;
2664 if (likely(dst_ops
->neigh_lookup
== NULL
))
2665 dst_ops
->neigh_lookup
= xfrm_neigh_lookup
;
2666 if (likely(afinfo
->garbage_collect
== NULL
))
2667 afinfo
->garbage_collect
= xfrm_garbage_collect_deferred
;
2668 rcu_assign_pointer(xfrm_policy_afinfo
[afinfo
->family
], afinfo
);
2670 spin_unlock(&xfrm_policy_afinfo_lock
);
2674 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2676 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2679 if (unlikely(afinfo
== NULL
))
2681 if (unlikely(afinfo
->family
>= NPROTO
))
2682 return -EAFNOSUPPORT
;
2683 spin_lock(&xfrm_policy_afinfo_lock
);
2684 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
2685 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
2688 RCU_INIT_POINTER(xfrm_policy_afinfo
[afinfo
->family
],
2691 spin_unlock(&xfrm_policy_afinfo_lock
);
2693 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2697 dst_ops
->kmem_cachep
= NULL
;
2698 dst_ops
->check
= NULL
;
2699 dst_ops
->negative_advice
= NULL
;
2700 dst_ops
->link_failure
= NULL
;
2701 afinfo
->garbage_collect
= NULL
;
2705 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2707 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
2709 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2713 xfrm_garbage_collect(dev_net(dev
));
2718 static struct notifier_block xfrm_dev_notifier
= {
2719 .notifier_call
= xfrm_dev_event
,
2722 #ifdef CONFIG_XFRM_STATISTICS
2723 static int __net_init
xfrm_statistics_init(struct net
*net
)
2726 net
->mib
.xfrm_statistics
= alloc_percpu(struct linux_xfrm_mib
);
2727 if (!net
->mib
.xfrm_statistics
)
2729 rv
= xfrm_proc_init(net
);
2731 free_percpu(net
->mib
.xfrm_statistics
);
2735 static void xfrm_statistics_fini(struct net
*net
)
2737 xfrm_proc_fini(net
);
2738 free_percpu(net
->mib
.xfrm_statistics
);
2741 static int __net_init
xfrm_statistics_init(struct net
*net
)
2746 static void xfrm_statistics_fini(struct net
*net
)
2751 static int __net_init
xfrm_policy_init(struct net
*net
)
2753 unsigned int hmask
, sz
;
2756 if (net_eq(net
, &init_net
))
2757 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2758 sizeof(struct xfrm_dst
),
2759 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2763 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2765 net
->xfrm
.policy_byidx
= xfrm_hash_alloc(sz
);
2766 if (!net
->xfrm
.policy_byidx
)
2768 net
->xfrm
.policy_idx_hmask
= hmask
;
2770 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2771 struct xfrm_policy_hash
*htab
;
2773 net
->xfrm
.policy_count
[dir
] = 0;
2774 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
2776 htab
= &net
->xfrm
.policy_bydst
[dir
];
2777 htab
->table
= xfrm_hash_alloc(sz
);
2780 htab
->hmask
= hmask
;
2783 INIT_LIST_HEAD(&net
->xfrm
.policy_all
);
2784 INIT_WORK(&net
->xfrm
.policy_hash_work
, xfrm_hash_resize
);
2785 if (net_eq(net
, &init_net
))
2786 register_netdevice_notifier(&xfrm_dev_notifier
);
2790 for (dir
--; dir
>= 0; dir
--) {
2791 struct xfrm_policy_hash
*htab
;
2793 htab
= &net
->xfrm
.policy_bydst
[dir
];
2794 xfrm_hash_free(htab
->table
, sz
);
2796 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
2801 static void xfrm_policy_fini(struct net
*net
)
2806 flush_work(&net
->xfrm
.policy_hash_work
);
2807 #ifdef CONFIG_XFRM_SUB_POLICY
2808 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_SUB
, false);
2810 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, false);
2812 WARN_ON(!list_empty(&net
->xfrm
.policy_all
));
2814 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2815 struct xfrm_policy_hash
*htab
;
2817 WARN_ON(!hlist_empty(&net
->xfrm
.policy_inexact
[dir
]));
2819 htab
= &net
->xfrm
.policy_bydst
[dir
];
2820 sz
= (htab
->hmask
+ 1) * sizeof(struct hlist_head
);
2821 WARN_ON(!hlist_empty(htab
->table
));
2822 xfrm_hash_free(htab
->table
, sz
);
2825 sz
= (net
->xfrm
.policy_idx_hmask
+ 1) * sizeof(struct hlist_head
);
2826 WARN_ON(!hlist_empty(net
->xfrm
.policy_byidx
));
2827 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
2830 static int __net_init
xfrm_net_init(struct net
*net
)
2834 /* Initialize the per-net locks here */
2835 spin_lock_init(&net
->xfrm
.xfrm_state_lock
);
2836 rwlock_init(&net
->xfrm
.xfrm_policy_lock
);
2837 mutex_init(&net
->xfrm
.xfrm_cfg_mutex
);
2839 rv
= xfrm_statistics_init(net
);
2841 goto out_statistics
;
2842 rv
= xfrm_state_init(net
);
2845 rv
= xfrm_policy_init(net
);
2848 rv
= xfrm_sysctl_init(net
);
2851 rv
= flow_cache_init(net
);
2858 xfrm_sysctl_fini(net
);
2860 xfrm_policy_fini(net
);
2862 xfrm_state_fini(net
);
2864 xfrm_statistics_fini(net
);
2869 static void __net_exit
xfrm_net_exit(struct net
*net
)
2871 flow_cache_fini(net
);
2872 xfrm_sysctl_fini(net
);
2873 xfrm_policy_fini(net
);
2874 xfrm_state_fini(net
);
2875 xfrm_statistics_fini(net
);
2878 static struct pernet_operations __net_initdata xfrm_net_ops
= {
2879 .init
= xfrm_net_init
,
2880 .exit
= xfrm_net_exit
,
2883 void __init
xfrm_init(void)
2885 register_pernet_subsys(&xfrm_net_ops
);
2889 #ifdef CONFIG_AUDITSYSCALL
2890 static void xfrm_audit_common_policyinfo(struct xfrm_policy
*xp
,
2891 struct audit_buffer
*audit_buf
)
2893 struct xfrm_sec_ctx
*ctx
= xp
->security
;
2894 struct xfrm_selector
*sel
= &xp
->selector
;
2897 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2898 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
2900 switch (sel
->family
) {
2902 audit_log_format(audit_buf
, " src=%pI4", &sel
->saddr
.a4
);
2903 if (sel
->prefixlen_s
!= 32)
2904 audit_log_format(audit_buf
, " src_prefixlen=%d",
2906 audit_log_format(audit_buf
, " dst=%pI4", &sel
->daddr
.a4
);
2907 if (sel
->prefixlen_d
!= 32)
2908 audit_log_format(audit_buf
, " dst_prefixlen=%d",
2912 audit_log_format(audit_buf
, " src=%pI6", sel
->saddr
.a6
);
2913 if (sel
->prefixlen_s
!= 128)
2914 audit_log_format(audit_buf
, " src_prefixlen=%d",
2916 audit_log_format(audit_buf
, " dst=%pI6", sel
->daddr
.a6
);
2917 if (sel
->prefixlen_d
!= 128)
2918 audit_log_format(audit_buf
, " dst_prefixlen=%d",
2924 void xfrm_audit_policy_add(struct xfrm_policy
*xp
, int result
, bool task_valid
)
2926 struct audit_buffer
*audit_buf
;
2928 audit_buf
= xfrm_audit_start("SPD-add");
2929 if (audit_buf
== NULL
)
2931 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
2932 audit_log_format(audit_buf
, " res=%u", result
);
2933 xfrm_audit_common_policyinfo(xp
, audit_buf
);
2934 audit_log_end(audit_buf
);
2936 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add
);
2938 void xfrm_audit_policy_delete(struct xfrm_policy
*xp
, int result
,
2941 struct audit_buffer
*audit_buf
;
2943 audit_buf
= xfrm_audit_start("SPD-delete");
2944 if (audit_buf
== NULL
)
2946 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
2947 audit_log_format(audit_buf
, " res=%u", result
);
2948 xfrm_audit_common_policyinfo(xp
, audit_buf
);
2949 audit_log_end(audit_buf
);
2951 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete
);
2954 #ifdef CONFIG_XFRM_MIGRATE
2955 static bool xfrm_migrate_selector_match(const struct xfrm_selector
*sel_cmp
,
2956 const struct xfrm_selector
*sel_tgt
)
2958 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
2959 if (sel_tgt
->family
== sel_cmp
->family
&&
2960 xfrm_addr_equal(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
2962 xfrm_addr_equal(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
2964 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
2965 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
2969 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
2976 static struct xfrm_policy
*xfrm_migrate_policy_find(const struct xfrm_selector
*sel
,
2977 u8 dir
, u8 type
, struct net
*net
)
2979 struct xfrm_policy
*pol
, *ret
= NULL
;
2980 struct hlist_head
*chain
;
2983 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
); /*FIXME*/
2984 chain
= policy_hash_direct(net
, &sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
2985 hlist_for_each_entry(pol
, chain
, bydst
) {
2986 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2987 pol
->type
== type
) {
2989 priority
= ret
->priority
;
2993 chain
= &net
->xfrm
.policy_inexact
[dir
];
2994 hlist_for_each_entry(pol
, chain
, bydst
) {
2995 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2996 pol
->type
== type
&&
2997 pol
->priority
< priority
) {
3006 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
3011 static int migrate_tmpl_match(const struct xfrm_migrate
*m
, const struct xfrm_tmpl
*t
)
3015 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
3016 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
3018 case XFRM_MODE_TUNNEL
:
3019 case XFRM_MODE_BEET
:
3020 if (xfrm_addr_equal(&t
->id
.daddr
, &m
->old_daddr
,
3022 xfrm_addr_equal(&t
->saddr
, &m
->old_saddr
,
3027 case XFRM_MODE_TRANSPORT
:
3028 /* in case of transport mode, template does not store
3029 any IP addresses, hence we just compare mode and
3040 /* update endpoint address(es) of template(s) */
3041 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
3042 struct xfrm_migrate
*m
, int num_migrate
)
3044 struct xfrm_migrate
*mp
;
3047 write_lock_bh(&pol
->lock
);
3048 if (unlikely(pol
->walk
.dead
)) {
3049 /* target policy has been deleted */
3050 write_unlock_bh(&pol
->lock
);
3054 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
3055 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
3056 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
3059 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
&&
3060 pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_BEET
)
3062 /* update endpoints */
3063 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
3064 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
3065 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
3066 sizeof(pol
->xfrm_vec
[i
].saddr
));
3067 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
3069 atomic_inc(&pol
->genid
);
3073 write_unlock_bh(&pol
->lock
);
3081 static int xfrm_migrate_check(const struct xfrm_migrate
*m
, int num_migrate
)
3085 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
3088 for (i
= 0; i
< num_migrate
; i
++) {
3089 if (xfrm_addr_equal(&m
[i
].old_daddr
, &m
[i
].new_daddr
,
3091 xfrm_addr_equal(&m
[i
].old_saddr
, &m
[i
].new_saddr
,
3094 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
3095 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
3098 /* check if there is any duplicated entry */
3099 for (j
= i
+ 1; j
< num_migrate
; j
++) {
3100 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
3101 sizeof(m
[i
].old_daddr
)) &&
3102 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
3103 sizeof(m
[i
].old_saddr
)) &&
3104 m
[i
].proto
== m
[j
].proto
&&
3105 m
[i
].mode
== m
[j
].mode
&&
3106 m
[i
].reqid
== m
[j
].reqid
&&
3107 m
[i
].old_family
== m
[j
].old_family
)
3115 int xfrm_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3116 struct xfrm_migrate
*m
, int num_migrate
,
3117 struct xfrm_kmaddress
*k
, struct net
*net
)
3119 int i
, err
, nx_cur
= 0, nx_new
= 0;
3120 struct xfrm_policy
*pol
= NULL
;
3121 struct xfrm_state
*x
, *xc
;
3122 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
3123 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
3124 struct xfrm_migrate
*mp
;
3126 /* Stage 0 - sanity checks */
3127 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
3130 if (dir
>= XFRM_POLICY_MAX
) {
3135 /* Stage 1 - find policy */
3136 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
, net
)) == NULL
) {
3141 /* Stage 2 - find and update state(s) */
3142 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
3143 if ((x
= xfrm_migrate_state_find(mp
, net
))) {
3146 if ((xc
= xfrm_state_migrate(x
, mp
))) {
3156 /* Stage 3 - update policy */
3157 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
3160 /* Stage 4 - delete old state(s) */
3162 xfrm_states_put(x_cur
, nx_cur
);
3163 xfrm_states_delete(x_cur
, nx_cur
);
3166 /* Stage 5 - announce */
3167 km_migrate(sel
, dir
, type
, m
, num_migrate
, k
);
3179 xfrm_states_put(x_cur
, nx_cur
);
3181 xfrm_states_delete(x_new
, nx_new
);
3185 EXPORT_SYMBOL(xfrm_migrate
);