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>
29 #include "xfrm_hash.h"
31 int sysctl_xfrm_larval_drop __read_mostly
;
33 DEFINE_MUTEX(xfrm_cfg_mutex
);
34 EXPORT_SYMBOL(xfrm_cfg_mutex
);
36 static DEFINE_RWLOCK(xfrm_policy_lock
);
38 unsigned int xfrm_policy_count
[XFRM_POLICY_MAX
*2];
39 EXPORT_SYMBOL(xfrm_policy_count
);
41 static DEFINE_RWLOCK(xfrm_policy_afinfo_lock
);
42 static struct xfrm_policy_afinfo
*xfrm_policy_afinfo
[NPROTO
];
44 static struct kmem_cache
*xfrm_dst_cache __read_mostly
;
46 static struct work_struct xfrm_policy_gc_work
;
47 static HLIST_HEAD(xfrm_policy_gc_list
);
48 static DEFINE_SPINLOCK(xfrm_policy_gc_lock
);
50 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
);
51 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
);
52 static struct xfrm_policy_afinfo
*xfrm_policy_lock_afinfo(unsigned int family
);
53 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo
*afinfo
);
56 __xfrm4_selector_match(struct xfrm_selector
*sel
, struct flowi
*fl
)
58 return addr_match(&fl
->fl4_dst
, &sel
->daddr
, sel
->prefixlen_d
) &&
59 addr_match(&fl
->fl4_src
, &sel
->saddr
, sel
->prefixlen_s
) &&
60 !((xfrm_flowi_dport(fl
) ^ sel
->dport
) & sel
->dport_mask
) &&
61 !((xfrm_flowi_sport(fl
) ^ sel
->sport
) & sel
->sport_mask
) &&
62 (fl
->proto
== sel
->proto
|| !sel
->proto
) &&
63 (fl
->oif
== sel
->ifindex
|| !sel
->ifindex
);
67 __xfrm6_selector_match(struct xfrm_selector
*sel
, struct flowi
*fl
)
69 return addr_match(&fl
->fl6_dst
, &sel
->daddr
, sel
->prefixlen_d
) &&
70 addr_match(&fl
->fl6_src
, &sel
->saddr
, sel
->prefixlen_s
) &&
71 !((xfrm_flowi_dport(fl
) ^ sel
->dport
) & sel
->dport_mask
) &&
72 !((xfrm_flowi_sport(fl
) ^ sel
->sport
) & sel
->sport_mask
) &&
73 (fl
->proto
== sel
->proto
|| !sel
->proto
) &&
74 (fl
->oif
== sel
->ifindex
|| !sel
->ifindex
);
77 int xfrm_selector_match(struct xfrm_selector
*sel
, struct flowi
*fl
,
78 unsigned short family
)
82 return __xfrm4_selector_match(sel
, fl
);
84 return __xfrm6_selector_match(sel
, fl
);
89 int xfrm_register_type(struct xfrm_type
*type
, unsigned short family
)
91 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_lock_afinfo(family
);
92 struct xfrm_type
**typemap
;
95 if (unlikely(afinfo
== NULL
))
97 typemap
= afinfo
->type_map
;
99 if (likely(typemap
[type
->proto
] == NULL
))
100 typemap
[type
->proto
] = type
;
103 xfrm_policy_unlock_afinfo(afinfo
);
106 EXPORT_SYMBOL(xfrm_register_type
);
108 int xfrm_unregister_type(struct xfrm_type
*type
, unsigned short family
)
110 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_lock_afinfo(family
);
111 struct xfrm_type
**typemap
;
114 if (unlikely(afinfo
== NULL
))
115 return -EAFNOSUPPORT
;
116 typemap
= afinfo
->type_map
;
118 if (unlikely(typemap
[type
->proto
] != type
))
121 typemap
[type
->proto
] = NULL
;
122 xfrm_policy_unlock_afinfo(afinfo
);
125 EXPORT_SYMBOL(xfrm_unregister_type
);
127 struct xfrm_type
*xfrm_get_type(u8 proto
, unsigned short family
)
129 struct xfrm_policy_afinfo
*afinfo
;
130 struct xfrm_type
**typemap
;
131 struct xfrm_type
*type
;
132 int modload_attempted
= 0;
135 afinfo
= xfrm_policy_get_afinfo(family
);
136 if (unlikely(afinfo
== NULL
))
138 typemap
= afinfo
->type_map
;
140 type
= typemap
[proto
];
141 if (unlikely(type
&& !try_module_get(type
->owner
)))
143 if (!type
&& !modload_attempted
) {
144 xfrm_policy_put_afinfo(afinfo
);
145 request_module("xfrm-type-%d-%d",
146 (int) family
, (int) proto
);
147 modload_attempted
= 1;
151 xfrm_policy_put_afinfo(afinfo
);
155 int xfrm_dst_lookup(struct xfrm_dst
**dst
, struct flowi
*fl
,
156 unsigned short family
)
158 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
161 if (unlikely(afinfo
== NULL
))
162 return -EAFNOSUPPORT
;
164 if (likely(afinfo
->dst_lookup
!= NULL
))
165 err
= afinfo
->dst_lookup(dst
, fl
);
168 xfrm_policy_put_afinfo(afinfo
);
171 EXPORT_SYMBOL(xfrm_dst_lookup
);
173 void xfrm_put_type(struct xfrm_type
*type
)
175 module_put(type
->owner
);
178 int xfrm_register_mode(struct xfrm_mode
*mode
, int family
)
180 struct xfrm_policy_afinfo
*afinfo
;
181 struct xfrm_mode
**modemap
;
184 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
187 afinfo
= xfrm_policy_lock_afinfo(family
);
188 if (unlikely(afinfo
== NULL
))
189 return -EAFNOSUPPORT
;
192 modemap
= afinfo
->mode_map
;
193 if (likely(modemap
[mode
->encap
] == NULL
)) {
194 modemap
[mode
->encap
] = mode
;
198 xfrm_policy_unlock_afinfo(afinfo
);
201 EXPORT_SYMBOL(xfrm_register_mode
);
203 int xfrm_unregister_mode(struct xfrm_mode
*mode
, int family
)
205 struct xfrm_policy_afinfo
*afinfo
;
206 struct xfrm_mode
**modemap
;
209 if (unlikely(mode
->encap
>= XFRM_MODE_MAX
))
212 afinfo
= xfrm_policy_lock_afinfo(family
);
213 if (unlikely(afinfo
== NULL
))
214 return -EAFNOSUPPORT
;
217 modemap
= afinfo
->mode_map
;
218 if (likely(modemap
[mode
->encap
] == mode
)) {
219 modemap
[mode
->encap
] = NULL
;
223 xfrm_policy_unlock_afinfo(afinfo
);
226 EXPORT_SYMBOL(xfrm_unregister_mode
);
228 struct xfrm_mode
*xfrm_get_mode(unsigned int encap
, int family
)
230 struct xfrm_policy_afinfo
*afinfo
;
231 struct xfrm_mode
*mode
;
232 int modload_attempted
= 0;
234 if (unlikely(encap
>= XFRM_MODE_MAX
))
238 afinfo
= xfrm_policy_get_afinfo(family
);
239 if (unlikely(afinfo
== NULL
))
242 mode
= afinfo
->mode_map
[encap
];
243 if (unlikely(mode
&& !try_module_get(mode
->owner
)))
245 if (!mode
&& !modload_attempted
) {
246 xfrm_policy_put_afinfo(afinfo
);
247 request_module("xfrm-mode-%d-%d", family
, encap
);
248 modload_attempted
= 1;
252 xfrm_policy_put_afinfo(afinfo
);
256 void xfrm_put_mode(struct xfrm_mode
*mode
)
258 module_put(mode
->owner
);
261 static inline unsigned long make_jiffies(long secs
)
263 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
264 return MAX_SCHEDULE_TIMEOUT
-1;
269 static void xfrm_policy_timer(unsigned long data
)
271 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
272 unsigned long now
= get_seconds();
273 long next
= LONG_MAX
;
277 read_lock(&xp
->lock
);
282 dir
= xfrm_policy_id2dir(xp
->index
);
284 if (xp
->lft
.hard_add_expires_seconds
) {
285 long tmo
= xp
->lft
.hard_add_expires_seconds
+
286 xp
->curlft
.add_time
- now
;
292 if (xp
->lft
.hard_use_expires_seconds
) {
293 long tmo
= xp
->lft
.hard_use_expires_seconds
+
294 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
300 if (xp
->lft
.soft_add_expires_seconds
) {
301 long tmo
= xp
->lft
.soft_add_expires_seconds
+
302 xp
->curlft
.add_time
- now
;
305 tmo
= XFRM_KM_TIMEOUT
;
310 if (xp
->lft
.soft_use_expires_seconds
) {
311 long tmo
= xp
->lft
.soft_use_expires_seconds
+
312 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
315 tmo
= XFRM_KM_TIMEOUT
;
322 km_policy_expired(xp
, dir
, 0, 0);
323 if (next
!= LONG_MAX
&&
324 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
328 read_unlock(&xp
->lock
);
333 read_unlock(&xp
->lock
);
334 if (!xfrm_policy_delete(xp
, dir
))
335 km_policy_expired(xp
, dir
, 1, 0);
340 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
344 struct xfrm_policy
*xfrm_policy_alloc(gfp_t gfp
)
346 struct xfrm_policy
*policy
;
348 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
351 INIT_HLIST_NODE(&policy
->bydst
);
352 INIT_HLIST_NODE(&policy
->byidx
);
353 rwlock_init(&policy
->lock
);
354 atomic_set(&policy
->refcnt
, 1);
355 init_timer(&policy
->timer
);
356 policy
->timer
.data
= (unsigned long)policy
;
357 policy
->timer
.function
= xfrm_policy_timer
;
361 EXPORT_SYMBOL(xfrm_policy_alloc
);
363 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
365 void __xfrm_policy_destroy(struct xfrm_policy
*policy
)
367 BUG_ON(!policy
->dead
);
369 BUG_ON(policy
->bundles
);
371 if (del_timer(&policy
->timer
))
374 security_xfrm_policy_free(policy
);
377 EXPORT_SYMBOL(__xfrm_policy_destroy
);
379 static void xfrm_policy_gc_kill(struct xfrm_policy
*policy
)
381 struct dst_entry
*dst
;
383 while ((dst
= policy
->bundles
) != NULL
) {
384 policy
->bundles
= dst
->next
;
388 if (del_timer(&policy
->timer
))
389 atomic_dec(&policy
->refcnt
);
391 if (atomic_read(&policy
->refcnt
) > 1)
394 xfrm_pol_put(policy
);
397 static void xfrm_policy_gc_task(struct work_struct
*work
)
399 struct xfrm_policy
*policy
;
400 struct hlist_node
*entry
, *tmp
;
401 struct hlist_head gc_list
;
403 spin_lock_bh(&xfrm_policy_gc_lock
);
404 gc_list
.first
= xfrm_policy_gc_list
.first
;
405 INIT_HLIST_HEAD(&xfrm_policy_gc_list
);
406 spin_unlock_bh(&xfrm_policy_gc_lock
);
408 hlist_for_each_entry_safe(policy
, entry
, tmp
, &gc_list
, bydst
)
409 xfrm_policy_gc_kill(policy
);
412 /* Rule must be locked. Release descentant resources, announce
413 * entry dead. The rule must be unlinked from lists to the moment.
416 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
420 write_lock_bh(&policy
->lock
);
423 write_unlock_bh(&policy
->lock
);
425 if (unlikely(dead
)) {
430 spin_lock(&xfrm_policy_gc_lock
);
431 hlist_add_head(&policy
->bydst
, &xfrm_policy_gc_list
);
432 spin_unlock(&xfrm_policy_gc_lock
);
434 schedule_work(&xfrm_policy_gc_work
);
437 struct xfrm_policy_hash
{
438 struct hlist_head
*table
;
442 static struct hlist_head xfrm_policy_inexact
[XFRM_POLICY_MAX
*2];
443 static struct xfrm_policy_hash xfrm_policy_bydst
[XFRM_POLICY_MAX
*2] __read_mostly
;
444 static struct hlist_head
*xfrm_policy_byidx __read_mostly
;
445 static unsigned int xfrm_idx_hmask __read_mostly
;
446 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
448 static inline unsigned int idx_hash(u32 index
)
450 return __idx_hash(index
, xfrm_idx_hmask
);
453 static struct hlist_head
*policy_hash_bysel(struct xfrm_selector
*sel
, unsigned short family
, int dir
)
455 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
456 unsigned int hash
= __sel_hash(sel
, family
, hmask
);
458 return (hash
== hmask
+ 1 ?
459 &xfrm_policy_inexact
[dir
] :
460 xfrm_policy_bydst
[dir
].table
+ hash
);
463 static struct hlist_head
*policy_hash_direct(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
, unsigned short family
, int dir
)
465 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
466 unsigned int hash
= __addr_hash(daddr
, saddr
, family
, hmask
);
468 return xfrm_policy_bydst
[dir
].table
+ hash
;
471 static void xfrm_dst_hash_transfer(struct hlist_head
*list
,
472 struct hlist_head
*ndsttable
,
473 unsigned int nhashmask
)
475 struct hlist_node
*entry
, *tmp
;
476 struct xfrm_policy
*pol
;
478 hlist_for_each_entry_safe(pol
, entry
, tmp
, list
, bydst
) {
481 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
482 pol
->family
, nhashmask
);
483 hlist_add_head(&pol
->bydst
, ndsttable
+h
);
487 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
488 struct hlist_head
*nidxtable
,
489 unsigned int nhashmask
)
491 struct hlist_node
*entry
, *tmp
;
492 struct xfrm_policy
*pol
;
494 hlist_for_each_entry_safe(pol
, entry
, tmp
, list
, byidx
) {
497 h
= __idx_hash(pol
->index
, nhashmask
);
498 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
502 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
504 return ((old_hmask
+ 1) << 1) - 1;
507 static void xfrm_bydst_resize(int dir
)
509 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
510 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
511 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
512 struct hlist_head
*odst
= xfrm_policy_bydst
[dir
].table
;
513 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
519 write_lock_bh(&xfrm_policy_lock
);
521 for (i
= hmask
; i
>= 0; i
--)
522 xfrm_dst_hash_transfer(odst
+ i
, ndst
, nhashmask
);
524 xfrm_policy_bydst
[dir
].table
= ndst
;
525 xfrm_policy_bydst
[dir
].hmask
= nhashmask
;
527 write_unlock_bh(&xfrm_policy_lock
);
529 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
532 static void xfrm_byidx_resize(int total
)
534 unsigned int hmask
= xfrm_idx_hmask
;
535 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
536 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
537 struct hlist_head
*oidx
= xfrm_policy_byidx
;
538 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
544 write_lock_bh(&xfrm_policy_lock
);
546 for (i
= hmask
; i
>= 0; i
--)
547 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
549 xfrm_policy_byidx
= nidx
;
550 xfrm_idx_hmask
= nhashmask
;
552 write_unlock_bh(&xfrm_policy_lock
);
554 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
557 static inline int xfrm_bydst_should_resize(int dir
, int *total
)
559 unsigned int cnt
= xfrm_policy_count
[dir
];
560 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
565 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
572 static inline int xfrm_byidx_should_resize(int total
)
574 unsigned int hmask
= xfrm_idx_hmask
;
576 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
583 void xfrm_spd_getinfo(struct xfrmk_spdinfo
*si
)
585 read_lock_bh(&xfrm_policy_lock
);
586 si
->incnt
= xfrm_policy_count
[XFRM_POLICY_IN
];
587 si
->outcnt
= xfrm_policy_count
[XFRM_POLICY_OUT
];
588 si
->fwdcnt
= xfrm_policy_count
[XFRM_POLICY_FWD
];
589 si
->inscnt
= xfrm_policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
590 si
->outscnt
= xfrm_policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
591 si
->fwdscnt
= xfrm_policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
592 si
->spdhcnt
= xfrm_idx_hmask
;
593 si
->spdhmcnt
= xfrm_policy_hashmax
;
594 read_unlock_bh(&xfrm_policy_lock
);
596 EXPORT_SYMBOL(xfrm_spd_getinfo
);
598 static DEFINE_MUTEX(hash_resize_mutex
);
599 static void xfrm_hash_resize(struct work_struct
*__unused
)
603 mutex_lock(&hash_resize_mutex
);
606 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
607 if (xfrm_bydst_should_resize(dir
, &total
))
608 xfrm_bydst_resize(dir
);
610 if (xfrm_byidx_should_resize(total
))
611 xfrm_byidx_resize(total
);
613 mutex_unlock(&hash_resize_mutex
);
616 static DECLARE_WORK(xfrm_hash_work
, xfrm_hash_resize
);
618 /* Generate new index... KAME seems to generate them ordered by cost
619 * of an absolute inpredictability of ordering of rules. This will not pass. */
620 static u32
xfrm_gen_index(u8 type
, int dir
)
622 static u32 idx_generator
;
625 struct hlist_node
*entry
;
626 struct hlist_head
*list
;
627 struct xfrm_policy
*p
;
631 idx
= (idx_generator
| dir
);
635 list
= xfrm_policy_byidx
+ idx_hash(idx
);
637 hlist_for_each_entry(p
, entry
, list
, byidx
) {
638 if (p
->index
== idx
) {
648 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
650 u32
*p1
= (u32
*) s1
;
651 u32
*p2
= (u32
*) s2
;
652 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
655 for (i
= 0; i
< len
; i
++) {
663 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
665 struct xfrm_policy
*pol
;
666 struct xfrm_policy
*delpol
;
667 struct hlist_head
*chain
;
668 struct hlist_node
*entry
, *newpos
;
669 struct dst_entry
*gc_list
;
671 write_lock_bh(&xfrm_policy_lock
);
672 chain
= policy_hash_bysel(&policy
->selector
, policy
->family
, dir
);
675 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
676 if (pol
->type
== policy
->type
&&
677 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
678 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
681 write_unlock_bh(&xfrm_policy_lock
);
685 if (policy
->priority
> pol
->priority
)
687 } else if (policy
->priority
>= pol
->priority
) {
688 newpos
= &pol
->bydst
;
695 hlist_add_after(newpos
, &policy
->bydst
);
697 hlist_add_head(&policy
->bydst
, chain
);
698 xfrm_pol_hold(policy
);
699 xfrm_policy_count
[dir
]++;
700 atomic_inc(&flow_cache_genid
);
702 hlist_del(&delpol
->bydst
);
703 hlist_del(&delpol
->byidx
);
704 xfrm_policy_count
[dir
]--;
706 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(policy
->type
, dir
);
707 hlist_add_head(&policy
->byidx
, xfrm_policy_byidx
+idx_hash(policy
->index
));
708 policy
->curlft
.add_time
= get_seconds();
709 policy
->curlft
.use_time
= 0;
710 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
711 xfrm_pol_hold(policy
);
712 write_unlock_bh(&xfrm_policy_lock
);
715 xfrm_policy_kill(delpol
);
716 else if (xfrm_bydst_should_resize(dir
, NULL
))
717 schedule_work(&xfrm_hash_work
);
719 read_lock_bh(&xfrm_policy_lock
);
721 entry
= &policy
->bydst
;
722 hlist_for_each_entry_continue(policy
, entry
, bydst
) {
723 struct dst_entry
*dst
;
725 write_lock(&policy
->lock
);
726 dst
= policy
->bundles
;
728 struct dst_entry
*tail
= dst
;
731 tail
->next
= gc_list
;
734 policy
->bundles
= NULL
;
736 write_unlock(&policy
->lock
);
738 read_unlock_bh(&xfrm_policy_lock
);
741 struct dst_entry
*dst
= gc_list
;
749 EXPORT_SYMBOL(xfrm_policy_insert
);
751 struct xfrm_policy
*xfrm_policy_bysel_ctx(u8 type
, int dir
,
752 struct xfrm_selector
*sel
,
753 struct xfrm_sec_ctx
*ctx
, int delete,
756 struct xfrm_policy
*pol
, *ret
;
757 struct hlist_head
*chain
;
758 struct hlist_node
*entry
;
761 write_lock_bh(&xfrm_policy_lock
);
762 chain
= policy_hash_bysel(sel
, sel
->family
, dir
);
764 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
765 if (pol
->type
== type
&&
766 !selector_cmp(sel
, &pol
->selector
) &&
767 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
770 *err
= security_xfrm_policy_delete(pol
);
772 write_unlock_bh(&xfrm_policy_lock
);
775 hlist_del(&pol
->bydst
);
776 hlist_del(&pol
->byidx
);
777 xfrm_policy_count
[dir
]--;
783 write_unlock_bh(&xfrm_policy_lock
);
786 atomic_inc(&flow_cache_genid
);
787 xfrm_policy_kill(ret
);
791 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
793 struct xfrm_policy
*xfrm_policy_byid(u8 type
, int dir
, u32 id
, int delete,
796 struct xfrm_policy
*pol
, *ret
;
797 struct hlist_head
*chain
;
798 struct hlist_node
*entry
;
801 if (xfrm_policy_id2dir(id
) != dir
)
805 write_lock_bh(&xfrm_policy_lock
);
806 chain
= xfrm_policy_byidx
+ idx_hash(id
);
808 hlist_for_each_entry(pol
, entry
, chain
, byidx
) {
809 if (pol
->type
== type
&& pol
->index
== id
) {
812 *err
= security_xfrm_policy_delete(pol
);
814 write_unlock_bh(&xfrm_policy_lock
);
817 hlist_del(&pol
->bydst
);
818 hlist_del(&pol
->byidx
);
819 xfrm_policy_count
[dir
]--;
825 write_unlock_bh(&xfrm_policy_lock
);
828 atomic_inc(&flow_cache_genid
);
829 xfrm_policy_kill(ret
);
833 EXPORT_SYMBOL(xfrm_policy_byid
);
835 #ifdef CONFIG_SECURITY_NETWORK_XFRM
837 xfrm_policy_flush_secctx_check(u8 type
, struct xfrm_audit
*audit_info
)
841 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
842 struct xfrm_policy
*pol
;
843 struct hlist_node
*entry
;
846 hlist_for_each_entry(pol
, entry
,
847 &xfrm_policy_inexact
[dir
], bydst
) {
848 if (pol
->type
!= type
)
850 err
= security_xfrm_policy_delete(pol
);
852 xfrm_audit_policy_delete(pol
, 0,
853 audit_info
->loginuid
,
858 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
859 hlist_for_each_entry(pol
, entry
,
860 xfrm_policy_bydst
[dir
].table
+ i
,
862 if (pol
->type
!= type
)
864 err
= security_xfrm_policy_delete(pol
);
866 xfrm_audit_policy_delete(pol
, 0,
867 audit_info
->loginuid
,
878 xfrm_policy_flush_secctx_check(u8 type
, struct xfrm_audit
*audit_info
)
884 int xfrm_policy_flush(u8 type
, struct xfrm_audit
*audit_info
)
888 write_lock_bh(&xfrm_policy_lock
);
890 err
= xfrm_policy_flush_secctx_check(type
, audit_info
);
894 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
895 struct xfrm_policy
*pol
;
896 struct hlist_node
*entry
;
901 hlist_for_each_entry(pol
, entry
,
902 &xfrm_policy_inexact
[dir
], bydst
) {
903 if (pol
->type
!= type
)
905 hlist_del(&pol
->bydst
);
906 hlist_del(&pol
->byidx
);
907 write_unlock_bh(&xfrm_policy_lock
);
909 xfrm_audit_policy_delete(pol
, 1, audit_info
->loginuid
,
912 xfrm_policy_kill(pol
);
915 write_lock_bh(&xfrm_policy_lock
);
919 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
921 hlist_for_each_entry(pol
, entry
,
922 xfrm_policy_bydst
[dir
].table
+ i
,
924 if (pol
->type
!= type
)
926 hlist_del(&pol
->bydst
);
927 hlist_del(&pol
->byidx
);
928 write_unlock_bh(&xfrm_policy_lock
);
930 xfrm_audit_policy_delete(pol
, 1,
931 audit_info
->loginuid
,
933 xfrm_policy_kill(pol
);
936 write_lock_bh(&xfrm_policy_lock
);
941 xfrm_policy_count
[dir
] -= killed
;
943 atomic_inc(&flow_cache_genid
);
945 write_unlock_bh(&xfrm_policy_lock
);
948 EXPORT_SYMBOL(xfrm_policy_flush
);
950 int xfrm_policy_walk(u8 type
, int (*func
)(struct xfrm_policy
*, int, int, void*),
953 struct xfrm_policy
*pol
, *last
= NULL
;
954 struct hlist_node
*entry
;
955 int dir
, last_dir
= 0, count
, error
;
957 read_lock_bh(&xfrm_policy_lock
);
960 for (dir
= 0; dir
< 2*XFRM_POLICY_MAX
; dir
++) {
961 struct hlist_head
*table
= xfrm_policy_bydst
[dir
].table
;
964 hlist_for_each_entry(pol
, entry
,
965 &xfrm_policy_inexact
[dir
], bydst
) {
966 if (pol
->type
!= type
)
969 error
= func(last
, last_dir
% XFRM_POLICY_MAX
,
978 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
979 hlist_for_each_entry(pol
, entry
, table
+ i
, bydst
) {
980 if (pol
->type
!= type
)
983 error
= func(last
, last_dir
% XFRM_POLICY_MAX
,
998 error
= func(last
, last_dir
% XFRM_POLICY_MAX
, 0, data
);
1000 read_unlock_bh(&xfrm_policy_lock
);
1003 EXPORT_SYMBOL(xfrm_policy_walk
);
1006 * Find policy to apply to this flow.
1008 * Returns 0 if policy found, else an -errno.
1010 static int xfrm_policy_match(struct xfrm_policy
*pol
, struct flowi
*fl
,
1011 u8 type
, u16 family
, int dir
)
1013 struct xfrm_selector
*sel
= &pol
->selector
;
1014 int match
, ret
= -ESRCH
;
1016 if (pol
->family
!= family
||
1020 match
= xfrm_selector_match(sel
, fl
, family
);
1022 ret
= security_xfrm_policy_lookup(pol
, fl
->secid
, dir
);
1027 static struct xfrm_policy
*xfrm_policy_lookup_bytype(u8 type
, struct flowi
*fl
,
1031 struct xfrm_policy
*pol
, *ret
;
1032 xfrm_address_t
*daddr
, *saddr
;
1033 struct hlist_node
*entry
;
1034 struct hlist_head
*chain
;
1037 daddr
= xfrm_flowi_daddr(fl
, family
);
1038 saddr
= xfrm_flowi_saddr(fl
, family
);
1039 if (unlikely(!daddr
|| !saddr
))
1042 read_lock_bh(&xfrm_policy_lock
);
1043 chain
= policy_hash_direct(daddr
, saddr
, family
, dir
);
1045 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
1046 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1056 priority
= ret
->priority
;
1060 chain
= &xfrm_policy_inexact
[dir
];
1061 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
1062 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1070 } else if (pol
->priority
< priority
) {
1078 read_unlock_bh(&xfrm_policy_lock
);
1083 static int xfrm_policy_lookup(struct flowi
*fl
, u16 family
, u8 dir
,
1084 void **objp
, atomic_t
**obj_refp
)
1086 struct xfrm_policy
*pol
;
1089 #ifdef CONFIG_XFRM_SUB_POLICY
1090 pol
= xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
1098 pol
= xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
1103 #ifdef CONFIG_XFRM_SUB_POLICY
1106 if ((*objp
= (void *) pol
) != NULL
)
1107 *obj_refp
= &pol
->refcnt
;
1111 static inline int policy_to_flow_dir(int dir
)
1113 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1114 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1115 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1119 case XFRM_POLICY_IN
:
1121 case XFRM_POLICY_OUT
:
1122 return FLOW_DIR_OUT
;
1123 case XFRM_POLICY_FWD
:
1124 return FLOW_DIR_FWD
;
1128 static struct xfrm_policy
*xfrm_sk_policy_lookup(struct sock
*sk
, int dir
, struct flowi
*fl
)
1130 struct xfrm_policy
*pol
;
1132 read_lock_bh(&xfrm_policy_lock
);
1133 if ((pol
= sk
->sk_policy
[dir
]) != NULL
) {
1134 int match
= xfrm_selector_match(&pol
->selector
, fl
,
1139 err
= security_xfrm_policy_lookup(pol
, fl
->secid
,
1140 policy_to_flow_dir(dir
));
1143 else if (err
== -ESRCH
)
1150 read_unlock_bh(&xfrm_policy_lock
);
1154 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1156 struct hlist_head
*chain
= policy_hash_bysel(&pol
->selector
,
1159 hlist_add_head(&pol
->bydst
, chain
);
1160 hlist_add_head(&pol
->byidx
, xfrm_policy_byidx
+idx_hash(pol
->index
));
1161 xfrm_policy_count
[dir
]++;
1164 if (xfrm_bydst_should_resize(dir
, NULL
))
1165 schedule_work(&xfrm_hash_work
);
1168 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1171 if (hlist_unhashed(&pol
->bydst
))
1174 hlist_del(&pol
->bydst
);
1175 hlist_del(&pol
->byidx
);
1176 xfrm_policy_count
[dir
]--;
1181 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1183 write_lock_bh(&xfrm_policy_lock
);
1184 pol
= __xfrm_policy_unlink(pol
, dir
);
1185 write_unlock_bh(&xfrm_policy_lock
);
1187 if (dir
< XFRM_POLICY_MAX
)
1188 atomic_inc(&flow_cache_genid
);
1189 xfrm_policy_kill(pol
);
1194 EXPORT_SYMBOL(xfrm_policy_delete
);
1196 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1198 struct xfrm_policy
*old_pol
;
1200 #ifdef CONFIG_XFRM_SUB_POLICY
1201 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1205 write_lock_bh(&xfrm_policy_lock
);
1206 old_pol
= sk
->sk_policy
[dir
];
1207 sk
->sk_policy
[dir
] = pol
;
1209 pol
->curlft
.add_time
= get_seconds();
1210 pol
->index
= xfrm_gen_index(pol
->type
, XFRM_POLICY_MAX
+dir
);
1211 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+dir
);
1214 __xfrm_policy_unlink(old_pol
, XFRM_POLICY_MAX
+dir
);
1215 write_unlock_bh(&xfrm_policy_lock
);
1218 xfrm_policy_kill(old_pol
);
1223 static struct xfrm_policy
*clone_policy(struct xfrm_policy
*old
, int dir
)
1225 struct xfrm_policy
*newp
= xfrm_policy_alloc(GFP_ATOMIC
);
1228 newp
->selector
= old
->selector
;
1229 if (security_xfrm_policy_clone(old
, newp
)) {
1231 return NULL
; /* ENOMEM */
1233 newp
->lft
= old
->lft
;
1234 newp
->curlft
= old
->curlft
;
1235 newp
->action
= old
->action
;
1236 newp
->flags
= old
->flags
;
1237 newp
->xfrm_nr
= old
->xfrm_nr
;
1238 newp
->index
= old
->index
;
1239 newp
->type
= old
->type
;
1240 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1241 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1242 write_lock_bh(&xfrm_policy_lock
);
1243 __xfrm_policy_link(newp
, XFRM_POLICY_MAX
+dir
);
1244 write_unlock_bh(&xfrm_policy_lock
);
1250 int __xfrm_sk_clone_policy(struct sock
*sk
)
1252 struct xfrm_policy
*p0
= sk
->sk_policy
[0],
1253 *p1
= sk
->sk_policy
[1];
1255 sk
->sk_policy
[0] = sk
->sk_policy
[1] = NULL
;
1256 if (p0
&& (sk
->sk_policy
[0] = clone_policy(p0
, 0)) == NULL
)
1258 if (p1
&& (sk
->sk_policy
[1] = clone_policy(p1
, 1)) == NULL
)
1264 xfrm_get_saddr(xfrm_address_t
*local
, xfrm_address_t
*remote
,
1265 unsigned short family
)
1268 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1270 if (unlikely(afinfo
== NULL
))
1272 err
= afinfo
->get_saddr(local
, remote
);
1273 xfrm_policy_put_afinfo(afinfo
);
1277 /* Resolve list of templates for the flow, given policy. */
1280 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, struct flowi
*fl
,
1281 struct xfrm_state
**xfrm
,
1282 unsigned short family
)
1286 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1287 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1290 for (nx
=0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1291 struct xfrm_state
*x
;
1292 xfrm_address_t
*remote
= daddr
;
1293 xfrm_address_t
*local
= saddr
;
1294 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1296 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1297 tmpl
->mode
== XFRM_MODE_BEET
) {
1298 remote
= &tmpl
->id
.daddr
;
1299 local
= &tmpl
->saddr
;
1300 family
= tmpl
->encap_family
;
1301 if (xfrm_addr_any(local
, family
)) {
1302 error
= xfrm_get_saddr(&tmp
, remote
, family
);
1309 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1311 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1318 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1323 if (!tmpl
->optional
)
1329 for (nx
--; nx
>=0; nx
--)
1330 xfrm_state_put(xfrm
[nx
]);
1335 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, struct flowi
*fl
,
1336 struct xfrm_state
**xfrm
,
1337 unsigned short family
)
1339 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1340 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1346 for (i
= 0; i
< npols
; i
++) {
1347 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1352 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1360 /* found states are sorted for outbound processing */
1362 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1367 for (cnx
--; cnx
>=0; cnx
--)
1368 xfrm_state_put(tpp
[cnx
]);
1373 /* Check that the bundle accepts the flow and its components are
1377 static struct dst_entry
*
1378 xfrm_find_bundle(struct flowi
*fl
, struct xfrm_policy
*policy
, unsigned short family
)
1380 struct dst_entry
*x
;
1381 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1382 if (unlikely(afinfo
== NULL
))
1383 return ERR_PTR(-EINVAL
);
1384 x
= afinfo
->find_bundle(fl
, policy
);
1385 xfrm_policy_put_afinfo(afinfo
);
1389 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1390 * all the metrics... Shortly, bundle a bundle.
1394 xfrm_bundle_create(struct xfrm_policy
*policy
, struct xfrm_state
**xfrm
, int nx
,
1395 struct flowi
*fl
, struct dst_entry
**dst_p
,
1396 unsigned short family
)
1399 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1400 if (unlikely(afinfo
== NULL
))
1402 err
= afinfo
->bundle_create(policy
, xfrm
, nx
, fl
, dst_p
);
1403 xfrm_policy_put_afinfo(afinfo
);
1408 xfrm_dst_alloc_copy(void **target
, void *src
, int size
)
1411 *target
= kmalloc(size
, GFP_ATOMIC
);
1415 memcpy(*target
, src
, size
);
1420 xfrm_dst_update_parent(struct dst_entry
*dst
, struct xfrm_selector
*sel
)
1422 #ifdef CONFIG_XFRM_SUB_POLICY
1423 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1424 return xfrm_dst_alloc_copy((void **)&(xdst
->partner
),
1432 xfrm_dst_update_origin(struct dst_entry
*dst
, struct flowi
*fl
)
1434 #ifdef CONFIG_XFRM_SUB_POLICY
1435 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1436 return xfrm_dst_alloc_copy((void **)&(xdst
->origin
), fl
, sizeof(*fl
));
1442 static int stale_bundle(struct dst_entry
*dst
);
1444 /* Main function: finds/creates a bundle for given flow.
1446 * At the moment we eat a raw IP route. Mostly to speed up lookups
1447 * on interfaces with disabled IPsec.
1449 int __xfrm_lookup(struct dst_entry
**dst_p
, struct flowi
*fl
,
1450 struct sock
*sk
, int flags
)
1452 struct xfrm_policy
*policy
;
1453 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1458 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1459 struct dst_entry
*dst
, *dst_orig
= *dst_p
;
1464 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
1467 genid
= atomic_read(&flow_cache_genid
);
1469 for (pi
= 0; pi
< ARRAY_SIZE(pols
); pi
++)
1475 if (sk
&& sk
->sk_policy
[XFRM_POLICY_OUT
]) {
1476 policy
= xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
);
1478 return PTR_ERR(policy
);
1482 /* To accelerate a bit... */
1483 if ((dst_orig
->flags
& DST_NOXFRM
) ||
1484 !xfrm_policy_count
[XFRM_POLICY_OUT
])
1487 policy
= flow_cache_lookup(fl
, dst_orig
->ops
->family
,
1488 dir
, xfrm_policy_lookup
);
1490 return PTR_ERR(policy
);
1496 family
= dst_orig
->ops
->family
;
1497 policy
->curlft
.use_time
= get_seconds();
1500 xfrm_nr
+= pols
[0]->xfrm_nr
;
1502 switch (policy
->action
) {
1503 case XFRM_POLICY_BLOCK
:
1504 /* Prohibit the flow */
1508 case XFRM_POLICY_ALLOW
:
1509 #ifndef CONFIG_XFRM_SUB_POLICY
1510 if (policy
->xfrm_nr
== 0) {
1511 /* Flow passes not transformed. */
1512 xfrm_pol_put(policy
);
1517 /* Try to find matching bundle.
1519 * LATER: help from flow cache. It is optional, this
1520 * is required only for output policy.
1522 dst
= xfrm_find_bundle(fl
, policy
, family
);
1531 #ifdef CONFIG_XFRM_SUB_POLICY
1532 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1533 pols
[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
,
1537 if (IS_ERR(pols
[1])) {
1538 err
= PTR_ERR(pols
[1]);
1541 if (pols
[1]->action
== XFRM_POLICY_BLOCK
) {
1546 xfrm_nr
+= pols
[1]->xfrm_nr
;
1551 * Because neither flowi nor bundle information knows about
1552 * transformation template size. On more than one policy usage
1553 * we can realize whether all of them is bypass or not after
1554 * they are searched. See above not-transformed bypass
1555 * is surrounded by non-sub policy configuration, too.
1558 /* Flow passes not transformed. */
1559 xfrm_pols_put(pols
, npols
);
1564 nx
= xfrm_tmpl_resolve(pols
, npols
, fl
, xfrm
, family
);
1566 if (unlikely(nx
<0)) {
1568 if (err
== -EAGAIN
&& sysctl_xfrm_larval_drop
) {
1569 /* EREMOTE tells the caller to generate
1570 * a one-shot blackhole route.
1572 xfrm_pol_put(policy
);
1575 if (err
== -EAGAIN
&& flags
) {
1576 DECLARE_WAITQUEUE(wait
, current
);
1578 add_wait_queue(&km_waitq
, &wait
);
1579 set_current_state(TASK_INTERRUPTIBLE
);
1581 set_current_state(TASK_RUNNING
);
1582 remove_wait_queue(&km_waitq
, &wait
);
1584 nx
= xfrm_tmpl_resolve(pols
, npols
, fl
, xfrm
, family
);
1586 if (nx
== -EAGAIN
&& signal_pending(current
)) {
1590 if (nx
== -EAGAIN
||
1591 genid
!= atomic_read(&flow_cache_genid
)) {
1592 xfrm_pols_put(pols
, npols
);
1601 /* Flow passes not transformed. */
1602 xfrm_pols_put(pols
, npols
);
1607 err
= xfrm_bundle_create(policy
, xfrm
, nx
, fl
, &dst
, family
);
1609 if (unlikely(err
)) {
1611 for (i
=0; i
<nx
; i
++)
1612 xfrm_state_put(xfrm
[i
]);
1616 for (pi
= 0; pi
< npols
; pi
++) {
1617 read_lock_bh(&pols
[pi
]->lock
);
1618 pol_dead
|= pols
[pi
]->dead
;
1619 read_unlock_bh(&pols
[pi
]->lock
);
1622 write_lock_bh(&policy
->lock
);
1623 if (unlikely(pol_dead
|| stale_bundle(dst
))) {
1624 /* Wow! While we worked on resolving, this
1625 * policy has gone. Retry. It is not paranoia,
1626 * we just cannot enlist new bundle to dead object.
1627 * We can't enlist stable bundles either.
1629 write_unlock_bh(&policy
->lock
);
1633 err
= -EHOSTUNREACH
;
1638 err
= xfrm_dst_update_parent(dst
, &pols
[1]->selector
);
1640 err
= xfrm_dst_update_origin(dst
, fl
);
1641 if (unlikely(err
)) {
1642 write_unlock_bh(&policy
->lock
);
1648 dst
->next
= policy
->bundles
;
1649 policy
->bundles
= dst
;
1651 write_unlock_bh(&policy
->lock
);
1654 dst_release(dst_orig
);
1655 xfrm_pols_put(pols
, npols
);
1659 dst_release(dst_orig
);
1660 xfrm_pols_put(pols
, npols
);
1664 EXPORT_SYMBOL(__xfrm_lookup
);
1666 int xfrm_lookup(struct dst_entry
**dst_p
, struct flowi
*fl
,
1667 struct sock
*sk
, int flags
)
1669 int err
= __xfrm_lookup(dst_p
, fl
, sk
, flags
);
1671 if (err
== -EREMOTE
) {
1672 dst_release(*dst_p
);
1679 EXPORT_SYMBOL(xfrm_lookup
);
1682 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, struct flowi
*fl
)
1684 struct xfrm_state
*x
;
1687 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
1689 x
= skb
->sp
->xvec
[idx
];
1690 if (!x
->type
->reject
)
1693 err
= x
->type
->reject(x
, skb
, fl
);
1698 /* When skb is transformed back to its "native" form, we have to
1699 * check policy restrictions. At the moment we make this in maximally
1700 * stupid way. Shame on me. :-) Of course, connected sockets must
1701 * have policy cached at them.
1705 xfrm_state_ok(struct xfrm_tmpl
*tmpl
, struct xfrm_state
*x
,
1706 unsigned short family
)
1708 if (xfrm_state_kern(x
))
1709 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
1710 return x
->id
.proto
== tmpl
->id
.proto
&&
1711 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
1712 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
1713 x
->props
.mode
== tmpl
->mode
&&
1714 ((tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
1715 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
1716 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
1717 xfrm_state_addr_cmp(tmpl
, x
, family
));
1721 * 0 or more than 0 is returned when validation is succeeded (either bypass
1722 * because of optional transport mode, or next index of the mathced secpath
1723 * state with the template.
1724 * -1 is returned when no matching template is found.
1725 * Otherwise "-2 - errored_index" is returned.
1728 xfrm_policy_ok(struct xfrm_tmpl
*tmpl
, struct sec_path
*sp
, int start
,
1729 unsigned short family
)
1733 if (tmpl
->optional
) {
1734 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
1738 for (; idx
< sp
->len
; idx
++) {
1739 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
1741 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1751 xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, unsigned short family
)
1753 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1756 if (unlikely(afinfo
== NULL
))
1757 return -EAFNOSUPPORT
;
1759 afinfo
->decode_session(skb
, fl
);
1760 err
= security_xfrm_decode_session(skb
, &fl
->secid
);
1761 xfrm_policy_put_afinfo(afinfo
);
1764 EXPORT_SYMBOL(xfrm_decode_session
);
1766 static inline int secpath_has_nontransport(struct sec_path
*sp
, int k
, int *idxp
)
1768 for (; k
< sp
->len
; k
++) {
1769 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1778 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
1779 unsigned short family
)
1781 struct xfrm_policy
*pol
;
1782 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1787 u8 fl_dir
= policy_to_flow_dir(dir
);
1790 if (xfrm_decode_session(skb
, &fl
, family
) < 0)
1792 nf_nat_decode_session(skb
, &fl
, family
);
1794 /* First, check used SA against their selectors. */
1798 for (i
=skb
->sp
->len
-1; i
>=0; i
--) {
1799 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
1800 if (!xfrm_selector_match(&x
->sel
, &fl
, family
))
1806 if (sk
&& sk
->sk_policy
[dir
]) {
1807 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
);
1813 pol
= flow_cache_lookup(&fl
, family
, fl_dir
,
1814 xfrm_policy_lookup
);
1820 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
1821 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
1827 pol
->curlft
.use_time
= get_seconds();
1831 #ifdef CONFIG_XFRM_SUB_POLICY
1832 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1833 pols
[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
,
1837 if (IS_ERR(pols
[1]))
1839 pols
[1]->curlft
.use_time
= get_seconds();
1845 if (pol
->action
== XFRM_POLICY_ALLOW
) {
1846 struct sec_path
*sp
;
1847 static struct sec_path dummy
;
1848 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
1849 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
1850 struct xfrm_tmpl
**tpp
= tp
;
1854 if ((sp
= skb
->sp
) == NULL
)
1857 for (pi
= 0; pi
< npols
; pi
++) {
1858 if (pols
[pi
] != pol
&&
1859 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
)
1861 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
)
1863 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
1864 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
1868 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
);
1872 /* For each tunnel xfrm, find the first matching tmpl.
1873 * For each tmpl before that, find corresponding xfrm.
1874 * Order is _important_. Later we will implement
1875 * some barriers, but at the moment barriers
1876 * are implied between each two transformations.
1878 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
1879 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
1882 /* "-2 - errored_index" returned */
1888 if (secpath_has_nontransport(sp
, k
, &xerr_idx
))
1891 xfrm_pols_put(pols
, npols
);
1896 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
1898 xfrm_pols_put(pols
, npols
);
1901 EXPORT_SYMBOL(__xfrm_policy_check
);
1903 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
1907 if (xfrm_decode_session(skb
, &fl
, family
) < 0)
1910 return xfrm_lookup(&skb
->dst
, &fl
, NULL
, 0) == 0;
1912 EXPORT_SYMBOL(__xfrm_route_forward
);
1914 /* Optimize later using cookies and generation ids. */
1916 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
1918 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1919 * to "-1" to force all XFRM destinations to get validated by
1920 * dst_ops->check on every use. We do this because when a
1921 * normal route referenced by an XFRM dst is obsoleted we do
1922 * not go looking around for all parent referencing XFRM dsts
1923 * so that we can invalidate them. It is just too much work.
1924 * Instead we make the checks here on every use. For example:
1926 * XFRM dst A --> IPv4 dst X
1928 * X is the "xdst->route" of A (X is also the "dst->path" of A
1929 * in this example). If X is marked obsolete, "A" will not
1930 * notice. That's what we are validating here via the
1931 * stale_bundle() check.
1933 * When a policy's bundle is pruned, we dst_free() the XFRM
1934 * dst which causes it's ->obsolete field to be set to a
1935 * positive non-zero integer. If an XFRM dst has been pruned
1936 * like this, we want to force a new route lookup.
1938 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
1944 static int stale_bundle(struct dst_entry
*dst
)
1946 return !xfrm_bundle_ok(NULL
, (struct xfrm_dst
*)dst
, NULL
, AF_UNSPEC
, 0);
1949 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
1951 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
1952 dst
->dev
= &loopback_dev
;
1953 dev_hold(&loopback_dev
);
1957 EXPORT_SYMBOL(xfrm_dst_ifdown
);
1959 static void xfrm_link_failure(struct sk_buff
*skb
)
1961 /* Impossible. Such dst must be popped before reaches point of failure. */
1965 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
1968 if (dst
->obsolete
) {
1976 static void prune_one_bundle(struct xfrm_policy
*pol
, int (*func
)(struct dst_entry
*), struct dst_entry
**gc_list_p
)
1978 struct dst_entry
*dst
, **dstp
;
1980 write_lock(&pol
->lock
);
1981 dstp
= &pol
->bundles
;
1982 while ((dst
=*dstp
) != NULL
) {
1985 dst
->next
= *gc_list_p
;
1991 write_unlock(&pol
->lock
);
1994 static void xfrm_prune_bundles(int (*func
)(struct dst_entry
*))
1996 struct dst_entry
*gc_list
= NULL
;
1999 read_lock_bh(&xfrm_policy_lock
);
2000 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2001 struct xfrm_policy
*pol
;
2002 struct hlist_node
*entry
;
2003 struct hlist_head
*table
;
2006 hlist_for_each_entry(pol
, entry
,
2007 &xfrm_policy_inexact
[dir
], bydst
)
2008 prune_one_bundle(pol
, func
, &gc_list
);
2010 table
= xfrm_policy_bydst
[dir
].table
;
2011 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
2012 hlist_for_each_entry(pol
, entry
, table
+ i
, bydst
)
2013 prune_one_bundle(pol
, func
, &gc_list
);
2016 read_unlock_bh(&xfrm_policy_lock
);
2019 struct dst_entry
*dst
= gc_list
;
2020 gc_list
= dst
->next
;
2025 static int unused_bundle(struct dst_entry
*dst
)
2027 return !atomic_read(&dst
->__refcnt
);
2030 static void __xfrm_garbage_collect(void)
2032 xfrm_prune_bundles(unused_bundle
);
2035 static int xfrm_flush_bundles(void)
2037 xfrm_prune_bundles(stale_bundle
);
2041 void xfrm_init_pmtu(struct dst_entry
*dst
)
2044 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2045 u32 pmtu
, route_mtu_cached
;
2047 pmtu
= dst_mtu(dst
->child
);
2048 xdst
->child_mtu_cached
= pmtu
;
2050 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2052 route_mtu_cached
= dst_mtu(xdst
->route
);
2053 xdst
->route_mtu_cached
= route_mtu_cached
;
2055 if (pmtu
> route_mtu_cached
)
2056 pmtu
= route_mtu_cached
;
2058 dst
->metrics
[RTAX_MTU
-1] = pmtu
;
2059 } while ((dst
= dst
->next
));
2062 EXPORT_SYMBOL(xfrm_init_pmtu
);
2064 /* Check that the bundle accepts the flow and its components are
2068 int xfrm_bundle_ok(struct xfrm_policy
*pol
, struct xfrm_dst
*first
,
2069 struct flowi
*fl
, int family
, int strict
)
2071 struct dst_entry
*dst
= &first
->u
.dst
;
2072 struct xfrm_dst
*last
;
2075 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2076 (dst
->dev
&& !netif_running(dst
->dev
)))
2078 #ifdef CONFIG_XFRM_SUB_POLICY
2080 if (first
->origin
&& !flow_cache_uli_match(first
->origin
, fl
))
2082 if (first
->partner
&&
2083 !xfrm_selector_match(first
->partner
, fl
, family
))
2091 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2093 if (fl
&& !xfrm_selector_match(&dst
->xfrm
->sel
, fl
, family
))
2096 !security_xfrm_state_pol_flow_match(dst
->xfrm
, pol
, fl
))
2098 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2100 if (xdst
->genid
!= dst
->xfrm
->genid
)
2103 if (strict
&& fl
&& dst
->xfrm
->props
.mode
!= XFRM_MODE_TUNNEL
&&
2104 !xfrm_state_addr_flow_check(dst
->xfrm
, fl
, family
))
2107 mtu
= dst_mtu(dst
->child
);
2108 if (xdst
->child_mtu_cached
!= mtu
) {
2110 xdst
->child_mtu_cached
= mtu
;
2113 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2115 mtu
= dst_mtu(xdst
->route
);
2116 if (xdst
->route_mtu_cached
!= mtu
) {
2118 xdst
->route_mtu_cached
= mtu
;
2122 } while (dst
->xfrm
);
2127 mtu
= last
->child_mtu_cached
;
2131 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2132 if (mtu
> last
->route_mtu_cached
)
2133 mtu
= last
->route_mtu_cached
;
2134 dst
->metrics
[RTAX_MTU
-1] = mtu
;
2139 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2140 last
->child_mtu_cached
= mtu
;
2146 EXPORT_SYMBOL(xfrm_bundle_ok
);
2148 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2151 if (unlikely(afinfo
== NULL
))
2153 if (unlikely(afinfo
->family
>= NPROTO
))
2154 return -EAFNOSUPPORT
;
2155 write_lock_bh(&xfrm_policy_afinfo_lock
);
2156 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
2159 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2160 if (likely(dst_ops
->kmem_cachep
== NULL
))
2161 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2162 if (likely(dst_ops
->check
== NULL
))
2163 dst_ops
->check
= xfrm_dst_check
;
2164 if (likely(dst_ops
->negative_advice
== NULL
))
2165 dst_ops
->negative_advice
= xfrm_negative_advice
;
2166 if (likely(dst_ops
->link_failure
== NULL
))
2167 dst_ops
->link_failure
= xfrm_link_failure
;
2168 if (likely(afinfo
->garbage_collect
== NULL
))
2169 afinfo
->garbage_collect
= __xfrm_garbage_collect
;
2170 xfrm_policy_afinfo
[afinfo
->family
] = afinfo
;
2172 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2175 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2177 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2180 if (unlikely(afinfo
== NULL
))
2182 if (unlikely(afinfo
->family
>= NPROTO
))
2183 return -EAFNOSUPPORT
;
2184 write_lock_bh(&xfrm_policy_afinfo_lock
);
2185 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
2186 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
2189 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2190 xfrm_policy_afinfo
[afinfo
->family
] = NULL
;
2191 dst_ops
->kmem_cachep
= NULL
;
2192 dst_ops
->check
= NULL
;
2193 dst_ops
->negative_advice
= NULL
;
2194 dst_ops
->link_failure
= NULL
;
2195 afinfo
->garbage_collect
= NULL
;
2198 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2201 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2203 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
2205 struct xfrm_policy_afinfo
*afinfo
;
2206 if (unlikely(family
>= NPROTO
))
2208 read_lock(&xfrm_policy_afinfo_lock
);
2209 afinfo
= xfrm_policy_afinfo
[family
];
2210 if (unlikely(!afinfo
))
2211 read_unlock(&xfrm_policy_afinfo_lock
);
2215 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2217 read_unlock(&xfrm_policy_afinfo_lock
);
2220 static struct xfrm_policy_afinfo
*xfrm_policy_lock_afinfo(unsigned int family
)
2222 struct xfrm_policy_afinfo
*afinfo
;
2223 if (unlikely(family
>= NPROTO
))
2225 write_lock_bh(&xfrm_policy_afinfo_lock
);
2226 afinfo
= xfrm_policy_afinfo
[family
];
2227 if (unlikely(!afinfo
))
2228 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2232 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2234 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2237 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
2239 struct net_device
*dev
= ptr
;
2241 if (dev
->nd_net
!= &init_net
)
2246 xfrm_flush_bundles();
2251 static struct notifier_block xfrm_dev_notifier
= {
2257 static void __init
xfrm_policy_init(void)
2259 unsigned int hmask
, sz
;
2262 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2263 sizeof(struct xfrm_dst
),
2264 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2268 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2270 xfrm_policy_byidx
= xfrm_hash_alloc(sz
);
2271 xfrm_idx_hmask
= hmask
;
2272 if (!xfrm_policy_byidx
)
2273 panic("XFRM: failed to allocate byidx hash\n");
2275 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2276 struct xfrm_policy_hash
*htab
;
2278 INIT_HLIST_HEAD(&xfrm_policy_inexact
[dir
]);
2280 htab
= &xfrm_policy_bydst
[dir
];
2281 htab
->table
= xfrm_hash_alloc(sz
);
2282 htab
->hmask
= hmask
;
2284 panic("XFRM: failed to allocate bydst hash\n");
2287 INIT_WORK(&xfrm_policy_gc_work
, xfrm_policy_gc_task
);
2288 register_netdevice_notifier(&xfrm_dev_notifier
);
2291 void __init
xfrm_init(void)
2298 #ifdef CONFIG_AUDITSYSCALL
2299 static inline void xfrm_audit_common_policyinfo(struct xfrm_policy
*xp
,
2300 struct audit_buffer
*audit_buf
)
2303 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2304 xp
->security
->ctx_alg
, xp
->security
->ctx_doi
,
2305 xp
->security
->ctx_str
);
2307 switch(xp
->selector
.family
) {
2309 audit_log_format(audit_buf
, " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
2310 NIPQUAD(xp
->selector
.saddr
.a4
),
2311 NIPQUAD(xp
->selector
.daddr
.a4
));
2315 struct in6_addr saddr6
, daddr6
;
2317 memcpy(&saddr6
, xp
->selector
.saddr
.a6
,
2318 sizeof(struct in6_addr
));
2319 memcpy(&daddr6
, xp
->selector
.daddr
.a6
,
2320 sizeof(struct in6_addr
));
2321 audit_log_format(audit_buf
,
2322 " src=" NIP6_FMT
" dst=" NIP6_FMT
,
2323 NIP6(saddr6
), NIP6(daddr6
));
2330 xfrm_audit_policy_add(struct xfrm_policy
*xp
, int result
, u32 auid
, u32 sid
)
2332 struct audit_buffer
*audit_buf
;
2333 extern int audit_enabled
;
2335 if (audit_enabled
== 0)
2337 audit_buf
= xfrm_audit_start(sid
, auid
);
2338 if (audit_buf
== NULL
)
2340 audit_log_format(audit_buf
, " op=SPD-add res=%u", result
);
2341 xfrm_audit_common_policyinfo(xp
, audit_buf
);
2342 audit_log_end(audit_buf
);
2344 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add
);
2347 xfrm_audit_policy_delete(struct xfrm_policy
*xp
, int result
, u32 auid
, u32 sid
)
2349 struct audit_buffer
*audit_buf
;
2350 extern int audit_enabled
;
2352 if (audit_enabled
== 0)
2354 audit_buf
= xfrm_audit_start(sid
, auid
);
2355 if (audit_buf
== NULL
)
2357 audit_log_format(audit_buf
, " op=SPD-delete res=%u", result
);
2358 xfrm_audit_common_policyinfo(xp
, audit_buf
);
2359 audit_log_end(audit_buf
);
2361 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete
);
2364 #ifdef CONFIG_XFRM_MIGRATE
2365 static int xfrm_migrate_selector_match(struct xfrm_selector
*sel_cmp
,
2366 struct xfrm_selector
*sel_tgt
)
2368 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
2369 if (sel_tgt
->family
== sel_cmp
->family
&&
2370 xfrm_addr_cmp(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
2371 sel_cmp
->family
) == 0 &&
2372 xfrm_addr_cmp(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
2373 sel_cmp
->family
) == 0 &&
2374 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
2375 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
2379 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
2386 static struct xfrm_policy
* xfrm_migrate_policy_find(struct xfrm_selector
*sel
,
2389 struct xfrm_policy
*pol
, *ret
= NULL
;
2390 struct hlist_node
*entry
;
2391 struct hlist_head
*chain
;
2394 read_lock_bh(&xfrm_policy_lock
);
2395 chain
= policy_hash_direct(&sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
2396 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
2397 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2398 pol
->type
== type
) {
2400 priority
= ret
->priority
;
2404 chain
= &xfrm_policy_inexact
[dir
];
2405 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
2406 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2407 pol
->type
== type
&&
2408 pol
->priority
< priority
) {
2417 read_unlock_bh(&xfrm_policy_lock
);
2422 static int migrate_tmpl_match(struct xfrm_migrate
*m
, struct xfrm_tmpl
*t
)
2426 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
2427 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
2429 case XFRM_MODE_TUNNEL
:
2430 case XFRM_MODE_BEET
:
2431 if (xfrm_addr_cmp(&t
->id
.daddr
, &m
->old_daddr
,
2432 m
->old_family
) == 0 &&
2433 xfrm_addr_cmp(&t
->saddr
, &m
->old_saddr
,
2434 m
->old_family
) == 0) {
2438 case XFRM_MODE_TRANSPORT
:
2439 /* in case of transport mode, template does not store
2440 any IP addresses, hence we just compare mode and
2451 /* update endpoint address(es) of template(s) */
2452 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
2453 struct xfrm_migrate
*m
, int num_migrate
)
2455 struct xfrm_migrate
*mp
;
2456 struct dst_entry
*dst
;
2459 write_lock_bh(&pol
->lock
);
2460 if (unlikely(pol
->dead
)) {
2461 /* target policy has been deleted */
2462 write_unlock_bh(&pol
->lock
);
2466 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
2467 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
2468 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
2471 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
)
2473 /* update endpoints */
2474 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
2475 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
2476 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
2477 sizeof(pol
->xfrm_vec
[i
].saddr
));
2478 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
2480 while ((dst
= pol
->bundles
) != NULL
) {
2481 pol
->bundles
= dst
->next
;
2487 write_unlock_bh(&pol
->lock
);
2495 static int xfrm_migrate_check(struct xfrm_migrate
*m
, int num_migrate
)
2499 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
2502 for (i
= 0; i
< num_migrate
; i
++) {
2503 if ((xfrm_addr_cmp(&m
[i
].old_daddr
, &m
[i
].new_daddr
,
2504 m
[i
].old_family
) == 0) &&
2505 (xfrm_addr_cmp(&m
[i
].old_saddr
, &m
[i
].new_saddr
,
2506 m
[i
].old_family
) == 0))
2508 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
2509 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
2512 /* check if there is any duplicated entry */
2513 for (j
= i
+ 1; j
< num_migrate
; j
++) {
2514 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
2515 sizeof(m
[i
].old_daddr
)) &&
2516 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
2517 sizeof(m
[i
].old_saddr
)) &&
2518 m
[i
].proto
== m
[j
].proto
&&
2519 m
[i
].mode
== m
[j
].mode
&&
2520 m
[i
].reqid
== m
[j
].reqid
&&
2521 m
[i
].old_family
== m
[j
].old_family
)
2529 int xfrm_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2530 struct xfrm_migrate
*m
, int num_migrate
)
2532 int i
, err
, nx_cur
= 0, nx_new
= 0;
2533 struct xfrm_policy
*pol
= NULL
;
2534 struct xfrm_state
*x
, *xc
;
2535 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
2536 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
2537 struct xfrm_migrate
*mp
;
2539 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
2542 /* Stage 1 - find policy */
2543 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
)) == NULL
) {
2548 /* Stage 2 - find and update state(s) */
2549 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2550 if ((x
= xfrm_migrate_state_find(mp
))) {
2553 if ((xc
= xfrm_state_migrate(x
, mp
))) {
2563 /* Stage 3 - update policy */
2564 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
2567 /* Stage 4 - delete old state(s) */
2569 xfrm_states_put(x_cur
, nx_cur
);
2570 xfrm_states_delete(x_cur
, nx_cur
);
2573 /* Stage 5 - announce */
2574 km_migrate(sel
, dir
, type
, m
, num_migrate
);
2586 xfrm_states_put(x_cur
, nx_cur
);
2588 xfrm_states_delete(x_new
, nx_new
);
2592 EXPORT_SYMBOL(xfrm_migrate
);