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>
31 #include "xfrm_hash.h"
33 int sysctl_xfrm_larval_drop __read_mostly
;
35 DEFINE_MUTEX(xfrm_cfg_mutex
);
36 EXPORT_SYMBOL(xfrm_cfg_mutex
);
38 static DEFINE_RWLOCK(xfrm_policy_lock
);
40 unsigned int xfrm_policy_count
[XFRM_POLICY_MAX
*2];
41 EXPORT_SYMBOL(xfrm_policy_count
);
43 static DEFINE_RWLOCK(xfrm_policy_afinfo_lock
);
44 static struct xfrm_policy_afinfo
*xfrm_policy_afinfo
[NPROTO
];
46 static struct kmem_cache
*xfrm_dst_cache __read_mostly
;
48 static struct work_struct xfrm_policy_gc_work
;
49 static HLIST_HEAD(xfrm_policy_gc_list
);
50 static DEFINE_SPINLOCK(xfrm_policy_gc_lock
);
52 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
);
53 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
);
54 static void xfrm_init_pmtu(struct dst_entry
*dst
);
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 static inline struct dst_entry
*xfrm_dst_lookup(struct xfrm_state
*x
, int tos
,
93 xfrm_address_t
*saddr
= &x
->props
.saddr
;
94 xfrm_address_t
*daddr
= &x
->id
.daddr
;
95 struct xfrm_policy_afinfo
*afinfo
;
96 struct dst_entry
*dst
;
98 if (x
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
)
100 if (x
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
)
103 afinfo
= xfrm_policy_get_afinfo(family
);
104 if (unlikely(afinfo
== NULL
))
105 return ERR_PTR(-EAFNOSUPPORT
);
107 dst
= afinfo
->dst_lookup(tos
, saddr
, daddr
);
108 xfrm_policy_put_afinfo(afinfo
);
112 static inline unsigned long make_jiffies(long secs
)
114 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
115 return MAX_SCHEDULE_TIMEOUT
-1;
120 static void xfrm_policy_timer(unsigned long data
)
122 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
123 unsigned long now
= get_seconds();
124 long next
= LONG_MAX
;
128 read_lock(&xp
->lock
);
133 dir
= xfrm_policy_id2dir(xp
->index
);
135 if (xp
->lft
.hard_add_expires_seconds
) {
136 long tmo
= xp
->lft
.hard_add_expires_seconds
+
137 xp
->curlft
.add_time
- now
;
143 if (xp
->lft
.hard_use_expires_seconds
) {
144 long tmo
= xp
->lft
.hard_use_expires_seconds
+
145 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
151 if (xp
->lft
.soft_add_expires_seconds
) {
152 long tmo
= xp
->lft
.soft_add_expires_seconds
+
153 xp
->curlft
.add_time
- now
;
156 tmo
= XFRM_KM_TIMEOUT
;
161 if (xp
->lft
.soft_use_expires_seconds
) {
162 long tmo
= xp
->lft
.soft_use_expires_seconds
+
163 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
166 tmo
= XFRM_KM_TIMEOUT
;
173 km_policy_expired(xp
, dir
, 0, 0);
174 if (next
!= LONG_MAX
&&
175 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
179 read_unlock(&xp
->lock
);
184 read_unlock(&xp
->lock
);
185 if (!xfrm_policy_delete(xp
, dir
))
186 km_policy_expired(xp
, dir
, 1, 0);
191 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
195 struct xfrm_policy
*xfrm_policy_alloc(gfp_t gfp
)
197 struct xfrm_policy
*policy
;
199 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
202 INIT_HLIST_NODE(&policy
->bydst
);
203 INIT_HLIST_NODE(&policy
->byidx
);
204 rwlock_init(&policy
->lock
);
205 atomic_set(&policy
->refcnt
, 1);
206 setup_timer(&policy
->timer
, xfrm_policy_timer
,
207 (unsigned long)policy
);
211 EXPORT_SYMBOL(xfrm_policy_alloc
);
213 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
215 void __xfrm_policy_destroy(struct xfrm_policy
*policy
)
217 BUG_ON(!policy
->dead
);
219 BUG_ON(policy
->bundles
);
221 if (del_timer(&policy
->timer
))
224 security_xfrm_policy_free(policy
);
227 EXPORT_SYMBOL(__xfrm_policy_destroy
);
229 static void xfrm_policy_gc_kill(struct xfrm_policy
*policy
)
231 struct dst_entry
*dst
;
233 while ((dst
= policy
->bundles
) != NULL
) {
234 policy
->bundles
= dst
->next
;
238 if (del_timer(&policy
->timer
))
239 atomic_dec(&policy
->refcnt
);
241 if (atomic_read(&policy
->refcnt
) > 1)
244 xfrm_pol_put(policy
);
247 static void xfrm_policy_gc_task(struct work_struct
*work
)
249 struct xfrm_policy
*policy
;
250 struct hlist_node
*entry
, *tmp
;
251 struct hlist_head gc_list
;
253 spin_lock_bh(&xfrm_policy_gc_lock
);
254 gc_list
.first
= xfrm_policy_gc_list
.first
;
255 INIT_HLIST_HEAD(&xfrm_policy_gc_list
);
256 spin_unlock_bh(&xfrm_policy_gc_lock
);
258 hlist_for_each_entry_safe(policy
, entry
, tmp
, &gc_list
, bydst
)
259 xfrm_policy_gc_kill(policy
);
262 /* Rule must be locked. Release descentant resources, announce
263 * entry dead. The rule must be unlinked from lists to the moment.
266 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
270 write_lock_bh(&policy
->lock
);
273 write_unlock_bh(&policy
->lock
);
275 if (unlikely(dead
)) {
280 spin_lock(&xfrm_policy_gc_lock
);
281 hlist_add_head(&policy
->bydst
, &xfrm_policy_gc_list
);
282 spin_unlock(&xfrm_policy_gc_lock
);
284 schedule_work(&xfrm_policy_gc_work
);
287 struct xfrm_policy_hash
{
288 struct hlist_head
*table
;
292 static struct hlist_head xfrm_policy_inexact
[XFRM_POLICY_MAX
*2];
293 static struct xfrm_policy_hash xfrm_policy_bydst
[XFRM_POLICY_MAX
*2] __read_mostly
;
294 static struct hlist_head
*xfrm_policy_byidx __read_mostly
;
295 static unsigned int xfrm_idx_hmask __read_mostly
;
296 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
298 static inline unsigned int idx_hash(u32 index
)
300 return __idx_hash(index
, xfrm_idx_hmask
);
303 static struct hlist_head
*policy_hash_bysel(struct xfrm_selector
*sel
, unsigned short family
, int dir
)
305 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
306 unsigned int hash
= __sel_hash(sel
, family
, hmask
);
308 return (hash
== hmask
+ 1 ?
309 &xfrm_policy_inexact
[dir
] :
310 xfrm_policy_bydst
[dir
].table
+ hash
);
313 static struct hlist_head
*policy_hash_direct(xfrm_address_t
*daddr
, xfrm_address_t
*saddr
, unsigned short family
, int dir
)
315 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
316 unsigned int hash
= __addr_hash(daddr
, saddr
, family
, hmask
);
318 return xfrm_policy_bydst
[dir
].table
+ hash
;
321 static void xfrm_dst_hash_transfer(struct hlist_head
*list
,
322 struct hlist_head
*ndsttable
,
323 unsigned int nhashmask
)
325 struct hlist_node
*entry
, *tmp
;
326 struct xfrm_policy
*pol
;
328 hlist_for_each_entry_safe(pol
, entry
, tmp
, list
, bydst
) {
331 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
332 pol
->family
, nhashmask
);
333 hlist_add_head(&pol
->bydst
, ndsttable
+h
);
337 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
338 struct hlist_head
*nidxtable
,
339 unsigned int nhashmask
)
341 struct hlist_node
*entry
, *tmp
;
342 struct xfrm_policy
*pol
;
344 hlist_for_each_entry_safe(pol
, entry
, tmp
, list
, byidx
) {
347 h
= __idx_hash(pol
->index
, nhashmask
);
348 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
352 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
354 return ((old_hmask
+ 1) << 1) - 1;
357 static void xfrm_bydst_resize(int dir
)
359 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
360 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
361 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
362 struct hlist_head
*odst
= xfrm_policy_bydst
[dir
].table
;
363 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
369 write_lock_bh(&xfrm_policy_lock
);
371 for (i
= hmask
; i
>= 0; i
--)
372 xfrm_dst_hash_transfer(odst
+ i
, ndst
, nhashmask
);
374 xfrm_policy_bydst
[dir
].table
= ndst
;
375 xfrm_policy_bydst
[dir
].hmask
= nhashmask
;
377 write_unlock_bh(&xfrm_policy_lock
);
379 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
382 static void xfrm_byidx_resize(int total
)
384 unsigned int hmask
= xfrm_idx_hmask
;
385 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
386 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
387 struct hlist_head
*oidx
= xfrm_policy_byidx
;
388 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
394 write_lock_bh(&xfrm_policy_lock
);
396 for (i
= hmask
; i
>= 0; i
--)
397 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
399 xfrm_policy_byidx
= nidx
;
400 xfrm_idx_hmask
= nhashmask
;
402 write_unlock_bh(&xfrm_policy_lock
);
404 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
407 static inline int xfrm_bydst_should_resize(int dir
, int *total
)
409 unsigned int cnt
= xfrm_policy_count
[dir
];
410 unsigned int hmask
= xfrm_policy_bydst
[dir
].hmask
;
415 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
422 static inline int xfrm_byidx_should_resize(int total
)
424 unsigned int hmask
= xfrm_idx_hmask
;
426 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
433 void xfrm_spd_getinfo(struct xfrmk_spdinfo
*si
)
435 read_lock_bh(&xfrm_policy_lock
);
436 si
->incnt
= xfrm_policy_count
[XFRM_POLICY_IN
];
437 si
->outcnt
= xfrm_policy_count
[XFRM_POLICY_OUT
];
438 si
->fwdcnt
= xfrm_policy_count
[XFRM_POLICY_FWD
];
439 si
->inscnt
= xfrm_policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
440 si
->outscnt
= xfrm_policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
441 si
->fwdscnt
= xfrm_policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
442 si
->spdhcnt
= xfrm_idx_hmask
;
443 si
->spdhmcnt
= xfrm_policy_hashmax
;
444 read_unlock_bh(&xfrm_policy_lock
);
446 EXPORT_SYMBOL(xfrm_spd_getinfo
);
448 static DEFINE_MUTEX(hash_resize_mutex
);
449 static void xfrm_hash_resize(struct work_struct
*__unused
)
453 mutex_lock(&hash_resize_mutex
);
456 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
457 if (xfrm_bydst_should_resize(dir
, &total
))
458 xfrm_bydst_resize(dir
);
460 if (xfrm_byidx_should_resize(total
))
461 xfrm_byidx_resize(total
);
463 mutex_unlock(&hash_resize_mutex
);
466 static DECLARE_WORK(xfrm_hash_work
, xfrm_hash_resize
);
468 /* Generate new index... KAME seems to generate them ordered by cost
469 * of an absolute inpredictability of ordering of rules. This will not pass. */
470 static u32
xfrm_gen_index(u8 type
, int dir
)
472 static u32 idx_generator
;
475 struct hlist_node
*entry
;
476 struct hlist_head
*list
;
477 struct xfrm_policy
*p
;
481 idx
= (idx_generator
| dir
);
485 list
= xfrm_policy_byidx
+ idx_hash(idx
);
487 hlist_for_each_entry(p
, entry
, list
, byidx
) {
488 if (p
->index
== idx
) {
498 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
500 u32
*p1
= (u32
*) s1
;
501 u32
*p2
= (u32
*) s2
;
502 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
505 for (i
= 0; i
< len
; i
++) {
513 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
515 struct xfrm_policy
*pol
;
516 struct xfrm_policy
*delpol
;
517 struct hlist_head
*chain
;
518 struct hlist_node
*entry
, *newpos
;
519 struct dst_entry
*gc_list
;
521 write_lock_bh(&xfrm_policy_lock
);
522 chain
= policy_hash_bysel(&policy
->selector
, policy
->family
, dir
);
525 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
526 if (pol
->type
== policy
->type
&&
527 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
528 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
531 write_unlock_bh(&xfrm_policy_lock
);
535 if (policy
->priority
> pol
->priority
)
537 } else if (policy
->priority
>= pol
->priority
) {
538 newpos
= &pol
->bydst
;
545 hlist_add_after(newpos
, &policy
->bydst
);
547 hlist_add_head(&policy
->bydst
, chain
);
548 xfrm_pol_hold(policy
);
549 xfrm_policy_count
[dir
]++;
550 atomic_inc(&flow_cache_genid
);
552 hlist_del(&delpol
->bydst
);
553 hlist_del(&delpol
->byidx
);
554 xfrm_policy_count
[dir
]--;
556 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(policy
->type
, dir
);
557 hlist_add_head(&policy
->byidx
, xfrm_policy_byidx
+idx_hash(policy
->index
));
558 policy
->curlft
.add_time
= get_seconds();
559 policy
->curlft
.use_time
= 0;
560 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
561 xfrm_pol_hold(policy
);
562 write_unlock_bh(&xfrm_policy_lock
);
565 xfrm_policy_kill(delpol
);
566 else if (xfrm_bydst_should_resize(dir
, NULL
))
567 schedule_work(&xfrm_hash_work
);
569 read_lock_bh(&xfrm_policy_lock
);
571 entry
= &policy
->bydst
;
572 hlist_for_each_entry_continue(policy
, entry
, bydst
) {
573 struct dst_entry
*dst
;
575 write_lock(&policy
->lock
);
576 dst
= policy
->bundles
;
578 struct dst_entry
*tail
= dst
;
581 tail
->next
= gc_list
;
584 policy
->bundles
= NULL
;
586 write_unlock(&policy
->lock
);
588 read_unlock_bh(&xfrm_policy_lock
);
591 struct dst_entry
*dst
= gc_list
;
599 EXPORT_SYMBOL(xfrm_policy_insert
);
601 struct xfrm_policy
*xfrm_policy_bysel_ctx(u8 type
, int dir
,
602 struct xfrm_selector
*sel
,
603 struct xfrm_sec_ctx
*ctx
, int delete,
606 struct xfrm_policy
*pol
, *ret
;
607 struct hlist_head
*chain
;
608 struct hlist_node
*entry
;
611 write_lock_bh(&xfrm_policy_lock
);
612 chain
= policy_hash_bysel(sel
, sel
->family
, dir
);
614 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
615 if (pol
->type
== type
&&
616 !selector_cmp(sel
, &pol
->selector
) &&
617 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
620 *err
= security_xfrm_policy_delete(pol
);
622 write_unlock_bh(&xfrm_policy_lock
);
625 hlist_del(&pol
->bydst
);
626 hlist_del(&pol
->byidx
);
627 xfrm_policy_count
[dir
]--;
633 write_unlock_bh(&xfrm_policy_lock
);
636 atomic_inc(&flow_cache_genid
);
637 xfrm_policy_kill(ret
);
641 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
643 struct xfrm_policy
*xfrm_policy_byid(u8 type
, int dir
, u32 id
, int delete,
646 struct xfrm_policy
*pol
, *ret
;
647 struct hlist_head
*chain
;
648 struct hlist_node
*entry
;
651 if (xfrm_policy_id2dir(id
) != dir
)
655 write_lock_bh(&xfrm_policy_lock
);
656 chain
= xfrm_policy_byidx
+ idx_hash(id
);
658 hlist_for_each_entry(pol
, entry
, chain
, byidx
) {
659 if (pol
->type
== type
&& pol
->index
== id
) {
662 *err
= security_xfrm_policy_delete(pol
);
664 write_unlock_bh(&xfrm_policy_lock
);
667 hlist_del(&pol
->bydst
);
668 hlist_del(&pol
->byidx
);
669 xfrm_policy_count
[dir
]--;
675 write_unlock_bh(&xfrm_policy_lock
);
678 atomic_inc(&flow_cache_genid
);
679 xfrm_policy_kill(ret
);
683 EXPORT_SYMBOL(xfrm_policy_byid
);
685 #ifdef CONFIG_SECURITY_NETWORK_XFRM
687 xfrm_policy_flush_secctx_check(u8 type
, struct xfrm_audit
*audit_info
)
691 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
692 struct xfrm_policy
*pol
;
693 struct hlist_node
*entry
;
696 hlist_for_each_entry(pol
, entry
,
697 &xfrm_policy_inexact
[dir
], bydst
) {
698 if (pol
->type
!= type
)
700 err
= security_xfrm_policy_delete(pol
);
702 xfrm_audit_policy_delete(pol
, 0,
703 audit_info
->loginuid
,
708 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
709 hlist_for_each_entry(pol
, entry
,
710 xfrm_policy_bydst
[dir
].table
+ i
,
712 if (pol
->type
!= type
)
714 err
= security_xfrm_policy_delete(pol
);
716 xfrm_audit_policy_delete(pol
, 0,
717 audit_info
->loginuid
,
728 xfrm_policy_flush_secctx_check(u8 type
, struct xfrm_audit
*audit_info
)
734 int xfrm_policy_flush(u8 type
, struct xfrm_audit
*audit_info
)
738 write_lock_bh(&xfrm_policy_lock
);
740 err
= xfrm_policy_flush_secctx_check(type
, audit_info
);
744 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
745 struct xfrm_policy
*pol
;
746 struct hlist_node
*entry
;
751 hlist_for_each_entry(pol
, entry
,
752 &xfrm_policy_inexact
[dir
], bydst
) {
753 if (pol
->type
!= type
)
755 hlist_del(&pol
->bydst
);
756 hlist_del(&pol
->byidx
);
757 write_unlock_bh(&xfrm_policy_lock
);
759 xfrm_audit_policy_delete(pol
, 1, audit_info
->loginuid
,
762 xfrm_policy_kill(pol
);
765 write_lock_bh(&xfrm_policy_lock
);
769 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
771 hlist_for_each_entry(pol
, entry
,
772 xfrm_policy_bydst
[dir
].table
+ i
,
774 if (pol
->type
!= type
)
776 hlist_del(&pol
->bydst
);
777 hlist_del(&pol
->byidx
);
778 write_unlock_bh(&xfrm_policy_lock
);
780 xfrm_audit_policy_delete(pol
, 1,
781 audit_info
->loginuid
,
783 xfrm_policy_kill(pol
);
786 write_lock_bh(&xfrm_policy_lock
);
791 xfrm_policy_count
[dir
] -= killed
;
793 atomic_inc(&flow_cache_genid
);
795 write_unlock_bh(&xfrm_policy_lock
);
798 EXPORT_SYMBOL(xfrm_policy_flush
);
800 int xfrm_policy_walk(u8 type
, int (*func
)(struct xfrm_policy
*, int, int, void*),
803 struct xfrm_policy
*pol
, *last
= NULL
;
804 struct hlist_node
*entry
;
805 int dir
, last_dir
= 0, count
, error
;
807 read_lock_bh(&xfrm_policy_lock
);
810 for (dir
= 0; dir
< 2*XFRM_POLICY_MAX
; dir
++) {
811 struct hlist_head
*table
= xfrm_policy_bydst
[dir
].table
;
814 hlist_for_each_entry(pol
, entry
,
815 &xfrm_policy_inexact
[dir
], bydst
) {
816 if (pol
->type
!= type
)
819 error
= func(last
, last_dir
% XFRM_POLICY_MAX
,
828 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
829 hlist_for_each_entry(pol
, entry
, table
+ i
, bydst
) {
830 if (pol
->type
!= type
)
833 error
= func(last
, last_dir
% XFRM_POLICY_MAX
,
848 error
= func(last
, last_dir
% XFRM_POLICY_MAX
, 0, data
);
850 read_unlock_bh(&xfrm_policy_lock
);
853 EXPORT_SYMBOL(xfrm_policy_walk
);
856 * Find policy to apply to this flow.
858 * Returns 0 if policy found, else an -errno.
860 static int xfrm_policy_match(struct xfrm_policy
*pol
, struct flowi
*fl
,
861 u8 type
, u16 family
, int dir
)
863 struct xfrm_selector
*sel
= &pol
->selector
;
864 int match
, ret
= -ESRCH
;
866 if (pol
->family
!= family
||
870 match
= xfrm_selector_match(sel
, fl
, family
);
872 ret
= security_xfrm_policy_lookup(pol
, fl
->secid
, dir
);
877 static struct xfrm_policy
*xfrm_policy_lookup_bytype(u8 type
, struct flowi
*fl
,
881 struct xfrm_policy
*pol
, *ret
;
882 xfrm_address_t
*daddr
, *saddr
;
883 struct hlist_node
*entry
;
884 struct hlist_head
*chain
;
887 daddr
= xfrm_flowi_daddr(fl
, family
);
888 saddr
= xfrm_flowi_saddr(fl
, family
);
889 if (unlikely(!daddr
|| !saddr
))
892 read_lock_bh(&xfrm_policy_lock
);
893 chain
= policy_hash_direct(daddr
, saddr
, family
, dir
);
895 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
896 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
906 priority
= ret
->priority
;
910 chain
= &xfrm_policy_inexact
[dir
];
911 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
912 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
920 } else if (pol
->priority
< priority
) {
928 read_unlock_bh(&xfrm_policy_lock
);
933 static int xfrm_policy_lookup(struct flowi
*fl
, u16 family
, u8 dir
,
934 void **objp
, atomic_t
**obj_refp
)
936 struct xfrm_policy
*pol
;
939 #ifdef CONFIG_XFRM_SUB_POLICY
940 pol
= xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
948 pol
= xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
953 #ifdef CONFIG_XFRM_SUB_POLICY
956 if ((*objp
= (void *) pol
) != NULL
)
957 *obj_refp
= &pol
->refcnt
;
961 static inline int policy_to_flow_dir(int dir
)
963 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
964 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
965 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
971 case XFRM_POLICY_OUT
:
973 case XFRM_POLICY_FWD
:
978 static struct xfrm_policy
*xfrm_sk_policy_lookup(struct sock
*sk
, int dir
, struct flowi
*fl
)
980 struct xfrm_policy
*pol
;
982 read_lock_bh(&xfrm_policy_lock
);
983 if ((pol
= sk
->sk_policy
[dir
]) != NULL
) {
984 int match
= xfrm_selector_match(&pol
->selector
, fl
,
989 err
= security_xfrm_policy_lookup(pol
, fl
->secid
,
990 policy_to_flow_dir(dir
));
993 else if (err
== -ESRCH
)
1000 read_unlock_bh(&xfrm_policy_lock
);
1004 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1006 struct hlist_head
*chain
= policy_hash_bysel(&pol
->selector
,
1009 hlist_add_head(&pol
->bydst
, chain
);
1010 hlist_add_head(&pol
->byidx
, xfrm_policy_byidx
+idx_hash(pol
->index
));
1011 xfrm_policy_count
[dir
]++;
1014 if (xfrm_bydst_should_resize(dir
, NULL
))
1015 schedule_work(&xfrm_hash_work
);
1018 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1021 if (hlist_unhashed(&pol
->bydst
))
1024 hlist_del(&pol
->bydst
);
1025 hlist_del(&pol
->byidx
);
1026 xfrm_policy_count
[dir
]--;
1031 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1033 write_lock_bh(&xfrm_policy_lock
);
1034 pol
= __xfrm_policy_unlink(pol
, dir
);
1035 write_unlock_bh(&xfrm_policy_lock
);
1037 if (dir
< XFRM_POLICY_MAX
)
1038 atomic_inc(&flow_cache_genid
);
1039 xfrm_policy_kill(pol
);
1044 EXPORT_SYMBOL(xfrm_policy_delete
);
1046 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1048 struct xfrm_policy
*old_pol
;
1050 #ifdef CONFIG_XFRM_SUB_POLICY
1051 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1055 write_lock_bh(&xfrm_policy_lock
);
1056 old_pol
= sk
->sk_policy
[dir
];
1057 sk
->sk_policy
[dir
] = pol
;
1059 pol
->curlft
.add_time
= get_seconds();
1060 pol
->index
= xfrm_gen_index(pol
->type
, XFRM_POLICY_MAX
+dir
);
1061 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+dir
);
1064 __xfrm_policy_unlink(old_pol
, XFRM_POLICY_MAX
+dir
);
1065 write_unlock_bh(&xfrm_policy_lock
);
1068 xfrm_policy_kill(old_pol
);
1073 static struct xfrm_policy
*clone_policy(struct xfrm_policy
*old
, int dir
)
1075 struct xfrm_policy
*newp
= xfrm_policy_alloc(GFP_ATOMIC
);
1078 newp
->selector
= old
->selector
;
1079 if (security_xfrm_policy_clone(old
, newp
)) {
1081 return NULL
; /* ENOMEM */
1083 newp
->lft
= old
->lft
;
1084 newp
->curlft
= old
->curlft
;
1085 newp
->action
= old
->action
;
1086 newp
->flags
= old
->flags
;
1087 newp
->xfrm_nr
= old
->xfrm_nr
;
1088 newp
->index
= old
->index
;
1089 newp
->type
= old
->type
;
1090 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1091 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1092 write_lock_bh(&xfrm_policy_lock
);
1093 __xfrm_policy_link(newp
, XFRM_POLICY_MAX
+dir
);
1094 write_unlock_bh(&xfrm_policy_lock
);
1100 int __xfrm_sk_clone_policy(struct sock
*sk
)
1102 struct xfrm_policy
*p0
= sk
->sk_policy
[0],
1103 *p1
= sk
->sk_policy
[1];
1105 sk
->sk_policy
[0] = sk
->sk_policy
[1] = NULL
;
1106 if (p0
&& (sk
->sk_policy
[0] = clone_policy(p0
, 0)) == NULL
)
1108 if (p1
&& (sk
->sk_policy
[1] = clone_policy(p1
, 1)) == NULL
)
1114 xfrm_get_saddr(xfrm_address_t
*local
, xfrm_address_t
*remote
,
1115 unsigned short family
)
1118 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1120 if (unlikely(afinfo
== NULL
))
1122 err
= afinfo
->get_saddr(local
, remote
);
1123 xfrm_policy_put_afinfo(afinfo
);
1127 /* Resolve list of templates for the flow, given policy. */
1130 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, struct flowi
*fl
,
1131 struct xfrm_state
**xfrm
,
1132 unsigned short family
)
1136 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1137 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1140 for (nx
=0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1141 struct xfrm_state
*x
;
1142 xfrm_address_t
*remote
= daddr
;
1143 xfrm_address_t
*local
= saddr
;
1144 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1146 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1147 tmpl
->mode
== XFRM_MODE_BEET
) {
1148 remote
= &tmpl
->id
.daddr
;
1149 local
= &tmpl
->saddr
;
1150 family
= tmpl
->encap_family
;
1151 if (xfrm_addr_any(local
, family
)) {
1152 error
= xfrm_get_saddr(&tmp
, remote
, family
);
1159 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1161 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1168 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1173 if (!tmpl
->optional
)
1179 for (nx
--; nx
>=0; nx
--)
1180 xfrm_state_put(xfrm
[nx
]);
1185 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, struct flowi
*fl
,
1186 struct xfrm_state
**xfrm
,
1187 unsigned short family
)
1189 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1190 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1196 for (i
= 0; i
< npols
; i
++) {
1197 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1202 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1210 /* found states are sorted for outbound processing */
1212 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1217 for (cnx
--; cnx
>=0; cnx
--)
1218 xfrm_state_put(tpp
[cnx
]);
1223 /* Check that the bundle accepts the flow and its components are
1227 static struct dst_entry
*
1228 xfrm_find_bundle(struct flowi
*fl
, struct xfrm_policy
*policy
, unsigned short family
)
1230 struct dst_entry
*x
;
1231 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1232 if (unlikely(afinfo
== NULL
))
1233 return ERR_PTR(-EINVAL
);
1234 x
= afinfo
->find_bundle(fl
, policy
);
1235 xfrm_policy_put_afinfo(afinfo
);
1239 static inline int xfrm_get_tos(struct flowi
*fl
, int family
)
1241 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1247 tos
= afinfo
->get_tos(fl
);
1249 xfrm_policy_put_afinfo(afinfo
);
1254 static inline struct xfrm_dst
*xfrm_alloc_dst(int family
)
1256 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1257 struct xfrm_dst
*xdst
;
1260 return ERR_PTR(-EINVAL
);
1262 xdst
= dst_alloc(afinfo
->dst_ops
) ?: ERR_PTR(-ENOBUFS
);
1264 xfrm_policy_put_afinfo(afinfo
);
1269 static inline int xfrm_fill_dst(struct xfrm_dst
*xdst
, struct net_device
*dev
)
1271 struct xfrm_policy_afinfo
*afinfo
=
1272 xfrm_policy_get_afinfo(xdst
->u
.dst
.ops
->family
);
1278 err
= afinfo
->fill_dst(xdst
, dev
);
1280 xfrm_policy_put_afinfo(afinfo
);
1285 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1286 * all the metrics... Shortly, bundle a bundle.
1289 static struct dst_entry
*xfrm_bundle_create(struct xfrm_policy
*policy
,
1290 struct xfrm_state
**xfrm
, int nx
,
1292 struct dst_entry
*dst
)
1294 unsigned long now
= jiffies
;
1295 struct net_device
*dev
;
1296 struct dst_entry
*dst_prev
= NULL
;
1297 struct dst_entry
*dst0
= NULL
;
1301 int trailer_len
= 0;
1303 int family
= policy
->selector
.family
;
1305 tos
= xfrm_get_tos(fl
, family
);
1312 for (; i
< nx
; i
++) {
1313 struct xfrm_dst
*xdst
= xfrm_alloc_dst(family
);
1314 struct dst_entry
*dst1
= &xdst
->u
.dst
;
1316 err
= PTR_ERR(xdst
);
1325 dst_prev
->child
= dst_clone(dst1
);
1326 dst1
->flags
|= DST_NOHASH
;
1330 memcpy(&dst1
->metrics
, &dst
->metrics
, sizeof(dst
->metrics
));
1332 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1333 family
= xfrm
[i
]->props
.family
;
1334 dst
= xfrm_dst_lookup(xfrm
[i
], tos
, family
);
1341 dst1
->xfrm
= xfrm
[i
];
1342 xdst
->genid
= xfrm
[i
]->genid
;
1344 dst1
->obsolete
= -1;
1345 dst1
->flags
|= DST_HOST
;
1346 dst1
->lastuse
= now
;
1348 dst1
->input
= dst_discard
;
1349 dst1
->output
= xfrm
[i
]->outer_mode
->afinfo
->output
;
1351 dst1
->next
= dst_prev
;
1354 header_len
+= xfrm
[i
]->props
.header_len
;
1355 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
1358 dst_prev
->child
= dst
;
1366 /* Copy neighbout for reachability confirmation */
1367 dst0
->neighbour
= neigh_clone(dst
->neighbour
);
1369 xfrm_init_pmtu(dst_prev
);
1371 for (dst_prev
= dst0
; dst_prev
!= dst
; dst_prev
= dst_prev
->child
) {
1372 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst_prev
;
1374 err
= xfrm_fill_dst(xdst
, dev
);
1378 dst_prev
->header_len
= header_len
;
1379 dst_prev
->trailer_len
= trailer_len
;
1380 header_len
-= xdst
->u
.dst
.xfrm
->props
.header_len
;
1381 trailer_len
-= xdst
->u
.dst
.xfrm
->props
.trailer_len
;
1389 xfrm_state_put(xfrm
[i
]);
1393 dst0
= ERR_PTR(err
);
1398 xfrm_dst_alloc_copy(void **target
, void *src
, int size
)
1401 *target
= kmalloc(size
, GFP_ATOMIC
);
1405 memcpy(*target
, src
, size
);
1410 xfrm_dst_update_parent(struct dst_entry
*dst
, struct xfrm_selector
*sel
)
1412 #ifdef CONFIG_XFRM_SUB_POLICY
1413 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1414 return xfrm_dst_alloc_copy((void **)&(xdst
->partner
),
1422 xfrm_dst_update_origin(struct dst_entry
*dst
, struct flowi
*fl
)
1424 #ifdef CONFIG_XFRM_SUB_POLICY
1425 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1426 return xfrm_dst_alloc_copy((void **)&(xdst
->origin
), fl
, sizeof(*fl
));
1432 static int stale_bundle(struct dst_entry
*dst
);
1434 /* Main function: finds/creates a bundle for given flow.
1436 * At the moment we eat a raw IP route. Mostly to speed up lookups
1437 * on interfaces with disabled IPsec.
1439 int __xfrm_lookup(struct dst_entry
**dst_p
, struct flowi
*fl
,
1440 struct sock
*sk
, int flags
)
1442 struct xfrm_policy
*policy
;
1443 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1448 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1449 struct dst_entry
*dst
, *dst_orig
= *dst_p
;
1454 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
1457 genid
= atomic_read(&flow_cache_genid
);
1459 for (pi
= 0; pi
< ARRAY_SIZE(pols
); pi
++)
1465 if (sk
&& sk
->sk_policy
[XFRM_POLICY_OUT
]) {
1466 policy
= xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
);
1467 err
= PTR_ERR(policy
);
1473 /* To accelerate a bit... */
1474 if ((dst_orig
->flags
& DST_NOXFRM
) ||
1475 !xfrm_policy_count
[XFRM_POLICY_OUT
])
1478 policy
= flow_cache_lookup(fl
, dst_orig
->ops
->family
,
1479 dir
, xfrm_policy_lookup
);
1480 err
= PTR_ERR(policy
);
1488 family
= dst_orig
->ops
->family
;
1489 policy
->curlft
.use_time
= get_seconds();
1492 xfrm_nr
+= pols
[0]->xfrm_nr
;
1494 switch (policy
->action
) {
1496 case XFRM_POLICY_BLOCK
:
1497 /* Prohibit the flow */
1501 case XFRM_POLICY_ALLOW
:
1502 #ifndef CONFIG_XFRM_SUB_POLICY
1503 if (policy
->xfrm_nr
== 0) {
1504 /* Flow passes not transformed. */
1505 xfrm_pol_put(policy
);
1510 /* Try to find matching bundle.
1512 * LATER: help from flow cache. It is optional, this
1513 * is required only for output policy.
1515 dst
= xfrm_find_bundle(fl
, policy
, family
);
1524 #ifdef CONFIG_XFRM_SUB_POLICY
1525 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1526 pols
[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
,
1530 if (IS_ERR(pols
[1])) {
1531 err
= PTR_ERR(pols
[1]);
1534 if (pols
[1]->action
== XFRM_POLICY_BLOCK
) {
1539 xfrm_nr
+= pols
[1]->xfrm_nr
;
1544 * Because neither flowi nor bundle information knows about
1545 * transformation template size. On more than one policy usage
1546 * we can realize whether all of them is bypass or not after
1547 * they are searched. See above not-transformed bypass
1548 * is surrounded by non-sub policy configuration, too.
1551 /* Flow passes not transformed. */
1552 xfrm_pols_put(pols
, npols
);
1557 nx
= xfrm_tmpl_resolve(pols
, npols
, fl
, xfrm
, family
);
1559 if (unlikely(nx
<0)) {
1561 if (err
== -EAGAIN
&& sysctl_xfrm_larval_drop
) {
1562 /* EREMOTE tells the caller to generate
1563 * a one-shot blackhole route.
1565 xfrm_pol_put(policy
);
1568 if (err
== -EAGAIN
&& flags
) {
1569 DECLARE_WAITQUEUE(wait
, current
);
1571 add_wait_queue(&km_waitq
, &wait
);
1572 set_current_state(TASK_INTERRUPTIBLE
);
1574 set_current_state(TASK_RUNNING
);
1575 remove_wait_queue(&km_waitq
, &wait
);
1577 nx
= xfrm_tmpl_resolve(pols
, npols
, fl
, xfrm
, family
);
1579 if (nx
== -EAGAIN
&& signal_pending(current
)) {
1583 if (nx
== -EAGAIN
||
1584 genid
!= atomic_read(&flow_cache_genid
)) {
1585 xfrm_pols_put(pols
, npols
);
1594 /* Flow passes not transformed. */
1595 xfrm_pols_put(pols
, npols
);
1599 dst
= xfrm_bundle_create(policy
, xfrm
, nx
, fl
, dst_orig
);
1604 for (pi
= 0; pi
< npols
; pi
++) {
1605 read_lock_bh(&pols
[pi
]->lock
);
1606 pol_dead
|= pols
[pi
]->dead
;
1607 read_unlock_bh(&pols
[pi
]->lock
);
1610 write_lock_bh(&policy
->lock
);
1611 if (unlikely(pol_dead
|| stale_bundle(dst
))) {
1612 /* Wow! While we worked on resolving, this
1613 * policy has gone. Retry. It is not paranoia,
1614 * we just cannot enlist new bundle to dead object.
1615 * We can't enlist stable bundles either.
1617 write_unlock_bh(&policy
->lock
);
1621 err
= -EHOSTUNREACH
;
1626 err
= xfrm_dst_update_parent(dst
, &pols
[1]->selector
);
1628 err
= xfrm_dst_update_origin(dst
, fl
);
1629 if (unlikely(err
)) {
1630 write_unlock_bh(&policy
->lock
);
1636 dst
->next
= policy
->bundles
;
1637 policy
->bundles
= dst
;
1639 write_unlock_bh(&policy
->lock
);
1642 dst_release(dst_orig
);
1643 xfrm_pols_put(pols
, npols
);
1647 xfrm_pols_put(pols
, npols
);
1649 dst_release(dst_orig
);
1653 EXPORT_SYMBOL(__xfrm_lookup
);
1655 int xfrm_lookup(struct dst_entry
**dst_p
, struct flowi
*fl
,
1656 struct sock
*sk
, int flags
)
1658 int err
= __xfrm_lookup(dst_p
, fl
, sk
, flags
);
1660 if (err
== -EREMOTE
) {
1661 dst_release(*dst_p
);
1668 EXPORT_SYMBOL(xfrm_lookup
);
1671 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, struct flowi
*fl
)
1673 struct xfrm_state
*x
;
1675 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
1677 x
= skb
->sp
->xvec
[idx
];
1678 if (!x
->type
->reject
)
1680 return x
->type
->reject(x
, skb
, fl
);
1683 /* When skb is transformed back to its "native" form, we have to
1684 * check policy restrictions. At the moment we make this in maximally
1685 * stupid way. Shame on me. :-) Of course, connected sockets must
1686 * have policy cached at them.
1690 xfrm_state_ok(struct xfrm_tmpl
*tmpl
, struct xfrm_state
*x
,
1691 unsigned short family
)
1693 if (xfrm_state_kern(x
))
1694 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
1695 return x
->id
.proto
== tmpl
->id
.proto
&&
1696 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
1697 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
1698 x
->props
.mode
== tmpl
->mode
&&
1699 ((tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
1700 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
1701 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
1702 xfrm_state_addr_cmp(tmpl
, x
, family
));
1706 * 0 or more than 0 is returned when validation is succeeded (either bypass
1707 * because of optional transport mode, or next index of the mathced secpath
1708 * state with the template.
1709 * -1 is returned when no matching template is found.
1710 * Otherwise "-2 - errored_index" is returned.
1713 xfrm_policy_ok(struct xfrm_tmpl
*tmpl
, struct sec_path
*sp
, int start
,
1714 unsigned short family
)
1718 if (tmpl
->optional
) {
1719 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
1723 for (; idx
< sp
->len
; idx
++) {
1724 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
1726 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1736 xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, unsigned short family
)
1738 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1741 if (unlikely(afinfo
== NULL
))
1742 return -EAFNOSUPPORT
;
1744 afinfo
->decode_session(skb
, fl
);
1745 err
= security_xfrm_decode_session(skb
, &fl
->secid
);
1746 xfrm_policy_put_afinfo(afinfo
);
1749 EXPORT_SYMBOL(xfrm_decode_session
);
1751 static inline int secpath_has_nontransport(struct sec_path
*sp
, int k
, int *idxp
)
1753 for (; k
< sp
->len
; k
++) {
1754 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1763 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
1764 unsigned short family
)
1766 struct xfrm_policy
*pol
;
1767 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1772 u8 fl_dir
= policy_to_flow_dir(dir
);
1775 if (xfrm_decode_session(skb
, &fl
, family
) < 0)
1777 nf_nat_decode_session(skb
, &fl
, family
);
1779 /* First, check used SA against their selectors. */
1783 for (i
=skb
->sp
->len
-1; i
>=0; i
--) {
1784 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
1785 if (!xfrm_selector_match(&x
->sel
, &fl
, family
))
1791 if (sk
&& sk
->sk_policy
[dir
]) {
1792 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
);
1798 pol
= flow_cache_lookup(&fl
, family
, fl_dir
,
1799 xfrm_policy_lookup
);
1805 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
1806 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
1812 pol
->curlft
.use_time
= get_seconds();
1816 #ifdef CONFIG_XFRM_SUB_POLICY
1817 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1818 pols
[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN
,
1822 if (IS_ERR(pols
[1]))
1824 pols
[1]->curlft
.use_time
= get_seconds();
1830 if (pol
->action
== XFRM_POLICY_ALLOW
) {
1831 struct sec_path
*sp
;
1832 static struct sec_path dummy
;
1833 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
1834 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
1835 struct xfrm_tmpl
**tpp
= tp
;
1839 if ((sp
= skb
->sp
) == NULL
)
1842 for (pi
= 0; pi
< npols
; pi
++) {
1843 if (pols
[pi
] != pol
&&
1844 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
)
1846 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
)
1848 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
1849 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
1853 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
);
1857 /* For each tunnel xfrm, find the first matching tmpl.
1858 * For each tmpl before that, find corresponding xfrm.
1859 * Order is _important_. Later we will implement
1860 * some barriers, but at the moment barriers
1861 * are implied between each two transformations.
1863 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
1864 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
1867 /* "-2 - errored_index" returned */
1873 if (secpath_has_nontransport(sp
, k
, &xerr_idx
))
1876 xfrm_pols_put(pols
, npols
);
1881 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
1883 xfrm_pols_put(pols
, npols
);
1886 EXPORT_SYMBOL(__xfrm_policy_check
);
1888 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
1892 if (xfrm_decode_session(skb
, &fl
, family
) < 0)
1895 return xfrm_lookup(&skb
->dst
, &fl
, NULL
, 0) == 0;
1897 EXPORT_SYMBOL(__xfrm_route_forward
);
1899 /* Optimize later using cookies and generation ids. */
1901 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
1903 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1904 * to "-1" to force all XFRM destinations to get validated by
1905 * dst_ops->check on every use. We do this because when a
1906 * normal route referenced by an XFRM dst is obsoleted we do
1907 * not go looking around for all parent referencing XFRM dsts
1908 * so that we can invalidate them. It is just too much work.
1909 * Instead we make the checks here on every use. For example:
1911 * XFRM dst A --> IPv4 dst X
1913 * X is the "xdst->route" of A (X is also the "dst->path" of A
1914 * in this example). If X is marked obsolete, "A" will not
1915 * notice. That's what we are validating here via the
1916 * stale_bundle() check.
1918 * When a policy's bundle is pruned, we dst_free() the XFRM
1919 * dst which causes it's ->obsolete field to be set to a
1920 * positive non-zero integer. If an XFRM dst has been pruned
1921 * like this, we want to force a new route lookup.
1923 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
1929 static int stale_bundle(struct dst_entry
*dst
)
1931 return !xfrm_bundle_ok(NULL
, (struct xfrm_dst
*)dst
, NULL
, AF_UNSPEC
, 0);
1934 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
1936 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
1937 dst
->dev
= init_net
.loopback_dev
;
1942 EXPORT_SYMBOL(xfrm_dst_ifdown
);
1944 static void xfrm_link_failure(struct sk_buff
*skb
)
1946 /* Impossible. Such dst must be popped before reaches point of failure. */
1950 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
1953 if (dst
->obsolete
) {
1961 static void prune_one_bundle(struct xfrm_policy
*pol
, int (*func
)(struct dst_entry
*), struct dst_entry
**gc_list_p
)
1963 struct dst_entry
*dst
, **dstp
;
1965 write_lock(&pol
->lock
);
1966 dstp
= &pol
->bundles
;
1967 while ((dst
=*dstp
) != NULL
) {
1970 dst
->next
= *gc_list_p
;
1976 write_unlock(&pol
->lock
);
1979 static void xfrm_prune_bundles(int (*func
)(struct dst_entry
*))
1981 struct dst_entry
*gc_list
= NULL
;
1984 read_lock_bh(&xfrm_policy_lock
);
1985 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
1986 struct xfrm_policy
*pol
;
1987 struct hlist_node
*entry
;
1988 struct hlist_head
*table
;
1991 hlist_for_each_entry(pol
, entry
,
1992 &xfrm_policy_inexact
[dir
], bydst
)
1993 prune_one_bundle(pol
, func
, &gc_list
);
1995 table
= xfrm_policy_bydst
[dir
].table
;
1996 for (i
= xfrm_policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
1997 hlist_for_each_entry(pol
, entry
, table
+ i
, bydst
)
1998 prune_one_bundle(pol
, func
, &gc_list
);
2001 read_unlock_bh(&xfrm_policy_lock
);
2004 struct dst_entry
*dst
= gc_list
;
2005 gc_list
= dst
->next
;
2010 static int unused_bundle(struct dst_entry
*dst
)
2012 return !atomic_read(&dst
->__refcnt
);
2015 static void __xfrm_garbage_collect(void)
2017 xfrm_prune_bundles(unused_bundle
);
2020 static int xfrm_flush_bundles(void)
2022 xfrm_prune_bundles(stale_bundle
);
2026 static void xfrm_init_pmtu(struct dst_entry
*dst
)
2029 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2030 u32 pmtu
, route_mtu_cached
;
2032 pmtu
= dst_mtu(dst
->child
);
2033 xdst
->child_mtu_cached
= pmtu
;
2035 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2037 route_mtu_cached
= dst_mtu(xdst
->route
);
2038 xdst
->route_mtu_cached
= route_mtu_cached
;
2040 if (pmtu
> route_mtu_cached
)
2041 pmtu
= route_mtu_cached
;
2043 dst
->metrics
[RTAX_MTU
-1] = pmtu
;
2044 } while ((dst
= dst
->next
));
2047 /* Check that the bundle accepts the flow and its components are
2051 int xfrm_bundle_ok(struct xfrm_policy
*pol
, struct xfrm_dst
*first
,
2052 struct flowi
*fl
, int family
, int strict
)
2054 struct dst_entry
*dst
= &first
->u
.dst
;
2055 struct xfrm_dst
*last
;
2058 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2059 (dst
->dev
&& !netif_running(dst
->dev
)))
2061 #ifdef CONFIG_XFRM_SUB_POLICY
2063 if (first
->origin
&& !flow_cache_uli_match(first
->origin
, fl
))
2065 if (first
->partner
&&
2066 !xfrm_selector_match(first
->partner
, fl
, family
))
2074 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2076 if (fl
&& !xfrm_selector_match(&dst
->xfrm
->sel
, fl
, family
))
2079 !security_xfrm_state_pol_flow_match(dst
->xfrm
, pol
, fl
))
2081 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2083 if (xdst
->genid
!= dst
->xfrm
->genid
)
2087 !(dst
->xfrm
->outer_mode
->flags
& XFRM_MODE_FLAG_TUNNEL
) &&
2088 !xfrm_state_addr_flow_check(dst
->xfrm
, fl
, family
))
2091 mtu
= dst_mtu(dst
->child
);
2092 if (xdst
->child_mtu_cached
!= mtu
) {
2094 xdst
->child_mtu_cached
= mtu
;
2097 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2099 mtu
= dst_mtu(xdst
->route
);
2100 if (xdst
->route_mtu_cached
!= mtu
) {
2102 xdst
->route_mtu_cached
= mtu
;
2106 } while (dst
->xfrm
);
2111 mtu
= last
->child_mtu_cached
;
2115 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2116 if (mtu
> last
->route_mtu_cached
)
2117 mtu
= last
->route_mtu_cached
;
2118 dst
->metrics
[RTAX_MTU
-1] = mtu
;
2123 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2124 last
->child_mtu_cached
= mtu
;
2130 EXPORT_SYMBOL(xfrm_bundle_ok
);
2132 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2135 if (unlikely(afinfo
== NULL
))
2137 if (unlikely(afinfo
->family
>= NPROTO
))
2138 return -EAFNOSUPPORT
;
2139 write_lock_bh(&xfrm_policy_afinfo_lock
);
2140 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
2143 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2144 if (likely(dst_ops
->kmem_cachep
== NULL
))
2145 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2146 if (likely(dst_ops
->check
== NULL
))
2147 dst_ops
->check
= xfrm_dst_check
;
2148 if (likely(dst_ops
->negative_advice
== NULL
))
2149 dst_ops
->negative_advice
= xfrm_negative_advice
;
2150 if (likely(dst_ops
->link_failure
== NULL
))
2151 dst_ops
->link_failure
= xfrm_link_failure
;
2152 if (likely(afinfo
->garbage_collect
== NULL
))
2153 afinfo
->garbage_collect
= __xfrm_garbage_collect
;
2154 xfrm_policy_afinfo
[afinfo
->family
] = afinfo
;
2156 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2159 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2161 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2164 if (unlikely(afinfo
== NULL
))
2166 if (unlikely(afinfo
->family
>= NPROTO
))
2167 return -EAFNOSUPPORT
;
2168 write_lock_bh(&xfrm_policy_afinfo_lock
);
2169 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
2170 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
2173 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2174 xfrm_policy_afinfo
[afinfo
->family
] = NULL
;
2175 dst_ops
->kmem_cachep
= NULL
;
2176 dst_ops
->check
= NULL
;
2177 dst_ops
->negative_advice
= NULL
;
2178 dst_ops
->link_failure
= NULL
;
2179 afinfo
->garbage_collect
= NULL
;
2182 write_unlock_bh(&xfrm_policy_afinfo_lock
);
2185 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2187 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
2189 struct xfrm_policy_afinfo
*afinfo
;
2190 if (unlikely(family
>= NPROTO
))
2192 read_lock(&xfrm_policy_afinfo_lock
);
2193 afinfo
= xfrm_policy_afinfo
[family
];
2194 if (unlikely(!afinfo
))
2195 read_unlock(&xfrm_policy_afinfo_lock
);
2199 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2201 read_unlock(&xfrm_policy_afinfo_lock
);
2204 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
2206 struct net_device
*dev
= ptr
;
2208 if (dev
->nd_net
!= &init_net
)
2213 xfrm_flush_bundles();
2218 static struct notifier_block xfrm_dev_notifier
= {
2224 static void __init
xfrm_policy_init(void)
2226 unsigned int hmask
, sz
;
2229 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2230 sizeof(struct xfrm_dst
),
2231 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2235 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2237 xfrm_policy_byidx
= xfrm_hash_alloc(sz
);
2238 xfrm_idx_hmask
= hmask
;
2239 if (!xfrm_policy_byidx
)
2240 panic("XFRM: failed to allocate byidx hash\n");
2242 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2243 struct xfrm_policy_hash
*htab
;
2245 INIT_HLIST_HEAD(&xfrm_policy_inexact
[dir
]);
2247 htab
= &xfrm_policy_bydst
[dir
];
2248 htab
->table
= xfrm_hash_alloc(sz
);
2249 htab
->hmask
= hmask
;
2251 panic("XFRM: failed to allocate bydst hash\n");
2254 INIT_WORK(&xfrm_policy_gc_work
, xfrm_policy_gc_task
);
2255 register_netdevice_notifier(&xfrm_dev_notifier
);
2258 void __init
xfrm_init(void)
2265 #ifdef CONFIG_AUDITSYSCALL
2266 static inline void xfrm_audit_common_policyinfo(struct xfrm_policy
*xp
,
2267 struct audit_buffer
*audit_buf
)
2270 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2271 xp
->security
->ctx_alg
, xp
->security
->ctx_doi
,
2272 xp
->security
->ctx_str
);
2274 switch(xp
->selector
.family
) {
2276 audit_log_format(audit_buf
, " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
2277 NIPQUAD(xp
->selector
.saddr
.a4
),
2278 NIPQUAD(xp
->selector
.daddr
.a4
));
2282 struct in6_addr saddr6
, daddr6
;
2284 memcpy(&saddr6
, xp
->selector
.saddr
.a6
,
2285 sizeof(struct in6_addr
));
2286 memcpy(&daddr6
, xp
->selector
.daddr
.a6
,
2287 sizeof(struct in6_addr
));
2288 audit_log_format(audit_buf
,
2289 " src=" NIP6_FMT
" dst=" NIP6_FMT
,
2290 NIP6(saddr6
), NIP6(daddr6
));
2297 xfrm_audit_policy_add(struct xfrm_policy
*xp
, int result
, u32 auid
, u32 sid
)
2299 struct audit_buffer
*audit_buf
;
2300 extern int audit_enabled
;
2302 if (audit_enabled
== 0)
2304 audit_buf
= xfrm_audit_start(auid
, sid
);
2305 if (audit_buf
== NULL
)
2307 audit_log_format(audit_buf
, " op=SPD-add res=%u", result
);
2308 xfrm_audit_common_policyinfo(xp
, audit_buf
);
2309 audit_log_end(audit_buf
);
2311 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add
);
2314 xfrm_audit_policy_delete(struct xfrm_policy
*xp
, int result
, u32 auid
, u32 sid
)
2316 struct audit_buffer
*audit_buf
;
2317 extern int audit_enabled
;
2319 if (audit_enabled
== 0)
2321 audit_buf
= xfrm_audit_start(auid
, sid
);
2322 if (audit_buf
== NULL
)
2324 audit_log_format(audit_buf
, " op=SPD-delete res=%u", result
);
2325 xfrm_audit_common_policyinfo(xp
, audit_buf
);
2326 audit_log_end(audit_buf
);
2328 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete
);
2331 #ifdef CONFIG_XFRM_MIGRATE
2332 static int xfrm_migrate_selector_match(struct xfrm_selector
*sel_cmp
,
2333 struct xfrm_selector
*sel_tgt
)
2335 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
2336 if (sel_tgt
->family
== sel_cmp
->family
&&
2337 xfrm_addr_cmp(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
2338 sel_cmp
->family
) == 0 &&
2339 xfrm_addr_cmp(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
2340 sel_cmp
->family
) == 0 &&
2341 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
2342 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
2346 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
2353 static struct xfrm_policy
* xfrm_migrate_policy_find(struct xfrm_selector
*sel
,
2356 struct xfrm_policy
*pol
, *ret
= NULL
;
2357 struct hlist_node
*entry
;
2358 struct hlist_head
*chain
;
2361 read_lock_bh(&xfrm_policy_lock
);
2362 chain
= policy_hash_direct(&sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
2363 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
2364 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2365 pol
->type
== type
) {
2367 priority
= ret
->priority
;
2371 chain
= &xfrm_policy_inexact
[dir
];
2372 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
2373 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2374 pol
->type
== type
&&
2375 pol
->priority
< priority
) {
2384 read_unlock_bh(&xfrm_policy_lock
);
2389 static int migrate_tmpl_match(struct xfrm_migrate
*m
, struct xfrm_tmpl
*t
)
2393 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
2394 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
2396 case XFRM_MODE_TUNNEL
:
2397 case XFRM_MODE_BEET
:
2398 if (xfrm_addr_cmp(&t
->id
.daddr
, &m
->old_daddr
,
2399 m
->old_family
) == 0 &&
2400 xfrm_addr_cmp(&t
->saddr
, &m
->old_saddr
,
2401 m
->old_family
) == 0) {
2405 case XFRM_MODE_TRANSPORT
:
2406 /* in case of transport mode, template does not store
2407 any IP addresses, hence we just compare mode and
2418 /* update endpoint address(es) of template(s) */
2419 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
2420 struct xfrm_migrate
*m
, int num_migrate
)
2422 struct xfrm_migrate
*mp
;
2423 struct dst_entry
*dst
;
2426 write_lock_bh(&pol
->lock
);
2427 if (unlikely(pol
->dead
)) {
2428 /* target policy has been deleted */
2429 write_unlock_bh(&pol
->lock
);
2433 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
2434 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
2435 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
2438 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
&&
2439 pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_BEET
)
2441 /* update endpoints */
2442 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
2443 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
2444 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
2445 sizeof(pol
->xfrm_vec
[i
].saddr
));
2446 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
2448 while ((dst
= pol
->bundles
) != NULL
) {
2449 pol
->bundles
= dst
->next
;
2455 write_unlock_bh(&pol
->lock
);
2463 static int xfrm_migrate_check(struct xfrm_migrate
*m
, int num_migrate
)
2467 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
2470 for (i
= 0; i
< num_migrate
; i
++) {
2471 if ((xfrm_addr_cmp(&m
[i
].old_daddr
, &m
[i
].new_daddr
,
2472 m
[i
].old_family
) == 0) &&
2473 (xfrm_addr_cmp(&m
[i
].old_saddr
, &m
[i
].new_saddr
,
2474 m
[i
].old_family
) == 0))
2476 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
2477 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
2480 /* check if there is any duplicated entry */
2481 for (j
= i
+ 1; j
< num_migrate
; j
++) {
2482 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
2483 sizeof(m
[i
].old_daddr
)) &&
2484 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
2485 sizeof(m
[i
].old_saddr
)) &&
2486 m
[i
].proto
== m
[j
].proto
&&
2487 m
[i
].mode
== m
[j
].mode
&&
2488 m
[i
].reqid
== m
[j
].reqid
&&
2489 m
[i
].old_family
== m
[j
].old_family
)
2497 int xfrm_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2498 struct xfrm_migrate
*m
, int num_migrate
)
2500 int i
, err
, nx_cur
= 0, nx_new
= 0;
2501 struct xfrm_policy
*pol
= NULL
;
2502 struct xfrm_state
*x
, *xc
;
2503 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
2504 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
2505 struct xfrm_migrate
*mp
;
2507 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
2510 /* Stage 1 - find policy */
2511 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
)) == NULL
) {
2516 /* Stage 2 - find and update state(s) */
2517 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2518 if ((x
= xfrm_migrate_state_find(mp
))) {
2521 if ((xc
= xfrm_state_migrate(x
, mp
))) {
2531 /* Stage 3 - update policy */
2532 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
2535 /* Stage 4 - delete old state(s) */
2537 xfrm_states_put(x_cur
, nx_cur
);
2538 xfrm_states_delete(x_cur
, nx_cur
);
2541 /* Stage 5 - announce */
2542 km_migrate(sel
, dir
, type
, m
, num_migrate
);
2554 xfrm_states_put(x_cur
, nx_cur
);
2556 xfrm_states_delete(x_new
, nx_new
);
2560 EXPORT_SYMBOL(xfrm_migrate
);