6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * Kazunori MIYAZAWA @USAGI
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/kmod.h>
19 #include <linux/list.h>
20 #include <linux/spinlock.h>
21 #include <linux/workqueue.h>
22 #include <linux/notifier.h>
23 #include <linux/netdevice.h>
24 #include <linux/netfilter.h>
25 #include <linux/module.h>
26 #include <linux/cache.h>
27 #include <linux/audit.h>
31 #ifdef CONFIG_XFRM_STATISTICS
35 #include "xfrm_hash.h"
37 int sysctl_xfrm_larval_drop __read_mostly
;
39 #ifdef CONFIG_XFRM_STATISTICS
40 DEFINE_SNMP_STAT(struct linux_xfrm_mib
, xfrm_statistics
) __read_mostly
;
41 EXPORT_SYMBOL(xfrm_statistics
);
44 DEFINE_MUTEX(xfrm_cfg_mutex
);
45 EXPORT_SYMBOL(xfrm_cfg_mutex
);
47 static DEFINE_RWLOCK(xfrm_policy_lock
);
49 unsigned int xfrm_policy_count
[XFRM_POLICY_MAX
*2];
50 EXPORT_SYMBOL(xfrm_policy_count
);
52 static DEFINE_RWLOCK(xfrm_policy_afinfo_lock
);
53 static struct xfrm_policy_afinfo
*xfrm_policy_afinfo
[NPROTO
];
55 static struct kmem_cache
*xfrm_dst_cache __read_mostly
;
57 static struct work_struct xfrm_policy_gc_work
;
58 static HLIST_HEAD(xfrm_policy_gc_list
);
59 static DEFINE_SPINLOCK(xfrm_policy_gc_lock
);
61 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
);
62 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
);
63 static void xfrm_init_pmtu(struct dst_entry
*dst
);
66 __xfrm4_selector_match(struct xfrm_selector
*sel
, struct flowi
*fl
)
68 return addr_match(&fl
->fl4_dst
, &sel
->daddr
, sel
->prefixlen_d
) &&
69 addr_match(&fl
->fl4_src
, &sel
->saddr
, sel
->prefixlen_s
) &&
70 !((xfrm_flowi_dport(fl
) ^ sel
->dport
) & sel
->dport_mask
) &&
71 !((xfrm_flowi_sport(fl
) ^ sel
->sport
) & sel
->sport_mask
) &&
72 (fl
->proto
== sel
->proto
|| !sel
->proto
) &&
73 (fl
->oif
== sel
->ifindex
|| !sel
->ifindex
);
77 __xfrm6_selector_match(struct xfrm_selector
*sel
, struct flowi
*fl
)
79 return addr_match(&fl
->fl6_dst
, &sel
->daddr
, sel
->prefixlen_d
) &&
80 addr_match(&fl
->fl6_src
, &sel
->saddr
, sel
->prefixlen_s
) &&
81 !((xfrm_flowi_dport(fl
) ^ sel
->dport
) & sel
->dport_mask
) &&
82 !((xfrm_flowi_sport(fl
) ^ sel
->sport
) & sel
->sport_mask
) &&
83 (fl
->proto
== sel
->proto
|| !sel
->proto
) &&
84 (fl
->oif
== sel
->ifindex
|| !sel
->ifindex
);
87 int xfrm_selector_match(struct xfrm_selector
*sel
, struct flowi
*fl
,
88 unsigned short family
)
92 return __xfrm4_selector_match(sel
, fl
);
94 return __xfrm6_selector_match(sel
, fl
);
99 static inline struct dst_entry
*xfrm_dst_lookup(struct xfrm_state
*x
, int tos
,
102 xfrm_address_t
*saddr
= &x
->props
.saddr
;
103 xfrm_address_t
*daddr
= &x
->id
.daddr
;
104 struct xfrm_policy_afinfo
*afinfo
;
105 struct dst_entry
*dst
;
107 if (x
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
)
109 if (x
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
)
112 afinfo
= xfrm_policy_get_afinfo(family
);
113 if (unlikely(afinfo
== NULL
))
114 return ERR_PTR(-EAFNOSUPPORT
);
116 dst
= afinfo
->dst_lookup(tos
, saddr
, daddr
);
117 xfrm_policy_put_afinfo(afinfo
);
121 static inline unsigned long make_jiffies(long secs
)
123 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
124 return MAX_SCHEDULE_TIMEOUT
-1;
129 static void xfrm_policy_timer(unsigned long data
)
131 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
132 unsigned long now
= get_seconds();
133 long next
= LONG_MAX
;
137 read_lock(&xp
->lock
);
142 dir
= xfrm_policy_id2dir(xp
->index
);
144 if (xp
->lft
.hard_add_expires_seconds
) {
145 long tmo
= xp
->lft
.hard_add_expires_seconds
+
146 xp
->curlft
.add_time
- now
;
152 if (xp
->lft
.hard_use_expires_seconds
) {
153 long tmo
= xp
->lft
.hard_use_expires_seconds
+
154 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
160 if (xp
->lft
.soft_add_expires_seconds
) {
161 long tmo
= xp
->lft
.soft_add_expires_seconds
+
162 xp
->curlft
.add_time
- now
;
165 tmo
= XFRM_KM_TIMEOUT
;
170 if (xp
->lft
.soft_use_expires_seconds
) {
171 long tmo
= xp
->lft
.soft_use_expires_seconds
+
172 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
175 tmo
= XFRM_KM_TIMEOUT
;
182 km_policy_expired(xp
, dir
, 0, 0);
183 if (next
!= LONG_MAX
&&
184 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
188 read_unlock(&xp
->lock
);
193 read_unlock(&xp
->lock
);
194 if (!xfrm_policy_delete(xp
, dir
))
195 km_policy_expired(xp
, dir
, 1, 0);
200 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
204 struct xfrm_policy
*xfrm_policy_alloc(gfp_t gfp
)
206 struct xfrm_policy
*policy
;
208 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
211 INIT_HLIST_NODE(&policy
->bydst
);
212 INIT_HLIST_NODE(&policy
->byidx
);
213 rwlock_init(&policy
->lock
);
214 atomic_set(&policy
->refcnt
, 1);
215 setup_timer(&policy
->timer
, xfrm_policy_timer
,
216 (unsigned long)policy
);
220 EXPORT_SYMBOL(xfrm_policy_alloc
);
222 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
224 void xfrm_policy_destroy(struct xfrm_policy
*policy
)
226 BUG_ON(!policy
->dead
);
228 BUG_ON(policy
->bundles
);
230 if (del_timer(&policy
->timer
))
233 security_xfrm_policy_free(policy
);
236 EXPORT_SYMBOL(xfrm_policy_destroy
);
238 static void xfrm_policy_gc_kill(struct xfrm_policy
*policy
)
240 struct dst_entry
*dst
;
242 while ((dst
= policy
->bundles
) != NULL
) {
243 policy
->bundles
= dst
->next
;
247 if (del_timer(&policy
->timer
))
248 atomic_dec(&policy
->refcnt
);
250 if (atomic_read(&policy
->refcnt
) > 1)
253 xfrm_pol_put(policy
);
256 static void xfrm_policy_gc_task(struct work_struct
*work
)
258 struct xfrm_policy
*policy
;
259 struct hlist_node
*entry
, *tmp
;
260 struct hlist_head gc_list
;
262 spin_lock_bh(&xfrm_policy_gc_lock
);
263 gc_list
.first
= xfrm_policy_gc_list
.first
;
264 INIT_HLIST_HEAD(&xfrm_policy_gc_list
);
265 spin_unlock_bh(&xfrm_policy_gc_lock
);
267 hlist_for_each_entry_safe(policy
, entry
, tmp
, &gc_list
, bydst
)
268 xfrm_policy_gc_kill(policy
);
271 /* Rule must be locked. Release descentant resources, announce
272 * entry dead. The rule must be unlinked from lists to the moment.
275 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
279 write_lock_bh(&policy
->lock
);
282 write_unlock_bh(&policy
->lock
);
284 if (unlikely(dead
)) {
289 spin_lock(&xfrm_policy_gc_lock
);
290 hlist_add_head(&policy
->bydst
, &xfrm_policy_gc_list
);
291 spin_unlock(&xfrm_policy_gc_lock
);
293 schedule_work(&xfrm_policy_gc_work
);
296 struct xfrm_policy_hash
{
297 struct hlist_head
*table
;
301 static struct hlist_head xfrm_policy_inexact
[XFRM_POLICY_MAX
*2];
302 static struct xfrm_policy_hash xfrm_policy_bydst
[XFRM_POLICY_MAX
*2] __read_mostly
;
303 static struct hlist_head
*xfrm_policy_byidx __read_mostly
;
304 static unsigned int xfrm_idx_hmask __read_mostly
;
305 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
307 static inline unsigned int idx_hash(u32 index
)
309 return __idx_hash(index
, xfrm_idx_hmask
);
312 static struct hlist_head
*policy_hash_bysel(struct xfrm_selector
*sel
, unsigned short family
, int dir
)
314 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
315 unsigned int hash
= __sel_hash(sel
, family
, hmask
);
317 return (hash
== hmask
+ 1 ?
318 &xfrm_policy_inexact
[dir
] :
319 xfrm_policy_bydst
[dir
].table
+ hash
);
322 static struct hlist_head
*policy_hash_direct(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
, unsigned short family
, int dir
)
324 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
325 unsigned int hash
= __addr_hash(daddr
, saddr
, family
, hmask
);
327 return xfrm_policy_bydst
[dir
].table
+ hash
;
330 static void xfrm_dst_hash_transfer(struct hlist_head
*list
,
331 struct hlist_head
*ndsttable
,
332 unsigned int nhashmask
)
334 struct hlist_node
*entry
, *tmp
, *entry0
= NULL
;
335 struct xfrm_policy
*pol
;
339 hlist_for_each_entry_safe(pol
, entry
, tmp
, list
, bydst
) {
342 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
343 pol
->family
, nhashmask
);
346 hlist_add_head(&pol
->bydst
, ndsttable
+h
);
352 hlist_add_after(entry0
, &pol
->bydst
);
356 if (!hlist_empty(list
)) {
362 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
363 struct hlist_head
*nidxtable
,
364 unsigned int nhashmask
)
366 struct hlist_node
*entry
, *tmp
;
367 struct xfrm_policy
*pol
;
369 hlist_for_each_entry_safe(pol
, entry
, tmp
, list
, byidx
) {
372 h
= __idx_hash(pol
->index
, nhashmask
);
373 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
377 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
379 return ((old_hmask
+ 1) << 1) - 1;
382 static void xfrm_bydst_resize(int dir
)
384 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
385 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
386 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
387 struct hlist_head
*odst
= xfrm_policy_bydst
[dir
].table
;
388 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
394 write_lock_bh(&xfrm_policy_lock
);
396 for (i
= hmask
; i
>= 0; i
--)
397 xfrm_dst_hash_transfer(odst
+ i
, ndst
, nhashmask
);
399 xfrm_policy_bydst
[dir
].table
= ndst
;
400 xfrm_policy_bydst
[dir
].hmask
= nhashmask
;
402 write_unlock_bh(&xfrm_policy_lock
);
404 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
407 static void xfrm_byidx_resize(int total
)
409 unsigned int hmask
= xfrm_idx_hmask
;
410 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
411 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
412 struct hlist_head
*oidx
= xfrm_policy_byidx
;
413 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
419 write_lock_bh(&xfrm_policy_lock
);
421 for (i
= hmask
; i
>= 0; i
--)
422 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
424 xfrm_policy_byidx
= nidx
;
425 xfrm_idx_hmask
= nhashmask
;
427 write_unlock_bh(&xfrm_policy_lock
);
429 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
432 static inline int xfrm_bydst_should_resize(int dir
, int *total
)
434 unsigned int cnt
= xfrm_policy_count
[dir
];
435 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
440 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
447 static inline int xfrm_byidx_should_resize(int total
)
449 unsigned int hmask
= xfrm_idx_hmask
;
451 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
458 void xfrm_spd_getinfo(struct xfrmk_spdinfo
*si
)
460 read_lock_bh(&xfrm_policy_lock
);
461 si
->incnt
= xfrm_policy_count
[XFRM_POLICY_IN
];
462 si
->outcnt
= xfrm_policy_count
[XFRM_POLICY_OUT
];
463 si
->fwdcnt
= xfrm_policy_count
[XFRM_POLICY_FWD
];
464 si
->inscnt
= xfrm_policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
465 si
->outscnt
= xfrm_policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
466 si
->fwdscnt
= xfrm_policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
467 si
->spdhcnt
= xfrm_idx_hmask
;
468 si
->spdhmcnt
= xfrm_policy_hashmax
;
469 read_unlock_bh(&xfrm_policy_lock
);
471 EXPORT_SYMBOL(xfrm_spd_getinfo
);
473 static DEFINE_MUTEX(hash_resize_mutex
);
474 static void xfrm_hash_resize(struct work_struct
*__unused
)
478 mutex_lock(&hash_resize_mutex
);
481 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
482 if (xfrm_bydst_should_resize(dir
, &total
))
483 xfrm_bydst_resize(dir
);
485 if (xfrm_byidx_should_resize(total
))
486 xfrm_byidx_resize(total
);
488 mutex_unlock(&hash_resize_mutex
);
491 static DECLARE_WORK(xfrm_hash_work
, xfrm_hash_resize
);
493 /* Generate new index... KAME seems to generate them ordered by cost
494 * of an absolute inpredictability of ordering of rules. This will not pass. */
495 static u32
xfrm_gen_index(u8 type
, int dir
)
497 static u32 idx_generator
;
500 struct hlist_node
*entry
;
501 struct hlist_head
*list
;
502 struct xfrm_policy
*p
;
506 idx
= (idx_generator
| dir
);
510 list
= xfrm_policy_byidx
+ idx_hash(idx
);
512 hlist_for_each_entry(p
, entry
, list
, byidx
) {
513 if (p
->index
== idx
) {
523 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
525 u32
*p1
= (u32
*) s1
;
526 u32
*p2
= (u32
*) s2
;
527 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
530 for (i
= 0; i
< len
; i
++) {
538 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
540 struct xfrm_policy
*pol
;
541 struct xfrm_policy
*delpol
;
542 struct hlist_head
*chain
;
543 struct hlist_node
*entry
, *newpos
;
544 struct dst_entry
*gc_list
;
546 write_lock_bh(&xfrm_policy_lock
);
547 chain
= policy_hash_bysel(&policy
->selector
, policy
->family
, dir
);
550 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
551 if (pol
->type
== policy
->type
&&
552 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
553 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
556 write_unlock_bh(&xfrm_policy_lock
);
560 if (policy
->priority
> pol
->priority
)
562 } else if (policy
->priority
>= pol
->priority
) {
563 newpos
= &pol
->bydst
;
570 hlist_add_after(newpos
, &policy
->bydst
);
572 hlist_add_head(&policy
->bydst
, chain
);
573 xfrm_pol_hold(policy
);
574 xfrm_policy_count
[dir
]++;
575 atomic_inc(&flow_cache_genid
);
577 hlist_del(&delpol
->bydst
);
578 hlist_del(&delpol
->byidx
);
579 xfrm_policy_count
[dir
]--;
581 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(policy
->type
, dir
);
582 hlist_add_head(&policy
->byidx
, xfrm_policy_byidx
+idx_hash(policy
->index
));
583 policy
->curlft
.add_time
= get_seconds();
584 policy
->curlft
.use_time
= 0;
585 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
586 xfrm_pol_hold(policy
);
587 write_unlock_bh(&xfrm_policy_lock
);
590 xfrm_policy_kill(delpol
);
591 else if (xfrm_bydst_should_resize(dir
, NULL
))
592 schedule_work(&xfrm_hash_work
);
594 read_lock_bh(&xfrm_policy_lock
);
596 entry
= &policy
->bydst
;
597 hlist_for_each_entry_continue(policy
, entry
, bydst
) {
598 struct dst_entry
*dst
;
600 write_lock(&policy
->lock
);
601 dst
= policy
->bundles
;
603 struct dst_entry
*tail
= dst
;
606 tail
->next
= gc_list
;
609 policy
->bundles
= NULL
;
611 write_unlock(&policy
->lock
);
613 read_unlock_bh(&xfrm_policy_lock
);
616 struct dst_entry
*dst
= gc_list
;
624 EXPORT_SYMBOL(xfrm_policy_insert
);
626 struct xfrm_policy
*xfrm_policy_bysel_ctx(u8 type
, int dir
,
627 struct xfrm_selector
*sel
,
628 struct xfrm_sec_ctx
*ctx
, int delete,
631 struct xfrm_policy
*pol
, *ret
;
632 struct hlist_head
*chain
;
633 struct hlist_node
*entry
;
636 write_lock_bh(&xfrm_policy_lock
);
637 chain
= policy_hash_bysel(sel
, sel
->family
, dir
);
639 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
640 if (pol
->type
== type
&&
641 !selector_cmp(sel
, &pol
->selector
) &&
642 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
645 *err
= security_xfrm_policy_delete(pol
);
647 write_unlock_bh(&xfrm_policy_lock
);
650 hlist_del(&pol
->bydst
);
651 hlist_del(&pol
->byidx
);
652 xfrm_policy_count
[dir
]--;
658 write_unlock_bh(&xfrm_policy_lock
);
661 atomic_inc(&flow_cache_genid
);
662 xfrm_policy_kill(ret
);
666 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
668 struct xfrm_policy
*xfrm_policy_byid(u8 type
, int dir
, u32 id
, int delete,
671 struct xfrm_policy
*pol
, *ret
;
672 struct hlist_head
*chain
;
673 struct hlist_node
*entry
;
676 if (xfrm_policy_id2dir(id
) != dir
)
680 write_lock_bh(&xfrm_policy_lock
);
681 chain
= xfrm_policy_byidx
+ idx_hash(id
);
683 hlist_for_each_entry(pol
, entry
, chain
, byidx
) {
684 if (pol
->type
== type
&& pol
->index
== id
) {
687 *err
= security_xfrm_policy_delete(pol
);
689 write_unlock_bh(&xfrm_policy_lock
);
692 hlist_del(&pol
->bydst
);
693 hlist_del(&pol
->byidx
);
694 xfrm_policy_count
[dir
]--;
700 write_unlock_bh(&xfrm_policy_lock
);
703 atomic_inc(&flow_cache_genid
);
704 xfrm_policy_kill(ret
);
708 EXPORT_SYMBOL(xfrm_policy_byid
);
710 #ifdef CONFIG_SECURITY_NETWORK_XFRM
712 xfrm_policy_flush_secctx_check(u8 type
, struct xfrm_audit
*audit_info
)
716 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
717 struct xfrm_policy
*pol
;
718 struct hlist_node
*entry
;
721 hlist_for_each_entry(pol
, entry
,
722 &xfrm_policy_inexact
[dir
], bydst
) {
723 if (pol
->type
!= type
)
725 err
= security_xfrm_policy_delete(pol
);
727 xfrm_audit_policy_delete(pol
, 0,
728 audit_info
->loginuid
,
733 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
734 hlist_for_each_entry(pol
, entry
,
735 xfrm_policy_bydst
[dir
].table
+ i
,
737 if (pol
->type
!= type
)
739 err
= security_xfrm_policy_delete(pol
);
741 xfrm_audit_policy_delete(pol
, 0,
742 audit_info
->loginuid
,
753 xfrm_policy_flush_secctx_check(u8 type
, struct xfrm_audit
*audit_info
)
759 int xfrm_policy_flush(u8 type
, struct xfrm_audit
*audit_info
)
763 write_lock_bh(&xfrm_policy_lock
);
765 err
= xfrm_policy_flush_secctx_check(type
, audit_info
);
769 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
770 struct xfrm_policy
*pol
;
771 struct hlist_node
*entry
;
776 hlist_for_each_entry(pol
, entry
,
777 &xfrm_policy_inexact
[dir
], bydst
) {
778 if (pol
->type
!= type
)
780 hlist_del(&pol
->bydst
);
781 hlist_del(&pol
->byidx
);
782 write_unlock_bh(&xfrm_policy_lock
);
784 xfrm_audit_policy_delete(pol
, 1, audit_info
->loginuid
,
787 xfrm_policy_kill(pol
);
790 write_lock_bh(&xfrm_policy_lock
);
794 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
796 hlist_for_each_entry(pol
, entry
,
797 xfrm_policy_bydst
[dir
].table
+ i
,
799 if (pol
->type
!= type
)
801 hlist_del(&pol
->bydst
);
802 hlist_del(&pol
->byidx
);
803 write_unlock_bh(&xfrm_policy_lock
);
805 xfrm_audit_policy_delete(pol
, 1,
806 audit_info
->loginuid
,
808 xfrm_policy_kill(pol
);
811 write_lock_bh(&xfrm_policy_lock
);
816 xfrm_policy_count
[dir
] -= killed
;
818 atomic_inc(&flow_cache_genid
);
820 write_unlock_bh(&xfrm_policy_lock
);
823 EXPORT_SYMBOL(xfrm_policy_flush
);
825 int xfrm_policy_walk(u8 type
, int (*func
)(struct xfrm_policy
*, int, int, void*),
828 struct xfrm_policy
*pol
, *last
= NULL
;
829 struct hlist_node
*entry
;
830 int dir
, last_dir
= 0, count
, error
;
832 read_lock_bh(&xfrm_policy_lock
);
835 for (dir
= 0; dir
< 2*XFRM_POLICY_MAX
; dir
++) {
836 struct hlist_head
*table
= xfrm_policy_bydst
[dir
].table
;
839 hlist_for_each_entry(pol
, entry
,
840 &xfrm_policy_inexact
[dir
], bydst
) {
841 if (pol
->type
!= type
)
844 error
= func(last
, last_dir
% XFRM_POLICY_MAX
,
853 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
854 hlist_for_each_entry(pol
, entry
, table
+ i
, bydst
) {
855 if (pol
->type
!= type
)
858 error
= func(last
, last_dir
% XFRM_POLICY_MAX
,
873 error
= func(last
, last_dir
% XFRM_POLICY_MAX
, 0, data
);
875 read_unlock_bh(&xfrm_policy_lock
);
878 EXPORT_SYMBOL(xfrm_policy_walk
);
881 * Find policy to apply to this flow.
883 * Returns 0 if policy found, else an -errno.
885 static int xfrm_policy_match(struct xfrm_policy
*pol
, struct flowi
*fl
,
886 u8 type
, u16 family
, int dir
)
888 struct xfrm_selector
*sel
= &pol
->selector
;
889 int match
, ret
= -ESRCH
;
891 if (pol
->family
!= family
||
895 match
= xfrm_selector_match(sel
, fl
, family
);
897 ret
= security_xfrm_policy_lookup(pol
, fl
->secid
, dir
);
902 static struct xfrm_policy
*xfrm_policy_lookup_bytype(u8 type
, struct flowi
*fl
,
906 struct xfrm_policy
*pol
, *ret
;
907 xfrm_address_t
*daddr
, *saddr
;
908 struct hlist_node
*entry
;
909 struct hlist_head
*chain
;
912 daddr
= xfrm_flowi_daddr(fl
, family
);
913 saddr
= xfrm_flowi_saddr(fl
, family
);
914 if (unlikely(!daddr
|| !saddr
))
917 read_lock_bh(&xfrm_policy_lock
);
918 chain
= policy_hash_direct(daddr
, saddr
, family
, dir
);
920 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
921 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
931 priority
= ret
->priority
;
935 chain
= &xfrm_policy_inexact
[dir
];
936 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
937 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
945 } else if (pol
->priority
< priority
) {
953 read_unlock_bh(&xfrm_policy_lock
);
958 static int xfrm_policy_lookup(struct flowi
*fl
, u16 family
, u8 dir
,
959 void **objp
, atomic_t
**obj_refp
)
961 struct xfrm_policy
*pol
;
964 #ifdef CONFIG_XFRM_SUB_POLICY
965 pol
= xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
973 pol
= xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
978 #ifdef CONFIG_XFRM_SUB_POLICY
981 if ((*objp
= (void *) pol
) != NULL
)
982 *obj_refp
= &pol
->refcnt
;
986 static inline int policy_to_flow_dir(int dir
)
988 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
989 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
990 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
996 case XFRM_POLICY_OUT
:
998 case XFRM_POLICY_FWD
:
1003 static struct xfrm_policy
*xfrm_sk_policy_lookup(struct sock
*sk
, int dir
, struct flowi
*fl
)
1005 struct xfrm_policy
*pol
;
1007 read_lock_bh(&xfrm_policy_lock
);
1008 if ((pol
= sk
->sk_policy
[dir
]) != NULL
) {
1009 int match
= xfrm_selector_match(&pol
->selector
, fl
,
1014 err
= security_xfrm_policy_lookup(pol
, fl
->secid
,
1015 policy_to_flow_dir(dir
));
1018 else if (err
== -ESRCH
)
1025 read_unlock_bh(&xfrm_policy_lock
);
1029 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1031 struct hlist_head
*chain
= policy_hash_bysel(&pol
->selector
,
1034 hlist_add_head(&pol
->bydst
, chain
);
1035 hlist_add_head(&pol
->byidx
, xfrm_policy_byidx
+idx_hash(pol
->index
));
1036 xfrm_policy_count
[dir
]++;
1039 if (xfrm_bydst_should_resize(dir
, NULL
))
1040 schedule_work(&xfrm_hash_work
);
1043 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1046 if (hlist_unhashed(&pol
->bydst
))
1049 hlist_del(&pol
->bydst
);
1050 hlist_del(&pol
->byidx
);
1051 xfrm_policy_count
[dir
]--;
1056 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1058 write_lock_bh(&xfrm_policy_lock
);
1059 pol
= __xfrm_policy_unlink(pol
, dir
);
1060 write_unlock_bh(&xfrm_policy_lock
);
1062 if (dir
< XFRM_POLICY_MAX
)
1063 atomic_inc(&flow_cache_genid
);
1064 xfrm_policy_kill(pol
);
1069 EXPORT_SYMBOL(xfrm_policy_delete
);
1071 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1073 struct xfrm_policy
*old_pol
;
1075 #ifdef CONFIG_XFRM_SUB_POLICY
1076 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1080 write_lock_bh(&xfrm_policy_lock
);
1081 old_pol
= sk
->sk_policy
[dir
];
1082 sk
->sk_policy
[dir
] = pol
;
1084 pol
->curlft
.add_time
= get_seconds();
1085 pol
->index
= xfrm_gen_index(pol
->type
, XFRM_POLICY_MAX
+dir
);
1086 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+dir
);
1089 __xfrm_policy_unlink(old_pol
, XFRM_POLICY_MAX
+dir
);
1090 write_unlock_bh(&xfrm_policy_lock
);
1093 xfrm_policy_kill(old_pol
);
1098 static struct xfrm_policy
*clone_policy(struct xfrm_policy
*old
, int dir
)
1100 struct xfrm_policy
*newp
= xfrm_policy_alloc(GFP_ATOMIC
);
1103 newp
->selector
= old
->selector
;
1104 if (security_xfrm_policy_clone(old
, newp
)) {
1106 return NULL
; /* ENOMEM */
1108 newp
->lft
= old
->lft
;
1109 newp
->curlft
= old
->curlft
;
1110 newp
->action
= old
->action
;
1111 newp
->flags
= old
->flags
;
1112 newp
->xfrm_nr
= old
->xfrm_nr
;
1113 newp
->index
= old
->index
;
1114 newp
->type
= old
->type
;
1115 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1116 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1117 write_lock_bh(&xfrm_policy_lock
);
1118 __xfrm_policy_link(newp
, XFRM_POLICY_MAX
+dir
);
1119 write_unlock_bh(&xfrm_policy_lock
);
1125 int __xfrm_sk_clone_policy(struct sock
*sk
)
1127 struct xfrm_policy
*p0
= sk
->sk_policy
[0],
1128 *p1
= sk
->sk_policy
[1];
1130 sk
->sk_policy
[0] = sk
->sk_policy
[1] = NULL
;
1131 if (p0
&& (sk
->sk_policy
[0] = clone_policy(p0
, 0)) == NULL
)
1133 if (p1
&& (sk
->sk_policy
[1] = clone_policy(p1
, 1)) == NULL
)
1139 xfrm_get_saddr(xfrm_address_t
*local
, xfrm_address_t
*remote
,
1140 unsigned short family
)
1143 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1145 if (unlikely(afinfo
== NULL
))
1147 err
= afinfo
->get_saddr(local
, remote
);
1148 xfrm_policy_put_afinfo(afinfo
);
1152 /* Resolve list of templates for the flow, given policy. */
1155 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, struct flowi
*fl
,
1156 struct xfrm_state
**xfrm
,
1157 unsigned short family
)
1161 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1162 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1165 for (nx
=0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1166 struct xfrm_state
*x
;
1167 xfrm_address_t
*remote
= daddr
;
1168 xfrm_address_t
*local
= saddr
;
1169 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1171 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1172 tmpl
->mode
== XFRM_MODE_BEET
) {
1173 remote
= &tmpl
->id
.daddr
;
1174 local
= &tmpl
->saddr
;
1175 family
= tmpl
->encap_family
;
1176 if (xfrm_addr_any(local
, family
)) {
1177 error
= xfrm_get_saddr(&tmp
, remote
, family
);
1184 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1186 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1193 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1198 if (!tmpl
->optional
)
1204 for (nx
--; nx
>=0; nx
--)
1205 xfrm_state_put(xfrm
[nx
]);
1210 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, struct flowi
*fl
,
1211 struct xfrm_state
**xfrm
,
1212 unsigned short family
)
1214 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1215 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1221 for (i
= 0; i
< npols
; i
++) {
1222 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1227 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1235 /* found states are sorted for outbound processing */
1237 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1242 for (cnx
--; cnx
>=0; cnx
--)
1243 xfrm_state_put(tpp
[cnx
]);
1248 /* Check that the bundle accepts the flow and its components are
1252 static struct dst_entry
*
1253 xfrm_find_bundle(struct flowi
*fl
, struct xfrm_policy
*policy
, unsigned short family
)
1255 struct dst_entry
*x
;
1256 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1257 if (unlikely(afinfo
== NULL
))
1258 return ERR_PTR(-EINVAL
);
1259 x
= afinfo
->find_bundle(fl
, policy
);
1260 xfrm_policy_put_afinfo(afinfo
);
1264 static inline int xfrm_get_tos(struct flowi
*fl
, int family
)
1266 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1272 tos
= afinfo
->get_tos(fl
);
1274 xfrm_policy_put_afinfo(afinfo
);
1279 static inline struct xfrm_dst
*xfrm_alloc_dst(int family
)
1281 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1282 struct xfrm_dst
*xdst
;
1285 return ERR_PTR(-EINVAL
);
1287 xdst
= dst_alloc(afinfo
->dst_ops
) ?: ERR_PTR(-ENOBUFS
);
1289 xfrm_policy_put_afinfo(afinfo
);
1294 static inline int xfrm_init_path(struct xfrm_dst
*path
, struct dst_entry
*dst
,
1297 struct xfrm_policy_afinfo
*afinfo
=
1298 xfrm_policy_get_afinfo(dst
->ops
->family
);
1304 err
= afinfo
->init_path(path
, dst
, nfheader_len
);
1306 xfrm_policy_put_afinfo(afinfo
);
1311 static inline int xfrm_fill_dst(struct xfrm_dst
*xdst
, struct net_device
*dev
)
1313 struct xfrm_policy_afinfo
*afinfo
=
1314 xfrm_policy_get_afinfo(xdst
->u
.dst
.ops
->family
);
1320 err
= afinfo
->fill_dst(xdst
, dev
);
1322 xfrm_policy_put_afinfo(afinfo
);
1327 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1328 * all the metrics... Shortly, bundle a bundle.
1331 static struct dst_entry
*xfrm_bundle_create(struct xfrm_policy
*policy
,
1332 struct xfrm_state
**xfrm
, int nx
,
1334 struct dst_entry
*dst
)
1336 unsigned long now
= jiffies
;
1337 struct net_device
*dev
;
1338 struct dst_entry
*dst_prev
= NULL
;
1339 struct dst_entry
*dst0
= NULL
;
1343 int nfheader_len
= 0;
1344 int trailer_len
= 0;
1346 int family
= policy
->selector
.family
;
1348 tos
= xfrm_get_tos(fl
, family
);
1355 for (; i
< nx
; i
++) {
1356 struct xfrm_dst
*xdst
= xfrm_alloc_dst(family
);
1357 struct dst_entry
*dst1
= &xdst
->u
.dst
;
1359 err
= PTR_ERR(xdst
);
1368 dst_prev
->child
= dst_clone(dst1
);
1369 dst1
->flags
|= DST_NOHASH
;
1373 memcpy(&dst1
->metrics
, &dst
->metrics
, sizeof(dst
->metrics
));
1375 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1376 family
= xfrm
[i
]->props
.family
;
1377 dst
= xfrm_dst_lookup(xfrm
[i
], tos
, family
);
1384 dst1
->xfrm
= xfrm
[i
];
1385 xdst
->genid
= xfrm
[i
]->genid
;
1387 dst1
->obsolete
= -1;
1388 dst1
->flags
|= DST_HOST
;
1389 dst1
->lastuse
= now
;
1391 dst1
->input
= dst_discard
;
1392 dst1
->output
= xfrm
[i
]->outer_mode
->afinfo
->output
;
1394 dst1
->next
= dst_prev
;
1397 header_len
+= xfrm
[i
]->props
.header_len
;
1398 if (xfrm
[i
]->type
->flags
& XFRM_TYPE_NON_FRAGMENT
)
1399 nfheader_len
+= xfrm
[i
]->props
.header_len
;
1400 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
1403 dst_prev
->child
= dst
;
1411 /* Copy neighbout for reachability confirmation */
1412 dst0
->neighbour
= neigh_clone(dst
->neighbour
);
1414 xfrm_init_path((struct xfrm_dst
*)dst0
, dst
, nfheader_len
);
1415 xfrm_init_pmtu(dst_prev
);
1417 for (dst_prev
= dst0
; dst_prev
!= dst
; dst_prev
= dst_prev
->child
) {
1418 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst_prev
;
1420 err
= xfrm_fill_dst(xdst
, dev
);
1424 dst_prev
->header_len
= header_len
;
1425 dst_prev
->trailer_len
= trailer_len
;
1426 header_len
-= xdst
->u
.dst
.xfrm
->props
.header_len
;
1427 trailer_len
-= xdst
->u
.dst
.xfrm
->props
.trailer_len
;
1435 xfrm_state_put(xfrm
[i
]);
1439 dst0
= ERR_PTR(err
);
1444 xfrm_dst_alloc_copy(void **target
, void *src
, int size
)
1447 *target
= kmalloc(size
, GFP_ATOMIC
);
1451 memcpy(*target
, src
, size
);
1456 xfrm_dst_update_parent(struct dst_entry
*dst
, struct xfrm_selector
*sel
)
1458 #ifdef CONFIG_XFRM_SUB_POLICY
1459 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1460 return xfrm_dst_alloc_copy((void **)&(xdst
->partner
),
1468 xfrm_dst_update_origin(struct dst_entry
*dst
, struct flowi
*fl
)
1470 #ifdef CONFIG_XFRM_SUB_POLICY
1471 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1472 return xfrm_dst_alloc_copy((void **)&(xdst
->origin
), fl
, sizeof(*fl
));
1478 static int stale_bundle(struct dst_entry
*dst
);
1480 /* Main function: finds/creates a bundle for given flow.
1482 * At the moment we eat a raw IP route. Mostly to speed up lookups
1483 * on interfaces with disabled IPsec.
1485 int __xfrm_lookup(struct dst_entry
**dst_p
, struct flowi
*fl
,
1486 struct sock
*sk
, int flags
)
1488 struct xfrm_policy
*policy
;
1489 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1494 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1495 struct dst_entry
*dst
, *dst_orig
= *dst_p
;
1500 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
1503 genid
= atomic_read(&flow_cache_genid
);
1505 for (pi
= 0; pi
< ARRAY_SIZE(pols
); pi
++)
1511 if (sk
&& sk
->sk_policy
[XFRM_POLICY_OUT
]) {
1512 policy
= xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
);
1513 err
= PTR_ERR(policy
);
1514 if (IS_ERR(policy
)) {
1515 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR
);
1521 /* To accelerate a bit... */
1522 if ((dst_orig
->flags
& DST_NOXFRM
) ||
1523 !xfrm_policy_count
[XFRM_POLICY_OUT
])
1526 policy
= flow_cache_lookup(fl
, dst_orig
->ops
->family
,
1527 dir
, xfrm_policy_lookup
);
1528 err
= PTR_ERR(policy
);
1529 if (IS_ERR(policy
)) {
1530 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR
);
1538 family
= dst_orig
->ops
->family
;
1541 xfrm_nr
+= pols
[0]->xfrm_nr
;
1544 if ((flags
& XFRM_LOOKUP_ICMP
) && !(policy
->flags
& XFRM_POLICY_ICMP
))
1547 policy
->curlft
.use_time
= get_seconds();
1549 switch (policy
->action
) {
1551 case XFRM_POLICY_BLOCK
:
1552 /* Prohibit the flow */
1553 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLBLOCK
);
1557 case XFRM_POLICY_ALLOW
:
1558 #ifndef CONFIG_XFRM_SUB_POLICY
1559 if (policy
->xfrm_nr
== 0) {
1560 /* Flow passes not transformed. */
1561 xfrm_pol_put(policy
);
1566 /* Try to find matching bundle.
1568 * LATER: help from flow cache. It is optional, this
1569 * is required only for output policy.
1571 dst
= xfrm_find_bundle(fl
, policy
, family
);
1573 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR
);
1581 #ifdef CONFIG_XFRM_SUB_POLICY
1582 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1583 pols
[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
,
1587 if (IS_ERR(pols
[1])) {
1588 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR
);
1589 err
= PTR_ERR(pols
[1]);
1592 if (pols
[1]->action
== XFRM_POLICY_BLOCK
) {
1593 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLBLOCK
);
1598 xfrm_nr
+= pols
[1]->xfrm_nr
;
1603 * Because neither flowi nor bundle information knows about
1604 * transformation template size. On more than one policy usage
1605 * we can realize whether all of them is bypass or not after
1606 * they are searched. See above not-transformed bypass
1607 * is surrounded by non-sub policy configuration, too.
1610 /* Flow passes not transformed. */
1611 xfrm_pols_put(pols
, npols
);
1616 nx
= xfrm_tmpl_resolve(pols
, npols
, fl
, xfrm
, family
);
1618 if (unlikely(nx
<0)) {
1620 if (err
== -EAGAIN
&& sysctl_xfrm_larval_drop
) {
1621 /* EREMOTE tells the caller to generate
1622 * a one-shot blackhole route.
1624 XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES
);
1625 xfrm_pol_put(policy
);
1628 if (err
== -EAGAIN
&& (flags
& XFRM_LOOKUP_WAIT
)) {
1629 DECLARE_WAITQUEUE(wait
, current
);
1631 add_wait_queue(&km_waitq
, &wait
);
1632 set_current_state(TASK_INTERRUPTIBLE
);
1634 set_current_state(TASK_RUNNING
);
1635 remove_wait_queue(&km_waitq
, &wait
);
1637 nx
= xfrm_tmpl_resolve(pols
, npols
, fl
, xfrm
, family
);
1639 if (nx
== -EAGAIN
&& signal_pending(current
)) {
1640 XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES
);
1644 if (nx
== -EAGAIN
||
1645 genid
!= atomic_read(&flow_cache_genid
)) {
1646 xfrm_pols_put(pols
, npols
);
1652 XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES
);
1657 /* Flow passes not transformed. */
1658 xfrm_pols_put(pols
, npols
);
1662 dst
= xfrm_bundle_create(policy
, xfrm
, nx
, fl
, dst_orig
);
1665 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLEGENERROR
);
1669 for (pi
= 0; pi
< npols
; pi
++) {
1670 read_lock_bh(&pols
[pi
]->lock
);
1671 pol_dead
|= pols
[pi
]->dead
;
1672 read_unlock_bh(&pols
[pi
]->lock
);
1675 write_lock_bh(&policy
->lock
);
1676 if (unlikely(pol_dead
|| stale_bundle(dst
))) {
1677 /* Wow! While we worked on resolving, this
1678 * policy has gone. Retry. It is not paranoia,
1679 * we just cannot enlist new bundle to dead object.
1680 * We can't enlist stable bundles either.
1682 write_unlock_bh(&policy
->lock
);
1687 XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLDEAD
);
1689 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR
);
1690 err
= -EHOSTUNREACH
;
1695 err
= xfrm_dst_update_parent(dst
, &pols
[1]->selector
);
1697 err
= xfrm_dst_update_origin(dst
, fl
);
1698 if (unlikely(err
)) {
1699 write_unlock_bh(&policy
->lock
);
1702 XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR
);
1706 dst
->next
= policy
->bundles
;
1707 policy
->bundles
= dst
;
1709 write_unlock_bh(&policy
->lock
);
1712 dst_release(dst_orig
);
1713 xfrm_pols_put(pols
, npols
);
1717 xfrm_pols_put(pols
, npols
);
1719 dst_release(dst_orig
);
1725 if (flags
& XFRM_LOOKUP_ICMP
)
1729 EXPORT_SYMBOL(__xfrm_lookup
);
1731 int xfrm_lookup(struct dst_entry
**dst_p
, struct flowi
*fl
,
1732 struct sock
*sk
, int flags
)
1734 int err
= __xfrm_lookup(dst_p
, fl
, sk
, flags
);
1736 if (err
== -EREMOTE
) {
1737 dst_release(*dst_p
);
1744 EXPORT_SYMBOL(xfrm_lookup
);
1747 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, struct flowi
*fl
)
1749 struct xfrm_state
*x
;
1751 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
1753 x
= skb
->sp
->xvec
[idx
];
1754 if (!x
->type
->reject
)
1756 return x
->type
->reject(x
, skb
, fl
);
1759 /* When skb is transformed back to its "native" form, we have to
1760 * check policy restrictions. At the moment we make this in maximally
1761 * stupid way. Shame on me. :-) Of course, connected sockets must
1762 * have policy cached at them.
1766 xfrm_state_ok(struct xfrm_tmpl
*tmpl
, struct xfrm_state
*x
,
1767 unsigned short family
)
1769 if (xfrm_state_kern(x
))
1770 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
1771 return x
->id
.proto
== tmpl
->id
.proto
&&
1772 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
1773 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
1774 x
->props
.mode
== tmpl
->mode
&&
1775 ((tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
1776 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
1777 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
1778 xfrm_state_addr_cmp(tmpl
, x
, family
));
1782 * 0 or more than 0 is returned when validation is succeeded (either bypass
1783 * because of optional transport mode, or next index of the mathced secpath
1784 * state with the template.
1785 * -1 is returned when no matching template is found.
1786 * Otherwise "-2 - errored_index" is returned.
1789 xfrm_policy_ok(struct xfrm_tmpl
*tmpl
, struct sec_path
*sp
, int start
,
1790 unsigned short family
)
1794 if (tmpl
->optional
) {
1795 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
1799 for (; idx
< sp
->len
; idx
++) {
1800 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
1802 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1811 int __xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
,
1812 unsigned int family
, int reverse
)
1814 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1817 if (unlikely(afinfo
== NULL
))
1818 return -EAFNOSUPPORT
;
1820 afinfo
->decode_session(skb
, fl
, reverse
);
1821 err
= security_xfrm_decode_session(skb
, &fl
->secid
);
1822 xfrm_policy_put_afinfo(afinfo
);
1825 EXPORT_SYMBOL(__xfrm_decode_session
);
1827 static inline int secpath_has_nontransport(struct sec_path
*sp
, int k
, int *idxp
)
1829 for (; k
< sp
->len
; k
++) {
1830 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1839 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
1840 unsigned short family
)
1842 struct xfrm_policy
*pol
;
1843 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1852 reverse
= dir
& ~XFRM_POLICY_MASK
;
1853 dir
&= XFRM_POLICY_MASK
;
1854 fl_dir
= policy_to_flow_dir(dir
);
1856 if (__xfrm_decode_session(skb
, &fl
, family
, reverse
) < 0) {
1857 XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR
);
1861 nf_nat_decode_session(skb
, &fl
, family
);
1863 /* First, check used SA against their selectors. */
1867 for (i
=skb
->sp
->len
-1; i
>=0; i
--) {
1868 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
1869 if (!xfrm_selector_match(&x
->sel
, &fl
, family
)) {
1870 XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEMISMATCH
);
1877 if (sk
&& sk
->sk_policy
[dir
]) {
1878 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
);
1880 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR
);
1886 pol
= flow_cache_lookup(&fl
, family
, fl_dir
,
1887 xfrm_policy_lookup
);
1890 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR
);
1895 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
1896 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
1897 XFRM_INC_STATS(LINUX_MIB_XFRMINNOPOLS
);
1903 pol
->curlft
.use_time
= get_seconds();
1907 #ifdef CONFIG_XFRM_SUB_POLICY
1908 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1909 pols
[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
,
1913 if (IS_ERR(pols
[1])) {
1914 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR
);
1917 pols
[1]->curlft
.use_time
= get_seconds();
1923 if (pol
->action
== XFRM_POLICY_ALLOW
) {
1924 struct sec_path
*sp
;
1925 static struct sec_path dummy
;
1926 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
1927 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
1928 struct xfrm_tmpl
**tpp
= tp
;
1932 if ((sp
= skb
->sp
) == NULL
)
1935 for (pi
= 0; pi
< npols
; pi
++) {
1936 if (pols
[pi
] != pol
&&
1937 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
) {
1938 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLBLOCK
);
1941 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1942 XFRM_INC_STATS(LINUX_MIB_XFRMINBUFFERERROR
);
1945 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
1946 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
1950 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
);
1954 /* For each tunnel xfrm, find the first matching tmpl.
1955 * For each tmpl before that, find corresponding xfrm.
1956 * Order is _important_. Later we will implement
1957 * some barriers, but at the moment barriers
1958 * are implied between each two transformations.
1960 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
1961 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
1964 /* "-2 - errored_index" returned */
1966 XFRM_INC_STATS(LINUX_MIB_XFRMINTMPLMISMATCH
);
1971 if (secpath_has_nontransport(sp
, k
, &xerr_idx
)) {
1972 XFRM_INC_STATS(LINUX_MIB_XFRMINTMPLMISMATCH
);
1976 xfrm_pols_put(pols
, npols
);
1979 XFRM_INC_STATS(LINUX_MIB_XFRMINPOLBLOCK
);
1982 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
1984 xfrm_pols_put(pols
, npols
);
1987 EXPORT_SYMBOL(__xfrm_policy_check
);
1989 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
1993 if (xfrm_decode_session(skb
, &fl
, family
) < 0) {
1994 /* XXX: we should have something like FWDHDRERROR here. */
1995 XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR
);
1999 return xfrm_lookup(&skb
->dst
, &fl
, NULL
, 0) == 0;
2001 EXPORT_SYMBOL(__xfrm_route_forward
);
2003 /* Optimize later using cookies and generation ids. */
2005 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
2007 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2008 * to "-1" to force all XFRM destinations to get validated by
2009 * dst_ops->check on every use. We do this because when a
2010 * normal route referenced by an XFRM dst is obsoleted we do
2011 * not go looking around for all parent referencing XFRM dsts
2012 * so that we can invalidate them. It is just too much work.
2013 * Instead we make the checks here on every use. For example:
2015 * XFRM dst A --> IPv4 dst X
2017 * X is the "xdst->route" of A (X is also the "dst->path" of A
2018 * in this example). If X is marked obsolete, "A" will not
2019 * notice. That's what we are validating here via the
2020 * stale_bundle() check.
2022 * When a policy's bundle is pruned, we dst_free() the XFRM
2023 * dst which causes it's ->obsolete field to be set to a
2024 * positive non-zero integer. If an XFRM dst has been pruned
2025 * like this, we want to force a new route lookup.
2027 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
2033 static int stale_bundle(struct dst_entry
*dst
)
2035 return !xfrm_bundle_ok(NULL
, (struct xfrm_dst
*)dst
, NULL
, AF_UNSPEC
, 0);
2038 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
2040 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
2041 dst
->dev
= dev
->nd_net
->loopback_dev
;
2046 EXPORT_SYMBOL(xfrm_dst_ifdown
);
2048 static void xfrm_link_failure(struct sk_buff
*skb
)
2050 /* Impossible. Such dst must be popped before reaches point of failure. */
2054 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
2057 if (dst
->obsolete
) {
2065 static void prune_one_bundle(struct xfrm_policy
*pol
, int (*func
)(struct dst_entry
*), struct dst_entry
**gc_list_p
)
2067 struct dst_entry
*dst
, **dstp
;
2069 write_lock(&pol
->lock
);
2070 dstp
= &pol
->bundles
;
2071 while ((dst
=*dstp
) != NULL
) {
2074 dst
->next
= *gc_list_p
;
2080 write_unlock(&pol
->lock
);
2083 static void xfrm_prune_bundles(int (*func
)(struct dst_entry
*))
2085 struct dst_entry
*gc_list
= NULL
;
2088 read_lock_bh(&xfrm_policy_lock
);
2089 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2090 struct xfrm_policy
*pol
;
2091 struct hlist_node
*entry
;
2092 struct hlist_head
*table
;
2095 hlist_for_each_entry(pol
, entry
,
2096 &xfrm_policy_inexact
[dir
], bydst
)
2097 prune_one_bundle(pol
, func
, &gc_list
);
2099 table
= xfrm_policy_bydst
[dir
].table
;
2100 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
2101 hlist_for_each_entry(pol
, entry
, table
+ i
, bydst
)
2102 prune_one_bundle(pol
, func
, &gc_list
);
2105 read_unlock_bh(&xfrm_policy_lock
);
2108 struct dst_entry
*dst
= gc_list
;
2109 gc_list
= dst
->next
;
2114 static int unused_bundle(struct dst_entry
*dst
)
2116 return !atomic_read(&dst
->__refcnt
);
2119 static void __xfrm_garbage_collect(void)
2121 xfrm_prune_bundles(unused_bundle
);
2124 static int xfrm_flush_bundles(void)
2126 xfrm_prune_bundles(stale_bundle
);
2130 static void xfrm_init_pmtu(struct dst_entry
*dst
)
2133 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2134 u32 pmtu
, route_mtu_cached
;
2136 pmtu
= dst_mtu(dst
->child
);
2137 xdst
->child_mtu_cached
= pmtu
;
2139 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2141 route_mtu_cached
= dst_mtu(xdst
->route
);
2142 xdst
->route_mtu_cached
= route_mtu_cached
;
2144 if (pmtu
> route_mtu_cached
)
2145 pmtu
= route_mtu_cached
;
2147 dst
->metrics
[RTAX_MTU
-1] = pmtu
;
2148 } while ((dst
= dst
->next
));
2151 /* Check that the bundle accepts the flow and its components are
2155 int xfrm_bundle_ok(struct xfrm_policy
*pol
, struct xfrm_dst
*first
,
2156 struct flowi
*fl
, int family
, int strict
)
2158 struct dst_entry
*dst
= &first
->u
.dst
;
2159 struct xfrm_dst
*last
;
2162 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2163 (dst
->dev
&& !netif_running(dst
->dev
)))
2165 #ifdef CONFIG_XFRM_SUB_POLICY
2167 if (first
->origin
&& !flow_cache_uli_match(first
->origin
, fl
))
2169 if (first
->partner
&&
2170 !xfrm_selector_match(first
->partner
, fl
, family
))
2178 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2180 if (fl
&& !xfrm_selector_match(&dst
->xfrm
->sel
, fl
, family
))
2183 !security_xfrm_state_pol_flow_match(dst
->xfrm
, pol
, fl
))
2185 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2187 if (xdst
->genid
!= dst
->xfrm
->genid
)
2191 !(dst
->xfrm
->outer_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
) &&
2192 !xfrm_state_addr_flow_check(dst
->xfrm
, fl
, family
))
2195 mtu
= dst_mtu(dst
->child
);
2196 if (xdst
->child_mtu_cached
!= mtu
) {
2198 xdst
->child_mtu_cached
= mtu
;
2201 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2203 mtu
= dst_mtu(xdst
->route
);
2204 if (xdst
->route_mtu_cached
!= mtu
) {
2206 xdst
->route_mtu_cached
= mtu
;
2210 } while (dst
->xfrm
);
2215 mtu
= last
->child_mtu_cached
;
2219 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2220 if (mtu
> last
->route_mtu_cached
)
2221 mtu
= last
->route_mtu_cached
;
2222 dst
->metrics
[RTAX_MTU
-1] = mtu
;
2227 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2228 last
->child_mtu_cached
= mtu
;
2234 EXPORT_SYMBOL(xfrm_bundle_ok
);
2236 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2239 if (unlikely(afinfo
== NULL
))
2241 if (unlikely(afinfo
->family
>= NPROTO
))
2242 return -EAFNOSUPPORT
;
2243 write_lock_bh(&xfrm_policy_afinfo_lock
);
2244 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
2247 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2248 if (likely(dst_ops
->kmem_cachep
== NULL
))
2249 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2250 if (likely(dst_ops
->check
== NULL
))
2251 dst_ops
->check
= xfrm_dst_check
;
2252 if (likely(dst_ops
->negative_advice
== NULL
))
2253 dst_ops
->negative_advice
= xfrm_negative_advice
;
2254 if (likely(dst_ops
->link_failure
== NULL
))
2255 dst_ops
->link_failure
= xfrm_link_failure
;
2256 if (likely(afinfo
->garbage_collect
== NULL
))
2257 afinfo
->garbage_collect
= __xfrm_garbage_collect
;
2258 xfrm_policy_afinfo
[afinfo
->family
] = afinfo
;
2260 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2263 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2265 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2268 if (unlikely(afinfo
== NULL
))
2270 if (unlikely(afinfo
->family
>= NPROTO
))
2271 return -EAFNOSUPPORT
;
2272 write_lock_bh(&xfrm_policy_afinfo_lock
);
2273 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
2274 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
2277 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2278 xfrm_policy_afinfo
[afinfo
->family
] = NULL
;
2279 dst_ops
->kmem_cachep
= NULL
;
2280 dst_ops
->check
= NULL
;
2281 dst_ops
->negative_advice
= NULL
;
2282 dst_ops
->link_failure
= NULL
;
2283 afinfo
->garbage_collect
= NULL
;
2286 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2289 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2291 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
2293 struct xfrm_policy_afinfo
*afinfo
;
2294 if (unlikely(family
>= NPROTO
))
2296 read_lock(&xfrm_policy_afinfo_lock
);
2297 afinfo
= xfrm_policy_afinfo
[family
];
2298 if (unlikely(!afinfo
))
2299 read_unlock(&xfrm_policy_afinfo_lock
);
2303 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2305 read_unlock(&xfrm_policy_afinfo_lock
);
2308 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
2310 struct net_device
*dev
= ptr
;
2312 if (dev
->nd_net
!= &init_net
)
2317 xfrm_flush_bundles();
2322 static struct notifier_block xfrm_dev_notifier
= {
2328 #ifdef CONFIG_XFRM_STATISTICS
2329 static int __init
xfrm_statistics_init(void)
2331 if (snmp_mib_init((void **)xfrm_statistics
,
2332 sizeof(struct linux_xfrm_mib
)) < 0)
2338 static void __init
xfrm_policy_init(void)
2340 unsigned int hmask
, sz
;
2343 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2344 sizeof(struct xfrm_dst
),
2345 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2349 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2351 xfrm_policy_byidx
= xfrm_hash_alloc(sz
);
2352 xfrm_idx_hmask
= hmask
;
2353 if (!xfrm_policy_byidx
)
2354 panic("XFRM: failed to allocate byidx hash\n");
2356 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2357 struct xfrm_policy_hash
*htab
;
2359 INIT_HLIST_HEAD(&xfrm_policy_inexact
[dir
]);
2361 htab
= &xfrm_policy_bydst
[dir
];
2362 htab
->table
= xfrm_hash_alloc(sz
);
2363 htab
->hmask
= hmask
;
2365 panic("XFRM: failed to allocate bydst hash\n");
2368 INIT_WORK(&xfrm_policy_gc_work
, xfrm_policy_gc_task
);
2369 register_netdevice_notifier(&xfrm_dev_notifier
);
2372 void __init
xfrm_init(void)
2374 #ifdef CONFIG_XFRM_STATISTICS
2375 xfrm_statistics_init();
2380 #ifdef CONFIG_XFRM_STATISTICS
2385 #ifdef CONFIG_AUDITSYSCALL
2386 static void xfrm_audit_common_policyinfo(struct xfrm_policy
*xp
,
2387 struct audit_buffer
*audit_buf
)
2389 struct xfrm_sec_ctx
*ctx
= xp
->security
;
2390 struct xfrm_selector
*sel
= &xp
->selector
;
2393 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2394 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
2396 switch(sel
->family
) {
2398 audit_log_format(audit_buf
, " src=" NIPQUAD_FMT
,
2399 NIPQUAD(sel
->saddr
.a4
));
2400 if (sel
->prefixlen_s
!= 32)
2401 audit_log_format(audit_buf
, " src_prefixlen=%d",
2403 audit_log_format(audit_buf
, " dst=" NIPQUAD_FMT
,
2404 NIPQUAD(sel
->daddr
.a4
));
2405 if (sel
->prefixlen_d
!= 32)
2406 audit_log_format(audit_buf
, " dst_prefixlen=%d",
2410 audit_log_format(audit_buf
, " src=" NIP6_FMT
,
2411 NIP6(*(struct in6_addr
*)sel
->saddr
.a6
));
2412 if (sel
->prefixlen_s
!= 128)
2413 audit_log_format(audit_buf
, " src_prefixlen=%d",
2415 audit_log_format(audit_buf
, " dst=" NIP6_FMT
,
2416 NIP6(*(struct in6_addr
*)sel
->daddr
.a6
));
2417 if (sel
->prefixlen_d
!= 128)
2418 audit_log_format(audit_buf
, " dst_prefixlen=%d",
2424 void xfrm_audit_policy_add(struct xfrm_policy
*xp
, int result
,
2425 u32 auid
, u32 secid
)
2427 struct audit_buffer
*audit_buf
;
2429 audit_buf
= xfrm_audit_start("SPD-add");
2430 if (audit_buf
== NULL
)
2432 xfrm_audit_helper_usrinfo(auid
, secid
, audit_buf
);
2433 audit_log_format(audit_buf
, " res=%u", result
);
2434 xfrm_audit_common_policyinfo(xp
, audit_buf
);
2435 audit_log_end(audit_buf
);
2437 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add
);
2439 void xfrm_audit_policy_delete(struct xfrm_policy
*xp
, int result
,
2440 u32 auid
, u32 secid
)
2442 struct audit_buffer
*audit_buf
;
2444 audit_buf
= xfrm_audit_start("SPD-delete");
2445 if (audit_buf
== NULL
)
2447 xfrm_audit_helper_usrinfo(auid
, secid
, audit_buf
);
2448 audit_log_format(audit_buf
, " res=%u", result
);
2449 xfrm_audit_common_policyinfo(xp
, audit_buf
);
2450 audit_log_end(audit_buf
);
2452 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete
);
2455 #ifdef CONFIG_XFRM_MIGRATE
2456 static int xfrm_migrate_selector_match(struct xfrm_selector
*sel_cmp
,
2457 struct xfrm_selector
*sel_tgt
)
2459 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
2460 if (sel_tgt
->family
== sel_cmp
->family
&&
2461 xfrm_addr_cmp(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
2462 sel_cmp
->family
) == 0 &&
2463 xfrm_addr_cmp(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
2464 sel_cmp
->family
) == 0 &&
2465 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
2466 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
2470 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
2477 static struct xfrm_policy
* xfrm_migrate_policy_find(struct xfrm_selector
*sel
,
2480 struct xfrm_policy
*pol
, *ret
= NULL
;
2481 struct hlist_node
*entry
;
2482 struct hlist_head
*chain
;
2485 read_lock_bh(&xfrm_policy_lock
);
2486 chain
= policy_hash_direct(&sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
2487 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
2488 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2489 pol
->type
== type
) {
2491 priority
= ret
->priority
;
2495 chain
= &xfrm_policy_inexact
[dir
];
2496 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
2497 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2498 pol
->type
== type
&&
2499 pol
->priority
< priority
) {
2508 read_unlock_bh(&xfrm_policy_lock
);
2513 static int migrate_tmpl_match(struct xfrm_migrate
*m
, struct xfrm_tmpl
*t
)
2517 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
2518 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
2520 case XFRM_MODE_TUNNEL
:
2521 case XFRM_MODE_BEET
:
2522 if (xfrm_addr_cmp(&t
->id
.daddr
, &m
->old_daddr
,
2523 m
->old_family
) == 0 &&
2524 xfrm_addr_cmp(&t
->saddr
, &m
->old_saddr
,
2525 m
->old_family
) == 0) {
2529 case XFRM_MODE_TRANSPORT
:
2530 /* in case of transport mode, template does not store
2531 any IP addresses, hence we just compare mode and
2542 /* update endpoint address(es) of template(s) */
2543 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
2544 struct xfrm_migrate
*m
, int num_migrate
)
2546 struct xfrm_migrate
*mp
;
2547 struct dst_entry
*dst
;
2550 write_lock_bh(&pol
->lock
);
2551 if (unlikely(pol
->dead
)) {
2552 /* target policy has been deleted */
2553 write_unlock_bh(&pol
->lock
);
2557 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
2558 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
2559 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
2562 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
&&
2563 pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_BEET
)
2565 /* update endpoints */
2566 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
2567 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
2568 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
2569 sizeof(pol
->xfrm_vec
[i
].saddr
));
2570 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
2572 while ((dst
= pol
->bundles
) != NULL
) {
2573 pol
->bundles
= dst
->next
;
2579 write_unlock_bh(&pol
->lock
);
2587 static int xfrm_migrate_check(struct xfrm_migrate
*m
, int num_migrate
)
2591 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
2594 for (i
= 0; i
< num_migrate
; i
++) {
2595 if ((xfrm_addr_cmp(&m
[i
].old_daddr
, &m
[i
].new_daddr
,
2596 m
[i
].old_family
) == 0) &&
2597 (xfrm_addr_cmp(&m
[i
].old_saddr
, &m
[i
].new_saddr
,
2598 m
[i
].old_family
) == 0))
2600 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
2601 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
2604 /* check if there is any duplicated entry */
2605 for (j
= i
+ 1; j
< num_migrate
; j
++) {
2606 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
2607 sizeof(m
[i
].old_daddr
)) &&
2608 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
2609 sizeof(m
[i
].old_saddr
)) &&
2610 m
[i
].proto
== m
[j
].proto
&&
2611 m
[i
].mode
== m
[j
].mode
&&
2612 m
[i
].reqid
== m
[j
].reqid
&&
2613 m
[i
].old_family
== m
[j
].old_family
)
2621 int xfrm_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2622 struct xfrm_migrate
*m
, int num_migrate
)
2624 int i
, err
, nx_cur
= 0, nx_new
= 0;
2625 struct xfrm_policy
*pol
= NULL
;
2626 struct xfrm_state
*x
, *xc
;
2627 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
2628 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
2629 struct xfrm_migrate
*mp
;
2631 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
2634 /* Stage 1 - find policy */
2635 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
)) == NULL
) {
2640 /* Stage 2 - find and update state(s) */
2641 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2642 if ((x
= xfrm_migrate_state_find(mp
))) {
2645 if ((xc
= xfrm_state_migrate(x
, mp
))) {
2655 /* Stage 3 - update policy */
2656 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
2659 /* Stage 4 - delete old state(s) */
2661 xfrm_states_put(x_cur
, nx_cur
);
2662 xfrm_states_delete(x_cur
, nx_cur
);
2665 /* Stage 5 - announce */
2666 km_migrate(sel
, dir
, type
, m
, num_migrate
);
2678 xfrm_states_put(x_cur
, nx_cur
);
2680 xfrm_states_delete(x_new
, nx_new
);
2684 EXPORT_SYMBOL(xfrm_migrate
);