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 __rcu
*xfrm_policy_afinfo
[NPROTO
]
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
,
64 __xfrm4_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
66 const struct flowi4
*fl4
= &fl
->u
.ip4
;
68 return addr4_match(fl4
->daddr
, sel
->daddr
.a4
, sel
->prefixlen_d
) &&
69 addr4_match(fl4
->saddr
, sel
->saddr
.a4
, sel
->prefixlen_s
) &&
70 !((xfrm_flowi_dport(fl
, &fl4
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
71 !((xfrm_flowi_sport(fl
, &fl4
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
72 (fl4
->flowi4_proto
== sel
->proto
|| !sel
->proto
) &&
73 (fl4
->flowi4_oif
== sel
->ifindex
|| !sel
->ifindex
);
77 __xfrm6_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
79 const struct flowi6
*fl6
= &fl
->u
.ip6
;
81 return addr_match(&fl6
->daddr
, &sel
->daddr
, sel
->prefixlen_d
) &&
82 addr_match(&fl6
->saddr
, &sel
->saddr
, sel
->prefixlen_s
) &&
83 !((xfrm_flowi_dport(fl
, &fl6
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
84 !((xfrm_flowi_sport(fl
, &fl6
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
85 (fl6
->flowi6_proto
== sel
->proto
|| !sel
->proto
) &&
86 (fl6
->flowi6_oif
== sel
->ifindex
|| !sel
->ifindex
);
89 bool xfrm_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
,
90 unsigned short family
)
94 return __xfrm4_selector_match(sel
, fl
);
96 return __xfrm6_selector_match(sel
, fl
);
101 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
103 struct xfrm_policy_afinfo
*afinfo
;
105 if (unlikely(family
>= NPROTO
))
108 afinfo
= rcu_dereference(xfrm_policy_afinfo
[family
]);
109 if (unlikely(!afinfo
))
114 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
)
119 static inline struct dst_entry
*__xfrm_dst_lookup(struct net
*net
,
121 const xfrm_address_t
*saddr
,
122 const xfrm_address_t
*daddr
,
125 struct xfrm_policy_afinfo
*afinfo
;
126 struct dst_entry
*dst
;
128 afinfo
= xfrm_policy_get_afinfo(family
);
129 if (unlikely(afinfo
== NULL
))
130 return ERR_PTR(-EAFNOSUPPORT
);
132 dst
= afinfo
->dst_lookup(net
, tos
, oif
, saddr
, daddr
);
134 xfrm_policy_put_afinfo(afinfo
);
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 descentant 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 write_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 write_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 write_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 write_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 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
559 si
->incnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
];
560 si
->outcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
];
561 si
->fwdcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
];
562 si
->inscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
563 si
->outscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
564 si
->fwdscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
565 si
->spdhcnt
= net
->xfrm
.policy_idx_hmask
;
566 si
->spdhmcnt
= xfrm_policy_hashmax
;
567 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
569 EXPORT_SYMBOL(xfrm_spd_getinfo
);
571 static DEFINE_MUTEX(hash_resize_mutex
);
572 static void xfrm_hash_resize(struct work_struct
*work
)
574 struct net
*net
= container_of(work
, struct net
, xfrm
.policy_hash_work
);
577 mutex_lock(&hash_resize_mutex
);
580 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
581 if (xfrm_bydst_should_resize(net
, dir
, &total
))
582 xfrm_bydst_resize(net
, dir
);
584 if (xfrm_byidx_should_resize(net
, total
))
585 xfrm_byidx_resize(net
, total
);
587 mutex_unlock(&hash_resize_mutex
);
590 static void xfrm_hash_rebuild(struct work_struct
*work
)
592 struct net
*net
= container_of(work
, struct net
,
593 xfrm
.policy_hthresh
.work
);
595 struct xfrm_policy
*pol
;
596 struct xfrm_policy
*policy
;
597 struct hlist_head
*chain
;
598 struct hlist_head
*odst
;
599 struct hlist_node
*newpos
;
603 u8 lbits4
, rbits4
, lbits6
, rbits6
;
605 mutex_lock(&hash_resize_mutex
);
607 /* read selector prefixlen thresholds */
609 seq
= read_seqbegin(&net
->xfrm
.policy_hthresh
.lock
);
611 lbits4
= net
->xfrm
.policy_hthresh
.lbits4
;
612 rbits4
= net
->xfrm
.policy_hthresh
.rbits4
;
613 lbits6
= net
->xfrm
.policy_hthresh
.lbits6
;
614 rbits6
= net
->xfrm
.policy_hthresh
.rbits6
;
615 } while (read_seqretry(&net
->xfrm
.policy_hthresh
.lock
, seq
));
617 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
619 /* reset the bydst and inexact table in all directions */
620 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
621 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
622 hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
623 odst
= net
->xfrm
.policy_bydst
[dir
].table
;
624 for (i
= hmask
; i
>= 0; i
--)
625 INIT_HLIST_HEAD(odst
+ i
);
626 if ((dir
& XFRM_POLICY_MASK
) == XFRM_POLICY_OUT
) {
627 /* dir out => dst = remote, src = local */
628 net
->xfrm
.policy_bydst
[dir
].dbits4
= rbits4
;
629 net
->xfrm
.policy_bydst
[dir
].sbits4
= lbits4
;
630 net
->xfrm
.policy_bydst
[dir
].dbits6
= rbits6
;
631 net
->xfrm
.policy_bydst
[dir
].sbits6
= lbits6
;
633 /* dir in/fwd => dst = local, src = remote */
634 net
->xfrm
.policy_bydst
[dir
].dbits4
= lbits4
;
635 net
->xfrm
.policy_bydst
[dir
].sbits4
= rbits4
;
636 net
->xfrm
.policy_bydst
[dir
].dbits6
= lbits6
;
637 net
->xfrm
.policy_bydst
[dir
].sbits6
= rbits6
;
641 /* re-insert all policies by order of creation */
642 list_for_each_entry_reverse(policy
, &net
->xfrm
.policy_all
, walk
.all
) {
644 chain
= policy_hash_bysel(net
, &policy
->selector
,
646 xfrm_policy_id2dir(policy
->index
));
647 hlist_for_each_entry(pol
, chain
, bydst
) {
648 if (policy
->priority
>= pol
->priority
)
649 newpos
= &pol
->bydst
;
654 hlist_add_behind(&policy
->bydst
, newpos
);
656 hlist_add_head(&policy
->bydst
, chain
);
659 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
661 mutex_unlock(&hash_resize_mutex
);
664 void xfrm_policy_hash_rebuild(struct net
*net
)
666 schedule_work(&net
->xfrm
.policy_hthresh
.work
);
668 EXPORT_SYMBOL(xfrm_policy_hash_rebuild
);
670 /* Generate new index... KAME seems to generate them ordered by cost
671 * of an absolute inpredictability of ordering of rules. This will not pass. */
672 static u32
xfrm_gen_index(struct net
*net
, int dir
, u32 index
)
674 static u32 idx_generator
;
677 struct hlist_head
*list
;
678 struct xfrm_policy
*p
;
683 idx
= (idx_generator
| dir
);
692 list
= net
->xfrm
.policy_byidx
+ idx_hash(net
, idx
);
694 hlist_for_each_entry(p
, list
, byidx
) {
695 if (p
->index
== idx
) {
705 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
707 u32
*p1
= (u32
*) s1
;
708 u32
*p2
= (u32
*) s2
;
709 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
712 for (i
= 0; i
< len
; i
++) {
720 static void xfrm_policy_requeue(struct xfrm_policy
*old
,
721 struct xfrm_policy
*new)
723 struct xfrm_policy_queue
*pq
= &old
->polq
;
724 struct sk_buff_head list
;
726 if (skb_queue_empty(&pq
->hold_queue
))
729 __skb_queue_head_init(&list
);
731 spin_lock_bh(&pq
->hold_queue
.lock
);
732 skb_queue_splice_init(&pq
->hold_queue
, &list
);
733 if (del_timer(&pq
->hold_timer
))
735 spin_unlock_bh(&pq
->hold_queue
.lock
);
739 spin_lock_bh(&pq
->hold_queue
.lock
);
740 skb_queue_splice(&list
, &pq
->hold_queue
);
741 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
742 if (!mod_timer(&pq
->hold_timer
, jiffies
))
744 spin_unlock_bh(&pq
->hold_queue
.lock
);
747 static bool xfrm_policy_mark_match(struct xfrm_policy
*policy
,
748 struct xfrm_policy
*pol
)
750 u32 mark
= policy
->mark
.v
& policy
->mark
.m
;
752 if (policy
->mark
.v
== pol
->mark
.v
&& policy
->mark
.m
== pol
->mark
.m
)
755 if ((mark
& pol
->mark
.m
) == pol
->mark
.v
&&
756 policy
->priority
== pol
->priority
)
762 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
764 struct net
*net
= xp_net(policy
);
765 struct xfrm_policy
*pol
;
766 struct xfrm_policy
*delpol
;
767 struct hlist_head
*chain
;
768 struct hlist_node
*newpos
;
770 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
771 chain
= policy_hash_bysel(net
, &policy
->selector
, policy
->family
, dir
);
774 hlist_for_each_entry(pol
, chain
, bydst
) {
775 if (pol
->type
== policy
->type
&&
776 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
777 xfrm_policy_mark_match(policy
, pol
) &&
778 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
781 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
785 if (policy
->priority
> pol
->priority
)
787 } else if (policy
->priority
>= pol
->priority
) {
788 newpos
= &pol
->bydst
;
795 hlist_add_behind(&policy
->bydst
, newpos
);
797 hlist_add_head(&policy
->bydst
, chain
);
798 __xfrm_policy_link(policy
, dir
);
799 atomic_inc(&net
->xfrm
.flow_cache_genid
);
801 /* After previous checking, family can either be AF_INET or AF_INET6 */
802 if (policy
->family
== AF_INET
)
803 rt_genid_bump_ipv4(net
);
805 rt_genid_bump_ipv6(net
);
808 xfrm_policy_requeue(delpol
, policy
);
809 __xfrm_policy_unlink(delpol
, dir
);
811 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(net
, dir
, policy
->index
);
812 hlist_add_head(&policy
->byidx
, net
->xfrm
.policy_byidx
+idx_hash(net
, policy
->index
));
813 policy
->curlft
.add_time
= get_seconds();
814 policy
->curlft
.use_time
= 0;
815 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
816 xfrm_pol_hold(policy
);
817 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
820 xfrm_policy_kill(delpol
);
821 else if (xfrm_bydst_should_resize(net
, dir
, NULL
))
822 schedule_work(&net
->xfrm
.policy_hash_work
);
826 EXPORT_SYMBOL(xfrm_policy_insert
);
828 struct xfrm_policy
*xfrm_policy_bysel_ctx(struct net
*net
, u32 mark
, u8 type
,
829 int dir
, struct xfrm_selector
*sel
,
830 struct xfrm_sec_ctx
*ctx
, int delete,
833 struct xfrm_policy
*pol
, *ret
;
834 struct hlist_head
*chain
;
837 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
838 chain
= policy_hash_bysel(net
, sel
, sel
->family
, dir
);
840 hlist_for_each_entry(pol
, chain
, bydst
) {
841 if (pol
->type
== type
&&
842 (mark
& pol
->mark
.m
) == pol
->mark
.v
&&
843 !selector_cmp(sel
, &pol
->selector
) &&
844 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
847 *err
= security_xfrm_policy_delete(
850 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
853 __xfrm_policy_unlink(pol
, dir
);
859 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
862 xfrm_policy_kill(ret
);
865 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
867 struct xfrm_policy
*xfrm_policy_byid(struct net
*net
, u32 mark
, u8 type
,
868 int dir
, u32 id
, int delete, int *err
)
870 struct xfrm_policy
*pol
, *ret
;
871 struct hlist_head
*chain
;
874 if (xfrm_policy_id2dir(id
) != dir
)
878 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
879 chain
= net
->xfrm
.policy_byidx
+ idx_hash(net
, id
);
881 hlist_for_each_entry(pol
, chain
, byidx
) {
882 if (pol
->type
== type
&& pol
->index
== id
&&
883 (mark
& pol
->mark
.m
) == pol
->mark
.v
) {
886 *err
= security_xfrm_policy_delete(
889 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
892 __xfrm_policy_unlink(pol
, dir
);
898 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
901 xfrm_policy_kill(ret
);
904 EXPORT_SYMBOL(xfrm_policy_byid
);
906 #ifdef CONFIG_SECURITY_NETWORK_XFRM
908 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
912 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
913 struct xfrm_policy
*pol
;
916 hlist_for_each_entry(pol
,
917 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
918 if (pol
->type
!= type
)
920 err
= security_xfrm_policy_delete(pol
->security
);
922 xfrm_audit_policy_delete(pol
, 0, task_valid
);
926 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
927 hlist_for_each_entry(pol
,
928 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
930 if (pol
->type
!= type
)
932 err
= security_xfrm_policy_delete(
935 xfrm_audit_policy_delete(pol
, 0,
946 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
952 int xfrm_policy_flush(struct net
*net
, u8 type
, bool task_valid
)
954 int dir
, err
= 0, cnt
= 0;
956 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
958 err
= xfrm_policy_flush_secctx_check(net
, type
, task_valid
);
962 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
963 struct xfrm_policy
*pol
;
967 hlist_for_each_entry(pol
,
968 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
969 if (pol
->type
!= type
)
971 __xfrm_policy_unlink(pol
, dir
);
972 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
975 xfrm_audit_policy_delete(pol
, 1, task_valid
);
977 xfrm_policy_kill(pol
);
979 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
983 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
985 hlist_for_each_entry(pol
,
986 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
988 if (pol
->type
!= type
)
990 __xfrm_policy_unlink(pol
, dir
);
991 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
994 xfrm_audit_policy_delete(pol
, 1, task_valid
);
995 xfrm_policy_kill(pol
);
997 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1006 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1009 EXPORT_SYMBOL(xfrm_policy_flush
);
1011 int xfrm_policy_walk(struct net
*net
, struct xfrm_policy_walk
*walk
,
1012 int (*func
)(struct xfrm_policy
*, int, int, void*),
1015 struct xfrm_policy
*pol
;
1016 struct xfrm_policy_walk_entry
*x
;
1019 if (walk
->type
>= XFRM_POLICY_TYPE_MAX
&&
1020 walk
->type
!= XFRM_POLICY_TYPE_ANY
)
1023 if (list_empty(&walk
->walk
.all
) && walk
->seq
!= 0)
1026 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1027 if (list_empty(&walk
->walk
.all
))
1028 x
= list_first_entry(&net
->xfrm
.policy_all
, struct xfrm_policy_walk_entry
, all
);
1030 x
= list_first_entry(&walk
->walk
.all
,
1031 struct xfrm_policy_walk_entry
, all
);
1033 list_for_each_entry_from(x
, &net
->xfrm
.policy_all
, all
) {
1036 pol
= container_of(x
, struct xfrm_policy
, walk
);
1037 if (walk
->type
!= XFRM_POLICY_TYPE_ANY
&&
1038 walk
->type
!= pol
->type
)
1040 error
= func(pol
, xfrm_policy_id2dir(pol
->index
),
1043 list_move_tail(&walk
->walk
.all
, &x
->all
);
1048 if (walk
->seq
== 0) {
1052 list_del_init(&walk
->walk
.all
);
1054 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1057 EXPORT_SYMBOL(xfrm_policy_walk
);
1059 void xfrm_policy_walk_init(struct xfrm_policy_walk
*walk
, u8 type
)
1061 INIT_LIST_HEAD(&walk
->walk
.all
);
1062 walk
->walk
.dead
= 1;
1066 EXPORT_SYMBOL(xfrm_policy_walk_init
);
1068 void xfrm_policy_walk_done(struct xfrm_policy_walk
*walk
, struct net
*net
)
1070 if (list_empty(&walk
->walk
.all
))
1073 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
); /*FIXME where is net? */
1074 list_del(&walk
->walk
.all
);
1075 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1077 EXPORT_SYMBOL(xfrm_policy_walk_done
);
1080 * Find policy to apply to this flow.
1082 * Returns 0 if policy found, else an -errno.
1084 static int xfrm_policy_match(const struct xfrm_policy
*pol
,
1085 const struct flowi
*fl
,
1086 u8 type
, u16 family
, int dir
)
1088 const struct xfrm_selector
*sel
= &pol
->selector
;
1092 if (pol
->family
!= family
||
1093 (fl
->flowi_mark
& pol
->mark
.m
) != pol
->mark
.v
||
1097 match
= xfrm_selector_match(sel
, fl
, family
);
1099 ret
= security_xfrm_policy_lookup(pol
->security
, fl
->flowi_secid
,
1105 static struct xfrm_policy
*xfrm_policy_lookup_bytype(struct net
*net
, u8 type
,
1106 const struct flowi
*fl
,
1110 struct xfrm_policy
*pol
, *ret
;
1111 const xfrm_address_t
*daddr
, *saddr
;
1112 struct hlist_head
*chain
;
1113 unsigned int sequence
;
1116 daddr
= xfrm_flowi_daddr(fl
, family
);
1117 saddr
= xfrm_flowi_saddr(fl
, family
);
1118 if (unlikely(!daddr
|| !saddr
))
1121 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1124 sequence
= read_seqcount_begin(&xfrm_policy_hash_generation
);
1125 chain
= policy_hash_direct(net
, daddr
, saddr
, family
, dir
);
1126 } while (read_seqcount_retry(&xfrm_policy_hash_generation
, sequence
));
1130 hlist_for_each_entry_rcu(pol
, chain
, bydst
) {
1131 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1141 priority
= ret
->priority
;
1145 chain
= &net
->xfrm
.policy_inexact
[dir
];
1146 hlist_for_each_entry_rcu(pol
, chain
, bydst
) {
1147 if ((pol
->priority
>= priority
) && ret
)
1150 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1164 if (read_seqcount_retry(&xfrm_policy_hash_generation
, sequence
))
1169 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1174 static struct xfrm_policy
*
1175 __xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
)
1177 #ifdef CONFIG_XFRM_SUB_POLICY
1178 struct xfrm_policy
*pol
;
1180 pol
= xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
1184 return xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
1187 static int flow_to_policy_dir(int dir
)
1189 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1190 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1191 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1197 return XFRM_POLICY_IN
;
1199 return XFRM_POLICY_OUT
;
1201 return XFRM_POLICY_FWD
;
1205 static struct flow_cache_object
*
1206 xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
,
1207 u8 dir
, struct flow_cache_object
*old_obj
, void *ctx
)
1209 struct xfrm_policy
*pol
;
1212 xfrm_pol_put(container_of(old_obj
, struct xfrm_policy
, flo
));
1214 pol
= __xfrm_policy_lookup(net
, fl
, family
, flow_to_policy_dir(dir
));
1215 if (IS_ERR_OR_NULL(pol
))
1216 return ERR_CAST(pol
);
1218 /* Resolver returns two references:
1219 * one for cache and one for caller of flow_cache_lookup() */
1225 static inline int policy_to_flow_dir(int dir
)
1227 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1228 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1229 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1233 case XFRM_POLICY_IN
:
1235 case XFRM_POLICY_OUT
:
1236 return FLOW_DIR_OUT
;
1237 case XFRM_POLICY_FWD
:
1238 return FLOW_DIR_FWD
;
1242 static struct xfrm_policy
*xfrm_sk_policy_lookup(const struct sock
*sk
, int dir
,
1243 const struct flowi
*fl
)
1245 struct xfrm_policy
*pol
;
1246 struct net
*net
= sock_net(sk
);
1249 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1250 pol
= rcu_dereference(sk
->sk_policy
[dir
]);
1252 bool match
= xfrm_selector_match(&pol
->selector
, fl
,
1257 if ((sk
->sk_mark
& pol
->mark
.m
) != pol
->mark
.v
) {
1261 err
= security_xfrm_policy_lookup(pol
->security
,
1263 policy_to_flow_dir(dir
));
1266 else if (err
== -ESRCH
)
1274 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1279 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1281 struct net
*net
= xp_net(pol
);
1283 list_add(&pol
->walk
.all
, &net
->xfrm
.policy_all
);
1284 net
->xfrm
.policy_count
[dir
]++;
1288 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1291 struct net
*net
= xp_net(pol
);
1293 if (list_empty(&pol
->walk
.all
))
1296 /* Socket policies are not hashed. */
1297 if (!hlist_unhashed(&pol
->bydst
)) {
1298 hlist_del_rcu(&pol
->bydst
);
1299 hlist_del(&pol
->byidx
);
1302 list_del_init(&pol
->walk
.all
);
1303 net
->xfrm
.policy_count
[dir
]--;
1308 static void xfrm_sk_policy_link(struct xfrm_policy
*pol
, int dir
)
1310 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+ dir
);
1313 static void xfrm_sk_policy_unlink(struct xfrm_policy
*pol
, int dir
)
1315 __xfrm_policy_unlink(pol
, XFRM_POLICY_MAX
+ dir
);
1318 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1320 struct net
*net
= xp_net(pol
);
1322 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1323 pol
= __xfrm_policy_unlink(pol
, dir
);
1324 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1326 xfrm_policy_kill(pol
);
1331 EXPORT_SYMBOL(xfrm_policy_delete
);
1333 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1335 struct net
*net
= xp_net(pol
);
1336 struct xfrm_policy
*old_pol
;
1338 #ifdef CONFIG_XFRM_SUB_POLICY
1339 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1343 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1344 old_pol
= rcu_dereference_protected(sk
->sk_policy
[dir
],
1345 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
1347 pol
->curlft
.add_time
= get_seconds();
1348 pol
->index
= xfrm_gen_index(net
, XFRM_POLICY_MAX
+dir
, 0);
1349 xfrm_sk_policy_link(pol
, dir
);
1351 rcu_assign_pointer(sk
->sk_policy
[dir
], pol
);
1354 xfrm_policy_requeue(old_pol
, pol
);
1356 /* Unlinking succeeds always. This is the only function
1357 * allowed to delete or replace socket policy.
1359 xfrm_sk_policy_unlink(old_pol
, dir
);
1361 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1364 xfrm_policy_kill(old_pol
);
1369 static struct xfrm_policy
*clone_policy(const struct xfrm_policy
*old
, int dir
)
1371 struct xfrm_policy
*newp
= xfrm_policy_alloc(xp_net(old
), GFP_ATOMIC
);
1372 struct net
*net
= xp_net(old
);
1375 newp
->selector
= old
->selector
;
1376 if (security_xfrm_policy_clone(old
->security
,
1379 return NULL
; /* ENOMEM */
1381 newp
->lft
= old
->lft
;
1382 newp
->curlft
= old
->curlft
;
1383 newp
->mark
= old
->mark
;
1384 newp
->action
= old
->action
;
1385 newp
->flags
= old
->flags
;
1386 newp
->xfrm_nr
= old
->xfrm_nr
;
1387 newp
->index
= old
->index
;
1388 newp
->type
= old
->type
;
1389 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1390 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1391 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1392 xfrm_sk_policy_link(newp
, dir
);
1393 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1399 int __xfrm_sk_clone_policy(struct sock
*sk
, const struct sock
*osk
)
1401 const struct xfrm_policy
*p
;
1402 struct xfrm_policy
*np
;
1406 for (i
= 0; i
< 2; i
++) {
1407 p
= rcu_dereference(osk
->sk_policy
[i
]);
1409 np
= clone_policy(p
, i
);
1410 if (unlikely(!np
)) {
1414 rcu_assign_pointer(sk
->sk_policy
[i
], np
);
1422 xfrm_get_saddr(struct net
*net
, int oif
, xfrm_address_t
*local
,
1423 xfrm_address_t
*remote
, unsigned short family
)
1426 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1428 if (unlikely(afinfo
== NULL
))
1430 err
= afinfo
->get_saddr(net
, oif
, local
, remote
);
1431 xfrm_policy_put_afinfo(afinfo
);
1435 /* Resolve list of templates for the flow, given policy. */
1438 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, const struct flowi
*fl
,
1439 struct xfrm_state
**xfrm
, unsigned short family
)
1441 struct net
*net
= xp_net(policy
);
1444 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1445 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1448 for (nx
= 0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1449 struct xfrm_state
*x
;
1450 xfrm_address_t
*remote
= daddr
;
1451 xfrm_address_t
*local
= saddr
;
1452 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1454 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1455 tmpl
->mode
== XFRM_MODE_BEET
) {
1456 remote
= &tmpl
->id
.daddr
;
1457 local
= &tmpl
->saddr
;
1458 if (xfrm_addr_any(local
, tmpl
->encap_family
)) {
1459 error
= xfrm_get_saddr(net
, fl
->flowi_oif
,
1461 tmpl
->encap_family
);
1468 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1470 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1477 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1480 } else if (error
== -ESRCH
) {
1484 if (!tmpl
->optional
)
1490 for (nx
--; nx
>= 0; nx
--)
1491 xfrm_state_put(xfrm
[nx
]);
1496 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, const struct flowi
*fl
,
1497 struct xfrm_state
**xfrm
, unsigned short family
)
1499 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1500 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1506 for (i
= 0; i
< npols
; i
++) {
1507 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1512 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1520 /* found states are sorted for outbound processing */
1522 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1527 for (cnx
--; cnx
>= 0; cnx
--)
1528 xfrm_state_put(tpp
[cnx
]);
1533 /* Check that the bundle accepts the flow and its components are
1537 static inline int xfrm_get_tos(const struct flowi
*fl
, int family
)
1539 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1545 tos
= afinfo
->get_tos(fl
);
1547 xfrm_policy_put_afinfo(afinfo
);
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 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
);
1633 xfrm_policy_put_afinfo(afinfo
);
1638 static inline int xfrm_init_path(struct xfrm_dst
*path
, struct dst_entry
*dst
,
1641 struct xfrm_policy_afinfo
*afinfo
=
1642 xfrm_policy_get_afinfo(dst
->ops
->family
);
1648 err
= afinfo
->init_path(path
, dst
, nfheader_len
);
1650 xfrm_policy_put_afinfo(afinfo
);
1655 static inline int xfrm_fill_dst(struct xfrm_dst
*xdst
, struct net_device
*dev
,
1656 const struct flowi
*fl
)
1658 struct xfrm_policy_afinfo
*afinfo
=
1659 xfrm_policy_get_afinfo(xdst
->u
.dst
.ops
->family
);
1665 err
= afinfo
->fill_dst(xdst
, dev
, fl
);
1667 xfrm_policy_put_afinfo(afinfo
);
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
);
1706 for (; i
< nx
; i
++) {
1707 struct xfrm_dst
*xdst
= xfrm_alloc_dst(net
, family
);
1708 struct dst_entry
*dst1
= &xdst
->u
.dst
;
1710 err
= PTR_ERR(xdst
);
1716 if (xfrm
[i
]->sel
.family
== AF_UNSPEC
) {
1717 inner_mode
= xfrm_ip2inner_mode(xfrm
[i
],
1718 xfrm_af2proto(family
));
1720 err
= -EAFNOSUPPORT
;
1725 inner_mode
= xfrm
[i
]->inner_mode
;
1730 dst_prev
->child
= dst_clone(dst1
);
1731 dst1
->flags
|= DST_NOHASH
;
1735 dst_copy_metrics(dst1
, dst
);
1737 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1738 family
= xfrm
[i
]->props
.family
;
1739 dst
= xfrm_dst_lookup(xfrm
[i
], tos
, fl
->flowi_oif
,
1740 &saddr
, &daddr
, family
);
1747 dst1
->xfrm
= xfrm
[i
];
1748 xdst
->xfrm_genid
= xfrm
[i
]->genid
;
1750 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
1751 dst1
->flags
|= DST_HOST
;
1752 dst1
->lastuse
= now
;
1754 dst1
->input
= dst_discard
;
1755 dst1
->output
= inner_mode
->afinfo
->output
;
1757 dst1
->next
= dst_prev
;
1760 header_len
+= xfrm
[i
]->props
.header_len
;
1761 if (xfrm
[i
]->type
->flags
& XFRM_TYPE_NON_FRAGMENT
)
1762 nfheader_len
+= xfrm
[i
]->props
.header_len
;
1763 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
1766 dst_prev
->child
= dst
;
1774 xfrm_init_path((struct xfrm_dst
*)dst0
, dst
, nfheader_len
);
1775 xfrm_init_pmtu(dst_prev
);
1777 for (dst_prev
= dst0
; dst_prev
!= dst
; dst_prev
= dst_prev
->child
) {
1778 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst_prev
;
1780 err
= xfrm_fill_dst(xdst
, dev
, fl
);
1784 dst_prev
->header_len
= header_len
;
1785 dst_prev
->trailer_len
= trailer_len
;
1786 header_len
-= xdst
->u
.dst
.xfrm
->props
.header_len
;
1787 trailer_len
-= xdst
->u
.dst
.xfrm
->props
.trailer_len
;
1795 xfrm_state_put(xfrm
[i
]);
1799 dst0
= ERR_PTR(err
);
1803 #ifdef CONFIG_XFRM_SUB_POLICY
1804 static int xfrm_dst_alloc_copy(void **target
, const void *src
, int size
)
1807 *target
= kmalloc(size
, GFP_ATOMIC
);
1812 memcpy(*target
, src
, size
);
1817 static int xfrm_dst_update_parent(struct dst_entry
*dst
,
1818 const struct xfrm_selector
*sel
)
1820 #ifdef CONFIG_XFRM_SUB_POLICY
1821 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1822 return xfrm_dst_alloc_copy((void **)&(xdst
->partner
),
1829 static int xfrm_dst_update_origin(struct dst_entry
*dst
,
1830 const struct flowi
*fl
)
1832 #ifdef CONFIG_XFRM_SUB_POLICY
1833 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1834 return xfrm_dst_alloc_copy((void **)&(xdst
->origin
), fl
, sizeof(*fl
));
1840 static int xfrm_expand_policies(const struct flowi
*fl
, u16 family
,
1841 struct xfrm_policy
**pols
,
1842 int *num_pols
, int *num_xfrms
)
1846 if (*num_pols
== 0 || !pols
[0]) {
1851 if (IS_ERR(pols
[0]))
1852 return PTR_ERR(pols
[0]);
1854 *num_xfrms
= pols
[0]->xfrm_nr
;
1856 #ifdef CONFIG_XFRM_SUB_POLICY
1857 if (pols
[0] && pols
[0]->action
== XFRM_POLICY_ALLOW
&&
1858 pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1859 pols
[1] = xfrm_policy_lookup_bytype(xp_net(pols
[0]),
1860 XFRM_POLICY_TYPE_MAIN
,
1864 if (IS_ERR(pols
[1])) {
1865 xfrm_pols_put(pols
, *num_pols
);
1866 return PTR_ERR(pols
[1]);
1869 (*num_xfrms
) += pols
[1]->xfrm_nr
;
1873 for (i
= 0; i
< *num_pols
; i
++) {
1874 if (pols
[i
]->action
!= XFRM_POLICY_ALLOW
) {
1884 static struct xfrm_dst
*
1885 xfrm_resolve_and_create_bundle(struct xfrm_policy
**pols
, int num_pols
,
1886 const struct flowi
*fl
, u16 family
,
1887 struct dst_entry
*dst_orig
)
1889 struct net
*net
= xp_net(pols
[0]);
1890 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1891 struct dst_entry
*dst
;
1892 struct xfrm_dst
*xdst
;
1895 /* Try to instantiate a bundle */
1896 err
= xfrm_tmpl_resolve(pols
, num_pols
, fl
, xfrm
, family
);
1898 if (err
!= 0 && err
!= -EAGAIN
)
1899 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1900 return ERR_PTR(err
);
1903 dst
= xfrm_bundle_create(pols
[0], xfrm
, err
, fl
, dst_orig
);
1905 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLEGENERROR
);
1906 return ERR_CAST(dst
);
1909 xdst
= (struct xfrm_dst
*)dst
;
1910 xdst
->num_xfrms
= err
;
1912 err
= xfrm_dst_update_parent(dst
, &pols
[1]->selector
);
1914 err
= xfrm_dst_update_origin(dst
, fl
);
1915 if (unlikely(err
)) {
1917 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLECHECKERROR
);
1918 return ERR_PTR(err
);
1921 xdst
->num_pols
= num_pols
;
1922 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1923 xdst
->policy_genid
= atomic_read(&pols
[0]->genid
);
1928 static void xfrm_policy_queue_process(unsigned long arg
)
1930 struct sk_buff
*skb
;
1932 struct dst_entry
*dst
;
1933 struct xfrm_policy
*pol
= (struct xfrm_policy
*)arg
;
1934 struct net
*net
= xp_net(pol
);
1935 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1937 struct sk_buff_head list
;
1939 spin_lock(&pq
->hold_queue
.lock
);
1940 skb
= skb_peek(&pq
->hold_queue
);
1942 spin_unlock(&pq
->hold_queue
.lock
);
1947 xfrm_decode_session(skb
, &fl
, dst
->ops
->family
);
1948 spin_unlock(&pq
->hold_queue
.lock
);
1950 dst_hold(dst
->path
);
1951 dst
= xfrm_lookup(net
, dst
->path
, &fl
, sk
, 0);
1955 if (dst
->flags
& DST_XFRM_QUEUE
) {
1958 if (pq
->timeout
>= XFRM_QUEUE_TMO_MAX
)
1961 pq
->timeout
= pq
->timeout
<< 1;
1962 if (!mod_timer(&pq
->hold_timer
, jiffies
+ pq
->timeout
))
1969 __skb_queue_head_init(&list
);
1971 spin_lock(&pq
->hold_queue
.lock
);
1973 skb_queue_splice_init(&pq
->hold_queue
, &list
);
1974 spin_unlock(&pq
->hold_queue
.lock
);
1976 while (!skb_queue_empty(&list
)) {
1977 skb
= __skb_dequeue(&list
);
1979 xfrm_decode_session(skb
, &fl
, skb_dst(skb
)->ops
->family
);
1980 dst_hold(skb_dst(skb
)->path
);
1981 dst
= xfrm_lookup(net
, skb_dst(skb
)->path
, &fl
, skb
->sk
, 0);
1989 skb_dst_set(skb
, dst
);
1991 dst_output(net
, skb
->sk
, skb
);
2000 skb_queue_purge(&pq
->hold_queue
);
2004 static int xdst_queue_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
2006 unsigned long sched_next
;
2007 struct dst_entry
*dst
= skb_dst(skb
);
2008 struct xfrm_dst
*xdst
= (struct xfrm_dst
*) dst
;
2009 struct xfrm_policy
*pol
= xdst
->pols
[0];
2010 struct xfrm_policy_queue
*pq
= &pol
->polq
;
2012 if (unlikely(skb_fclone_busy(sk
, skb
))) {
2017 if (pq
->hold_queue
.qlen
> XFRM_MAX_QUEUE_LEN
) {
2024 spin_lock_bh(&pq
->hold_queue
.lock
);
2027 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
2029 sched_next
= jiffies
+ pq
->timeout
;
2031 if (del_timer(&pq
->hold_timer
)) {
2032 if (time_before(pq
->hold_timer
.expires
, sched_next
))
2033 sched_next
= pq
->hold_timer
.expires
;
2037 __skb_queue_tail(&pq
->hold_queue
, skb
);
2038 if (!mod_timer(&pq
->hold_timer
, sched_next
))
2041 spin_unlock_bh(&pq
->hold_queue
.lock
);
2046 static struct xfrm_dst
*xfrm_create_dummy_bundle(struct net
*net
,
2047 struct xfrm_flo
*xflo
,
2048 const struct flowi
*fl
,
2053 struct net_device
*dev
;
2054 struct dst_entry
*dst
;
2055 struct dst_entry
*dst1
;
2056 struct xfrm_dst
*xdst
;
2058 xdst
= xfrm_alloc_dst(net
, family
);
2062 if (!(xflo
->flags
& XFRM_LOOKUP_QUEUE
) ||
2063 net
->xfrm
.sysctl_larval_drop
||
2067 dst
= xflo
->dst_orig
;
2068 dst1
= &xdst
->u
.dst
;
2072 dst_copy_metrics(dst1
, dst
);
2074 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
2075 dst1
->flags
|= DST_HOST
| DST_XFRM_QUEUE
;
2076 dst1
->lastuse
= jiffies
;
2078 dst1
->input
= dst_discard
;
2079 dst1
->output
= xdst_queue_output
;
2085 xfrm_init_path((struct xfrm_dst
*)dst1
, dst
, 0);
2092 err
= xfrm_fill_dst(xdst
, dev
, fl
);
2101 xdst
= ERR_PTR(err
);
2105 static struct flow_cache_object
*
2106 xfrm_bundle_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
,
2107 struct flow_cache_object
*oldflo
, void *ctx
)
2109 struct xfrm_flo
*xflo
= (struct xfrm_flo
*)ctx
;
2110 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2111 struct xfrm_dst
*xdst
, *new_xdst
;
2112 int num_pols
= 0, num_xfrms
= 0, i
, err
, pol_dead
;
2114 /* Check if the policies from old bundle are usable */
2117 xdst
= container_of(oldflo
, struct xfrm_dst
, flo
);
2118 num_pols
= xdst
->num_pols
;
2119 num_xfrms
= xdst
->num_xfrms
;
2121 for (i
= 0; i
< num_pols
; i
++) {
2122 pols
[i
] = xdst
->pols
[i
];
2123 pol_dead
|= pols
[i
]->walk
.dead
;
2126 dst_free(&xdst
->u
.dst
);
2134 /* Resolve policies to use if we couldn't get them from
2135 * previous cache entry */
2138 pols
[0] = __xfrm_policy_lookup(net
, fl
, family
,
2139 flow_to_policy_dir(dir
));
2140 err
= xfrm_expand_policies(fl
, family
, pols
,
2141 &num_pols
, &num_xfrms
);
2147 goto make_dummy_bundle
;
2150 new_xdst
= xfrm_resolve_and_create_bundle(pols
, num_pols
, fl
, family
,
2152 if (IS_ERR(new_xdst
)) {
2153 err
= PTR_ERR(new_xdst
);
2157 goto make_dummy_bundle
;
2158 dst_hold(&xdst
->u
.dst
);
2160 } else if (new_xdst
== NULL
) {
2163 goto make_dummy_bundle
;
2164 xdst
->num_xfrms
= 0;
2165 dst_hold(&xdst
->u
.dst
);
2169 /* Kill the previous bundle */
2171 /* The policies were stolen for newly generated bundle */
2173 dst_free(&xdst
->u
.dst
);
2176 /* Flow cache does not have reference, it dst_free()'s,
2177 * but we do need to return one reference for original caller */
2178 dst_hold(&new_xdst
->u
.dst
);
2179 return &new_xdst
->flo
;
2182 /* We found policies, but there's no bundles to instantiate:
2183 * either because the policy blocks, has no transformations or
2184 * we could not build template (no xfrm_states).*/
2185 xdst
= xfrm_create_dummy_bundle(net
, xflo
, fl
, num_xfrms
, family
);
2187 xfrm_pols_put(pols
, num_pols
);
2188 return ERR_CAST(xdst
);
2190 xdst
->num_pols
= num_pols
;
2191 xdst
->num_xfrms
= num_xfrms
;
2192 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2194 dst_hold(&xdst
->u
.dst
);
2198 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
2201 dst_free(&xdst
->u
.dst
);
2203 xfrm_pols_put(pols
, num_pols
);
2204 return ERR_PTR(err
);
2207 static struct dst_entry
*make_blackhole(struct net
*net
, u16 family
,
2208 struct dst_entry
*dst_orig
)
2210 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2211 struct dst_entry
*ret
;
2214 dst_release(dst_orig
);
2215 return ERR_PTR(-EINVAL
);
2217 ret
= afinfo
->blackhole_route(net
, dst_orig
);
2219 xfrm_policy_put_afinfo(afinfo
);
2224 /* Main function: finds/creates a bundle for given flow.
2226 * At the moment we eat a raw IP route. Mostly to speed up lookups
2227 * on interfaces with disabled IPsec.
2229 struct dst_entry
*xfrm_lookup(struct net
*net
, struct dst_entry
*dst_orig
,
2230 const struct flowi
*fl
,
2231 const struct sock
*sk
, int flags
)
2233 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2234 struct flow_cache_object
*flo
;
2235 struct xfrm_dst
*xdst
;
2236 struct dst_entry
*dst
, *route
;
2237 u16 family
= dst_orig
->ops
->family
;
2238 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
2239 int i
, err
, num_pols
, num_xfrms
= 0, drop_pols
= 0;
2245 sk
= sk_const_to_full_sk(sk
);
2246 if (sk
&& sk
->sk_policy
[XFRM_POLICY_OUT
]) {
2248 pols
[0] = xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
);
2249 err
= xfrm_expand_policies(fl
, family
, pols
,
2250 &num_pols
, &num_xfrms
);
2255 if (num_xfrms
<= 0) {
2256 drop_pols
= num_pols
;
2260 xdst
= xfrm_resolve_and_create_bundle(
2264 xfrm_pols_put(pols
, num_pols
);
2265 err
= PTR_ERR(xdst
);
2267 } else if (xdst
== NULL
) {
2269 drop_pols
= num_pols
;
2273 dst_hold(&xdst
->u
.dst
);
2274 xdst
->u
.dst
.flags
|= DST_NOCACHE
;
2275 route
= xdst
->route
;
2280 struct xfrm_flo xflo
;
2282 xflo
.dst_orig
= dst_orig
;
2285 /* To accelerate a bit... */
2286 if ((dst_orig
->flags
& DST_NOXFRM
) ||
2287 !net
->xfrm
.policy_count
[XFRM_POLICY_OUT
])
2290 flo
= flow_cache_lookup(net
, fl
, family
, dir
,
2291 xfrm_bundle_lookup
, &xflo
);
2298 xdst
= container_of(flo
, struct xfrm_dst
, flo
);
2300 num_pols
= xdst
->num_pols
;
2301 num_xfrms
= xdst
->num_xfrms
;
2302 memcpy(pols
, xdst
->pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2303 route
= xdst
->route
;
2307 if (route
== NULL
&& num_xfrms
> 0) {
2308 /* The only case when xfrm_bundle_lookup() returns a
2309 * bundle with null route, is when the template could
2310 * not be resolved. It means policies are there, but
2311 * bundle could not be created, since we don't yet
2312 * have the xfrm_state's. We need to wait for KM to
2313 * negotiate new SA's or bail out with error.*/
2314 if (net
->xfrm
.sysctl_larval_drop
) {
2315 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2322 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2330 if ((flags
& XFRM_LOOKUP_ICMP
) &&
2331 !(pols
[0]->flags
& XFRM_POLICY_ICMP
)) {
2336 for (i
= 0; i
< num_pols
; i
++)
2337 pols
[i
]->curlft
.use_time
= get_seconds();
2339 if (num_xfrms
< 0) {
2340 /* Prohibit the flow */
2341 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLBLOCK
);
2344 } else if (num_xfrms
> 0) {
2345 /* Flow transformed */
2346 dst_release(dst_orig
);
2348 /* Flow passes untransformed */
2353 xfrm_pols_put(pols
, drop_pols
);
2354 if (dst
&& dst
->xfrm
&&
2355 dst
->xfrm
->props
.mode
== XFRM_MODE_TUNNEL
)
2356 dst
->flags
|= DST_XFRM_TUNNEL
;
2360 if (!(flags
& XFRM_LOOKUP_ICMP
)) {
2368 if (!(flags
& XFRM_LOOKUP_KEEP_DST_REF
))
2369 dst_release(dst_orig
);
2370 xfrm_pols_put(pols
, drop_pols
);
2371 return ERR_PTR(err
);
2373 EXPORT_SYMBOL(xfrm_lookup
);
2375 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2376 * Otherwise we may send out blackholed packets.
2378 struct dst_entry
*xfrm_lookup_route(struct net
*net
, struct dst_entry
*dst_orig
,
2379 const struct flowi
*fl
,
2380 const struct sock
*sk
, int flags
)
2382 struct dst_entry
*dst
= xfrm_lookup(net
, dst_orig
, fl
, sk
,
2383 flags
| XFRM_LOOKUP_QUEUE
|
2384 XFRM_LOOKUP_KEEP_DST_REF
);
2386 if (IS_ERR(dst
) && PTR_ERR(dst
) == -EREMOTE
)
2387 return make_blackhole(net
, dst_orig
->ops
->family
, dst_orig
);
2391 EXPORT_SYMBOL(xfrm_lookup_route
);
2394 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, const struct flowi
*fl
)
2396 struct xfrm_state
*x
;
2398 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
2400 x
= skb
->sp
->xvec
[idx
];
2401 if (!x
->type
->reject
)
2403 return x
->type
->reject(x
, skb
, fl
);
2406 /* When skb is transformed back to its "native" form, we have to
2407 * check policy restrictions. At the moment we make this in maximally
2408 * stupid way. Shame on me. :-) Of course, connected sockets must
2409 * have policy cached at them.
2413 xfrm_state_ok(const struct xfrm_tmpl
*tmpl
, const struct xfrm_state
*x
,
2414 unsigned short family
)
2416 if (xfrm_state_kern(x
))
2417 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
2418 return x
->id
.proto
== tmpl
->id
.proto
&&
2419 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
2420 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
2421 x
->props
.mode
== tmpl
->mode
&&
2422 (tmpl
->allalgs
|| (tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
2423 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
2424 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
2425 xfrm_state_addr_cmp(tmpl
, x
, family
));
2429 * 0 or more than 0 is returned when validation is succeeded (either bypass
2430 * because of optional transport mode, or next index of the mathced secpath
2431 * state with the template.
2432 * -1 is returned when no matching template is found.
2433 * Otherwise "-2 - errored_index" is returned.
2436 xfrm_policy_ok(const struct xfrm_tmpl
*tmpl
, const struct sec_path
*sp
, int start
,
2437 unsigned short family
)
2441 if (tmpl
->optional
) {
2442 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
2446 for (; idx
< sp
->len
; idx
++) {
2447 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
2449 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2458 int __xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
,
2459 unsigned int family
, int reverse
)
2461 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2464 if (unlikely(afinfo
== NULL
))
2465 return -EAFNOSUPPORT
;
2467 afinfo
->decode_session(skb
, fl
, reverse
);
2468 err
= security_xfrm_decode_session(skb
, &fl
->flowi_secid
);
2469 xfrm_policy_put_afinfo(afinfo
);
2472 EXPORT_SYMBOL(__xfrm_decode_session
);
2474 static inline int secpath_has_nontransport(const struct sec_path
*sp
, int k
, int *idxp
)
2476 for (; k
< sp
->len
; k
++) {
2477 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2486 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
2487 unsigned short family
)
2489 struct net
*net
= dev_net(skb
->dev
);
2490 struct xfrm_policy
*pol
;
2491 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2500 reverse
= dir
& ~XFRM_POLICY_MASK
;
2501 dir
&= XFRM_POLICY_MASK
;
2502 fl_dir
= policy_to_flow_dir(dir
);
2504 if (__xfrm_decode_session(skb
, &fl
, family
, reverse
) < 0) {
2505 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
2509 nf_nat_decode_session(skb
, &fl
, family
);
2511 /* First, check used SA against their selectors. */
2515 for (i
= skb
->sp
->len
-1; i
>= 0; i
--) {
2516 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
2517 if (!xfrm_selector_match(&x
->sel
, &fl
, family
)) {
2518 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMISMATCH
);
2525 sk
= sk_to_full_sk(sk
);
2526 if (sk
&& sk
->sk_policy
[dir
]) {
2527 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
);
2529 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2535 struct flow_cache_object
*flo
;
2537 flo
= flow_cache_lookup(net
, &fl
, family
, fl_dir
,
2538 xfrm_policy_lookup
, NULL
);
2539 if (IS_ERR_OR_NULL(flo
))
2540 pol
= ERR_CAST(flo
);
2542 pol
= container_of(flo
, struct xfrm_policy
, flo
);
2546 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2551 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
2552 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2553 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINNOPOLS
);
2559 pol
->curlft
.use_time
= get_seconds();
2563 #ifdef CONFIG_XFRM_SUB_POLICY
2564 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
2565 pols
[1] = xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
,
2569 if (IS_ERR(pols
[1])) {
2570 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2573 pols
[1]->curlft
.use_time
= get_seconds();
2579 if (pol
->action
== XFRM_POLICY_ALLOW
) {
2580 struct sec_path
*sp
;
2581 static struct sec_path dummy
;
2582 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
2583 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
2584 struct xfrm_tmpl
**tpp
= tp
;
2588 if ((sp
= skb
->sp
) == NULL
)
2591 for (pi
= 0; pi
< npols
; pi
++) {
2592 if (pols
[pi
] != pol
&&
2593 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
) {
2594 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2597 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
2598 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
2601 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
2602 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
2606 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
, net
);
2610 /* For each tunnel xfrm, find the first matching tmpl.
2611 * For each tmpl before that, find corresponding xfrm.
2612 * Order is _important_. Later we will implement
2613 * some barriers, but at the moment barriers
2614 * are implied between each two transformations.
2616 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
2617 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
2620 /* "-2 - errored_index" returned */
2622 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2627 if (secpath_has_nontransport(sp
, k
, &xerr_idx
)) {
2628 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2632 xfrm_pols_put(pols
, npols
);
2635 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2638 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2640 xfrm_pols_put(pols
, npols
);
2643 EXPORT_SYMBOL(__xfrm_policy_check
);
2645 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
2647 struct net
*net
= dev_net(skb
->dev
);
2649 struct dst_entry
*dst
;
2652 if (xfrm_decode_session(skb
, &fl
, family
) < 0) {
2653 XFRM_INC_STATS(net
, LINUX_MIB_XFRMFWDHDRERROR
);
2659 dst
= xfrm_lookup(net
, skb_dst(skb
), &fl
, NULL
, XFRM_LOOKUP_QUEUE
);
2664 skb_dst_set(skb
, dst
);
2667 EXPORT_SYMBOL(__xfrm_route_forward
);
2669 /* Optimize later using cookies and generation ids. */
2671 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
2673 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2674 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2675 * get validated by dst_ops->check on every use. We do this
2676 * because when a normal route referenced by an XFRM dst is
2677 * obsoleted we do not go looking around for all parent
2678 * referencing XFRM dsts so that we can invalidate them. It
2679 * is just too much work. Instead we make the checks here on
2680 * every use. For example:
2682 * XFRM dst A --> IPv4 dst X
2684 * X is the "xdst->route" of A (X is also the "dst->path" of A
2685 * in this example). If X is marked obsolete, "A" will not
2686 * notice. That's what we are validating here via the
2687 * stale_bundle() check.
2689 * When a policy's bundle is pruned, we dst_free() the XFRM
2690 * dst which causes it's ->obsolete field to be set to
2691 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2692 * this, we want to force a new route lookup.
2694 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
2700 static int stale_bundle(struct dst_entry
*dst
)
2702 return !xfrm_bundle_ok((struct xfrm_dst
*)dst
);
2705 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
2707 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
2708 dst
->dev
= dev_net(dev
)->loopback_dev
;
2713 EXPORT_SYMBOL(xfrm_dst_ifdown
);
2715 static void xfrm_link_failure(struct sk_buff
*skb
)
2717 /* Impossible. Such dst must be popped before reaches point of failure. */
2720 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
2723 if (dst
->obsolete
) {
2731 void xfrm_garbage_collect(struct net
*net
)
2733 flow_cache_flush(net
);
2735 EXPORT_SYMBOL(xfrm_garbage_collect
);
2737 static void xfrm_garbage_collect_deferred(struct net
*net
)
2739 flow_cache_flush_deferred(net
);
2742 static void xfrm_init_pmtu(struct dst_entry
*dst
)
2745 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2746 u32 pmtu
, route_mtu_cached
;
2748 pmtu
= dst_mtu(dst
->child
);
2749 xdst
->child_mtu_cached
= pmtu
;
2751 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2753 route_mtu_cached
= dst_mtu(xdst
->route
);
2754 xdst
->route_mtu_cached
= route_mtu_cached
;
2756 if (pmtu
> route_mtu_cached
)
2757 pmtu
= route_mtu_cached
;
2759 dst_metric_set(dst
, RTAX_MTU
, pmtu
);
2760 } while ((dst
= dst
->next
));
2763 /* Check that the bundle accepts the flow and its components are
2767 static int xfrm_bundle_ok(struct xfrm_dst
*first
)
2769 struct dst_entry
*dst
= &first
->u
.dst
;
2770 struct xfrm_dst
*last
;
2773 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2774 (dst
->dev
&& !netif_running(dst
->dev
)))
2777 if (dst
->flags
& DST_XFRM_QUEUE
)
2783 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2785 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2787 if (xdst
->xfrm_genid
!= dst
->xfrm
->genid
)
2789 if (xdst
->num_pols
> 0 &&
2790 xdst
->policy_genid
!= atomic_read(&xdst
->pols
[0]->genid
))
2793 mtu
= dst_mtu(dst
->child
);
2794 if (xdst
->child_mtu_cached
!= mtu
) {
2796 xdst
->child_mtu_cached
= mtu
;
2799 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2801 mtu
= dst_mtu(xdst
->route
);
2802 if (xdst
->route_mtu_cached
!= mtu
) {
2804 xdst
->route_mtu_cached
= mtu
;
2808 } while (dst
->xfrm
);
2813 mtu
= last
->child_mtu_cached
;
2817 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2818 if (mtu
> last
->route_mtu_cached
)
2819 mtu
= last
->route_mtu_cached
;
2820 dst_metric_set(dst
, RTAX_MTU
, mtu
);
2825 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2826 last
->child_mtu_cached
= mtu
;
2832 static unsigned int xfrm_default_advmss(const struct dst_entry
*dst
)
2834 return dst_metric_advmss(dst
->path
);
2837 static unsigned int xfrm_mtu(const struct dst_entry
*dst
)
2839 unsigned int mtu
= dst_metric_raw(dst
, RTAX_MTU
);
2841 return mtu
? : dst_mtu(dst
->path
);
2844 static struct neighbour
*xfrm_neigh_lookup(const struct dst_entry
*dst
,
2845 struct sk_buff
*skb
,
2848 return dst
->path
->ops
->neigh_lookup(dst
, skb
, daddr
);
2851 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2854 if (unlikely(afinfo
== NULL
))
2856 if (unlikely(afinfo
->family
>= NPROTO
))
2857 return -EAFNOSUPPORT
;
2858 spin_lock(&xfrm_policy_afinfo_lock
);
2859 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
2862 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2863 if (likely(dst_ops
->kmem_cachep
== NULL
))
2864 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2865 if (likely(dst_ops
->check
== NULL
))
2866 dst_ops
->check
= xfrm_dst_check
;
2867 if (likely(dst_ops
->default_advmss
== NULL
))
2868 dst_ops
->default_advmss
= xfrm_default_advmss
;
2869 if (likely(dst_ops
->mtu
== NULL
))
2870 dst_ops
->mtu
= xfrm_mtu
;
2871 if (likely(dst_ops
->negative_advice
== NULL
))
2872 dst_ops
->negative_advice
= xfrm_negative_advice
;
2873 if (likely(dst_ops
->link_failure
== NULL
))
2874 dst_ops
->link_failure
= xfrm_link_failure
;
2875 if (likely(dst_ops
->neigh_lookup
== NULL
))
2876 dst_ops
->neigh_lookup
= xfrm_neigh_lookup
;
2877 if (likely(afinfo
->garbage_collect
== NULL
))
2878 afinfo
->garbage_collect
= xfrm_garbage_collect_deferred
;
2879 rcu_assign_pointer(xfrm_policy_afinfo
[afinfo
->family
], afinfo
);
2881 spin_unlock(&xfrm_policy_afinfo_lock
);
2885 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2887 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2890 if (unlikely(afinfo
== NULL
))
2892 if (unlikely(afinfo
->family
>= NPROTO
))
2893 return -EAFNOSUPPORT
;
2894 spin_lock(&xfrm_policy_afinfo_lock
);
2895 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
2896 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
2899 RCU_INIT_POINTER(xfrm_policy_afinfo
[afinfo
->family
],
2902 spin_unlock(&xfrm_policy_afinfo_lock
);
2904 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2908 dst_ops
->kmem_cachep
= NULL
;
2909 dst_ops
->check
= NULL
;
2910 dst_ops
->negative_advice
= NULL
;
2911 dst_ops
->link_failure
= NULL
;
2912 afinfo
->garbage_collect
= NULL
;
2916 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2918 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
2920 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2924 xfrm_garbage_collect(dev_net(dev
));
2929 static struct notifier_block xfrm_dev_notifier
= {
2930 .notifier_call
= xfrm_dev_event
,
2933 #ifdef CONFIG_XFRM_STATISTICS
2934 static int __net_init
xfrm_statistics_init(struct net
*net
)
2937 net
->mib
.xfrm_statistics
= alloc_percpu(struct linux_xfrm_mib
);
2938 if (!net
->mib
.xfrm_statistics
)
2940 rv
= xfrm_proc_init(net
);
2942 free_percpu(net
->mib
.xfrm_statistics
);
2946 static void xfrm_statistics_fini(struct net
*net
)
2948 xfrm_proc_fini(net
);
2949 free_percpu(net
->mib
.xfrm_statistics
);
2952 static int __net_init
xfrm_statistics_init(struct net
*net
)
2957 static void xfrm_statistics_fini(struct net
*net
)
2962 static int __net_init
xfrm_policy_init(struct net
*net
)
2964 unsigned int hmask
, sz
;
2967 if (net_eq(net
, &init_net
))
2968 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2969 sizeof(struct xfrm_dst
),
2970 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2974 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2976 net
->xfrm
.policy_byidx
= xfrm_hash_alloc(sz
);
2977 if (!net
->xfrm
.policy_byidx
)
2979 net
->xfrm
.policy_idx_hmask
= hmask
;
2981 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
2982 struct xfrm_policy_hash
*htab
;
2984 net
->xfrm
.policy_count
[dir
] = 0;
2985 net
->xfrm
.policy_count
[XFRM_POLICY_MAX
+ dir
] = 0;
2986 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
2988 htab
= &net
->xfrm
.policy_bydst
[dir
];
2989 htab
->table
= xfrm_hash_alloc(sz
);
2992 htab
->hmask
= hmask
;
2998 net
->xfrm
.policy_hthresh
.lbits4
= 32;
2999 net
->xfrm
.policy_hthresh
.rbits4
= 32;
3000 net
->xfrm
.policy_hthresh
.lbits6
= 128;
3001 net
->xfrm
.policy_hthresh
.rbits6
= 128;
3003 seqlock_init(&net
->xfrm
.policy_hthresh
.lock
);
3005 INIT_LIST_HEAD(&net
->xfrm
.policy_all
);
3006 INIT_WORK(&net
->xfrm
.policy_hash_work
, xfrm_hash_resize
);
3007 INIT_WORK(&net
->xfrm
.policy_hthresh
.work
, xfrm_hash_rebuild
);
3008 if (net_eq(net
, &init_net
))
3009 register_netdevice_notifier(&xfrm_dev_notifier
);
3013 for (dir
--; dir
>= 0; dir
--) {
3014 struct xfrm_policy_hash
*htab
;
3016 htab
= &net
->xfrm
.policy_bydst
[dir
];
3017 xfrm_hash_free(htab
->table
, sz
);
3019 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
3024 static void xfrm_policy_fini(struct net
*net
)
3029 flush_work(&net
->xfrm
.policy_hash_work
);
3030 #ifdef CONFIG_XFRM_SUB_POLICY
3031 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_SUB
, false);
3033 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, false);
3035 WARN_ON(!list_empty(&net
->xfrm
.policy_all
));
3037 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
3038 struct xfrm_policy_hash
*htab
;
3040 WARN_ON(!hlist_empty(&net
->xfrm
.policy_inexact
[dir
]));
3042 htab
= &net
->xfrm
.policy_bydst
[dir
];
3043 sz
= (htab
->hmask
+ 1) * sizeof(struct hlist_head
);
3044 WARN_ON(!hlist_empty(htab
->table
));
3045 xfrm_hash_free(htab
->table
, sz
);
3048 sz
= (net
->xfrm
.policy_idx_hmask
+ 1) * sizeof(struct hlist_head
);
3049 WARN_ON(!hlist_empty(net
->xfrm
.policy_byidx
));
3050 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
3053 static int __net_init
xfrm_net_init(struct net
*net
)
3057 rv
= xfrm_statistics_init(net
);
3059 goto out_statistics
;
3060 rv
= xfrm_state_init(net
);
3063 rv
= xfrm_policy_init(net
);
3066 rv
= xfrm_sysctl_init(net
);
3069 rv
= flow_cache_init(net
);
3073 /* Initialize the per-net locks here */
3074 spin_lock_init(&net
->xfrm
.xfrm_state_lock
);
3075 rwlock_init(&net
->xfrm
.xfrm_policy_lock
);
3076 mutex_init(&net
->xfrm
.xfrm_cfg_mutex
);
3081 xfrm_sysctl_fini(net
);
3083 xfrm_policy_fini(net
);
3085 xfrm_state_fini(net
);
3087 xfrm_statistics_fini(net
);
3092 static void __net_exit
xfrm_net_exit(struct net
*net
)
3094 flow_cache_fini(net
);
3095 xfrm_sysctl_fini(net
);
3096 xfrm_policy_fini(net
);
3097 xfrm_state_fini(net
);
3098 xfrm_statistics_fini(net
);
3101 static struct pernet_operations __net_initdata xfrm_net_ops
= {
3102 .init
= xfrm_net_init
,
3103 .exit
= xfrm_net_exit
,
3106 void __init
xfrm_init(void)
3108 register_pernet_subsys(&xfrm_net_ops
);
3109 seqcount_init(&xfrm_policy_hash_generation
);
3113 #ifdef CONFIG_AUDITSYSCALL
3114 static void xfrm_audit_common_policyinfo(struct xfrm_policy
*xp
,
3115 struct audit_buffer
*audit_buf
)
3117 struct xfrm_sec_ctx
*ctx
= xp
->security
;
3118 struct xfrm_selector
*sel
= &xp
->selector
;
3121 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
3122 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
3124 switch (sel
->family
) {
3126 audit_log_format(audit_buf
, " src=%pI4", &sel
->saddr
.a4
);
3127 if (sel
->prefixlen_s
!= 32)
3128 audit_log_format(audit_buf
, " src_prefixlen=%d",
3130 audit_log_format(audit_buf
, " dst=%pI4", &sel
->daddr
.a4
);
3131 if (sel
->prefixlen_d
!= 32)
3132 audit_log_format(audit_buf
, " dst_prefixlen=%d",
3136 audit_log_format(audit_buf
, " src=%pI6", sel
->saddr
.a6
);
3137 if (sel
->prefixlen_s
!= 128)
3138 audit_log_format(audit_buf
, " src_prefixlen=%d",
3140 audit_log_format(audit_buf
, " dst=%pI6", sel
->daddr
.a6
);
3141 if (sel
->prefixlen_d
!= 128)
3142 audit_log_format(audit_buf
, " dst_prefixlen=%d",
3148 void xfrm_audit_policy_add(struct xfrm_policy
*xp
, int result
, bool task_valid
)
3150 struct audit_buffer
*audit_buf
;
3152 audit_buf
= xfrm_audit_start("SPD-add");
3153 if (audit_buf
== NULL
)
3155 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
3156 audit_log_format(audit_buf
, " res=%u", result
);
3157 xfrm_audit_common_policyinfo(xp
, audit_buf
);
3158 audit_log_end(audit_buf
);
3160 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add
);
3162 void xfrm_audit_policy_delete(struct xfrm_policy
*xp
, int result
,
3165 struct audit_buffer
*audit_buf
;
3167 audit_buf
= xfrm_audit_start("SPD-delete");
3168 if (audit_buf
== NULL
)
3170 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
3171 audit_log_format(audit_buf
, " res=%u", result
);
3172 xfrm_audit_common_policyinfo(xp
, audit_buf
);
3173 audit_log_end(audit_buf
);
3175 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete
);
3178 #ifdef CONFIG_XFRM_MIGRATE
3179 static bool xfrm_migrate_selector_match(const struct xfrm_selector
*sel_cmp
,
3180 const struct xfrm_selector
*sel_tgt
)
3182 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
3183 if (sel_tgt
->family
== sel_cmp
->family
&&
3184 xfrm_addr_equal(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
3186 xfrm_addr_equal(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
3188 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
3189 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
3193 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
3200 static struct xfrm_policy
*xfrm_migrate_policy_find(const struct xfrm_selector
*sel
,
3201 u8 dir
, u8 type
, struct net
*net
)
3203 struct xfrm_policy
*pol
, *ret
= NULL
;
3204 struct hlist_head
*chain
;
3207 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
); /*FIXME*/
3208 chain
= policy_hash_direct(net
, &sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
3209 hlist_for_each_entry(pol
, chain
, bydst
) {
3210 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3211 pol
->type
== type
) {
3213 priority
= ret
->priority
;
3217 chain
= &net
->xfrm
.policy_inexact
[dir
];
3218 hlist_for_each_entry(pol
, chain
, bydst
) {
3219 if ((pol
->priority
>= priority
) && ret
)
3222 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3223 pol
->type
== type
) {
3231 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
3236 static int migrate_tmpl_match(const struct xfrm_migrate
*m
, const struct xfrm_tmpl
*t
)
3240 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
3241 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
3243 case XFRM_MODE_TUNNEL
:
3244 case XFRM_MODE_BEET
:
3245 if (xfrm_addr_equal(&t
->id
.daddr
, &m
->old_daddr
,
3247 xfrm_addr_equal(&t
->saddr
, &m
->old_saddr
,
3252 case XFRM_MODE_TRANSPORT
:
3253 /* in case of transport mode, template does not store
3254 any IP addresses, hence we just compare mode and
3265 /* update endpoint address(es) of template(s) */
3266 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
3267 struct xfrm_migrate
*m
, int num_migrate
)
3269 struct xfrm_migrate
*mp
;
3272 write_lock_bh(&pol
->lock
);
3273 if (unlikely(pol
->walk
.dead
)) {
3274 /* target policy has been deleted */
3275 write_unlock_bh(&pol
->lock
);
3279 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
3280 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
3281 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
3284 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
&&
3285 pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_BEET
)
3287 /* update endpoints */
3288 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
3289 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
3290 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
3291 sizeof(pol
->xfrm_vec
[i
].saddr
));
3292 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
3294 atomic_inc(&pol
->genid
);
3298 write_unlock_bh(&pol
->lock
);
3306 static int xfrm_migrate_check(const struct xfrm_migrate
*m
, int num_migrate
)
3310 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
3313 for (i
= 0; i
< num_migrate
; i
++) {
3314 if (xfrm_addr_equal(&m
[i
].old_daddr
, &m
[i
].new_daddr
,
3316 xfrm_addr_equal(&m
[i
].old_saddr
, &m
[i
].new_saddr
,
3319 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
3320 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
3323 /* check if there is any duplicated entry */
3324 for (j
= i
+ 1; j
< num_migrate
; j
++) {
3325 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
3326 sizeof(m
[i
].old_daddr
)) &&
3327 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
3328 sizeof(m
[i
].old_saddr
)) &&
3329 m
[i
].proto
== m
[j
].proto
&&
3330 m
[i
].mode
== m
[j
].mode
&&
3331 m
[i
].reqid
== m
[j
].reqid
&&
3332 m
[i
].old_family
== m
[j
].old_family
)
3340 int xfrm_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3341 struct xfrm_migrate
*m
, int num_migrate
,
3342 struct xfrm_kmaddress
*k
, struct net
*net
)
3344 int i
, err
, nx_cur
= 0, nx_new
= 0;
3345 struct xfrm_policy
*pol
= NULL
;
3346 struct xfrm_state
*x
, *xc
;
3347 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
3348 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
3349 struct xfrm_migrate
*mp
;
3351 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
3354 /* Stage 1 - find policy */
3355 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
, net
)) == NULL
) {
3360 /* Stage 2 - find and update state(s) */
3361 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
3362 if ((x
= xfrm_migrate_state_find(mp
, net
))) {
3365 if ((xc
= xfrm_state_migrate(x
, mp
))) {
3375 /* Stage 3 - update policy */
3376 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
3379 /* Stage 4 - delete old state(s) */
3381 xfrm_states_put(x_cur
, nx_cur
);
3382 xfrm_states_delete(x_cur
, nx_cur
);
3385 /* Stage 5 - announce */
3386 km_migrate(sel
, dir
, type
, m
, num_migrate
, k
);
3398 xfrm_states_put(x_cur
, nx_cur
);
3400 xfrm_states_delete(x_new
, nx_new
);
3404 EXPORT_SYMBOL(xfrm_migrate
);