6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * Kazunori MIYAZAWA @USAGI
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/kmod.h>
19 #include <linux/list.h>
20 #include <linux/spinlock.h>
21 #include <linux/workqueue.h>
22 #include <linux/notifier.h>
23 #include <linux/netdevice.h>
24 #include <linux/netfilter.h>
25 #include <linux/module.h>
26 #include <linux/cache.h>
27 #include <linux/audit.h>
32 #ifdef CONFIG_XFRM_STATISTICS
36 #include "xfrm_hash.h"
38 #define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
39 #define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
40 #define XFRM_MAX_QUEUE_LEN 100
43 struct dst_entry
*dst_orig
;
47 static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock
);
48 static struct xfrm_policy_afinfo
const __rcu
*xfrm_policy_afinfo
[AF_INET6
+ 1]
51 static struct kmem_cache
*xfrm_dst_cache __read_mostly
;
52 static __read_mostly seqcount_t xfrm_policy_hash_generation
;
54 static void xfrm_init_pmtu(struct dst_entry
*dst
);
55 static int stale_bundle(struct dst_entry
*dst
);
56 static int xfrm_bundle_ok(struct xfrm_dst
*xdst
);
57 static void xfrm_policy_queue_process(unsigned long arg
);
59 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
);
60 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
63 static inline bool xfrm_pol_hold_rcu(struct xfrm_policy
*policy
)
65 return atomic_inc_not_zero(&policy
->refcnt
);
69 __xfrm4_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
71 const struct flowi4
*fl4
= &fl
->u
.ip4
;
73 return addr4_match(fl4
->daddr
, sel
->daddr
.a4
, sel
->prefixlen_d
) &&
74 addr4_match(fl4
->saddr
, sel
->saddr
.a4
, sel
->prefixlen_s
) &&
75 !((xfrm_flowi_dport(fl
, &fl4
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
76 !((xfrm_flowi_sport(fl
, &fl4
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
77 (fl4
->flowi4_proto
== sel
->proto
|| !sel
->proto
) &&
78 (fl4
->flowi4_oif
== sel
->ifindex
|| !sel
->ifindex
);
82 __xfrm6_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
84 const struct flowi6
*fl6
= &fl
->u
.ip6
;
86 return addr_match(&fl6
->daddr
, &sel
->daddr
, sel
->prefixlen_d
) &&
87 addr_match(&fl6
->saddr
, &sel
->saddr
, sel
->prefixlen_s
) &&
88 !((xfrm_flowi_dport(fl
, &fl6
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
89 !((xfrm_flowi_sport(fl
, &fl6
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
90 (fl6
->flowi6_proto
== sel
->proto
|| !sel
->proto
) &&
91 (fl6
->flowi6_oif
== sel
->ifindex
|| !sel
->ifindex
);
94 bool xfrm_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
,
95 unsigned short family
)
99 return __xfrm4_selector_match(sel
, fl
);
101 return __xfrm6_selector_match(sel
, fl
);
106 static const struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
108 const struct xfrm_policy_afinfo
*afinfo
;
110 if (unlikely(family
>= ARRAY_SIZE(xfrm_policy_afinfo
)))
113 afinfo
= rcu_dereference(xfrm_policy_afinfo
[family
]);
114 if (unlikely(!afinfo
))
119 struct dst_entry
*__xfrm_dst_lookup(struct net
*net
, int tos
, int oif
,
120 const xfrm_address_t
*saddr
,
121 const xfrm_address_t
*daddr
,
124 const struct xfrm_policy_afinfo
*afinfo
;
125 struct dst_entry
*dst
;
127 afinfo
= xfrm_policy_get_afinfo(family
);
128 if (unlikely(afinfo
== NULL
))
129 return ERR_PTR(-EAFNOSUPPORT
);
131 dst
= afinfo
->dst_lookup(net
, tos
, oif
, saddr
, daddr
);
137 EXPORT_SYMBOL(__xfrm_dst_lookup
);
139 static inline struct dst_entry
*xfrm_dst_lookup(struct xfrm_state
*x
,
141 xfrm_address_t
*prev_saddr
,
142 xfrm_address_t
*prev_daddr
,
145 struct net
*net
= xs_net(x
);
146 xfrm_address_t
*saddr
= &x
->props
.saddr
;
147 xfrm_address_t
*daddr
= &x
->id
.daddr
;
148 struct dst_entry
*dst
;
150 if (x
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
) {
154 if (x
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
) {
159 dst
= __xfrm_dst_lookup(net
, tos
, oif
, saddr
, daddr
, family
);
162 if (prev_saddr
!= saddr
)
163 memcpy(prev_saddr
, saddr
, sizeof(*prev_saddr
));
164 if (prev_daddr
!= daddr
)
165 memcpy(prev_daddr
, daddr
, sizeof(*prev_daddr
));
171 static inline unsigned long make_jiffies(long secs
)
173 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
174 return MAX_SCHEDULE_TIMEOUT
-1;
179 static void xfrm_policy_timer(unsigned long data
)
181 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
182 unsigned long now
= get_seconds();
183 long next
= LONG_MAX
;
187 read_lock(&xp
->lock
);
189 if (unlikely(xp
->walk
.dead
))
192 dir
= xfrm_policy_id2dir(xp
->index
);
194 if (xp
->lft
.hard_add_expires_seconds
) {
195 long tmo
= xp
->lft
.hard_add_expires_seconds
+
196 xp
->curlft
.add_time
- now
;
202 if (xp
->lft
.hard_use_expires_seconds
) {
203 long tmo
= xp
->lft
.hard_use_expires_seconds
+
204 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
210 if (xp
->lft
.soft_add_expires_seconds
) {
211 long tmo
= xp
->lft
.soft_add_expires_seconds
+
212 xp
->curlft
.add_time
- now
;
215 tmo
= XFRM_KM_TIMEOUT
;
220 if (xp
->lft
.soft_use_expires_seconds
) {
221 long tmo
= xp
->lft
.soft_use_expires_seconds
+
222 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
225 tmo
= XFRM_KM_TIMEOUT
;
232 km_policy_expired(xp
, dir
, 0, 0);
233 if (next
!= LONG_MAX
&&
234 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
238 read_unlock(&xp
->lock
);
243 read_unlock(&xp
->lock
);
244 if (!xfrm_policy_delete(xp
, dir
))
245 km_policy_expired(xp
, dir
, 1, 0);
249 static struct flow_cache_object
*xfrm_policy_flo_get(struct flow_cache_object
*flo
)
251 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
253 if (unlikely(pol
->walk
.dead
))
261 static int xfrm_policy_flo_check(struct flow_cache_object
*flo
)
263 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
265 return !pol
->walk
.dead
;
268 static void xfrm_policy_flo_delete(struct flow_cache_object
*flo
)
270 xfrm_pol_put(container_of(flo
, struct xfrm_policy
, flo
));
273 static const struct flow_cache_ops xfrm_policy_fc_ops
= {
274 .get
= xfrm_policy_flo_get
,
275 .check
= xfrm_policy_flo_check
,
276 .delete = xfrm_policy_flo_delete
,
279 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
283 struct xfrm_policy
*xfrm_policy_alloc(struct net
*net
, gfp_t gfp
)
285 struct xfrm_policy
*policy
;
287 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
290 write_pnet(&policy
->xp_net
, net
);
291 INIT_LIST_HEAD(&policy
->walk
.all
);
292 INIT_HLIST_NODE(&policy
->bydst
);
293 INIT_HLIST_NODE(&policy
->byidx
);
294 rwlock_init(&policy
->lock
);
295 atomic_set(&policy
->refcnt
, 1);
296 skb_queue_head_init(&policy
->polq
.hold_queue
);
297 setup_timer(&policy
->timer
, xfrm_policy_timer
,
298 (unsigned long)policy
);
299 setup_timer(&policy
->polq
.hold_timer
, xfrm_policy_queue_process
,
300 (unsigned long)policy
);
301 policy
->flo
.ops
= &xfrm_policy_fc_ops
;
305 EXPORT_SYMBOL(xfrm_policy_alloc
);
307 static void xfrm_policy_destroy_rcu(struct rcu_head
*head
)
309 struct xfrm_policy
*policy
= container_of(head
, struct xfrm_policy
, rcu
);
311 security_xfrm_policy_free(policy
->security
);
315 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
317 void xfrm_policy_destroy(struct xfrm_policy
*policy
)
319 BUG_ON(!policy
->walk
.dead
);
321 if (del_timer(&policy
->timer
) || del_timer(&policy
->polq
.hold_timer
))
324 call_rcu(&policy
->rcu
, xfrm_policy_destroy_rcu
);
326 EXPORT_SYMBOL(xfrm_policy_destroy
);
328 /* Rule must be locked. Release descendant resources, announce
329 * entry dead. The rule must be unlinked from lists to the moment.
332 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
334 policy
->walk
.dead
= 1;
336 atomic_inc(&policy
->genid
);
338 if (del_timer(&policy
->polq
.hold_timer
))
339 xfrm_pol_put(policy
);
340 skb_queue_purge(&policy
->polq
.hold_queue
);
342 if (del_timer(&policy
->timer
))
343 xfrm_pol_put(policy
);
345 xfrm_pol_put(policy
);
348 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
350 static inline unsigned int idx_hash(struct net
*net
, u32 index
)
352 return __idx_hash(index
, net
->xfrm
.policy_idx_hmask
);
355 /* calculate policy hash thresholds */
356 static void __get_hash_thresh(struct net
*net
,
357 unsigned short family
, int dir
,
358 u8
*dbits
, u8
*sbits
)
362 *dbits
= net
->xfrm
.policy_bydst
[dir
].dbits4
;
363 *sbits
= net
->xfrm
.policy_bydst
[dir
].sbits4
;
367 *dbits
= net
->xfrm
.policy_bydst
[dir
].dbits6
;
368 *sbits
= net
->xfrm
.policy_bydst
[dir
].sbits6
;
377 static struct hlist_head
*policy_hash_bysel(struct net
*net
,
378 const struct xfrm_selector
*sel
,
379 unsigned short family
, int dir
)
381 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
386 __get_hash_thresh(net
, family
, dir
, &dbits
, &sbits
);
387 hash
= __sel_hash(sel
, family
, hmask
, dbits
, sbits
);
389 if (hash
== hmask
+ 1)
390 return &net
->xfrm
.policy_inexact
[dir
];
392 return rcu_dereference_check(net
->xfrm
.policy_bydst
[dir
].table
,
393 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
)) + hash
;
396 static struct hlist_head
*policy_hash_direct(struct net
*net
,
397 const xfrm_address_t
*daddr
,
398 const xfrm_address_t
*saddr
,
399 unsigned short family
, int dir
)
401 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
406 __get_hash_thresh(net
, family
, dir
, &dbits
, &sbits
);
407 hash
= __addr_hash(daddr
, saddr
, family
, hmask
, dbits
, sbits
);
409 return rcu_dereference_check(net
->xfrm
.policy_bydst
[dir
].table
,
410 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
)) + hash
;
413 static void xfrm_dst_hash_transfer(struct net
*net
,
414 struct hlist_head
*list
,
415 struct hlist_head
*ndsttable
,
416 unsigned int nhashmask
,
419 struct hlist_node
*tmp
, *entry0
= NULL
;
420 struct xfrm_policy
*pol
;
426 hlist_for_each_entry_safe(pol
, tmp
, list
, bydst
) {
429 __get_hash_thresh(net
, pol
->family
, dir
, &dbits
, &sbits
);
430 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
431 pol
->family
, nhashmask
, dbits
, sbits
);
433 hlist_del_rcu(&pol
->bydst
);
434 hlist_add_head_rcu(&pol
->bydst
, ndsttable
+ h
);
439 hlist_del_rcu(&pol
->bydst
);
440 hlist_add_behind_rcu(&pol
->bydst
, entry0
);
442 entry0
= &pol
->bydst
;
444 if (!hlist_empty(list
)) {
450 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
451 struct hlist_head
*nidxtable
,
452 unsigned int nhashmask
)
454 struct hlist_node
*tmp
;
455 struct xfrm_policy
*pol
;
457 hlist_for_each_entry_safe(pol
, tmp
, list
, byidx
) {
460 h
= __idx_hash(pol
->index
, nhashmask
);
461 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
465 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
467 return ((old_hmask
+ 1) << 1) - 1;
470 static void xfrm_bydst_resize(struct net
*net
, int dir
)
472 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
473 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
474 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
475 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
476 struct hlist_head
*odst
;
482 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
483 write_seqcount_begin(&xfrm_policy_hash_generation
);
485 odst
= rcu_dereference_protected(net
->xfrm
.policy_bydst
[dir
].table
,
486 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
488 odst
= rcu_dereference_protected(net
->xfrm
.policy_bydst
[dir
].table
,
489 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
491 for (i
= hmask
; i
>= 0; i
--)
492 xfrm_dst_hash_transfer(net
, odst
+ i
, ndst
, nhashmask
, dir
);
494 rcu_assign_pointer(net
->xfrm
.policy_bydst
[dir
].table
, ndst
);
495 net
->xfrm
.policy_bydst
[dir
].hmask
= nhashmask
;
497 write_seqcount_end(&xfrm_policy_hash_generation
);
498 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
502 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
505 static void xfrm_byidx_resize(struct net
*net
, int total
)
507 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
508 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
509 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
510 struct hlist_head
*oidx
= net
->xfrm
.policy_byidx
;
511 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
517 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
519 for (i
= hmask
; i
>= 0; i
--)
520 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
522 net
->xfrm
.policy_byidx
= nidx
;
523 net
->xfrm
.policy_idx_hmask
= nhashmask
;
525 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
527 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
530 static inline int xfrm_bydst_should_resize(struct net
*net
, int dir
, int *total
)
532 unsigned int cnt
= net
->xfrm
.policy_count
[dir
];
533 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
538 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
545 static inline int xfrm_byidx_should_resize(struct net
*net
, int total
)
547 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
549 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
556 void xfrm_spd_getinfo(struct net
*net
, struct xfrmk_spdinfo
*si
)
558 si
->incnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
];
559 si
->outcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
];
560 si
->fwdcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
];
561 si
->inscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
562 si
->outscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
563 si
->fwdscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
564 si
->spdhcnt
= net
->xfrm
.policy_idx_hmask
;
565 si
->spdhmcnt
= xfrm_policy_hashmax
;
567 EXPORT_SYMBOL(xfrm_spd_getinfo
);
569 static DEFINE_MUTEX(hash_resize_mutex
);
570 static void xfrm_hash_resize(struct work_struct
*work
)
572 struct net
*net
= container_of(work
, struct net
, xfrm
.policy_hash_work
);
575 mutex_lock(&hash_resize_mutex
);
578 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
579 if (xfrm_bydst_should_resize(net
, dir
, &total
))
580 xfrm_bydst_resize(net
, dir
);
582 if (xfrm_byidx_should_resize(net
, total
))
583 xfrm_byidx_resize(net
, total
);
585 mutex_unlock(&hash_resize_mutex
);
588 static void xfrm_hash_rebuild(struct work_struct
*work
)
590 struct net
*net
= container_of(work
, struct net
,
591 xfrm
.policy_hthresh
.work
);
593 struct xfrm_policy
*pol
;
594 struct xfrm_policy
*policy
;
595 struct hlist_head
*chain
;
596 struct hlist_head
*odst
;
597 struct hlist_node
*newpos
;
601 u8 lbits4
, rbits4
, lbits6
, rbits6
;
603 mutex_lock(&hash_resize_mutex
);
605 /* read selector prefixlen thresholds */
607 seq
= read_seqbegin(&net
->xfrm
.policy_hthresh
.lock
);
609 lbits4
= net
->xfrm
.policy_hthresh
.lbits4
;
610 rbits4
= net
->xfrm
.policy_hthresh
.rbits4
;
611 lbits6
= net
->xfrm
.policy_hthresh
.lbits6
;
612 rbits6
= net
->xfrm
.policy_hthresh
.rbits6
;
613 } while (read_seqretry(&net
->xfrm
.policy_hthresh
.lock
, seq
));
615 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
617 /* reset the bydst and inexact table in all directions */
618 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
619 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
620 hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
621 odst
= net
->xfrm
.policy_bydst
[dir
].table
;
622 for (i
= hmask
; i
>= 0; i
--)
623 INIT_HLIST_HEAD(odst
+ i
);
624 if ((dir
& XFRM_POLICY_MASK
) == XFRM_POLICY_OUT
) {
625 /* dir out => dst = remote, src = local */
626 net
->xfrm
.policy_bydst
[dir
].dbits4
= rbits4
;
627 net
->xfrm
.policy_bydst
[dir
].sbits4
= lbits4
;
628 net
->xfrm
.policy_bydst
[dir
].dbits6
= rbits6
;
629 net
->xfrm
.policy_bydst
[dir
].sbits6
= lbits6
;
631 /* dir in/fwd => dst = local, src = remote */
632 net
->xfrm
.policy_bydst
[dir
].dbits4
= lbits4
;
633 net
->xfrm
.policy_bydst
[dir
].sbits4
= rbits4
;
634 net
->xfrm
.policy_bydst
[dir
].dbits6
= lbits6
;
635 net
->xfrm
.policy_bydst
[dir
].sbits6
= rbits6
;
639 /* re-insert all policies by order of creation */
640 list_for_each_entry_reverse(policy
, &net
->xfrm
.policy_all
, walk
.all
) {
641 if (xfrm_policy_id2dir(policy
->index
) >= XFRM_POLICY_MAX
) {
642 /* skip socket policies */
646 chain
= policy_hash_bysel(net
, &policy
->selector
,
648 xfrm_policy_id2dir(policy
->index
));
649 hlist_for_each_entry(pol
, chain
, bydst
) {
650 if (policy
->priority
>= pol
->priority
)
651 newpos
= &pol
->bydst
;
656 hlist_add_behind(&policy
->bydst
, newpos
);
658 hlist_add_head(&policy
->bydst
, chain
);
661 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
663 mutex_unlock(&hash_resize_mutex
);
666 void xfrm_policy_hash_rebuild(struct net
*net
)
668 schedule_work(&net
->xfrm
.policy_hthresh
.work
);
670 EXPORT_SYMBOL(xfrm_policy_hash_rebuild
);
672 /* Generate new index... KAME seems to generate them ordered by cost
673 * of an absolute inpredictability of ordering of rules. This will not pass. */
674 static u32
xfrm_gen_index(struct net
*net
, int dir
, u32 index
)
676 static u32 idx_generator
;
679 struct hlist_head
*list
;
680 struct xfrm_policy
*p
;
685 idx
= (idx_generator
| dir
);
694 list
= net
->xfrm
.policy_byidx
+ idx_hash(net
, idx
);
696 hlist_for_each_entry(p
, list
, byidx
) {
697 if (p
->index
== idx
) {
707 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
709 u32
*p1
= (u32
*) s1
;
710 u32
*p2
= (u32
*) s2
;
711 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
714 for (i
= 0; i
< len
; i
++) {
722 static void xfrm_policy_requeue(struct xfrm_policy
*old
,
723 struct xfrm_policy
*new)
725 struct xfrm_policy_queue
*pq
= &old
->polq
;
726 struct sk_buff_head list
;
728 if (skb_queue_empty(&pq
->hold_queue
))
731 __skb_queue_head_init(&list
);
733 spin_lock_bh(&pq
->hold_queue
.lock
);
734 skb_queue_splice_init(&pq
->hold_queue
, &list
);
735 if (del_timer(&pq
->hold_timer
))
737 spin_unlock_bh(&pq
->hold_queue
.lock
);
741 spin_lock_bh(&pq
->hold_queue
.lock
);
742 skb_queue_splice(&list
, &pq
->hold_queue
);
743 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
744 if (!mod_timer(&pq
->hold_timer
, jiffies
))
746 spin_unlock_bh(&pq
->hold_queue
.lock
);
749 static bool xfrm_policy_mark_match(struct xfrm_policy
*policy
,
750 struct xfrm_policy
*pol
)
752 u32 mark
= policy
->mark
.v
& policy
->mark
.m
;
754 if (policy
->mark
.v
== pol
->mark
.v
&& policy
->mark
.m
== pol
->mark
.m
)
757 if ((mark
& pol
->mark
.m
) == pol
->mark
.v
&&
758 policy
->priority
== pol
->priority
)
764 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
766 struct net
*net
= xp_net(policy
);
767 struct xfrm_policy
*pol
;
768 struct xfrm_policy
*delpol
;
769 struct hlist_head
*chain
;
770 struct hlist_node
*newpos
;
772 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
773 chain
= policy_hash_bysel(net
, &policy
->selector
, policy
->family
, dir
);
776 hlist_for_each_entry(pol
, chain
, bydst
) {
777 if (pol
->type
== policy
->type
&&
778 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
779 xfrm_policy_mark_match(policy
, pol
) &&
780 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
783 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
787 if (policy
->priority
> pol
->priority
)
789 } else if (policy
->priority
>= pol
->priority
) {
790 newpos
= &pol
->bydst
;
797 hlist_add_behind(&policy
->bydst
, newpos
);
799 hlist_add_head(&policy
->bydst
, chain
);
800 __xfrm_policy_link(policy
, dir
);
801 atomic_inc(&net
->xfrm
.flow_cache_genid
);
803 /* After previous checking, family can either be AF_INET or AF_INET6 */
804 if (policy
->family
== AF_INET
)
805 rt_genid_bump_ipv4(net
);
807 rt_genid_bump_ipv6(net
);
810 xfrm_policy_requeue(delpol
, policy
);
811 __xfrm_policy_unlink(delpol
, dir
);
813 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(net
, dir
, policy
->index
);
814 hlist_add_head(&policy
->byidx
, net
->xfrm
.policy_byidx
+idx_hash(net
, policy
->index
));
815 policy
->curlft
.add_time
= get_seconds();
816 policy
->curlft
.use_time
= 0;
817 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
818 xfrm_pol_hold(policy
);
819 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
822 xfrm_policy_kill(delpol
);
823 else if (xfrm_bydst_should_resize(net
, dir
, NULL
))
824 schedule_work(&net
->xfrm
.policy_hash_work
);
828 EXPORT_SYMBOL(xfrm_policy_insert
);
830 struct xfrm_policy
*xfrm_policy_bysel_ctx(struct net
*net
, u32 mark
, u8 type
,
831 int dir
, struct xfrm_selector
*sel
,
832 struct xfrm_sec_ctx
*ctx
, int delete,
835 struct xfrm_policy
*pol
, *ret
;
836 struct hlist_head
*chain
;
839 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
840 chain
= policy_hash_bysel(net
, sel
, sel
->family
, dir
);
842 hlist_for_each_entry(pol
, chain
, bydst
) {
843 if (pol
->type
== type
&&
844 (mark
& pol
->mark
.m
) == pol
->mark
.v
&&
845 !selector_cmp(sel
, &pol
->selector
) &&
846 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
849 *err
= security_xfrm_policy_delete(
852 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
855 __xfrm_policy_unlink(pol
, dir
);
861 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
864 xfrm_policy_kill(ret
);
867 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
869 struct xfrm_policy
*xfrm_policy_byid(struct net
*net
, u32 mark
, u8 type
,
870 int dir
, u32 id
, int delete, int *err
)
872 struct xfrm_policy
*pol
, *ret
;
873 struct hlist_head
*chain
;
876 if (xfrm_policy_id2dir(id
) != dir
)
880 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
881 chain
= net
->xfrm
.policy_byidx
+ idx_hash(net
, id
);
883 hlist_for_each_entry(pol
, chain
, byidx
) {
884 if (pol
->type
== type
&& pol
->index
== id
&&
885 (mark
& pol
->mark
.m
) == pol
->mark
.v
) {
888 *err
= security_xfrm_policy_delete(
891 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
894 __xfrm_policy_unlink(pol
, dir
);
900 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
903 xfrm_policy_kill(ret
);
906 EXPORT_SYMBOL(xfrm_policy_byid
);
908 #ifdef CONFIG_SECURITY_NETWORK_XFRM
910 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
914 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
915 struct xfrm_policy
*pol
;
918 hlist_for_each_entry(pol
,
919 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
920 if (pol
->type
!= type
)
922 err
= security_xfrm_policy_delete(pol
->security
);
924 xfrm_audit_policy_delete(pol
, 0, task_valid
);
928 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
929 hlist_for_each_entry(pol
,
930 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
932 if (pol
->type
!= type
)
934 err
= security_xfrm_policy_delete(
937 xfrm_audit_policy_delete(pol
, 0,
948 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
954 int xfrm_policy_flush(struct net
*net
, u8 type
, bool task_valid
)
956 int dir
, err
= 0, cnt
= 0;
958 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
960 err
= xfrm_policy_flush_secctx_check(net
, type
, task_valid
);
964 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
965 struct xfrm_policy
*pol
;
969 hlist_for_each_entry(pol
,
970 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
971 if (pol
->type
!= type
)
973 __xfrm_policy_unlink(pol
, dir
);
974 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
977 xfrm_audit_policy_delete(pol
, 1, task_valid
);
979 xfrm_policy_kill(pol
);
981 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
985 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
987 hlist_for_each_entry(pol
,
988 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
990 if (pol
->type
!= type
)
992 __xfrm_policy_unlink(pol
, dir
);
993 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
996 xfrm_audit_policy_delete(pol
, 1, task_valid
);
997 xfrm_policy_kill(pol
);
999 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1008 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1011 xfrm_garbage_collect(net
);
1015 EXPORT_SYMBOL(xfrm_policy_flush
);
1017 int xfrm_policy_walk(struct net
*net
, struct xfrm_policy_walk
*walk
,
1018 int (*func
)(struct xfrm_policy
*, int, int, void*),
1021 struct xfrm_policy
*pol
;
1022 struct xfrm_policy_walk_entry
*x
;
1025 if (walk
->type
>= XFRM_POLICY_TYPE_MAX
&&
1026 walk
->type
!= XFRM_POLICY_TYPE_ANY
)
1029 if (list_empty(&walk
->walk
.all
) && walk
->seq
!= 0)
1032 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1033 if (list_empty(&walk
->walk
.all
))
1034 x
= list_first_entry(&net
->xfrm
.policy_all
, struct xfrm_policy_walk_entry
, all
);
1036 x
= list_first_entry(&walk
->walk
.all
,
1037 struct xfrm_policy_walk_entry
, all
);
1039 list_for_each_entry_from(x
, &net
->xfrm
.policy_all
, all
) {
1042 pol
= container_of(x
, struct xfrm_policy
, walk
);
1043 if (walk
->type
!= XFRM_POLICY_TYPE_ANY
&&
1044 walk
->type
!= pol
->type
)
1046 error
= func(pol
, xfrm_policy_id2dir(pol
->index
),
1049 list_move_tail(&walk
->walk
.all
, &x
->all
);
1054 if (walk
->seq
== 0) {
1058 list_del_init(&walk
->walk
.all
);
1060 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1063 EXPORT_SYMBOL(xfrm_policy_walk
);
1065 void xfrm_policy_walk_init(struct xfrm_policy_walk
*walk
, u8 type
)
1067 INIT_LIST_HEAD(&walk
->walk
.all
);
1068 walk
->walk
.dead
= 1;
1072 EXPORT_SYMBOL(xfrm_policy_walk_init
);
1074 void xfrm_policy_walk_done(struct xfrm_policy_walk
*walk
, struct net
*net
)
1076 if (list_empty(&walk
->walk
.all
))
1079 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
); /*FIXME where is net? */
1080 list_del(&walk
->walk
.all
);
1081 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1083 EXPORT_SYMBOL(xfrm_policy_walk_done
);
1086 * Find policy to apply to this flow.
1088 * Returns 0 if policy found, else an -errno.
1090 static int xfrm_policy_match(const struct xfrm_policy
*pol
,
1091 const struct flowi
*fl
,
1092 u8 type
, u16 family
, int dir
)
1094 const struct xfrm_selector
*sel
= &pol
->selector
;
1098 if (pol
->family
!= family
||
1099 (fl
->flowi_mark
& pol
->mark
.m
) != pol
->mark
.v
||
1103 match
= xfrm_selector_match(sel
, fl
, family
);
1105 ret
= security_xfrm_policy_lookup(pol
->security
, fl
->flowi_secid
,
1111 static struct xfrm_policy
*xfrm_policy_lookup_bytype(struct net
*net
, u8 type
,
1112 const struct flowi
*fl
,
1116 struct xfrm_policy
*pol
, *ret
;
1117 const xfrm_address_t
*daddr
, *saddr
;
1118 struct hlist_head
*chain
;
1119 unsigned int sequence
;
1122 daddr
= xfrm_flowi_daddr(fl
, family
);
1123 saddr
= xfrm_flowi_saddr(fl
, family
);
1124 if (unlikely(!daddr
|| !saddr
))
1130 sequence
= read_seqcount_begin(&xfrm_policy_hash_generation
);
1131 chain
= policy_hash_direct(net
, daddr
, saddr
, family
, dir
);
1132 } while (read_seqcount_retry(&xfrm_policy_hash_generation
, sequence
));
1136 hlist_for_each_entry_rcu(pol
, chain
, bydst
) {
1137 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1147 priority
= ret
->priority
;
1151 chain
= &net
->xfrm
.policy_inexact
[dir
];
1152 hlist_for_each_entry_rcu(pol
, chain
, bydst
) {
1153 if ((pol
->priority
>= priority
) && ret
)
1156 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1170 if (read_seqcount_retry(&xfrm_policy_hash_generation
, sequence
))
1173 if (ret
&& !xfrm_pol_hold_rcu(ret
))
1181 static struct xfrm_policy
*
1182 __xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
)
1184 #ifdef CONFIG_XFRM_SUB_POLICY
1185 struct xfrm_policy
*pol
;
1187 pol
= xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
1191 return xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
1194 static int flow_to_policy_dir(int dir
)
1196 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1197 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1198 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1204 return XFRM_POLICY_IN
;
1206 return XFRM_POLICY_OUT
;
1208 return XFRM_POLICY_FWD
;
1212 static struct flow_cache_object
*
1213 xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
,
1214 u8 dir
, struct flow_cache_object
*old_obj
, void *ctx
)
1216 struct xfrm_policy
*pol
;
1219 xfrm_pol_put(container_of(old_obj
, struct xfrm_policy
, flo
));
1221 pol
= __xfrm_policy_lookup(net
, fl
, family
, flow_to_policy_dir(dir
));
1222 if (IS_ERR_OR_NULL(pol
))
1223 return ERR_CAST(pol
);
1225 /* Resolver returns two references:
1226 * one for cache and one for caller of flow_cache_lookup() */
1232 static inline int policy_to_flow_dir(int dir
)
1234 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1235 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1236 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1240 case XFRM_POLICY_IN
:
1242 case XFRM_POLICY_OUT
:
1243 return FLOW_DIR_OUT
;
1244 case XFRM_POLICY_FWD
:
1245 return FLOW_DIR_FWD
;
1249 static struct xfrm_policy
*xfrm_sk_policy_lookup(const struct sock
*sk
, int dir
,
1250 const struct flowi
*fl
, u16 family
)
1252 struct xfrm_policy
*pol
;
1256 pol
= rcu_dereference(sk
->sk_policy
[dir
]);
1258 bool match
= xfrm_selector_match(&pol
->selector
, fl
, family
);
1262 if ((sk
->sk_mark
& pol
->mark
.m
) != pol
->mark
.v
) {
1266 err
= security_xfrm_policy_lookup(pol
->security
,
1268 policy_to_flow_dir(dir
));
1270 if (!xfrm_pol_hold_rcu(pol
))
1272 } else if (err
== -ESRCH
) {
1285 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1287 struct net
*net
= xp_net(pol
);
1289 list_add(&pol
->walk
.all
, &net
->xfrm
.policy_all
);
1290 net
->xfrm
.policy_count
[dir
]++;
1294 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1297 struct net
*net
= xp_net(pol
);
1299 if (list_empty(&pol
->walk
.all
))
1302 /* Socket policies are not hashed. */
1303 if (!hlist_unhashed(&pol
->bydst
)) {
1304 hlist_del_rcu(&pol
->bydst
);
1305 hlist_del(&pol
->byidx
);
1308 list_del_init(&pol
->walk
.all
);
1309 net
->xfrm
.policy_count
[dir
]--;
1314 static void xfrm_sk_policy_link(struct xfrm_policy
*pol
, int dir
)
1316 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+ dir
);
1319 static void xfrm_sk_policy_unlink(struct xfrm_policy
*pol
, int dir
)
1321 __xfrm_policy_unlink(pol
, XFRM_POLICY_MAX
+ dir
);
1324 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1326 struct net
*net
= xp_net(pol
);
1328 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1329 pol
= __xfrm_policy_unlink(pol
, dir
);
1330 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1332 xfrm_policy_kill(pol
);
1337 EXPORT_SYMBOL(xfrm_policy_delete
);
1339 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1341 struct net
*net
= xp_net(pol
);
1342 struct xfrm_policy
*old_pol
;
1344 #ifdef CONFIG_XFRM_SUB_POLICY
1345 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1349 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1350 old_pol
= rcu_dereference_protected(sk
->sk_policy
[dir
],
1351 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
1353 pol
->curlft
.add_time
= get_seconds();
1354 pol
->index
= xfrm_gen_index(net
, XFRM_POLICY_MAX
+dir
, 0);
1355 xfrm_sk_policy_link(pol
, dir
);
1357 rcu_assign_pointer(sk
->sk_policy
[dir
], pol
);
1360 xfrm_policy_requeue(old_pol
, pol
);
1362 /* Unlinking succeeds always. This is the only function
1363 * allowed to delete or replace socket policy.
1365 xfrm_sk_policy_unlink(old_pol
, dir
);
1367 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1370 xfrm_policy_kill(old_pol
);
1375 static struct xfrm_policy
*clone_policy(const struct xfrm_policy
*old
, int dir
)
1377 struct xfrm_policy
*newp
= xfrm_policy_alloc(xp_net(old
), GFP_ATOMIC
);
1378 struct net
*net
= xp_net(old
);
1381 newp
->selector
= old
->selector
;
1382 if (security_xfrm_policy_clone(old
->security
,
1385 return NULL
; /* ENOMEM */
1387 newp
->lft
= old
->lft
;
1388 newp
->curlft
= old
->curlft
;
1389 newp
->mark
= old
->mark
;
1390 newp
->action
= old
->action
;
1391 newp
->flags
= old
->flags
;
1392 newp
->xfrm_nr
= old
->xfrm_nr
;
1393 newp
->index
= old
->index
;
1394 newp
->type
= old
->type
;
1395 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1396 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1397 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1398 xfrm_sk_policy_link(newp
, dir
);
1399 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1405 int __xfrm_sk_clone_policy(struct sock
*sk
, const struct sock
*osk
)
1407 const struct xfrm_policy
*p
;
1408 struct xfrm_policy
*np
;
1412 for (i
= 0; i
< 2; i
++) {
1413 p
= rcu_dereference(osk
->sk_policy
[i
]);
1415 np
= clone_policy(p
, i
);
1416 if (unlikely(!np
)) {
1420 rcu_assign_pointer(sk
->sk_policy
[i
], np
);
1428 xfrm_get_saddr(struct net
*net
, int oif
, xfrm_address_t
*local
,
1429 xfrm_address_t
*remote
, unsigned short family
)
1432 const struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1434 if (unlikely(afinfo
== NULL
))
1436 err
= afinfo
->get_saddr(net
, oif
, local
, remote
);
1441 /* Resolve list of templates for the flow, given policy. */
1444 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, const struct flowi
*fl
,
1445 struct xfrm_state
**xfrm
, unsigned short family
)
1447 struct net
*net
= xp_net(policy
);
1450 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1451 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1454 for (nx
= 0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1455 struct xfrm_state
*x
;
1456 xfrm_address_t
*remote
= daddr
;
1457 xfrm_address_t
*local
= saddr
;
1458 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1460 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1461 tmpl
->mode
== XFRM_MODE_BEET
) {
1462 remote
= &tmpl
->id
.daddr
;
1463 local
= &tmpl
->saddr
;
1464 if (xfrm_addr_any(local
, tmpl
->encap_family
)) {
1465 error
= xfrm_get_saddr(net
, fl
->flowi_oif
,
1467 tmpl
->encap_family
);
1474 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1476 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1483 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1486 } else if (error
== -ESRCH
) {
1490 if (!tmpl
->optional
)
1496 for (nx
--; nx
>= 0; nx
--)
1497 xfrm_state_put(xfrm
[nx
]);
1502 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, const struct flowi
*fl
,
1503 struct xfrm_state
**xfrm
, unsigned short family
)
1505 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1506 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1512 for (i
= 0; i
< npols
; i
++) {
1513 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1518 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1526 /* found states are sorted for outbound processing */
1528 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1533 for (cnx
--; cnx
>= 0; cnx
--)
1534 xfrm_state_put(tpp
[cnx
]);
1539 static int xfrm_get_tos(const struct flowi
*fl
, int family
)
1541 const struct xfrm_policy_afinfo
*afinfo
;
1544 afinfo
= xfrm_policy_get_afinfo(family
);
1545 tos
= afinfo
? afinfo
->get_tos(fl
) : 0;
1552 static struct flow_cache_object
*xfrm_bundle_flo_get(struct flow_cache_object
*flo
)
1554 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1555 struct dst_entry
*dst
= &xdst
->u
.dst
;
1557 if (xdst
->route
== NULL
) {
1558 /* Dummy bundle - if it has xfrms we were not
1559 * able to build bundle as template resolution failed.
1560 * It means we need to try again resolving. */
1561 if (xdst
->num_xfrms
> 0)
1563 } else if (dst
->flags
& DST_XFRM_QUEUE
) {
1567 if (stale_bundle(dst
))
1575 static int xfrm_bundle_flo_check(struct flow_cache_object
*flo
)
1577 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1578 struct dst_entry
*dst
= &xdst
->u
.dst
;
1582 if (stale_bundle(dst
))
1588 static void xfrm_bundle_flo_delete(struct flow_cache_object
*flo
)
1590 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1591 struct dst_entry
*dst
= &xdst
->u
.dst
;
1596 static const struct flow_cache_ops xfrm_bundle_fc_ops
= {
1597 .get
= xfrm_bundle_flo_get
,
1598 .check
= xfrm_bundle_flo_check
,
1599 .delete = xfrm_bundle_flo_delete
,
1602 static inline struct xfrm_dst
*xfrm_alloc_dst(struct net
*net
, int family
)
1604 const struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1605 struct dst_ops
*dst_ops
;
1606 struct xfrm_dst
*xdst
;
1609 return ERR_PTR(-EINVAL
);
1613 dst_ops
= &net
->xfrm
.xfrm4_dst_ops
;
1615 #if IS_ENABLED(CONFIG_IPV6)
1617 dst_ops
= &net
->xfrm
.xfrm6_dst_ops
;
1623 xdst
= dst_alloc(dst_ops
, NULL
, 0, DST_OBSOLETE_NONE
, 0);
1626 struct dst_entry
*dst
= &xdst
->u
.dst
;
1628 memset(dst
+ 1, 0, sizeof(*xdst
) - sizeof(*dst
));
1629 xdst
->flo
.ops
= &xfrm_bundle_fc_ops
;
1631 xdst
= ERR_PTR(-ENOBUFS
);
1638 static inline int xfrm_init_path(struct xfrm_dst
*path
, struct dst_entry
*dst
,
1641 const struct xfrm_policy_afinfo
*afinfo
=
1642 xfrm_policy_get_afinfo(dst
->ops
->family
);
1648 err
= afinfo
->init_path(path
, dst
, nfheader_len
);
1655 static inline int xfrm_fill_dst(struct xfrm_dst
*xdst
, struct net_device
*dev
,
1656 const struct flowi
*fl
)
1658 const struct xfrm_policy_afinfo
*afinfo
=
1659 xfrm_policy_get_afinfo(xdst
->u
.dst
.ops
->family
);
1665 err
= afinfo
->fill_dst(xdst
, dev
, fl
);
1673 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1674 * all the metrics... Shortly, bundle a bundle.
1677 static struct dst_entry
*xfrm_bundle_create(struct xfrm_policy
*policy
,
1678 struct xfrm_state
**xfrm
, int nx
,
1679 const struct flowi
*fl
,
1680 struct dst_entry
*dst
)
1682 struct net
*net
= xp_net(policy
);
1683 unsigned long now
= jiffies
;
1684 struct net_device
*dev
;
1685 struct xfrm_mode
*inner_mode
;
1686 struct dst_entry
*dst_prev
= NULL
;
1687 struct dst_entry
*dst0
= NULL
;
1691 int nfheader_len
= 0;
1692 int trailer_len
= 0;
1694 int family
= policy
->selector
.family
;
1695 xfrm_address_t saddr
, daddr
;
1697 xfrm_flowi_addr_get(fl
, &saddr
, &daddr
, family
);
1699 tos
= xfrm_get_tos(fl
, family
);
1703 for (; i
< nx
; i
++) {
1704 struct xfrm_dst
*xdst
= xfrm_alloc_dst(net
, family
);
1705 struct dst_entry
*dst1
= &xdst
->u
.dst
;
1707 err
= PTR_ERR(xdst
);
1713 if (xfrm
[i
]->sel
.family
== AF_UNSPEC
) {
1714 inner_mode
= xfrm_ip2inner_mode(xfrm
[i
],
1715 xfrm_af2proto(family
));
1717 err
= -EAFNOSUPPORT
;
1722 inner_mode
= xfrm
[i
]->inner_mode
;
1727 dst_prev
->child
= dst_clone(dst1
);
1728 dst1
->flags
|= DST_NOHASH
;
1732 dst_copy_metrics(dst1
, dst
);
1734 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1735 family
= xfrm
[i
]->props
.family
;
1736 dst
= xfrm_dst_lookup(xfrm
[i
], tos
, fl
->flowi_oif
,
1737 &saddr
, &daddr
, family
);
1744 dst1
->xfrm
= xfrm
[i
];
1745 xdst
->xfrm_genid
= xfrm
[i
]->genid
;
1747 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
1748 dst1
->flags
|= DST_HOST
;
1749 dst1
->lastuse
= now
;
1751 dst1
->input
= dst_discard
;
1752 dst1
->output
= inner_mode
->afinfo
->output
;
1754 dst1
->next
= dst_prev
;
1757 header_len
+= xfrm
[i
]->props
.header_len
;
1758 if (xfrm
[i
]->type
->flags
& XFRM_TYPE_NON_FRAGMENT
)
1759 nfheader_len
+= xfrm
[i
]->props
.header_len
;
1760 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
1763 dst_prev
->child
= dst
;
1771 xfrm_init_path((struct xfrm_dst
*)dst0
, dst
, nfheader_len
);
1772 xfrm_init_pmtu(dst_prev
);
1774 for (dst_prev
= dst0
; dst_prev
!= dst
; dst_prev
= dst_prev
->child
) {
1775 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst_prev
;
1777 err
= xfrm_fill_dst(xdst
, dev
, fl
);
1781 dst_prev
->header_len
= header_len
;
1782 dst_prev
->trailer_len
= trailer_len
;
1783 header_len
-= xdst
->u
.dst
.xfrm
->props
.header_len
;
1784 trailer_len
-= xdst
->u
.dst
.xfrm
->props
.trailer_len
;
1792 xfrm_state_put(xfrm
[i
]);
1796 dst0
= ERR_PTR(err
);
1800 static int xfrm_expand_policies(const struct flowi
*fl
, u16 family
,
1801 struct xfrm_policy
**pols
,
1802 int *num_pols
, int *num_xfrms
)
1806 if (*num_pols
== 0 || !pols
[0]) {
1811 if (IS_ERR(pols
[0]))
1812 return PTR_ERR(pols
[0]);
1814 *num_xfrms
= pols
[0]->xfrm_nr
;
1816 #ifdef CONFIG_XFRM_SUB_POLICY
1817 if (pols
[0] && pols
[0]->action
== XFRM_POLICY_ALLOW
&&
1818 pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1819 pols
[1] = xfrm_policy_lookup_bytype(xp_net(pols
[0]),
1820 XFRM_POLICY_TYPE_MAIN
,
1824 if (IS_ERR(pols
[1])) {
1825 xfrm_pols_put(pols
, *num_pols
);
1826 return PTR_ERR(pols
[1]);
1829 (*num_xfrms
) += pols
[1]->xfrm_nr
;
1833 for (i
= 0; i
< *num_pols
; i
++) {
1834 if (pols
[i
]->action
!= XFRM_POLICY_ALLOW
) {
1844 static struct xfrm_dst
*
1845 xfrm_resolve_and_create_bundle(struct xfrm_policy
**pols
, int num_pols
,
1846 const struct flowi
*fl
, u16 family
,
1847 struct dst_entry
*dst_orig
)
1849 struct net
*net
= xp_net(pols
[0]);
1850 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1851 struct dst_entry
*dst
;
1852 struct xfrm_dst
*xdst
;
1855 /* Try to instantiate a bundle */
1856 err
= xfrm_tmpl_resolve(pols
, num_pols
, fl
, xfrm
, family
);
1858 if (err
!= 0 && err
!= -EAGAIN
)
1859 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1860 return ERR_PTR(err
);
1863 dst
= xfrm_bundle_create(pols
[0], xfrm
, err
, fl
, dst_orig
);
1865 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLEGENERROR
);
1866 return ERR_CAST(dst
);
1869 xdst
= (struct xfrm_dst
*)dst
;
1870 xdst
->num_xfrms
= err
;
1871 xdst
->num_pols
= num_pols
;
1872 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1873 xdst
->policy_genid
= atomic_read(&pols
[0]->genid
);
1878 static void xfrm_policy_queue_process(unsigned long arg
)
1880 struct sk_buff
*skb
;
1882 struct dst_entry
*dst
;
1883 struct xfrm_policy
*pol
= (struct xfrm_policy
*)arg
;
1884 struct net
*net
= xp_net(pol
);
1885 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1887 struct sk_buff_head list
;
1889 spin_lock(&pq
->hold_queue
.lock
);
1890 skb
= skb_peek(&pq
->hold_queue
);
1892 spin_unlock(&pq
->hold_queue
.lock
);
1897 xfrm_decode_session(skb
, &fl
, dst
->ops
->family
);
1898 spin_unlock(&pq
->hold_queue
.lock
);
1900 dst_hold(dst
->path
);
1901 dst
= xfrm_lookup(net
, dst
->path
, &fl
, sk
, 0);
1905 if (dst
->flags
& DST_XFRM_QUEUE
) {
1908 if (pq
->timeout
>= XFRM_QUEUE_TMO_MAX
)
1911 pq
->timeout
= pq
->timeout
<< 1;
1912 if (!mod_timer(&pq
->hold_timer
, jiffies
+ pq
->timeout
))
1919 __skb_queue_head_init(&list
);
1921 spin_lock(&pq
->hold_queue
.lock
);
1923 skb_queue_splice_init(&pq
->hold_queue
, &list
);
1924 spin_unlock(&pq
->hold_queue
.lock
);
1926 while (!skb_queue_empty(&list
)) {
1927 skb
= __skb_dequeue(&list
);
1929 xfrm_decode_session(skb
, &fl
, skb_dst(skb
)->ops
->family
);
1930 dst_hold(skb_dst(skb
)->path
);
1931 dst
= xfrm_lookup(net
, skb_dst(skb
)->path
, &fl
, skb
->sk
, 0);
1939 skb_dst_set(skb
, dst
);
1941 dst_output(net
, skb
->sk
, skb
);
1950 skb_queue_purge(&pq
->hold_queue
);
1954 static int xdst_queue_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
1956 unsigned long sched_next
;
1957 struct dst_entry
*dst
= skb_dst(skb
);
1958 struct xfrm_dst
*xdst
= (struct xfrm_dst
*) dst
;
1959 struct xfrm_policy
*pol
= xdst
->pols
[0];
1960 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1962 if (unlikely(skb_fclone_busy(sk
, skb
))) {
1967 if (pq
->hold_queue
.qlen
> XFRM_MAX_QUEUE_LEN
) {
1974 spin_lock_bh(&pq
->hold_queue
.lock
);
1977 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
1979 sched_next
= jiffies
+ pq
->timeout
;
1981 if (del_timer(&pq
->hold_timer
)) {
1982 if (time_before(pq
->hold_timer
.expires
, sched_next
))
1983 sched_next
= pq
->hold_timer
.expires
;
1987 __skb_queue_tail(&pq
->hold_queue
, skb
);
1988 if (!mod_timer(&pq
->hold_timer
, sched_next
))
1991 spin_unlock_bh(&pq
->hold_queue
.lock
);
1996 static struct xfrm_dst
*xfrm_create_dummy_bundle(struct net
*net
,
1997 struct xfrm_flo
*xflo
,
1998 const struct flowi
*fl
,
2003 struct net_device
*dev
;
2004 struct dst_entry
*dst
;
2005 struct dst_entry
*dst1
;
2006 struct xfrm_dst
*xdst
;
2008 xdst
= xfrm_alloc_dst(net
, family
);
2012 if (!(xflo
->flags
& XFRM_LOOKUP_QUEUE
) ||
2013 net
->xfrm
.sysctl_larval_drop
||
2017 dst
= xflo
->dst_orig
;
2018 dst1
= &xdst
->u
.dst
;
2022 dst_copy_metrics(dst1
, dst
);
2024 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
2025 dst1
->flags
|= DST_HOST
| DST_XFRM_QUEUE
;
2026 dst1
->lastuse
= jiffies
;
2028 dst1
->input
= dst_discard
;
2029 dst1
->output
= xdst_queue_output
;
2035 xfrm_init_path((struct xfrm_dst
*)dst1
, dst
, 0);
2042 err
= xfrm_fill_dst(xdst
, dev
, fl
);
2051 xdst
= ERR_PTR(err
);
2055 static struct flow_cache_object
*
2056 xfrm_bundle_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
,
2057 struct flow_cache_object
*oldflo
, void *ctx
)
2059 struct xfrm_flo
*xflo
= (struct xfrm_flo
*)ctx
;
2060 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2061 struct xfrm_dst
*xdst
, *new_xdst
;
2062 int num_pols
= 0, num_xfrms
= 0, i
, err
, pol_dead
;
2064 /* Check if the policies from old bundle are usable */
2067 xdst
= container_of(oldflo
, struct xfrm_dst
, flo
);
2068 num_pols
= xdst
->num_pols
;
2069 num_xfrms
= xdst
->num_xfrms
;
2071 for (i
= 0; i
< num_pols
; i
++) {
2072 pols
[i
] = xdst
->pols
[i
];
2073 pol_dead
|= pols
[i
]->walk
.dead
;
2076 dst_free(&xdst
->u
.dst
);
2084 /* Resolve policies to use if we couldn't get them from
2085 * previous cache entry */
2088 pols
[0] = __xfrm_policy_lookup(net
, fl
, family
,
2089 flow_to_policy_dir(dir
));
2090 err
= xfrm_expand_policies(fl
, family
, pols
,
2091 &num_pols
, &num_xfrms
);
2097 goto make_dummy_bundle
;
2100 new_xdst
= xfrm_resolve_and_create_bundle(pols
, num_pols
, fl
, family
,
2102 if (IS_ERR(new_xdst
)) {
2103 err
= PTR_ERR(new_xdst
);
2107 goto make_dummy_bundle
;
2108 dst_hold(&xdst
->u
.dst
);
2110 } else if (new_xdst
== NULL
) {
2113 goto make_dummy_bundle
;
2114 xdst
->num_xfrms
= 0;
2115 dst_hold(&xdst
->u
.dst
);
2119 /* Kill the previous bundle */
2121 /* The policies were stolen for newly generated bundle */
2123 dst_free(&xdst
->u
.dst
);
2126 /* Flow cache does not have reference, it dst_free()'s,
2127 * but we do need to return one reference for original caller */
2128 dst_hold(&new_xdst
->u
.dst
);
2129 return &new_xdst
->flo
;
2132 /* We found policies, but there's no bundles to instantiate:
2133 * either because the policy blocks, has no transformations or
2134 * we could not build template (no xfrm_states).*/
2135 xdst
= xfrm_create_dummy_bundle(net
, xflo
, fl
, num_xfrms
, family
);
2137 xfrm_pols_put(pols
, num_pols
);
2138 return ERR_CAST(xdst
);
2140 xdst
->num_pols
= num_pols
;
2141 xdst
->num_xfrms
= num_xfrms
;
2142 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2144 dst_hold(&xdst
->u
.dst
);
2148 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
2151 dst_free(&xdst
->u
.dst
);
2153 xfrm_pols_put(pols
, num_pols
);
2154 return ERR_PTR(err
);
2157 static struct dst_entry
*make_blackhole(struct net
*net
, u16 family
,
2158 struct dst_entry
*dst_orig
)
2160 const struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2161 struct dst_entry
*ret
;
2164 dst_release(dst_orig
);
2165 return ERR_PTR(-EINVAL
);
2167 ret
= afinfo
->blackhole_route(net
, dst_orig
);
2174 /* Main function: finds/creates a bundle for given flow.
2176 * At the moment we eat a raw IP route. Mostly to speed up lookups
2177 * on interfaces with disabled IPsec.
2179 struct dst_entry
*xfrm_lookup(struct net
*net
, struct dst_entry
*dst_orig
,
2180 const struct flowi
*fl
,
2181 const struct sock
*sk
, int flags
)
2183 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2184 struct flow_cache_object
*flo
;
2185 struct xfrm_dst
*xdst
;
2186 struct dst_entry
*dst
, *route
;
2187 u16 family
= dst_orig
->ops
->family
;
2188 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
2189 int i
, err
, num_pols
, num_xfrms
= 0, drop_pols
= 0;
2195 sk
= sk_const_to_full_sk(sk
);
2196 if (sk
&& sk
->sk_policy
[XFRM_POLICY_OUT
]) {
2198 pols
[0] = xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
, family
);
2199 err
= xfrm_expand_policies(fl
, family
, pols
,
2200 &num_pols
, &num_xfrms
);
2205 if (num_xfrms
<= 0) {
2206 drop_pols
= num_pols
;
2210 xdst
= xfrm_resolve_and_create_bundle(
2214 xfrm_pols_put(pols
, num_pols
);
2215 err
= PTR_ERR(xdst
);
2217 } else if (xdst
== NULL
) {
2219 drop_pols
= num_pols
;
2223 dst_hold(&xdst
->u
.dst
);
2224 xdst
->u
.dst
.flags
|= DST_NOCACHE
;
2225 route
= xdst
->route
;
2230 struct xfrm_flo xflo
;
2232 xflo
.dst_orig
= dst_orig
;
2235 /* To accelerate a bit... */
2236 if ((dst_orig
->flags
& DST_NOXFRM
) ||
2237 !net
->xfrm
.policy_count
[XFRM_POLICY_OUT
])
2240 flo
= flow_cache_lookup(net
, fl
, family
, dir
,
2241 xfrm_bundle_lookup
, &xflo
);
2248 xdst
= container_of(flo
, struct xfrm_dst
, flo
);
2250 num_pols
= xdst
->num_pols
;
2251 num_xfrms
= xdst
->num_xfrms
;
2252 memcpy(pols
, xdst
->pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2253 route
= xdst
->route
;
2257 if (route
== NULL
&& num_xfrms
> 0) {
2258 /* The only case when xfrm_bundle_lookup() returns a
2259 * bundle with null route, is when the template could
2260 * not be resolved. It means policies are there, but
2261 * bundle could not be created, since we don't yet
2262 * have the xfrm_state's. We need to wait for KM to
2263 * negotiate new SA's or bail out with error.*/
2264 if (net
->xfrm
.sysctl_larval_drop
) {
2265 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2272 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2280 if ((flags
& XFRM_LOOKUP_ICMP
) &&
2281 !(pols
[0]->flags
& XFRM_POLICY_ICMP
)) {
2286 for (i
= 0; i
< num_pols
; i
++)
2287 pols
[i
]->curlft
.use_time
= get_seconds();
2289 if (num_xfrms
< 0) {
2290 /* Prohibit the flow */
2291 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLBLOCK
);
2294 } else if (num_xfrms
> 0) {
2295 /* Flow transformed */
2296 dst_release(dst_orig
);
2298 /* Flow passes untransformed */
2303 xfrm_pols_put(pols
, drop_pols
);
2304 if (dst
&& dst
->xfrm
&&
2305 dst
->xfrm
->props
.mode
== XFRM_MODE_TUNNEL
)
2306 dst
->flags
|= DST_XFRM_TUNNEL
;
2310 if (!(flags
& XFRM_LOOKUP_ICMP
)) {
2318 if (!(flags
& XFRM_LOOKUP_KEEP_DST_REF
))
2319 dst_release(dst_orig
);
2320 xfrm_pols_put(pols
, drop_pols
);
2321 return ERR_PTR(err
);
2323 EXPORT_SYMBOL(xfrm_lookup
);
2325 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2326 * Otherwise we may send out blackholed packets.
2328 struct dst_entry
*xfrm_lookup_route(struct net
*net
, struct dst_entry
*dst_orig
,
2329 const struct flowi
*fl
,
2330 const struct sock
*sk
, int flags
)
2332 struct dst_entry
*dst
= xfrm_lookup(net
, dst_orig
, fl
, sk
,
2333 flags
| XFRM_LOOKUP_QUEUE
|
2334 XFRM_LOOKUP_KEEP_DST_REF
);
2336 if (IS_ERR(dst
) && PTR_ERR(dst
) == -EREMOTE
)
2337 return make_blackhole(net
, dst_orig
->ops
->family
, dst_orig
);
2341 EXPORT_SYMBOL(xfrm_lookup_route
);
2344 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, const struct flowi
*fl
)
2346 struct xfrm_state
*x
;
2348 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
2350 x
= skb
->sp
->xvec
[idx
];
2351 if (!x
->type
->reject
)
2353 return x
->type
->reject(x
, skb
, fl
);
2356 /* When skb is transformed back to its "native" form, we have to
2357 * check policy restrictions. At the moment we make this in maximally
2358 * stupid way. Shame on me. :-) Of course, connected sockets must
2359 * have policy cached at them.
2363 xfrm_state_ok(const struct xfrm_tmpl
*tmpl
, const struct xfrm_state
*x
,
2364 unsigned short family
)
2366 if (xfrm_state_kern(x
))
2367 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
2368 return x
->id
.proto
== tmpl
->id
.proto
&&
2369 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
2370 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
2371 x
->props
.mode
== tmpl
->mode
&&
2372 (tmpl
->allalgs
|| (tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
2373 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
2374 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
2375 xfrm_state_addr_cmp(tmpl
, x
, family
));
2379 * 0 or more than 0 is returned when validation is succeeded (either bypass
2380 * because of optional transport mode, or next index of the mathced secpath
2381 * state with the template.
2382 * -1 is returned when no matching template is found.
2383 * Otherwise "-2 - errored_index" is returned.
2386 xfrm_policy_ok(const struct xfrm_tmpl
*tmpl
, const struct sec_path
*sp
, int start
,
2387 unsigned short family
)
2391 if (tmpl
->optional
) {
2392 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
2396 for (; idx
< sp
->len
; idx
++) {
2397 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
2399 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2408 int __xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
,
2409 unsigned int family
, int reverse
)
2411 const struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2414 if (unlikely(afinfo
== NULL
))
2415 return -EAFNOSUPPORT
;
2417 afinfo
->decode_session(skb
, fl
, reverse
);
2418 err
= security_xfrm_decode_session(skb
, &fl
->flowi_secid
);
2422 EXPORT_SYMBOL(__xfrm_decode_session
);
2424 static inline int secpath_has_nontransport(const struct sec_path
*sp
, int k
, int *idxp
)
2426 for (; k
< sp
->len
; k
++) {
2427 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2436 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
2437 unsigned short family
)
2439 struct net
*net
= dev_net(skb
->dev
);
2440 struct xfrm_policy
*pol
;
2441 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2450 reverse
= dir
& ~XFRM_POLICY_MASK
;
2451 dir
&= XFRM_POLICY_MASK
;
2452 fl_dir
= policy_to_flow_dir(dir
);
2454 if (__xfrm_decode_session(skb
, &fl
, family
, reverse
) < 0) {
2455 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
2459 nf_nat_decode_session(skb
, &fl
, family
);
2461 /* First, check used SA against their selectors. */
2465 for (i
= skb
->sp
->len
-1; i
>= 0; i
--) {
2466 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
2467 if (!xfrm_selector_match(&x
->sel
, &fl
, family
)) {
2468 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMISMATCH
);
2475 sk
= sk_to_full_sk(sk
);
2476 if (sk
&& sk
->sk_policy
[dir
]) {
2477 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
, family
);
2479 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2485 struct flow_cache_object
*flo
;
2487 flo
= flow_cache_lookup(net
, &fl
, family
, fl_dir
,
2488 xfrm_policy_lookup
, NULL
);
2489 if (IS_ERR_OR_NULL(flo
))
2490 pol
= ERR_CAST(flo
);
2492 pol
= container_of(flo
, struct xfrm_policy
, flo
);
2496 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2501 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
2502 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2503 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINNOPOLS
);
2509 pol
->curlft
.use_time
= get_seconds();
2513 #ifdef CONFIG_XFRM_SUB_POLICY
2514 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
2515 pols
[1] = xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
,
2519 if (IS_ERR(pols
[1])) {
2520 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2523 pols
[1]->curlft
.use_time
= get_seconds();
2529 if (pol
->action
== XFRM_POLICY_ALLOW
) {
2530 struct sec_path
*sp
;
2531 static struct sec_path dummy
;
2532 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
2533 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
2534 struct xfrm_tmpl
**tpp
= tp
;
2538 if ((sp
= skb
->sp
) == NULL
)
2541 for (pi
= 0; pi
< npols
; pi
++) {
2542 if (pols
[pi
] != pol
&&
2543 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
) {
2544 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2547 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
2548 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
2551 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
2552 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
2556 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
, net
);
2560 /* For each tunnel xfrm, find the first matching tmpl.
2561 * For each tmpl before that, find corresponding xfrm.
2562 * Order is _important_. Later we will implement
2563 * some barriers, but at the moment barriers
2564 * are implied between each two transformations.
2566 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
2567 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
2570 /* "-2 - errored_index" returned */
2572 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2577 if (secpath_has_nontransport(sp
, k
, &xerr_idx
)) {
2578 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2582 xfrm_pols_put(pols
, npols
);
2585 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2588 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2590 xfrm_pols_put(pols
, npols
);
2593 EXPORT_SYMBOL(__xfrm_policy_check
);
2595 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
2597 struct net
*net
= dev_net(skb
->dev
);
2599 struct dst_entry
*dst
;
2602 if (xfrm_decode_session(skb
, &fl
, family
) < 0) {
2603 XFRM_INC_STATS(net
, LINUX_MIB_XFRMFWDHDRERROR
);
2609 dst
= xfrm_lookup(net
, skb_dst(skb
), &fl
, NULL
, XFRM_LOOKUP_QUEUE
);
2614 skb_dst_set(skb
, dst
);
2617 EXPORT_SYMBOL(__xfrm_route_forward
);
2619 /* Optimize later using cookies and generation ids. */
2621 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
2623 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2624 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2625 * get validated by dst_ops->check on every use. We do this
2626 * because when a normal route referenced by an XFRM dst is
2627 * obsoleted we do not go looking around for all parent
2628 * referencing XFRM dsts so that we can invalidate them. It
2629 * is just too much work. Instead we make the checks here on
2630 * every use. For example:
2632 * XFRM dst A --> IPv4 dst X
2634 * X is the "xdst->route" of A (X is also the "dst->path" of A
2635 * in this example). If X is marked obsolete, "A" will not
2636 * notice. That's what we are validating here via the
2637 * stale_bundle() check.
2639 * When a policy's bundle is pruned, we dst_free() the XFRM
2640 * dst which causes it's ->obsolete field to be set to
2641 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2642 * this, we want to force a new route lookup.
2644 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
2650 static int stale_bundle(struct dst_entry
*dst
)
2652 return !xfrm_bundle_ok((struct xfrm_dst
*)dst
);
2655 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
2657 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
2658 dst
->dev
= dev_net(dev
)->loopback_dev
;
2663 EXPORT_SYMBOL(xfrm_dst_ifdown
);
2665 static void xfrm_link_failure(struct sk_buff
*skb
)
2667 /* Impossible. Such dst must be popped before reaches point of failure. */
2670 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
2673 if (dst
->obsolete
) {
2681 void xfrm_garbage_collect(struct net
*net
)
2683 flow_cache_flush(net
);
2685 EXPORT_SYMBOL(xfrm_garbage_collect
);
2687 void xfrm_garbage_collect_deferred(struct net
*net
)
2689 flow_cache_flush_deferred(net
);
2691 EXPORT_SYMBOL(xfrm_garbage_collect_deferred
);
2693 static void xfrm_init_pmtu(struct dst_entry
*dst
)
2696 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2697 u32 pmtu
, route_mtu_cached
;
2699 pmtu
= dst_mtu(dst
->child
);
2700 xdst
->child_mtu_cached
= pmtu
;
2702 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2704 route_mtu_cached
= dst_mtu(xdst
->route
);
2705 xdst
->route_mtu_cached
= route_mtu_cached
;
2707 if (pmtu
> route_mtu_cached
)
2708 pmtu
= route_mtu_cached
;
2710 dst_metric_set(dst
, RTAX_MTU
, pmtu
);
2711 } while ((dst
= dst
->next
));
2714 /* Check that the bundle accepts the flow and its components are
2718 static int xfrm_bundle_ok(struct xfrm_dst
*first
)
2720 struct dst_entry
*dst
= &first
->u
.dst
;
2721 struct xfrm_dst
*last
;
2724 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2725 (dst
->dev
&& !netif_running(dst
->dev
)))
2728 if (dst
->flags
& DST_XFRM_QUEUE
)
2734 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2736 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2738 if (xdst
->xfrm_genid
!= dst
->xfrm
->genid
)
2740 if (xdst
->num_pols
> 0 &&
2741 xdst
->policy_genid
!= atomic_read(&xdst
->pols
[0]->genid
))
2744 mtu
= dst_mtu(dst
->child
);
2745 if (xdst
->child_mtu_cached
!= mtu
) {
2747 xdst
->child_mtu_cached
= mtu
;
2750 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2752 mtu
= dst_mtu(xdst
->route
);
2753 if (xdst
->route_mtu_cached
!= mtu
) {
2755 xdst
->route_mtu_cached
= mtu
;
2759 } while (dst
->xfrm
);
2764 mtu
= last
->child_mtu_cached
;
2768 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2769 if (mtu
> last
->route_mtu_cached
)
2770 mtu
= last
->route_mtu_cached
;
2771 dst_metric_set(dst
, RTAX_MTU
, mtu
);
2776 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2777 last
->child_mtu_cached
= mtu
;
2783 static unsigned int xfrm_default_advmss(const struct dst_entry
*dst
)
2785 return dst_metric_advmss(dst
->path
);
2788 static unsigned int xfrm_mtu(const struct dst_entry
*dst
)
2790 unsigned int mtu
= dst_metric_raw(dst
, RTAX_MTU
);
2792 return mtu
? : dst_mtu(dst
->path
);
2795 static const void *xfrm_get_dst_nexthop(const struct dst_entry
*dst
,
2798 const struct dst_entry
*path
= dst
->path
;
2800 for (; dst
!= path
; dst
= dst
->child
) {
2801 const struct xfrm_state
*xfrm
= dst
->xfrm
;
2803 if (xfrm
->props
.mode
== XFRM_MODE_TRANSPORT
)
2805 if (xfrm
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
)
2806 daddr
= xfrm
->coaddr
;
2807 else if (!(xfrm
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
))
2808 daddr
= &xfrm
->id
.daddr
;
2813 static struct neighbour
*xfrm_neigh_lookup(const struct dst_entry
*dst
,
2814 struct sk_buff
*skb
,
2817 const struct dst_entry
*path
= dst
->path
;
2820 daddr
= xfrm_get_dst_nexthop(dst
, daddr
);
2821 return path
->ops
->neigh_lookup(path
, skb
, daddr
);
2824 static void xfrm_confirm_neigh(const struct dst_entry
*dst
, const void *daddr
)
2826 const struct dst_entry
*path
= dst
->path
;
2828 daddr
= xfrm_get_dst_nexthop(dst
, daddr
);
2829 path
->ops
->confirm_neigh(path
, daddr
);
2832 int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo
*afinfo
, int family
)
2836 if (WARN_ON(family
>= ARRAY_SIZE(xfrm_policy_afinfo
)))
2837 return -EAFNOSUPPORT
;
2839 spin_lock(&xfrm_policy_afinfo_lock
);
2840 if (unlikely(xfrm_policy_afinfo
[family
] != NULL
))
2843 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2844 if (likely(dst_ops
->kmem_cachep
== NULL
))
2845 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2846 if (likely(dst_ops
->check
== NULL
))
2847 dst_ops
->check
= xfrm_dst_check
;
2848 if (likely(dst_ops
->default_advmss
== NULL
))
2849 dst_ops
->default_advmss
= xfrm_default_advmss
;
2850 if (likely(dst_ops
->mtu
== NULL
))
2851 dst_ops
->mtu
= xfrm_mtu
;
2852 if (likely(dst_ops
->negative_advice
== NULL
))
2853 dst_ops
->negative_advice
= xfrm_negative_advice
;
2854 if (likely(dst_ops
->link_failure
== NULL
))
2855 dst_ops
->link_failure
= xfrm_link_failure
;
2856 if (likely(dst_ops
->neigh_lookup
== NULL
))
2857 dst_ops
->neigh_lookup
= xfrm_neigh_lookup
;
2858 if (likely(!dst_ops
->confirm_neigh
))
2859 dst_ops
->confirm_neigh
= xfrm_confirm_neigh
;
2860 rcu_assign_pointer(xfrm_policy_afinfo
[family
], afinfo
);
2862 spin_unlock(&xfrm_policy_afinfo_lock
);
2866 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2868 void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo
*afinfo
)
2870 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2873 for (i
= 0; i
< ARRAY_SIZE(xfrm_policy_afinfo
); i
++) {
2874 if (xfrm_policy_afinfo
[i
] != afinfo
)
2876 RCU_INIT_POINTER(xfrm_policy_afinfo
[i
], NULL
);
2882 dst_ops
->kmem_cachep
= NULL
;
2883 dst_ops
->check
= NULL
;
2884 dst_ops
->negative_advice
= NULL
;
2885 dst_ops
->link_failure
= NULL
;
2887 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2889 #ifdef CONFIG_XFRM_STATISTICS
2890 static int __net_init
xfrm_statistics_init(struct net
*net
)
2893 net
->mib
.xfrm_statistics
= alloc_percpu(struct linux_xfrm_mib
);
2894 if (!net
->mib
.xfrm_statistics
)
2896 rv
= xfrm_proc_init(net
);
2898 free_percpu(net
->mib
.xfrm_statistics
);
2902 static void xfrm_statistics_fini(struct net
*net
)
2904 xfrm_proc_fini(net
);
2905 free_percpu(net
->mib
.xfrm_statistics
);
2908 static int __net_init
xfrm_statistics_init(struct net
*net
)
2913 static void xfrm_statistics_fini(struct net
*net
)
2918 static int __net_init
xfrm_policy_init(struct net
*net
)
2920 unsigned int hmask
, sz
;
2923 if (net_eq(net
, &init_net
))
2924 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2925 sizeof(struct xfrm_dst
),
2926 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2930 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2932 net
->xfrm
.policy_byidx
= xfrm_hash_alloc(sz
);
2933 if (!net
->xfrm
.policy_byidx
)
2935 net
->xfrm
.policy_idx_hmask
= hmask
;
2937 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
2938 struct xfrm_policy_hash
*htab
;
2940 net
->xfrm
.policy_count
[dir
] = 0;
2941 net
->xfrm
.policy_count
[XFRM_POLICY_MAX
+ dir
] = 0;
2942 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
2944 htab
= &net
->xfrm
.policy_bydst
[dir
];
2945 htab
->table
= xfrm_hash_alloc(sz
);
2948 htab
->hmask
= hmask
;
2954 net
->xfrm
.policy_hthresh
.lbits4
= 32;
2955 net
->xfrm
.policy_hthresh
.rbits4
= 32;
2956 net
->xfrm
.policy_hthresh
.lbits6
= 128;
2957 net
->xfrm
.policy_hthresh
.rbits6
= 128;
2959 seqlock_init(&net
->xfrm
.policy_hthresh
.lock
);
2961 INIT_LIST_HEAD(&net
->xfrm
.policy_all
);
2962 INIT_WORK(&net
->xfrm
.policy_hash_work
, xfrm_hash_resize
);
2963 INIT_WORK(&net
->xfrm
.policy_hthresh
.work
, xfrm_hash_rebuild
);
2964 if (net_eq(net
, &init_net
))
2969 for (dir
--; dir
>= 0; dir
--) {
2970 struct xfrm_policy_hash
*htab
;
2972 htab
= &net
->xfrm
.policy_bydst
[dir
];
2973 xfrm_hash_free(htab
->table
, sz
);
2975 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
2980 static void xfrm_policy_fini(struct net
*net
)
2985 flush_work(&net
->xfrm
.policy_hash_work
);
2986 #ifdef CONFIG_XFRM_SUB_POLICY
2987 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_SUB
, false);
2989 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, false);
2991 WARN_ON(!list_empty(&net
->xfrm
.policy_all
));
2993 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
2994 struct xfrm_policy_hash
*htab
;
2996 WARN_ON(!hlist_empty(&net
->xfrm
.policy_inexact
[dir
]));
2998 htab
= &net
->xfrm
.policy_bydst
[dir
];
2999 sz
= (htab
->hmask
+ 1) * sizeof(struct hlist_head
);
3000 WARN_ON(!hlist_empty(htab
->table
));
3001 xfrm_hash_free(htab
->table
, sz
);
3004 sz
= (net
->xfrm
.policy_idx_hmask
+ 1) * sizeof(struct hlist_head
);
3005 WARN_ON(!hlist_empty(net
->xfrm
.policy_byidx
));
3006 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
3009 static int __net_init
xfrm_net_init(struct net
*net
)
3013 /* Initialize the per-net locks here */
3014 spin_lock_init(&net
->xfrm
.xfrm_state_lock
);
3015 spin_lock_init(&net
->xfrm
.xfrm_policy_lock
);
3016 mutex_init(&net
->xfrm
.xfrm_cfg_mutex
);
3018 rv
= xfrm_statistics_init(net
);
3020 goto out_statistics
;
3021 rv
= xfrm_state_init(net
);
3024 rv
= xfrm_policy_init(net
);
3027 rv
= xfrm_sysctl_init(net
);
3030 rv
= flow_cache_init(net
);
3037 xfrm_sysctl_fini(net
);
3039 xfrm_policy_fini(net
);
3041 xfrm_state_fini(net
);
3043 xfrm_statistics_fini(net
);
3048 static void __net_exit
xfrm_net_exit(struct net
*net
)
3050 flow_cache_fini(net
);
3051 xfrm_sysctl_fini(net
);
3052 xfrm_policy_fini(net
);
3053 xfrm_state_fini(net
);
3054 xfrm_statistics_fini(net
);
3057 static struct pernet_operations __net_initdata xfrm_net_ops
= {
3058 .init
= xfrm_net_init
,
3059 .exit
= xfrm_net_exit
,
3062 void __init
xfrm_init(void)
3064 flow_cache_hp_init();
3065 register_pernet_subsys(&xfrm_net_ops
);
3066 seqcount_init(&xfrm_policy_hash_generation
);
3070 #ifdef CONFIG_AUDITSYSCALL
3071 static void xfrm_audit_common_policyinfo(struct xfrm_policy
*xp
,
3072 struct audit_buffer
*audit_buf
)
3074 struct xfrm_sec_ctx
*ctx
= xp
->security
;
3075 struct xfrm_selector
*sel
= &xp
->selector
;
3078 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
3079 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
3081 switch (sel
->family
) {
3083 audit_log_format(audit_buf
, " src=%pI4", &sel
->saddr
.a4
);
3084 if (sel
->prefixlen_s
!= 32)
3085 audit_log_format(audit_buf
, " src_prefixlen=%d",
3087 audit_log_format(audit_buf
, " dst=%pI4", &sel
->daddr
.a4
);
3088 if (sel
->prefixlen_d
!= 32)
3089 audit_log_format(audit_buf
, " dst_prefixlen=%d",
3093 audit_log_format(audit_buf
, " src=%pI6", sel
->saddr
.a6
);
3094 if (sel
->prefixlen_s
!= 128)
3095 audit_log_format(audit_buf
, " src_prefixlen=%d",
3097 audit_log_format(audit_buf
, " dst=%pI6", sel
->daddr
.a6
);
3098 if (sel
->prefixlen_d
!= 128)
3099 audit_log_format(audit_buf
, " dst_prefixlen=%d",
3105 void xfrm_audit_policy_add(struct xfrm_policy
*xp
, int result
, bool task_valid
)
3107 struct audit_buffer
*audit_buf
;
3109 audit_buf
= xfrm_audit_start("SPD-add");
3110 if (audit_buf
== NULL
)
3112 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
3113 audit_log_format(audit_buf
, " res=%u", result
);
3114 xfrm_audit_common_policyinfo(xp
, audit_buf
);
3115 audit_log_end(audit_buf
);
3117 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add
);
3119 void xfrm_audit_policy_delete(struct xfrm_policy
*xp
, int result
,
3122 struct audit_buffer
*audit_buf
;
3124 audit_buf
= xfrm_audit_start("SPD-delete");
3125 if (audit_buf
== NULL
)
3127 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
3128 audit_log_format(audit_buf
, " res=%u", result
);
3129 xfrm_audit_common_policyinfo(xp
, audit_buf
);
3130 audit_log_end(audit_buf
);
3132 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete
);
3135 #ifdef CONFIG_XFRM_MIGRATE
3136 static bool xfrm_migrate_selector_match(const struct xfrm_selector
*sel_cmp
,
3137 const struct xfrm_selector
*sel_tgt
)
3139 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
3140 if (sel_tgt
->family
== sel_cmp
->family
&&
3141 xfrm_addr_equal(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
3143 xfrm_addr_equal(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
3145 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
3146 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
3150 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
3157 static struct xfrm_policy
*xfrm_migrate_policy_find(const struct xfrm_selector
*sel
,
3158 u8 dir
, u8 type
, struct net
*net
)
3160 struct xfrm_policy
*pol
, *ret
= NULL
;
3161 struct hlist_head
*chain
;
3164 spin_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
3165 chain
= policy_hash_direct(net
, &sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
3166 hlist_for_each_entry(pol
, chain
, bydst
) {
3167 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3168 pol
->type
== type
) {
3170 priority
= ret
->priority
;
3174 chain
= &net
->xfrm
.policy_inexact
[dir
];
3175 hlist_for_each_entry(pol
, chain
, bydst
) {
3176 if ((pol
->priority
>= priority
) && ret
)
3179 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3180 pol
->type
== type
) {
3188 spin_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
3193 static int migrate_tmpl_match(const struct xfrm_migrate
*m
, const struct xfrm_tmpl
*t
)
3197 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
3198 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
3200 case XFRM_MODE_TUNNEL
:
3201 case XFRM_MODE_BEET
:
3202 if (xfrm_addr_equal(&t
->id
.daddr
, &m
->old_daddr
,
3204 xfrm_addr_equal(&t
->saddr
, &m
->old_saddr
,
3209 case XFRM_MODE_TRANSPORT
:
3210 /* in case of transport mode, template does not store
3211 any IP addresses, hence we just compare mode and
3222 /* update endpoint address(es) of template(s) */
3223 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
3224 struct xfrm_migrate
*m
, int num_migrate
)
3226 struct xfrm_migrate
*mp
;
3229 write_lock_bh(&pol
->lock
);
3230 if (unlikely(pol
->walk
.dead
)) {
3231 /* target policy has been deleted */
3232 write_unlock_bh(&pol
->lock
);
3236 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
3237 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
3238 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
3241 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
&&
3242 pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_BEET
)
3244 /* update endpoints */
3245 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
3246 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
3247 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
3248 sizeof(pol
->xfrm_vec
[i
].saddr
));
3249 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
3251 atomic_inc(&pol
->genid
);
3255 write_unlock_bh(&pol
->lock
);
3263 static int xfrm_migrate_check(const struct xfrm_migrate
*m
, int num_migrate
)
3267 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
3270 for (i
= 0; i
< num_migrate
; i
++) {
3271 if (xfrm_addr_equal(&m
[i
].old_daddr
, &m
[i
].new_daddr
,
3273 xfrm_addr_equal(&m
[i
].old_saddr
, &m
[i
].new_saddr
,
3276 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
3277 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
3280 /* check if there is any duplicated entry */
3281 for (j
= i
+ 1; j
< num_migrate
; j
++) {
3282 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
3283 sizeof(m
[i
].old_daddr
)) &&
3284 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
3285 sizeof(m
[i
].old_saddr
)) &&
3286 m
[i
].proto
== m
[j
].proto
&&
3287 m
[i
].mode
== m
[j
].mode
&&
3288 m
[i
].reqid
== m
[j
].reqid
&&
3289 m
[i
].old_family
== m
[j
].old_family
)
3297 int xfrm_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3298 struct xfrm_migrate
*m
, int num_migrate
,
3299 struct xfrm_kmaddress
*k
, struct net
*net
)
3301 int i
, err
, nx_cur
= 0, nx_new
= 0;
3302 struct xfrm_policy
*pol
= NULL
;
3303 struct xfrm_state
*x
, *xc
;
3304 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
3305 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
3306 struct xfrm_migrate
*mp
;
3308 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
3311 /* Stage 1 - find policy */
3312 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
, net
)) == NULL
) {
3317 /* Stage 2 - find and update state(s) */
3318 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
3319 if ((x
= xfrm_migrate_state_find(mp
, net
))) {
3322 if ((xc
= xfrm_state_migrate(x
, mp
))) {
3332 /* Stage 3 - update policy */
3333 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
3336 /* Stage 4 - delete old state(s) */
3338 xfrm_states_put(x_cur
, nx_cur
);
3339 xfrm_states_delete(x_cur
, nx_cur
);
3342 /* Stage 5 - announce */
3343 km_migrate(sel
, dir
, type
, m
, num_migrate
, k
);
3355 xfrm_states_put(x_cur
, nx_cur
);
3357 xfrm_states_delete(x_new
, nx_new
);
3361 EXPORT_SYMBOL(xfrm_migrate
);