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/cpu.h>
28 #include <linux/audit.h>
33 #ifdef CONFIG_XFRM_STATISTICS
37 #include "xfrm_hash.h"
39 #define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
40 #define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
41 #define XFRM_MAX_QUEUE_LEN 100
44 struct dst_entry
*dst_orig
;
48 static DEFINE_PER_CPU(struct xfrm_dst
*, xfrm_last_dst
);
49 static struct work_struct
*xfrm_pcpu_work __read_mostly
;
50 static DEFINE_SPINLOCK(xfrm_if_cb_lock
);
51 static struct xfrm_if_cb
const __rcu
*xfrm_if_cb __read_mostly
;
53 static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock
);
54 static struct xfrm_policy_afinfo
const __rcu
*xfrm_policy_afinfo
[AF_INET6
+ 1]
57 static struct kmem_cache
*xfrm_dst_cache __read_mostly
;
58 static __read_mostly seqcount_t xfrm_policy_hash_generation
;
60 static void xfrm_init_pmtu(struct dst_entry
*dst
);
61 static int stale_bundle(struct dst_entry
*dst
);
62 static int xfrm_bundle_ok(struct xfrm_dst
*xdst
);
63 static void xfrm_policy_queue_process(unsigned long arg
);
65 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
);
66 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
69 static inline bool xfrm_pol_hold_rcu(struct xfrm_policy
*policy
)
71 return refcount_inc_not_zero(&policy
->refcnt
);
75 __xfrm4_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
77 const struct flowi4
*fl4
= &fl
->u
.ip4
;
79 return addr4_match(fl4
->daddr
, sel
->daddr
.a4
, sel
->prefixlen_d
) &&
80 addr4_match(fl4
->saddr
, sel
->saddr
.a4
, sel
->prefixlen_s
) &&
81 !((xfrm_flowi_dport(fl
, &fl4
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
82 !((xfrm_flowi_sport(fl
, &fl4
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
83 (fl4
->flowi4_proto
== sel
->proto
|| !sel
->proto
) &&
84 (fl4
->flowi4_oif
== sel
->ifindex
|| !sel
->ifindex
);
88 __xfrm6_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
90 const struct flowi6
*fl6
= &fl
->u
.ip6
;
92 return addr_match(&fl6
->daddr
, &sel
->daddr
, sel
->prefixlen_d
) &&
93 addr_match(&fl6
->saddr
, &sel
->saddr
, sel
->prefixlen_s
) &&
94 !((xfrm_flowi_dport(fl
, &fl6
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
95 !((xfrm_flowi_sport(fl
, &fl6
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
96 (fl6
->flowi6_proto
== sel
->proto
|| !sel
->proto
) &&
97 (fl6
->flowi6_oif
== sel
->ifindex
|| !sel
->ifindex
);
100 bool xfrm_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
,
101 unsigned short family
)
105 return __xfrm4_selector_match(sel
, fl
);
107 return __xfrm6_selector_match(sel
, fl
);
112 static const struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
114 const struct xfrm_policy_afinfo
*afinfo
;
116 if (unlikely(family
>= ARRAY_SIZE(xfrm_policy_afinfo
)))
119 afinfo
= rcu_dereference(xfrm_policy_afinfo
[family
]);
120 if (unlikely(!afinfo
))
125 /* Called with rcu_read_lock(). */
126 static const struct xfrm_if_cb
*xfrm_if_get_cb(void)
128 return rcu_dereference(xfrm_if_cb
);
131 struct dst_entry
*__xfrm_dst_lookup(struct net
*net
, int tos
, int oif
,
132 const xfrm_address_t
*saddr
,
133 const xfrm_address_t
*daddr
,
134 int family
, u32 mark
)
136 const struct xfrm_policy_afinfo
*afinfo
;
137 struct dst_entry
*dst
;
139 afinfo
= xfrm_policy_get_afinfo(family
);
140 if (unlikely(afinfo
== NULL
))
141 return ERR_PTR(-EAFNOSUPPORT
);
143 dst
= afinfo
->dst_lookup(net
, tos
, oif
, saddr
, daddr
, mark
);
149 EXPORT_SYMBOL(__xfrm_dst_lookup
);
151 static inline struct dst_entry
*xfrm_dst_lookup(struct xfrm_state
*x
,
153 xfrm_address_t
*prev_saddr
,
154 xfrm_address_t
*prev_daddr
,
155 int family
, u32 mark
)
157 struct net
*net
= xs_net(x
);
158 xfrm_address_t
*saddr
= &x
->props
.saddr
;
159 xfrm_address_t
*daddr
= &x
->id
.daddr
;
160 struct dst_entry
*dst
;
162 if (x
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
) {
166 if (x
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
) {
171 dst
= __xfrm_dst_lookup(net
, tos
, oif
, saddr
, daddr
, family
, mark
);
174 if (prev_saddr
!= saddr
)
175 memcpy(prev_saddr
, saddr
, sizeof(*prev_saddr
));
176 if (prev_daddr
!= daddr
)
177 memcpy(prev_daddr
, daddr
, sizeof(*prev_daddr
));
183 static inline unsigned long make_jiffies(long secs
)
185 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
186 return MAX_SCHEDULE_TIMEOUT
-1;
191 static void xfrm_policy_timer(unsigned long data
)
193 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
194 unsigned long now
= get_seconds();
195 long next
= LONG_MAX
;
199 read_lock(&xp
->lock
);
201 if (unlikely(xp
->walk
.dead
))
204 dir
= xfrm_policy_id2dir(xp
->index
);
206 if (xp
->lft
.hard_add_expires_seconds
) {
207 long tmo
= xp
->lft
.hard_add_expires_seconds
+
208 xp
->curlft
.add_time
- now
;
214 if (xp
->lft
.hard_use_expires_seconds
) {
215 long tmo
= xp
->lft
.hard_use_expires_seconds
+
216 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
222 if (xp
->lft
.soft_add_expires_seconds
) {
223 long tmo
= xp
->lft
.soft_add_expires_seconds
+
224 xp
->curlft
.add_time
- now
;
227 tmo
= XFRM_KM_TIMEOUT
;
232 if (xp
->lft
.soft_use_expires_seconds
) {
233 long tmo
= xp
->lft
.soft_use_expires_seconds
+
234 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
237 tmo
= XFRM_KM_TIMEOUT
;
244 km_policy_expired(xp
, dir
, 0, 0);
245 if (next
!= LONG_MAX
&&
246 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
250 read_unlock(&xp
->lock
);
255 read_unlock(&xp
->lock
);
256 if (!xfrm_policy_delete(xp
, dir
))
257 km_policy_expired(xp
, dir
, 1, 0);
261 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
265 struct xfrm_policy
*xfrm_policy_alloc(struct net
*net
, gfp_t gfp
)
267 struct xfrm_policy
*policy
;
269 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
272 write_pnet(&policy
->xp_net
, net
);
273 INIT_LIST_HEAD(&policy
->walk
.all
);
274 INIT_HLIST_NODE(&policy
->bydst
);
275 INIT_HLIST_NODE(&policy
->byidx
);
276 rwlock_init(&policy
->lock
);
277 refcount_set(&policy
->refcnt
, 1);
278 skb_queue_head_init(&policy
->polq
.hold_queue
);
279 setup_timer(&policy
->timer
, xfrm_policy_timer
,
280 (unsigned long)policy
);
281 setup_timer(&policy
->polq
.hold_timer
, xfrm_policy_queue_process
,
282 (unsigned long)policy
);
286 EXPORT_SYMBOL(xfrm_policy_alloc
);
288 static void xfrm_policy_destroy_rcu(struct rcu_head
*head
)
290 struct xfrm_policy
*policy
= container_of(head
, struct xfrm_policy
, rcu
);
292 security_xfrm_policy_free(policy
->security
);
296 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
298 void xfrm_policy_destroy(struct xfrm_policy
*policy
)
300 BUG_ON(!policy
->walk
.dead
);
302 if (del_timer(&policy
->timer
) || del_timer(&policy
->polq
.hold_timer
))
305 call_rcu(&policy
->rcu
, xfrm_policy_destroy_rcu
);
307 EXPORT_SYMBOL(xfrm_policy_destroy
);
309 /* Rule must be locked. Release descendant resources, announce
310 * entry dead. The rule must be unlinked from lists to the moment.
313 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
315 policy
->walk
.dead
= 1;
317 atomic_inc(&policy
->genid
);
319 if (del_timer(&policy
->polq
.hold_timer
))
320 xfrm_pol_put(policy
);
321 skb_queue_purge(&policy
->polq
.hold_queue
);
323 if (del_timer(&policy
->timer
))
324 xfrm_pol_put(policy
);
326 xfrm_pol_put(policy
);
329 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
331 static inline unsigned int idx_hash(struct net
*net
, u32 index
)
333 return __idx_hash(index
, net
->xfrm
.policy_idx_hmask
);
336 /* calculate policy hash thresholds */
337 static void __get_hash_thresh(struct net
*net
,
338 unsigned short family
, int dir
,
339 u8
*dbits
, u8
*sbits
)
343 *dbits
= net
->xfrm
.policy_bydst
[dir
].dbits4
;
344 *sbits
= net
->xfrm
.policy_bydst
[dir
].sbits4
;
348 *dbits
= net
->xfrm
.policy_bydst
[dir
].dbits6
;
349 *sbits
= net
->xfrm
.policy_bydst
[dir
].sbits6
;
358 static struct hlist_head
*policy_hash_bysel(struct net
*net
,
359 const struct xfrm_selector
*sel
,
360 unsigned short family
, int dir
)
362 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
367 __get_hash_thresh(net
, family
, dir
, &dbits
, &sbits
);
368 hash
= __sel_hash(sel
, family
, hmask
, dbits
, sbits
);
370 if (hash
== hmask
+ 1)
371 return &net
->xfrm
.policy_inexact
[dir
];
373 return rcu_dereference_check(net
->xfrm
.policy_bydst
[dir
].table
,
374 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
)) + hash
;
377 static struct hlist_head
*policy_hash_direct(struct net
*net
,
378 const xfrm_address_t
*daddr
,
379 const xfrm_address_t
*saddr
,
380 unsigned short family
, int dir
)
382 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
387 __get_hash_thresh(net
, family
, dir
, &dbits
, &sbits
);
388 hash
= __addr_hash(daddr
, saddr
, family
, hmask
, dbits
, sbits
);
390 return rcu_dereference_check(net
->xfrm
.policy_bydst
[dir
].table
,
391 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
)) + hash
;
394 static void xfrm_dst_hash_transfer(struct net
*net
,
395 struct hlist_head
*list
,
396 struct hlist_head
*ndsttable
,
397 unsigned int nhashmask
,
400 struct hlist_node
*tmp
, *entry0
= NULL
;
401 struct xfrm_policy
*pol
;
407 hlist_for_each_entry_safe(pol
, tmp
, list
, bydst
) {
410 __get_hash_thresh(net
, pol
->family
, dir
, &dbits
, &sbits
);
411 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
412 pol
->family
, nhashmask
, dbits
, sbits
);
414 hlist_del_rcu(&pol
->bydst
);
415 hlist_add_head_rcu(&pol
->bydst
, ndsttable
+ h
);
420 hlist_del_rcu(&pol
->bydst
);
421 hlist_add_behind_rcu(&pol
->bydst
, entry0
);
423 entry0
= &pol
->bydst
;
425 if (!hlist_empty(list
)) {
431 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
432 struct hlist_head
*nidxtable
,
433 unsigned int nhashmask
)
435 struct hlist_node
*tmp
;
436 struct xfrm_policy
*pol
;
438 hlist_for_each_entry_safe(pol
, tmp
, list
, byidx
) {
441 h
= __idx_hash(pol
->index
, nhashmask
);
442 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
446 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
448 return ((old_hmask
+ 1) << 1) - 1;
451 static void xfrm_bydst_resize(struct net
*net
, int dir
)
453 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
454 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
455 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
456 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
457 struct hlist_head
*odst
;
463 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
464 write_seqcount_begin(&xfrm_policy_hash_generation
);
466 odst
= rcu_dereference_protected(net
->xfrm
.policy_bydst
[dir
].table
,
467 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
469 odst
= rcu_dereference_protected(net
->xfrm
.policy_bydst
[dir
].table
,
470 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
472 for (i
= hmask
; i
>= 0; i
--)
473 xfrm_dst_hash_transfer(net
, odst
+ i
, ndst
, nhashmask
, dir
);
475 rcu_assign_pointer(net
->xfrm
.policy_bydst
[dir
].table
, ndst
);
476 net
->xfrm
.policy_bydst
[dir
].hmask
= nhashmask
;
478 write_seqcount_end(&xfrm_policy_hash_generation
);
479 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
483 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
486 static void xfrm_byidx_resize(struct net
*net
, int total
)
488 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
489 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
490 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
491 struct hlist_head
*oidx
= net
->xfrm
.policy_byidx
;
492 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
498 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
500 for (i
= hmask
; i
>= 0; i
--)
501 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
503 net
->xfrm
.policy_byidx
= nidx
;
504 net
->xfrm
.policy_idx_hmask
= nhashmask
;
506 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
508 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
511 static inline int xfrm_bydst_should_resize(struct net
*net
, int dir
, int *total
)
513 unsigned int cnt
= net
->xfrm
.policy_count
[dir
];
514 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
519 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
526 static inline int xfrm_byidx_should_resize(struct net
*net
, int total
)
528 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
530 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
537 void xfrm_spd_getinfo(struct net
*net
, struct xfrmk_spdinfo
*si
)
539 si
->incnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
];
540 si
->outcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
];
541 si
->fwdcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
];
542 si
->inscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
543 si
->outscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
544 si
->fwdscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
545 si
->spdhcnt
= net
->xfrm
.policy_idx_hmask
;
546 si
->spdhmcnt
= xfrm_policy_hashmax
;
548 EXPORT_SYMBOL(xfrm_spd_getinfo
);
550 static DEFINE_MUTEX(hash_resize_mutex
);
551 static void xfrm_hash_resize(struct work_struct
*work
)
553 struct net
*net
= container_of(work
, struct net
, xfrm
.policy_hash_work
);
556 mutex_lock(&hash_resize_mutex
);
559 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
560 if (xfrm_bydst_should_resize(net
, dir
, &total
))
561 xfrm_bydst_resize(net
, dir
);
563 if (xfrm_byidx_should_resize(net
, total
))
564 xfrm_byidx_resize(net
, total
);
566 mutex_unlock(&hash_resize_mutex
);
569 static void xfrm_hash_rebuild(struct work_struct
*work
)
571 struct net
*net
= container_of(work
, struct net
,
572 xfrm
.policy_hthresh
.work
);
574 struct xfrm_policy
*pol
;
575 struct xfrm_policy
*policy
;
576 struct hlist_head
*chain
;
577 struct hlist_head
*odst
;
578 struct hlist_node
*newpos
;
582 u8 lbits4
, rbits4
, lbits6
, rbits6
;
584 mutex_lock(&hash_resize_mutex
);
586 /* read selector prefixlen thresholds */
588 seq
= read_seqbegin(&net
->xfrm
.policy_hthresh
.lock
);
590 lbits4
= net
->xfrm
.policy_hthresh
.lbits4
;
591 rbits4
= net
->xfrm
.policy_hthresh
.rbits4
;
592 lbits6
= net
->xfrm
.policy_hthresh
.lbits6
;
593 rbits6
= net
->xfrm
.policy_hthresh
.rbits6
;
594 } while (read_seqretry(&net
->xfrm
.policy_hthresh
.lock
, seq
));
596 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
598 /* reset the bydst and inexact table in all directions */
599 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
600 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
601 hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
602 odst
= net
->xfrm
.policy_bydst
[dir
].table
;
603 for (i
= hmask
; i
>= 0; i
--)
604 INIT_HLIST_HEAD(odst
+ i
);
605 if ((dir
& XFRM_POLICY_MASK
) == XFRM_POLICY_OUT
) {
606 /* dir out => dst = remote, src = local */
607 net
->xfrm
.policy_bydst
[dir
].dbits4
= rbits4
;
608 net
->xfrm
.policy_bydst
[dir
].sbits4
= lbits4
;
609 net
->xfrm
.policy_bydst
[dir
].dbits6
= rbits6
;
610 net
->xfrm
.policy_bydst
[dir
].sbits6
= lbits6
;
612 /* dir in/fwd => dst = local, src = remote */
613 net
->xfrm
.policy_bydst
[dir
].dbits4
= lbits4
;
614 net
->xfrm
.policy_bydst
[dir
].sbits4
= rbits4
;
615 net
->xfrm
.policy_bydst
[dir
].dbits6
= lbits6
;
616 net
->xfrm
.policy_bydst
[dir
].sbits6
= rbits6
;
620 /* re-insert all policies by order of creation */
621 list_for_each_entry_reverse(policy
, &net
->xfrm
.policy_all
, walk
.all
) {
622 if (policy
->walk
.dead
||
623 xfrm_policy_id2dir(policy
->index
) >= XFRM_POLICY_MAX
) {
624 /* skip socket policies */
628 chain
= policy_hash_bysel(net
, &policy
->selector
,
630 xfrm_policy_id2dir(policy
->index
));
631 hlist_for_each_entry(pol
, chain
, bydst
) {
632 if (policy
->priority
>= pol
->priority
)
633 newpos
= &pol
->bydst
;
638 hlist_add_behind_rcu(&policy
->bydst
, newpos
);
640 hlist_add_head_rcu(&policy
->bydst
, chain
);
643 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
645 mutex_unlock(&hash_resize_mutex
);
648 void xfrm_policy_hash_rebuild(struct net
*net
)
650 schedule_work(&net
->xfrm
.policy_hthresh
.work
);
652 EXPORT_SYMBOL(xfrm_policy_hash_rebuild
);
654 /* Generate new index... KAME seems to generate them ordered by cost
655 * of an absolute inpredictability of ordering of rules. This will not pass. */
656 static u32
xfrm_gen_index(struct net
*net
, int dir
, u32 index
)
658 static u32 idx_generator
;
661 struct hlist_head
*list
;
662 struct xfrm_policy
*p
;
667 idx
= (idx_generator
| dir
);
676 list
= net
->xfrm
.policy_byidx
+ idx_hash(net
, idx
);
678 hlist_for_each_entry(p
, list
, byidx
) {
679 if (p
->index
== idx
) {
689 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
691 u32
*p1
= (u32
*) s1
;
692 u32
*p2
= (u32
*) s2
;
693 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
696 for (i
= 0; i
< len
; i
++) {
704 static void xfrm_policy_requeue(struct xfrm_policy
*old
,
705 struct xfrm_policy
*new)
707 struct xfrm_policy_queue
*pq
= &old
->polq
;
708 struct sk_buff_head list
;
710 if (skb_queue_empty(&pq
->hold_queue
))
713 __skb_queue_head_init(&list
);
715 spin_lock_bh(&pq
->hold_queue
.lock
);
716 skb_queue_splice_init(&pq
->hold_queue
, &list
);
717 if (del_timer(&pq
->hold_timer
))
719 spin_unlock_bh(&pq
->hold_queue
.lock
);
723 spin_lock_bh(&pq
->hold_queue
.lock
);
724 skb_queue_splice(&list
, &pq
->hold_queue
);
725 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
726 if (!mod_timer(&pq
->hold_timer
, jiffies
))
728 spin_unlock_bh(&pq
->hold_queue
.lock
);
731 static bool xfrm_policy_mark_match(struct xfrm_policy
*policy
,
732 struct xfrm_policy
*pol
)
734 u32 mark
= policy
->mark
.v
& policy
->mark
.m
;
736 if (policy
->mark
.v
== pol
->mark
.v
&& policy
->mark
.m
== pol
->mark
.m
)
739 if ((mark
& pol
->mark
.m
) == pol
->mark
.v
&&
740 policy
->priority
== pol
->priority
)
746 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
748 struct net
*net
= xp_net(policy
);
749 struct xfrm_policy
*pol
;
750 struct xfrm_policy
*delpol
;
751 struct hlist_head
*chain
;
752 struct hlist_node
*newpos
;
754 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
755 chain
= policy_hash_bysel(net
, &policy
->selector
, policy
->family
, dir
);
758 hlist_for_each_entry(pol
, chain
, bydst
) {
759 if (pol
->type
== policy
->type
&&
760 pol
->if_id
== policy
->if_id
&&
761 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
762 xfrm_policy_mark_match(policy
, pol
) &&
763 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
766 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
770 if (policy
->priority
> pol
->priority
)
772 } else if (policy
->priority
>= pol
->priority
) {
773 newpos
= &pol
->bydst
;
780 hlist_add_behind_rcu(&policy
->bydst
, newpos
);
782 hlist_add_head_rcu(&policy
->bydst
, chain
);
783 __xfrm_policy_link(policy
, dir
);
785 /* After previous checking, family can either be AF_INET or AF_INET6 */
786 if (policy
->family
== AF_INET
)
787 rt_genid_bump_ipv4(net
);
789 rt_genid_bump_ipv6(net
);
792 xfrm_policy_requeue(delpol
, policy
);
793 __xfrm_policy_unlink(delpol
, dir
);
795 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(net
, dir
, policy
->index
);
796 hlist_add_head(&policy
->byidx
, net
->xfrm
.policy_byidx
+idx_hash(net
, policy
->index
));
797 policy
->curlft
.add_time
= get_seconds();
798 policy
->curlft
.use_time
= 0;
799 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
800 xfrm_pol_hold(policy
);
801 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
804 xfrm_policy_kill(delpol
);
805 else if (xfrm_bydst_should_resize(net
, dir
, NULL
))
806 schedule_work(&net
->xfrm
.policy_hash_work
);
810 EXPORT_SYMBOL(xfrm_policy_insert
);
812 struct xfrm_policy
*xfrm_policy_bysel_ctx(struct net
*net
, u32 mark
, u32 if_id
,
814 struct xfrm_selector
*sel
,
815 struct xfrm_sec_ctx
*ctx
, int delete,
818 struct xfrm_policy
*pol
, *ret
;
819 struct hlist_head
*chain
;
822 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
823 chain
= policy_hash_bysel(net
, sel
, sel
->family
, dir
);
825 hlist_for_each_entry(pol
, chain
, bydst
) {
826 if (pol
->type
== type
&&
827 pol
->if_id
== if_id
&&
828 (mark
& pol
->mark
.m
) == pol
->mark
.v
&&
829 !selector_cmp(sel
, &pol
->selector
) &&
830 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
833 *err
= security_xfrm_policy_delete(
836 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
839 __xfrm_policy_unlink(pol
, dir
);
845 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
848 xfrm_policy_kill(ret
);
851 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
853 struct xfrm_policy
*xfrm_policy_byid(struct net
*net
, u32 mark
, u32 if_id
,
854 u8 type
, int dir
, u32 id
, int delete,
857 struct xfrm_policy
*pol
, *ret
;
858 struct hlist_head
*chain
;
861 if (xfrm_policy_id2dir(id
) != dir
)
865 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
866 chain
= net
->xfrm
.policy_byidx
+ idx_hash(net
, id
);
868 hlist_for_each_entry(pol
, chain
, byidx
) {
869 if (pol
->type
== type
&& pol
->index
== id
&&
870 pol
->if_id
== if_id
&&
871 (mark
& pol
->mark
.m
) == pol
->mark
.v
) {
874 *err
= security_xfrm_policy_delete(
877 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
880 __xfrm_policy_unlink(pol
, dir
);
886 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
889 xfrm_policy_kill(ret
);
892 EXPORT_SYMBOL(xfrm_policy_byid
);
894 #ifdef CONFIG_SECURITY_NETWORK_XFRM
896 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
900 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
901 struct xfrm_policy
*pol
;
904 hlist_for_each_entry(pol
,
905 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
906 if (pol
->type
!= type
)
908 err
= security_xfrm_policy_delete(pol
->security
);
910 xfrm_audit_policy_delete(pol
, 0, task_valid
);
914 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
915 hlist_for_each_entry(pol
,
916 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
918 if (pol
->type
!= type
)
920 err
= security_xfrm_policy_delete(
923 xfrm_audit_policy_delete(pol
, 0,
934 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
940 int xfrm_policy_flush(struct net
*net
, u8 type
, bool task_valid
)
942 int dir
, err
= 0, cnt
= 0;
944 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
946 err
= xfrm_policy_flush_secctx_check(net
, type
, task_valid
);
950 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
951 struct xfrm_policy
*pol
;
955 hlist_for_each_entry(pol
,
956 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
957 if (pol
->type
!= type
)
959 __xfrm_policy_unlink(pol
, dir
);
960 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
963 xfrm_audit_policy_delete(pol
, 1, task_valid
);
965 xfrm_policy_kill(pol
);
967 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
971 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
973 hlist_for_each_entry(pol
,
974 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
976 if (pol
->type
!= type
)
978 __xfrm_policy_unlink(pol
, dir
);
979 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
982 xfrm_audit_policy_delete(pol
, 1, task_valid
);
983 xfrm_policy_kill(pol
);
985 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
994 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
997 EXPORT_SYMBOL(xfrm_policy_flush
);
999 int xfrm_policy_walk(struct net
*net
, struct xfrm_policy_walk
*walk
,
1000 int (*func
)(struct xfrm_policy
*, int, int, void*),
1003 struct xfrm_policy
*pol
;
1004 struct xfrm_policy_walk_entry
*x
;
1007 if (walk
->type
>= XFRM_POLICY_TYPE_MAX
&&
1008 walk
->type
!= XFRM_POLICY_TYPE_ANY
)
1011 if (list_empty(&walk
->walk
.all
) && walk
->seq
!= 0)
1014 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1015 if (list_empty(&walk
->walk
.all
))
1016 x
= list_first_entry(&net
->xfrm
.policy_all
, struct xfrm_policy_walk_entry
, all
);
1018 x
= list_first_entry(&walk
->walk
.all
,
1019 struct xfrm_policy_walk_entry
, all
);
1021 list_for_each_entry_from(x
, &net
->xfrm
.policy_all
, all
) {
1024 pol
= container_of(x
, struct xfrm_policy
, walk
);
1025 if (walk
->type
!= XFRM_POLICY_TYPE_ANY
&&
1026 walk
->type
!= pol
->type
)
1028 error
= func(pol
, xfrm_policy_id2dir(pol
->index
),
1031 list_move_tail(&walk
->walk
.all
, &x
->all
);
1036 if (walk
->seq
== 0) {
1040 list_del_init(&walk
->walk
.all
);
1042 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1045 EXPORT_SYMBOL(xfrm_policy_walk
);
1047 void xfrm_policy_walk_init(struct xfrm_policy_walk
*walk
, u8 type
)
1049 INIT_LIST_HEAD(&walk
->walk
.all
);
1050 walk
->walk
.dead
= 1;
1054 EXPORT_SYMBOL(xfrm_policy_walk_init
);
1056 void xfrm_policy_walk_done(struct xfrm_policy_walk
*walk
, struct net
*net
)
1058 if (list_empty(&walk
->walk
.all
))
1061 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
); /*FIXME where is net? */
1062 list_del(&walk
->walk
.all
);
1063 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1065 EXPORT_SYMBOL(xfrm_policy_walk_done
);
1068 * Find policy to apply to this flow.
1070 * Returns 0 if policy found, else an -errno.
1072 static int xfrm_policy_match(const struct xfrm_policy
*pol
,
1073 const struct flowi
*fl
,
1074 u8 type
, u16 family
, int dir
, u32 if_id
)
1076 const struct xfrm_selector
*sel
= &pol
->selector
;
1080 if (pol
->family
!= family
||
1081 pol
->if_id
!= if_id
||
1082 (fl
->flowi_mark
& pol
->mark
.m
) != pol
->mark
.v
||
1086 match
= xfrm_selector_match(sel
, fl
, family
);
1088 ret
= security_xfrm_policy_lookup(pol
->security
, fl
->flowi_secid
,
1094 static struct xfrm_policy
*xfrm_policy_lookup_bytype(struct net
*net
, u8 type
,
1095 const struct flowi
*fl
,
1100 struct xfrm_policy
*pol
, *ret
;
1101 const xfrm_address_t
*daddr
, *saddr
;
1102 struct hlist_head
*chain
;
1103 unsigned int sequence
;
1106 daddr
= xfrm_flowi_daddr(fl
, family
);
1107 saddr
= xfrm_flowi_saddr(fl
, family
);
1108 if (unlikely(!daddr
|| !saddr
))
1114 sequence
= read_seqcount_begin(&xfrm_policy_hash_generation
);
1115 chain
= policy_hash_direct(net
, daddr
, saddr
, family
, dir
);
1116 } while (read_seqcount_retry(&xfrm_policy_hash_generation
, sequence
));
1120 hlist_for_each_entry_rcu(pol
, chain
, bydst
) {
1121 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
, if_id
);
1131 priority
= ret
->priority
;
1135 chain
= &net
->xfrm
.policy_inexact
[dir
];
1136 hlist_for_each_entry_rcu(pol
, chain
, bydst
) {
1137 if ((pol
->priority
>= priority
) && ret
)
1140 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
, if_id
);
1154 if (read_seqcount_retry(&xfrm_policy_hash_generation
, sequence
))
1157 if (ret
&& !xfrm_pol_hold_rcu(ret
))
1165 static struct xfrm_policy
*xfrm_policy_lookup(struct net
*net
,
1166 const struct flowi
*fl
,
1167 u16 family
, u8 dir
, u32 if_id
)
1169 #ifdef CONFIG_XFRM_SUB_POLICY
1170 struct xfrm_policy
*pol
;
1172 pol
= xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_SUB
, fl
, family
,
1177 return xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
, fl
, family
,
1181 static struct xfrm_policy
*xfrm_sk_policy_lookup(const struct sock
*sk
, int dir
,
1182 const struct flowi
*fl
,
1183 u16 family
, u32 if_id
)
1185 struct xfrm_policy
*pol
;
1189 pol
= rcu_dereference(sk
->sk_policy
[dir
]);
1194 if (pol
->family
!= family
) {
1199 match
= xfrm_selector_match(&pol
->selector
, fl
, family
);
1201 if ((sk
->sk_mark
& pol
->mark
.m
) != pol
->mark
.v
||
1202 pol
->if_id
!= if_id
) {
1206 err
= security_xfrm_policy_lookup(pol
->security
,
1210 if (!xfrm_pol_hold_rcu(pol
))
1212 } else if (err
== -ESRCH
) {
1225 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1227 struct net
*net
= xp_net(pol
);
1229 list_add(&pol
->walk
.all
, &net
->xfrm
.policy_all
);
1230 net
->xfrm
.policy_count
[dir
]++;
1234 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1237 struct net
*net
= xp_net(pol
);
1239 if (list_empty(&pol
->walk
.all
))
1242 /* Socket policies are not hashed. */
1243 if (!hlist_unhashed(&pol
->bydst
)) {
1244 hlist_del_rcu(&pol
->bydst
);
1245 hlist_del(&pol
->byidx
);
1248 list_del_init(&pol
->walk
.all
);
1249 net
->xfrm
.policy_count
[dir
]--;
1254 static void xfrm_sk_policy_link(struct xfrm_policy
*pol
, int dir
)
1256 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+ dir
);
1259 static void xfrm_sk_policy_unlink(struct xfrm_policy
*pol
, int dir
)
1261 __xfrm_policy_unlink(pol
, XFRM_POLICY_MAX
+ dir
);
1264 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1266 struct net
*net
= xp_net(pol
);
1268 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1269 pol
= __xfrm_policy_unlink(pol
, dir
);
1270 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1272 xfrm_policy_kill(pol
);
1277 EXPORT_SYMBOL(xfrm_policy_delete
);
1279 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1281 struct net
*net
= sock_net(sk
);
1282 struct xfrm_policy
*old_pol
;
1284 #ifdef CONFIG_XFRM_SUB_POLICY
1285 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1289 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1290 old_pol
= rcu_dereference_protected(sk
->sk_policy
[dir
],
1291 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
1293 pol
->curlft
.add_time
= get_seconds();
1294 pol
->index
= xfrm_gen_index(net
, XFRM_POLICY_MAX
+dir
, 0);
1295 xfrm_sk_policy_link(pol
, dir
);
1297 rcu_assign_pointer(sk
->sk_policy
[dir
], pol
);
1300 xfrm_policy_requeue(old_pol
, pol
);
1302 /* Unlinking succeeds always. This is the only function
1303 * allowed to delete or replace socket policy.
1305 xfrm_sk_policy_unlink(old_pol
, dir
);
1307 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1310 xfrm_policy_kill(old_pol
);
1315 static struct xfrm_policy
*clone_policy(const struct xfrm_policy
*old
, int dir
)
1317 struct xfrm_policy
*newp
= xfrm_policy_alloc(xp_net(old
), GFP_ATOMIC
);
1318 struct net
*net
= xp_net(old
);
1321 newp
->selector
= old
->selector
;
1322 if (security_xfrm_policy_clone(old
->security
,
1325 return NULL
; /* ENOMEM */
1327 newp
->lft
= old
->lft
;
1328 newp
->curlft
= old
->curlft
;
1329 newp
->mark
= old
->mark
;
1330 newp
->if_id
= old
->if_id
;
1331 newp
->action
= old
->action
;
1332 newp
->flags
= old
->flags
;
1333 newp
->xfrm_nr
= old
->xfrm_nr
;
1334 newp
->index
= old
->index
;
1335 newp
->type
= old
->type
;
1336 newp
->family
= old
->family
;
1337 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1338 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1339 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1340 xfrm_sk_policy_link(newp
, dir
);
1341 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1347 int __xfrm_sk_clone_policy(struct sock
*sk
, const struct sock
*osk
)
1349 const struct xfrm_policy
*p
;
1350 struct xfrm_policy
*np
;
1354 for (i
= 0; i
< 2; i
++) {
1355 p
= rcu_dereference(osk
->sk_policy
[i
]);
1357 np
= clone_policy(p
, i
);
1358 if (unlikely(!np
)) {
1362 rcu_assign_pointer(sk
->sk_policy
[i
], np
);
1370 xfrm_get_saddr(struct net
*net
, int oif
, xfrm_address_t
*local
,
1371 xfrm_address_t
*remote
, unsigned short family
, u32 mark
)
1374 const struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1376 if (unlikely(afinfo
== NULL
))
1378 err
= afinfo
->get_saddr(net
, oif
, local
, remote
, mark
);
1383 /* Resolve list of templates for the flow, given policy. */
1386 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, const struct flowi
*fl
,
1387 struct xfrm_state
**xfrm
, unsigned short family
)
1389 struct net
*net
= xp_net(policy
);
1392 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1393 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1396 for (nx
= 0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1397 struct xfrm_state
*x
;
1398 xfrm_address_t
*remote
= daddr
;
1399 xfrm_address_t
*local
= saddr
;
1400 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1402 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1403 tmpl
->mode
== XFRM_MODE_BEET
) {
1404 remote
= &tmpl
->id
.daddr
;
1405 local
= &tmpl
->saddr
;
1406 if (xfrm_addr_any(local
, tmpl
->encap_family
)) {
1407 error
= xfrm_get_saddr(net
, fl
->flowi_oif
,
1409 tmpl
->encap_family
, 0);
1416 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
,
1417 family
, policy
->if_id
);
1419 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1426 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1429 } else if (error
== -ESRCH
) {
1433 if (!tmpl
->optional
)
1439 for (nx
--; nx
>= 0; nx
--)
1440 xfrm_state_put(xfrm
[nx
]);
1445 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, const struct flowi
*fl
,
1446 struct xfrm_state
**xfrm
, unsigned short family
)
1448 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1449 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1455 for (i
= 0; i
< npols
; i
++) {
1456 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1461 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1469 /* found states are sorted for outbound processing */
1471 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1476 for (cnx
--; cnx
>= 0; cnx
--)
1477 xfrm_state_put(tpp
[cnx
]);
1482 static int xfrm_get_tos(const struct flowi
*fl
, int family
)
1484 const struct xfrm_policy_afinfo
*afinfo
;
1487 afinfo
= xfrm_policy_get_afinfo(family
);
1491 tos
= afinfo
->get_tos(fl
);
1498 static inline struct xfrm_dst
*xfrm_alloc_dst(struct net
*net
, int family
)
1500 const struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1501 struct dst_ops
*dst_ops
;
1502 struct xfrm_dst
*xdst
;
1505 return ERR_PTR(-EINVAL
);
1509 dst_ops
= &net
->xfrm
.xfrm4_dst_ops
;
1511 #if IS_ENABLED(CONFIG_IPV6)
1513 dst_ops
= &net
->xfrm
.xfrm6_dst_ops
;
1519 xdst
= dst_alloc(dst_ops
, NULL
, 1, DST_OBSOLETE_NONE
, 0);
1522 struct dst_entry
*dst
= &xdst
->u
.dst
;
1524 memset(dst
+ 1, 0, sizeof(*xdst
) - sizeof(*dst
));
1526 xdst
= ERR_PTR(-ENOBUFS
);
1533 static inline int xfrm_init_path(struct xfrm_dst
*path
, struct dst_entry
*dst
,
1536 const struct xfrm_policy_afinfo
*afinfo
=
1537 xfrm_policy_get_afinfo(dst
->ops
->family
);
1543 err
= afinfo
->init_path(path
, dst
, nfheader_len
);
1550 static inline int xfrm_fill_dst(struct xfrm_dst
*xdst
, struct net_device
*dev
,
1551 const struct flowi
*fl
)
1553 const struct xfrm_policy_afinfo
*afinfo
=
1554 xfrm_policy_get_afinfo(xdst
->u
.dst
.ops
->family
);
1560 err
= afinfo
->fill_dst(xdst
, dev
, fl
);
1568 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1569 * all the metrics... Shortly, bundle a bundle.
1572 static struct dst_entry
*xfrm_bundle_create(struct xfrm_policy
*policy
,
1573 struct xfrm_state
**xfrm
, int nx
,
1574 const struct flowi
*fl
,
1575 struct dst_entry
*dst
)
1577 struct net
*net
= xp_net(policy
);
1578 unsigned long now
= jiffies
;
1579 struct net_device
*dev
;
1580 struct xfrm_mode
*inner_mode
;
1581 struct dst_entry
*dst_prev
= NULL
;
1582 struct dst_entry
*dst0
= NULL
;
1586 int nfheader_len
= 0;
1587 int trailer_len
= 0;
1589 int family
= policy
->selector
.family
;
1590 xfrm_address_t saddr
, daddr
;
1592 xfrm_flowi_addr_get(fl
, &saddr
, &daddr
, family
);
1594 tos
= xfrm_get_tos(fl
, family
);
1598 for (; i
< nx
; i
++) {
1599 struct xfrm_dst
*xdst
= xfrm_alloc_dst(net
, family
);
1600 struct dst_entry
*dst1
= &xdst
->u
.dst
;
1602 err
= PTR_ERR(xdst
);
1611 /* Ref count is taken during xfrm_alloc_dst()
1612 * No need to do dst_clone() on dst1
1614 dst_prev
->child
= dst1
;
1616 if (xfrm
[i
]->sel
.family
== AF_UNSPEC
) {
1617 inner_mode
= xfrm_ip2inner_mode(xfrm
[i
],
1618 xfrm_af2proto(family
));
1620 err
= -EAFNOSUPPORT
;
1625 inner_mode
= xfrm
[i
]->inner_mode
;
1628 dst_copy_metrics(dst1
, dst
);
1630 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1631 __u32 mark
= xfrm_smark_get(fl
->flowi_mark
, xfrm
[i
]);
1633 family
= xfrm
[i
]->props
.family
;
1634 dst
= xfrm_dst_lookup(xfrm
[i
], tos
, fl
->flowi_oif
,
1635 &saddr
, &daddr
, family
, mark
);
1642 dst1
->xfrm
= xfrm
[i
];
1643 xdst
->xfrm_genid
= xfrm
[i
]->genid
;
1645 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
1646 dst1
->flags
|= DST_HOST
;
1647 dst1
->lastuse
= now
;
1649 dst1
->input
= dst_discard
;
1650 dst1
->output
= inner_mode
->afinfo
->output
;
1652 dst1
->next
= dst_prev
;
1655 header_len
+= xfrm
[i
]->props
.header_len
;
1656 if (xfrm
[i
]->type
->flags
& XFRM_TYPE_NON_FRAGMENT
)
1657 nfheader_len
+= xfrm
[i
]->props
.header_len
;
1658 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
1661 dst_prev
->child
= dst
;
1669 xfrm_init_path((struct xfrm_dst
*)dst0
, dst
, nfheader_len
);
1670 xfrm_init_pmtu(dst_prev
);
1672 for (dst_prev
= dst0
; dst_prev
!= dst
; dst_prev
= dst_prev
->child
) {
1673 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst_prev
;
1675 err
= xfrm_fill_dst(xdst
, dev
, fl
);
1679 dst_prev
->header_len
= header_len
;
1680 dst_prev
->trailer_len
= trailer_len
;
1681 header_len
-= xdst
->u
.dst
.xfrm
->props
.header_len
;
1682 trailer_len
-= xdst
->u
.dst
.xfrm
->props
.trailer_len
;
1690 xfrm_state_put(xfrm
[i
]);
1693 dst_release_immediate(dst0
);
1694 dst0
= ERR_PTR(err
);
1698 static int xfrm_expand_policies(const struct flowi
*fl
, u16 family
,
1699 struct xfrm_policy
**pols
,
1700 int *num_pols
, int *num_xfrms
)
1704 if (*num_pols
== 0 || !pols
[0]) {
1709 if (IS_ERR(pols
[0]))
1710 return PTR_ERR(pols
[0]);
1712 *num_xfrms
= pols
[0]->xfrm_nr
;
1714 #ifdef CONFIG_XFRM_SUB_POLICY
1715 if (pols
[0] && pols
[0]->action
== XFRM_POLICY_ALLOW
&&
1716 pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1717 pols
[1] = xfrm_policy_lookup_bytype(xp_net(pols
[0]),
1718 XFRM_POLICY_TYPE_MAIN
,
1723 if (IS_ERR(pols
[1])) {
1724 xfrm_pols_put(pols
, *num_pols
);
1725 return PTR_ERR(pols
[1]);
1728 (*num_xfrms
) += pols
[1]->xfrm_nr
;
1732 for (i
= 0; i
< *num_pols
; i
++) {
1733 if (pols
[i
]->action
!= XFRM_POLICY_ALLOW
) {
1743 static void xfrm_last_dst_update(struct xfrm_dst
*xdst
, struct xfrm_dst
*old
)
1745 this_cpu_write(xfrm_last_dst
, xdst
);
1747 dst_release(&old
->u
.dst
);
1750 static void __xfrm_pcpu_work_fn(void)
1752 struct xfrm_dst
*old
;
1754 old
= this_cpu_read(xfrm_last_dst
);
1755 if (old
&& !xfrm_bundle_ok(old
))
1756 xfrm_last_dst_update(NULL
, old
);
1759 static void xfrm_pcpu_work_fn(struct work_struct
*work
)
1763 __xfrm_pcpu_work_fn();
1768 void xfrm_policy_cache_flush(void)
1770 struct xfrm_dst
*old
;
1778 for_each_possible_cpu(cpu
) {
1779 old
= per_cpu(xfrm_last_dst
, cpu
);
1780 if (old
&& !xfrm_bundle_ok(old
)) {
1781 if (smp_processor_id() == cpu
) {
1782 __xfrm_pcpu_work_fn();
1798 for_each_possible_cpu(cpu
) {
1799 bool bundle_release
;
1802 old
= per_cpu(xfrm_last_dst
, cpu
);
1803 bundle_release
= old
&& !xfrm_bundle_ok(old
);
1806 if (!bundle_release
)
1809 if (cpu_online(cpu
)) {
1810 schedule_work_on(cpu
, &xfrm_pcpu_work
[cpu
]);
1815 old
= per_cpu(xfrm_last_dst
, cpu
);
1816 if (old
&& !xfrm_bundle_ok(old
)) {
1817 per_cpu(xfrm_last_dst
, cpu
) = NULL
;
1818 dst_release(&old
->u
.dst
);
1826 static bool xfrm_xdst_can_reuse(struct xfrm_dst
*xdst
,
1827 struct xfrm_state
* const xfrm
[],
1830 const struct dst_entry
*dst
= &xdst
->u
.dst
;
1833 if (xdst
->num_xfrms
!= num
)
1836 for (i
= 0; i
< num
; i
++) {
1837 if (!dst
|| dst
->xfrm
!= xfrm
[i
])
1842 return xfrm_bundle_ok(xdst
);
1845 static struct xfrm_dst
*
1846 xfrm_resolve_and_create_bundle(struct xfrm_policy
**pols
, int num_pols
,
1847 const struct flowi
*fl
, u16 family
,
1848 struct dst_entry
*dst_orig
)
1850 struct net
*net
= xp_net(pols
[0]);
1851 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1852 struct xfrm_dst
*xdst
, *old
;
1853 struct dst_entry
*dst
;
1856 /* Try to instantiate a bundle */
1857 err
= xfrm_tmpl_resolve(pols
, num_pols
, fl
, xfrm
, family
);
1863 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1864 return ERR_PTR(err
);
1867 xdst
= this_cpu_read(xfrm_last_dst
);
1869 xdst
->u
.dst
.dev
== dst_orig
->dev
&&
1870 xdst
->num_pols
== num_pols
&&
1871 memcmp(xdst
->pols
, pols
,
1872 sizeof(struct xfrm_policy
*) * num_pols
) == 0 &&
1873 xfrm_xdst_can_reuse(xdst
, xfrm
, err
)) {
1874 dst_hold(&xdst
->u
.dst
);
1876 xfrm_state_put(xfrm
[--err
]);
1882 dst
= xfrm_bundle_create(pols
[0], xfrm
, err
, fl
, dst_orig
);
1884 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLEGENERROR
);
1885 return ERR_CAST(dst
);
1888 xdst
= (struct xfrm_dst
*)dst
;
1889 xdst
->num_xfrms
= err
;
1890 xdst
->num_pols
= num_pols
;
1891 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1892 xdst
->policy_genid
= atomic_read(&pols
[0]->genid
);
1894 atomic_set(&xdst
->u
.dst
.__refcnt
, 2);
1895 xfrm_last_dst_update(xdst
, old
);
1900 static void xfrm_policy_queue_process(unsigned long arg
)
1902 struct sk_buff
*skb
;
1904 struct dst_entry
*dst
;
1905 struct xfrm_policy
*pol
= (struct xfrm_policy
*)arg
;
1906 struct net
*net
= xp_net(pol
);
1907 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1909 struct sk_buff_head list
;
1911 spin_lock(&pq
->hold_queue
.lock
);
1912 skb
= skb_peek(&pq
->hold_queue
);
1914 spin_unlock(&pq
->hold_queue
.lock
);
1919 xfrm_decode_session(skb
, &fl
, dst
->ops
->family
);
1920 spin_unlock(&pq
->hold_queue
.lock
);
1922 dst_hold(dst
->path
);
1923 dst
= xfrm_lookup(net
, dst
->path
, &fl
, sk
, 0);
1927 if (dst
->flags
& DST_XFRM_QUEUE
) {
1930 if (pq
->timeout
>= XFRM_QUEUE_TMO_MAX
)
1933 pq
->timeout
= pq
->timeout
<< 1;
1934 if (!mod_timer(&pq
->hold_timer
, jiffies
+ pq
->timeout
))
1941 __skb_queue_head_init(&list
);
1943 spin_lock(&pq
->hold_queue
.lock
);
1945 skb_queue_splice_init(&pq
->hold_queue
, &list
);
1946 spin_unlock(&pq
->hold_queue
.lock
);
1948 while (!skb_queue_empty(&list
)) {
1949 skb
= __skb_dequeue(&list
);
1951 xfrm_decode_session(skb
, &fl
, skb_dst(skb
)->ops
->family
);
1952 dst_hold(skb_dst(skb
)->path
);
1953 dst
= xfrm_lookup(net
, skb_dst(skb
)->path
, &fl
, skb
->sk
, 0);
1961 skb_dst_set(skb
, dst
);
1963 dst_output(net
, skb
->sk
, skb
);
1972 skb_queue_purge(&pq
->hold_queue
);
1976 static int xdst_queue_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
1978 unsigned long sched_next
;
1979 struct dst_entry
*dst
= skb_dst(skb
);
1980 struct xfrm_dst
*xdst
= (struct xfrm_dst
*) dst
;
1981 struct xfrm_policy
*pol
= xdst
->pols
[0];
1982 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1984 if (unlikely(skb_fclone_busy(sk
, skb
))) {
1989 if (pq
->hold_queue
.qlen
> XFRM_MAX_QUEUE_LEN
) {
1996 spin_lock_bh(&pq
->hold_queue
.lock
);
1999 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
2001 sched_next
= jiffies
+ pq
->timeout
;
2003 if (del_timer(&pq
->hold_timer
)) {
2004 if (time_before(pq
->hold_timer
.expires
, sched_next
))
2005 sched_next
= pq
->hold_timer
.expires
;
2009 __skb_queue_tail(&pq
->hold_queue
, skb
);
2010 if (!mod_timer(&pq
->hold_timer
, sched_next
))
2013 spin_unlock_bh(&pq
->hold_queue
.lock
);
2018 static struct xfrm_dst
*xfrm_create_dummy_bundle(struct net
*net
,
2019 struct xfrm_flo
*xflo
,
2020 const struct flowi
*fl
,
2025 struct net_device
*dev
;
2026 struct dst_entry
*dst
;
2027 struct dst_entry
*dst1
;
2028 struct xfrm_dst
*xdst
;
2030 xdst
= xfrm_alloc_dst(net
, family
);
2034 if (!(xflo
->flags
& XFRM_LOOKUP_QUEUE
) ||
2035 net
->xfrm
.sysctl_larval_drop
||
2039 dst
= xflo
->dst_orig
;
2040 dst1
= &xdst
->u
.dst
;
2044 dst_copy_metrics(dst1
, dst
);
2046 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
2047 dst1
->flags
|= DST_HOST
| DST_XFRM_QUEUE
;
2048 dst1
->lastuse
= jiffies
;
2050 dst1
->input
= dst_discard
;
2051 dst1
->output
= xdst_queue_output
;
2057 xfrm_init_path((struct xfrm_dst
*)dst1
, dst
, 0);
2064 err
= xfrm_fill_dst(xdst
, dev
, fl
);
2073 xdst
= ERR_PTR(err
);
2077 static struct xfrm_dst
*xfrm_bundle_lookup(struct net
*net
,
2078 const struct flowi
*fl
,
2080 struct xfrm_flo
*xflo
, u32 if_id
)
2082 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2083 int num_pols
= 0, num_xfrms
= 0, err
;
2084 struct xfrm_dst
*xdst
;
2086 /* Resolve policies to use if we couldn't get them from
2087 * previous cache entry */
2089 pols
[0] = xfrm_policy_lookup(net
, fl
, family
, dir
, if_id
);
2090 err
= xfrm_expand_policies(fl
, family
, pols
,
2091 &num_pols
, &num_xfrms
);
2097 goto make_dummy_bundle
;
2100 xdst
= xfrm_resolve_and_create_bundle(pols
, num_pols
, fl
, family
,
2105 err
= PTR_ERR(xdst
);
2106 if (err
== -EREMOTE
) {
2107 xfrm_pols_put(pols
, num_pols
);
2113 goto make_dummy_bundle
;
2114 } else if (xdst
== NULL
) {
2116 goto make_dummy_bundle
;
2122 /* We found policies, but there's no bundles to instantiate:
2123 * either because the policy blocks, has no transformations or
2124 * we could not build template (no xfrm_states).*/
2125 xdst
= xfrm_create_dummy_bundle(net
, xflo
, fl
, num_xfrms
, family
);
2127 xfrm_pols_put(pols
, num_pols
);
2128 return ERR_CAST(xdst
);
2130 xdst
->num_pols
= num_pols
;
2131 xdst
->num_xfrms
= num_xfrms
;
2132 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2137 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
2139 xfrm_pols_put(pols
, num_pols
);
2140 return ERR_PTR(err
);
2143 static struct dst_entry
*make_blackhole(struct net
*net
, u16 family
,
2144 struct dst_entry
*dst_orig
)
2146 const struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2147 struct dst_entry
*ret
;
2150 dst_release(dst_orig
);
2151 return ERR_PTR(-EINVAL
);
2153 ret
= afinfo
->blackhole_route(net
, dst_orig
);
2160 /* Finds/creates a bundle for given flow and if_id
2162 * At the moment we eat a raw IP route. Mostly to speed up lookups
2163 * on interfaces with disabled IPsec.
2165 * xfrm_lookup uses an if_id of 0 by default, and is provided for
2168 struct dst_entry
*xfrm_lookup_with_ifid(struct net
*net
,
2169 struct dst_entry
*dst_orig
,
2170 const struct flowi
*fl
,
2171 const struct sock
*sk
,
2172 int flags
, u32 if_id
)
2174 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2175 struct xfrm_dst
*xdst
;
2176 struct dst_entry
*dst
, *route
;
2177 u16 family
= dst_orig
->ops
->family
;
2178 u8 dir
= XFRM_POLICY_OUT
;
2179 int i
, err
, num_pols
, num_xfrms
= 0, drop_pols
= 0;
2185 sk
= sk_const_to_full_sk(sk
);
2186 if (sk
&& sk
->sk_policy
[XFRM_POLICY_OUT
]) {
2188 pols
[0] = xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
, family
,
2190 err
= xfrm_expand_policies(fl
, family
, pols
,
2191 &num_pols
, &num_xfrms
);
2196 if (num_xfrms
<= 0) {
2197 drop_pols
= num_pols
;
2202 xdst
= xfrm_resolve_and_create_bundle(
2208 xfrm_pols_put(pols
, num_pols
);
2209 err
= PTR_ERR(xdst
);
2210 if (err
== -EREMOTE
)
2214 } else if (xdst
== NULL
) {
2216 drop_pols
= num_pols
;
2220 route
= xdst
->route
;
2225 struct xfrm_flo xflo
;
2227 xflo
.dst_orig
= dst_orig
;
2230 /* To accelerate a bit... */
2231 if ((dst_orig
->flags
& DST_NOXFRM
) ||
2232 !net
->xfrm
.policy_count
[XFRM_POLICY_OUT
])
2235 xdst
= xfrm_bundle_lookup(net
, fl
, family
, dir
, &xflo
, if_id
);
2239 err
= PTR_ERR(xdst
);
2243 num_pols
= xdst
->num_pols
;
2244 num_xfrms
= xdst
->num_xfrms
;
2245 memcpy(pols
, xdst
->pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2246 route
= xdst
->route
;
2250 if (route
== NULL
&& num_xfrms
> 0) {
2251 /* The only case when xfrm_bundle_lookup() returns a
2252 * bundle with null route, is when the template could
2253 * not be resolved. It means policies are there, but
2254 * bundle could not be created, since we don't yet
2255 * have the xfrm_state's. We need to wait for KM to
2256 * negotiate new SA's or bail out with error.*/
2257 if (net
->xfrm
.sysctl_larval_drop
) {
2258 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2265 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2273 if ((flags
& XFRM_LOOKUP_ICMP
) &&
2274 !(pols
[0]->flags
& XFRM_POLICY_ICMP
)) {
2279 for (i
= 0; i
< num_pols
; i
++)
2280 pols
[i
]->curlft
.use_time
= get_seconds();
2282 if (num_xfrms
< 0) {
2283 /* Prohibit the flow */
2284 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLBLOCK
);
2287 } else if (num_xfrms
> 0) {
2288 /* Flow transformed */
2289 dst_release(dst_orig
);
2291 /* Flow passes untransformed */
2296 xfrm_pols_put(pols
, drop_pols
);
2297 if (dst
&& dst
->xfrm
&&
2298 dst
->xfrm
->props
.mode
== XFRM_MODE_TUNNEL
)
2299 dst
->flags
|= DST_XFRM_TUNNEL
;
2303 if (!(flags
& XFRM_LOOKUP_ICMP
)) {
2311 if (!(flags
& XFRM_LOOKUP_KEEP_DST_REF
))
2312 dst_release(dst_orig
);
2313 xfrm_pols_put(pols
, drop_pols
);
2314 return ERR_PTR(err
);
2316 EXPORT_SYMBOL(xfrm_lookup_with_ifid
);
2318 /* Main function: finds/creates a bundle for given flow.
2320 * At the moment we eat a raw IP route. Mostly to speed up lookups
2321 * on interfaces with disabled IPsec.
2323 struct dst_entry
*xfrm_lookup(struct net
*net
, struct dst_entry
*dst_orig
,
2324 const struct flowi
*fl
, const struct sock
*sk
,
2327 return xfrm_lookup_with_ifid(net
, dst_orig
, fl
, sk
, flags
, 0);
2329 EXPORT_SYMBOL(xfrm_lookup
);
2331 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2332 * Otherwise we may send out blackholed packets.
2334 struct dst_entry
*xfrm_lookup_route(struct net
*net
, struct dst_entry
*dst_orig
,
2335 const struct flowi
*fl
,
2336 const struct sock
*sk
, int flags
)
2338 struct dst_entry
*dst
= xfrm_lookup(net
, dst_orig
, fl
, sk
,
2339 flags
| XFRM_LOOKUP_QUEUE
|
2340 XFRM_LOOKUP_KEEP_DST_REF
);
2342 if (IS_ERR(dst
) && PTR_ERR(dst
) == -EREMOTE
)
2343 return make_blackhole(net
, dst_orig
->ops
->family
, dst_orig
);
2346 dst_release(dst_orig
);
2350 EXPORT_SYMBOL(xfrm_lookup_route
);
2353 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, const struct flowi
*fl
)
2355 struct xfrm_state
*x
;
2357 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
2359 x
= skb
->sp
->xvec
[idx
];
2360 if (!x
->type
->reject
)
2362 return x
->type
->reject(x
, skb
, fl
);
2365 /* When skb is transformed back to its "native" form, we have to
2366 * check policy restrictions. At the moment we make this in maximally
2367 * stupid way. Shame on me. :-) Of course, connected sockets must
2368 * have policy cached at them.
2372 xfrm_state_ok(const struct xfrm_tmpl
*tmpl
, const struct xfrm_state
*x
,
2373 unsigned short family
)
2375 if (xfrm_state_kern(x
))
2376 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
2377 return x
->id
.proto
== tmpl
->id
.proto
&&
2378 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
2379 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
2380 x
->props
.mode
== tmpl
->mode
&&
2381 (tmpl
->allalgs
|| (tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
2382 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
2383 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
2384 xfrm_state_addr_cmp(tmpl
, x
, family
));
2388 * 0 or more than 0 is returned when validation is succeeded (either bypass
2389 * because of optional transport mode, or next index of the mathced secpath
2390 * state with the template.
2391 * -1 is returned when no matching template is found.
2392 * Otherwise "-2 - errored_index" is returned.
2395 xfrm_policy_ok(const struct xfrm_tmpl
*tmpl
, const struct sec_path
*sp
, int start
,
2396 unsigned short family
)
2400 if (tmpl
->optional
) {
2401 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
2405 for (; idx
< sp
->len
; idx
++) {
2406 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
2408 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2417 int __xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
,
2418 unsigned int family
, int reverse
)
2420 const struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2423 if (unlikely(afinfo
== NULL
))
2424 return -EAFNOSUPPORT
;
2426 afinfo
->decode_session(skb
, fl
, reverse
);
2428 err
= security_xfrm_decode_session(skb
, &fl
->flowi_secid
);
2432 EXPORT_SYMBOL(__xfrm_decode_session
);
2434 static inline int secpath_has_nontransport(const struct sec_path
*sp
, int k
, int *idxp
)
2436 for (; k
< sp
->len
; k
++) {
2437 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2446 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
2447 unsigned short family
)
2449 struct net
*net
= dev_net(skb
->dev
);
2450 struct xfrm_policy
*pol
;
2451 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2458 const struct xfrm_if_cb
*ifcb
;
2463 ifcb
= xfrm_if_get_cb();
2466 xi
= ifcb
->decode_session(skb
);
2468 if_id
= xi
->p
.if_id
;
2472 reverse
= dir
& ~XFRM_POLICY_MASK
;
2473 dir
&= XFRM_POLICY_MASK
;
2475 if (__xfrm_decode_session(skb
, &fl
, family
, reverse
) < 0) {
2476 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
2480 nf_nat_decode_session(skb
, &fl
, family
);
2482 /* First, check used SA against their selectors. */
2486 for (i
= skb
->sp
->len
-1; i
>= 0; i
--) {
2487 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
2488 if (!xfrm_selector_match(&x
->sel
, &fl
, family
)) {
2489 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMISMATCH
);
2496 sk
= sk_to_full_sk(sk
);
2497 if (sk
&& sk
->sk_policy
[dir
]) {
2498 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
, family
, if_id
);
2500 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2506 pol
= xfrm_policy_lookup(net
, &fl
, family
, dir
, if_id
);
2509 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2514 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
2515 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2516 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINNOPOLS
);
2522 pol
->curlft
.use_time
= get_seconds();
2526 #ifdef CONFIG_XFRM_SUB_POLICY
2527 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
2528 pols
[1] = xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
,
2530 XFRM_POLICY_IN
, if_id
);
2532 if (IS_ERR(pols
[1])) {
2533 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2536 pols
[1]->curlft
.use_time
= get_seconds();
2542 if (pol
->action
== XFRM_POLICY_ALLOW
) {
2543 struct sec_path
*sp
;
2544 static struct sec_path dummy
;
2545 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
2546 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
2547 struct xfrm_tmpl
**tpp
= tp
;
2551 if ((sp
= skb
->sp
) == NULL
)
2554 for (pi
= 0; pi
< npols
; pi
++) {
2555 if (pols
[pi
] != pol
&&
2556 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
) {
2557 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2560 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
2561 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
2564 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
2565 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
2569 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
, net
);
2573 /* For each tunnel xfrm, find the first matching tmpl.
2574 * For each tmpl before that, find corresponding xfrm.
2575 * Order is _important_. Later we will implement
2576 * some barriers, but at the moment barriers
2577 * are implied between each two transformations.
2579 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
2580 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
2583 /* "-2 - errored_index" returned */
2585 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2590 if (secpath_has_nontransport(sp
, k
, &xerr_idx
)) {
2591 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2595 xfrm_pols_put(pols
, npols
);
2598 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2601 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2603 xfrm_pols_put(pols
, npols
);
2606 EXPORT_SYMBOL(__xfrm_policy_check
);
2608 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
2610 struct net
*net
= dev_net(skb
->dev
);
2612 struct dst_entry
*dst
;
2615 if (xfrm_decode_session(skb
, &fl
, family
) < 0) {
2616 XFRM_INC_STATS(net
, LINUX_MIB_XFRMFWDHDRERROR
);
2621 if (!skb_dst(skb
)) {
2622 XFRM_INC_STATS(net
, LINUX_MIB_XFRMFWDHDRERROR
);
2626 dst
= xfrm_lookup(net
, skb_dst(skb
), &fl
, NULL
, XFRM_LOOKUP_QUEUE
);
2631 skb_dst_set(skb
, dst
);
2634 EXPORT_SYMBOL(__xfrm_route_forward
);
2636 /* Optimize later using cookies and generation ids. */
2638 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
2640 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2641 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2642 * get validated by dst_ops->check on every use. We do this
2643 * because when a normal route referenced by an XFRM dst is
2644 * obsoleted we do not go looking around for all parent
2645 * referencing XFRM dsts so that we can invalidate them. It
2646 * is just too much work. Instead we make the checks here on
2647 * every use. For example:
2649 * XFRM dst A --> IPv4 dst X
2651 * X is the "xdst->route" of A (X is also the "dst->path" of A
2652 * in this example). If X is marked obsolete, "A" will not
2653 * notice. That's what we are validating here via the
2654 * stale_bundle() check.
2656 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
2658 * This will force stale_bundle() to fail on any xdst bundle with
2659 * this dst linked in it.
2661 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
2667 static int stale_bundle(struct dst_entry
*dst
)
2669 return !xfrm_bundle_ok((struct xfrm_dst
*)dst
);
2672 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
2674 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
2675 dst
->dev
= dev_net(dev
)->loopback_dev
;
2680 EXPORT_SYMBOL(xfrm_dst_ifdown
);
2682 static void xfrm_link_failure(struct sk_buff
*skb
)
2684 /* Impossible. Such dst must be popped before reaches point of failure. */
2687 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
2690 if (dst
->obsolete
) {
2698 static void xfrm_init_pmtu(struct dst_entry
*dst
)
2701 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2702 u32 pmtu
, route_mtu_cached
;
2704 pmtu
= dst_mtu(dst
->child
);
2705 xdst
->child_mtu_cached
= pmtu
;
2707 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2709 route_mtu_cached
= dst_mtu(xdst
->route
);
2710 xdst
->route_mtu_cached
= route_mtu_cached
;
2712 if (pmtu
> route_mtu_cached
)
2713 pmtu
= route_mtu_cached
;
2715 dst_metric_set(dst
, RTAX_MTU
, pmtu
);
2716 } while ((dst
= dst
->next
));
2719 /* Check that the bundle accepts the flow and its components are
2723 static int xfrm_bundle_ok(struct xfrm_dst
*first
)
2725 struct dst_entry
*dst
= &first
->u
.dst
;
2726 struct xfrm_dst
*last
;
2729 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2730 (dst
->dev
&& !netif_running(dst
->dev
)))
2733 if (dst
->flags
& DST_XFRM_QUEUE
)
2739 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2741 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2743 if (xdst
->xfrm_genid
!= dst
->xfrm
->genid
)
2745 if (xdst
->num_pols
> 0 &&
2746 xdst
->policy_genid
!= atomic_read(&xdst
->pols
[0]->genid
))
2749 mtu
= dst_mtu(dst
->child
);
2750 if (xdst
->child_mtu_cached
!= mtu
) {
2752 xdst
->child_mtu_cached
= mtu
;
2755 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2757 mtu
= dst_mtu(xdst
->route
);
2758 if (xdst
->route_mtu_cached
!= mtu
) {
2760 xdst
->route_mtu_cached
= mtu
;
2764 } while (dst
->xfrm
);
2769 mtu
= last
->child_mtu_cached
;
2773 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2774 if (mtu
> last
->route_mtu_cached
)
2775 mtu
= last
->route_mtu_cached
;
2776 dst_metric_set(dst
, RTAX_MTU
, mtu
);
2781 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2782 last
->child_mtu_cached
= mtu
;
2788 static unsigned int xfrm_default_advmss(const struct dst_entry
*dst
)
2790 return dst_metric_advmss(dst
->path
);
2793 static unsigned int xfrm_mtu(const struct dst_entry
*dst
)
2795 unsigned int mtu
= dst_metric_raw(dst
, RTAX_MTU
);
2797 return mtu
? : dst_mtu(dst
->path
);
2800 static const void *xfrm_get_dst_nexthop(const struct dst_entry
*dst
,
2803 const struct dst_entry
*path
= dst
->path
;
2805 for (; dst
!= path
; dst
= dst
->child
) {
2806 const struct xfrm_state
*xfrm
= dst
->xfrm
;
2808 if (xfrm
->props
.mode
== XFRM_MODE_TRANSPORT
)
2810 if (xfrm
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
)
2811 daddr
= xfrm
->coaddr
;
2812 else if (!(xfrm
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
))
2813 daddr
= &xfrm
->id
.daddr
;
2818 static struct neighbour
*xfrm_neigh_lookup(const struct dst_entry
*dst
,
2819 struct sk_buff
*skb
,
2822 const struct dst_entry
*path
= dst
->path
;
2825 daddr
= xfrm_get_dst_nexthop(dst
, daddr
);
2826 return path
->ops
->neigh_lookup(path
, skb
, daddr
);
2829 static void xfrm_confirm_neigh(const struct dst_entry
*dst
, const void *daddr
)
2831 const struct dst_entry
*path
= dst
->path
;
2833 daddr
= xfrm_get_dst_nexthop(dst
, daddr
);
2834 path
->ops
->confirm_neigh(path
, daddr
);
2837 int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo
*afinfo
, int family
)
2841 if (WARN_ON(family
>= ARRAY_SIZE(xfrm_policy_afinfo
)))
2842 return -EAFNOSUPPORT
;
2844 spin_lock(&xfrm_policy_afinfo_lock
);
2845 if (unlikely(xfrm_policy_afinfo
[family
] != NULL
))
2848 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2849 if (likely(dst_ops
->kmem_cachep
== NULL
))
2850 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2851 if (likely(dst_ops
->check
== NULL
))
2852 dst_ops
->check
= xfrm_dst_check
;
2853 if (likely(dst_ops
->default_advmss
== NULL
))
2854 dst_ops
->default_advmss
= xfrm_default_advmss
;
2855 if (likely(dst_ops
->mtu
== NULL
))
2856 dst_ops
->mtu
= xfrm_mtu
;
2857 if (likely(dst_ops
->negative_advice
== NULL
))
2858 dst_ops
->negative_advice
= xfrm_negative_advice
;
2859 if (likely(dst_ops
->link_failure
== NULL
))
2860 dst_ops
->link_failure
= xfrm_link_failure
;
2861 if (likely(dst_ops
->neigh_lookup
== NULL
))
2862 dst_ops
->neigh_lookup
= xfrm_neigh_lookup
;
2863 if (likely(!dst_ops
->confirm_neigh
))
2864 dst_ops
->confirm_neigh
= xfrm_confirm_neigh
;
2865 rcu_assign_pointer(xfrm_policy_afinfo
[family
], afinfo
);
2867 spin_unlock(&xfrm_policy_afinfo_lock
);
2871 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2873 void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo
*afinfo
)
2875 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2878 for (i
= 0; i
< ARRAY_SIZE(xfrm_policy_afinfo
); i
++) {
2879 if (xfrm_policy_afinfo
[i
] != afinfo
)
2881 RCU_INIT_POINTER(xfrm_policy_afinfo
[i
], NULL
);
2887 dst_ops
->kmem_cachep
= NULL
;
2888 dst_ops
->check
= NULL
;
2889 dst_ops
->negative_advice
= NULL
;
2890 dst_ops
->link_failure
= NULL
;
2892 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2894 void xfrm_if_register_cb(const struct xfrm_if_cb
*ifcb
)
2896 spin_lock(&xfrm_if_cb_lock
);
2897 rcu_assign_pointer(xfrm_if_cb
, ifcb
);
2898 spin_unlock(&xfrm_if_cb_lock
);
2900 EXPORT_SYMBOL(xfrm_if_register_cb
);
2902 void xfrm_if_unregister_cb(void)
2904 RCU_INIT_POINTER(xfrm_if_cb
, NULL
);
2907 EXPORT_SYMBOL(xfrm_if_unregister_cb
);
2909 #ifdef CONFIG_XFRM_STATISTICS
2910 static int __net_init
xfrm_statistics_init(struct net
*net
)
2913 net
->mib
.xfrm_statistics
= alloc_percpu(struct linux_xfrm_mib
);
2914 if (!net
->mib
.xfrm_statistics
)
2916 rv
= xfrm_proc_init(net
);
2918 free_percpu(net
->mib
.xfrm_statistics
);
2922 static void xfrm_statistics_fini(struct net
*net
)
2924 xfrm_proc_fini(net
);
2925 free_percpu(net
->mib
.xfrm_statistics
);
2928 static int __net_init
xfrm_statistics_init(struct net
*net
)
2933 static void xfrm_statistics_fini(struct net
*net
)
2938 static int __net_init
xfrm_policy_init(struct net
*net
)
2940 unsigned int hmask
, sz
;
2943 if (net_eq(net
, &init_net
))
2944 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2945 sizeof(struct xfrm_dst
),
2946 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2950 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2952 net
->xfrm
.policy_byidx
= xfrm_hash_alloc(sz
);
2953 if (!net
->xfrm
.policy_byidx
)
2955 net
->xfrm
.policy_idx_hmask
= hmask
;
2957 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
2958 struct xfrm_policy_hash
*htab
;
2960 net
->xfrm
.policy_count
[dir
] = 0;
2961 net
->xfrm
.policy_count
[XFRM_POLICY_MAX
+ dir
] = 0;
2962 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
2964 htab
= &net
->xfrm
.policy_bydst
[dir
];
2965 htab
->table
= xfrm_hash_alloc(sz
);
2968 htab
->hmask
= hmask
;
2974 net
->xfrm
.policy_hthresh
.lbits4
= 32;
2975 net
->xfrm
.policy_hthresh
.rbits4
= 32;
2976 net
->xfrm
.policy_hthresh
.lbits6
= 128;
2977 net
->xfrm
.policy_hthresh
.rbits6
= 128;
2979 seqlock_init(&net
->xfrm
.policy_hthresh
.lock
);
2981 INIT_LIST_HEAD(&net
->xfrm
.policy_all
);
2982 INIT_WORK(&net
->xfrm
.policy_hash_work
, xfrm_hash_resize
);
2983 INIT_WORK(&net
->xfrm
.policy_hthresh
.work
, xfrm_hash_rebuild
);
2984 if (net_eq(net
, &init_net
))
2989 for (dir
--; dir
>= 0; dir
--) {
2990 struct xfrm_policy_hash
*htab
;
2992 htab
= &net
->xfrm
.policy_bydst
[dir
];
2993 xfrm_hash_free(htab
->table
, sz
);
2995 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
3000 static void xfrm_policy_fini(struct net
*net
)
3005 flush_work(&net
->xfrm
.policy_hash_work
);
3006 #ifdef CONFIG_XFRM_SUB_POLICY
3007 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_SUB
, false);
3009 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, false);
3011 WARN_ON(!list_empty(&net
->xfrm
.policy_all
));
3013 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
3014 struct xfrm_policy_hash
*htab
;
3016 WARN_ON(!hlist_empty(&net
->xfrm
.policy_inexact
[dir
]));
3018 htab
= &net
->xfrm
.policy_bydst
[dir
];
3019 sz
= (htab
->hmask
+ 1) * sizeof(struct hlist_head
);
3020 WARN_ON(!hlist_empty(htab
->table
));
3021 xfrm_hash_free(htab
->table
, sz
);
3024 sz
= (net
->xfrm
.policy_idx_hmask
+ 1) * sizeof(struct hlist_head
);
3025 WARN_ON(!hlist_empty(net
->xfrm
.policy_byidx
));
3026 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
3029 static int __net_init
xfrm_net_init(struct net
*net
)
3033 /* Initialize the per-net locks here */
3034 spin_lock_init(&net
->xfrm
.xfrm_state_lock
);
3035 spin_lock_init(&net
->xfrm
.xfrm_policy_lock
);
3036 mutex_init(&net
->xfrm
.xfrm_cfg_mutex
);
3038 rv
= xfrm_statistics_init(net
);
3040 goto out_statistics
;
3041 rv
= xfrm_state_init(net
);
3044 rv
= xfrm_policy_init(net
);
3047 rv
= xfrm_sysctl_init(net
);
3054 xfrm_policy_fini(net
);
3056 xfrm_state_fini(net
);
3058 xfrm_statistics_fini(net
);
3063 static void __net_exit
xfrm_net_exit(struct net
*net
)
3065 xfrm_sysctl_fini(net
);
3066 xfrm_policy_fini(net
);
3067 xfrm_state_fini(net
);
3068 xfrm_statistics_fini(net
);
3071 static struct pernet_operations __net_initdata xfrm_net_ops
= {
3072 .init
= xfrm_net_init
,
3073 .exit
= xfrm_net_exit
,
3076 void __init
xfrm_init(void)
3080 xfrm_pcpu_work
= kmalloc_array(NR_CPUS
, sizeof(*xfrm_pcpu_work
),
3082 BUG_ON(!xfrm_pcpu_work
);
3084 for (i
= 0; i
< NR_CPUS
; i
++)
3085 INIT_WORK(&xfrm_pcpu_work
[i
], xfrm_pcpu_work_fn
);
3087 register_pernet_subsys(&xfrm_net_ops
);
3088 seqcount_init(&xfrm_policy_hash_generation
);
3091 RCU_INIT_POINTER(xfrm_if_cb
, NULL
);
3095 #ifdef CONFIG_AUDITSYSCALL
3096 static void xfrm_audit_common_policyinfo(struct xfrm_policy
*xp
,
3097 struct audit_buffer
*audit_buf
)
3099 struct xfrm_sec_ctx
*ctx
= xp
->security
;
3100 struct xfrm_selector
*sel
= &xp
->selector
;
3103 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
3104 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
3106 switch (sel
->family
) {
3108 audit_log_format(audit_buf
, " src=%pI4", &sel
->saddr
.a4
);
3109 if (sel
->prefixlen_s
!= 32)
3110 audit_log_format(audit_buf
, " src_prefixlen=%d",
3112 audit_log_format(audit_buf
, " dst=%pI4", &sel
->daddr
.a4
);
3113 if (sel
->prefixlen_d
!= 32)
3114 audit_log_format(audit_buf
, " dst_prefixlen=%d",
3118 audit_log_format(audit_buf
, " src=%pI6", sel
->saddr
.a6
);
3119 if (sel
->prefixlen_s
!= 128)
3120 audit_log_format(audit_buf
, " src_prefixlen=%d",
3122 audit_log_format(audit_buf
, " dst=%pI6", sel
->daddr
.a6
);
3123 if (sel
->prefixlen_d
!= 128)
3124 audit_log_format(audit_buf
, " dst_prefixlen=%d",
3130 void xfrm_audit_policy_add(struct xfrm_policy
*xp
, int result
, bool task_valid
)
3132 struct audit_buffer
*audit_buf
;
3134 audit_buf
= xfrm_audit_start("SPD-add");
3135 if (audit_buf
== NULL
)
3137 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
3138 audit_log_format(audit_buf
, " res=%u", result
);
3139 xfrm_audit_common_policyinfo(xp
, audit_buf
);
3140 audit_log_end(audit_buf
);
3142 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add
);
3144 void xfrm_audit_policy_delete(struct xfrm_policy
*xp
, int result
,
3147 struct audit_buffer
*audit_buf
;
3149 audit_buf
= xfrm_audit_start("SPD-delete");
3150 if (audit_buf
== NULL
)
3152 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
3153 audit_log_format(audit_buf
, " res=%u", result
);
3154 xfrm_audit_common_policyinfo(xp
, audit_buf
);
3155 audit_log_end(audit_buf
);
3157 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete
);
3160 #ifdef CONFIG_XFRM_MIGRATE
3161 static bool xfrm_migrate_selector_match(const struct xfrm_selector
*sel_cmp
,
3162 const struct xfrm_selector
*sel_tgt
)
3164 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
3165 if (sel_tgt
->family
== sel_cmp
->family
&&
3166 xfrm_addr_equal(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
3168 xfrm_addr_equal(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
3170 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
3171 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
3175 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
3182 static struct xfrm_policy
*xfrm_migrate_policy_find(const struct xfrm_selector
*sel
,
3183 u8 dir
, u8 type
, struct net
*net
)
3185 struct xfrm_policy
*pol
, *ret
= NULL
;
3186 struct hlist_head
*chain
;
3189 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
3190 chain
= policy_hash_direct(net
, &sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
3191 hlist_for_each_entry(pol
, chain
, bydst
) {
3192 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3193 pol
->type
== type
) {
3195 priority
= ret
->priority
;
3199 chain
= &net
->xfrm
.policy_inexact
[dir
];
3200 hlist_for_each_entry(pol
, chain
, bydst
) {
3201 if ((pol
->priority
>= priority
) && ret
)
3204 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3205 pol
->type
== type
) {
3213 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
3218 static int migrate_tmpl_match(const struct xfrm_migrate
*m
, const struct xfrm_tmpl
*t
)
3222 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
3223 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
3225 case XFRM_MODE_TUNNEL
:
3226 case XFRM_MODE_BEET
:
3227 if (xfrm_addr_equal(&t
->id
.daddr
, &m
->old_daddr
,
3229 xfrm_addr_equal(&t
->saddr
, &m
->old_saddr
,
3234 case XFRM_MODE_TRANSPORT
:
3235 /* in case of transport mode, template does not store
3236 any IP addresses, hence we just compare mode and
3247 /* update endpoint address(es) of template(s) */
3248 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
3249 struct xfrm_migrate
*m
, int num_migrate
)
3251 struct xfrm_migrate
*mp
;
3254 write_lock_bh(&pol
->lock
);
3255 if (unlikely(pol
->walk
.dead
)) {
3256 /* target policy has been deleted */
3257 write_unlock_bh(&pol
->lock
);
3261 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
3262 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
3263 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
3266 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
&&
3267 pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_BEET
)
3269 /* update endpoints */
3270 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
3271 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
3272 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
3273 sizeof(pol
->xfrm_vec
[i
].saddr
));
3274 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
3276 atomic_inc(&pol
->genid
);
3280 write_unlock_bh(&pol
->lock
);
3288 static int xfrm_migrate_check(const struct xfrm_migrate
*m
, int num_migrate
)
3292 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
3295 for (i
= 0; i
< num_migrate
; i
++) {
3296 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
3297 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
3300 /* check if there is any duplicated entry */
3301 for (j
= i
+ 1; j
< num_migrate
; j
++) {
3302 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
3303 sizeof(m
[i
].old_daddr
)) &&
3304 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
3305 sizeof(m
[i
].old_saddr
)) &&
3306 m
[i
].proto
== m
[j
].proto
&&
3307 m
[i
].mode
== m
[j
].mode
&&
3308 m
[i
].reqid
== m
[j
].reqid
&&
3309 m
[i
].old_family
== m
[j
].old_family
)
3317 int xfrm_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3318 struct xfrm_migrate
*m
, int num_migrate
,
3319 struct xfrm_kmaddress
*k
, struct net
*net
,
3320 struct xfrm_encap_tmpl
*encap
)
3322 int i
, err
, nx_cur
= 0, nx_new
= 0;
3323 struct xfrm_policy
*pol
= NULL
;
3324 struct xfrm_state
*x
, *xc
;
3325 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
3326 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
3327 struct xfrm_migrate
*mp
;
3329 /* Stage 0 - sanity checks */
3330 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
3333 if (dir
>= XFRM_POLICY_MAX
) {
3338 /* Stage 1 - find policy */
3339 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
, net
)) == NULL
) {
3344 /* Stage 2 - find and update state(s) */
3345 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
3346 if ((x
= xfrm_migrate_state_find(mp
, net
))) {
3349 xc
= xfrm_state_migrate(x
, mp
, encap
);
3360 /* Stage 3 - update policy */
3361 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
3364 /* Stage 4 - delete old state(s) */
3366 xfrm_states_put(x_cur
, nx_cur
);
3367 xfrm_states_delete(x_cur
, nx_cur
);
3370 /* Stage 5 - announce */
3371 km_migrate(sel
, dir
, type
, m
, num_migrate
, k
, encap
);
3383 xfrm_states_put(x_cur
, nx_cur
);
3385 xfrm_states_delete(x_new
, nx_new
);
3389 EXPORT_SYMBOL(xfrm_migrate
);