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/slab.h>
17 #include <linux/kmod.h>
18 #include <linux/list.h>
19 #include <linux/spinlock.h>
20 #include <linux/workqueue.h>
21 #include <linux/notifier.h>
22 #include <linux/netdevice.h>
23 #include <linux/netfilter.h>
24 #include <linux/module.h>
25 #include <linux/cache.h>
28 #include <linux/audit.h>
30 #include "xfrm_hash.h"
32 int sysctl_xfrm_larval_drop
;
34 DEFINE_MUTEX(xfrm_cfg_mutex
);
35 EXPORT_SYMBOL(xfrm_cfg_mutex
);
37 static DEFINE_RWLOCK(xfrm_policy_lock
);
39 unsigned int xfrm_policy_count
[XFRM_POLICY_MAX
*2];
40 EXPORT_SYMBOL(xfrm_policy_count
);
42 static DEFINE_RWLOCK(xfrm_policy_afinfo_lock
);
43 static struct xfrm_policy_afinfo
*xfrm_policy_afinfo
[NPROTO
];
45 static struct kmem_cache
*xfrm_dst_cache __read_mostly
;
47 static struct work_struct xfrm_policy_gc_work
;
48 static HLIST_HEAD(xfrm_policy_gc_list
);
49 static DEFINE_SPINLOCK(xfrm_policy_gc_lock
);
51 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
);
52 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
);
53 static struct xfrm_policy_afinfo
*xfrm_policy_lock_afinfo(unsigned int family
);
54 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo
*afinfo
);
57 __xfrm4_selector_match(struct xfrm_selector
*sel
, struct flowi
*fl
)
59 return addr_match(&fl
->fl4_dst
, &sel
->daddr
, sel
->prefixlen_d
) &&
60 addr_match(&fl
->fl4_src
, &sel
->saddr
, sel
->prefixlen_s
) &&
61 !((xfrm_flowi_dport(fl
) ^ sel
->dport
) & sel
->dport_mask
) &&
62 !((xfrm_flowi_sport(fl
) ^ sel
->sport
) & sel
->sport_mask
) &&
63 (fl
->proto
== sel
->proto
|| !sel
->proto
) &&
64 (fl
->oif
== sel
->ifindex
|| !sel
->ifindex
);
68 __xfrm6_selector_match(struct xfrm_selector
*sel
, struct flowi
*fl
)
70 return addr_match(&fl
->fl6_dst
, &sel
->daddr
, sel
->prefixlen_d
) &&
71 addr_match(&fl
->fl6_src
, &sel
->saddr
, sel
->prefixlen_s
) &&
72 !((xfrm_flowi_dport(fl
) ^ sel
->dport
) & sel
->dport_mask
) &&
73 !((xfrm_flowi_sport(fl
) ^ sel
->sport
) & sel
->sport_mask
) &&
74 (fl
->proto
== sel
->proto
|| !sel
->proto
) &&
75 (fl
->oif
== sel
->ifindex
|| !sel
->ifindex
);
78 int xfrm_selector_match(struct xfrm_selector
*sel
, struct flowi
*fl
,
79 unsigned short family
)
83 return __xfrm4_selector_match(sel
, fl
);
85 return __xfrm6_selector_match(sel
, fl
);
90 int xfrm_register_type(struct xfrm_type
*type
, unsigned short family
)
92 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_lock_afinfo(family
);
93 struct xfrm_type
**typemap
;
96 if (unlikely(afinfo
== NULL
))
98 typemap
= afinfo
->type_map
;
100 if (likely(typemap
[type
->proto
] == NULL
))
101 typemap
[type
->proto
] = type
;
104 xfrm_policy_unlock_afinfo(afinfo
);
107 EXPORT_SYMBOL(xfrm_register_type
);
109 int xfrm_unregister_type(struct xfrm_type
*type
, unsigned short family
)
111 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_lock_afinfo(family
);
112 struct xfrm_type
**typemap
;
115 if (unlikely(afinfo
== NULL
))
116 return -EAFNOSUPPORT
;
117 typemap
= afinfo
->type_map
;
119 if (unlikely(typemap
[type
->proto
] != type
))
122 typemap
[type
->proto
] = NULL
;
123 xfrm_policy_unlock_afinfo(afinfo
);
126 EXPORT_SYMBOL(xfrm_unregister_type
);
128 struct xfrm_type
*xfrm_get_type(u8 proto
, unsigned short family
)
130 struct xfrm_policy_afinfo
*afinfo
;
131 struct xfrm_type
**typemap
;
132 struct xfrm_type
*type
;
133 int modload_attempted
= 0;
136 afinfo
= xfrm_policy_get_afinfo(family
);
137 if (unlikely(afinfo
== NULL
))
139 typemap
= afinfo
->type_map
;
141 type
= typemap
[proto
];
142 if (unlikely(type
&& !try_module_get(type
->owner
)))
144 if (!type
&& !modload_attempted
) {
145 xfrm_policy_put_afinfo(afinfo
);
146 request_module("xfrm-type-%d-%d",
147 (int) family
, (int) proto
);
148 modload_attempted
= 1;
152 xfrm_policy_put_afinfo(afinfo
);
156 int xfrm_dst_lookup(struct xfrm_dst
**dst
, struct flowi
*fl
,
157 unsigned short family
)
159 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
162 if (unlikely(afinfo
== NULL
))
163 return -EAFNOSUPPORT
;
165 if (likely(afinfo
->dst_lookup
!= NULL
))
166 err
= afinfo
->dst_lookup(dst
, fl
);
169 xfrm_policy_put_afinfo(afinfo
);
172 EXPORT_SYMBOL(xfrm_dst_lookup
);
174 void xfrm_put_type(struct xfrm_type
*type
)
176 module_put(type
->owner
);
179 int xfrm_register_mode(struct xfrm_mode
*mode
, int family
)
181 struct xfrm_policy_afinfo
*afinfo
;
182 struct xfrm_mode
**modemap
;
185 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
188 afinfo
= xfrm_policy_lock_afinfo(family
);
189 if (unlikely(afinfo
== NULL
))
190 return -EAFNOSUPPORT
;
193 modemap
= afinfo
->mode_map
;
194 if (likely(modemap
[mode
->encap
] == NULL
)) {
195 modemap
[mode
->encap
] = mode
;
199 xfrm_policy_unlock_afinfo(afinfo
);
202 EXPORT_SYMBOL(xfrm_register_mode
);
204 int xfrm_unregister_mode(struct xfrm_mode
*mode
, int family
)
206 struct xfrm_policy_afinfo
*afinfo
;
207 struct xfrm_mode
**modemap
;
210 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
213 afinfo
= xfrm_policy_lock_afinfo(family
);
214 if (unlikely(afinfo
== NULL
))
215 return -EAFNOSUPPORT
;
218 modemap
= afinfo
->mode_map
;
219 if (likely(modemap
[mode
->encap
] == mode
)) {
220 modemap
[mode
->encap
] = NULL
;
224 xfrm_policy_unlock_afinfo(afinfo
);
227 EXPORT_SYMBOL(xfrm_unregister_mode
);
229 struct xfrm_mode
*xfrm_get_mode(unsigned int encap
, int family
)
231 struct xfrm_policy_afinfo
*afinfo
;
232 struct xfrm_mode
*mode
;
233 int modload_attempted
= 0;
235 if (unlikely(encap
>= XFRM_MODE_MAX
))
239 afinfo
= xfrm_policy_get_afinfo(family
);
240 if (unlikely(afinfo
== NULL
))
243 mode
= afinfo
->mode_map
[encap
];
244 if (unlikely(mode
&& !try_module_get(mode
->owner
)))
246 if (!mode
&& !modload_attempted
) {
247 xfrm_policy_put_afinfo(afinfo
);
248 request_module("xfrm-mode-%d-%d", family
, encap
);
249 modload_attempted
= 1;
253 xfrm_policy_put_afinfo(afinfo
);
257 void xfrm_put_mode(struct xfrm_mode
*mode
)
259 module_put(mode
->owner
);
262 static inline unsigned long make_jiffies(long secs
)
264 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
265 return MAX_SCHEDULE_TIMEOUT
-1;
270 static void xfrm_policy_timer(unsigned long data
)
272 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
273 unsigned long now
= get_seconds();
274 long next
= LONG_MAX
;
278 read_lock(&xp
->lock
);
283 dir
= xfrm_policy_id2dir(xp
->index
);
285 if (xp
->lft
.hard_add_expires_seconds
) {
286 long tmo
= xp
->lft
.hard_add_expires_seconds
+
287 xp
->curlft
.add_time
- now
;
293 if (xp
->lft
.hard_use_expires_seconds
) {
294 long tmo
= xp
->lft
.hard_use_expires_seconds
+
295 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
301 if (xp
->lft
.soft_add_expires_seconds
) {
302 long tmo
= xp
->lft
.soft_add_expires_seconds
+
303 xp
->curlft
.add_time
- now
;
306 tmo
= XFRM_KM_TIMEOUT
;
311 if (xp
->lft
.soft_use_expires_seconds
) {
312 long tmo
= xp
->lft
.soft_use_expires_seconds
+
313 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
316 tmo
= XFRM_KM_TIMEOUT
;
323 km_policy_expired(xp
, dir
, 0, 0);
324 if (next
!= LONG_MAX
&&
325 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
329 read_unlock(&xp
->lock
);
334 read_unlock(&xp
->lock
);
335 if (!xfrm_policy_delete(xp
, dir
))
336 km_policy_expired(xp
, dir
, 1, 0);
341 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
345 struct xfrm_policy
*xfrm_policy_alloc(gfp_t gfp
)
347 struct xfrm_policy
*policy
;
349 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
352 INIT_HLIST_NODE(&policy
->bydst
);
353 INIT_HLIST_NODE(&policy
->byidx
);
354 rwlock_init(&policy
->lock
);
355 atomic_set(&policy
->refcnt
, 1);
356 init_timer(&policy
->timer
);
357 policy
->timer
.data
= (unsigned long)policy
;
358 policy
->timer
.function
= xfrm_policy_timer
;
362 EXPORT_SYMBOL(xfrm_policy_alloc
);
364 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
366 void __xfrm_policy_destroy(struct xfrm_policy
*policy
)
368 BUG_ON(!policy
->dead
);
370 BUG_ON(policy
->bundles
);
372 if (del_timer(&policy
->timer
))
375 security_xfrm_policy_free(policy
);
378 EXPORT_SYMBOL(__xfrm_policy_destroy
);
380 static void xfrm_policy_gc_kill(struct xfrm_policy
*policy
)
382 struct dst_entry
*dst
;
384 while ((dst
= policy
->bundles
) != NULL
) {
385 policy
->bundles
= dst
->next
;
389 if (del_timer(&policy
->timer
))
390 atomic_dec(&policy
->refcnt
);
392 if (atomic_read(&policy
->refcnt
) > 1)
395 xfrm_pol_put(policy
);
398 static void xfrm_policy_gc_task(struct work_struct
*work
)
400 struct xfrm_policy
*policy
;
401 struct hlist_node
*entry
, *tmp
;
402 struct hlist_head gc_list
;
404 spin_lock_bh(&xfrm_policy_gc_lock
);
405 gc_list
.first
= xfrm_policy_gc_list
.first
;
406 INIT_HLIST_HEAD(&xfrm_policy_gc_list
);
407 spin_unlock_bh(&xfrm_policy_gc_lock
);
409 hlist_for_each_entry_safe(policy
, entry
, tmp
, &gc_list
, bydst
)
410 xfrm_policy_gc_kill(policy
);
413 /* Rule must be locked. Release descentant resources, announce
414 * entry dead. The rule must be unlinked from lists to the moment.
417 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
421 write_lock_bh(&policy
->lock
);
424 write_unlock_bh(&policy
->lock
);
426 if (unlikely(dead
)) {
431 spin_lock(&xfrm_policy_gc_lock
);
432 hlist_add_head(&policy
->bydst
, &xfrm_policy_gc_list
);
433 spin_unlock(&xfrm_policy_gc_lock
);
435 schedule_work(&xfrm_policy_gc_work
);
438 struct xfrm_policy_hash
{
439 struct hlist_head
*table
;
443 static struct hlist_head xfrm_policy_inexact
[XFRM_POLICY_MAX
*2];
444 static struct xfrm_policy_hash xfrm_policy_bydst
[XFRM_POLICY_MAX
*2] __read_mostly
;
445 static struct hlist_head
*xfrm_policy_byidx __read_mostly
;
446 static unsigned int xfrm_idx_hmask __read_mostly
;
447 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
449 static inline unsigned int idx_hash(u32 index
)
451 return __idx_hash(index
, xfrm_idx_hmask
);
454 static struct hlist_head
*policy_hash_bysel(struct xfrm_selector
*sel
, unsigned short family
, int dir
)
456 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
457 unsigned int hash
= __sel_hash(sel
, family
, hmask
);
459 return (hash
== hmask
+ 1 ?
460 &xfrm_policy_inexact
[dir
] :
461 xfrm_policy_bydst
[dir
].table
+ hash
);
464 static struct hlist_head
*policy_hash_direct(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
, unsigned short family
, int dir
)
466 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
467 unsigned int hash
= __addr_hash(daddr
, saddr
, family
, hmask
);
469 return xfrm_policy_bydst
[dir
].table
+ hash
;
472 static void xfrm_dst_hash_transfer(struct hlist_head
*list
,
473 struct hlist_head
*ndsttable
,
474 unsigned int nhashmask
)
476 struct hlist_node
*entry
, *tmp
;
477 struct xfrm_policy
*pol
;
479 hlist_for_each_entry_safe(pol
, entry
, tmp
, list
, bydst
) {
482 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
483 pol
->family
, nhashmask
);
484 hlist_add_head(&pol
->bydst
, ndsttable
+h
);
488 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
489 struct hlist_head
*nidxtable
,
490 unsigned int nhashmask
)
492 struct hlist_node
*entry
, *tmp
;
493 struct xfrm_policy
*pol
;
495 hlist_for_each_entry_safe(pol
, entry
, tmp
, list
, byidx
) {
498 h
= __idx_hash(pol
->index
, nhashmask
);
499 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
503 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
505 return ((old_hmask
+ 1) << 1) - 1;
508 static void xfrm_bydst_resize(int dir
)
510 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
511 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
512 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
513 struct hlist_head
*odst
= xfrm_policy_bydst
[dir
].table
;
514 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
520 write_lock_bh(&xfrm_policy_lock
);
522 for (i
= hmask
; i
>= 0; i
--)
523 xfrm_dst_hash_transfer(odst
+ i
, ndst
, nhashmask
);
525 xfrm_policy_bydst
[dir
].table
= ndst
;
526 xfrm_policy_bydst
[dir
].hmask
= nhashmask
;
528 write_unlock_bh(&xfrm_policy_lock
);
530 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
533 static void xfrm_byidx_resize(int total
)
535 unsigned int hmask
= xfrm_idx_hmask
;
536 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
537 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
538 struct hlist_head
*oidx
= xfrm_policy_byidx
;
539 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
545 write_lock_bh(&xfrm_policy_lock
);
547 for (i
= hmask
; i
>= 0; i
--)
548 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
550 xfrm_policy_byidx
= nidx
;
551 xfrm_idx_hmask
= nhashmask
;
553 write_unlock_bh(&xfrm_policy_lock
);
555 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
558 static inline int xfrm_bydst_should_resize(int dir
, int *total
)
560 unsigned int cnt
= xfrm_policy_count
[dir
];
561 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
566 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
573 static inline int xfrm_byidx_should_resize(int total
)
575 unsigned int hmask
= xfrm_idx_hmask
;
577 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
584 void xfrm_spd_getinfo(struct xfrmk_spdinfo
*si
)
586 read_lock_bh(&xfrm_policy_lock
);
587 si
->incnt
= xfrm_policy_count
[XFRM_POLICY_IN
];
588 si
->outcnt
= xfrm_policy_count
[XFRM_POLICY_OUT
];
589 si
->fwdcnt
= xfrm_policy_count
[XFRM_POLICY_FWD
];
590 si
->inscnt
= xfrm_policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
591 si
->outscnt
= xfrm_policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
592 si
->fwdscnt
= xfrm_policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
593 si
->spdhcnt
= xfrm_idx_hmask
;
594 si
->spdhmcnt
= xfrm_policy_hashmax
;
595 read_unlock_bh(&xfrm_policy_lock
);
597 EXPORT_SYMBOL(xfrm_spd_getinfo
);
599 static DEFINE_MUTEX(hash_resize_mutex
);
600 static void xfrm_hash_resize(struct work_struct
*__unused
)
604 mutex_lock(&hash_resize_mutex
);
607 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
608 if (xfrm_bydst_should_resize(dir
, &total
))
609 xfrm_bydst_resize(dir
);
611 if (xfrm_byidx_should_resize(total
))
612 xfrm_byidx_resize(total
);
614 mutex_unlock(&hash_resize_mutex
);
617 static DECLARE_WORK(xfrm_hash_work
, xfrm_hash_resize
);
619 /* Generate new index... KAME seems to generate them ordered by cost
620 * of an absolute inpredictability of ordering of rules. This will not pass. */
621 static u32
xfrm_gen_index(u8 type
, int dir
)
623 static u32 idx_generator
;
626 struct hlist_node
*entry
;
627 struct hlist_head
*list
;
628 struct xfrm_policy
*p
;
632 idx
= (idx_generator
| dir
);
636 list
= xfrm_policy_byidx
+ idx_hash(idx
);
638 hlist_for_each_entry(p
, entry
, list
, byidx
) {
639 if (p
->index
== idx
) {
649 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
651 u32
*p1
= (u32
*) s1
;
652 u32
*p2
= (u32
*) s2
;
653 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
656 for (i
= 0; i
< len
; i
++) {
664 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
666 struct xfrm_policy
*pol
;
667 struct xfrm_policy
*delpol
;
668 struct hlist_head
*chain
;
669 struct hlist_node
*entry
, *newpos
;
670 struct dst_entry
*gc_list
;
672 write_lock_bh(&xfrm_policy_lock
);
673 chain
= policy_hash_bysel(&policy
->selector
, policy
->family
, dir
);
676 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
677 if (pol
->type
== policy
->type
&&
678 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
679 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
682 write_unlock_bh(&xfrm_policy_lock
);
686 if (policy
->priority
> pol
->priority
)
688 } else if (policy
->priority
>= pol
->priority
) {
689 newpos
= &pol
->bydst
;
696 hlist_add_after(newpos
, &policy
->bydst
);
698 hlist_add_head(&policy
->bydst
, chain
);
699 xfrm_pol_hold(policy
);
700 xfrm_policy_count
[dir
]++;
701 atomic_inc(&flow_cache_genid
);
703 hlist_del(&delpol
->bydst
);
704 hlist_del(&delpol
->byidx
);
705 xfrm_policy_count
[dir
]--;
707 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(policy
->type
, dir
);
708 hlist_add_head(&policy
->byidx
, xfrm_policy_byidx
+idx_hash(policy
->index
));
709 policy
->curlft
.add_time
= get_seconds();
710 policy
->curlft
.use_time
= 0;
711 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
712 xfrm_pol_hold(policy
);
713 write_unlock_bh(&xfrm_policy_lock
);
716 xfrm_policy_kill(delpol
);
717 else if (xfrm_bydst_should_resize(dir
, NULL
))
718 schedule_work(&xfrm_hash_work
);
720 read_lock_bh(&xfrm_policy_lock
);
722 entry
= &policy
->bydst
;
723 hlist_for_each_entry_continue(policy
, entry
, bydst
) {
724 struct dst_entry
*dst
;
726 write_lock(&policy
->lock
);
727 dst
= policy
->bundles
;
729 struct dst_entry
*tail
= dst
;
732 tail
->next
= gc_list
;
735 policy
->bundles
= NULL
;
737 write_unlock(&policy
->lock
);
739 read_unlock_bh(&xfrm_policy_lock
);
742 struct dst_entry
*dst
= gc_list
;
750 EXPORT_SYMBOL(xfrm_policy_insert
);
752 struct xfrm_policy
*xfrm_policy_bysel_ctx(u8 type
, int dir
,
753 struct xfrm_selector
*sel
,
754 struct xfrm_sec_ctx
*ctx
, int delete,
757 struct xfrm_policy
*pol
, *ret
;
758 struct hlist_head
*chain
;
759 struct hlist_node
*entry
;
762 write_lock_bh(&xfrm_policy_lock
);
763 chain
= policy_hash_bysel(sel
, sel
->family
, dir
);
765 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
766 if (pol
->type
== type
&&
767 !selector_cmp(sel
, &pol
->selector
) &&
768 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
771 *err
= security_xfrm_policy_delete(pol
);
773 write_unlock_bh(&xfrm_policy_lock
);
776 hlist_del(&pol
->bydst
);
777 hlist_del(&pol
->byidx
);
778 xfrm_policy_count
[dir
]--;
784 write_unlock_bh(&xfrm_policy_lock
);
787 atomic_inc(&flow_cache_genid
);
788 xfrm_policy_kill(ret
);
792 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
794 struct xfrm_policy
*xfrm_policy_byid(u8 type
, int dir
, u32 id
, int delete,
797 struct xfrm_policy
*pol
, *ret
;
798 struct hlist_head
*chain
;
799 struct hlist_node
*entry
;
802 if (xfrm_policy_id2dir(id
) != dir
)
806 write_lock_bh(&xfrm_policy_lock
);
807 chain
= xfrm_policy_byidx
+ idx_hash(id
);
809 hlist_for_each_entry(pol
, entry
, chain
, byidx
) {
810 if (pol
->type
== type
&& pol
->index
== id
) {
813 *err
= security_xfrm_policy_delete(pol
);
815 write_unlock_bh(&xfrm_policy_lock
);
818 hlist_del(&pol
->bydst
);
819 hlist_del(&pol
->byidx
);
820 xfrm_policy_count
[dir
]--;
826 write_unlock_bh(&xfrm_policy_lock
);
829 atomic_inc(&flow_cache_genid
);
830 xfrm_policy_kill(ret
);
834 EXPORT_SYMBOL(xfrm_policy_byid
);
836 void xfrm_policy_flush(u8 type
, struct xfrm_audit
*audit_info
)
840 write_lock_bh(&xfrm_policy_lock
);
841 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
842 struct xfrm_policy
*pol
;
843 struct hlist_node
*entry
;
848 hlist_for_each_entry(pol
, entry
,
849 &xfrm_policy_inexact
[dir
], bydst
) {
850 if (pol
->type
!= type
)
852 hlist_del(&pol
->bydst
);
853 hlist_del(&pol
->byidx
);
854 write_unlock_bh(&xfrm_policy_lock
);
856 xfrm_audit_log(audit_info
->loginuid
, audit_info
->secid
,
857 AUDIT_MAC_IPSEC_DELSPD
, 1, pol
, NULL
);
859 xfrm_policy_kill(pol
);
862 write_lock_bh(&xfrm_policy_lock
);
866 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
868 hlist_for_each_entry(pol
, entry
,
869 xfrm_policy_bydst
[dir
].table
+ i
,
871 if (pol
->type
!= type
)
873 hlist_del(&pol
->bydst
);
874 hlist_del(&pol
->byidx
);
875 write_unlock_bh(&xfrm_policy_lock
);
877 xfrm_audit_log(audit_info
->loginuid
,
879 AUDIT_MAC_IPSEC_DELSPD
, 1,
882 xfrm_policy_kill(pol
);
885 write_lock_bh(&xfrm_policy_lock
);
890 xfrm_policy_count
[dir
] -= killed
;
892 atomic_inc(&flow_cache_genid
);
893 write_unlock_bh(&xfrm_policy_lock
);
895 EXPORT_SYMBOL(xfrm_policy_flush
);
897 int xfrm_policy_walk(u8 type
, int (*func
)(struct xfrm_policy
*, int, int, void*),
900 struct xfrm_policy
*pol
, *last
= NULL
;
901 struct hlist_node
*entry
;
902 int dir
, last_dir
= 0, count
, error
;
904 read_lock_bh(&xfrm_policy_lock
);
907 for (dir
= 0; dir
< 2*XFRM_POLICY_MAX
; dir
++) {
908 struct hlist_head
*table
= xfrm_policy_bydst
[dir
].table
;
911 hlist_for_each_entry(pol
, entry
,
912 &xfrm_policy_inexact
[dir
], bydst
) {
913 if (pol
->type
!= type
)
916 error
= func(last
, last_dir
% XFRM_POLICY_MAX
,
925 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
926 hlist_for_each_entry(pol
, entry
, table
+ i
, bydst
) {
927 if (pol
->type
!= type
)
930 error
= func(last
, last_dir
% XFRM_POLICY_MAX
,
945 error
= func(last
, last_dir
% XFRM_POLICY_MAX
, 0, data
);
947 read_unlock_bh(&xfrm_policy_lock
);
950 EXPORT_SYMBOL(xfrm_policy_walk
);
953 * Find policy to apply to this flow.
955 * Returns 0 if policy found, else an -errno.
957 static int xfrm_policy_match(struct xfrm_policy
*pol
, struct flowi
*fl
,
958 u8 type
, u16 family
, int dir
)
960 struct xfrm_selector
*sel
= &pol
->selector
;
961 int match
, ret
= -ESRCH
;
963 if (pol
->family
!= family
||
967 match
= xfrm_selector_match(sel
, fl
, family
);
969 ret
= security_xfrm_policy_lookup(pol
, fl
->secid
, dir
);
974 static struct xfrm_policy
*xfrm_policy_lookup_bytype(u8 type
, struct flowi
*fl
,
978 struct xfrm_policy
*pol
, *ret
;
979 xfrm_address_t
*daddr
, *saddr
;
980 struct hlist_node
*entry
;
981 struct hlist_head
*chain
;
984 daddr
= xfrm_flowi_daddr(fl
, family
);
985 saddr
= xfrm_flowi_saddr(fl
, family
);
986 if (unlikely(!daddr
|| !saddr
))
989 read_lock_bh(&xfrm_policy_lock
);
990 chain
= policy_hash_direct(daddr
, saddr
, family
, dir
);
992 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
993 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1003 priority
= ret
->priority
;
1007 chain
= &xfrm_policy_inexact
[dir
];
1008 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
1009 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1017 } else if (pol
->priority
< priority
) {
1025 read_unlock_bh(&xfrm_policy_lock
);
1030 static int xfrm_policy_lookup(struct flowi
*fl
, u16 family
, u8 dir
,
1031 void **objp
, atomic_t
**obj_refp
)
1033 struct xfrm_policy
*pol
;
1036 #ifdef CONFIG_XFRM_SUB_POLICY
1037 pol
= xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
1045 pol
= xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
1050 #ifdef CONFIG_XFRM_SUB_POLICY
1053 if ((*objp
= (void *) pol
) != NULL
)
1054 *obj_refp
= &pol
->refcnt
;
1058 static inline int policy_to_flow_dir(int dir
)
1060 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1061 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1062 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1066 case XFRM_POLICY_IN
:
1068 case XFRM_POLICY_OUT
:
1069 return FLOW_DIR_OUT
;
1070 case XFRM_POLICY_FWD
:
1071 return FLOW_DIR_FWD
;
1075 static struct xfrm_policy
*xfrm_sk_policy_lookup(struct sock
*sk
, int dir
, struct flowi
*fl
)
1077 struct xfrm_policy
*pol
;
1079 read_lock_bh(&xfrm_policy_lock
);
1080 if ((pol
= sk
->sk_policy
[dir
]) != NULL
) {
1081 int match
= xfrm_selector_match(&pol
->selector
, fl
,
1086 err
= security_xfrm_policy_lookup(pol
, fl
->secid
,
1087 policy_to_flow_dir(dir
));
1090 else if (err
== -ESRCH
)
1097 read_unlock_bh(&xfrm_policy_lock
);
1101 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1103 struct hlist_head
*chain
= policy_hash_bysel(&pol
->selector
,
1106 hlist_add_head(&pol
->bydst
, chain
);
1107 hlist_add_head(&pol
->byidx
, xfrm_policy_byidx
+idx_hash(pol
->index
));
1108 xfrm_policy_count
[dir
]++;
1111 if (xfrm_bydst_should_resize(dir
, NULL
))
1112 schedule_work(&xfrm_hash_work
);
1115 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1118 if (hlist_unhashed(&pol
->bydst
))
1121 hlist_del(&pol
->bydst
);
1122 hlist_del(&pol
->byidx
);
1123 xfrm_policy_count
[dir
]--;
1128 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1130 write_lock_bh(&xfrm_policy_lock
);
1131 pol
= __xfrm_policy_unlink(pol
, dir
);
1132 write_unlock_bh(&xfrm_policy_lock
);
1134 if (dir
< XFRM_POLICY_MAX
)
1135 atomic_inc(&flow_cache_genid
);
1136 xfrm_policy_kill(pol
);
1141 EXPORT_SYMBOL(xfrm_policy_delete
);
1143 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1145 struct xfrm_policy
*old_pol
;
1147 #ifdef CONFIG_XFRM_SUB_POLICY
1148 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1152 write_lock_bh(&xfrm_policy_lock
);
1153 old_pol
= sk
->sk_policy
[dir
];
1154 sk
->sk_policy
[dir
] = pol
;
1156 pol
->curlft
.add_time
= get_seconds();
1157 pol
->index
= xfrm_gen_index(pol
->type
, XFRM_POLICY_MAX
+dir
);
1158 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+dir
);
1161 __xfrm_policy_unlink(old_pol
, XFRM_POLICY_MAX
+dir
);
1162 write_unlock_bh(&xfrm_policy_lock
);
1165 xfrm_policy_kill(old_pol
);
1170 static struct xfrm_policy
*clone_policy(struct xfrm_policy
*old
, int dir
)
1172 struct xfrm_policy
*newp
= xfrm_policy_alloc(GFP_ATOMIC
);
1175 newp
->selector
= old
->selector
;
1176 if (security_xfrm_policy_clone(old
, newp
)) {
1178 return NULL
; /* ENOMEM */
1180 newp
->lft
= old
->lft
;
1181 newp
->curlft
= old
->curlft
;
1182 newp
->action
= old
->action
;
1183 newp
->flags
= old
->flags
;
1184 newp
->xfrm_nr
= old
->xfrm_nr
;
1185 newp
->index
= old
->index
;
1186 newp
->type
= old
->type
;
1187 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1188 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1189 write_lock_bh(&xfrm_policy_lock
);
1190 __xfrm_policy_link(newp
, XFRM_POLICY_MAX
+dir
);
1191 write_unlock_bh(&xfrm_policy_lock
);
1197 int __xfrm_sk_clone_policy(struct sock
*sk
)
1199 struct xfrm_policy
*p0
= sk
->sk_policy
[0],
1200 *p1
= sk
->sk_policy
[1];
1202 sk
->sk_policy
[0] = sk
->sk_policy
[1] = NULL
;
1203 if (p0
&& (sk
->sk_policy
[0] = clone_policy(p0
, 0)) == NULL
)
1205 if (p1
&& (sk
->sk_policy
[1] = clone_policy(p1
, 1)) == NULL
)
1211 xfrm_get_saddr(xfrm_address_t
*local
, xfrm_address_t
*remote
,
1212 unsigned short family
)
1215 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1217 if (unlikely(afinfo
== NULL
))
1219 err
= afinfo
->get_saddr(local
, remote
);
1220 xfrm_policy_put_afinfo(afinfo
);
1224 /* Resolve list of templates for the flow, given policy. */
1227 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, struct flowi
*fl
,
1228 struct xfrm_state
**xfrm
,
1229 unsigned short family
)
1233 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1234 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1237 for (nx
=0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1238 struct xfrm_state
*x
;
1239 xfrm_address_t
*remote
= daddr
;
1240 xfrm_address_t
*local
= saddr
;
1241 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1243 if (tmpl
->mode
== XFRM_MODE_TUNNEL
) {
1244 remote
= &tmpl
->id
.daddr
;
1245 local
= &tmpl
->saddr
;
1246 family
= tmpl
->encap_family
;
1247 if (xfrm_addr_any(local
, family
)) {
1248 error
= xfrm_get_saddr(&tmp
, remote
, family
);
1255 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1257 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1264 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1269 if (!tmpl
->optional
)
1275 for (nx
--; nx
>=0; nx
--)
1276 xfrm_state_put(xfrm
[nx
]);
1281 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, struct flowi
*fl
,
1282 struct xfrm_state
**xfrm
,
1283 unsigned short family
)
1285 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1286 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1292 for (i
= 0; i
< npols
; i
++) {
1293 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1298 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1306 /* found states are sorted for outbound processing */
1308 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1313 for (cnx
--; cnx
>=0; cnx
--)
1314 xfrm_state_put(tpp
[cnx
]);
1319 /* Check that the bundle accepts the flow and its components are
1323 static struct dst_entry
*
1324 xfrm_find_bundle(struct flowi
*fl
, struct xfrm_policy
*policy
, unsigned short family
)
1326 struct dst_entry
*x
;
1327 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1328 if (unlikely(afinfo
== NULL
))
1329 return ERR_PTR(-EINVAL
);
1330 x
= afinfo
->find_bundle(fl
, policy
);
1331 xfrm_policy_put_afinfo(afinfo
);
1335 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1336 * all the metrics... Shortly, bundle a bundle.
1340 xfrm_bundle_create(struct xfrm_policy
*policy
, struct xfrm_state
**xfrm
, int nx
,
1341 struct flowi
*fl
, struct dst_entry
**dst_p
,
1342 unsigned short family
)
1345 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1346 if (unlikely(afinfo
== NULL
))
1348 err
= afinfo
->bundle_create(policy
, xfrm
, nx
, fl
, dst_p
);
1349 xfrm_policy_put_afinfo(afinfo
);
1354 xfrm_dst_alloc_copy(void **target
, void *src
, int size
)
1357 *target
= kmalloc(size
, GFP_ATOMIC
);
1361 memcpy(*target
, src
, size
);
1366 xfrm_dst_update_parent(struct dst_entry
*dst
, struct xfrm_selector
*sel
)
1368 #ifdef CONFIG_XFRM_SUB_POLICY
1369 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1370 return xfrm_dst_alloc_copy((void **)&(xdst
->partner
),
1378 xfrm_dst_update_origin(struct dst_entry
*dst
, struct flowi
*fl
)
1380 #ifdef CONFIG_XFRM_SUB_POLICY
1381 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1382 return xfrm_dst_alloc_copy((void **)&(xdst
->origin
), fl
, sizeof(*fl
));
1388 static int stale_bundle(struct dst_entry
*dst
);
1390 /* Main function: finds/creates a bundle for given flow.
1392 * At the moment we eat a raw IP route. Mostly to speed up lookups
1393 * on interfaces with disabled IPsec.
1395 int __xfrm_lookup(struct dst_entry
**dst_p
, struct flowi
*fl
,
1396 struct sock
*sk
, int flags
)
1398 struct xfrm_policy
*policy
;
1399 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1404 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1405 struct dst_entry
*dst
, *dst_orig
= *dst_p
;
1410 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
1413 genid
= atomic_read(&flow_cache_genid
);
1415 for (pi
= 0; pi
< ARRAY_SIZE(pols
); pi
++)
1421 if (sk
&& sk
->sk_policy
[1]) {
1422 policy
= xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
);
1424 return PTR_ERR(policy
);
1428 /* To accelerate a bit... */
1429 if ((dst_orig
->flags
& DST_NOXFRM
) ||
1430 !xfrm_policy_count
[XFRM_POLICY_OUT
])
1433 policy
= flow_cache_lookup(fl
, dst_orig
->ops
->family
,
1434 dir
, xfrm_policy_lookup
);
1436 return PTR_ERR(policy
);
1442 family
= dst_orig
->ops
->family
;
1443 policy
->curlft
.use_time
= get_seconds();
1446 xfrm_nr
+= pols
[0]->xfrm_nr
;
1448 switch (policy
->action
) {
1449 case XFRM_POLICY_BLOCK
:
1450 /* Prohibit the flow */
1454 case XFRM_POLICY_ALLOW
:
1455 #ifndef CONFIG_XFRM_SUB_POLICY
1456 if (policy
->xfrm_nr
== 0) {
1457 /* Flow passes not transformed. */
1458 xfrm_pol_put(policy
);
1463 /* Try to find matching bundle.
1465 * LATER: help from flow cache. It is optional, this
1466 * is required only for output policy.
1468 dst
= xfrm_find_bundle(fl
, policy
, family
);
1477 #ifdef CONFIG_XFRM_SUB_POLICY
1478 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1479 pols
[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
,
1483 if (IS_ERR(pols
[1])) {
1484 err
= PTR_ERR(pols
[1]);
1487 if (pols
[1]->action
== XFRM_POLICY_BLOCK
) {
1492 xfrm_nr
+= pols
[1]->xfrm_nr
;
1497 * Because neither flowi nor bundle information knows about
1498 * transformation template size. On more than one policy usage
1499 * we can realize whether all of them is bypass or not after
1500 * they are searched. See above not-transformed bypass
1501 * is surrounded by non-sub policy configuration, too.
1504 /* Flow passes not transformed. */
1505 xfrm_pols_put(pols
, npols
);
1510 nx
= xfrm_tmpl_resolve(pols
, npols
, fl
, xfrm
, family
);
1512 if (unlikely(nx
<0)) {
1514 if (err
== -EAGAIN
&& sysctl_xfrm_larval_drop
) {
1515 /* EREMOTE tells the caller to generate
1516 * a one-shot blackhole route.
1518 xfrm_pol_put(policy
);
1521 if (err
== -EAGAIN
&& flags
) {
1522 DECLARE_WAITQUEUE(wait
, current
);
1524 add_wait_queue(&km_waitq
, &wait
);
1525 set_current_state(TASK_INTERRUPTIBLE
);
1527 set_current_state(TASK_RUNNING
);
1528 remove_wait_queue(&km_waitq
, &wait
);
1530 nx
= xfrm_tmpl_resolve(pols
, npols
, fl
, xfrm
, family
);
1532 if (nx
== -EAGAIN
&& signal_pending(current
)) {
1536 if (nx
== -EAGAIN
||
1537 genid
!= atomic_read(&flow_cache_genid
)) {
1538 xfrm_pols_put(pols
, npols
);
1547 /* Flow passes not transformed. */
1548 xfrm_pols_put(pols
, npols
);
1553 err
= xfrm_bundle_create(policy
, xfrm
, nx
, fl
, &dst
, family
);
1555 if (unlikely(err
)) {
1557 for (i
=0; i
<nx
; i
++)
1558 xfrm_state_put(xfrm
[i
]);
1562 for (pi
= 0; pi
< npols
; pi
++) {
1563 read_lock_bh(&pols
[pi
]->lock
);
1564 pol_dead
|= pols
[pi
]->dead
;
1565 read_unlock_bh(&pols
[pi
]->lock
);
1568 write_lock_bh(&policy
->lock
);
1569 if (unlikely(pol_dead
|| stale_bundle(dst
))) {
1570 /* Wow! While we worked on resolving, this
1571 * policy has gone. Retry. It is not paranoia,
1572 * we just cannot enlist new bundle to dead object.
1573 * We can't enlist stable bundles either.
1575 write_unlock_bh(&policy
->lock
);
1579 err
= -EHOSTUNREACH
;
1584 err
= xfrm_dst_update_parent(dst
, &pols
[1]->selector
);
1586 err
= xfrm_dst_update_origin(dst
, fl
);
1587 if (unlikely(err
)) {
1588 write_unlock_bh(&policy
->lock
);
1594 dst
->next
= policy
->bundles
;
1595 policy
->bundles
= dst
;
1597 write_unlock_bh(&policy
->lock
);
1600 dst_release(dst_orig
);
1601 xfrm_pols_put(pols
, npols
);
1605 dst_release(dst_orig
);
1606 xfrm_pols_put(pols
, npols
);
1610 EXPORT_SYMBOL(__xfrm_lookup
);
1612 int xfrm_lookup(struct dst_entry
**dst_p
, struct flowi
*fl
,
1613 struct sock
*sk
, int flags
)
1615 int err
= __xfrm_lookup(dst_p
, fl
, sk
, flags
);
1617 if (err
== -EREMOTE
) {
1618 dst_release(*dst_p
);
1625 EXPORT_SYMBOL(xfrm_lookup
);
1628 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, struct flowi
*fl
)
1630 struct xfrm_state
*x
;
1633 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
1635 x
= skb
->sp
->xvec
[idx
];
1636 if (!x
->type
->reject
)
1639 err
= x
->type
->reject(x
, skb
, fl
);
1644 /* When skb is transformed back to its "native" form, we have to
1645 * check policy restrictions. At the moment we make this in maximally
1646 * stupid way. Shame on me. :-) Of course, connected sockets must
1647 * have policy cached at them.
1651 xfrm_state_ok(struct xfrm_tmpl
*tmpl
, struct xfrm_state
*x
,
1652 unsigned short family
)
1654 if (xfrm_state_kern(x
))
1655 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
1656 return x
->id
.proto
== tmpl
->id
.proto
&&
1657 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
1658 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
1659 x
->props
.mode
== tmpl
->mode
&&
1660 ((tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
1661 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
1662 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
1663 xfrm_state_addr_cmp(tmpl
, x
, family
));
1667 * 0 or more than 0 is returned when validation is succeeded (either bypass
1668 * because of optional transport mode, or next index of the mathced secpath
1669 * state with the template.
1670 * -1 is returned when no matching template is found.
1671 * Otherwise "-2 - errored_index" is returned.
1674 xfrm_policy_ok(struct xfrm_tmpl
*tmpl
, struct sec_path
*sp
, int start
,
1675 unsigned short family
)
1679 if (tmpl
->optional
) {
1680 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
1684 for (; idx
< sp
->len
; idx
++) {
1685 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
1687 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1697 xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, unsigned short family
)
1699 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1702 if (unlikely(afinfo
== NULL
))
1703 return -EAFNOSUPPORT
;
1705 afinfo
->decode_session(skb
, fl
);
1706 err
= security_xfrm_decode_session(skb
, &fl
->secid
);
1707 xfrm_policy_put_afinfo(afinfo
);
1710 EXPORT_SYMBOL(xfrm_decode_session
);
1712 static inline int secpath_has_nontransport(struct sec_path
*sp
, int k
, int *idxp
)
1714 for (; k
< sp
->len
; k
++) {
1715 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1724 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
1725 unsigned short family
)
1727 struct xfrm_policy
*pol
;
1728 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1733 u8 fl_dir
= policy_to_flow_dir(dir
);
1736 if (xfrm_decode_session(skb
, &fl
, family
) < 0)
1738 nf_nat_decode_session(skb
, &fl
, family
);
1740 /* First, check used SA against their selectors. */
1744 for (i
=skb
->sp
->len
-1; i
>=0; i
--) {
1745 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
1746 if (!xfrm_selector_match(&x
->sel
, &fl
, family
))
1752 if (sk
&& sk
->sk_policy
[dir
]) {
1753 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
);
1759 pol
= flow_cache_lookup(&fl
, family
, fl_dir
,
1760 xfrm_policy_lookup
);
1766 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
1767 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
1773 pol
->curlft
.use_time
= get_seconds();
1777 #ifdef CONFIG_XFRM_SUB_POLICY
1778 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1779 pols
[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
,
1783 if (IS_ERR(pols
[1]))
1785 pols
[1]->curlft
.use_time
= get_seconds();
1791 if (pol
->action
== XFRM_POLICY_ALLOW
) {
1792 struct sec_path
*sp
;
1793 static struct sec_path dummy
;
1794 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
1795 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
1796 struct xfrm_tmpl
**tpp
= tp
;
1800 if ((sp
= skb
->sp
) == NULL
)
1803 for (pi
= 0; pi
< npols
; pi
++) {
1804 if (pols
[pi
] != pol
&&
1805 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
)
1807 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
)
1809 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
1810 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
1814 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
);
1818 /* For each tunnel xfrm, find the first matching tmpl.
1819 * For each tmpl before that, find corresponding xfrm.
1820 * Order is _important_. Later we will implement
1821 * some barriers, but at the moment barriers
1822 * are implied between each two transformations.
1824 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
1825 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
1828 /* "-2 - errored_index" returned */
1834 if (secpath_has_nontransport(sp
, k
, &xerr_idx
))
1837 xfrm_pols_put(pols
, npols
);
1842 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
1844 xfrm_pols_put(pols
, npols
);
1847 EXPORT_SYMBOL(__xfrm_policy_check
);
1849 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
1853 if (xfrm_decode_session(skb
, &fl
, family
) < 0)
1856 return xfrm_lookup(&skb
->dst
, &fl
, NULL
, 0) == 0;
1858 EXPORT_SYMBOL(__xfrm_route_forward
);
1860 /* Optimize later using cookies and generation ids. */
1862 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
1864 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1865 * to "-1" to force all XFRM destinations to get validated by
1866 * dst_ops->check on every use. We do this because when a
1867 * normal route referenced by an XFRM dst is obsoleted we do
1868 * not go looking around for all parent referencing XFRM dsts
1869 * so that we can invalidate them. It is just too much work.
1870 * Instead we make the checks here on every use. For example:
1872 * XFRM dst A --> IPv4 dst X
1874 * X is the "xdst->route" of A (X is also the "dst->path" of A
1875 * in this example). If X is marked obsolete, "A" will not
1876 * notice. That's what we are validating here via the
1877 * stale_bundle() check.
1879 * When a policy's bundle is pruned, we dst_free() the XFRM
1880 * dst which causes it's ->obsolete field to be set to a
1881 * positive non-zero integer. If an XFRM dst has been pruned
1882 * like this, we want to force a new route lookup.
1884 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
1890 static int stale_bundle(struct dst_entry
*dst
)
1892 return !xfrm_bundle_ok(NULL
, (struct xfrm_dst
*)dst
, NULL
, AF_UNSPEC
, 0);
1895 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
1897 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
1898 dst
->dev
= &loopback_dev
;
1899 dev_hold(&loopback_dev
);
1903 EXPORT_SYMBOL(xfrm_dst_ifdown
);
1905 static void xfrm_link_failure(struct sk_buff
*skb
)
1907 /* Impossible. Such dst must be popped before reaches point of failure. */
1911 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
1914 if (dst
->obsolete
) {
1922 static void prune_one_bundle(struct xfrm_policy
*pol
, int (*func
)(struct dst_entry
*), struct dst_entry
**gc_list_p
)
1924 struct dst_entry
*dst
, **dstp
;
1926 write_lock(&pol
->lock
);
1927 dstp
= &pol
->bundles
;
1928 while ((dst
=*dstp
) != NULL
) {
1931 dst
->next
= *gc_list_p
;
1937 write_unlock(&pol
->lock
);
1940 static void xfrm_prune_bundles(int (*func
)(struct dst_entry
*))
1942 struct dst_entry
*gc_list
= NULL
;
1945 read_lock_bh(&xfrm_policy_lock
);
1946 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
1947 struct xfrm_policy
*pol
;
1948 struct hlist_node
*entry
;
1949 struct hlist_head
*table
;
1952 hlist_for_each_entry(pol
, entry
,
1953 &xfrm_policy_inexact
[dir
], bydst
)
1954 prune_one_bundle(pol
, func
, &gc_list
);
1956 table
= xfrm_policy_bydst
[dir
].table
;
1957 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
1958 hlist_for_each_entry(pol
, entry
, table
+ i
, bydst
)
1959 prune_one_bundle(pol
, func
, &gc_list
);
1962 read_unlock_bh(&xfrm_policy_lock
);
1965 struct dst_entry
*dst
= gc_list
;
1966 gc_list
= dst
->next
;
1971 static int unused_bundle(struct dst_entry
*dst
)
1973 return !atomic_read(&dst
->__refcnt
);
1976 static void __xfrm_garbage_collect(void)
1978 xfrm_prune_bundles(unused_bundle
);
1981 static int xfrm_flush_bundles(void)
1983 xfrm_prune_bundles(stale_bundle
);
1987 void xfrm_init_pmtu(struct dst_entry
*dst
)
1990 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1991 u32 pmtu
, route_mtu_cached
;
1993 pmtu
= dst_mtu(dst
->child
);
1994 xdst
->child_mtu_cached
= pmtu
;
1996 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
1998 route_mtu_cached
= dst_mtu(xdst
->route
);
1999 xdst
->route_mtu_cached
= route_mtu_cached
;
2001 if (pmtu
> route_mtu_cached
)
2002 pmtu
= route_mtu_cached
;
2004 dst
->metrics
[RTAX_MTU
-1] = pmtu
;
2005 } while ((dst
= dst
->next
));
2008 EXPORT_SYMBOL(xfrm_init_pmtu
);
2010 /* Check that the bundle accepts the flow and its components are
2014 int xfrm_bundle_ok(struct xfrm_policy
*pol
, struct xfrm_dst
*first
,
2015 struct flowi
*fl
, int family
, int strict
)
2017 struct dst_entry
*dst
= &first
->u
.dst
;
2018 struct xfrm_dst
*last
;
2021 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2022 (dst
->dev
&& !netif_running(dst
->dev
)))
2024 #ifdef CONFIG_XFRM_SUB_POLICY
2026 if (first
->origin
&& !flow_cache_uli_match(first
->origin
, fl
))
2028 if (first
->partner
&&
2029 !xfrm_selector_match(first
->partner
, fl
, family
))
2037 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2039 if (fl
&& !xfrm_selector_match(&dst
->xfrm
->sel
, fl
, family
))
2042 !security_xfrm_state_pol_flow_match(dst
->xfrm
, pol
, fl
))
2044 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2046 if (xdst
->genid
!= dst
->xfrm
->genid
)
2049 if (strict
&& fl
&& dst
->xfrm
->props
.mode
!= XFRM_MODE_TUNNEL
&&
2050 !xfrm_state_addr_flow_check(dst
->xfrm
, fl
, family
))
2053 mtu
= dst_mtu(dst
->child
);
2054 if (xdst
->child_mtu_cached
!= mtu
) {
2056 xdst
->child_mtu_cached
= mtu
;
2059 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2061 mtu
= dst_mtu(xdst
->route
);
2062 if (xdst
->route_mtu_cached
!= mtu
) {
2064 xdst
->route_mtu_cached
= mtu
;
2068 } while (dst
->xfrm
);
2073 mtu
= last
->child_mtu_cached
;
2077 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2078 if (mtu
> last
->route_mtu_cached
)
2079 mtu
= last
->route_mtu_cached
;
2080 dst
->metrics
[RTAX_MTU
-1] = mtu
;
2085 last
= last
->u
.next
;
2086 last
->child_mtu_cached
= mtu
;
2092 EXPORT_SYMBOL(xfrm_bundle_ok
);
2094 #ifdef CONFIG_AUDITSYSCALL
2095 /* Audit addition and deletion of SAs and ipsec policy */
2097 void xfrm_audit_log(uid_t auid
, u32 sid
, int type
, int result
,
2098 struct xfrm_policy
*xp
, struct xfrm_state
*x
)
2103 struct xfrm_sec_ctx
*sctx
= NULL
;
2104 struct audit_buffer
*audit_buf
;
2106 extern int audit_enabled
;
2108 if (audit_enabled
== 0)
2111 BUG_ON((type
== AUDIT_MAC_IPSEC_ADDSA
||
2112 type
== AUDIT_MAC_IPSEC_DELSA
) && !x
);
2113 BUG_ON((type
== AUDIT_MAC_IPSEC_ADDSPD
||
2114 type
== AUDIT_MAC_IPSEC_DELSPD
) && !xp
);
2116 audit_buf
= audit_log_start(current
->audit_context
, GFP_ATOMIC
, type
);
2117 if (audit_buf
== NULL
)
2121 case AUDIT_MAC_IPSEC_ADDSA
:
2122 audit_log_format(audit_buf
, "SAD add: auid=%u", auid
);
2124 case AUDIT_MAC_IPSEC_DELSA
:
2125 audit_log_format(audit_buf
, "SAD delete: auid=%u", auid
);
2127 case AUDIT_MAC_IPSEC_ADDSPD
:
2128 audit_log_format(audit_buf
, "SPD add: auid=%u", auid
);
2130 case AUDIT_MAC_IPSEC_DELSPD
:
2131 audit_log_format(audit_buf
, "SPD delete: auid=%u", auid
);
2138 security_secid_to_secctx(sid
, &secctx
, &secctx_len
) == 0)
2139 audit_log_format(audit_buf
, " subj=%s", secctx
);
2141 audit_log_task_context(audit_buf
);
2144 family
= xp
->selector
.family
;
2146 sctx
= xp
->security
;
2148 family
= x
->props
.family
;
2154 audit_log_format(audit_buf
,
2155 " sec_alg=%u sec_doi=%u sec_obj=%s",
2156 sctx
->ctx_alg
, sctx
->ctx_doi
, sctx
->ctx_str
);
2161 struct in_addr saddr
, daddr
;
2163 saddr
.s_addr
= xp
->selector
.saddr
.a4
;
2164 daddr
.s_addr
= xp
->selector
.daddr
.a4
;
2166 saddr
.s_addr
= x
->props
.saddr
.a4
;
2167 daddr
.s_addr
= x
->id
.daddr
.a4
;
2169 audit_log_format(audit_buf
,
2170 " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
2171 NIPQUAD(saddr
), NIPQUAD(daddr
));
2176 struct in6_addr saddr6
, daddr6
;
2178 memcpy(&saddr6
, xp
->selector
.saddr
.a6
,
2179 sizeof(struct in6_addr
));
2180 memcpy(&daddr6
, xp
->selector
.daddr
.a6
,
2181 sizeof(struct in6_addr
));
2183 memcpy(&saddr6
, x
->props
.saddr
.a6
,
2184 sizeof(struct in6_addr
));
2185 memcpy(&daddr6
, x
->id
.daddr
.a6
,
2186 sizeof(struct in6_addr
));
2188 audit_log_format(audit_buf
,
2189 " src=" NIP6_FMT
" dst=" NIP6_FMT
,
2190 NIP6(saddr6
), NIP6(daddr6
));
2196 audit_log_format(audit_buf
, " spi=%lu(0x%lx) protocol=%s",
2197 (unsigned long)ntohl(x
->id
.spi
),
2198 (unsigned long)ntohl(x
->id
.spi
),
2199 x
->id
.proto
== IPPROTO_AH
? "AH" :
2200 (x
->id
.proto
== IPPROTO_ESP
?
2203 audit_log_format(audit_buf
, " res=%u", result
);
2204 audit_log_end(audit_buf
);
2207 EXPORT_SYMBOL(xfrm_audit_log
);
2208 #endif /* CONFIG_AUDITSYSCALL */
2210 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2213 if (unlikely(afinfo
== NULL
))
2215 if (unlikely(afinfo
->family
>= NPROTO
))
2216 return -EAFNOSUPPORT
;
2217 write_lock_bh(&xfrm_policy_afinfo_lock
);
2218 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
2221 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2222 if (likely(dst_ops
->kmem_cachep
== NULL
))
2223 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2224 if (likely(dst_ops
->check
== NULL
))
2225 dst_ops
->check
= xfrm_dst_check
;
2226 if (likely(dst_ops
->negative_advice
== NULL
))
2227 dst_ops
->negative_advice
= xfrm_negative_advice
;
2228 if (likely(dst_ops
->link_failure
== NULL
))
2229 dst_ops
->link_failure
= xfrm_link_failure
;
2230 if (likely(afinfo
->garbage_collect
== NULL
))
2231 afinfo
->garbage_collect
= __xfrm_garbage_collect
;
2232 xfrm_policy_afinfo
[afinfo
->family
] = afinfo
;
2234 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2237 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2239 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2242 if (unlikely(afinfo
== NULL
))
2244 if (unlikely(afinfo
->family
>= NPROTO
))
2245 return -EAFNOSUPPORT
;
2246 write_lock_bh(&xfrm_policy_afinfo_lock
);
2247 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
2248 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
2251 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2252 xfrm_policy_afinfo
[afinfo
->family
] = NULL
;
2253 dst_ops
->kmem_cachep
= NULL
;
2254 dst_ops
->check
= NULL
;
2255 dst_ops
->negative_advice
= NULL
;
2256 dst_ops
->link_failure
= NULL
;
2257 afinfo
->garbage_collect
= NULL
;
2260 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2263 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2265 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
2267 struct xfrm_policy_afinfo
*afinfo
;
2268 if (unlikely(family
>= NPROTO
))
2270 read_lock(&xfrm_policy_afinfo_lock
);
2271 afinfo
= xfrm_policy_afinfo
[family
];
2272 if (unlikely(!afinfo
))
2273 read_unlock(&xfrm_policy_afinfo_lock
);
2277 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2279 read_unlock(&xfrm_policy_afinfo_lock
);
2282 static struct xfrm_policy_afinfo
*xfrm_policy_lock_afinfo(unsigned int family
)
2284 struct xfrm_policy_afinfo
*afinfo
;
2285 if (unlikely(family
>= NPROTO
))
2287 write_lock_bh(&xfrm_policy_afinfo_lock
);
2288 afinfo
= xfrm_policy_afinfo
[family
];
2289 if (unlikely(!afinfo
))
2290 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2294 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2296 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2299 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
2303 xfrm_flush_bundles();
2308 static struct notifier_block xfrm_dev_notifier
= {
2314 static void __init
xfrm_policy_init(void)
2316 unsigned int hmask
, sz
;
2319 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2320 sizeof(struct xfrm_dst
),
2321 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2325 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2327 xfrm_policy_byidx
= xfrm_hash_alloc(sz
);
2328 xfrm_idx_hmask
= hmask
;
2329 if (!xfrm_policy_byidx
)
2330 panic("XFRM: failed to allocate byidx hash\n");
2332 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2333 struct xfrm_policy_hash
*htab
;
2335 INIT_HLIST_HEAD(&xfrm_policy_inexact
[dir
]);
2337 htab
= &xfrm_policy_bydst
[dir
];
2338 htab
->table
= xfrm_hash_alloc(sz
);
2339 htab
->hmask
= hmask
;
2341 panic("XFRM: failed to allocate bydst hash\n");
2344 INIT_WORK(&xfrm_policy_gc_work
, xfrm_policy_gc_task
);
2345 register_netdevice_notifier(&xfrm_dev_notifier
);
2348 void __init
xfrm_init(void)
2355 #ifdef CONFIG_XFRM_MIGRATE
2356 static int xfrm_migrate_selector_match(struct xfrm_selector
*sel_cmp
,
2357 struct xfrm_selector
*sel_tgt
)
2359 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
2360 if (sel_tgt
->family
== sel_cmp
->family
&&
2361 xfrm_addr_cmp(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
2362 sel_cmp
->family
) == 0 &&
2363 xfrm_addr_cmp(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
2364 sel_cmp
->family
) == 0 &&
2365 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
2366 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
2370 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
2377 static struct xfrm_policy
* xfrm_migrate_policy_find(struct xfrm_selector
*sel
,
2380 struct xfrm_policy
*pol
, *ret
= NULL
;
2381 struct hlist_node
*entry
;
2382 struct hlist_head
*chain
;
2385 read_lock_bh(&xfrm_policy_lock
);
2386 chain
= policy_hash_direct(&sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
2387 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
2388 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2389 pol
->type
== type
) {
2391 priority
= ret
->priority
;
2395 chain
= &xfrm_policy_inexact
[dir
];
2396 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
2397 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2398 pol
->type
== type
&&
2399 pol
->priority
< priority
) {
2408 read_unlock_bh(&xfrm_policy_lock
);
2413 static int migrate_tmpl_match(struct xfrm_migrate
*m
, struct xfrm_tmpl
*t
)
2417 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
2418 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
2420 case XFRM_MODE_TUNNEL
:
2421 case XFRM_MODE_BEET
:
2422 if (xfrm_addr_cmp(&t
->id
.daddr
, &m
->old_daddr
,
2423 m
->old_family
) == 0 &&
2424 xfrm_addr_cmp(&t
->saddr
, &m
->old_saddr
,
2425 m
->old_family
) == 0) {
2429 case XFRM_MODE_TRANSPORT
:
2430 /* in case of transport mode, template does not store
2431 any IP addresses, hence we just compare mode and
2442 /* update endpoint address(es) of template(s) */
2443 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
2444 struct xfrm_migrate
*m
, int num_migrate
)
2446 struct xfrm_migrate
*mp
;
2447 struct dst_entry
*dst
;
2450 write_lock_bh(&pol
->lock
);
2451 if (unlikely(pol
->dead
)) {
2452 /* target policy has been deleted */
2453 write_unlock_bh(&pol
->lock
);
2457 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
2458 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
2459 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
2462 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
)
2464 /* update endpoints */
2465 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
2466 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
2467 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
2468 sizeof(pol
->xfrm_vec
[i
].saddr
));
2469 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
2471 while ((dst
= pol
->bundles
) != NULL
) {
2472 pol
->bundles
= dst
->next
;
2478 write_unlock_bh(&pol
->lock
);
2486 static int xfrm_migrate_check(struct xfrm_migrate
*m
, int num_migrate
)
2490 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
2493 for (i
= 0; i
< num_migrate
; i
++) {
2494 if ((xfrm_addr_cmp(&m
[i
].old_daddr
, &m
[i
].new_daddr
,
2495 m
[i
].old_family
) == 0) &&
2496 (xfrm_addr_cmp(&m
[i
].old_saddr
, &m
[i
].new_saddr
,
2497 m
[i
].old_family
) == 0))
2499 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
2500 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
2503 /* check if there is any duplicated entry */
2504 for (j
= i
+ 1; j
< num_migrate
; j
++) {
2505 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
2506 sizeof(m
[i
].old_daddr
)) &&
2507 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
2508 sizeof(m
[i
].old_saddr
)) &&
2509 m
[i
].proto
== m
[j
].proto
&&
2510 m
[i
].mode
== m
[j
].mode
&&
2511 m
[i
].reqid
== m
[j
].reqid
&&
2512 m
[i
].old_family
== m
[j
].old_family
)
2520 int xfrm_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2521 struct xfrm_migrate
*m
, int num_migrate
)
2523 int i
, err
, nx_cur
= 0, nx_new
= 0;
2524 struct xfrm_policy
*pol
= NULL
;
2525 struct xfrm_state
*x
, *xc
;
2526 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
2527 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
2528 struct xfrm_migrate
*mp
;
2530 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
2533 /* Stage 1 - find policy */
2534 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
)) == NULL
) {
2539 /* Stage 2 - find and update state(s) */
2540 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2541 if ((x
= xfrm_migrate_state_find(mp
))) {
2544 if ((xc
= xfrm_state_migrate(x
, mp
))) {
2554 /* Stage 3 - update policy */
2555 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
2558 /* Stage 4 - delete old state(s) */
2560 xfrm_states_put(x_cur
, nx_cur
);
2561 xfrm_states_delete(x_cur
, nx_cur
);
2564 /* Stage 5 - announce */
2565 km_migrate(sel
, dir
, type
, m
, num_migrate
);
2577 xfrm_states_put(x_cur
, nx_cur
);
2579 xfrm_states_delete(x_new
, nx_new
);
2583 EXPORT_SYMBOL(xfrm_migrate
);