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
, int tos
,
119 const xfrm_address_t
*saddr
,
120 const xfrm_address_t
*daddr
,
123 struct xfrm_policy_afinfo
*afinfo
;
124 struct dst_entry
*dst
;
126 afinfo
= xfrm_policy_get_afinfo(family
);
127 if (unlikely(afinfo
== NULL
))
128 return ERR_PTR(-EAFNOSUPPORT
);
130 dst
= afinfo
->dst_lookup(net
, tos
, saddr
, daddr
);
132 xfrm_policy_put_afinfo(afinfo
);
137 static inline struct dst_entry
*xfrm_dst_lookup(struct xfrm_state
*x
, int tos
,
138 xfrm_address_t
*prev_saddr
,
139 xfrm_address_t
*prev_daddr
,
142 struct net
*net
= xs_net(x
);
143 xfrm_address_t
*saddr
= &x
->props
.saddr
;
144 xfrm_address_t
*daddr
= &x
->id
.daddr
;
145 struct dst_entry
*dst
;
147 if (x
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
) {
151 if (x
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
) {
156 dst
= __xfrm_dst_lookup(net
, tos
, saddr
, daddr
, family
);
159 if (prev_saddr
!= saddr
)
160 memcpy(prev_saddr
, saddr
, sizeof(*prev_saddr
));
161 if (prev_daddr
!= daddr
)
162 memcpy(prev_daddr
, daddr
, sizeof(*prev_daddr
));
168 static inline unsigned long make_jiffies(long secs
)
170 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
171 return MAX_SCHEDULE_TIMEOUT
-1;
176 static void xfrm_policy_timer(unsigned long data
)
178 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
179 unsigned long now
= get_seconds();
180 long next
= LONG_MAX
;
184 read_lock(&xp
->lock
);
186 if (unlikely(xp
->walk
.dead
))
189 dir
= xfrm_policy_id2dir(xp
->index
);
191 if (xp
->lft
.hard_add_expires_seconds
) {
192 long tmo
= xp
->lft
.hard_add_expires_seconds
+
193 xp
->curlft
.add_time
- now
;
199 if (xp
->lft
.hard_use_expires_seconds
) {
200 long tmo
= xp
->lft
.hard_use_expires_seconds
+
201 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
207 if (xp
->lft
.soft_add_expires_seconds
) {
208 long tmo
= xp
->lft
.soft_add_expires_seconds
+
209 xp
->curlft
.add_time
- now
;
212 tmo
= XFRM_KM_TIMEOUT
;
217 if (xp
->lft
.soft_use_expires_seconds
) {
218 long tmo
= xp
->lft
.soft_use_expires_seconds
+
219 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
222 tmo
= XFRM_KM_TIMEOUT
;
229 km_policy_expired(xp
, dir
, 0, 0);
230 if (next
!= LONG_MAX
&&
231 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
235 read_unlock(&xp
->lock
);
240 read_unlock(&xp
->lock
);
241 if (!xfrm_policy_delete(xp
, dir
))
242 km_policy_expired(xp
, dir
, 1, 0);
246 static struct flow_cache_object
*xfrm_policy_flo_get(struct flow_cache_object
*flo
)
248 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
250 if (unlikely(pol
->walk
.dead
))
258 static int xfrm_policy_flo_check(struct flow_cache_object
*flo
)
260 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
262 return !pol
->walk
.dead
;
265 static void xfrm_policy_flo_delete(struct flow_cache_object
*flo
)
267 xfrm_pol_put(container_of(flo
, struct xfrm_policy
, flo
));
270 static const struct flow_cache_ops xfrm_policy_fc_ops
= {
271 .get
= xfrm_policy_flo_get
,
272 .check
= xfrm_policy_flo_check
,
273 .delete = xfrm_policy_flo_delete
,
276 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
280 struct xfrm_policy
*xfrm_policy_alloc(struct net
*net
, gfp_t gfp
)
282 struct xfrm_policy
*policy
;
284 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
287 write_pnet(&policy
->xp_net
, net
);
288 INIT_LIST_HEAD(&policy
->walk
.all
);
289 INIT_HLIST_NODE(&policy
->bydst
);
290 INIT_HLIST_NODE(&policy
->byidx
);
291 rwlock_init(&policy
->lock
);
292 atomic_set(&policy
->refcnt
, 1);
293 skb_queue_head_init(&policy
->polq
.hold_queue
);
294 setup_timer(&policy
->timer
, xfrm_policy_timer
,
295 (unsigned long)policy
);
296 setup_timer(&policy
->polq
.hold_timer
, xfrm_policy_queue_process
,
297 (unsigned long)policy
);
298 policy
->flo
.ops
= &xfrm_policy_fc_ops
;
302 EXPORT_SYMBOL(xfrm_policy_alloc
);
304 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
306 void xfrm_policy_destroy(struct xfrm_policy
*policy
)
308 BUG_ON(!policy
->walk
.dead
);
310 if (del_timer(&policy
->timer
) || del_timer(&policy
->polq
.hold_timer
))
313 security_xfrm_policy_free(policy
->security
);
316 EXPORT_SYMBOL(xfrm_policy_destroy
);
318 static void xfrm_queue_purge(struct sk_buff_head
*list
)
322 while ((skb
= skb_dequeue(list
)) != NULL
)
326 /* Rule must be locked. Release descentant resources, announce
327 * entry dead. The rule must be unlinked from lists to the moment.
330 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
332 policy
->walk
.dead
= 1;
334 atomic_inc(&policy
->genid
);
336 if (del_timer(&policy
->polq
.hold_timer
))
337 xfrm_pol_put(policy
);
338 xfrm_queue_purge(&policy
->polq
.hold_queue
);
340 if (del_timer(&policy
->timer
))
341 xfrm_pol_put(policy
);
343 xfrm_pol_put(policy
);
346 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
348 static inline unsigned int idx_hash(struct net
*net
, u32 index
)
350 return __idx_hash(index
, net
->xfrm
.policy_idx_hmask
);
353 /* calculate policy hash thresholds */
354 static void __get_hash_thresh(struct net
*net
,
355 unsigned short family
, int dir
,
356 u8
*dbits
, u8
*sbits
)
360 *dbits
= net
->xfrm
.policy_bydst
[dir
].dbits4
;
361 *sbits
= net
->xfrm
.policy_bydst
[dir
].sbits4
;
365 *dbits
= net
->xfrm
.policy_bydst
[dir
].dbits6
;
366 *sbits
= net
->xfrm
.policy_bydst
[dir
].sbits6
;
375 static struct hlist_head
*policy_hash_bysel(struct net
*net
,
376 const struct xfrm_selector
*sel
,
377 unsigned short family
, int dir
)
379 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
384 __get_hash_thresh(net
, family
, dir
, &dbits
, &sbits
);
385 hash
= __sel_hash(sel
, family
, hmask
, dbits
, sbits
);
387 return (hash
== hmask
+ 1 ?
388 &net
->xfrm
.policy_inexact
[dir
] :
389 net
->xfrm
.policy_bydst
[dir
].table
+ hash
);
392 static struct hlist_head
*policy_hash_direct(struct net
*net
,
393 const xfrm_address_t
*daddr
,
394 const xfrm_address_t
*saddr
,
395 unsigned short family
, int dir
)
397 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
402 __get_hash_thresh(net
, family
, dir
, &dbits
, &sbits
);
403 hash
= __addr_hash(daddr
, saddr
, family
, hmask
, dbits
, sbits
);
405 return net
->xfrm
.policy_bydst
[dir
].table
+ hash
;
408 static void xfrm_dst_hash_transfer(struct net
*net
,
409 struct hlist_head
*list
,
410 struct hlist_head
*ndsttable
,
411 unsigned int nhashmask
,
414 struct hlist_node
*tmp
, *entry0
= NULL
;
415 struct xfrm_policy
*pol
;
421 hlist_for_each_entry_safe(pol
, tmp
, list
, bydst
) {
424 __get_hash_thresh(net
, pol
->family
, dir
, &dbits
, &sbits
);
425 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
426 pol
->family
, nhashmask
, dbits
, sbits
);
428 hlist_del(&pol
->bydst
);
429 hlist_add_head(&pol
->bydst
, ndsttable
+h
);
434 hlist_del(&pol
->bydst
);
435 hlist_add_behind(&pol
->bydst
, entry0
);
437 entry0
= &pol
->bydst
;
439 if (!hlist_empty(list
)) {
445 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
446 struct hlist_head
*nidxtable
,
447 unsigned int nhashmask
)
449 struct hlist_node
*tmp
;
450 struct xfrm_policy
*pol
;
452 hlist_for_each_entry_safe(pol
, tmp
, list
, byidx
) {
455 h
= __idx_hash(pol
->index
, nhashmask
);
456 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
460 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
462 return ((old_hmask
+ 1) << 1) - 1;
465 static void xfrm_bydst_resize(struct net
*net
, int dir
)
467 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
468 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
469 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
470 struct hlist_head
*odst
= net
->xfrm
.policy_bydst
[dir
].table
;
471 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
477 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
479 for (i
= hmask
; i
>= 0; i
--)
480 xfrm_dst_hash_transfer(net
, odst
+ i
, ndst
, nhashmask
, dir
);
482 net
->xfrm
.policy_bydst
[dir
].table
= ndst
;
483 net
->xfrm
.policy_bydst
[dir
].hmask
= nhashmask
;
485 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
487 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
490 static void xfrm_byidx_resize(struct net
*net
, int total
)
492 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
493 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
494 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
495 struct hlist_head
*oidx
= net
->xfrm
.policy_byidx
;
496 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
502 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
504 for (i
= hmask
; i
>= 0; i
--)
505 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
507 net
->xfrm
.policy_byidx
= nidx
;
508 net
->xfrm
.policy_idx_hmask
= nhashmask
;
510 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
512 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
515 static inline int xfrm_bydst_should_resize(struct net
*net
, int dir
, int *total
)
517 unsigned int cnt
= net
->xfrm
.policy_count
[dir
];
518 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
523 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
530 static inline int xfrm_byidx_should_resize(struct net
*net
, int total
)
532 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
534 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
541 void xfrm_spd_getinfo(struct net
*net
, struct xfrmk_spdinfo
*si
)
543 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
544 si
->incnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
];
545 si
->outcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
];
546 si
->fwdcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
];
547 si
->inscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
548 si
->outscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
549 si
->fwdscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
550 si
->spdhcnt
= net
->xfrm
.policy_idx_hmask
;
551 si
->spdhmcnt
= xfrm_policy_hashmax
;
552 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
554 EXPORT_SYMBOL(xfrm_spd_getinfo
);
556 static DEFINE_MUTEX(hash_resize_mutex
);
557 static void xfrm_hash_resize(struct work_struct
*work
)
559 struct net
*net
= container_of(work
, struct net
, xfrm
.policy_hash_work
);
562 mutex_lock(&hash_resize_mutex
);
565 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
566 if (xfrm_bydst_should_resize(net
, dir
, &total
))
567 xfrm_bydst_resize(net
, dir
);
569 if (xfrm_byidx_should_resize(net
, total
))
570 xfrm_byidx_resize(net
, total
);
572 mutex_unlock(&hash_resize_mutex
);
575 static void xfrm_hash_rebuild(struct work_struct
*work
)
577 struct net
*net
= container_of(work
, struct net
,
578 xfrm
.policy_hthresh
.work
);
580 struct xfrm_policy
*pol
;
581 struct xfrm_policy
*policy
;
582 struct hlist_head
*chain
;
583 struct hlist_head
*odst
;
584 struct hlist_node
*newpos
;
588 u8 lbits4
, rbits4
, lbits6
, rbits6
;
590 mutex_lock(&hash_resize_mutex
);
592 /* read selector prefixlen thresholds */
594 seq
= read_seqbegin(&net
->xfrm
.policy_hthresh
.lock
);
596 lbits4
= net
->xfrm
.policy_hthresh
.lbits4
;
597 rbits4
= net
->xfrm
.policy_hthresh
.rbits4
;
598 lbits6
= net
->xfrm
.policy_hthresh
.lbits6
;
599 rbits6
= net
->xfrm
.policy_hthresh
.rbits6
;
600 } while (read_seqretry(&net
->xfrm
.policy_hthresh
.lock
, seq
));
602 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
604 /* reset the bydst and inexact table in all directions */
605 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
606 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
607 hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
608 odst
= net
->xfrm
.policy_bydst
[dir
].table
;
609 for (i
= hmask
; i
>= 0; i
--)
610 INIT_HLIST_HEAD(odst
+ i
);
611 if ((dir
& XFRM_POLICY_MASK
) == XFRM_POLICY_OUT
) {
612 /* dir out => dst = remote, src = local */
613 net
->xfrm
.policy_bydst
[dir
].dbits4
= rbits4
;
614 net
->xfrm
.policy_bydst
[dir
].sbits4
= lbits4
;
615 net
->xfrm
.policy_bydst
[dir
].dbits6
= rbits6
;
616 net
->xfrm
.policy_bydst
[dir
].sbits6
= lbits6
;
618 /* dir in/fwd => dst = local, src = remote */
619 net
->xfrm
.policy_bydst
[dir
].dbits4
= lbits4
;
620 net
->xfrm
.policy_bydst
[dir
].sbits4
= rbits4
;
621 net
->xfrm
.policy_bydst
[dir
].dbits6
= lbits6
;
622 net
->xfrm
.policy_bydst
[dir
].sbits6
= rbits6
;
626 /* re-insert all policies by order of creation */
627 list_for_each_entry_reverse(policy
, &net
->xfrm
.policy_all
, walk
.all
) {
629 chain
= policy_hash_bysel(net
, &policy
->selector
,
631 xfrm_policy_id2dir(policy
->index
));
632 hlist_for_each_entry(pol
, chain
, bydst
) {
633 if (policy
->priority
>= pol
->priority
)
634 newpos
= &pol
->bydst
;
639 hlist_add_behind(&policy
->bydst
, newpos
);
641 hlist_add_head(&policy
->bydst
, chain
);
644 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
646 mutex_unlock(&hash_resize_mutex
);
649 void xfrm_policy_hash_rebuild(struct net
*net
)
651 schedule_work(&net
->xfrm
.policy_hthresh
.work
);
653 EXPORT_SYMBOL(xfrm_policy_hash_rebuild
);
655 /* Generate new index... KAME seems to generate them ordered by cost
656 * of an absolute inpredictability of ordering of rules. This will not pass. */
657 static u32
xfrm_gen_index(struct net
*net
, int dir
, u32 index
)
659 static u32 idx_generator
;
662 struct hlist_head
*list
;
663 struct xfrm_policy
*p
;
668 idx
= (idx_generator
| dir
);
677 list
= net
->xfrm
.policy_byidx
+ idx_hash(net
, idx
);
679 hlist_for_each_entry(p
, list
, byidx
) {
680 if (p
->index
== idx
) {
690 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
692 u32
*p1
= (u32
*) s1
;
693 u32
*p2
= (u32
*) s2
;
694 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
697 for (i
= 0; i
< len
; i
++) {
705 static void xfrm_policy_requeue(struct xfrm_policy
*old
,
706 struct xfrm_policy
*new)
708 struct xfrm_policy_queue
*pq
= &old
->polq
;
709 struct sk_buff_head list
;
711 __skb_queue_head_init(&list
);
713 spin_lock_bh(&pq
->hold_queue
.lock
);
714 skb_queue_splice_init(&pq
->hold_queue
, &list
);
715 if (del_timer(&pq
->hold_timer
))
717 spin_unlock_bh(&pq
->hold_queue
.lock
);
719 if (skb_queue_empty(&list
))
724 spin_lock_bh(&pq
->hold_queue
.lock
);
725 skb_queue_splice(&list
, &pq
->hold_queue
);
726 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
727 if (!mod_timer(&pq
->hold_timer
, jiffies
))
729 spin_unlock_bh(&pq
->hold_queue
.lock
);
732 static bool xfrm_policy_mark_match(struct xfrm_policy
*policy
,
733 struct xfrm_policy
*pol
)
735 u32 mark
= policy
->mark
.v
& policy
->mark
.m
;
737 if (policy
->mark
.v
== pol
->mark
.v
&& policy
->mark
.m
== pol
->mark
.m
)
740 if ((mark
& pol
->mark
.m
) == pol
->mark
.v
&&
741 policy
->priority
== pol
->priority
)
747 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
749 struct net
*net
= xp_net(policy
);
750 struct xfrm_policy
*pol
;
751 struct xfrm_policy
*delpol
;
752 struct hlist_head
*chain
;
753 struct hlist_node
*newpos
;
755 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
756 chain
= policy_hash_bysel(net
, &policy
->selector
, policy
->family
, dir
);
759 hlist_for_each_entry(pol
, chain
, bydst
) {
760 if (pol
->type
== policy
->type
&&
761 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
762 xfrm_policy_mark_match(policy
, pol
) &&
763 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
766 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
770 if (policy
->priority
> pol
->priority
)
772 } else if (policy
->priority
>= pol
->priority
) {
773 newpos
= &pol
->bydst
;
780 hlist_add_behind(&policy
->bydst
, newpos
);
782 hlist_add_head(&policy
->bydst
, chain
);
783 __xfrm_policy_link(policy
, dir
);
784 atomic_inc(&net
->xfrm
.flow_cache_genid
);
786 /* After previous checking, family can either be AF_INET or AF_INET6 */
787 if (policy
->family
== AF_INET
)
788 rt_genid_bump_ipv4(net
);
790 rt_genid_bump_ipv6(net
);
793 xfrm_policy_requeue(delpol
, policy
);
794 __xfrm_policy_unlink(delpol
, dir
);
796 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(net
, dir
, policy
->index
);
797 hlist_add_head(&policy
->byidx
, net
->xfrm
.policy_byidx
+idx_hash(net
, policy
->index
));
798 policy
->curlft
.add_time
= get_seconds();
799 policy
->curlft
.use_time
= 0;
800 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
801 xfrm_pol_hold(policy
);
802 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
805 xfrm_policy_kill(delpol
);
806 else if (xfrm_bydst_should_resize(net
, dir
, NULL
))
807 schedule_work(&net
->xfrm
.policy_hash_work
);
811 EXPORT_SYMBOL(xfrm_policy_insert
);
813 struct xfrm_policy
*xfrm_policy_bysel_ctx(struct net
*net
, u32 mark
, u8 type
,
814 int dir
, struct xfrm_selector
*sel
,
815 struct xfrm_sec_ctx
*ctx
, int delete,
818 struct xfrm_policy
*pol
, *ret
;
819 struct hlist_head
*chain
;
822 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
823 chain
= policy_hash_bysel(net
, sel
, sel
->family
, dir
);
825 hlist_for_each_entry(pol
, chain
, bydst
) {
826 if (pol
->type
== type
&&
827 (mark
& pol
->mark
.m
) == pol
->mark
.v
&&
828 !selector_cmp(sel
, &pol
->selector
) &&
829 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
832 *err
= security_xfrm_policy_delete(
835 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
838 __xfrm_policy_unlink(pol
, dir
);
844 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
847 xfrm_policy_kill(ret
);
850 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
852 struct xfrm_policy
*xfrm_policy_byid(struct net
*net
, u32 mark
, u8 type
,
853 int dir
, u32 id
, int delete, int *err
)
855 struct xfrm_policy
*pol
, *ret
;
856 struct hlist_head
*chain
;
859 if (xfrm_policy_id2dir(id
) != dir
)
863 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
864 chain
= net
->xfrm
.policy_byidx
+ idx_hash(net
, id
);
866 hlist_for_each_entry(pol
, chain
, byidx
) {
867 if (pol
->type
== type
&& pol
->index
== id
&&
868 (mark
& pol
->mark
.m
) == pol
->mark
.v
) {
871 *err
= security_xfrm_policy_delete(
874 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
877 __xfrm_policy_unlink(pol
, dir
);
883 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
886 xfrm_policy_kill(ret
);
889 EXPORT_SYMBOL(xfrm_policy_byid
);
891 #ifdef CONFIG_SECURITY_NETWORK_XFRM
893 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
897 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
898 struct xfrm_policy
*pol
;
901 hlist_for_each_entry(pol
,
902 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
903 if (pol
->type
!= type
)
905 err
= security_xfrm_policy_delete(pol
->security
);
907 xfrm_audit_policy_delete(pol
, 0, task_valid
);
911 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
912 hlist_for_each_entry(pol
,
913 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
915 if (pol
->type
!= type
)
917 err
= security_xfrm_policy_delete(
920 xfrm_audit_policy_delete(pol
, 0,
931 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, bool task_valid
)
937 int xfrm_policy_flush(struct net
*net
, u8 type
, bool task_valid
)
939 int dir
, err
= 0, cnt
= 0;
941 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
943 err
= xfrm_policy_flush_secctx_check(net
, type
, task_valid
);
947 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
948 struct xfrm_policy
*pol
;
952 hlist_for_each_entry(pol
,
953 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
954 if (pol
->type
!= type
)
956 __xfrm_policy_unlink(pol
, dir
);
957 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
960 xfrm_audit_policy_delete(pol
, 1, task_valid
);
962 xfrm_policy_kill(pol
);
964 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
968 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
970 hlist_for_each_entry(pol
,
971 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
973 if (pol
->type
!= type
)
975 __xfrm_policy_unlink(pol
, dir
);
976 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
979 xfrm_audit_policy_delete(pol
, 1, task_valid
);
980 xfrm_policy_kill(pol
);
982 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
991 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
994 EXPORT_SYMBOL(xfrm_policy_flush
);
996 int xfrm_policy_walk(struct net
*net
, struct xfrm_policy_walk
*walk
,
997 int (*func
)(struct xfrm_policy
*, int, int, void*),
1000 struct xfrm_policy
*pol
;
1001 struct xfrm_policy_walk_entry
*x
;
1004 if (walk
->type
>= XFRM_POLICY_TYPE_MAX
&&
1005 walk
->type
!= XFRM_POLICY_TYPE_ANY
)
1008 if (list_empty(&walk
->walk
.all
) && walk
->seq
!= 0)
1011 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1012 if (list_empty(&walk
->walk
.all
))
1013 x
= list_first_entry(&net
->xfrm
.policy_all
, struct xfrm_policy_walk_entry
, all
);
1015 x
= list_entry(&walk
->walk
.all
, struct xfrm_policy_walk_entry
, all
);
1016 list_for_each_entry_from(x
, &net
->xfrm
.policy_all
, all
) {
1019 pol
= container_of(x
, struct xfrm_policy
, walk
);
1020 if (walk
->type
!= XFRM_POLICY_TYPE_ANY
&&
1021 walk
->type
!= pol
->type
)
1023 error
= func(pol
, xfrm_policy_id2dir(pol
->index
),
1026 list_move_tail(&walk
->walk
.all
, &x
->all
);
1031 if (walk
->seq
== 0) {
1035 list_del_init(&walk
->walk
.all
);
1037 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1040 EXPORT_SYMBOL(xfrm_policy_walk
);
1042 void xfrm_policy_walk_init(struct xfrm_policy_walk
*walk
, u8 type
)
1044 INIT_LIST_HEAD(&walk
->walk
.all
);
1045 walk
->walk
.dead
= 1;
1049 EXPORT_SYMBOL(xfrm_policy_walk_init
);
1051 void xfrm_policy_walk_done(struct xfrm_policy_walk
*walk
, struct net
*net
)
1053 if (list_empty(&walk
->walk
.all
))
1056 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
); /*FIXME where is net? */
1057 list_del(&walk
->walk
.all
);
1058 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1060 EXPORT_SYMBOL(xfrm_policy_walk_done
);
1063 * Find policy to apply to this flow.
1065 * Returns 0 if policy found, else an -errno.
1067 static int xfrm_policy_match(const struct xfrm_policy
*pol
,
1068 const struct flowi
*fl
,
1069 u8 type
, u16 family
, int dir
)
1071 const struct xfrm_selector
*sel
= &pol
->selector
;
1075 if (pol
->family
!= family
||
1076 (fl
->flowi_mark
& pol
->mark
.m
) != pol
->mark
.v
||
1080 match
= xfrm_selector_match(sel
, fl
, family
);
1082 ret
= security_xfrm_policy_lookup(pol
->security
, fl
->flowi_secid
,
1088 static struct xfrm_policy
*xfrm_policy_lookup_bytype(struct net
*net
, u8 type
,
1089 const struct flowi
*fl
,
1093 struct xfrm_policy
*pol
, *ret
;
1094 const xfrm_address_t
*daddr
, *saddr
;
1095 struct hlist_head
*chain
;
1098 daddr
= xfrm_flowi_daddr(fl
, family
);
1099 saddr
= xfrm_flowi_saddr(fl
, family
);
1100 if (unlikely(!daddr
|| !saddr
))
1103 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1104 chain
= policy_hash_direct(net
, daddr
, saddr
, family
, dir
);
1106 hlist_for_each_entry(pol
, chain
, bydst
) {
1107 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1117 priority
= ret
->priority
;
1121 chain
= &net
->xfrm
.policy_inexact
[dir
];
1122 hlist_for_each_entry(pol
, chain
, bydst
) {
1123 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
1131 } else if (pol
->priority
< priority
) {
1139 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1144 static struct xfrm_policy
*
1145 __xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
)
1147 #ifdef CONFIG_XFRM_SUB_POLICY
1148 struct xfrm_policy
*pol
;
1150 pol
= xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
1154 return xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
1157 static int flow_to_policy_dir(int dir
)
1159 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1160 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1161 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1167 return XFRM_POLICY_IN
;
1169 return XFRM_POLICY_OUT
;
1171 return XFRM_POLICY_FWD
;
1175 static struct flow_cache_object
*
1176 xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
,
1177 u8 dir
, struct flow_cache_object
*old_obj
, void *ctx
)
1179 struct xfrm_policy
*pol
;
1182 xfrm_pol_put(container_of(old_obj
, struct xfrm_policy
, flo
));
1184 pol
= __xfrm_policy_lookup(net
, fl
, family
, flow_to_policy_dir(dir
));
1185 if (IS_ERR_OR_NULL(pol
))
1186 return ERR_CAST(pol
);
1188 /* Resolver returns two references:
1189 * one for cache and one for caller of flow_cache_lookup() */
1195 static inline int policy_to_flow_dir(int dir
)
1197 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1198 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1199 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1203 case XFRM_POLICY_IN
:
1205 case XFRM_POLICY_OUT
:
1206 return FLOW_DIR_OUT
;
1207 case XFRM_POLICY_FWD
:
1208 return FLOW_DIR_FWD
;
1212 static struct xfrm_policy
*xfrm_sk_policy_lookup(struct sock
*sk
, int dir
,
1213 const struct flowi
*fl
)
1215 struct xfrm_policy
*pol
;
1216 struct net
*net
= sock_net(sk
);
1218 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1219 if ((pol
= sk
->sk_policy
[dir
]) != NULL
) {
1220 bool match
= xfrm_selector_match(&pol
->selector
, fl
,
1225 if ((sk
->sk_mark
& pol
->mark
.m
) != pol
->mark
.v
) {
1229 err
= security_xfrm_policy_lookup(pol
->security
,
1231 policy_to_flow_dir(dir
));
1234 else if (err
== -ESRCH
)
1242 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1246 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1248 struct net
*net
= xp_net(pol
);
1250 list_add(&pol
->walk
.all
, &net
->xfrm
.policy_all
);
1251 net
->xfrm
.policy_count
[dir
]++;
1255 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1258 struct net
*net
= xp_net(pol
);
1260 if (list_empty(&pol
->walk
.all
))
1263 /* Socket policies are not hashed. */
1264 if (!hlist_unhashed(&pol
->bydst
)) {
1265 hlist_del(&pol
->bydst
);
1266 hlist_del(&pol
->byidx
);
1269 list_del_init(&pol
->walk
.all
);
1270 net
->xfrm
.policy_count
[dir
]--;
1275 static void xfrm_sk_policy_link(struct xfrm_policy
*pol
, int dir
)
1277 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+ dir
);
1280 static void xfrm_sk_policy_unlink(struct xfrm_policy
*pol
, int dir
)
1282 __xfrm_policy_unlink(pol
, XFRM_POLICY_MAX
+ dir
);
1285 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1287 struct net
*net
= xp_net(pol
);
1289 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1290 pol
= __xfrm_policy_unlink(pol
, dir
);
1291 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1293 xfrm_policy_kill(pol
);
1298 EXPORT_SYMBOL(xfrm_policy_delete
);
1300 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1302 struct net
*net
= xp_net(pol
);
1303 struct xfrm_policy
*old_pol
;
1305 #ifdef CONFIG_XFRM_SUB_POLICY
1306 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1310 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1311 old_pol
= sk
->sk_policy
[dir
];
1312 sk
->sk_policy
[dir
] = pol
;
1314 pol
->curlft
.add_time
= get_seconds();
1315 pol
->index
= xfrm_gen_index(net
, XFRM_POLICY_MAX
+dir
, 0);
1316 xfrm_sk_policy_link(pol
, dir
);
1320 xfrm_policy_requeue(old_pol
, pol
);
1322 /* Unlinking succeeds always. This is the only function
1323 * allowed to delete or replace socket policy.
1325 xfrm_sk_policy_unlink(old_pol
, dir
);
1327 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1330 xfrm_policy_kill(old_pol
);
1335 static struct xfrm_policy
*clone_policy(const struct xfrm_policy
*old
, int dir
)
1337 struct xfrm_policy
*newp
= xfrm_policy_alloc(xp_net(old
), GFP_ATOMIC
);
1338 struct net
*net
= xp_net(old
);
1341 newp
->selector
= old
->selector
;
1342 if (security_xfrm_policy_clone(old
->security
,
1345 return NULL
; /* ENOMEM */
1347 newp
->lft
= old
->lft
;
1348 newp
->curlft
= old
->curlft
;
1349 newp
->mark
= old
->mark
;
1350 newp
->action
= old
->action
;
1351 newp
->flags
= old
->flags
;
1352 newp
->xfrm_nr
= old
->xfrm_nr
;
1353 newp
->index
= old
->index
;
1354 newp
->type
= old
->type
;
1355 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1356 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1357 write_lock_bh(&net
->xfrm
.xfrm_policy_lock
);
1358 xfrm_sk_policy_link(newp
, dir
);
1359 write_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
1365 int __xfrm_sk_clone_policy(struct sock
*sk
)
1367 struct xfrm_policy
*p0
= sk
->sk_policy
[0],
1368 *p1
= sk
->sk_policy
[1];
1370 sk
->sk_policy
[0] = sk
->sk_policy
[1] = NULL
;
1371 if (p0
&& (sk
->sk_policy
[0] = clone_policy(p0
, 0)) == NULL
)
1373 if (p1
&& (sk
->sk_policy
[1] = clone_policy(p1
, 1)) == NULL
)
1379 xfrm_get_saddr(struct net
*net
, xfrm_address_t
*local
, xfrm_address_t
*remote
,
1380 unsigned short family
)
1383 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1385 if (unlikely(afinfo
== NULL
))
1387 err
= afinfo
->get_saddr(net
, local
, remote
);
1388 xfrm_policy_put_afinfo(afinfo
);
1392 /* Resolve list of templates for the flow, given policy. */
1395 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, const struct flowi
*fl
,
1396 struct xfrm_state
**xfrm
, unsigned short family
)
1398 struct net
*net
= xp_net(policy
);
1401 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1402 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1405 for (nx
= 0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1406 struct xfrm_state
*x
;
1407 xfrm_address_t
*remote
= daddr
;
1408 xfrm_address_t
*local
= saddr
;
1409 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1411 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1412 tmpl
->mode
== XFRM_MODE_BEET
) {
1413 remote
= &tmpl
->id
.daddr
;
1414 local
= &tmpl
->saddr
;
1415 if (xfrm_addr_any(local
, tmpl
->encap_family
)) {
1416 error
= xfrm_get_saddr(net
, &tmp
, remote
, tmpl
->encap_family
);
1423 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1425 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1432 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1435 } else if (error
== -ESRCH
) {
1439 if (!tmpl
->optional
)
1445 for (nx
--; nx
>= 0; nx
--)
1446 xfrm_state_put(xfrm
[nx
]);
1451 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, const struct flowi
*fl
,
1452 struct xfrm_state
**xfrm
, unsigned short family
)
1454 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1455 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1461 for (i
= 0; i
< npols
; i
++) {
1462 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1467 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1475 /* found states are sorted for outbound processing */
1477 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1482 for (cnx
--; cnx
>= 0; cnx
--)
1483 xfrm_state_put(tpp
[cnx
]);
1488 /* Check that the bundle accepts the flow and its components are
1492 static inline int xfrm_get_tos(const struct flowi
*fl
, int family
)
1494 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1500 tos
= afinfo
->get_tos(fl
);
1502 xfrm_policy_put_afinfo(afinfo
);
1507 static struct flow_cache_object
*xfrm_bundle_flo_get(struct flow_cache_object
*flo
)
1509 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1510 struct dst_entry
*dst
= &xdst
->u
.dst
;
1512 if (xdst
->route
== NULL
) {
1513 /* Dummy bundle - if it has xfrms we were not
1514 * able to build bundle as template resolution failed.
1515 * It means we need to try again resolving. */
1516 if (xdst
->num_xfrms
> 0)
1518 } else if (dst
->flags
& DST_XFRM_QUEUE
) {
1522 if (stale_bundle(dst
))
1530 static int xfrm_bundle_flo_check(struct flow_cache_object
*flo
)
1532 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1533 struct dst_entry
*dst
= &xdst
->u
.dst
;
1537 if (stale_bundle(dst
))
1543 static void xfrm_bundle_flo_delete(struct flow_cache_object
*flo
)
1545 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1546 struct dst_entry
*dst
= &xdst
->u
.dst
;
1551 static const struct flow_cache_ops xfrm_bundle_fc_ops
= {
1552 .get
= xfrm_bundle_flo_get
,
1553 .check
= xfrm_bundle_flo_check
,
1554 .delete = xfrm_bundle_flo_delete
,
1557 static inline struct xfrm_dst
*xfrm_alloc_dst(struct net
*net
, int family
)
1559 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1560 struct dst_ops
*dst_ops
;
1561 struct xfrm_dst
*xdst
;
1564 return ERR_PTR(-EINVAL
);
1568 dst_ops
= &net
->xfrm
.xfrm4_dst_ops
;
1570 #if IS_ENABLED(CONFIG_IPV6)
1572 dst_ops
= &net
->xfrm
.xfrm6_dst_ops
;
1578 xdst
= dst_alloc(dst_ops
, NULL
, 0, DST_OBSOLETE_NONE
, 0);
1581 struct dst_entry
*dst
= &xdst
->u
.dst
;
1583 memset(dst
+ 1, 0, sizeof(*xdst
) - sizeof(*dst
));
1584 xdst
->flo
.ops
= &xfrm_bundle_fc_ops
;
1585 if (afinfo
->init_dst
)
1586 afinfo
->init_dst(net
, xdst
);
1588 xdst
= ERR_PTR(-ENOBUFS
);
1590 xfrm_policy_put_afinfo(afinfo
);
1595 static inline int xfrm_init_path(struct xfrm_dst
*path
, struct dst_entry
*dst
,
1598 struct xfrm_policy_afinfo
*afinfo
=
1599 xfrm_policy_get_afinfo(dst
->ops
->family
);
1605 err
= afinfo
->init_path(path
, dst
, nfheader_len
);
1607 xfrm_policy_put_afinfo(afinfo
);
1612 static inline int xfrm_fill_dst(struct xfrm_dst
*xdst
, struct net_device
*dev
,
1613 const struct flowi
*fl
)
1615 struct xfrm_policy_afinfo
*afinfo
=
1616 xfrm_policy_get_afinfo(xdst
->u
.dst
.ops
->family
);
1622 err
= afinfo
->fill_dst(xdst
, dev
, fl
);
1624 xfrm_policy_put_afinfo(afinfo
);
1630 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1631 * all the metrics... Shortly, bundle a bundle.
1634 static struct dst_entry
*xfrm_bundle_create(struct xfrm_policy
*policy
,
1635 struct xfrm_state
**xfrm
, int nx
,
1636 const struct flowi
*fl
,
1637 struct dst_entry
*dst
)
1639 struct net
*net
= xp_net(policy
);
1640 unsigned long now
= jiffies
;
1641 struct net_device
*dev
;
1642 struct xfrm_mode
*inner_mode
;
1643 struct dst_entry
*dst_prev
= NULL
;
1644 struct dst_entry
*dst0
= NULL
;
1648 int nfheader_len
= 0;
1649 int trailer_len
= 0;
1651 int family
= policy
->selector
.family
;
1652 xfrm_address_t saddr
, daddr
;
1654 xfrm_flowi_addr_get(fl
, &saddr
, &daddr
, family
);
1656 tos
= xfrm_get_tos(fl
, family
);
1663 for (; i
< nx
; i
++) {
1664 struct xfrm_dst
*xdst
= xfrm_alloc_dst(net
, family
);
1665 struct dst_entry
*dst1
= &xdst
->u
.dst
;
1667 err
= PTR_ERR(xdst
);
1673 if (xfrm
[i
]->sel
.family
== AF_UNSPEC
) {
1674 inner_mode
= xfrm_ip2inner_mode(xfrm
[i
],
1675 xfrm_af2proto(family
));
1677 err
= -EAFNOSUPPORT
;
1682 inner_mode
= xfrm
[i
]->inner_mode
;
1687 dst_prev
->child
= dst_clone(dst1
);
1688 dst1
->flags
|= DST_NOHASH
;
1692 dst_copy_metrics(dst1
, dst
);
1694 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1695 family
= xfrm
[i
]->props
.family
;
1696 dst
= xfrm_dst_lookup(xfrm
[i
], tos
, &saddr
, &daddr
,
1704 dst1
->xfrm
= xfrm
[i
];
1705 xdst
->xfrm_genid
= xfrm
[i
]->genid
;
1707 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
1708 dst1
->flags
|= DST_HOST
;
1709 dst1
->lastuse
= now
;
1711 dst1
->input
= dst_discard
;
1712 dst1
->output
= inner_mode
->afinfo
->output
;
1714 dst1
->next
= dst_prev
;
1717 header_len
+= xfrm
[i
]->props
.header_len
;
1718 if (xfrm
[i
]->type
->flags
& XFRM_TYPE_NON_FRAGMENT
)
1719 nfheader_len
+= xfrm
[i
]->props
.header_len
;
1720 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
1723 dst_prev
->child
= dst
;
1731 xfrm_init_path((struct xfrm_dst
*)dst0
, dst
, nfheader_len
);
1732 xfrm_init_pmtu(dst_prev
);
1734 for (dst_prev
= dst0
; dst_prev
!= dst
; dst_prev
= dst_prev
->child
) {
1735 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst_prev
;
1737 err
= xfrm_fill_dst(xdst
, dev
, fl
);
1741 dst_prev
->header_len
= header_len
;
1742 dst_prev
->trailer_len
= trailer_len
;
1743 header_len
-= xdst
->u
.dst
.xfrm
->props
.header_len
;
1744 trailer_len
-= xdst
->u
.dst
.xfrm
->props
.trailer_len
;
1752 xfrm_state_put(xfrm
[i
]);
1756 dst0
= ERR_PTR(err
);
1760 #ifdef CONFIG_XFRM_SUB_POLICY
1761 static int xfrm_dst_alloc_copy(void **target
, const void *src
, int size
)
1764 *target
= kmalloc(size
, GFP_ATOMIC
);
1769 memcpy(*target
, src
, size
);
1774 static int xfrm_dst_update_parent(struct dst_entry
*dst
,
1775 const struct xfrm_selector
*sel
)
1777 #ifdef CONFIG_XFRM_SUB_POLICY
1778 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1779 return xfrm_dst_alloc_copy((void **)&(xdst
->partner
),
1786 static int xfrm_dst_update_origin(struct dst_entry
*dst
,
1787 const struct flowi
*fl
)
1789 #ifdef CONFIG_XFRM_SUB_POLICY
1790 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1791 return xfrm_dst_alloc_copy((void **)&(xdst
->origin
), fl
, sizeof(*fl
));
1797 static int xfrm_expand_policies(const struct flowi
*fl
, u16 family
,
1798 struct xfrm_policy
**pols
,
1799 int *num_pols
, int *num_xfrms
)
1803 if (*num_pols
== 0 || !pols
[0]) {
1808 if (IS_ERR(pols
[0]))
1809 return PTR_ERR(pols
[0]);
1811 *num_xfrms
= pols
[0]->xfrm_nr
;
1813 #ifdef CONFIG_XFRM_SUB_POLICY
1814 if (pols
[0] && pols
[0]->action
== XFRM_POLICY_ALLOW
&&
1815 pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1816 pols
[1] = xfrm_policy_lookup_bytype(xp_net(pols
[0]),
1817 XFRM_POLICY_TYPE_MAIN
,
1821 if (IS_ERR(pols
[1])) {
1822 xfrm_pols_put(pols
, *num_pols
);
1823 return PTR_ERR(pols
[1]);
1826 (*num_xfrms
) += pols
[1]->xfrm_nr
;
1830 for (i
= 0; i
< *num_pols
; i
++) {
1831 if (pols
[i
]->action
!= XFRM_POLICY_ALLOW
) {
1841 static struct xfrm_dst
*
1842 xfrm_resolve_and_create_bundle(struct xfrm_policy
**pols
, int num_pols
,
1843 const struct flowi
*fl
, u16 family
,
1844 struct dst_entry
*dst_orig
)
1846 struct net
*net
= xp_net(pols
[0]);
1847 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1848 struct dst_entry
*dst
;
1849 struct xfrm_dst
*xdst
;
1852 /* Try to instantiate a bundle */
1853 err
= xfrm_tmpl_resolve(pols
, num_pols
, fl
, xfrm
, family
);
1855 if (err
!= 0 && err
!= -EAGAIN
)
1856 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1857 return ERR_PTR(err
);
1860 dst
= xfrm_bundle_create(pols
[0], xfrm
, err
, fl
, dst_orig
);
1862 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLEGENERROR
);
1863 return ERR_CAST(dst
);
1866 xdst
= (struct xfrm_dst
*)dst
;
1867 xdst
->num_xfrms
= err
;
1869 err
= xfrm_dst_update_parent(dst
, &pols
[1]->selector
);
1871 err
= xfrm_dst_update_origin(dst
, fl
);
1872 if (unlikely(err
)) {
1874 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLECHECKERROR
);
1875 return ERR_PTR(err
);
1878 xdst
->num_pols
= num_pols
;
1879 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1880 xdst
->policy_genid
= atomic_read(&pols
[0]->genid
);
1885 static void xfrm_policy_queue_process(unsigned long arg
)
1887 struct sk_buff
*skb
;
1889 struct dst_entry
*dst
;
1890 struct xfrm_policy
*pol
= (struct xfrm_policy
*)arg
;
1891 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1893 struct sk_buff_head list
;
1895 spin_lock(&pq
->hold_queue
.lock
);
1896 skb
= skb_peek(&pq
->hold_queue
);
1898 spin_unlock(&pq
->hold_queue
.lock
);
1903 xfrm_decode_session(skb
, &fl
, dst
->ops
->family
);
1904 spin_unlock(&pq
->hold_queue
.lock
);
1906 dst_hold(dst
->path
);
1907 dst
= xfrm_lookup(xp_net(pol
), dst
->path
, &fl
,
1912 if (dst
->flags
& DST_XFRM_QUEUE
) {
1915 if (pq
->timeout
>= XFRM_QUEUE_TMO_MAX
)
1918 pq
->timeout
= pq
->timeout
<< 1;
1919 if (!mod_timer(&pq
->hold_timer
, jiffies
+ pq
->timeout
))
1926 __skb_queue_head_init(&list
);
1928 spin_lock(&pq
->hold_queue
.lock
);
1930 skb_queue_splice_init(&pq
->hold_queue
, &list
);
1931 spin_unlock(&pq
->hold_queue
.lock
);
1933 while (!skb_queue_empty(&list
)) {
1934 skb
= __skb_dequeue(&list
);
1936 xfrm_decode_session(skb
, &fl
, skb_dst(skb
)->ops
->family
);
1937 dst_hold(skb_dst(skb
)->path
);
1938 dst
= xfrm_lookup(xp_net(pol
), skb_dst(skb
)->path
,
1947 skb_dst_set(skb
, dst
);
1958 xfrm_queue_purge(&pq
->hold_queue
);
1962 static int xdst_queue_output(struct sock
*sk
, struct sk_buff
*skb
)
1964 unsigned long sched_next
;
1965 struct dst_entry
*dst
= skb_dst(skb
);
1966 struct xfrm_dst
*xdst
= (struct xfrm_dst
*) dst
;
1967 struct xfrm_policy
*pol
= xdst
->pols
[0];
1968 struct xfrm_policy_queue
*pq
= &pol
->polq
;
1970 if (unlikely(skb_fclone_busy(sk
, skb
))) {
1975 if (pq
->hold_queue
.qlen
> XFRM_MAX_QUEUE_LEN
) {
1982 spin_lock_bh(&pq
->hold_queue
.lock
);
1985 pq
->timeout
= XFRM_QUEUE_TMO_MIN
;
1987 sched_next
= jiffies
+ pq
->timeout
;
1989 if (del_timer(&pq
->hold_timer
)) {
1990 if (time_before(pq
->hold_timer
.expires
, sched_next
))
1991 sched_next
= pq
->hold_timer
.expires
;
1995 __skb_queue_tail(&pq
->hold_queue
, skb
);
1996 if (!mod_timer(&pq
->hold_timer
, sched_next
))
1999 spin_unlock_bh(&pq
->hold_queue
.lock
);
2004 static struct xfrm_dst
*xfrm_create_dummy_bundle(struct net
*net
,
2005 struct xfrm_flo
*xflo
,
2006 const struct flowi
*fl
,
2011 struct net_device
*dev
;
2012 struct dst_entry
*dst
;
2013 struct dst_entry
*dst1
;
2014 struct xfrm_dst
*xdst
;
2016 xdst
= xfrm_alloc_dst(net
, family
);
2020 if (!(xflo
->flags
& XFRM_LOOKUP_QUEUE
) ||
2021 net
->xfrm
.sysctl_larval_drop
||
2025 dst
= xflo
->dst_orig
;
2026 dst1
= &xdst
->u
.dst
;
2030 dst_copy_metrics(dst1
, dst
);
2032 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
2033 dst1
->flags
|= DST_HOST
| DST_XFRM_QUEUE
;
2034 dst1
->lastuse
= jiffies
;
2036 dst1
->input
= dst_discard
;
2037 dst1
->output
= xdst_queue_output
;
2043 xfrm_init_path((struct xfrm_dst
*)dst1
, dst
, 0);
2050 err
= xfrm_fill_dst(xdst
, dev
, fl
);
2059 xdst
= ERR_PTR(err
);
2063 static struct flow_cache_object
*
2064 xfrm_bundle_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
,
2065 struct flow_cache_object
*oldflo
, void *ctx
)
2067 struct xfrm_flo
*xflo
= (struct xfrm_flo
*)ctx
;
2068 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2069 struct xfrm_dst
*xdst
, *new_xdst
;
2070 int num_pols
= 0, num_xfrms
= 0, i
, err
, pol_dead
;
2072 /* Check if the policies from old bundle are usable */
2075 xdst
= container_of(oldflo
, struct xfrm_dst
, flo
);
2076 num_pols
= xdst
->num_pols
;
2077 num_xfrms
= xdst
->num_xfrms
;
2079 for (i
= 0; i
< num_pols
; i
++) {
2080 pols
[i
] = xdst
->pols
[i
];
2081 pol_dead
|= pols
[i
]->walk
.dead
;
2084 dst_free(&xdst
->u
.dst
);
2092 /* Resolve policies to use if we couldn't get them from
2093 * previous cache entry */
2096 pols
[0] = __xfrm_policy_lookup(net
, fl
, family
,
2097 flow_to_policy_dir(dir
));
2098 err
= xfrm_expand_policies(fl
, family
, pols
,
2099 &num_pols
, &num_xfrms
);
2105 goto make_dummy_bundle
;
2108 new_xdst
= xfrm_resolve_and_create_bundle(pols
, num_pols
, fl
, family
,
2110 if (IS_ERR(new_xdst
)) {
2111 err
= PTR_ERR(new_xdst
);
2115 goto make_dummy_bundle
;
2116 dst_hold(&xdst
->u
.dst
);
2118 } else if (new_xdst
== NULL
) {
2121 goto make_dummy_bundle
;
2122 xdst
->num_xfrms
= 0;
2123 dst_hold(&xdst
->u
.dst
);
2127 /* Kill the previous bundle */
2129 /* The policies were stolen for newly generated bundle */
2131 dst_free(&xdst
->u
.dst
);
2134 /* Flow cache does not have reference, it dst_free()'s,
2135 * but we do need to return one reference for original caller */
2136 dst_hold(&new_xdst
->u
.dst
);
2137 return &new_xdst
->flo
;
2140 /* We found policies, but there's no bundles to instantiate:
2141 * either because the policy blocks, has no transformations or
2142 * we could not build template (no xfrm_states).*/
2143 xdst
= xfrm_create_dummy_bundle(net
, xflo
, fl
, num_xfrms
, family
);
2145 xfrm_pols_put(pols
, num_pols
);
2146 return ERR_CAST(xdst
);
2148 xdst
->num_pols
= num_pols
;
2149 xdst
->num_xfrms
= num_xfrms
;
2150 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2152 dst_hold(&xdst
->u
.dst
);
2156 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
2159 dst_free(&xdst
->u
.dst
);
2161 xfrm_pols_put(pols
, num_pols
);
2162 return ERR_PTR(err
);
2165 static struct dst_entry
*make_blackhole(struct net
*net
, u16 family
,
2166 struct dst_entry
*dst_orig
)
2168 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2169 struct dst_entry
*ret
;
2172 dst_release(dst_orig
);
2173 return ERR_PTR(-EINVAL
);
2175 ret
= afinfo
->blackhole_route(net
, dst_orig
);
2177 xfrm_policy_put_afinfo(afinfo
);
2182 /* Main function: finds/creates a bundle for given flow.
2184 * At the moment we eat a raw IP route. Mostly to speed up lookups
2185 * on interfaces with disabled IPsec.
2187 struct dst_entry
*xfrm_lookup(struct net
*net
, struct dst_entry
*dst_orig
,
2188 const struct flowi
*fl
,
2189 struct sock
*sk
, int flags
)
2191 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2192 struct flow_cache_object
*flo
;
2193 struct xfrm_dst
*xdst
;
2194 struct dst_entry
*dst
, *route
;
2195 u16 family
= dst_orig
->ops
->family
;
2196 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
2197 int i
, err
, num_pols
, num_xfrms
= 0, drop_pols
= 0;
2203 if (sk
&& sk
->sk_policy
[XFRM_POLICY_OUT
]) {
2205 pols
[0] = xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
);
2206 err
= xfrm_expand_policies(fl
, family
, pols
,
2207 &num_pols
, &num_xfrms
);
2212 if (num_xfrms
<= 0) {
2213 drop_pols
= num_pols
;
2217 xdst
= xfrm_resolve_and_create_bundle(
2221 xfrm_pols_put(pols
, num_pols
);
2222 err
= PTR_ERR(xdst
);
2224 } else if (xdst
== NULL
) {
2226 drop_pols
= num_pols
;
2230 dst_hold(&xdst
->u
.dst
);
2231 xdst
->u
.dst
.flags
|= DST_NOCACHE
;
2232 route
= xdst
->route
;
2237 struct xfrm_flo xflo
;
2239 xflo
.dst_orig
= dst_orig
;
2242 /* To accelerate a bit... */
2243 if ((dst_orig
->flags
& DST_NOXFRM
) ||
2244 !net
->xfrm
.policy_count
[XFRM_POLICY_OUT
])
2247 flo
= flow_cache_lookup(net
, fl
, family
, dir
,
2248 xfrm_bundle_lookup
, &xflo
);
2255 xdst
= container_of(flo
, struct xfrm_dst
, flo
);
2257 num_pols
= xdst
->num_pols
;
2258 num_xfrms
= xdst
->num_xfrms
;
2259 memcpy(pols
, xdst
->pols
, sizeof(struct xfrm_policy
*) * num_pols
);
2260 route
= xdst
->route
;
2264 if (route
== NULL
&& num_xfrms
> 0) {
2265 /* The only case when xfrm_bundle_lookup() returns a
2266 * bundle with null route, is when the template could
2267 * not be resolved. It means policies are there, but
2268 * bundle could not be created, since we don't yet
2269 * have the xfrm_state's. We need to wait for KM to
2270 * negotiate new SA's or bail out with error.*/
2271 if (net
->xfrm
.sysctl_larval_drop
) {
2272 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2279 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
2287 if ((flags
& XFRM_LOOKUP_ICMP
) &&
2288 !(pols
[0]->flags
& XFRM_POLICY_ICMP
)) {
2293 for (i
= 0; i
< num_pols
; i
++)
2294 pols
[i
]->curlft
.use_time
= get_seconds();
2296 if (num_xfrms
< 0) {
2297 /* Prohibit the flow */
2298 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLBLOCK
);
2301 } else if (num_xfrms
> 0) {
2302 /* Flow transformed */
2303 dst_release(dst_orig
);
2305 /* Flow passes untransformed */
2310 xfrm_pols_put(pols
, drop_pols
);
2311 if (dst
&& dst
->xfrm
&&
2312 dst
->xfrm
->props
.mode
== XFRM_MODE_TUNNEL
)
2313 dst
->flags
|= DST_XFRM_TUNNEL
;
2317 if (!(flags
& XFRM_LOOKUP_ICMP
)) {
2325 if (!(flags
& XFRM_LOOKUP_KEEP_DST_REF
))
2326 dst_release(dst_orig
);
2327 xfrm_pols_put(pols
, drop_pols
);
2328 return ERR_PTR(err
);
2330 EXPORT_SYMBOL(xfrm_lookup
);
2332 /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2333 * Otherwise we may send out blackholed packets.
2335 struct dst_entry
*xfrm_lookup_route(struct net
*net
, struct dst_entry
*dst_orig
,
2336 const struct flowi
*fl
,
2337 struct sock
*sk
, int flags
)
2339 struct dst_entry
*dst
= xfrm_lookup(net
, dst_orig
, fl
, sk
,
2340 flags
| XFRM_LOOKUP_QUEUE
|
2341 XFRM_LOOKUP_KEEP_DST_REF
);
2343 if (IS_ERR(dst
) && PTR_ERR(dst
) == -EREMOTE
)
2344 return make_blackhole(net
, dst_orig
->ops
->family
, dst_orig
);
2348 EXPORT_SYMBOL(xfrm_lookup_route
);
2351 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, const struct flowi
*fl
)
2353 struct xfrm_state
*x
;
2355 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
2357 x
= skb
->sp
->xvec
[idx
];
2358 if (!x
->type
->reject
)
2360 return x
->type
->reject(x
, skb
, fl
);
2363 /* When skb is transformed back to its "native" form, we have to
2364 * check policy restrictions. At the moment we make this in maximally
2365 * stupid way. Shame on me. :-) Of course, connected sockets must
2366 * have policy cached at them.
2370 xfrm_state_ok(const struct xfrm_tmpl
*tmpl
, const struct xfrm_state
*x
,
2371 unsigned short family
)
2373 if (xfrm_state_kern(x
))
2374 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
2375 return x
->id
.proto
== tmpl
->id
.proto
&&
2376 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
2377 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
2378 x
->props
.mode
== tmpl
->mode
&&
2379 (tmpl
->allalgs
|| (tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
2380 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
2381 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
2382 xfrm_state_addr_cmp(tmpl
, x
, family
));
2386 * 0 or more than 0 is returned when validation is succeeded (either bypass
2387 * because of optional transport mode, or next index of the mathced secpath
2388 * state with the template.
2389 * -1 is returned when no matching template is found.
2390 * Otherwise "-2 - errored_index" is returned.
2393 xfrm_policy_ok(const struct xfrm_tmpl
*tmpl
, const struct sec_path
*sp
, int start
,
2394 unsigned short family
)
2398 if (tmpl
->optional
) {
2399 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
2403 for (; idx
< sp
->len
; idx
++) {
2404 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
2406 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2415 int __xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
,
2416 unsigned int family
, int reverse
)
2418 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2421 if (unlikely(afinfo
== NULL
))
2422 return -EAFNOSUPPORT
;
2424 afinfo
->decode_session(skb
, fl
, reverse
);
2425 err
= security_xfrm_decode_session(skb
, &fl
->flowi_secid
);
2426 xfrm_policy_put_afinfo(afinfo
);
2429 EXPORT_SYMBOL(__xfrm_decode_session
);
2431 static inline int secpath_has_nontransport(const struct sec_path
*sp
, int k
, int *idxp
)
2433 for (; k
< sp
->len
; k
++) {
2434 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2443 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
2444 unsigned short family
)
2446 struct net
*net
= dev_net(skb
->dev
);
2447 struct xfrm_policy
*pol
;
2448 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2457 reverse
= dir
& ~XFRM_POLICY_MASK
;
2458 dir
&= XFRM_POLICY_MASK
;
2459 fl_dir
= policy_to_flow_dir(dir
);
2461 if (__xfrm_decode_session(skb
, &fl
, family
, reverse
) < 0) {
2462 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
2466 nf_nat_decode_session(skb
, &fl
, family
);
2468 /* First, check used SA against their selectors. */
2472 for (i
= skb
->sp
->len
-1; i
>= 0; i
--) {
2473 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
2474 if (!xfrm_selector_match(&x
->sel
, &fl
, family
)) {
2475 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMISMATCH
);
2482 if (sk
&& sk
->sk_policy
[dir
]) {
2483 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
);
2485 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2491 struct flow_cache_object
*flo
;
2493 flo
= flow_cache_lookup(net
, &fl
, family
, fl_dir
,
2494 xfrm_policy_lookup
, NULL
);
2495 if (IS_ERR_OR_NULL(flo
))
2496 pol
= ERR_CAST(flo
);
2498 pol
= container_of(flo
, struct xfrm_policy
, flo
);
2502 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2507 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
2508 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2509 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINNOPOLS
);
2515 pol
->curlft
.use_time
= get_seconds();
2519 #ifdef CONFIG_XFRM_SUB_POLICY
2520 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
2521 pols
[1] = xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
,
2525 if (IS_ERR(pols
[1])) {
2526 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2529 pols
[1]->curlft
.use_time
= get_seconds();
2535 if (pol
->action
== XFRM_POLICY_ALLOW
) {
2536 struct sec_path
*sp
;
2537 static struct sec_path dummy
;
2538 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
2539 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
2540 struct xfrm_tmpl
**tpp
= tp
;
2544 if ((sp
= skb
->sp
) == NULL
)
2547 for (pi
= 0; pi
< npols
; pi
++) {
2548 if (pols
[pi
] != pol
&&
2549 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
) {
2550 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2553 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
2554 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
2557 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
2558 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
2562 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
, net
);
2566 /* For each tunnel xfrm, find the first matching tmpl.
2567 * For each tmpl before that, find corresponding xfrm.
2568 * Order is _important_. Later we will implement
2569 * some barriers, but at the moment barriers
2570 * are implied between each two transformations.
2572 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
2573 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
2576 /* "-2 - errored_index" returned */
2578 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2583 if (secpath_has_nontransport(sp
, k
, &xerr_idx
)) {
2584 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2588 xfrm_pols_put(pols
, npols
);
2591 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2594 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2596 xfrm_pols_put(pols
, npols
);
2599 EXPORT_SYMBOL(__xfrm_policy_check
);
2601 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
2603 struct net
*net
= dev_net(skb
->dev
);
2605 struct dst_entry
*dst
;
2608 if (xfrm_decode_session(skb
, &fl
, family
) < 0) {
2609 XFRM_INC_STATS(net
, LINUX_MIB_XFRMFWDHDRERROR
);
2615 dst
= xfrm_lookup(net
, skb_dst(skb
), &fl
, NULL
, XFRM_LOOKUP_QUEUE
);
2620 skb_dst_set(skb
, dst
);
2623 EXPORT_SYMBOL(__xfrm_route_forward
);
2625 /* Optimize later using cookies and generation ids. */
2627 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
2629 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2630 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2631 * get validated by dst_ops->check on every use. We do this
2632 * because when a normal route referenced by an XFRM dst is
2633 * obsoleted we do not go looking around for all parent
2634 * referencing XFRM dsts so that we can invalidate them. It
2635 * is just too much work. Instead we make the checks here on
2636 * every use. For example:
2638 * XFRM dst A --> IPv4 dst X
2640 * X is the "xdst->route" of A (X is also the "dst->path" of A
2641 * in this example). If X is marked obsolete, "A" will not
2642 * notice. That's what we are validating here via the
2643 * stale_bundle() check.
2645 * When a policy's bundle is pruned, we dst_free() the XFRM
2646 * dst which causes it's ->obsolete field to be set to
2647 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2648 * this, we want to force a new route lookup.
2650 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
2656 static int stale_bundle(struct dst_entry
*dst
)
2658 return !xfrm_bundle_ok((struct xfrm_dst
*)dst
);
2661 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
2663 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
2664 dst
->dev
= dev_net(dev
)->loopback_dev
;
2669 EXPORT_SYMBOL(xfrm_dst_ifdown
);
2671 static void xfrm_link_failure(struct sk_buff
*skb
)
2673 /* Impossible. Such dst must be popped before reaches point of failure. */
2676 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
2679 if (dst
->obsolete
) {
2687 void xfrm_garbage_collect(struct net
*net
)
2689 flow_cache_flush(net
);
2691 EXPORT_SYMBOL(xfrm_garbage_collect
);
2693 static void xfrm_garbage_collect_deferred(struct net
*net
)
2695 flow_cache_flush_deferred(net
);
2698 static void xfrm_init_pmtu(struct dst_entry
*dst
)
2701 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2702 u32 pmtu
, route_mtu_cached
;
2704 pmtu
= dst_mtu(dst
->child
);
2705 xdst
->child_mtu_cached
= pmtu
;
2707 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2709 route_mtu_cached
= dst_mtu(xdst
->route
);
2710 xdst
->route_mtu_cached
= route_mtu_cached
;
2712 if (pmtu
> route_mtu_cached
)
2713 pmtu
= route_mtu_cached
;
2715 dst_metric_set(dst
, RTAX_MTU
, pmtu
);
2716 } while ((dst
= dst
->next
));
2719 /* Check that the bundle accepts the flow and its components are
2723 static int xfrm_bundle_ok(struct xfrm_dst
*first
)
2725 struct dst_entry
*dst
= &first
->u
.dst
;
2726 struct xfrm_dst
*last
;
2729 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2730 (dst
->dev
&& !netif_running(dst
->dev
)))
2733 if (dst
->flags
& DST_XFRM_QUEUE
)
2739 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2741 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2743 if (xdst
->xfrm_genid
!= dst
->xfrm
->genid
)
2745 if (xdst
->num_pols
> 0 &&
2746 xdst
->policy_genid
!= atomic_read(&xdst
->pols
[0]->genid
))
2749 mtu
= dst_mtu(dst
->child
);
2750 if (xdst
->child_mtu_cached
!= mtu
) {
2752 xdst
->child_mtu_cached
= mtu
;
2755 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2757 mtu
= dst_mtu(xdst
->route
);
2758 if (xdst
->route_mtu_cached
!= mtu
) {
2760 xdst
->route_mtu_cached
= mtu
;
2764 } while (dst
->xfrm
);
2769 mtu
= last
->child_mtu_cached
;
2773 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2774 if (mtu
> last
->route_mtu_cached
)
2775 mtu
= last
->route_mtu_cached
;
2776 dst_metric_set(dst
, RTAX_MTU
, mtu
);
2781 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2782 last
->child_mtu_cached
= mtu
;
2788 static unsigned int xfrm_default_advmss(const struct dst_entry
*dst
)
2790 return dst_metric_advmss(dst
->path
);
2793 static unsigned int xfrm_mtu(const struct dst_entry
*dst
)
2795 unsigned int mtu
= dst_metric_raw(dst
, RTAX_MTU
);
2797 return mtu
? : dst_mtu(dst
->path
);
2800 static struct neighbour
*xfrm_neigh_lookup(const struct dst_entry
*dst
,
2801 struct sk_buff
*skb
,
2804 return dst
->path
->ops
->neigh_lookup(dst
, skb
, daddr
);
2807 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2811 if (unlikely(afinfo
== NULL
))
2813 if (unlikely(afinfo
->family
>= NPROTO
))
2814 return -EAFNOSUPPORT
;
2815 spin_lock(&xfrm_policy_afinfo_lock
);
2816 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
2819 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2820 if (likely(dst_ops
->kmem_cachep
== NULL
))
2821 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2822 if (likely(dst_ops
->check
== NULL
))
2823 dst_ops
->check
= xfrm_dst_check
;
2824 if (likely(dst_ops
->default_advmss
== NULL
))
2825 dst_ops
->default_advmss
= xfrm_default_advmss
;
2826 if (likely(dst_ops
->mtu
== NULL
))
2827 dst_ops
->mtu
= xfrm_mtu
;
2828 if (likely(dst_ops
->negative_advice
== NULL
))
2829 dst_ops
->negative_advice
= xfrm_negative_advice
;
2830 if (likely(dst_ops
->link_failure
== NULL
))
2831 dst_ops
->link_failure
= xfrm_link_failure
;
2832 if (likely(dst_ops
->neigh_lookup
== NULL
))
2833 dst_ops
->neigh_lookup
= xfrm_neigh_lookup
;
2834 if (likely(afinfo
->garbage_collect
== NULL
))
2835 afinfo
->garbage_collect
= xfrm_garbage_collect_deferred
;
2836 rcu_assign_pointer(xfrm_policy_afinfo
[afinfo
->family
], afinfo
);
2838 spin_unlock(&xfrm_policy_afinfo_lock
);
2842 struct dst_ops
*xfrm_dst_ops
;
2844 switch (afinfo
->family
) {
2846 xfrm_dst_ops
= &net
->xfrm
.xfrm4_dst_ops
;
2848 #if IS_ENABLED(CONFIG_IPV6)
2850 xfrm_dst_ops
= &net
->xfrm
.xfrm6_dst_ops
;
2856 *xfrm_dst_ops
= *afinfo
->dst_ops
;
2862 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2864 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2867 if (unlikely(afinfo
== NULL
))
2869 if (unlikely(afinfo
->family
>= NPROTO
))
2870 return -EAFNOSUPPORT
;
2871 spin_lock(&xfrm_policy_afinfo_lock
);
2872 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
2873 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
2876 RCU_INIT_POINTER(xfrm_policy_afinfo
[afinfo
->family
],
2879 spin_unlock(&xfrm_policy_afinfo_lock
);
2881 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2885 dst_ops
->kmem_cachep
= NULL
;
2886 dst_ops
->check
= NULL
;
2887 dst_ops
->negative_advice
= NULL
;
2888 dst_ops
->link_failure
= NULL
;
2889 afinfo
->garbage_collect
= NULL
;
2893 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2895 static void __net_init
xfrm_dst_ops_init(struct net
*net
)
2897 struct xfrm_policy_afinfo
*afinfo
;
2900 afinfo
= rcu_dereference(xfrm_policy_afinfo
[AF_INET
]);
2902 net
->xfrm
.xfrm4_dst_ops
= *afinfo
->dst_ops
;
2903 #if IS_ENABLED(CONFIG_IPV6)
2904 afinfo
= rcu_dereference(xfrm_policy_afinfo
[AF_INET6
]);
2906 net
->xfrm
.xfrm6_dst_ops
= *afinfo
->dst_ops
;
2911 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
2913 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
2917 xfrm_garbage_collect(dev_net(dev
));
2922 static struct notifier_block xfrm_dev_notifier
= {
2923 .notifier_call
= xfrm_dev_event
,
2926 #ifdef CONFIG_XFRM_STATISTICS
2927 static int __net_init
xfrm_statistics_init(struct net
*net
)
2930 net
->mib
.xfrm_statistics
= alloc_percpu(struct linux_xfrm_mib
);
2931 if (!net
->mib
.xfrm_statistics
)
2933 rv
= xfrm_proc_init(net
);
2935 free_percpu(net
->mib
.xfrm_statistics
);
2939 static void xfrm_statistics_fini(struct net
*net
)
2941 xfrm_proc_fini(net
);
2942 free_percpu(net
->mib
.xfrm_statistics
);
2945 static int __net_init
xfrm_statistics_init(struct net
*net
)
2950 static void xfrm_statistics_fini(struct net
*net
)
2955 static int __net_init
xfrm_policy_init(struct net
*net
)
2957 unsigned int hmask
, sz
;
2960 if (net_eq(net
, &init_net
))
2961 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2962 sizeof(struct xfrm_dst
),
2963 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2967 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2969 net
->xfrm
.policy_byidx
= xfrm_hash_alloc(sz
);
2970 if (!net
->xfrm
.policy_byidx
)
2972 net
->xfrm
.policy_idx_hmask
= hmask
;
2974 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
2975 struct xfrm_policy_hash
*htab
;
2977 net
->xfrm
.policy_count
[dir
] = 0;
2978 net
->xfrm
.policy_count
[XFRM_POLICY_MAX
+ dir
] = 0;
2979 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
2981 htab
= &net
->xfrm
.policy_bydst
[dir
];
2982 htab
->table
= xfrm_hash_alloc(sz
);
2985 htab
->hmask
= hmask
;
2991 net
->xfrm
.policy_hthresh
.lbits4
= 32;
2992 net
->xfrm
.policy_hthresh
.rbits4
= 32;
2993 net
->xfrm
.policy_hthresh
.lbits6
= 128;
2994 net
->xfrm
.policy_hthresh
.rbits6
= 128;
2996 seqlock_init(&net
->xfrm
.policy_hthresh
.lock
);
2998 INIT_LIST_HEAD(&net
->xfrm
.policy_all
);
2999 INIT_WORK(&net
->xfrm
.policy_hash_work
, xfrm_hash_resize
);
3000 INIT_WORK(&net
->xfrm
.policy_hthresh
.work
, xfrm_hash_rebuild
);
3001 if (net_eq(net
, &init_net
))
3002 register_netdevice_notifier(&xfrm_dev_notifier
);
3006 for (dir
--; dir
>= 0; dir
--) {
3007 struct xfrm_policy_hash
*htab
;
3009 htab
= &net
->xfrm
.policy_bydst
[dir
];
3010 xfrm_hash_free(htab
->table
, sz
);
3012 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
3017 static void xfrm_policy_fini(struct net
*net
)
3022 flush_work(&net
->xfrm
.policy_hash_work
);
3023 #ifdef CONFIG_XFRM_SUB_POLICY
3024 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_SUB
, false);
3026 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, false);
3028 WARN_ON(!list_empty(&net
->xfrm
.policy_all
));
3030 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
3031 struct xfrm_policy_hash
*htab
;
3033 WARN_ON(!hlist_empty(&net
->xfrm
.policy_inexact
[dir
]));
3035 htab
= &net
->xfrm
.policy_bydst
[dir
];
3036 sz
= (htab
->hmask
+ 1) * sizeof(struct hlist_head
);
3037 WARN_ON(!hlist_empty(htab
->table
));
3038 xfrm_hash_free(htab
->table
, sz
);
3041 sz
= (net
->xfrm
.policy_idx_hmask
+ 1) * sizeof(struct hlist_head
);
3042 WARN_ON(!hlist_empty(net
->xfrm
.policy_byidx
));
3043 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
3046 static int __net_init
xfrm_net_init(struct net
*net
)
3050 rv
= xfrm_statistics_init(net
);
3052 goto out_statistics
;
3053 rv
= xfrm_state_init(net
);
3056 rv
= xfrm_policy_init(net
);
3059 xfrm_dst_ops_init(net
);
3060 rv
= xfrm_sysctl_init(net
);
3063 rv
= flow_cache_init(net
);
3067 /* Initialize the per-net locks here */
3068 spin_lock_init(&net
->xfrm
.xfrm_state_lock
);
3069 rwlock_init(&net
->xfrm
.xfrm_policy_lock
);
3070 mutex_init(&net
->xfrm
.xfrm_cfg_mutex
);
3075 xfrm_sysctl_fini(net
);
3077 xfrm_policy_fini(net
);
3079 xfrm_state_fini(net
);
3081 xfrm_statistics_fini(net
);
3086 static void __net_exit
xfrm_net_exit(struct net
*net
)
3088 flow_cache_fini(net
);
3089 xfrm_sysctl_fini(net
);
3090 xfrm_policy_fini(net
);
3091 xfrm_state_fini(net
);
3092 xfrm_statistics_fini(net
);
3095 static struct pernet_operations __net_initdata xfrm_net_ops
= {
3096 .init
= xfrm_net_init
,
3097 .exit
= xfrm_net_exit
,
3100 void __init
xfrm_init(void)
3102 register_pernet_subsys(&xfrm_net_ops
);
3106 #ifdef CONFIG_AUDITSYSCALL
3107 static void xfrm_audit_common_policyinfo(struct xfrm_policy
*xp
,
3108 struct audit_buffer
*audit_buf
)
3110 struct xfrm_sec_ctx
*ctx
= xp
->security
;
3111 struct xfrm_selector
*sel
= &xp
->selector
;
3114 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
3115 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
3117 switch (sel
->family
) {
3119 audit_log_format(audit_buf
, " src=%pI4", &sel
->saddr
.a4
);
3120 if (sel
->prefixlen_s
!= 32)
3121 audit_log_format(audit_buf
, " src_prefixlen=%d",
3123 audit_log_format(audit_buf
, " dst=%pI4", &sel
->daddr
.a4
);
3124 if (sel
->prefixlen_d
!= 32)
3125 audit_log_format(audit_buf
, " dst_prefixlen=%d",
3129 audit_log_format(audit_buf
, " src=%pI6", sel
->saddr
.a6
);
3130 if (sel
->prefixlen_s
!= 128)
3131 audit_log_format(audit_buf
, " src_prefixlen=%d",
3133 audit_log_format(audit_buf
, " dst=%pI6", sel
->daddr
.a6
);
3134 if (sel
->prefixlen_d
!= 128)
3135 audit_log_format(audit_buf
, " dst_prefixlen=%d",
3141 void xfrm_audit_policy_add(struct xfrm_policy
*xp
, int result
, bool task_valid
)
3143 struct audit_buffer
*audit_buf
;
3145 audit_buf
= xfrm_audit_start("SPD-add");
3146 if (audit_buf
== NULL
)
3148 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
3149 audit_log_format(audit_buf
, " res=%u", result
);
3150 xfrm_audit_common_policyinfo(xp
, audit_buf
);
3151 audit_log_end(audit_buf
);
3153 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add
);
3155 void xfrm_audit_policy_delete(struct xfrm_policy
*xp
, int result
,
3158 struct audit_buffer
*audit_buf
;
3160 audit_buf
= xfrm_audit_start("SPD-delete");
3161 if (audit_buf
== NULL
)
3163 xfrm_audit_helper_usrinfo(task_valid
, audit_buf
);
3164 audit_log_format(audit_buf
, " res=%u", result
);
3165 xfrm_audit_common_policyinfo(xp
, audit_buf
);
3166 audit_log_end(audit_buf
);
3168 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete
);
3171 #ifdef CONFIG_XFRM_MIGRATE
3172 static bool xfrm_migrate_selector_match(const struct xfrm_selector
*sel_cmp
,
3173 const struct xfrm_selector
*sel_tgt
)
3175 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
3176 if (sel_tgt
->family
== sel_cmp
->family
&&
3177 xfrm_addr_equal(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
3179 xfrm_addr_equal(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
3181 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
3182 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
3186 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
3193 static struct xfrm_policy
*xfrm_migrate_policy_find(const struct xfrm_selector
*sel
,
3194 u8 dir
, u8 type
, struct net
*net
)
3196 struct xfrm_policy
*pol
, *ret
= NULL
;
3197 struct hlist_head
*chain
;
3200 read_lock_bh(&net
->xfrm
.xfrm_policy_lock
); /*FIXME*/
3201 chain
= policy_hash_direct(net
, &sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
3202 hlist_for_each_entry(pol
, chain
, bydst
) {
3203 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3204 pol
->type
== type
) {
3206 priority
= ret
->priority
;
3210 chain
= &net
->xfrm
.policy_inexact
[dir
];
3211 hlist_for_each_entry(pol
, chain
, bydst
) {
3212 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
3213 pol
->type
== type
&&
3214 pol
->priority
< priority
) {
3223 read_unlock_bh(&net
->xfrm
.xfrm_policy_lock
);
3228 static int migrate_tmpl_match(const struct xfrm_migrate
*m
, const struct xfrm_tmpl
*t
)
3232 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
3233 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
3235 case XFRM_MODE_TUNNEL
:
3236 case XFRM_MODE_BEET
:
3237 if (xfrm_addr_equal(&t
->id
.daddr
, &m
->old_daddr
,
3239 xfrm_addr_equal(&t
->saddr
, &m
->old_saddr
,
3244 case XFRM_MODE_TRANSPORT
:
3245 /* in case of transport mode, template does not store
3246 any IP addresses, hence we just compare mode and
3257 /* update endpoint address(es) of template(s) */
3258 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
3259 struct xfrm_migrate
*m
, int num_migrate
)
3261 struct xfrm_migrate
*mp
;
3264 write_lock_bh(&pol
->lock
);
3265 if (unlikely(pol
->walk
.dead
)) {
3266 /* target policy has been deleted */
3267 write_unlock_bh(&pol
->lock
);
3271 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
3272 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
3273 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
3276 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
&&
3277 pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_BEET
)
3279 /* update endpoints */
3280 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
3281 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
3282 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
3283 sizeof(pol
->xfrm_vec
[i
].saddr
));
3284 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
3286 atomic_inc(&pol
->genid
);
3290 write_unlock_bh(&pol
->lock
);
3298 static int xfrm_migrate_check(const struct xfrm_migrate
*m
, int num_migrate
)
3302 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
3305 for (i
= 0; i
< num_migrate
; i
++) {
3306 if (xfrm_addr_equal(&m
[i
].old_daddr
, &m
[i
].new_daddr
,
3308 xfrm_addr_equal(&m
[i
].old_saddr
, &m
[i
].new_saddr
,
3311 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
3312 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
3315 /* check if there is any duplicated entry */
3316 for (j
= i
+ 1; j
< num_migrate
; j
++) {
3317 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
3318 sizeof(m
[i
].old_daddr
)) &&
3319 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
3320 sizeof(m
[i
].old_saddr
)) &&
3321 m
[i
].proto
== m
[j
].proto
&&
3322 m
[i
].mode
== m
[j
].mode
&&
3323 m
[i
].reqid
== m
[j
].reqid
&&
3324 m
[i
].old_family
== m
[j
].old_family
)
3332 int xfrm_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3333 struct xfrm_migrate
*m
, int num_migrate
,
3334 struct xfrm_kmaddress
*k
, struct net
*net
)
3336 int i
, err
, nx_cur
= 0, nx_new
= 0;
3337 struct xfrm_policy
*pol
= NULL
;
3338 struct xfrm_state
*x
, *xc
;
3339 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
3340 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
3341 struct xfrm_migrate
*mp
;
3343 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
3346 /* Stage 1 - find policy */
3347 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
, net
)) == NULL
) {
3352 /* Stage 2 - find and update state(s) */
3353 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
3354 if ((x
= xfrm_migrate_state_find(mp
, net
))) {
3357 if ((xc
= xfrm_state_migrate(x
, mp
))) {
3367 /* Stage 3 - update policy */
3368 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
3371 /* Stage 4 - delete old state(s) */
3373 xfrm_states_put(x_cur
, nx_cur
);
3374 xfrm_states_delete(x_cur
, nx_cur
);
3377 /* Stage 5 - announce */
3378 km_migrate(sel
, dir
, type
, m
, num_migrate
, k
);
3390 xfrm_states_put(x_cur
, nx_cur
);
3392 xfrm_states_delete(x_new
, nx_new
);
3396 EXPORT_SYMBOL(xfrm_migrate
);