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
;
53 static void xfrm_init_pmtu(struct dst_entry
*dst
);
54 static int stale_bundle(struct dst_entry
*dst
);
55 static int xfrm_bundle_ok(struct xfrm_dst
*xdst
);
56 static void xfrm_policy_queue_process(unsigned long arg
);
58 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
);
59 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
63 __xfrm4_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
65 const struct flowi4
*fl4
= &fl
->u
.ip4
;
67 return addr4_match(fl4
->daddr
, sel
->daddr
.a4
, sel
->prefixlen_d
) &&
68 addr4_match(fl4
->saddr
, sel
->saddr
.a4
, sel
->prefixlen_s
) &&
69 !((xfrm_flowi_dport(fl
, &fl4
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
70 !((xfrm_flowi_sport(fl
, &fl4
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
71 (fl4
->flowi4_proto
== sel
->proto
|| !sel
->proto
) &&
72 (fl4
->flowi4_oif
== sel
->ifindex
|| !sel
->ifindex
);
76 __xfrm6_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
78 const struct flowi6
*fl6
= &fl
->u
.ip6
;
80 return addr_match(&fl6
->daddr
, &sel
->daddr
, sel
->prefixlen_d
) &&
81 addr_match(&fl6
->saddr
, &sel
->saddr
, sel
->prefixlen_s
) &&
82 !((xfrm_flowi_dport(fl
, &fl6
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
83 !((xfrm_flowi_sport(fl
, &fl6
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
84 (fl6
->flowi6_proto
== sel
->proto
|| !sel
->proto
) &&
85 (fl6
->flowi6_oif
== sel
->ifindex
|| !sel
->ifindex
);
88 bool xfrm_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
,
89 unsigned short family
)
93 return __xfrm4_selector_match(sel
, fl
);
95 return __xfrm6_selector_match(sel
, fl
);
100 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
102 struct xfrm_policy_afinfo
*afinfo
;
104 if (unlikely(family
>= NPROTO
))
107 afinfo
= rcu_dereference(xfrm_policy_afinfo
[family
]);
108 if (unlikely(!afinfo
))
113 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
)
118 static inline struct dst_entry
*__xfrm_dst_lookup(struct net
*net
,
120 const xfrm_address_t
*saddr
,
121 const xfrm_address_t
*daddr
,
124 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
);
133 xfrm_policy_put_afinfo(afinfo
);
138 static inline struct dst_entry
*xfrm_dst_lookup(struct xfrm_state
*x
,
140 xfrm_address_t
*prev_saddr
,
141 xfrm_address_t
*prev_daddr
,
144 struct net
*net
= xs_net(x
);
145 xfrm_address_t
*saddr
= &x
->props
.saddr
;
146 xfrm_address_t
*daddr
= &x
->id
.daddr
;
147 struct dst_entry
*dst
;
149 if (x
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
) {
153 if (x
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
) {
158 dst
= __xfrm_dst_lookup(net
, tos
, oif
, saddr
, daddr
, family
);
161 if (prev_saddr
!= saddr
)
162 memcpy(prev_saddr
, saddr
, sizeof(*prev_saddr
));
163 if (prev_daddr
!= daddr
)
164 memcpy(prev_daddr
, daddr
, sizeof(*prev_daddr
));
170 static inline unsigned long make_jiffies(long secs
)
172 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
173 return MAX_SCHEDULE_TIMEOUT
-1;
178 static void xfrm_policy_timer(unsigned long data
)
180 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
181 unsigned long now
= get_seconds();
182 long next
= LONG_MAX
;
186 read_lock(&xp
->lock
);
188 if (unlikely(xp
->walk
.dead
))
191 dir
= xfrm_policy_id2dir(xp
->index
);
193 if (xp
->lft
.hard_add_expires_seconds
) {
194 long tmo
= xp
->lft
.hard_add_expires_seconds
+
195 xp
->curlft
.add_time
- now
;
201 if (xp
->lft
.hard_use_expires_seconds
) {
202 long tmo
= xp
->lft
.hard_use_expires_seconds
+
203 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
209 if (xp
->lft
.soft_add_expires_seconds
) {
210 long tmo
= xp
->lft
.soft_add_expires_seconds
+
211 xp
->curlft
.add_time
- now
;
214 tmo
= XFRM_KM_TIMEOUT
;
219 if (xp
->lft
.soft_use_expires_seconds
) {
220 long tmo
= xp
->lft
.soft_use_expires_seconds
+
221 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
224 tmo
= XFRM_KM_TIMEOUT
;
231 km_policy_expired(xp
, dir
, 0, 0);
232 if (next
!= LONG_MAX
&&
233 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
237 read_unlock(&xp
->lock
);
242 read_unlock(&xp
->lock
);
243 if (!xfrm_policy_delete(xp
, dir
))
244 km_policy_expired(xp
, dir
, 1, 0);
248 static struct flow_cache_object
*xfrm_policy_flo_get(struct flow_cache_object
*flo
)
250 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
252 if (unlikely(pol
->walk
.dead
))
260 static int xfrm_policy_flo_check(struct flow_cache_object
*flo
)
262 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
264 return !pol
->walk
.dead
;
267 static void xfrm_policy_flo_delete(struct flow_cache_object
*flo
)
269 xfrm_pol_put(container_of(flo
, struct xfrm_policy
, flo
));
272 static const struct flow_cache_ops xfrm_policy_fc_ops
= {
273 .get
= xfrm_policy_flo_get
,
274 .check
= xfrm_policy_flo_check
,
275 .delete = xfrm_policy_flo_delete
,
278 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
282 struct xfrm_policy
*xfrm_policy_alloc(struct net
*net
, gfp_t gfp
)
284 struct xfrm_policy
*policy
;
286 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
289 write_pnet(&policy
->xp_net
, net
);
290 INIT_LIST_HEAD(&policy
->walk
.all
);
291 INIT_HLIST_NODE(&policy
->bydst
);
292 INIT_HLIST_NODE(&policy
->byidx
);
293 rwlock_init(&policy
->lock
);
294 atomic_set(&policy
->refcnt
, 1);
295 skb_queue_head_init(&policy
->polq
.hold_queue
);
296 setup_timer(&policy
->timer
, xfrm_policy_timer
,
297 (unsigned long)policy
);
298 setup_timer(&policy
->polq
.hold_timer
, xfrm_policy_queue_process
,
299 (unsigned long)policy
);
300 policy
->flo
.ops
= &xfrm_policy_fc_ops
;
304 EXPORT_SYMBOL(xfrm_policy_alloc
);
306 static void xfrm_policy_destroy_rcu(struct rcu_head
*head
)
308 struct xfrm_policy
*policy
= container_of(head
, struct xfrm_policy
, rcu
);
310 security_xfrm_policy_free(policy
->security
);
314 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
316 void xfrm_policy_destroy(struct xfrm_policy
*policy
)
318 BUG_ON(!policy
->walk
.dead
);
320 if (del_timer(&policy
->timer
) || del_timer(&policy
->polq
.hold_timer
))
323 call_rcu(&policy
->rcu
, xfrm_policy_destroy_rcu
);
325 EXPORT_SYMBOL(xfrm_policy_destroy
);
327 /* Rule must be locked. Release descentant resources, announce
328 * entry dead. The rule must be unlinked from lists to the moment.
331 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
333 policy
->walk
.dead
= 1;
335 atomic_inc(&policy
->genid
);
337 if (del_timer(&policy
->polq
.hold_timer
))
338 xfrm_pol_put(policy
);
339 skb_queue_purge(&policy
->polq
.hold_queue
);
341 if (del_timer(&policy
->timer
))
342 xfrm_pol_put(policy
);
344 xfrm_pol_put(policy
);
347 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
349 static inline unsigned int idx_hash(struct net
*net
, u32 index
)
351 return __idx_hash(index
, net
->xfrm
.policy_idx_hmask
);
354 /* calculate policy hash thresholds */
355 static void __get_hash_thresh(struct net
*net
,
356 unsigned short family
, int dir
,
357 u8
*dbits
, u8
*sbits
)
361 *dbits
= net
->xfrm
.policy_bydst
[dir
].dbits4
;
362 *sbits
= net
->xfrm
.policy_bydst
[dir
].sbits4
;
366 *dbits
= net
->xfrm
.policy_bydst
[dir
].dbits6
;
367 *sbits
= net
->xfrm
.policy_bydst
[dir
].sbits6
;
376 static struct hlist_head
*policy_hash_bysel(struct net
*net
,
377 const struct xfrm_selector
*sel
,
378 unsigned short family
, int dir
)
380 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
385 __get_hash_thresh(net
, family
, dir
, &dbits
, &sbits
);
386 hash
= __sel_hash(sel
, family
, hmask
, dbits
, sbits
);
388 return (hash
== hmask
+ 1 ?
389 &net
->xfrm
.policy_inexact
[dir
] :
390 net
->xfrm
.policy_bydst
[dir
].table
+ hash
);
393 static struct hlist_head
*policy_hash_direct(struct net
*net
,
394 const xfrm_address_t
*daddr
,
395 const xfrm_address_t
*saddr
,
396 unsigned short family
, int dir
)
398 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
403 __get_hash_thresh(net
, family
, dir
, &dbits
, &sbits
);
404 hash
= __addr_hash(daddr
, saddr
, family
, hmask
, dbits
, sbits
);
406 return net
->xfrm
.policy_bydst
[dir
].table
+ hash
;
409 static void xfrm_dst_hash_transfer(struct net
*net
,
410 struct hlist_head
*list
,
411 struct hlist_head
*ndsttable
,
412 unsigned int nhashmask
,
415 struct hlist_node
*tmp
, *entry0
= NULL
;
416 struct xfrm_policy
*pol
;
422 hlist_for_each_entry_safe(pol
, tmp
, list
, bydst
) {
425 __get_hash_thresh(net
, pol
->family
, dir
, &dbits
, &sbits
);
426 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
427 pol
->family
, nhashmask
, dbits
, sbits
);
429 hlist_del(&pol
->bydst
);
430 hlist_add_head(&pol
->bydst
, ndsttable
+h
);
435 hlist_del(&pol
->bydst
);
436 hlist_add_behind(&pol
->bydst
, entry0
);
438 entry0
= &pol
->bydst
;
440 if (!hlist_empty(list
)) {
446 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
447 struct hlist_head
*nidxtable
,
448 unsigned int nhashmask
)
450 struct hlist_node
*tmp
;
451 struct xfrm_policy
*pol
;
453 hlist_for_each_entry_safe(pol
, tmp
, list
, byidx
) {
456 h
= __idx_hash(pol
->index
, nhashmask
);
457 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
461 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
463 return ((old_hmask
+ 1) << 1) - 1;
466 static void xfrm_bydst_resize(struct net
*net
, int dir
)
468 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
469 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
470 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
471 struct hlist_head
*odst
= net
->xfrm
.policy_bydst
[dir
].table
;
472 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
478 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
480 for (i
= hmask
; i
>= 0; i
--)
481 xfrm_dst_hash_transfer(net
, odst
+ i
, ndst
, nhashmask
, dir
);
483 net
->xfrm
.policy_bydst
[dir
].table
= ndst
;
484 net
->xfrm
.policy_bydst
[dir
].hmask
= nhashmask
;
486 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
488 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
491 static void xfrm_byidx_resize(struct net
*net
, int total
)
493 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
494 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
495 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
496 struct hlist_head
*oidx
= net
->xfrm
.policy_byidx
;
497 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
503 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
505 for (i
= hmask
; i
>= 0; i
--)
506 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
508 net
->xfrm
.policy_byidx
= nidx
;
509 net
->xfrm
.policy_idx_hmask
= nhashmask
;
511 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
513 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
516 static inline int xfrm_bydst_should_resize(struct net
*net
, int dir
, int *total
)
518 unsigned int cnt
= net
->xfrm
.policy_count
[dir
];
519 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
524 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
531 static inline int xfrm_byidx_should_resize(struct net
*net
, int total
)
533 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
535 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
542 void xfrm_spd_getinfo(struct net
*net
, struct xfrmk_spdinfo
*si
)
544 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
545 si
->incnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
];
546 si
->outcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
];
547 si
->fwdcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
];
548 si
->inscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
549 si
->outscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
550 si
->fwdscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
551 si
->spdhcnt
= net
->xfrm
.policy_idx_hmask
;
552 si
->spdhmcnt
= xfrm_policy_hashmax
;
553 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
555 EXPORT_SYMBOL(xfrm_spd_getinfo
);
557 static DEFINE_MUTEX(hash_resize_mutex
);
558 static void xfrm_hash_resize(struct work_struct
*work
)
560 struct net
*net
= container_of(work
, struct net
, xfrm
.policy_hash_work
);
563 mutex_lock(&hash_resize_mutex
);
566 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
567 if (xfrm_bydst_should_resize(net
, dir
, &total
))
568 xfrm_bydst_resize(net
, dir
);
570 if (xfrm_byidx_should_resize(net
, total
))
571 xfrm_byidx_resize(net
, total
);
573 mutex_unlock(&hash_resize_mutex
);
576 static void xfrm_hash_rebuild(struct work_struct
*work
)
578 struct net
*net
= container_of(work
, struct net
,
579 xfrm
.policy_hthresh
.work
);
581 struct xfrm_policy
*pol
;
582 struct xfrm_policy
*policy
;
583 struct hlist_head
*chain
;
584 struct hlist_head
*odst
;
585 struct hlist_node
*newpos
;
589 u8 lbits4
, rbits4
, lbits6
, rbits6
;
591 mutex_lock(&hash_resize_mutex
);
593 /* read selector prefixlen thresholds */
595 seq
= read_seqbegin(&net
->xfrm
.policy_hthresh
.lock
);
597 lbits4
= net
->xfrm
.policy_hthresh
.lbits4
;
598 rbits4
= net
->xfrm
.policy_hthresh
.rbits4
;
599 lbits6
= net
->xfrm
.policy_hthresh
.lbits6
;
600 rbits6
= net
->xfrm
.policy_hthresh
.rbits6
;
601 } while (read_seqretry(&net
->xfrm
.policy_hthresh
.lock
, seq
));
603 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
605 /* reset the bydst and inexact table in all directions */
606 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
607 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
608 hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
609 odst
= net
->xfrm
.policy_bydst
[dir
].table
;
610 for (i
= hmask
; i
>= 0; i
--)
611 INIT_HLIST_HEAD(odst
+ i
);
612 if ((dir
& XFRM_POLICY_MASK
) == XFRM_POLICY_OUT
) {
613 /* dir out => dst = remote, src = local */
614 net
->xfrm
.policy_bydst
[dir
].dbits4
= rbits4
;
615 net
->xfrm
.policy_bydst
[dir
].sbits4
= lbits4
;
616 net
->xfrm
.policy_bydst
[dir
].dbits6
= rbits6
;
617 net
->xfrm
.policy_bydst
[dir
].sbits6
= lbits6
;
619 /* dir in/fwd => dst = local, src = remote */
620 net
->xfrm
.policy_bydst
[dir
].dbits4
= lbits4
;
621 net
->xfrm
.policy_bydst
[dir
].sbits4
= rbits4
;
622 net
->xfrm
.policy_bydst
[dir
].dbits6
= lbits6
;
623 net
->xfrm
.policy_bydst
[dir
].sbits6
= rbits6
;
627 /* re-insert all policies by order of creation */
628 list_for_each_entry_reverse(policy
, &net
->xfrm
.policy_all
, walk
.all
) {
630 chain
= policy_hash_bysel(net
, &policy
->selector
,
632 xfrm_policy_id2dir(policy
->index
));
633 hlist_for_each_entry(pol
, chain
, bydst
) {
634 if (policy
->priority
>= pol
->priority
)
635 newpos
= &pol
->bydst
;
640 hlist_add_behind(&policy
->bydst
, newpos
);
642 hlist_add_head(&policy
->bydst
, chain
);
645 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
647 mutex_unlock(&hash_resize_mutex
);
650 void xfrm_policy_hash_rebuild(struct net
*net
)
652 schedule_work(&net
->xfrm
.policy_hthresh
.work
);
654 EXPORT_SYMBOL(xfrm_policy_hash_rebuild
);
656 /* Generate new index... KAME seems to generate them ordered by cost
657 * of an absolute inpredictability of ordering of rules. This will not pass. */
658 static u32
xfrm_gen_index(struct net
*net
, int dir
, u32 index
)
660 static u32 idx_generator
;
663 struct hlist_head
*list
;
664 struct xfrm_policy
*p
;
669 idx
= (idx_generator
| dir
);
678 list
= net
->xfrm
.policy_byidx
+ idx_hash(net
, idx
);
680 hlist_for_each_entry(p
, list
, byidx
) {
681 if (p
->index
== idx
) {
691 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
693 u32
*p1
= (u32
*) s1
;
694 u32
*p2
= (u32
*) s2
;
695 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
698 for (i
= 0; i
< len
; i
++) {
706 static void xfrm_policy_requeue(struct xfrm_policy
*old
,
707 struct xfrm_policy
*new)
709 struct xfrm_policy_queue
*pq
= &old
->polq
;
710 struct sk_buff_head list
;
712 if (skb_queue_empty(&pq
->hold_queue
))
715 __skb_queue_head_init(&list
);
717 spin_lock_bh(&pq
->hold_queue
.lock
);
718 skb_queue_splice_init(&pq
->hold_queue
, &list
);
719 if (del_timer(&pq
->hold_timer
))
721 spin_unlock_bh(&pq
->hold_queue
.lock
);
725 spin_lock_bh(&pq
->hold_queue
.lock
);
726 skb_queue_splice(&list
, &pq
->hold_queue
);
727 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
728 if (!mod_timer(&pq
->hold_timer
, jiffies
))
730 spin_unlock_bh(&pq
->hold_queue
.lock
);
733 static bool xfrm_policy_mark_match(struct xfrm_policy
*policy
,
734 struct xfrm_policy
*pol
)
736 u32 mark
= policy
->mark
.v
& policy
->mark
.m
;
738 if (policy
->mark
.v
== pol
->mark
.v
&& policy
->mark
.m
== pol
->mark
.m
)
741 if ((mark
& pol
->mark
.m
) == pol
->mark
.v
&&
742 policy
->priority
== pol
->priority
)
748 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
750 struct net
*net
= xp_net(policy
);
751 struct xfrm_policy
*pol
;
752 struct xfrm_policy
*delpol
;
753 struct hlist_head
*chain
;
754 struct hlist_node
*newpos
;
756 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
757 chain
= policy_hash_bysel(net
, &policy
->selector
, policy
->family
, dir
);
760 hlist_for_each_entry(pol
, chain
, bydst
) {
761 if (pol
->type
== policy
->type
&&
762 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
763 xfrm_policy_mark_match(policy
, pol
) &&
764 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
767 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
771 if (policy
->priority
> pol
->priority
)
773 } else if (policy
->priority
>= pol
->priority
) {
774 newpos
= &pol
->bydst
;
781 hlist_add_behind(&policy
->bydst
, newpos
);
783 hlist_add_head(&policy
->bydst
, chain
);
784 __xfrm_policy_link(policy
, dir
);
785 atomic_inc(&net
->xfrm
.flow_cache_genid
);
787 /* After previous checking, family can either be AF_INET or AF_INET6 */
788 if (policy
->family
== AF_INET
)
789 rt_genid_bump_ipv4(net
);
791 rt_genid_bump_ipv6(net
);
794 xfrm_policy_requeue(delpol
, policy
);
795 __xfrm_policy_unlink(delpol
, dir
);
797 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(net
, dir
, policy
->index
);
798 hlist_add_head(&policy
->byidx
, net
->xfrm
.policy_byidx
+idx_hash(net
, policy
->index
));
799 policy
->curlft
.add_time
= get_seconds();
800 policy
->curlft
.use_time
= 0;
801 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
802 xfrm_pol_hold(policy
);
803 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
806 xfrm_policy_kill(delpol
);
807 else if (xfrm_bydst_should_resize(net
, dir
, NULL
))
808 schedule_work(&net
->xfrm
.policy_hash_work
);
812 EXPORT_SYMBOL(xfrm_policy_insert
);
814 struct xfrm_policy
*xfrm_policy_bysel_ctx(struct net
*net
, u32 mark
, u8 type
,
815 int dir
, struct xfrm_selector
*sel
,
816 struct xfrm_sec_ctx
*ctx
, int delete,
819 struct xfrm_policy
*pol
, *ret
;
820 struct hlist_head
*chain
;
823 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
824 chain
= policy_hash_bysel(net
, sel
, sel
->family
, dir
);
826 hlist_for_each_entry(pol
, chain
, bydst
) {
827 if (pol
->type
== type
&&
828 (mark
& pol
->mark
.m
) == pol
->mark
.v
&&
829 !selector_cmp(sel
, &pol
->selector
) &&
830 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
833 *err
= security_xfrm_policy_delete(
836 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
839 __xfrm_policy_unlink(pol
, dir
);
845 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
848 xfrm_policy_kill(ret
);
851 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
853 struct xfrm_policy
*xfrm_policy_byid(struct net
*net
, u32 mark
, u8 type
,
854 int dir
, u32 id
, int delete, int *err
)
856 struct xfrm_policy
*pol
, *ret
;
857 struct hlist_head
*chain
;
860 if (xfrm_policy_id2dir(id
) != dir
)
864 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
865 chain
= net
->xfrm
.policy_byidx
+ idx_hash(net
, id
);
867 hlist_for_each_entry(pol
, chain
, byidx
) {
868 if (pol
->type
== type
&& pol
->index
== id
&&
869 (mark
& pol
->mark
.m
) == pol
->mark
.v
) {
872 *err
= security_xfrm_policy_delete(
875 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
878 __xfrm_policy_unlink(pol
, dir
);
884 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
887 xfrm_policy_kill(ret
);
890 EXPORT_SYMBOL(xfrm_policy_byid
);
892 #ifdef CONFIG_SECURITY_NETWORK_XFRM
894 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
898 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
899 struct xfrm_policy
*pol
;
902 hlist_for_each_entry(pol
,
903 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
904 if (pol
->type
!= type
)
906 err
= security_xfrm_policy_delete(pol
->security
);
908 xfrm_audit_policy_delete(pol
, 0, task_valid
);
912 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
913 hlist_for_each_entry(pol
,
914 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
916 if (pol
->type
!= type
)
918 err
= security_xfrm_policy_delete(
921 xfrm_audit_policy_delete(pol
, 0,
932 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
938 int xfrm_policy_flush(struct net
*net
, u8 type
, bool task_valid
)
940 int dir
, err
= 0, cnt
= 0;
942 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
944 err
= xfrm_policy_flush_secctx_check(net
, type
, task_valid
);
948 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
949 struct xfrm_policy
*pol
;
953 hlist_for_each_entry(pol
,
954 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
955 if (pol
->type
!= type
)
957 __xfrm_policy_unlink(pol
, dir
);
958 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
961 xfrm_audit_policy_delete(pol
, 1, task_valid
);
963 xfrm_policy_kill(pol
);
965 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
969 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
971 hlist_for_each_entry(pol
,
972 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
974 if (pol
->type
!= type
)
976 __xfrm_policy_unlink(pol
, dir
);
977 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
980 xfrm_audit_policy_delete(pol
, 1, task_valid
);
981 xfrm_policy_kill(pol
);
983 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
992 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
995 EXPORT_SYMBOL(xfrm_policy_flush
);
997 int xfrm_policy_walk(struct net
*net
, struct xfrm_policy_walk
*walk
,
998 int (*func
)(struct xfrm_policy
*, int, int, void*),
1001 struct xfrm_policy
*pol
;
1002 struct xfrm_policy_walk_entry
*x
;
1005 if (walk
->type
>= XFRM_POLICY_TYPE_MAX
&&
1006 walk
->type
!= XFRM_POLICY_TYPE_ANY
)
1009 if (list_empty(&walk
->walk
.all
) && walk
->seq
!= 0)
1012 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1013 if (list_empty(&walk
->walk
.all
))
1014 x
= list_first_entry(&net
->xfrm
.policy_all
, struct xfrm_policy_walk_entry
, all
);
1016 x
= list_first_entry(&walk
->walk
.all
,
1017 struct xfrm_policy_walk_entry
, all
);
1019 list_for_each_entry_from(x
, &net
->xfrm
.policy_all
, all
) {
1022 pol
= container_of(x
, struct xfrm_policy
, walk
);
1023 if (walk
->type
!= XFRM_POLICY_TYPE_ANY
&&
1024 walk
->type
!= pol
->type
)
1026 error
= func(pol
, xfrm_policy_id2dir(pol
->index
),
1029 list_move_tail(&walk
->walk
.all
, &x
->all
);
1034 if (walk
->seq
== 0) {
1038 list_del_init(&walk
->walk
.all
);
1040 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1043 EXPORT_SYMBOL(xfrm_policy_walk
);
1045 void xfrm_policy_walk_init(struct xfrm_policy_walk
*walk
, u8 type
)
1047 INIT_LIST_HEAD(&walk
->walk
.all
);
1048 walk
->walk
.dead
= 1;
1052 EXPORT_SYMBOL(xfrm_policy_walk_init
);
1054 void xfrm_policy_walk_done(struct xfrm_policy_walk
*walk
, struct net
*net
)
1056 if (list_empty(&walk
->walk
.all
))
1059 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
); /*FIXME where is net? */
1060 list_del(&walk
->walk
.all
);
1061 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1063 EXPORT_SYMBOL(xfrm_policy_walk_done
);
1066 * Find policy to apply to this flow.
1068 * Returns 0 if policy found, else an -errno.
1070 static int xfrm_policy_match(const struct xfrm_policy
*pol
,
1071 const struct flowi
*fl
,
1072 u8 type
, u16 family
, int dir
)
1074 const struct xfrm_selector
*sel
= &pol
->selector
;
1078 if (pol
->family
!= family
||
1079 (fl
->flowi_mark
& pol
->mark
.m
) != pol
->mark
.v
||
1083 match
= xfrm_selector_match(sel
, fl
, family
);
1085 ret
= security_xfrm_policy_lookup(pol
->security
, fl
->flowi_secid
,
1091 static struct xfrm_policy
*xfrm_policy_lookup_bytype(struct net
*net
, u8 type
,
1092 const struct flowi
*fl
,
1096 struct xfrm_policy
*pol
, *ret
;
1097 const xfrm_address_t
*daddr
, *saddr
;
1098 struct hlist_head
*chain
;
1101 daddr
= xfrm_flowi_daddr(fl
, family
);
1102 saddr
= xfrm_flowi_saddr(fl
, family
);
1103 if (unlikely(!daddr
|| !saddr
))
1106 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1107 chain
= policy_hash_direct(net
, daddr
, saddr
, family
, dir
);
1109 hlist_for_each_entry(pol
, chain
, bydst
) {
1110 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1120 priority
= ret
->priority
;
1124 chain
= &net
->xfrm
.policy_inexact
[dir
];
1125 hlist_for_each_entry(pol
, chain
, bydst
) {
1126 if ((pol
->priority
>= priority
) && ret
)
1129 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1145 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1150 static struct xfrm_policy
*
1151 __xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
)
1153 #ifdef CONFIG_XFRM_SUB_POLICY
1154 struct xfrm_policy
*pol
;
1156 pol
= xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
1160 return xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
1163 static int flow_to_policy_dir(int dir
)
1165 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1166 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1167 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1173 return XFRM_POLICY_IN
;
1175 return XFRM_POLICY_OUT
;
1177 return XFRM_POLICY_FWD
;
1181 static struct flow_cache_object
*
1182 xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
,
1183 u8 dir
, struct flow_cache_object
*old_obj
, void *ctx
)
1185 struct xfrm_policy
*pol
;
1188 xfrm_pol_put(container_of(old_obj
, struct xfrm_policy
, flo
));
1190 pol
= __xfrm_policy_lookup(net
, fl
, family
, flow_to_policy_dir(dir
));
1191 if (IS_ERR_OR_NULL(pol
))
1192 return ERR_CAST(pol
);
1194 /* Resolver returns two references:
1195 * one for cache and one for caller of flow_cache_lookup() */
1201 static inline int policy_to_flow_dir(int dir
)
1203 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1204 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1205 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1209 case XFRM_POLICY_IN
:
1211 case XFRM_POLICY_OUT
:
1212 return FLOW_DIR_OUT
;
1213 case XFRM_POLICY_FWD
:
1214 return FLOW_DIR_FWD
;
1218 static struct xfrm_policy
*xfrm_sk_policy_lookup(const struct sock
*sk
, int dir
,
1219 const struct flowi
*fl
)
1221 struct xfrm_policy
*pol
;
1222 struct net
*net
= sock_net(sk
);
1225 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1226 pol
= rcu_dereference(sk
->sk_policy
[dir
]);
1228 bool match
= xfrm_selector_match(&pol
->selector
, fl
,
1233 if ((sk
->sk_mark
& pol
->mark
.m
) != pol
->mark
.v
) {
1237 err
= security_xfrm_policy_lookup(pol
->security
,
1239 policy_to_flow_dir(dir
));
1242 else if (err
== -ESRCH
)
1250 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1255 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1257 struct net
*net
= xp_net(pol
);
1259 list_add(&pol
->walk
.all
, &net
->xfrm
.policy_all
);
1260 net
->xfrm
.policy_count
[dir
]++;
1264 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1267 struct net
*net
= xp_net(pol
);
1269 if (list_empty(&pol
->walk
.all
))
1272 /* Socket policies are not hashed. */
1273 if (!hlist_unhashed(&pol
->bydst
)) {
1274 hlist_del(&pol
->bydst
);
1275 hlist_del(&pol
->byidx
);
1278 list_del_init(&pol
->walk
.all
);
1279 net
->xfrm
.policy_count
[dir
]--;
1284 static void xfrm_sk_policy_link(struct xfrm_policy
*pol
, int dir
)
1286 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+ dir
);
1289 static void xfrm_sk_policy_unlink(struct xfrm_policy
*pol
, int dir
)
1291 __xfrm_policy_unlink(pol
, XFRM_POLICY_MAX
+ dir
);
1294 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1296 struct net
*net
= xp_net(pol
);
1298 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1299 pol
= __xfrm_policy_unlink(pol
, dir
);
1300 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1302 xfrm_policy_kill(pol
);
1307 EXPORT_SYMBOL(xfrm_policy_delete
);
1309 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1311 struct net
*net
= xp_net(pol
);
1312 struct xfrm_policy
*old_pol
;
1314 #ifdef CONFIG_XFRM_SUB_POLICY
1315 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1319 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1320 old_pol
= rcu_dereference_protected(sk
->sk_policy
[dir
],
1321 lockdep_is_held(&net
->xfrm
.xfrm_policy_lock
));
1323 pol
->curlft
.add_time
= get_seconds();
1324 pol
->index
= xfrm_gen_index(net
, XFRM_POLICY_MAX
+dir
, 0);
1325 xfrm_sk_policy_link(pol
, dir
);
1327 rcu_assign_pointer(sk
->sk_policy
[dir
], pol
);
1330 xfrm_policy_requeue(old_pol
, pol
);
1332 /* Unlinking succeeds always. This is the only function
1333 * allowed to delete or replace socket policy.
1335 xfrm_sk_policy_unlink(old_pol
, dir
);
1337 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1340 xfrm_policy_kill(old_pol
);
1345 static struct xfrm_policy
*clone_policy(const struct xfrm_policy
*old
, int dir
)
1347 struct xfrm_policy
*newp
= xfrm_policy_alloc(xp_net(old
), GFP_ATOMIC
);
1348 struct net
*net
= xp_net(old
);
1351 newp
->selector
= old
->selector
;
1352 if (security_xfrm_policy_clone(old
->security
,
1355 return NULL
; /* ENOMEM */
1357 newp
->lft
= old
->lft
;
1358 newp
->curlft
= old
->curlft
;
1359 newp
->mark
= old
->mark
;
1360 newp
->action
= old
->action
;
1361 newp
->flags
= old
->flags
;
1362 newp
->xfrm_nr
= old
->xfrm_nr
;
1363 newp
->index
= old
->index
;
1364 newp
->type
= old
->type
;
1365 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1366 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1367 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1368 xfrm_sk_policy_link(newp
, dir
);
1369 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1375 int __xfrm_sk_clone_policy(struct sock
*sk
, const struct sock
*osk
)
1377 const struct xfrm_policy
*p
;
1378 struct xfrm_policy
*np
;
1382 for (i
= 0; i
< 2; i
++) {
1383 p
= rcu_dereference(osk
->sk_policy
[i
]);
1385 np
= clone_policy(p
, i
);
1386 if (unlikely(!np
)) {
1390 rcu_assign_pointer(sk
->sk_policy
[i
], np
);
1398 xfrm_get_saddr(struct net
*net
, int oif
, xfrm_address_t
*local
,
1399 xfrm_address_t
*remote
, unsigned short family
)
1402 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1404 if (unlikely(afinfo
== NULL
))
1406 err
= afinfo
->get_saddr(net
, oif
, local
, remote
);
1407 xfrm_policy_put_afinfo(afinfo
);
1411 /* Resolve list of templates for the flow, given policy. */
1414 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, const struct flowi
*fl
,
1415 struct xfrm_state
**xfrm
, unsigned short family
)
1417 struct net
*net
= xp_net(policy
);
1420 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1421 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1424 for (nx
= 0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1425 struct xfrm_state
*x
;
1426 xfrm_address_t
*remote
= daddr
;
1427 xfrm_address_t
*local
= saddr
;
1428 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1430 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1431 tmpl
->mode
== XFRM_MODE_BEET
) {
1432 remote
= &tmpl
->id
.daddr
;
1433 local
= &tmpl
->saddr
;
1434 if (xfrm_addr_any(local
, tmpl
->encap_family
)) {
1435 error
= xfrm_get_saddr(net
, fl
->flowi_oif
,
1437 tmpl
->encap_family
);
1444 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1446 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1453 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1456 } else if (error
== -ESRCH
) {
1460 if (!tmpl
->optional
)
1466 for (nx
--; nx
>= 0; nx
--)
1467 xfrm_state_put(xfrm
[nx
]);
1472 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, const struct flowi
*fl
,
1473 struct xfrm_state
**xfrm
, unsigned short family
)
1475 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1476 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1482 for (i
= 0; i
< npols
; i
++) {
1483 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1488 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1496 /* found states are sorted for outbound processing */
1498 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1503 for (cnx
--; cnx
>= 0; cnx
--)
1504 xfrm_state_put(tpp
[cnx
]);
1509 /* Check that the bundle accepts the flow and its components are
1513 static inline int xfrm_get_tos(const struct flowi
*fl
, int family
)
1515 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1521 tos
= afinfo
->get_tos(fl
);
1523 xfrm_policy_put_afinfo(afinfo
);
1528 static struct flow_cache_object
*xfrm_bundle_flo_get(struct flow_cache_object
*flo
)
1530 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1531 struct dst_entry
*dst
= &xdst
->u
.dst
;
1533 if (xdst
->route
== NULL
) {
1534 /* Dummy bundle - if it has xfrms we were not
1535 * able to build bundle as template resolution failed.
1536 * It means we need to try again resolving. */
1537 if (xdst
->num_xfrms
> 0)
1539 } else if (dst
->flags
& DST_XFRM_QUEUE
) {
1543 if (stale_bundle(dst
))
1551 static int xfrm_bundle_flo_check(struct flow_cache_object
*flo
)
1553 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1554 struct dst_entry
*dst
= &xdst
->u
.dst
;
1558 if (stale_bundle(dst
))
1564 static void xfrm_bundle_flo_delete(struct flow_cache_object
*flo
)
1566 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1567 struct dst_entry
*dst
= &xdst
->u
.dst
;
1572 static const struct flow_cache_ops xfrm_bundle_fc_ops
= {
1573 .get
= xfrm_bundle_flo_get
,
1574 .check
= xfrm_bundle_flo_check
,
1575 .delete = xfrm_bundle_flo_delete
,
1578 static inline struct xfrm_dst
*xfrm_alloc_dst(struct net
*net
, int family
)
1580 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1581 struct dst_ops
*dst_ops
;
1582 struct xfrm_dst
*xdst
;
1585 return ERR_PTR(-EINVAL
);
1589 dst_ops
= &net
->xfrm
.xfrm4_dst_ops
;
1591 #if IS_ENABLED(CONFIG_IPV6)
1593 dst_ops
= &net
->xfrm
.xfrm6_dst_ops
;
1599 xdst
= dst_alloc(dst_ops
, NULL
, 0, DST_OBSOLETE_NONE
, 0);
1602 struct dst_entry
*dst
= &xdst
->u
.dst
;
1604 memset(dst
+ 1, 0, sizeof(*xdst
) - sizeof(*dst
));
1605 xdst
->flo
.ops
= &xfrm_bundle_fc_ops
;
1607 xdst
= ERR_PTR(-ENOBUFS
);
1609 xfrm_policy_put_afinfo(afinfo
);
1614 static inline int xfrm_init_path(struct xfrm_dst
*path
, struct dst_entry
*dst
,
1617 struct xfrm_policy_afinfo
*afinfo
=
1618 xfrm_policy_get_afinfo(dst
->ops
->family
);
1624 err
= afinfo
->init_path(path
, dst
, nfheader_len
);
1626 xfrm_policy_put_afinfo(afinfo
);
1631 static inline int xfrm_fill_dst(struct xfrm_dst
*xdst
, struct net_device
*dev
,
1632 const struct flowi
*fl
)
1634 struct xfrm_policy_afinfo
*afinfo
=
1635 xfrm_policy_get_afinfo(xdst
->u
.dst
.ops
->family
);
1641 err
= afinfo
->fill_dst(xdst
, dev
, fl
);
1643 xfrm_policy_put_afinfo(afinfo
);
1649 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1650 * all the metrics... Shortly, bundle a bundle.
1653 static struct dst_entry
*xfrm_bundle_create(struct xfrm_policy
*policy
,
1654 struct xfrm_state
**xfrm
, int nx
,
1655 const struct flowi
*fl
,
1656 struct dst_entry
*dst
)
1658 struct net
*net
= xp_net(policy
);
1659 unsigned long now
= jiffies
;
1660 struct net_device
*dev
;
1661 struct xfrm_mode
*inner_mode
;
1662 struct dst_entry
*dst_prev
= NULL
;
1663 struct dst_entry
*dst0
= NULL
;
1667 int nfheader_len
= 0;
1668 int trailer_len
= 0;
1670 int family
= policy
->selector
.family
;
1671 xfrm_address_t saddr
, daddr
;
1673 xfrm_flowi_addr_get(fl
, &saddr
, &daddr
, family
);
1675 tos
= xfrm_get_tos(fl
, family
);
1682 for (; i
< nx
; i
++) {
1683 struct xfrm_dst
*xdst
= xfrm_alloc_dst(net
, family
);
1684 struct dst_entry
*dst1
= &xdst
->u
.dst
;
1686 err
= PTR_ERR(xdst
);
1692 if (xfrm
[i
]->sel
.family
== AF_UNSPEC
) {
1693 inner_mode
= xfrm_ip2inner_mode(xfrm
[i
],
1694 xfrm_af2proto(family
));
1696 err
= -EAFNOSUPPORT
;
1701 inner_mode
= xfrm
[i
]->inner_mode
;
1706 dst_prev
->child
= dst_clone(dst1
);
1707 dst1
->flags
|= DST_NOHASH
;
1711 dst_copy_metrics(dst1
, dst
);
1713 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1714 family
= xfrm
[i
]->props
.family
;
1715 dst
= xfrm_dst_lookup(xfrm
[i
], tos
, fl
->flowi_oif
,
1716 &saddr
, &daddr
, family
);
1723 dst1
->xfrm
= xfrm
[i
];
1724 xdst
->xfrm_genid
= xfrm
[i
]->genid
;
1726 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
1727 dst1
->flags
|= DST_HOST
;
1728 dst1
->lastuse
= now
;
1730 dst1
->input
= dst_discard
;
1731 dst1
->output
= inner_mode
->afinfo
->output
;
1733 dst1
->next
= dst_prev
;
1736 header_len
+= xfrm
[i
]->props
.header_len
;
1737 if (xfrm
[i
]->type
->flags
& XFRM_TYPE_NON_FRAGMENT
)
1738 nfheader_len
+= xfrm
[i
]->props
.header_len
;
1739 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
1742 dst_prev
->child
= dst
;
1750 xfrm_init_path((struct xfrm_dst
*)dst0
, dst
, nfheader_len
);
1751 xfrm_init_pmtu(dst_prev
);
1753 for (dst_prev
= dst0
; dst_prev
!= dst
; dst_prev
= dst_prev
->child
) {
1754 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst_prev
;
1756 err
= xfrm_fill_dst(xdst
, dev
, fl
);
1760 dst_prev
->header_len
= header_len
;
1761 dst_prev
->trailer_len
= trailer_len
;
1762 header_len
-= xdst
->u
.dst
.xfrm
->props
.header_len
;
1763 trailer_len
-= xdst
->u
.dst
.xfrm
->props
.trailer_len
;
1771 xfrm_state_put(xfrm
[i
]);
1775 dst0
= ERR_PTR(err
);
1779 #ifdef CONFIG_XFRM_SUB_POLICY
1780 static int xfrm_dst_alloc_copy(void **target
, const void *src
, int size
)
1783 *target
= kmalloc(size
, GFP_ATOMIC
);
1788 memcpy(*target
, src
, size
);
1793 static int xfrm_dst_update_parent(struct dst_entry
*dst
,
1794 const struct xfrm_selector
*sel
)
1796 #ifdef CONFIG_XFRM_SUB_POLICY
1797 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1798 return xfrm_dst_alloc_copy((void **)&(xdst
->partner
),
1805 static int xfrm_dst_update_origin(struct dst_entry
*dst
,
1806 const struct flowi
*fl
)
1808 #ifdef CONFIG_XFRM_SUB_POLICY
1809 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1810 return xfrm_dst_alloc_copy((void **)&(xdst
->origin
), fl
, sizeof(*fl
));
1816 static int xfrm_expand_policies(const struct flowi
*fl
, u16 family
,
1817 struct xfrm_policy
**pols
,
1818 int *num_pols
, int *num_xfrms
)
1822 if (*num_pols
== 0 || !pols
[0]) {
1827 if (IS_ERR(pols
[0]))
1828 return PTR_ERR(pols
[0]);
1830 *num_xfrms
= pols
[0]->xfrm_nr
;
1832 #ifdef CONFIG_XFRM_SUB_POLICY
1833 if (pols
[0] && pols
[0]->action
== XFRM_POLICY_ALLOW
&&
1834 pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1835 pols
[1] = xfrm_policy_lookup_bytype(xp_net(pols
[0]),
1836 XFRM_POLICY_TYPE_MAIN
,
1840 if (IS_ERR(pols
[1])) {
1841 xfrm_pols_put(pols
, *num_pols
);
1842 return PTR_ERR(pols
[1]);
1845 (*num_xfrms
) += pols
[1]->xfrm_nr
;
1849 for (i
= 0; i
< *num_pols
; i
++) {
1850 if (pols
[i
]->action
!= XFRM_POLICY_ALLOW
) {
1860 static struct xfrm_dst
*
1861 xfrm_resolve_and_create_bundle(struct xfrm_policy
**pols
, int num_pols
,
1862 const struct flowi
*fl
, u16 family
,
1863 struct dst_entry
*dst_orig
)
1865 struct net
*net
= xp_net(pols
[0]);
1866 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1867 struct dst_entry
*dst
;
1868 struct xfrm_dst
*xdst
;
1871 /* Try to instantiate a bundle */
1872 err
= xfrm_tmpl_resolve(pols
, num_pols
, fl
, xfrm
, family
);
1874 if (err
!= 0 && err
!= -EAGAIN
)
1875 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1876 return ERR_PTR(err
);
1879 dst
= xfrm_bundle_create(pols
[0], xfrm
, err
, fl
, dst_orig
);
1881 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLEGENERROR
);
1882 return ERR_CAST(dst
);
1885 xdst
= (struct xfrm_dst
*)dst
;
1886 xdst
->num_xfrms
= err
;
1888 err
= xfrm_dst_update_parent(dst
, &pols
[1]->selector
);
1890 err
= xfrm_dst_update_origin(dst
, fl
);
1891 if (unlikely(err
)) {
1893 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLECHECKERROR
);
1894 return ERR_PTR(err
);
1897 xdst
->num_pols
= num_pols
;
1898 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1899 xdst
->policy_genid
= atomic_read(&pols
[0]->genid
);
1904 static void xfrm_policy_queue_process(unsigned long arg
)
1906 struct sk_buff
*skb
;
1908 struct dst_entry
*dst
;
1909 struct xfrm_policy
*pol
= (struct xfrm_policy
*)arg
;
1910 struct net
*net
= xp_net(pol
);
1911 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1913 struct sk_buff_head list
;
1915 spin_lock(&pq
->hold_queue
.lock
);
1916 skb
= skb_peek(&pq
->hold_queue
);
1918 spin_unlock(&pq
->hold_queue
.lock
);
1923 xfrm_decode_session(skb
, &fl
, dst
->ops
->family
);
1924 spin_unlock(&pq
->hold_queue
.lock
);
1926 dst_hold(dst
->path
);
1927 dst
= xfrm_lookup(net
, dst
->path
, &fl
, sk
, 0);
1931 if (dst
->flags
& DST_XFRM_QUEUE
) {
1934 if (pq
->timeout
>= XFRM_QUEUE_TMO_MAX
)
1937 pq
->timeout
= pq
->timeout
<< 1;
1938 if (!mod_timer(&pq
->hold_timer
, jiffies
+ pq
->timeout
))
1945 __skb_queue_head_init(&list
);
1947 spin_lock(&pq
->hold_queue
.lock
);
1949 skb_queue_splice_init(&pq
->hold_queue
, &list
);
1950 spin_unlock(&pq
->hold_queue
.lock
);
1952 while (!skb_queue_empty(&list
)) {
1953 skb
= __skb_dequeue(&list
);
1955 xfrm_decode_session(skb
, &fl
, skb_dst(skb
)->ops
->family
);
1956 dst_hold(skb_dst(skb
)->path
);
1957 dst
= xfrm_lookup(net
, skb_dst(skb
)->path
, &fl
, skb
->sk
, 0);
1965 skb_dst_set(skb
, dst
);
1967 dst_output(net
, skb
->sk
, skb
);
1976 skb_queue_purge(&pq
->hold_queue
);
1980 static int xdst_queue_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
1982 unsigned long sched_next
;
1983 struct dst_entry
*dst
= skb_dst(skb
);
1984 struct xfrm_dst
*xdst
= (struct xfrm_dst
*) dst
;
1985 struct xfrm_policy
*pol
= xdst
->pols
[0];
1986 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1988 if (unlikely(skb_fclone_busy(sk
, skb
))) {
1993 if (pq
->hold_queue
.qlen
> XFRM_MAX_QUEUE_LEN
) {
2000 spin_lock_bh(&pq
->hold_queue
.lock
);
2003 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
2005 sched_next
= jiffies
+ pq
->timeout
;
2007 if (del_timer(&pq
->hold_timer
)) {
2008 if (time_before(pq
->hold_timer
.expires
, sched_next
))
2009 sched_next
= pq
->hold_timer
.expires
;
2013 __skb_queue_tail(&pq
->hold_queue
, skb
);
2014 if (!mod_timer(&pq
->hold_timer
, sched_next
))
2017 spin_unlock_bh(&pq
->hold_queue
.lock
);
2022 static struct xfrm_dst
*xfrm_create_dummy_bundle(struct net
*net
,
2023 struct xfrm_flo
*xflo
,
2024 const struct flowi
*fl
,
2029 struct net_device
*dev
;
2030 struct dst_entry
*dst
;
2031 struct dst_entry
*dst1
;
2032 struct xfrm_dst
*xdst
;
2034 xdst
= xfrm_alloc_dst(net
, family
);
2038 if (!(xflo
->flags
& XFRM_LOOKUP_QUEUE
) ||
2039 net
->xfrm
.sysctl_larval_drop
||
2043 dst
= xflo
->dst_orig
;
2044 dst1
= &xdst
->u
.dst
;
2048 dst_copy_metrics(dst1
, dst
);
2050 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
2051 dst1
->flags
|= DST_HOST
| DST_XFRM_QUEUE
;
2052 dst1
->lastuse
= jiffies
;
2054 dst1
->input
= dst_discard
;
2055 dst1
->output
= xdst_queue_output
;
2061 xfrm_init_path((struct xfrm_dst
*)dst1
, dst
, 0);
2068 err
= xfrm_fill_dst(xdst
, dev
, fl
);
2077 xdst
= ERR_PTR(err
);
2081 static struct flow_cache_object
*
2082 xfrm_bundle_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
,
2083 struct flow_cache_object
*oldflo
, void *ctx
)
2085 struct xfrm_flo
*xflo
= (struct xfrm_flo
*)ctx
;
2086 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2087 struct xfrm_dst
*xdst
, *new_xdst
;
2088 int num_pols
= 0, num_xfrms
= 0, i
, err
, pol_dead
;
2090 /* Check if the policies from old bundle are usable */
2093 xdst
= container_of(oldflo
, struct xfrm_dst
, flo
);
2094 num_pols
= xdst
->num_pols
;
2095 num_xfrms
= xdst
->num_xfrms
;
2097 for (i
= 0; i
< num_pols
; i
++) {
2098 pols
[i
] = xdst
->pols
[i
];
2099 pol_dead
|= pols
[i
]->walk
.dead
;
2102 dst_free(&xdst
->u
.dst
);
2110 /* Resolve policies to use if we couldn't get them from
2111 * previous cache entry */
2114 pols
[0] = __xfrm_policy_lookup(net
, fl
, family
,
2115 flow_to_policy_dir(dir
));
2116 err
= xfrm_expand_policies(fl
, family
, pols
,
2117 &num_pols
, &num_xfrms
);
2123 goto make_dummy_bundle
;
2126 new_xdst
= xfrm_resolve_and_create_bundle(pols
, num_pols
, fl
, family
,
2128 if (IS_ERR(new_xdst
)) {
2129 err
= PTR_ERR(new_xdst
);
2133 goto make_dummy_bundle
;
2134 dst_hold(&xdst
->u
.dst
);
2136 } else if (new_xdst
== NULL
) {
2139 goto make_dummy_bundle
;
2140 xdst
->num_xfrms
= 0;
2141 dst_hold(&xdst
->u
.dst
);
2145 /* Kill the previous bundle */
2147 /* The policies were stolen for newly generated bundle */
2149 dst_free(&xdst
->u
.dst
);
2152 /* Flow cache does not have reference, it dst_free()'s,
2153 * but we do need to return one reference for original caller */
2154 dst_hold(&new_xdst
->u
.dst
);
2155 return &new_xdst
->flo
;
2158 /* We found policies, but there's no bundles to instantiate:
2159 * either because the policy blocks, has no transformations or
2160 * we could not build template (no xfrm_states).*/
2161 xdst
= xfrm_create_dummy_bundle(net
, xflo
, fl
, num_xfrms
, family
);
2163 xfrm_pols_put(pols
, num_pols
);
2164 return ERR_CAST(xdst
);
2166 xdst
->num_pols
= num_pols
;
2167 xdst
->num_xfrms
= num_xfrms
;
2168 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2170 dst_hold(&xdst
->u
.dst
);
2174 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
2177 dst_free(&xdst
->u
.dst
);
2179 xfrm_pols_put(pols
, num_pols
);
2180 return ERR_PTR(err
);
2183 static struct dst_entry
*make_blackhole(struct net
*net
, u16 family
,
2184 struct dst_entry
*dst_orig
)
2186 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2187 struct dst_entry
*ret
;
2190 dst_release(dst_orig
);
2191 return ERR_PTR(-EINVAL
);
2193 ret
= afinfo
->blackhole_route(net
, dst_orig
);
2195 xfrm_policy_put_afinfo(afinfo
);
2200 /* Main function: finds/creates a bundle for given flow.
2202 * At the moment we eat a raw IP route. Mostly to speed up lookups
2203 * on interfaces with disabled IPsec.
2205 struct dst_entry
*xfrm_lookup(struct net
*net
, struct dst_entry
*dst_orig
,
2206 const struct flowi
*fl
,
2207 const struct sock
*sk
, int flags
)
2209 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2210 struct flow_cache_object
*flo
;
2211 struct xfrm_dst
*xdst
;
2212 struct dst_entry
*dst
, *route
;
2213 u16 family
= dst_orig
->ops
->family
;
2214 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
2215 int i
, err
, num_pols
, num_xfrms
= 0, drop_pols
= 0;
2221 sk
= sk_const_to_full_sk(sk
);
2222 if (sk
&& sk
->sk_policy
[XFRM_POLICY_OUT
]) {
2224 pols
[0] = xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
);
2225 err
= xfrm_expand_policies(fl
, family
, pols
,
2226 &num_pols
, &num_xfrms
);
2231 if (num_xfrms
<= 0) {
2232 drop_pols
= num_pols
;
2236 xdst
= xfrm_resolve_and_create_bundle(
2240 xfrm_pols_put(pols
, num_pols
);
2241 err
= PTR_ERR(xdst
);
2243 } else if (xdst
== NULL
) {
2245 drop_pols
= num_pols
;
2249 dst_hold(&xdst
->u
.dst
);
2250 xdst
->u
.dst
.flags
|= DST_NOCACHE
;
2251 route
= xdst
->route
;
2256 struct xfrm_flo xflo
;
2258 xflo
.dst_orig
= dst_orig
;
2261 /* To accelerate a bit... */
2262 if ((dst_orig
->flags
& DST_NOXFRM
) ||
2263 !net
->xfrm
.policy_count
[XFRM_POLICY_OUT
])
2266 flo
= flow_cache_lookup(net
, fl
, family
, dir
,
2267 xfrm_bundle_lookup
, &xflo
);
2274 xdst
= container_of(flo
, struct xfrm_dst
, flo
);
2276 num_pols
= xdst
->num_pols
;
2277 num_xfrms
= xdst
->num_xfrms
;
2278 memcpy(pols
, xdst
->pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2279 route
= xdst
->route
;
2283 if (route
== NULL
&& num_xfrms
> 0) {
2284 /* The only case when xfrm_bundle_lookup() returns a
2285 * bundle with null route, is when the template could
2286 * not be resolved. It means policies are there, but
2287 * bundle could not be created, since we don't yet
2288 * have the xfrm_state's. We need to wait for KM to
2289 * negotiate new SA's or bail out with error.*/
2290 if (net
->xfrm
.sysctl_larval_drop
) {
2291 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2298 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2306 if ((flags
& XFRM_LOOKUP_ICMP
) &&
2307 !(pols
[0]->flags
& XFRM_POLICY_ICMP
)) {
2312 for (i
= 0; i
< num_pols
; i
++)
2313 pols
[i
]->curlft
.use_time
= get_seconds();
2315 if (num_xfrms
< 0) {
2316 /* Prohibit the flow */
2317 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLBLOCK
);
2320 } else if (num_xfrms
> 0) {
2321 /* Flow transformed */
2322 dst_release(dst_orig
);
2324 /* Flow passes untransformed */
2329 xfrm_pols_put(pols
, drop_pols
);
2330 if (dst
&& dst
->xfrm
&&
2331 dst
->xfrm
->props
.mode
== XFRM_MODE_TUNNEL
)
2332 dst
->flags
|= DST_XFRM_TUNNEL
;
2336 if (!(flags
& XFRM_LOOKUP_ICMP
)) {
2344 if (!(flags
& XFRM_LOOKUP_KEEP_DST_REF
))
2345 dst_release(dst_orig
);
2346 xfrm_pols_put(pols
, drop_pols
);
2347 return ERR_PTR(err
);
2349 EXPORT_SYMBOL(xfrm_lookup
);
2351 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2352 * Otherwise we may send out blackholed packets.
2354 struct dst_entry
*xfrm_lookup_route(struct net
*net
, struct dst_entry
*dst_orig
,
2355 const struct flowi
*fl
,
2356 const struct sock
*sk
, int flags
)
2358 struct dst_entry
*dst
= xfrm_lookup(net
, dst_orig
, fl
, sk
,
2359 flags
| XFRM_LOOKUP_QUEUE
|
2360 XFRM_LOOKUP_KEEP_DST_REF
);
2362 if (IS_ERR(dst
) && PTR_ERR(dst
) == -EREMOTE
)
2363 return make_blackhole(net
, dst_orig
->ops
->family
, dst_orig
);
2367 EXPORT_SYMBOL(xfrm_lookup_route
);
2370 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, const struct flowi
*fl
)
2372 struct xfrm_state
*x
;
2374 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
2376 x
= skb
->sp
->xvec
[idx
];
2377 if (!x
->type
->reject
)
2379 return x
->type
->reject(x
, skb
, fl
);
2382 /* When skb is transformed back to its "native" form, we have to
2383 * check policy restrictions. At the moment we make this in maximally
2384 * stupid way. Shame on me. :-) Of course, connected sockets must
2385 * have policy cached at them.
2389 xfrm_state_ok(const struct xfrm_tmpl
*tmpl
, const struct xfrm_state
*x
,
2390 unsigned short family
)
2392 if (xfrm_state_kern(x
))
2393 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
2394 return x
->id
.proto
== tmpl
->id
.proto
&&
2395 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
2396 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
2397 x
->props
.mode
== tmpl
->mode
&&
2398 (tmpl
->allalgs
|| (tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
2399 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
2400 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
2401 xfrm_state_addr_cmp(tmpl
, x
, family
));
2405 * 0 or more than 0 is returned when validation is succeeded (either bypass
2406 * because of optional transport mode, or next index of the mathced secpath
2407 * state with the template.
2408 * -1 is returned when no matching template is found.
2409 * Otherwise "-2 - errored_index" is returned.
2412 xfrm_policy_ok(const struct xfrm_tmpl
*tmpl
, const struct sec_path
*sp
, int start
,
2413 unsigned short family
)
2417 if (tmpl
->optional
) {
2418 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
2422 for (; idx
< sp
->len
; idx
++) {
2423 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
2425 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2434 int __xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
,
2435 unsigned int family
, int reverse
)
2437 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2440 if (unlikely(afinfo
== NULL
))
2441 return -EAFNOSUPPORT
;
2443 afinfo
->decode_session(skb
, fl
, reverse
);
2444 err
= security_xfrm_decode_session(skb
, &fl
->flowi_secid
);
2445 xfrm_policy_put_afinfo(afinfo
);
2448 EXPORT_SYMBOL(__xfrm_decode_session
);
2450 static inline int secpath_has_nontransport(const struct sec_path
*sp
, int k
, int *idxp
)
2452 for (; k
< sp
->len
; k
++) {
2453 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2462 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
2463 unsigned short family
)
2465 struct net
*net
= dev_net(skb
->dev
);
2466 struct xfrm_policy
*pol
;
2467 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2476 reverse
= dir
& ~XFRM_POLICY_MASK
;
2477 dir
&= XFRM_POLICY_MASK
;
2478 fl_dir
= policy_to_flow_dir(dir
);
2480 if (__xfrm_decode_session(skb
, &fl
, family
, reverse
) < 0) {
2481 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
2485 nf_nat_decode_session(skb
, &fl
, family
);
2487 /* First, check used SA against their selectors. */
2491 for (i
= skb
->sp
->len
-1; i
>= 0; i
--) {
2492 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
2493 if (!xfrm_selector_match(&x
->sel
, &fl
, family
)) {
2494 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMISMATCH
);
2501 sk
= sk_to_full_sk(sk
);
2502 if (sk
&& sk
->sk_policy
[dir
]) {
2503 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
);
2505 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2511 struct flow_cache_object
*flo
;
2513 flo
= flow_cache_lookup(net
, &fl
, family
, fl_dir
,
2514 xfrm_policy_lookup
, NULL
);
2515 if (IS_ERR_OR_NULL(flo
))
2516 pol
= ERR_CAST(flo
);
2518 pol
= container_of(flo
, struct xfrm_policy
, flo
);
2522 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2527 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
2528 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2529 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINNOPOLS
);
2535 pol
->curlft
.use_time
= get_seconds();
2539 #ifdef CONFIG_XFRM_SUB_POLICY
2540 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
2541 pols
[1] = xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
,
2545 if (IS_ERR(pols
[1])) {
2546 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2549 pols
[1]->curlft
.use_time
= get_seconds();
2555 if (pol
->action
== XFRM_POLICY_ALLOW
) {
2556 struct sec_path
*sp
;
2557 static struct sec_path dummy
;
2558 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
2559 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
2560 struct xfrm_tmpl
**tpp
= tp
;
2564 if ((sp
= skb
->sp
) == NULL
)
2567 for (pi
= 0; pi
< npols
; pi
++) {
2568 if (pols
[pi
] != pol
&&
2569 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
) {
2570 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2573 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
2574 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
2577 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
2578 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
2582 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
, net
);
2586 /* For each tunnel xfrm, find the first matching tmpl.
2587 * For each tmpl before that, find corresponding xfrm.
2588 * Order is _important_. Later we will implement
2589 * some barriers, but at the moment barriers
2590 * are implied between each two transformations.
2592 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
2593 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
2596 /* "-2 - errored_index" returned */
2598 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2603 if (secpath_has_nontransport(sp
, k
, &xerr_idx
)) {
2604 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2608 xfrm_pols_put(pols
, npols
);
2611 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2614 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2616 xfrm_pols_put(pols
, npols
);
2619 EXPORT_SYMBOL(__xfrm_policy_check
);
2621 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
2623 struct net
*net
= dev_net(skb
->dev
);
2625 struct dst_entry
*dst
;
2628 if (xfrm_decode_session(skb
, &fl
, family
) < 0) {
2629 XFRM_INC_STATS(net
, LINUX_MIB_XFRMFWDHDRERROR
);
2635 dst
= xfrm_lookup(net
, skb_dst(skb
), &fl
, NULL
, XFRM_LOOKUP_QUEUE
);
2640 skb_dst_set(skb
, dst
);
2643 EXPORT_SYMBOL(__xfrm_route_forward
);
2645 /* Optimize later using cookies and generation ids. */
2647 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
2649 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2650 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2651 * get validated by dst_ops->check on every use. We do this
2652 * because when a normal route referenced by an XFRM dst is
2653 * obsoleted we do not go looking around for all parent
2654 * referencing XFRM dsts so that we can invalidate them. It
2655 * is just too much work. Instead we make the checks here on
2656 * every use. For example:
2658 * XFRM dst A --> IPv4 dst X
2660 * X is the "xdst->route" of A (X is also the "dst->path" of A
2661 * in this example). If X is marked obsolete, "A" will not
2662 * notice. That's what we are validating here via the
2663 * stale_bundle() check.
2665 * When a policy's bundle is pruned, we dst_free() the XFRM
2666 * dst which causes it's ->obsolete field to be set to
2667 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2668 * this, we want to force a new route lookup.
2670 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
2676 static int stale_bundle(struct dst_entry
*dst
)
2678 return !xfrm_bundle_ok((struct xfrm_dst
*)dst
);
2681 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
2683 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
2684 dst
->dev
= dev_net(dev
)->loopback_dev
;
2689 EXPORT_SYMBOL(xfrm_dst_ifdown
);
2691 static void xfrm_link_failure(struct sk_buff
*skb
)
2693 /* Impossible. Such dst must be popped before reaches point of failure. */
2696 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
2699 if (dst
->obsolete
) {
2707 void xfrm_garbage_collect(struct net
*net
)
2709 flow_cache_flush(net
);
2711 EXPORT_SYMBOL(xfrm_garbage_collect
);
2713 static void xfrm_garbage_collect_deferred(struct net
*net
)
2715 flow_cache_flush_deferred(net
);
2718 static void xfrm_init_pmtu(struct dst_entry
*dst
)
2721 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2722 u32 pmtu
, route_mtu_cached
;
2724 pmtu
= dst_mtu(dst
->child
);
2725 xdst
->child_mtu_cached
= pmtu
;
2727 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2729 route_mtu_cached
= dst_mtu(xdst
->route
);
2730 xdst
->route_mtu_cached
= route_mtu_cached
;
2732 if (pmtu
> route_mtu_cached
)
2733 pmtu
= route_mtu_cached
;
2735 dst_metric_set(dst
, RTAX_MTU
, pmtu
);
2736 } while ((dst
= dst
->next
));
2739 /* Check that the bundle accepts the flow and its components are
2743 static int xfrm_bundle_ok(struct xfrm_dst
*first
)
2745 struct dst_entry
*dst
= &first
->u
.dst
;
2746 struct xfrm_dst
*last
;
2749 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2750 (dst
->dev
&& !netif_running(dst
->dev
)))
2753 if (dst
->flags
& DST_XFRM_QUEUE
)
2759 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2761 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2763 if (xdst
->xfrm_genid
!= dst
->xfrm
->genid
)
2765 if (xdst
->num_pols
> 0 &&
2766 xdst
->policy_genid
!= atomic_read(&xdst
->pols
[0]->genid
))
2769 mtu
= dst_mtu(dst
->child
);
2770 if (xdst
->child_mtu_cached
!= mtu
) {
2772 xdst
->child_mtu_cached
= mtu
;
2775 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2777 mtu
= dst_mtu(xdst
->route
);
2778 if (xdst
->route_mtu_cached
!= mtu
) {
2780 xdst
->route_mtu_cached
= mtu
;
2784 } while (dst
->xfrm
);
2789 mtu
= last
->child_mtu_cached
;
2793 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2794 if (mtu
> last
->route_mtu_cached
)
2795 mtu
= last
->route_mtu_cached
;
2796 dst_metric_set(dst
, RTAX_MTU
, mtu
);
2801 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2802 last
->child_mtu_cached
= mtu
;
2808 static unsigned int xfrm_default_advmss(const struct dst_entry
*dst
)
2810 return dst_metric_advmss(dst
->path
);
2813 static unsigned int xfrm_mtu(const struct dst_entry
*dst
)
2815 unsigned int mtu
= dst_metric_raw(dst
, RTAX_MTU
);
2817 return mtu
? : dst_mtu(dst
->path
);
2820 static struct neighbour
*xfrm_neigh_lookup(const struct dst_entry
*dst
,
2821 struct sk_buff
*skb
,
2824 return dst
->path
->ops
->neigh_lookup(dst
, skb
, daddr
);
2827 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2830 if (unlikely(afinfo
== NULL
))
2832 if (unlikely(afinfo
->family
>= NPROTO
))
2833 return -EAFNOSUPPORT
;
2834 spin_lock(&xfrm_policy_afinfo_lock
);
2835 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
2838 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2839 if (likely(dst_ops
->kmem_cachep
== NULL
))
2840 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2841 if (likely(dst_ops
->check
== NULL
))
2842 dst_ops
->check
= xfrm_dst_check
;
2843 if (likely(dst_ops
->default_advmss
== NULL
))
2844 dst_ops
->default_advmss
= xfrm_default_advmss
;
2845 if (likely(dst_ops
->mtu
== NULL
))
2846 dst_ops
->mtu
= xfrm_mtu
;
2847 if (likely(dst_ops
->negative_advice
== NULL
))
2848 dst_ops
->negative_advice
= xfrm_negative_advice
;
2849 if (likely(dst_ops
->link_failure
== NULL
))
2850 dst_ops
->link_failure
= xfrm_link_failure
;
2851 if (likely(dst_ops
->neigh_lookup
== NULL
))
2852 dst_ops
->neigh_lookup
= xfrm_neigh_lookup
;
2853 if (likely(afinfo
->garbage_collect
== NULL
))
2854 afinfo
->garbage_collect
= xfrm_garbage_collect_deferred
;
2855 rcu_assign_pointer(xfrm_policy_afinfo
[afinfo
->family
], afinfo
);
2857 spin_unlock(&xfrm_policy_afinfo_lock
);
2861 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2863 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2866 if (unlikely(afinfo
== NULL
))
2868 if (unlikely(afinfo
->family
>= NPROTO
))
2869 return -EAFNOSUPPORT
;
2870 spin_lock(&xfrm_policy_afinfo_lock
);
2871 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
2872 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
2875 RCU_INIT_POINTER(xfrm_policy_afinfo
[afinfo
->family
],
2878 spin_unlock(&xfrm_policy_afinfo_lock
);
2880 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2884 dst_ops
->kmem_cachep
= NULL
;
2885 dst_ops
->check
= NULL
;
2886 dst_ops
->negative_advice
= NULL
;
2887 dst_ops
->link_failure
= NULL
;
2888 afinfo
->garbage_collect
= NULL
;
2892 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2894 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
2896 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2900 xfrm_garbage_collect(dev_net(dev
));
2905 static struct notifier_block xfrm_dev_notifier
= {
2906 .notifier_call
= xfrm_dev_event
,
2909 #ifdef CONFIG_XFRM_STATISTICS
2910 static int __net_init
xfrm_statistics_init(struct net
*net
)
2913 net
->mib
.xfrm_statistics
= alloc_percpu(struct linux_xfrm_mib
);
2914 if (!net
->mib
.xfrm_statistics
)
2916 rv
= xfrm_proc_init(net
);
2918 free_percpu(net
->mib
.xfrm_statistics
);
2922 static void xfrm_statistics_fini(struct net
*net
)
2924 xfrm_proc_fini(net
);
2925 free_percpu(net
->mib
.xfrm_statistics
);
2928 static int __net_init
xfrm_statistics_init(struct net
*net
)
2933 static void xfrm_statistics_fini(struct net
*net
)
2938 static int __net_init
xfrm_policy_init(struct net
*net
)
2940 unsigned int hmask
, sz
;
2943 if (net_eq(net
, &init_net
))
2944 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2945 sizeof(struct xfrm_dst
),
2946 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2950 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2952 net
->xfrm
.policy_byidx
= xfrm_hash_alloc(sz
);
2953 if (!net
->xfrm
.policy_byidx
)
2955 net
->xfrm
.policy_idx_hmask
= hmask
;
2957 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
2958 struct xfrm_policy_hash
*htab
;
2960 net
->xfrm
.policy_count
[dir
] = 0;
2961 net
->xfrm
.policy_count
[XFRM_POLICY_MAX
+ dir
] = 0;
2962 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
2964 htab
= &net
->xfrm
.policy_bydst
[dir
];
2965 htab
->table
= xfrm_hash_alloc(sz
);
2968 htab
->hmask
= hmask
;
2974 net
->xfrm
.policy_hthresh
.lbits4
= 32;
2975 net
->xfrm
.policy_hthresh
.rbits4
= 32;
2976 net
->xfrm
.policy_hthresh
.lbits6
= 128;
2977 net
->xfrm
.policy_hthresh
.rbits6
= 128;
2979 seqlock_init(&net
->xfrm
.policy_hthresh
.lock
);
2981 INIT_LIST_HEAD(&net
->xfrm
.policy_all
);
2982 INIT_WORK(&net
->xfrm
.policy_hash_work
, xfrm_hash_resize
);
2983 INIT_WORK(&net
->xfrm
.policy_hthresh
.work
, xfrm_hash_rebuild
);
2984 if (net_eq(net
, &init_net
))
2985 register_netdevice_notifier(&xfrm_dev_notifier
);
2989 for (dir
--; dir
>= 0; dir
--) {
2990 struct xfrm_policy_hash
*htab
;
2992 htab
= &net
->xfrm
.policy_bydst
[dir
];
2993 xfrm_hash_free(htab
->table
, sz
);
2995 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
3000 static void xfrm_policy_fini(struct net
*net
)
3005 flush_work(&net
->xfrm
.policy_hash_work
);
3006 #ifdef CONFIG_XFRM_SUB_POLICY
3007 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_SUB
, false);
3009 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, false);
3011 WARN_ON(!list_empty(&net
->xfrm
.policy_all
));
3013 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
3014 struct xfrm_policy_hash
*htab
;
3016 WARN_ON(!hlist_empty(&net
->xfrm
.policy_inexact
[dir
]));
3018 htab
= &net
->xfrm
.policy_bydst
[dir
];
3019 sz
= (htab
->hmask
+ 1) * sizeof(struct hlist_head
);
3020 WARN_ON(!hlist_empty(htab
->table
));
3021 xfrm_hash_free(htab
->table
, sz
);
3024 sz
= (net
->xfrm
.policy_idx_hmask
+ 1) * sizeof(struct hlist_head
);
3025 WARN_ON(!hlist_empty(net
->xfrm
.policy_byidx
));
3026 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
3029 static int __net_init
xfrm_net_init(struct net
*net
)
3033 rv
= xfrm_statistics_init(net
);
3035 goto out_statistics
;
3036 rv
= xfrm_state_init(net
);
3039 rv
= xfrm_policy_init(net
);
3042 rv
= xfrm_sysctl_init(net
);
3045 rv
= flow_cache_init(net
);
3049 /* Initialize the per-net locks here */
3050 spin_lock_init(&net
->xfrm
.xfrm_state_lock
);
3051 rwlock_init(&net
->xfrm
.xfrm_policy_lock
);
3052 mutex_init(&net
->xfrm
.xfrm_cfg_mutex
);
3057 xfrm_sysctl_fini(net
);
3059 xfrm_policy_fini(net
);
3061 xfrm_state_fini(net
);
3063 xfrm_statistics_fini(net
);
3068 static void __net_exit
xfrm_net_exit(struct net
*net
)
3070 flow_cache_fini(net
);
3071 xfrm_sysctl_fini(net
);
3072 xfrm_policy_fini(net
);
3073 xfrm_state_fini(net
);
3074 xfrm_statistics_fini(net
);
3077 static struct pernet_operations __net_initdata xfrm_net_ops
= {
3078 .init
= xfrm_net_init
,
3079 .exit
= xfrm_net_exit
,
3082 void __init
xfrm_init(void)
3084 register_pernet_subsys(&xfrm_net_ops
);
3088 #ifdef CONFIG_AUDITSYSCALL
3089 static void xfrm_audit_common_policyinfo(struct xfrm_policy
*xp
,
3090 struct audit_buffer
*audit_buf
)
3092 struct xfrm_sec_ctx
*ctx
= xp
->security
;
3093 struct xfrm_selector
*sel
= &xp
->selector
;
3096 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
3097 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
3099 switch (sel
->family
) {
3101 audit_log_format(audit_buf
, " src=%pI4", &sel
->saddr
.a4
);
3102 if (sel
->prefixlen_s
!= 32)
3103 audit_log_format(audit_buf
, " src_prefixlen=%d",
3105 audit_log_format(audit_buf
, " dst=%pI4", &sel
->daddr
.a4
);
3106 if (sel
->prefixlen_d
!= 32)
3107 audit_log_format(audit_buf
, " dst_prefixlen=%d",
3111 audit_log_format(audit_buf
, " src=%pI6", sel
->saddr
.a6
);
3112 if (sel
->prefixlen_s
!= 128)
3113 audit_log_format(audit_buf
, " src_prefixlen=%d",
3115 audit_log_format(audit_buf
, " dst=%pI6", sel
->daddr
.a6
);
3116 if (sel
->prefixlen_d
!= 128)
3117 audit_log_format(audit_buf
, " dst_prefixlen=%d",
3123 void xfrm_audit_policy_add(struct xfrm_policy
*xp
, int result
, bool task_valid
)
3125 struct audit_buffer
*audit_buf
;
3127 audit_buf
= xfrm_audit_start("SPD-add");
3128 if (audit_buf
== NULL
)
3130 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
3131 audit_log_format(audit_buf
, " res=%u", result
);
3132 xfrm_audit_common_policyinfo(xp
, audit_buf
);
3133 audit_log_end(audit_buf
);
3135 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add
);
3137 void xfrm_audit_policy_delete(struct xfrm_policy
*xp
, int result
,
3140 struct audit_buffer
*audit_buf
;
3142 audit_buf
= xfrm_audit_start("SPD-delete");
3143 if (audit_buf
== NULL
)
3145 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
3146 audit_log_format(audit_buf
, " res=%u", result
);
3147 xfrm_audit_common_policyinfo(xp
, audit_buf
);
3148 audit_log_end(audit_buf
);
3150 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete
);
3153 #ifdef CONFIG_XFRM_MIGRATE
3154 static bool xfrm_migrate_selector_match(const struct xfrm_selector
*sel_cmp
,
3155 const struct xfrm_selector
*sel_tgt
)
3157 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
3158 if (sel_tgt
->family
== sel_cmp
->family
&&
3159 xfrm_addr_equal(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
3161 xfrm_addr_equal(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
3163 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
3164 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
3168 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
3175 static struct xfrm_policy
*xfrm_migrate_policy_find(const struct xfrm_selector
*sel
,
3176 u8 dir
, u8 type
, struct net
*net
)
3178 struct xfrm_policy
*pol
, *ret
= NULL
;
3179 struct hlist_head
*chain
;
3182 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
); /*FIXME*/
3183 chain
= policy_hash_direct(net
, &sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
3184 hlist_for_each_entry(pol
, chain
, bydst
) {
3185 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3186 pol
->type
== type
) {
3188 priority
= ret
->priority
;
3192 chain
= &net
->xfrm
.policy_inexact
[dir
];
3193 hlist_for_each_entry(pol
, chain
, bydst
) {
3194 if ((pol
->priority
>= priority
) && ret
)
3197 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3198 pol
->type
== type
) {
3206 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
3211 static int migrate_tmpl_match(const struct xfrm_migrate
*m
, const struct xfrm_tmpl
*t
)
3215 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
3216 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
3218 case XFRM_MODE_TUNNEL
:
3219 case XFRM_MODE_BEET
:
3220 if (xfrm_addr_equal(&t
->id
.daddr
, &m
->old_daddr
,
3222 xfrm_addr_equal(&t
->saddr
, &m
->old_saddr
,
3227 case XFRM_MODE_TRANSPORT
:
3228 /* in case of transport mode, template does not store
3229 any IP addresses, hence we just compare mode and
3240 /* update endpoint address(es) of template(s) */
3241 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
3242 struct xfrm_migrate
*m
, int num_migrate
)
3244 struct xfrm_migrate
*mp
;
3247 write_lock_bh(&pol
->lock
);
3248 if (unlikely(pol
->walk
.dead
)) {
3249 /* target policy has been deleted */
3250 write_unlock_bh(&pol
->lock
);
3254 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
3255 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
3256 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
3259 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
&&
3260 pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_BEET
)
3262 /* update endpoints */
3263 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
3264 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
3265 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
3266 sizeof(pol
->xfrm_vec
[i
].saddr
));
3267 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
3269 atomic_inc(&pol
->genid
);
3273 write_unlock_bh(&pol
->lock
);
3281 static int xfrm_migrate_check(const struct xfrm_migrate
*m
, int num_migrate
)
3285 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
3288 for (i
= 0; i
< num_migrate
; i
++) {
3289 if (xfrm_addr_equal(&m
[i
].old_daddr
, &m
[i
].new_daddr
,
3291 xfrm_addr_equal(&m
[i
].old_saddr
, &m
[i
].new_saddr
,
3294 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
3295 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
3298 /* check if there is any duplicated entry */
3299 for (j
= i
+ 1; j
< num_migrate
; j
++) {
3300 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
3301 sizeof(m
[i
].old_daddr
)) &&
3302 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
3303 sizeof(m
[i
].old_saddr
)) &&
3304 m
[i
].proto
== m
[j
].proto
&&
3305 m
[i
].mode
== m
[j
].mode
&&
3306 m
[i
].reqid
== m
[j
].reqid
&&
3307 m
[i
].old_family
== m
[j
].old_family
)
3315 int xfrm_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3316 struct xfrm_migrate
*m
, int num_migrate
,
3317 struct xfrm_kmaddress
*k
, struct net
*net
)
3319 int i
, err
, nx_cur
= 0, nx_new
= 0;
3320 struct xfrm_policy
*pol
= NULL
;
3321 struct xfrm_state
*x
, *xc
;
3322 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
3323 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
3324 struct xfrm_migrate
*mp
;
3326 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
3329 /* Stage 1 - find policy */
3330 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
, net
)) == NULL
) {
3335 /* Stage 2 - find and update state(s) */
3336 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
3337 if ((x
= xfrm_migrate_state_find(mp
, net
))) {
3340 if ((xc
= xfrm_state_migrate(x
, mp
))) {
3350 /* Stage 3 - update policy */
3351 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
3354 /* Stage 4 - delete old state(s) */
3356 xfrm_states_put(x_cur
, nx_cur
);
3357 xfrm_states_delete(x_cur
, nx_cur
);
3360 /* Stage 5 - announce */
3361 km_migrate(sel
, dir
, type
, m
, num_migrate
, k
);
3373 xfrm_states_put(x_cur
, nx_cur
);
3375 xfrm_states_delete(x_new
, nx_new
);
3379 EXPORT_SYMBOL(xfrm_migrate
);