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>
26 #include <linux/audit.h>
30 #include "xfrm_hash.h"
32 int sysctl_xfrm_larval_drop __read_mostly
;
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 #ifdef CONFIG_SECURITY_NETWORK_XFRM
838 xfrm_policy_flush_secctx_check(u8 type
, struct xfrm_audit
*audit_info
)
842 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
843 struct xfrm_policy
*pol
;
844 struct hlist_node
*entry
;
847 hlist_for_each_entry(pol
, entry
,
848 &xfrm_policy_inexact
[dir
], bydst
) {
849 if (pol
->type
!= type
)
851 err
= security_xfrm_policy_delete(pol
);
853 xfrm_audit_log(audit_info
->loginuid
,
855 AUDIT_MAC_IPSEC_DELSPD
, 0,
860 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
861 hlist_for_each_entry(pol
, entry
,
862 xfrm_policy_bydst
[dir
].table
+ i
,
864 if (pol
->type
!= type
)
866 err
= security_xfrm_policy_delete(pol
);
868 xfrm_audit_log(audit_info
->loginuid
,
870 AUDIT_MAC_IPSEC_DELSPD
,
881 xfrm_policy_flush_secctx_check(u8 type
, struct xfrm_audit
*audit_info
)
887 int xfrm_policy_flush(u8 type
, struct xfrm_audit
*audit_info
)
891 write_lock_bh(&xfrm_policy_lock
);
893 err
= xfrm_policy_flush_secctx_check(type
, audit_info
);
897 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
898 struct xfrm_policy
*pol
;
899 struct hlist_node
*entry
;
904 hlist_for_each_entry(pol
, entry
,
905 &xfrm_policy_inexact
[dir
], bydst
) {
906 if (pol
->type
!= type
)
908 hlist_del(&pol
->bydst
);
909 hlist_del(&pol
->byidx
);
910 write_unlock_bh(&xfrm_policy_lock
);
912 xfrm_audit_log(audit_info
->loginuid
, audit_info
->secid
,
913 AUDIT_MAC_IPSEC_DELSPD
, 1, pol
, NULL
);
915 xfrm_policy_kill(pol
);
918 write_lock_bh(&xfrm_policy_lock
);
922 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
924 hlist_for_each_entry(pol
, entry
,
925 xfrm_policy_bydst
[dir
].table
+ i
,
927 if (pol
->type
!= type
)
929 hlist_del(&pol
->bydst
);
930 hlist_del(&pol
->byidx
);
931 write_unlock_bh(&xfrm_policy_lock
);
933 xfrm_audit_log(audit_info
->loginuid
,
935 AUDIT_MAC_IPSEC_DELSPD
, 1,
938 xfrm_policy_kill(pol
);
941 write_lock_bh(&xfrm_policy_lock
);
946 xfrm_policy_count
[dir
] -= killed
;
948 atomic_inc(&flow_cache_genid
);
950 write_unlock_bh(&xfrm_policy_lock
);
953 EXPORT_SYMBOL(xfrm_policy_flush
);
955 int xfrm_policy_walk(u8 type
, int (*func
)(struct xfrm_policy
*, int, int, void*),
958 struct xfrm_policy
*pol
, *last
= NULL
;
959 struct hlist_node
*entry
;
960 int dir
, last_dir
= 0, count
, error
;
962 read_lock_bh(&xfrm_policy_lock
);
965 for (dir
= 0; dir
< 2*XFRM_POLICY_MAX
; dir
++) {
966 struct hlist_head
*table
= xfrm_policy_bydst
[dir
].table
;
969 hlist_for_each_entry(pol
, entry
,
970 &xfrm_policy_inexact
[dir
], bydst
) {
971 if (pol
->type
!= type
)
974 error
= func(last
, last_dir
% XFRM_POLICY_MAX
,
983 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
984 hlist_for_each_entry(pol
, entry
, table
+ i
, bydst
) {
985 if (pol
->type
!= type
)
988 error
= func(last
, last_dir
% XFRM_POLICY_MAX
,
1003 error
= func(last
, last_dir
% XFRM_POLICY_MAX
, 0, data
);
1005 read_unlock_bh(&xfrm_policy_lock
);
1008 EXPORT_SYMBOL(xfrm_policy_walk
);
1011 * Find policy to apply to this flow.
1013 * Returns 0 if policy found, else an -errno.
1015 static int xfrm_policy_match(struct xfrm_policy
*pol
, struct flowi
*fl
,
1016 u8 type
, u16 family
, int dir
)
1018 struct xfrm_selector
*sel
= &pol
->selector
;
1019 int match
, ret
= -ESRCH
;
1021 if (pol
->family
!= family
||
1025 match
= xfrm_selector_match(sel
, fl
, family
);
1027 ret
= security_xfrm_policy_lookup(pol
, fl
->secid
, dir
);
1032 static struct xfrm_policy
*xfrm_policy_lookup_bytype(u8 type
, struct flowi
*fl
,
1036 struct xfrm_policy
*pol
, *ret
;
1037 xfrm_address_t
*daddr
, *saddr
;
1038 struct hlist_node
*entry
;
1039 struct hlist_head
*chain
;
1042 daddr
= xfrm_flowi_daddr(fl
, family
);
1043 saddr
= xfrm_flowi_saddr(fl
, family
);
1044 if (unlikely(!daddr
|| !saddr
))
1047 read_lock_bh(&xfrm_policy_lock
);
1048 chain
= policy_hash_direct(daddr
, saddr
, family
, dir
);
1050 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
1051 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1061 priority
= ret
->priority
;
1065 chain
= &xfrm_policy_inexact
[dir
];
1066 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
1067 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1075 } else if (pol
->priority
< priority
) {
1083 read_unlock_bh(&xfrm_policy_lock
);
1088 static int xfrm_policy_lookup(struct flowi
*fl
, u16 family
, u8 dir
,
1089 void **objp
, atomic_t
**obj_refp
)
1091 struct xfrm_policy
*pol
;
1094 #ifdef CONFIG_XFRM_SUB_POLICY
1095 pol
= xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
1103 pol
= xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
1108 #ifdef CONFIG_XFRM_SUB_POLICY
1111 if ((*objp
= (void *) pol
) != NULL
)
1112 *obj_refp
= &pol
->refcnt
;
1116 static inline int policy_to_flow_dir(int dir
)
1118 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1119 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1120 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1124 case XFRM_POLICY_IN
:
1126 case XFRM_POLICY_OUT
:
1127 return FLOW_DIR_OUT
;
1128 case XFRM_POLICY_FWD
:
1129 return FLOW_DIR_FWD
;
1133 static struct xfrm_policy
*xfrm_sk_policy_lookup(struct sock
*sk
, int dir
, struct flowi
*fl
)
1135 struct xfrm_policy
*pol
;
1137 read_lock_bh(&xfrm_policy_lock
);
1138 if ((pol
= sk
->sk_policy
[dir
]) != NULL
) {
1139 int match
= xfrm_selector_match(&pol
->selector
, fl
,
1144 err
= security_xfrm_policy_lookup(pol
, fl
->secid
,
1145 policy_to_flow_dir(dir
));
1148 else if (err
== -ESRCH
)
1155 read_unlock_bh(&xfrm_policy_lock
);
1159 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1161 struct hlist_head
*chain
= policy_hash_bysel(&pol
->selector
,
1164 hlist_add_head(&pol
->bydst
, chain
);
1165 hlist_add_head(&pol
->byidx
, xfrm_policy_byidx
+idx_hash(pol
->index
));
1166 xfrm_policy_count
[dir
]++;
1169 if (xfrm_bydst_should_resize(dir
, NULL
))
1170 schedule_work(&xfrm_hash_work
);
1173 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1176 if (hlist_unhashed(&pol
->bydst
))
1179 hlist_del(&pol
->bydst
);
1180 hlist_del(&pol
->byidx
);
1181 xfrm_policy_count
[dir
]--;
1186 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1188 write_lock_bh(&xfrm_policy_lock
);
1189 pol
= __xfrm_policy_unlink(pol
, dir
);
1190 write_unlock_bh(&xfrm_policy_lock
);
1192 if (dir
< XFRM_POLICY_MAX
)
1193 atomic_inc(&flow_cache_genid
);
1194 xfrm_policy_kill(pol
);
1199 EXPORT_SYMBOL(xfrm_policy_delete
);
1201 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1203 struct xfrm_policy
*old_pol
;
1205 #ifdef CONFIG_XFRM_SUB_POLICY
1206 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1210 write_lock_bh(&xfrm_policy_lock
);
1211 old_pol
= sk
->sk_policy
[dir
];
1212 sk
->sk_policy
[dir
] = pol
;
1214 pol
->curlft
.add_time
= get_seconds();
1215 pol
->index
= xfrm_gen_index(pol
->type
, XFRM_POLICY_MAX
+dir
);
1216 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+dir
);
1219 __xfrm_policy_unlink(old_pol
, XFRM_POLICY_MAX
+dir
);
1220 write_unlock_bh(&xfrm_policy_lock
);
1223 xfrm_policy_kill(old_pol
);
1228 static struct xfrm_policy
*clone_policy(struct xfrm_policy
*old
, int dir
)
1230 struct xfrm_policy
*newp
= xfrm_policy_alloc(GFP_ATOMIC
);
1233 newp
->selector
= old
->selector
;
1234 if (security_xfrm_policy_clone(old
, newp
)) {
1236 return NULL
; /* ENOMEM */
1238 newp
->lft
= old
->lft
;
1239 newp
->curlft
= old
->curlft
;
1240 newp
->action
= old
->action
;
1241 newp
->flags
= old
->flags
;
1242 newp
->xfrm_nr
= old
->xfrm_nr
;
1243 newp
->index
= old
->index
;
1244 newp
->type
= old
->type
;
1245 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1246 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1247 write_lock_bh(&xfrm_policy_lock
);
1248 __xfrm_policy_link(newp
, XFRM_POLICY_MAX
+dir
);
1249 write_unlock_bh(&xfrm_policy_lock
);
1255 int __xfrm_sk_clone_policy(struct sock
*sk
)
1257 struct xfrm_policy
*p0
= sk
->sk_policy
[0],
1258 *p1
= sk
->sk_policy
[1];
1260 sk
->sk_policy
[0] = sk
->sk_policy
[1] = NULL
;
1261 if (p0
&& (sk
->sk_policy
[0] = clone_policy(p0
, 0)) == NULL
)
1263 if (p1
&& (sk
->sk_policy
[1] = clone_policy(p1
, 1)) == NULL
)
1269 xfrm_get_saddr(xfrm_address_t
*local
, xfrm_address_t
*remote
,
1270 unsigned short family
)
1273 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1275 if (unlikely(afinfo
== NULL
))
1277 err
= afinfo
->get_saddr(local
, remote
);
1278 xfrm_policy_put_afinfo(afinfo
);
1282 /* Resolve list of templates for the flow, given policy. */
1285 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, struct flowi
*fl
,
1286 struct xfrm_state
**xfrm
,
1287 unsigned short family
)
1291 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1292 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1295 for (nx
=0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1296 struct xfrm_state
*x
;
1297 xfrm_address_t
*remote
= daddr
;
1298 xfrm_address_t
*local
= saddr
;
1299 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1301 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1302 tmpl
->mode
== XFRM_MODE_BEET
) {
1303 remote
= &tmpl
->id
.daddr
;
1304 local
= &tmpl
->saddr
;
1305 family
= tmpl
->encap_family
;
1306 if (xfrm_addr_any(local
, family
)) {
1307 error
= xfrm_get_saddr(&tmp
, remote
, family
);
1314 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1316 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1323 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1328 if (!tmpl
->optional
)
1334 for (nx
--; nx
>=0; nx
--)
1335 xfrm_state_put(xfrm
[nx
]);
1340 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, struct flowi
*fl
,
1341 struct xfrm_state
**xfrm
,
1342 unsigned short family
)
1344 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1345 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1351 for (i
= 0; i
< npols
; i
++) {
1352 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1357 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1365 /* found states are sorted for outbound processing */
1367 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1372 for (cnx
--; cnx
>=0; cnx
--)
1373 xfrm_state_put(tpp
[cnx
]);
1378 /* Check that the bundle accepts the flow and its components are
1382 static struct dst_entry
*
1383 xfrm_find_bundle(struct flowi
*fl
, struct xfrm_policy
*policy
, unsigned short family
)
1385 struct dst_entry
*x
;
1386 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1387 if (unlikely(afinfo
== NULL
))
1388 return ERR_PTR(-EINVAL
);
1389 x
= afinfo
->find_bundle(fl
, policy
);
1390 xfrm_policy_put_afinfo(afinfo
);
1394 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1395 * all the metrics... Shortly, bundle a bundle.
1399 xfrm_bundle_create(struct xfrm_policy
*policy
, struct xfrm_state
**xfrm
, int nx
,
1400 struct flowi
*fl
, struct dst_entry
**dst_p
,
1401 unsigned short family
)
1404 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1405 if (unlikely(afinfo
== NULL
))
1407 err
= afinfo
->bundle_create(policy
, xfrm
, nx
, fl
, dst_p
);
1408 xfrm_policy_put_afinfo(afinfo
);
1413 xfrm_dst_alloc_copy(void **target
, void *src
, int size
)
1416 *target
= kmalloc(size
, GFP_ATOMIC
);
1420 memcpy(*target
, src
, size
);
1425 xfrm_dst_update_parent(struct dst_entry
*dst
, struct xfrm_selector
*sel
)
1427 #ifdef CONFIG_XFRM_SUB_POLICY
1428 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1429 return xfrm_dst_alloc_copy((void **)&(xdst
->partner
),
1437 xfrm_dst_update_origin(struct dst_entry
*dst
, struct flowi
*fl
)
1439 #ifdef CONFIG_XFRM_SUB_POLICY
1440 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1441 return xfrm_dst_alloc_copy((void **)&(xdst
->origin
), fl
, sizeof(*fl
));
1447 static int stale_bundle(struct dst_entry
*dst
);
1449 /* Main function: finds/creates a bundle for given flow.
1451 * At the moment we eat a raw IP route. Mostly to speed up lookups
1452 * on interfaces with disabled IPsec.
1454 int __xfrm_lookup(struct dst_entry
**dst_p
, struct flowi
*fl
,
1455 struct sock
*sk
, int flags
)
1457 struct xfrm_policy
*policy
;
1458 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1463 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1464 struct dst_entry
*dst
, *dst_orig
= *dst_p
;
1469 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
1472 genid
= atomic_read(&flow_cache_genid
);
1474 for (pi
= 0; pi
< ARRAY_SIZE(pols
); pi
++)
1480 if (sk
&& sk
->sk_policy
[1]) {
1481 policy
= xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
);
1483 return PTR_ERR(policy
);
1487 /* To accelerate a bit... */
1488 if ((dst_orig
->flags
& DST_NOXFRM
) ||
1489 !xfrm_policy_count
[XFRM_POLICY_OUT
])
1492 policy
= flow_cache_lookup(fl
, dst_orig
->ops
->family
,
1493 dir
, xfrm_policy_lookup
);
1495 return PTR_ERR(policy
);
1501 family
= dst_orig
->ops
->family
;
1502 policy
->curlft
.use_time
= get_seconds();
1505 xfrm_nr
+= pols
[0]->xfrm_nr
;
1507 switch (policy
->action
) {
1508 case XFRM_POLICY_BLOCK
:
1509 /* Prohibit the flow */
1513 case XFRM_POLICY_ALLOW
:
1514 #ifndef CONFIG_XFRM_SUB_POLICY
1515 if (policy
->xfrm_nr
== 0) {
1516 /* Flow passes not transformed. */
1517 xfrm_pol_put(policy
);
1522 /* Try to find matching bundle.
1524 * LATER: help from flow cache. It is optional, this
1525 * is required only for output policy.
1527 dst
= xfrm_find_bundle(fl
, policy
, family
);
1536 #ifdef CONFIG_XFRM_SUB_POLICY
1537 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1538 pols
[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
,
1542 if (IS_ERR(pols
[1])) {
1543 err
= PTR_ERR(pols
[1]);
1546 if (pols
[1]->action
== XFRM_POLICY_BLOCK
) {
1551 xfrm_nr
+= pols
[1]->xfrm_nr
;
1556 * Because neither flowi nor bundle information knows about
1557 * transformation template size. On more than one policy usage
1558 * we can realize whether all of them is bypass or not after
1559 * they are searched. See above not-transformed bypass
1560 * is surrounded by non-sub policy configuration, too.
1563 /* Flow passes not transformed. */
1564 xfrm_pols_put(pols
, npols
);
1569 nx
= xfrm_tmpl_resolve(pols
, npols
, fl
, xfrm
, family
);
1571 if (unlikely(nx
<0)) {
1573 if (err
== -EAGAIN
&& sysctl_xfrm_larval_drop
) {
1574 /* EREMOTE tells the caller to generate
1575 * a one-shot blackhole route.
1577 xfrm_pol_put(policy
);
1580 if (err
== -EAGAIN
&& flags
) {
1581 DECLARE_WAITQUEUE(wait
, current
);
1583 add_wait_queue(&km_waitq
, &wait
);
1584 set_current_state(TASK_INTERRUPTIBLE
);
1586 set_current_state(TASK_RUNNING
);
1587 remove_wait_queue(&km_waitq
, &wait
);
1589 nx
= xfrm_tmpl_resolve(pols
, npols
, fl
, xfrm
, family
);
1591 if (nx
== -EAGAIN
&& signal_pending(current
)) {
1595 if (nx
== -EAGAIN
||
1596 genid
!= atomic_read(&flow_cache_genid
)) {
1597 xfrm_pols_put(pols
, npols
);
1606 /* Flow passes not transformed. */
1607 xfrm_pols_put(pols
, npols
);
1612 err
= xfrm_bundle_create(policy
, xfrm
, nx
, fl
, &dst
, family
);
1614 if (unlikely(err
)) {
1616 for (i
=0; i
<nx
; i
++)
1617 xfrm_state_put(xfrm
[i
]);
1621 for (pi
= 0; pi
< npols
; pi
++) {
1622 read_lock_bh(&pols
[pi
]->lock
);
1623 pol_dead
|= pols
[pi
]->dead
;
1624 read_unlock_bh(&pols
[pi
]->lock
);
1627 write_lock_bh(&policy
->lock
);
1628 if (unlikely(pol_dead
|| stale_bundle(dst
))) {
1629 /* Wow! While we worked on resolving, this
1630 * policy has gone. Retry. It is not paranoia,
1631 * we just cannot enlist new bundle to dead object.
1632 * We can't enlist stable bundles either.
1634 write_unlock_bh(&policy
->lock
);
1638 err
= -EHOSTUNREACH
;
1643 err
= xfrm_dst_update_parent(dst
, &pols
[1]->selector
);
1645 err
= xfrm_dst_update_origin(dst
, fl
);
1646 if (unlikely(err
)) {
1647 write_unlock_bh(&policy
->lock
);
1653 dst
->next
= policy
->bundles
;
1654 policy
->bundles
= dst
;
1656 write_unlock_bh(&policy
->lock
);
1659 dst_release(dst_orig
);
1660 xfrm_pols_put(pols
, npols
);
1664 dst_release(dst_orig
);
1665 xfrm_pols_put(pols
, npols
);
1669 EXPORT_SYMBOL(__xfrm_lookup
);
1671 int xfrm_lookup(struct dst_entry
**dst_p
, struct flowi
*fl
,
1672 struct sock
*sk
, int flags
)
1674 int err
= __xfrm_lookup(dst_p
, fl
, sk
, flags
);
1676 if (err
== -EREMOTE
) {
1677 dst_release(*dst_p
);
1684 EXPORT_SYMBOL(xfrm_lookup
);
1687 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, struct flowi
*fl
)
1689 struct xfrm_state
*x
;
1692 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
1694 x
= skb
->sp
->xvec
[idx
];
1695 if (!x
->type
->reject
)
1698 err
= x
->type
->reject(x
, skb
, fl
);
1703 /* When skb is transformed back to its "native" form, we have to
1704 * check policy restrictions. At the moment we make this in maximally
1705 * stupid way. Shame on me. :-) Of course, connected sockets must
1706 * have policy cached at them.
1710 xfrm_state_ok(struct xfrm_tmpl
*tmpl
, struct xfrm_state
*x
,
1711 unsigned short family
)
1713 if (xfrm_state_kern(x
))
1714 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
1715 return x
->id
.proto
== tmpl
->id
.proto
&&
1716 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
1717 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
1718 x
->props
.mode
== tmpl
->mode
&&
1719 ((tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
1720 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
1721 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
1722 xfrm_state_addr_cmp(tmpl
, x
, family
));
1726 * 0 or more than 0 is returned when validation is succeeded (either bypass
1727 * because of optional transport mode, or next index of the mathced secpath
1728 * state with the template.
1729 * -1 is returned when no matching template is found.
1730 * Otherwise "-2 - errored_index" is returned.
1733 xfrm_policy_ok(struct xfrm_tmpl
*tmpl
, struct sec_path
*sp
, int start
,
1734 unsigned short family
)
1738 if (tmpl
->optional
) {
1739 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
1743 for (; idx
< sp
->len
; idx
++) {
1744 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
1746 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1756 xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, unsigned short family
)
1758 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1761 if (unlikely(afinfo
== NULL
))
1762 return -EAFNOSUPPORT
;
1764 afinfo
->decode_session(skb
, fl
);
1765 err
= security_xfrm_decode_session(skb
, &fl
->secid
);
1766 xfrm_policy_put_afinfo(afinfo
);
1769 EXPORT_SYMBOL(xfrm_decode_session
);
1771 static inline int secpath_has_nontransport(struct sec_path
*sp
, int k
, int *idxp
)
1773 for (; k
< sp
->len
; k
++) {
1774 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1783 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
1784 unsigned short family
)
1786 struct xfrm_policy
*pol
;
1787 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1792 u8 fl_dir
= policy_to_flow_dir(dir
);
1795 if (xfrm_decode_session(skb
, &fl
, family
) < 0)
1797 nf_nat_decode_session(skb
, &fl
, family
);
1799 /* First, check used SA against their selectors. */
1803 for (i
=skb
->sp
->len
-1; i
>=0; i
--) {
1804 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
1805 if (!xfrm_selector_match(&x
->sel
, &fl
, family
))
1811 if (sk
&& sk
->sk_policy
[dir
]) {
1812 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
);
1818 pol
= flow_cache_lookup(&fl
, family
, fl_dir
,
1819 xfrm_policy_lookup
);
1825 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
1826 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
1832 pol
->curlft
.use_time
= get_seconds();
1836 #ifdef CONFIG_XFRM_SUB_POLICY
1837 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1838 pols
[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
,
1842 if (IS_ERR(pols
[1]))
1844 pols
[1]->curlft
.use_time
= get_seconds();
1850 if (pol
->action
== XFRM_POLICY_ALLOW
) {
1851 struct sec_path
*sp
;
1852 static struct sec_path dummy
;
1853 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
1854 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
1855 struct xfrm_tmpl
**tpp
= tp
;
1859 if ((sp
= skb
->sp
) == NULL
)
1862 for (pi
= 0; pi
< npols
; pi
++) {
1863 if (pols
[pi
] != pol
&&
1864 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
)
1866 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
)
1868 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
1869 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
1873 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
);
1877 /* For each tunnel xfrm, find the first matching tmpl.
1878 * For each tmpl before that, find corresponding xfrm.
1879 * Order is _important_. Later we will implement
1880 * some barriers, but at the moment barriers
1881 * are implied between each two transformations.
1883 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
1884 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
1887 /* "-2 - errored_index" returned */
1893 if (secpath_has_nontransport(sp
, k
, &xerr_idx
))
1896 xfrm_pols_put(pols
, npols
);
1901 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
1903 xfrm_pols_put(pols
, npols
);
1906 EXPORT_SYMBOL(__xfrm_policy_check
);
1908 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
1912 if (xfrm_decode_session(skb
, &fl
, family
) < 0)
1915 return xfrm_lookup(&skb
->dst
, &fl
, NULL
, 0) == 0;
1917 EXPORT_SYMBOL(__xfrm_route_forward
);
1919 /* Optimize later using cookies and generation ids. */
1921 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
1923 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1924 * to "-1" to force all XFRM destinations to get validated by
1925 * dst_ops->check on every use. We do this because when a
1926 * normal route referenced by an XFRM dst is obsoleted we do
1927 * not go looking around for all parent referencing XFRM dsts
1928 * so that we can invalidate them. It is just too much work.
1929 * Instead we make the checks here on every use. For example:
1931 * XFRM dst A --> IPv4 dst X
1933 * X is the "xdst->route" of A (X is also the "dst->path" of A
1934 * in this example). If X is marked obsolete, "A" will not
1935 * notice. That's what we are validating here via the
1936 * stale_bundle() check.
1938 * When a policy's bundle is pruned, we dst_free() the XFRM
1939 * dst which causes it's ->obsolete field to be set to a
1940 * positive non-zero integer. If an XFRM dst has been pruned
1941 * like this, we want to force a new route lookup.
1943 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
1949 static int stale_bundle(struct dst_entry
*dst
)
1951 return !xfrm_bundle_ok(NULL
, (struct xfrm_dst
*)dst
, NULL
, AF_UNSPEC
, 0);
1954 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
1956 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
1957 dst
->dev
= &loopback_dev
;
1958 dev_hold(&loopback_dev
);
1962 EXPORT_SYMBOL(xfrm_dst_ifdown
);
1964 static void xfrm_link_failure(struct sk_buff
*skb
)
1966 /* Impossible. Such dst must be popped before reaches point of failure. */
1970 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
1973 if (dst
->obsolete
) {
1981 static void prune_one_bundle(struct xfrm_policy
*pol
, int (*func
)(struct dst_entry
*), struct dst_entry
**gc_list_p
)
1983 struct dst_entry
*dst
, **dstp
;
1985 write_lock(&pol
->lock
);
1986 dstp
= &pol
->bundles
;
1987 while ((dst
=*dstp
) != NULL
) {
1990 dst
->next
= *gc_list_p
;
1996 write_unlock(&pol
->lock
);
1999 static void xfrm_prune_bundles(int (*func
)(struct dst_entry
*))
2001 struct dst_entry
*gc_list
= NULL
;
2004 read_lock_bh(&xfrm_policy_lock
);
2005 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2006 struct xfrm_policy
*pol
;
2007 struct hlist_node
*entry
;
2008 struct hlist_head
*table
;
2011 hlist_for_each_entry(pol
, entry
,
2012 &xfrm_policy_inexact
[dir
], bydst
)
2013 prune_one_bundle(pol
, func
, &gc_list
);
2015 table
= xfrm_policy_bydst
[dir
].table
;
2016 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
2017 hlist_for_each_entry(pol
, entry
, table
+ i
, bydst
)
2018 prune_one_bundle(pol
, func
, &gc_list
);
2021 read_unlock_bh(&xfrm_policy_lock
);
2024 struct dst_entry
*dst
= gc_list
;
2025 gc_list
= dst
->next
;
2030 static int unused_bundle(struct dst_entry
*dst
)
2032 return !atomic_read(&dst
->__refcnt
);
2035 static void __xfrm_garbage_collect(void)
2037 xfrm_prune_bundles(unused_bundle
);
2040 static int xfrm_flush_bundles(void)
2042 xfrm_prune_bundles(stale_bundle
);
2046 void xfrm_init_pmtu(struct dst_entry
*dst
)
2049 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2050 u32 pmtu
, route_mtu_cached
;
2052 pmtu
= dst_mtu(dst
->child
);
2053 xdst
->child_mtu_cached
= pmtu
;
2055 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2057 route_mtu_cached
= dst_mtu(xdst
->route
);
2058 xdst
->route_mtu_cached
= route_mtu_cached
;
2060 if (pmtu
> route_mtu_cached
)
2061 pmtu
= route_mtu_cached
;
2063 dst
->metrics
[RTAX_MTU
-1] = pmtu
;
2064 } while ((dst
= dst
->next
));
2067 EXPORT_SYMBOL(xfrm_init_pmtu
);
2069 /* Check that the bundle accepts the flow and its components are
2073 int xfrm_bundle_ok(struct xfrm_policy
*pol
, struct xfrm_dst
*first
,
2074 struct flowi
*fl
, int family
, int strict
)
2076 struct dst_entry
*dst
= &first
->u
.dst
;
2077 struct xfrm_dst
*last
;
2080 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2081 (dst
->dev
&& !netif_running(dst
->dev
)))
2083 #ifdef CONFIG_XFRM_SUB_POLICY
2085 if (first
->origin
&& !flow_cache_uli_match(first
->origin
, fl
))
2087 if (first
->partner
&&
2088 !xfrm_selector_match(first
->partner
, fl
, family
))
2096 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2098 if (fl
&& !xfrm_selector_match(&dst
->xfrm
->sel
, fl
, family
))
2101 !security_xfrm_state_pol_flow_match(dst
->xfrm
, pol
, fl
))
2103 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2105 if (xdst
->genid
!= dst
->xfrm
->genid
)
2108 if (strict
&& fl
&& dst
->xfrm
->props
.mode
!= XFRM_MODE_TUNNEL
&&
2109 !xfrm_state_addr_flow_check(dst
->xfrm
, fl
, family
))
2112 mtu
= dst_mtu(dst
->child
);
2113 if (xdst
->child_mtu_cached
!= mtu
) {
2115 xdst
->child_mtu_cached
= mtu
;
2118 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2120 mtu
= dst_mtu(xdst
->route
);
2121 if (xdst
->route_mtu_cached
!= mtu
) {
2123 xdst
->route_mtu_cached
= mtu
;
2127 } while (dst
->xfrm
);
2132 mtu
= last
->child_mtu_cached
;
2136 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2137 if (mtu
> last
->route_mtu_cached
)
2138 mtu
= last
->route_mtu_cached
;
2139 dst
->metrics
[RTAX_MTU
-1] = mtu
;
2144 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2145 last
->child_mtu_cached
= mtu
;
2151 EXPORT_SYMBOL(xfrm_bundle_ok
);
2153 #ifdef CONFIG_AUDITSYSCALL
2154 /* Audit addition and deletion of SAs and ipsec policy */
2156 void xfrm_audit_log(uid_t auid
, u32 sid
, int type
, int result
,
2157 struct xfrm_policy
*xp
, struct xfrm_state
*x
)
2162 struct xfrm_sec_ctx
*sctx
= NULL
;
2163 struct audit_buffer
*audit_buf
;
2165 extern int audit_enabled
;
2167 if (audit_enabled
== 0)
2170 BUG_ON((type
== AUDIT_MAC_IPSEC_ADDSA
||
2171 type
== AUDIT_MAC_IPSEC_DELSA
) && !x
);
2172 BUG_ON((type
== AUDIT_MAC_IPSEC_ADDSPD
||
2173 type
== AUDIT_MAC_IPSEC_DELSPD
) && !xp
);
2175 audit_buf
= audit_log_start(current
->audit_context
, GFP_ATOMIC
, type
);
2176 if (audit_buf
== NULL
)
2180 case AUDIT_MAC_IPSEC_ADDSA
:
2181 audit_log_format(audit_buf
, "SAD add: auid=%u", auid
);
2183 case AUDIT_MAC_IPSEC_DELSA
:
2184 audit_log_format(audit_buf
, "SAD delete: auid=%u", auid
);
2186 case AUDIT_MAC_IPSEC_ADDSPD
:
2187 audit_log_format(audit_buf
, "SPD add: auid=%u", auid
);
2189 case AUDIT_MAC_IPSEC_DELSPD
:
2190 audit_log_format(audit_buf
, "SPD delete: auid=%u", auid
);
2197 security_secid_to_secctx(sid
, &secctx
, &secctx_len
) == 0) {
2198 audit_log_format(audit_buf
, " subj=%s", secctx
);
2199 security_release_secctx(secctx
, secctx_len
);
2201 audit_log_task_context(audit_buf
);
2204 family
= xp
->selector
.family
;
2206 sctx
= xp
->security
;
2208 family
= x
->props
.family
;
2214 audit_log_format(audit_buf
,
2215 " sec_alg=%u sec_doi=%u sec_obj=%s",
2216 sctx
->ctx_alg
, sctx
->ctx_doi
, sctx
->ctx_str
);
2221 struct in_addr saddr
, daddr
;
2223 saddr
.s_addr
= xp
->selector
.saddr
.a4
;
2224 daddr
.s_addr
= xp
->selector
.daddr
.a4
;
2226 saddr
.s_addr
= x
->props
.saddr
.a4
;
2227 daddr
.s_addr
= x
->id
.daddr
.a4
;
2229 audit_log_format(audit_buf
,
2230 " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
2231 NIPQUAD(saddr
), NIPQUAD(daddr
));
2236 struct in6_addr saddr6
, daddr6
;
2238 memcpy(&saddr6
, xp
->selector
.saddr
.a6
,
2239 sizeof(struct in6_addr
));
2240 memcpy(&daddr6
, xp
->selector
.daddr
.a6
,
2241 sizeof(struct in6_addr
));
2243 memcpy(&saddr6
, x
->props
.saddr
.a6
,
2244 sizeof(struct in6_addr
));
2245 memcpy(&daddr6
, x
->id
.daddr
.a6
,
2246 sizeof(struct in6_addr
));
2248 audit_log_format(audit_buf
,
2249 " src=" NIP6_FMT
" dst=" NIP6_FMT
,
2250 NIP6(saddr6
), NIP6(daddr6
));
2256 audit_log_format(audit_buf
, " spi=%lu(0x%lx) protocol=%s",
2257 (unsigned long)ntohl(x
->id
.spi
),
2258 (unsigned long)ntohl(x
->id
.spi
),
2259 x
->id
.proto
== IPPROTO_AH
? "AH" :
2260 (x
->id
.proto
== IPPROTO_ESP
?
2263 audit_log_format(audit_buf
, " res=%u", result
);
2264 audit_log_end(audit_buf
);
2267 EXPORT_SYMBOL(xfrm_audit_log
);
2268 #endif /* CONFIG_AUDITSYSCALL */
2270 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2273 if (unlikely(afinfo
== NULL
))
2275 if (unlikely(afinfo
->family
>= NPROTO
))
2276 return -EAFNOSUPPORT
;
2277 write_lock_bh(&xfrm_policy_afinfo_lock
);
2278 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
2281 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2282 if (likely(dst_ops
->kmem_cachep
== NULL
))
2283 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2284 if (likely(dst_ops
->check
== NULL
))
2285 dst_ops
->check
= xfrm_dst_check
;
2286 if (likely(dst_ops
->negative_advice
== NULL
))
2287 dst_ops
->negative_advice
= xfrm_negative_advice
;
2288 if (likely(dst_ops
->link_failure
== NULL
))
2289 dst_ops
->link_failure
= xfrm_link_failure
;
2290 if (likely(afinfo
->garbage_collect
== NULL
))
2291 afinfo
->garbage_collect
= __xfrm_garbage_collect
;
2292 xfrm_policy_afinfo
[afinfo
->family
] = afinfo
;
2294 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2297 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2299 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2302 if (unlikely(afinfo
== NULL
))
2304 if (unlikely(afinfo
->family
>= NPROTO
))
2305 return -EAFNOSUPPORT
;
2306 write_lock_bh(&xfrm_policy_afinfo_lock
);
2307 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
2308 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
2311 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2312 xfrm_policy_afinfo
[afinfo
->family
] = NULL
;
2313 dst_ops
->kmem_cachep
= NULL
;
2314 dst_ops
->check
= NULL
;
2315 dst_ops
->negative_advice
= NULL
;
2316 dst_ops
->link_failure
= NULL
;
2317 afinfo
->garbage_collect
= NULL
;
2320 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2323 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2325 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
2327 struct xfrm_policy_afinfo
*afinfo
;
2328 if (unlikely(family
>= NPROTO
))
2330 read_lock(&xfrm_policy_afinfo_lock
);
2331 afinfo
= xfrm_policy_afinfo
[family
];
2332 if (unlikely(!afinfo
))
2333 read_unlock(&xfrm_policy_afinfo_lock
);
2337 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2339 read_unlock(&xfrm_policy_afinfo_lock
);
2342 static struct xfrm_policy_afinfo
*xfrm_policy_lock_afinfo(unsigned int family
)
2344 struct xfrm_policy_afinfo
*afinfo
;
2345 if (unlikely(family
>= NPROTO
))
2347 write_lock_bh(&xfrm_policy_afinfo_lock
);
2348 afinfo
= xfrm_policy_afinfo
[family
];
2349 if (unlikely(!afinfo
))
2350 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2354 static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2356 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2359 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
2363 xfrm_flush_bundles();
2368 static struct notifier_block xfrm_dev_notifier
= {
2374 static void __init
xfrm_policy_init(void)
2376 unsigned int hmask
, sz
;
2379 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2380 sizeof(struct xfrm_dst
),
2381 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2385 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2387 xfrm_policy_byidx
= xfrm_hash_alloc(sz
);
2388 xfrm_idx_hmask
= hmask
;
2389 if (!xfrm_policy_byidx
)
2390 panic("XFRM: failed to allocate byidx hash\n");
2392 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2393 struct xfrm_policy_hash
*htab
;
2395 INIT_HLIST_HEAD(&xfrm_policy_inexact
[dir
]);
2397 htab
= &xfrm_policy_bydst
[dir
];
2398 htab
->table
= xfrm_hash_alloc(sz
);
2399 htab
->hmask
= hmask
;
2401 panic("XFRM: failed to allocate bydst hash\n");
2404 INIT_WORK(&xfrm_policy_gc_work
, xfrm_policy_gc_task
);
2405 register_netdevice_notifier(&xfrm_dev_notifier
);
2408 void __init
xfrm_init(void)
2415 #ifdef CONFIG_XFRM_MIGRATE
2416 static int xfrm_migrate_selector_match(struct xfrm_selector
*sel_cmp
,
2417 struct xfrm_selector
*sel_tgt
)
2419 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
2420 if (sel_tgt
->family
== sel_cmp
->family
&&
2421 xfrm_addr_cmp(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
2422 sel_cmp
->family
) == 0 &&
2423 xfrm_addr_cmp(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
2424 sel_cmp
->family
) == 0 &&
2425 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
2426 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
2430 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
2437 static struct xfrm_policy
* xfrm_migrate_policy_find(struct xfrm_selector
*sel
,
2440 struct xfrm_policy
*pol
, *ret
= NULL
;
2441 struct hlist_node
*entry
;
2442 struct hlist_head
*chain
;
2445 read_lock_bh(&xfrm_policy_lock
);
2446 chain
= policy_hash_direct(&sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
2447 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
2448 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2449 pol
->type
== type
) {
2451 priority
= ret
->priority
;
2455 chain
= &xfrm_policy_inexact
[dir
];
2456 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
2457 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2458 pol
->type
== type
&&
2459 pol
->priority
< priority
) {
2468 read_unlock_bh(&xfrm_policy_lock
);
2473 static int migrate_tmpl_match(struct xfrm_migrate
*m
, struct xfrm_tmpl
*t
)
2477 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
2478 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
2480 case XFRM_MODE_TUNNEL
:
2481 case XFRM_MODE_BEET
:
2482 if (xfrm_addr_cmp(&t
->id
.daddr
, &m
->old_daddr
,
2483 m
->old_family
) == 0 &&
2484 xfrm_addr_cmp(&t
->saddr
, &m
->old_saddr
,
2485 m
->old_family
) == 0) {
2489 case XFRM_MODE_TRANSPORT
:
2490 /* in case of transport mode, template does not store
2491 any IP addresses, hence we just compare mode and
2502 /* update endpoint address(es) of template(s) */
2503 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
2504 struct xfrm_migrate
*m
, int num_migrate
)
2506 struct xfrm_migrate
*mp
;
2507 struct dst_entry
*dst
;
2510 write_lock_bh(&pol
->lock
);
2511 if (unlikely(pol
->dead
)) {
2512 /* target policy has been deleted */
2513 write_unlock_bh(&pol
->lock
);
2517 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
2518 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
2519 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
2522 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
)
2524 /* update endpoints */
2525 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
2526 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
2527 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
2528 sizeof(pol
->xfrm_vec
[i
].saddr
));
2529 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
2531 while ((dst
= pol
->bundles
) != NULL
) {
2532 pol
->bundles
= dst
->next
;
2538 write_unlock_bh(&pol
->lock
);
2546 static int xfrm_migrate_check(struct xfrm_migrate
*m
, int num_migrate
)
2550 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
2553 for (i
= 0; i
< num_migrate
; i
++) {
2554 if ((xfrm_addr_cmp(&m
[i
].old_daddr
, &m
[i
].new_daddr
,
2555 m
[i
].old_family
) == 0) &&
2556 (xfrm_addr_cmp(&m
[i
].old_saddr
, &m
[i
].new_saddr
,
2557 m
[i
].old_family
) == 0))
2559 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
2560 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
2563 /* check if there is any duplicated entry */
2564 for (j
= i
+ 1; j
< num_migrate
; j
++) {
2565 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
2566 sizeof(m
[i
].old_daddr
)) &&
2567 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
2568 sizeof(m
[i
].old_saddr
)) &&
2569 m
[i
].proto
== m
[j
].proto
&&
2570 m
[i
].mode
== m
[j
].mode
&&
2571 m
[i
].reqid
== m
[j
].reqid
&&
2572 m
[i
].old_family
== m
[j
].old_family
)
2580 int xfrm_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2581 struct xfrm_migrate
*m
, int num_migrate
)
2583 int i
, err
, nx_cur
= 0, nx_new
= 0;
2584 struct xfrm_policy
*pol
= NULL
;
2585 struct xfrm_state
*x
, *xc
;
2586 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
2587 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
2588 struct xfrm_migrate
*mp
;
2590 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
2593 /* Stage 1 - find policy */
2594 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
)) == NULL
) {
2599 /* Stage 2 - find and update state(s) */
2600 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2601 if ((x
= xfrm_migrate_state_find(mp
))) {
2604 if ((xc
= xfrm_state_migrate(x
, mp
))) {
2614 /* Stage 3 - update policy */
2615 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
2618 /* Stage 4 - delete old state(s) */
2620 xfrm_states_put(x_cur
, nx_cur
);
2621 xfrm_states_delete(x_cur
, nx_cur
);
2624 /* Stage 5 - announce */
2625 km_migrate(sel
, dir
, type
, m
, num_migrate
);
2637 xfrm_states_put(x_cur
, nx_cur
);
2639 xfrm_states_delete(x_new
, nx_new
);
2643 EXPORT_SYMBOL(xfrm_migrate
);