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_MUTEX(xfrm_cfg_mutex
);
39 EXPORT_SYMBOL(xfrm_cfg_mutex
);
41 static DEFINE_SPINLOCK(xfrm_policy_sk_bundle_lock
);
42 static struct dst_entry
*xfrm_policy_sk_bundles
;
43 static DEFINE_RWLOCK(xfrm_policy_lock
);
45 static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock
);
46 static struct xfrm_policy_afinfo __rcu
*xfrm_policy_afinfo
[NPROTO
]
49 static struct kmem_cache
*xfrm_dst_cache __read_mostly
;
51 static void xfrm_init_pmtu(struct dst_entry
*dst
);
52 static int stale_bundle(struct dst_entry
*dst
);
53 static int xfrm_bundle_ok(struct xfrm_dst
*xdst
);
56 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
60 __xfrm4_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
62 const struct flowi4
*fl4
= &fl
->u
.ip4
;
64 return addr4_match(fl4
->daddr
, sel
->daddr
.a4
, sel
->prefixlen_d
) &&
65 addr4_match(fl4
->saddr
, sel
->saddr
.a4
, sel
->prefixlen_s
) &&
66 !((xfrm_flowi_dport(fl
, &fl4
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
67 !((xfrm_flowi_sport(fl
, &fl4
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
68 (fl4
->flowi4_proto
== sel
->proto
|| !sel
->proto
) &&
69 (fl4
->flowi4_oif
== sel
->ifindex
|| !sel
->ifindex
);
73 __xfrm6_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
)
75 const struct flowi6
*fl6
= &fl
->u
.ip6
;
77 return addr_match(&fl6
->daddr
, &sel
->daddr
, sel
->prefixlen_d
) &&
78 addr_match(&fl6
->saddr
, &sel
->saddr
, sel
->prefixlen_s
) &&
79 !((xfrm_flowi_dport(fl
, &fl6
->uli
) ^ sel
->dport
) & sel
->dport_mask
) &&
80 !((xfrm_flowi_sport(fl
, &fl6
->uli
) ^ sel
->sport
) & sel
->sport_mask
) &&
81 (fl6
->flowi6_proto
== sel
->proto
|| !sel
->proto
) &&
82 (fl6
->flowi6_oif
== sel
->ifindex
|| !sel
->ifindex
);
85 bool xfrm_selector_match(const struct xfrm_selector
*sel
, const struct flowi
*fl
,
86 unsigned short family
)
90 return __xfrm4_selector_match(sel
, fl
);
92 return __xfrm6_selector_match(sel
, fl
);
97 static struct xfrm_policy_afinfo
*xfrm_policy_get_afinfo(unsigned short family
)
99 struct xfrm_policy_afinfo
*afinfo
;
101 if (unlikely(family
>= NPROTO
))
104 afinfo
= rcu_dereference(xfrm_policy_afinfo
[family
]);
105 if (unlikely(!afinfo
))
110 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo
*afinfo
)
115 static inline struct dst_entry
*__xfrm_dst_lookup(struct net
*net
, int tos
,
116 const xfrm_address_t
*saddr
,
117 const xfrm_address_t
*daddr
,
120 struct xfrm_policy_afinfo
*afinfo
;
121 struct dst_entry
*dst
;
123 afinfo
= xfrm_policy_get_afinfo(family
);
124 if (unlikely(afinfo
== NULL
))
125 return ERR_PTR(-EAFNOSUPPORT
);
127 dst
= afinfo
->dst_lookup(net
, tos
, saddr
, daddr
);
129 xfrm_policy_put_afinfo(afinfo
);
134 static inline struct dst_entry
*xfrm_dst_lookup(struct xfrm_state
*x
, int tos
,
135 xfrm_address_t
*prev_saddr
,
136 xfrm_address_t
*prev_daddr
,
139 struct net
*net
= xs_net(x
);
140 xfrm_address_t
*saddr
= &x
->props
.saddr
;
141 xfrm_address_t
*daddr
= &x
->id
.daddr
;
142 struct dst_entry
*dst
;
144 if (x
->type
->flags
& XFRM_TYPE_LOCAL_COADDR
) {
148 if (x
->type
->flags
& XFRM_TYPE_REMOTE_COADDR
) {
153 dst
= __xfrm_dst_lookup(net
, tos
, saddr
, daddr
, family
);
156 if (prev_saddr
!= saddr
)
157 memcpy(prev_saddr
, saddr
, sizeof(*prev_saddr
));
158 if (prev_daddr
!= daddr
)
159 memcpy(prev_daddr
, daddr
, sizeof(*prev_daddr
));
165 static inline unsigned long make_jiffies(long secs
)
167 if (secs
>= (MAX_SCHEDULE_TIMEOUT
-1)/HZ
)
168 return MAX_SCHEDULE_TIMEOUT
-1;
173 static void xfrm_policy_timer(unsigned long data
)
175 struct xfrm_policy
*xp
= (struct xfrm_policy
*)data
;
176 unsigned long now
= get_seconds();
177 long next
= LONG_MAX
;
181 read_lock(&xp
->lock
);
183 if (unlikely(xp
->walk
.dead
))
186 dir
= xfrm_policy_id2dir(xp
->index
);
188 if (xp
->lft
.hard_add_expires_seconds
) {
189 long tmo
= xp
->lft
.hard_add_expires_seconds
+
190 xp
->curlft
.add_time
- now
;
196 if (xp
->lft
.hard_use_expires_seconds
) {
197 long tmo
= xp
->lft
.hard_use_expires_seconds
+
198 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
204 if (xp
->lft
.soft_add_expires_seconds
) {
205 long tmo
= xp
->lft
.soft_add_expires_seconds
+
206 xp
->curlft
.add_time
- now
;
209 tmo
= XFRM_KM_TIMEOUT
;
214 if (xp
->lft
.soft_use_expires_seconds
) {
215 long tmo
= xp
->lft
.soft_use_expires_seconds
+
216 (xp
->curlft
.use_time
? : xp
->curlft
.add_time
) - now
;
219 tmo
= XFRM_KM_TIMEOUT
;
226 km_policy_expired(xp
, dir
, 0, 0);
227 if (next
!= LONG_MAX
&&
228 !mod_timer(&xp
->timer
, jiffies
+ make_jiffies(next
)))
232 read_unlock(&xp
->lock
);
237 read_unlock(&xp
->lock
);
238 if (!xfrm_policy_delete(xp
, dir
))
239 km_policy_expired(xp
, dir
, 1, 0);
243 static struct flow_cache_object
*xfrm_policy_flo_get(struct flow_cache_object
*flo
)
245 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
247 if (unlikely(pol
->walk
.dead
))
255 static int xfrm_policy_flo_check(struct flow_cache_object
*flo
)
257 struct xfrm_policy
*pol
= container_of(flo
, struct xfrm_policy
, flo
);
259 return !pol
->walk
.dead
;
262 static void xfrm_policy_flo_delete(struct flow_cache_object
*flo
)
264 xfrm_pol_put(container_of(flo
, struct xfrm_policy
, flo
));
267 static const struct flow_cache_ops xfrm_policy_fc_ops
= {
268 .get
= xfrm_policy_flo_get
,
269 .check
= xfrm_policy_flo_check
,
270 .delete = xfrm_policy_flo_delete
,
273 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
277 struct xfrm_policy
*xfrm_policy_alloc(struct net
*net
, gfp_t gfp
)
279 struct xfrm_policy
*policy
;
281 policy
= kzalloc(sizeof(struct xfrm_policy
), gfp
);
284 write_pnet(&policy
->xp_net
, net
);
285 INIT_LIST_HEAD(&policy
->walk
.all
);
286 INIT_HLIST_NODE(&policy
->bydst
);
287 INIT_HLIST_NODE(&policy
->byidx
);
288 rwlock_init(&policy
->lock
);
289 atomic_set(&policy
->refcnt
, 1);
290 setup_timer(&policy
->timer
, xfrm_policy_timer
,
291 (unsigned long)policy
);
292 policy
->flo
.ops
= &xfrm_policy_fc_ops
;
296 EXPORT_SYMBOL(xfrm_policy_alloc
);
298 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
300 void xfrm_policy_destroy(struct xfrm_policy
*policy
)
302 BUG_ON(!policy
->walk
.dead
);
304 if (del_timer(&policy
->timer
))
307 security_xfrm_policy_free(policy
->security
);
310 EXPORT_SYMBOL(xfrm_policy_destroy
);
312 /* Rule must be locked. Release descentant resources, announce
313 * entry dead. The rule must be unlinked from lists to the moment.
316 static void xfrm_policy_kill(struct xfrm_policy
*policy
)
318 policy
->walk
.dead
= 1;
320 atomic_inc(&policy
->genid
);
322 if (del_timer(&policy
->timer
))
323 xfrm_pol_put(policy
);
325 xfrm_pol_put(policy
);
328 static unsigned int xfrm_policy_hashmax __read_mostly
= 1 * 1024 * 1024;
330 static inline unsigned int idx_hash(struct net
*net
, u32 index
)
332 return __idx_hash(index
, net
->xfrm
.policy_idx_hmask
);
335 static struct hlist_head
*policy_hash_bysel(struct net
*net
,
336 const struct xfrm_selector
*sel
,
337 unsigned short family
, int dir
)
339 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
340 unsigned int hash
= __sel_hash(sel
, family
, hmask
);
342 return (hash
== hmask
+ 1 ?
343 &net
->xfrm
.policy_inexact
[dir
] :
344 net
->xfrm
.policy_bydst
[dir
].table
+ hash
);
347 static struct hlist_head
*policy_hash_direct(struct net
*net
,
348 const xfrm_address_t
*daddr
,
349 const xfrm_address_t
*saddr
,
350 unsigned short family
, int dir
)
352 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
353 unsigned int hash
= __addr_hash(daddr
, saddr
, family
, hmask
);
355 return net
->xfrm
.policy_bydst
[dir
].table
+ hash
;
358 static void xfrm_dst_hash_transfer(struct hlist_head
*list
,
359 struct hlist_head
*ndsttable
,
360 unsigned int nhashmask
)
362 struct hlist_node
*entry
, *tmp
, *entry0
= NULL
;
363 struct xfrm_policy
*pol
;
367 hlist_for_each_entry_safe(pol
, entry
, tmp
, list
, bydst
) {
370 h
= __addr_hash(&pol
->selector
.daddr
, &pol
->selector
.saddr
,
371 pol
->family
, nhashmask
);
374 hlist_add_head(&pol
->bydst
, ndsttable
+h
);
380 hlist_add_after(entry0
, &pol
->bydst
);
384 if (!hlist_empty(list
)) {
390 static void xfrm_idx_hash_transfer(struct hlist_head
*list
,
391 struct hlist_head
*nidxtable
,
392 unsigned int nhashmask
)
394 struct hlist_node
*entry
, *tmp
;
395 struct xfrm_policy
*pol
;
397 hlist_for_each_entry_safe(pol
, entry
, tmp
, list
, byidx
) {
400 h
= __idx_hash(pol
->index
, nhashmask
);
401 hlist_add_head(&pol
->byidx
, nidxtable
+h
);
405 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask
)
407 return ((old_hmask
+ 1) << 1) - 1;
410 static void xfrm_bydst_resize(struct net
*net
, int dir
)
412 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
413 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
414 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
415 struct hlist_head
*odst
= net
->xfrm
.policy_bydst
[dir
].table
;
416 struct hlist_head
*ndst
= xfrm_hash_alloc(nsize
);
422 write_lock_bh(&xfrm_policy_lock
);
424 for (i
= hmask
; i
>= 0; i
--)
425 xfrm_dst_hash_transfer(odst
+ i
, ndst
, nhashmask
);
427 net
->xfrm
.policy_bydst
[dir
].table
= ndst
;
428 net
->xfrm
.policy_bydst
[dir
].hmask
= nhashmask
;
430 write_unlock_bh(&xfrm_policy_lock
);
432 xfrm_hash_free(odst
, (hmask
+ 1) * sizeof(struct hlist_head
));
435 static void xfrm_byidx_resize(struct net
*net
, int total
)
437 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
438 unsigned int nhashmask
= xfrm_new_hash_mask(hmask
);
439 unsigned int nsize
= (nhashmask
+ 1) * sizeof(struct hlist_head
);
440 struct hlist_head
*oidx
= net
->xfrm
.policy_byidx
;
441 struct hlist_head
*nidx
= xfrm_hash_alloc(nsize
);
447 write_lock_bh(&xfrm_policy_lock
);
449 for (i
= hmask
; i
>= 0; i
--)
450 xfrm_idx_hash_transfer(oidx
+ i
, nidx
, nhashmask
);
452 net
->xfrm
.policy_byidx
= nidx
;
453 net
->xfrm
.policy_idx_hmask
= nhashmask
;
455 write_unlock_bh(&xfrm_policy_lock
);
457 xfrm_hash_free(oidx
, (hmask
+ 1) * sizeof(struct hlist_head
));
460 static inline int xfrm_bydst_should_resize(struct net
*net
, int dir
, int *total
)
462 unsigned int cnt
= net
->xfrm
.policy_count
[dir
];
463 unsigned int hmask
= net
->xfrm
.policy_bydst
[dir
].hmask
;
468 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
475 static inline int xfrm_byidx_should_resize(struct net
*net
, int total
)
477 unsigned int hmask
= net
->xfrm
.policy_idx_hmask
;
479 if ((hmask
+ 1) < xfrm_policy_hashmax
&&
486 void xfrm_spd_getinfo(struct net
*net
, struct xfrmk_spdinfo
*si
)
488 read_lock_bh(&xfrm_policy_lock
);
489 si
->incnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
];
490 si
->outcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
];
491 si
->fwdcnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
];
492 si
->inscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_IN
+XFRM_POLICY_MAX
];
493 si
->outscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_OUT
+XFRM_POLICY_MAX
];
494 si
->fwdscnt
= net
->xfrm
.policy_count
[XFRM_POLICY_FWD
+XFRM_POLICY_MAX
];
495 si
->spdhcnt
= net
->xfrm
.policy_idx_hmask
;
496 si
->spdhmcnt
= xfrm_policy_hashmax
;
497 read_unlock_bh(&xfrm_policy_lock
);
499 EXPORT_SYMBOL(xfrm_spd_getinfo
);
501 static DEFINE_MUTEX(hash_resize_mutex
);
502 static void xfrm_hash_resize(struct work_struct
*work
)
504 struct net
*net
= container_of(work
, struct net
, xfrm
.policy_hash_work
);
507 mutex_lock(&hash_resize_mutex
);
510 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
511 if (xfrm_bydst_should_resize(net
, dir
, &total
))
512 xfrm_bydst_resize(net
, dir
);
514 if (xfrm_byidx_should_resize(net
, total
))
515 xfrm_byidx_resize(net
, total
);
517 mutex_unlock(&hash_resize_mutex
);
520 /* Generate new index... KAME seems to generate them ordered by cost
521 * of an absolute inpredictability of ordering of rules. This will not pass. */
522 static u32
xfrm_gen_index(struct net
*net
, int dir
)
524 static u32 idx_generator
;
527 struct hlist_node
*entry
;
528 struct hlist_head
*list
;
529 struct xfrm_policy
*p
;
533 idx
= (idx_generator
| dir
);
537 list
= net
->xfrm
.policy_byidx
+ idx_hash(net
, idx
);
539 hlist_for_each_entry(p
, entry
, list
, byidx
) {
540 if (p
->index
== idx
) {
550 static inline int selector_cmp(struct xfrm_selector
*s1
, struct xfrm_selector
*s2
)
552 u32
*p1
= (u32
*) s1
;
553 u32
*p2
= (u32
*) s2
;
554 int len
= sizeof(struct xfrm_selector
) / sizeof(u32
);
557 for (i
= 0; i
< len
; i
++) {
565 int xfrm_policy_insert(int dir
, struct xfrm_policy
*policy
, int excl
)
567 struct net
*net
= xp_net(policy
);
568 struct xfrm_policy
*pol
;
569 struct xfrm_policy
*delpol
;
570 struct hlist_head
*chain
;
571 struct hlist_node
*entry
, *newpos
;
572 u32 mark
= policy
->mark
.v
& policy
->mark
.m
;
574 write_lock_bh(&xfrm_policy_lock
);
575 chain
= policy_hash_bysel(net
, &policy
->selector
, policy
->family
, dir
);
578 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
579 if (pol
->type
== policy
->type
&&
580 !selector_cmp(&pol
->selector
, &policy
->selector
) &&
581 (mark
& pol
->mark
.m
) == pol
->mark
.v
&&
582 xfrm_sec_ctx_match(pol
->security
, policy
->security
) &&
585 write_unlock_bh(&xfrm_policy_lock
);
589 if (policy
->priority
> pol
->priority
)
591 } else if (policy
->priority
>= pol
->priority
) {
592 newpos
= &pol
->bydst
;
599 hlist_add_after(newpos
, &policy
->bydst
);
601 hlist_add_head(&policy
->bydst
, chain
);
602 xfrm_pol_hold(policy
);
603 net
->xfrm
.policy_count
[dir
]++;
604 atomic_inc(&flow_cache_genid
);
607 __xfrm_policy_unlink(delpol
, dir
);
608 policy
->index
= delpol
? delpol
->index
: xfrm_gen_index(net
, dir
);
609 hlist_add_head(&policy
->byidx
, net
->xfrm
.policy_byidx
+idx_hash(net
, policy
->index
));
610 policy
->curlft
.add_time
= get_seconds();
611 policy
->curlft
.use_time
= 0;
612 if (!mod_timer(&policy
->timer
, jiffies
+ HZ
))
613 xfrm_pol_hold(policy
);
614 list_add(&policy
->walk
.all
, &net
->xfrm
.policy_all
);
615 write_unlock_bh(&xfrm_policy_lock
);
618 xfrm_policy_kill(delpol
);
619 else if (xfrm_bydst_should_resize(net
, dir
, NULL
))
620 schedule_work(&net
->xfrm
.policy_hash_work
);
624 EXPORT_SYMBOL(xfrm_policy_insert
);
626 struct xfrm_policy
*xfrm_policy_bysel_ctx(struct net
*net
, u32 mark
, u8 type
,
627 int dir
, struct xfrm_selector
*sel
,
628 struct xfrm_sec_ctx
*ctx
, int delete,
631 struct xfrm_policy
*pol
, *ret
;
632 struct hlist_head
*chain
;
633 struct hlist_node
*entry
;
636 write_lock_bh(&xfrm_policy_lock
);
637 chain
= policy_hash_bysel(net
, sel
, sel
->family
, dir
);
639 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
640 if (pol
->type
== type
&&
641 (mark
& pol
->mark
.m
) == pol
->mark
.v
&&
642 !selector_cmp(sel
, &pol
->selector
) &&
643 xfrm_sec_ctx_match(ctx
, pol
->security
)) {
646 *err
= security_xfrm_policy_delete(
649 write_unlock_bh(&xfrm_policy_lock
);
652 __xfrm_policy_unlink(pol
, dir
);
658 write_unlock_bh(&xfrm_policy_lock
);
661 xfrm_policy_kill(ret
);
664 EXPORT_SYMBOL(xfrm_policy_bysel_ctx
);
666 struct xfrm_policy
*xfrm_policy_byid(struct net
*net
, u32 mark
, u8 type
,
667 int dir
, u32 id
, int delete, int *err
)
669 struct xfrm_policy
*pol
, *ret
;
670 struct hlist_head
*chain
;
671 struct hlist_node
*entry
;
674 if (xfrm_policy_id2dir(id
) != dir
)
678 write_lock_bh(&xfrm_policy_lock
);
679 chain
= net
->xfrm
.policy_byidx
+ idx_hash(net
, id
);
681 hlist_for_each_entry(pol
, entry
, chain
, byidx
) {
682 if (pol
->type
== type
&& pol
->index
== id
&&
683 (mark
& pol
->mark
.m
) == pol
->mark
.v
) {
686 *err
= security_xfrm_policy_delete(
689 write_unlock_bh(&xfrm_policy_lock
);
692 __xfrm_policy_unlink(pol
, dir
);
698 write_unlock_bh(&xfrm_policy_lock
);
701 xfrm_policy_kill(ret
);
704 EXPORT_SYMBOL(xfrm_policy_byid
);
706 #ifdef CONFIG_SECURITY_NETWORK_XFRM
708 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, struct xfrm_audit
*audit_info
)
712 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
713 struct xfrm_policy
*pol
;
714 struct hlist_node
*entry
;
717 hlist_for_each_entry(pol
, entry
,
718 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
719 if (pol
->type
!= type
)
721 err
= security_xfrm_policy_delete(pol
->security
);
723 xfrm_audit_policy_delete(pol
, 0,
724 audit_info
->loginuid
,
725 audit_info
->sessionid
,
730 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
731 hlist_for_each_entry(pol
, entry
,
732 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
734 if (pol
->type
!= type
)
736 err
= security_xfrm_policy_delete(
739 xfrm_audit_policy_delete(pol
, 0,
740 audit_info
->loginuid
,
741 audit_info
->sessionid
,
752 xfrm_policy_flush_secctx_check(struct net
*net
, u8 type
, struct xfrm_audit
*audit_info
)
758 int xfrm_policy_flush(struct net
*net
, u8 type
, struct xfrm_audit
*audit_info
)
760 int dir
, err
= 0, cnt
= 0;
762 write_lock_bh(&xfrm_policy_lock
);
764 err
= xfrm_policy_flush_secctx_check(net
, type
, audit_info
);
768 for (dir
= 0; dir
< XFRM_POLICY_MAX
; dir
++) {
769 struct xfrm_policy
*pol
;
770 struct hlist_node
*entry
;
774 hlist_for_each_entry(pol
, entry
,
775 &net
->xfrm
.policy_inexact
[dir
], bydst
) {
776 if (pol
->type
!= type
)
778 __xfrm_policy_unlink(pol
, dir
);
779 write_unlock_bh(&xfrm_policy_lock
);
782 xfrm_audit_policy_delete(pol
, 1, audit_info
->loginuid
,
783 audit_info
->sessionid
,
786 xfrm_policy_kill(pol
);
788 write_lock_bh(&xfrm_policy_lock
);
792 for (i
= net
->xfrm
.policy_bydst
[dir
].hmask
; i
>= 0; i
--) {
794 hlist_for_each_entry(pol
, entry
,
795 net
->xfrm
.policy_bydst
[dir
].table
+ i
,
797 if (pol
->type
!= type
)
799 __xfrm_policy_unlink(pol
, dir
);
800 write_unlock_bh(&xfrm_policy_lock
);
803 xfrm_audit_policy_delete(pol
, 1,
804 audit_info
->loginuid
,
805 audit_info
->sessionid
,
807 xfrm_policy_kill(pol
);
809 write_lock_bh(&xfrm_policy_lock
);
818 write_unlock_bh(&xfrm_policy_lock
);
821 EXPORT_SYMBOL(xfrm_policy_flush
);
823 int xfrm_policy_walk(struct net
*net
, struct xfrm_policy_walk
*walk
,
824 int (*func
)(struct xfrm_policy
*, int, int, void*),
827 struct xfrm_policy
*pol
;
828 struct xfrm_policy_walk_entry
*x
;
831 if (walk
->type
>= XFRM_POLICY_TYPE_MAX
&&
832 walk
->type
!= XFRM_POLICY_TYPE_ANY
)
835 if (list_empty(&walk
->walk
.all
) && walk
->seq
!= 0)
838 write_lock_bh(&xfrm_policy_lock
);
839 if (list_empty(&walk
->walk
.all
))
840 x
= list_first_entry(&net
->xfrm
.policy_all
, struct xfrm_policy_walk_entry
, all
);
842 x
= list_entry(&walk
->walk
.all
, struct xfrm_policy_walk_entry
, all
);
843 list_for_each_entry_from(x
, &net
->xfrm
.policy_all
, all
) {
846 pol
= container_of(x
, struct xfrm_policy
, walk
);
847 if (walk
->type
!= XFRM_POLICY_TYPE_ANY
&&
848 walk
->type
!= pol
->type
)
850 error
= func(pol
, xfrm_policy_id2dir(pol
->index
),
853 list_move_tail(&walk
->walk
.all
, &x
->all
);
858 if (walk
->seq
== 0) {
862 list_del_init(&walk
->walk
.all
);
864 write_unlock_bh(&xfrm_policy_lock
);
867 EXPORT_SYMBOL(xfrm_policy_walk
);
869 void xfrm_policy_walk_init(struct xfrm_policy_walk
*walk
, u8 type
)
871 INIT_LIST_HEAD(&walk
->walk
.all
);
876 EXPORT_SYMBOL(xfrm_policy_walk_init
);
878 void xfrm_policy_walk_done(struct xfrm_policy_walk
*walk
)
880 if (list_empty(&walk
->walk
.all
))
883 write_lock_bh(&xfrm_policy_lock
);
884 list_del(&walk
->walk
.all
);
885 write_unlock_bh(&xfrm_policy_lock
);
887 EXPORT_SYMBOL(xfrm_policy_walk_done
);
890 * Find policy to apply to this flow.
892 * Returns 0 if policy found, else an -errno.
894 static int xfrm_policy_match(const struct xfrm_policy
*pol
,
895 const struct flowi
*fl
,
896 u8 type
, u16 family
, int dir
)
898 const struct xfrm_selector
*sel
= &pol
->selector
;
902 if (pol
->family
!= family
||
903 (fl
->flowi_mark
& pol
->mark
.m
) != pol
->mark
.v
||
907 match
= xfrm_selector_match(sel
, fl
, family
);
909 ret
= security_xfrm_policy_lookup(pol
->security
, fl
->flowi_secid
,
915 static struct xfrm_policy
*xfrm_policy_lookup_bytype(struct net
*net
, u8 type
,
916 const struct flowi
*fl
,
920 struct xfrm_policy
*pol
, *ret
;
921 const xfrm_address_t
*daddr
, *saddr
;
922 struct hlist_node
*entry
;
923 struct hlist_head
*chain
;
926 daddr
= xfrm_flowi_daddr(fl
, family
);
927 saddr
= xfrm_flowi_saddr(fl
, family
);
928 if (unlikely(!daddr
|| !saddr
))
931 read_lock_bh(&xfrm_policy_lock
);
932 chain
= policy_hash_direct(net
, daddr
, saddr
, family
, dir
);
934 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
935 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
945 priority
= ret
->priority
;
949 chain
= &net
->xfrm
.policy_inexact
[dir
];
950 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
951 err
= xfrm_policy_match(pol
, fl
, type
, family
, dir
);
959 } else if (pol
->priority
< priority
) {
967 read_unlock_bh(&xfrm_policy_lock
);
972 static struct xfrm_policy
*
973 __xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
)
975 #ifdef CONFIG_XFRM_SUB_POLICY
976 struct xfrm_policy
*pol
;
978 pol
= xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_SUB
, fl
, family
, dir
);
982 return xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
, fl
, family
, dir
);
985 static struct flow_cache_object
*
986 xfrm_policy_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
,
987 u8 dir
, struct flow_cache_object
*old_obj
, void *ctx
)
989 struct xfrm_policy
*pol
;
992 xfrm_pol_put(container_of(old_obj
, struct xfrm_policy
, flo
));
994 pol
= __xfrm_policy_lookup(net
, fl
, family
, dir
);
995 if (IS_ERR_OR_NULL(pol
))
996 return ERR_CAST(pol
);
998 /* Resolver returns two references:
999 * one for cache and one for caller of flow_cache_lookup() */
1005 static inline int policy_to_flow_dir(int dir
)
1007 if (XFRM_POLICY_IN
== FLOW_DIR_IN
&&
1008 XFRM_POLICY_OUT
== FLOW_DIR_OUT
&&
1009 XFRM_POLICY_FWD
== FLOW_DIR_FWD
)
1013 case XFRM_POLICY_IN
:
1015 case XFRM_POLICY_OUT
:
1016 return FLOW_DIR_OUT
;
1017 case XFRM_POLICY_FWD
:
1018 return FLOW_DIR_FWD
;
1022 static struct xfrm_policy
*xfrm_sk_policy_lookup(struct sock
*sk
, int dir
,
1023 const struct flowi
*fl
)
1025 struct xfrm_policy
*pol
;
1027 read_lock_bh(&xfrm_policy_lock
);
1028 if ((pol
= sk
->sk_policy
[dir
]) != NULL
) {
1029 bool match
= xfrm_selector_match(&pol
->selector
, fl
,
1034 if ((sk
->sk_mark
& pol
->mark
.m
) != pol
->mark
.v
) {
1038 err
= security_xfrm_policy_lookup(pol
->security
,
1040 policy_to_flow_dir(dir
));
1043 else if (err
== -ESRCH
)
1051 read_unlock_bh(&xfrm_policy_lock
);
1055 static void __xfrm_policy_link(struct xfrm_policy
*pol
, int dir
)
1057 struct net
*net
= xp_net(pol
);
1058 struct hlist_head
*chain
= policy_hash_bysel(net
, &pol
->selector
,
1061 list_add(&pol
->walk
.all
, &net
->xfrm
.policy_all
);
1062 hlist_add_head(&pol
->bydst
, chain
);
1063 hlist_add_head(&pol
->byidx
, net
->xfrm
.policy_byidx
+idx_hash(net
, pol
->index
));
1064 net
->xfrm
.policy_count
[dir
]++;
1067 if (xfrm_bydst_should_resize(net
, dir
, NULL
))
1068 schedule_work(&net
->xfrm
.policy_hash_work
);
1071 static struct xfrm_policy
*__xfrm_policy_unlink(struct xfrm_policy
*pol
,
1074 struct net
*net
= xp_net(pol
);
1076 if (hlist_unhashed(&pol
->bydst
))
1079 hlist_del(&pol
->bydst
);
1080 hlist_del(&pol
->byidx
);
1081 list_del(&pol
->walk
.all
);
1082 net
->xfrm
.policy_count
[dir
]--;
1087 int xfrm_policy_delete(struct xfrm_policy
*pol
, int dir
)
1089 write_lock_bh(&xfrm_policy_lock
);
1090 pol
= __xfrm_policy_unlink(pol
, dir
);
1091 write_unlock_bh(&xfrm_policy_lock
);
1093 xfrm_policy_kill(pol
);
1098 EXPORT_SYMBOL(xfrm_policy_delete
);
1100 int xfrm_sk_policy_insert(struct sock
*sk
, int dir
, struct xfrm_policy
*pol
)
1102 struct net
*net
= xp_net(pol
);
1103 struct xfrm_policy
*old_pol
;
1105 #ifdef CONFIG_XFRM_SUB_POLICY
1106 if (pol
&& pol
->type
!= XFRM_POLICY_TYPE_MAIN
)
1110 write_lock_bh(&xfrm_policy_lock
);
1111 old_pol
= sk
->sk_policy
[dir
];
1112 sk
->sk_policy
[dir
] = pol
;
1114 pol
->curlft
.add_time
= get_seconds();
1115 pol
->index
= xfrm_gen_index(net
, XFRM_POLICY_MAX
+dir
);
1116 __xfrm_policy_link(pol
, XFRM_POLICY_MAX
+dir
);
1119 /* Unlinking succeeds always. This is the only function
1120 * allowed to delete or replace socket policy.
1122 __xfrm_policy_unlink(old_pol
, XFRM_POLICY_MAX
+dir
);
1123 write_unlock_bh(&xfrm_policy_lock
);
1126 xfrm_policy_kill(old_pol
);
1131 static struct xfrm_policy
*clone_policy(const struct xfrm_policy
*old
, int dir
)
1133 struct xfrm_policy
*newp
= xfrm_policy_alloc(xp_net(old
), GFP_ATOMIC
);
1136 newp
->selector
= old
->selector
;
1137 if (security_xfrm_policy_clone(old
->security
,
1140 return NULL
; /* ENOMEM */
1142 newp
->lft
= old
->lft
;
1143 newp
->curlft
= old
->curlft
;
1144 newp
->mark
= old
->mark
;
1145 newp
->action
= old
->action
;
1146 newp
->flags
= old
->flags
;
1147 newp
->xfrm_nr
= old
->xfrm_nr
;
1148 newp
->index
= old
->index
;
1149 newp
->type
= old
->type
;
1150 memcpy(newp
->xfrm_vec
, old
->xfrm_vec
,
1151 newp
->xfrm_nr
*sizeof(struct xfrm_tmpl
));
1152 write_lock_bh(&xfrm_policy_lock
);
1153 __xfrm_policy_link(newp
, XFRM_POLICY_MAX
+dir
);
1154 write_unlock_bh(&xfrm_policy_lock
);
1160 int __xfrm_sk_clone_policy(struct sock
*sk
)
1162 struct xfrm_policy
*p0
= sk
->sk_policy
[0],
1163 *p1
= sk
->sk_policy
[1];
1165 sk
->sk_policy
[0] = sk
->sk_policy
[1] = NULL
;
1166 if (p0
&& (sk
->sk_policy
[0] = clone_policy(p0
, 0)) == NULL
)
1168 if (p1
&& (sk
->sk_policy
[1] = clone_policy(p1
, 1)) == NULL
)
1174 xfrm_get_saddr(struct net
*net
, xfrm_address_t
*local
, xfrm_address_t
*remote
,
1175 unsigned short family
)
1178 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1180 if (unlikely(afinfo
== NULL
))
1182 err
= afinfo
->get_saddr(net
, local
, remote
);
1183 xfrm_policy_put_afinfo(afinfo
);
1187 /* Resolve list of templates for the flow, given policy. */
1190 xfrm_tmpl_resolve_one(struct xfrm_policy
*policy
, const struct flowi
*fl
,
1191 struct xfrm_state
**xfrm
, unsigned short family
)
1193 struct net
*net
= xp_net(policy
);
1196 xfrm_address_t
*daddr
= xfrm_flowi_daddr(fl
, family
);
1197 xfrm_address_t
*saddr
= xfrm_flowi_saddr(fl
, family
);
1200 for (nx
=0, i
= 0; i
< policy
->xfrm_nr
; i
++) {
1201 struct xfrm_state
*x
;
1202 xfrm_address_t
*remote
= daddr
;
1203 xfrm_address_t
*local
= saddr
;
1204 struct xfrm_tmpl
*tmpl
= &policy
->xfrm_vec
[i
];
1206 if (tmpl
->mode
== XFRM_MODE_TUNNEL
||
1207 tmpl
->mode
== XFRM_MODE_BEET
) {
1208 remote
= &tmpl
->id
.daddr
;
1209 local
= &tmpl
->saddr
;
1210 if (xfrm_addr_any(local
, tmpl
->encap_family
)) {
1211 error
= xfrm_get_saddr(net
, &tmp
, remote
, tmpl
->encap_family
);
1218 x
= xfrm_state_find(remote
, local
, fl
, tmpl
, policy
, &error
, family
);
1220 if (x
&& x
->km
.state
== XFRM_STATE_VALID
) {
1227 error
= (x
->km
.state
== XFRM_STATE_ERROR
?
1231 else if (error
== -ESRCH
)
1234 if (!tmpl
->optional
)
1240 for (nx
--; nx
>=0; nx
--)
1241 xfrm_state_put(xfrm
[nx
]);
1246 xfrm_tmpl_resolve(struct xfrm_policy
**pols
, int npols
, const struct flowi
*fl
,
1247 struct xfrm_state
**xfrm
, unsigned short family
)
1249 struct xfrm_state
*tp
[XFRM_MAX_DEPTH
];
1250 struct xfrm_state
**tpp
= (npols
> 1) ? tp
: xfrm
;
1256 for (i
= 0; i
< npols
; i
++) {
1257 if (cnx
+ pols
[i
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
1262 ret
= xfrm_tmpl_resolve_one(pols
[i
], fl
, &tpp
[cnx
], family
);
1270 /* found states are sorted for outbound processing */
1272 xfrm_state_sort(xfrm
, tpp
, cnx
, family
);
1277 for (cnx
--; cnx
>=0; cnx
--)
1278 xfrm_state_put(tpp
[cnx
]);
1283 /* Check that the bundle accepts the flow and its components are
1287 static inline int xfrm_get_tos(const struct flowi
*fl
, int family
)
1289 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1295 tos
= afinfo
->get_tos(fl
);
1297 xfrm_policy_put_afinfo(afinfo
);
1302 static struct flow_cache_object
*xfrm_bundle_flo_get(struct flow_cache_object
*flo
)
1304 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1305 struct dst_entry
*dst
= &xdst
->u
.dst
;
1307 if (xdst
->route
== NULL
) {
1308 /* Dummy bundle - if it has xfrms we were not
1309 * able to build bundle as template resolution failed.
1310 * It means we need to try again resolving. */
1311 if (xdst
->num_xfrms
> 0)
1315 if (stale_bundle(dst
))
1323 static int xfrm_bundle_flo_check(struct flow_cache_object
*flo
)
1325 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1326 struct dst_entry
*dst
= &xdst
->u
.dst
;
1330 if (stale_bundle(dst
))
1336 static void xfrm_bundle_flo_delete(struct flow_cache_object
*flo
)
1338 struct xfrm_dst
*xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1339 struct dst_entry
*dst
= &xdst
->u
.dst
;
1344 static const struct flow_cache_ops xfrm_bundle_fc_ops
= {
1345 .get
= xfrm_bundle_flo_get
,
1346 .check
= xfrm_bundle_flo_check
,
1347 .delete = xfrm_bundle_flo_delete
,
1350 static inline struct xfrm_dst
*xfrm_alloc_dst(struct net
*net
, int family
)
1352 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1353 struct dst_ops
*dst_ops
;
1354 struct xfrm_dst
*xdst
;
1357 return ERR_PTR(-EINVAL
);
1361 dst_ops
= &net
->xfrm
.xfrm4_dst_ops
;
1363 #if IS_ENABLED(CONFIG_IPV6)
1365 dst_ops
= &net
->xfrm
.xfrm6_dst_ops
;
1371 xdst
= dst_alloc(dst_ops
, NULL
, 0, DST_OBSOLETE_NONE
, 0);
1374 struct dst_entry
*dst
= &xdst
->u
.dst
;
1376 memset(dst
+ 1, 0, sizeof(*xdst
) - sizeof(*dst
));
1377 xdst
->flo
.ops
= &xfrm_bundle_fc_ops
;
1378 if (afinfo
->init_dst
)
1379 afinfo
->init_dst(net
, xdst
);
1381 xdst
= ERR_PTR(-ENOBUFS
);
1383 xfrm_policy_put_afinfo(afinfo
);
1388 static inline int xfrm_init_path(struct xfrm_dst
*path
, struct dst_entry
*dst
,
1391 struct xfrm_policy_afinfo
*afinfo
=
1392 xfrm_policy_get_afinfo(dst
->ops
->family
);
1398 err
= afinfo
->init_path(path
, dst
, nfheader_len
);
1400 xfrm_policy_put_afinfo(afinfo
);
1405 static inline int xfrm_fill_dst(struct xfrm_dst
*xdst
, struct net_device
*dev
,
1406 const struct flowi
*fl
)
1408 struct xfrm_policy_afinfo
*afinfo
=
1409 xfrm_policy_get_afinfo(xdst
->u
.dst
.ops
->family
);
1415 err
= afinfo
->fill_dst(xdst
, dev
, fl
);
1417 xfrm_policy_put_afinfo(afinfo
);
1423 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1424 * all the metrics... Shortly, bundle a bundle.
1427 static struct dst_entry
*xfrm_bundle_create(struct xfrm_policy
*policy
,
1428 struct xfrm_state
**xfrm
, int nx
,
1429 const struct flowi
*fl
,
1430 struct dst_entry
*dst
)
1432 struct net
*net
= xp_net(policy
);
1433 unsigned long now
= jiffies
;
1434 struct net_device
*dev
;
1435 struct xfrm_mode
*inner_mode
;
1436 struct dst_entry
*dst_prev
= NULL
;
1437 struct dst_entry
*dst0
= NULL
;
1441 int nfheader_len
= 0;
1442 int trailer_len
= 0;
1444 int family
= policy
->selector
.family
;
1445 xfrm_address_t saddr
, daddr
;
1447 xfrm_flowi_addr_get(fl
, &saddr
, &daddr
, family
);
1449 tos
= xfrm_get_tos(fl
, family
);
1456 for (; i
< nx
; i
++) {
1457 struct xfrm_dst
*xdst
= xfrm_alloc_dst(net
, family
);
1458 struct dst_entry
*dst1
= &xdst
->u
.dst
;
1460 err
= PTR_ERR(xdst
);
1466 if (xfrm
[i
]->sel
.family
== AF_UNSPEC
) {
1467 inner_mode
= xfrm_ip2inner_mode(xfrm
[i
],
1468 xfrm_af2proto(family
));
1470 err
= -EAFNOSUPPORT
;
1475 inner_mode
= xfrm
[i
]->inner_mode
;
1480 dst_prev
->child
= dst_clone(dst1
);
1481 dst1
->flags
|= DST_NOHASH
;
1485 dst_copy_metrics(dst1
, dst
);
1487 if (xfrm
[i
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
1488 family
= xfrm
[i
]->props
.family
;
1489 dst
= xfrm_dst_lookup(xfrm
[i
], tos
, &saddr
, &daddr
,
1497 dst1
->xfrm
= xfrm
[i
];
1498 xdst
->xfrm_genid
= xfrm
[i
]->genid
;
1500 dst1
->obsolete
= DST_OBSOLETE_FORCE_CHK
;
1501 dst1
->flags
|= DST_HOST
;
1502 dst1
->lastuse
= now
;
1504 dst1
->input
= dst_discard
;
1505 dst1
->output
= inner_mode
->afinfo
->output
;
1507 dst1
->next
= dst_prev
;
1510 header_len
+= xfrm
[i
]->props
.header_len
;
1511 if (xfrm
[i
]->type
->flags
& XFRM_TYPE_NON_FRAGMENT
)
1512 nfheader_len
+= xfrm
[i
]->props
.header_len
;
1513 trailer_len
+= xfrm
[i
]->props
.trailer_len
;
1516 dst_prev
->child
= dst
;
1524 xfrm_init_path((struct xfrm_dst
*)dst0
, dst
, nfheader_len
);
1525 xfrm_init_pmtu(dst_prev
);
1527 for (dst_prev
= dst0
; dst_prev
!= dst
; dst_prev
= dst_prev
->child
) {
1528 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst_prev
;
1530 err
= xfrm_fill_dst(xdst
, dev
, fl
);
1534 dst_prev
->header_len
= header_len
;
1535 dst_prev
->trailer_len
= trailer_len
;
1536 header_len
-= xdst
->u
.dst
.xfrm
->props
.header_len
;
1537 trailer_len
-= xdst
->u
.dst
.xfrm
->props
.trailer_len
;
1545 xfrm_state_put(xfrm
[i
]);
1549 dst0
= ERR_PTR(err
);
1554 xfrm_dst_alloc_copy(void **target
, const void *src
, int size
)
1557 *target
= kmalloc(size
, GFP_ATOMIC
);
1561 memcpy(*target
, src
, size
);
1566 xfrm_dst_update_parent(struct dst_entry
*dst
, const struct xfrm_selector
*sel
)
1568 #ifdef CONFIG_XFRM_SUB_POLICY
1569 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1570 return xfrm_dst_alloc_copy((void **)&(xdst
->partner
),
1578 xfrm_dst_update_origin(struct dst_entry
*dst
, const struct flowi
*fl
)
1580 #ifdef CONFIG_XFRM_SUB_POLICY
1581 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
1582 return xfrm_dst_alloc_copy((void **)&(xdst
->origin
), fl
, sizeof(*fl
));
1588 static int xfrm_expand_policies(const struct flowi
*fl
, u16 family
,
1589 struct xfrm_policy
**pols
,
1590 int *num_pols
, int *num_xfrms
)
1594 if (*num_pols
== 0 || !pols
[0]) {
1599 if (IS_ERR(pols
[0]))
1600 return PTR_ERR(pols
[0]);
1602 *num_xfrms
= pols
[0]->xfrm_nr
;
1604 #ifdef CONFIG_XFRM_SUB_POLICY
1605 if (pols
[0] && pols
[0]->action
== XFRM_POLICY_ALLOW
&&
1606 pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
1607 pols
[1] = xfrm_policy_lookup_bytype(xp_net(pols
[0]),
1608 XFRM_POLICY_TYPE_MAIN
,
1612 if (IS_ERR(pols
[1])) {
1613 xfrm_pols_put(pols
, *num_pols
);
1614 return PTR_ERR(pols
[1]);
1617 (*num_xfrms
) += pols
[1]->xfrm_nr
;
1621 for (i
= 0; i
< *num_pols
; i
++) {
1622 if (pols
[i
]->action
!= XFRM_POLICY_ALLOW
) {
1632 static struct xfrm_dst
*
1633 xfrm_resolve_and_create_bundle(struct xfrm_policy
**pols
, int num_pols
,
1634 const struct flowi
*fl
, u16 family
,
1635 struct dst_entry
*dst_orig
)
1637 struct net
*net
= xp_net(pols
[0]);
1638 struct xfrm_state
*xfrm
[XFRM_MAX_DEPTH
];
1639 struct dst_entry
*dst
;
1640 struct xfrm_dst
*xdst
;
1643 /* Try to instantiate a bundle */
1644 err
= xfrm_tmpl_resolve(pols
, num_pols
, fl
, xfrm
, family
);
1646 if (err
!= 0 && err
!= -EAGAIN
)
1647 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1648 return ERR_PTR(err
);
1651 dst
= xfrm_bundle_create(pols
[0], xfrm
, err
, fl
, dst_orig
);
1653 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLEGENERROR
);
1654 return ERR_CAST(dst
);
1657 xdst
= (struct xfrm_dst
*)dst
;
1658 xdst
->num_xfrms
= err
;
1660 err
= xfrm_dst_update_parent(dst
, &pols
[1]->selector
);
1662 err
= xfrm_dst_update_origin(dst
, fl
);
1663 if (unlikely(err
)) {
1665 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTBUNDLECHECKERROR
);
1666 return ERR_PTR(err
);
1669 xdst
->num_pols
= num_pols
;
1670 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1671 xdst
->policy_genid
= atomic_read(&pols
[0]->genid
);
1676 static struct flow_cache_object
*
1677 xfrm_bundle_lookup(struct net
*net
, const struct flowi
*fl
, u16 family
, u8 dir
,
1678 struct flow_cache_object
*oldflo
, void *ctx
)
1680 struct dst_entry
*dst_orig
= (struct dst_entry
*)ctx
;
1681 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1682 struct xfrm_dst
*xdst
, *new_xdst
;
1683 int num_pols
= 0, num_xfrms
= 0, i
, err
, pol_dead
;
1685 /* Check if the policies from old bundle are usable */
1688 xdst
= container_of(oldflo
, struct xfrm_dst
, flo
);
1689 num_pols
= xdst
->num_pols
;
1690 num_xfrms
= xdst
->num_xfrms
;
1692 for (i
= 0; i
< num_pols
; i
++) {
1693 pols
[i
] = xdst
->pols
[i
];
1694 pol_dead
|= pols
[i
]->walk
.dead
;
1697 dst_free(&xdst
->u
.dst
);
1705 /* Resolve policies to use if we couldn't get them from
1706 * previous cache entry */
1709 pols
[0] = __xfrm_policy_lookup(net
, fl
, family
, dir
);
1710 err
= xfrm_expand_policies(fl
, family
, pols
,
1711 &num_pols
, &num_xfrms
);
1717 goto make_dummy_bundle
;
1720 new_xdst
= xfrm_resolve_and_create_bundle(pols
, num_pols
, fl
, family
, dst_orig
);
1721 if (IS_ERR(new_xdst
)) {
1722 err
= PTR_ERR(new_xdst
);
1726 goto make_dummy_bundle
;
1727 dst_hold(&xdst
->u
.dst
);
1729 } else if (new_xdst
== NULL
) {
1732 goto make_dummy_bundle
;
1733 xdst
->num_xfrms
= 0;
1734 dst_hold(&xdst
->u
.dst
);
1738 /* Kill the previous bundle */
1740 /* The policies were stolen for newly generated bundle */
1742 dst_free(&xdst
->u
.dst
);
1745 /* Flow cache does not have reference, it dst_free()'s,
1746 * but we do need to return one reference for original caller */
1747 dst_hold(&new_xdst
->u
.dst
);
1748 return &new_xdst
->flo
;
1751 /* We found policies, but there's no bundles to instantiate:
1752 * either because the policy blocks, has no transformations or
1753 * we could not build template (no xfrm_states).*/
1754 xdst
= xfrm_alloc_dst(net
, family
);
1756 xfrm_pols_put(pols
, num_pols
);
1757 return ERR_CAST(xdst
);
1759 xdst
->num_pols
= num_pols
;
1760 xdst
->num_xfrms
= num_xfrms
;
1761 memcpy(xdst
->pols
, pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1763 dst_hold(&xdst
->u
.dst
);
1767 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLERROR
);
1770 dst_free(&xdst
->u
.dst
);
1772 xfrm_pols_put(pols
, num_pols
);
1773 return ERR_PTR(err
);
1776 static struct dst_entry
*make_blackhole(struct net
*net
, u16 family
,
1777 struct dst_entry
*dst_orig
)
1779 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
1780 struct dst_entry
*ret
;
1783 dst_release(dst_orig
);
1784 return ERR_PTR(-EINVAL
);
1786 ret
= afinfo
->blackhole_route(net
, dst_orig
);
1788 xfrm_policy_put_afinfo(afinfo
);
1793 /* Main function: finds/creates a bundle for given flow.
1795 * At the moment we eat a raw IP route. Mostly to speed up lookups
1796 * on interfaces with disabled IPsec.
1798 struct dst_entry
*xfrm_lookup(struct net
*net
, struct dst_entry
*dst_orig
,
1799 const struct flowi
*fl
,
1800 struct sock
*sk
, int flags
)
1802 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
1803 struct flow_cache_object
*flo
;
1804 struct xfrm_dst
*xdst
;
1805 struct dst_entry
*dst
, *route
;
1806 u16 family
= dst_orig
->ops
->family
;
1807 u8 dir
= policy_to_flow_dir(XFRM_POLICY_OUT
);
1808 int i
, err
, num_pols
, num_xfrms
= 0, drop_pols
= 0;
1815 if (sk
&& sk
->sk_policy
[XFRM_POLICY_OUT
]) {
1817 pols
[0] = xfrm_sk_policy_lookup(sk
, XFRM_POLICY_OUT
, fl
);
1818 err
= xfrm_expand_policies(fl
, family
, pols
,
1819 &num_pols
, &num_xfrms
);
1824 if (num_xfrms
<= 0) {
1825 drop_pols
= num_pols
;
1829 xdst
= xfrm_resolve_and_create_bundle(
1833 xfrm_pols_put(pols
, num_pols
);
1834 err
= PTR_ERR(xdst
);
1836 } else if (xdst
== NULL
) {
1838 drop_pols
= num_pols
;
1842 dst_hold(&xdst
->u
.dst
);
1844 spin_lock_bh(&xfrm_policy_sk_bundle_lock
);
1845 xdst
->u
.dst
.next
= xfrm_policy_sk_bundles
;
1846 xfrm_policy_sk_bundles
= &xdst
->u
.dst
;
1847 spin_unlock_bh(&xfrm_policy_sk_bundle_lock
);
1849 route
= xdst
->route
;
1854 /* To accelerate a bit... */
1855 if ((dst_orig
->flags
& DST_NOXFRM
) ||
1856 !net
->xfrm
.policy_count
[XFRM_POLICY_OUT
])
1859 flo
= flow_cache_lookup(net
, fl
, family
, dir
,
1860 xfrm_bundle_lookup
, dst_orig
);
1867 xdst
= container_of(flo
, struct xfrm_dst
, flo
);
1869 num_pols
= xdst
->num_pols
;
1870 num_xfrms
= xdst
->num_xfrms
;
1871 memcpy(pols
, xdst
->pols
, sizeof(struct xfrm_policy
*) * num_pols
);
1872 route
= xdst
->route
;
1876 if (route
== NULL
&& num_xfrms
> 0) {
1877 /* The only case when xfrm_bundle_lookup() returns a
1878 * bundle with null route, is when the template could
1879 * not be resolved. It means policies are there, but
1880 * bundle could not be created, since we don't yet
1881 * have the xfrm_state's. We need to wait for KM to
1882 * negotiate new SA's or bail out with error.*/
1883 if (net
->xfrm
.sysctl_larval_drop
) {
1884 /* EREMOTE tells the caller to generate
1885 * a one-shot blackhole route. */
1887 xfrm_pols_put(pols
, drop_pols
);
1888 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
1890 return make_blackhole(net
, family
, dst_orig
);
1892 if (fl
->flowi_flags
& FLOWI_FLAG_CAN_SLEEP
) {
1893 DECLARE_WAITQUEUE(wait
, current
);
1895 add_wait_queue(&net
->xfrm
.km_waitq
, &wait
);
1896 set_current_state(TASK_INTERRUPTIBLE
);
1898 set_current_state(TASK_RUNNING
);
1899 remove_wait_queue(&net
->xfrm
.km_waitq
, &wait
);
1901 if (!signal_pending(current
)) {
1910 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTNOSTATES
);
1918 if ((flags
& XFRM_LOOKUP_ICMP
) &&
1919 !(pols
[0]->flags
& XFRM_POLICY_ICMP
)) {
1924 for (i
= 0; i
< num_pols
; i
++)
1925 pols
[i
]->curlft
.use_time
= get_seconds();
1927 if (num_xfrms
< 0) {
1928 /* Prohibit the flow */
1929 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTPOLBLOCK
);
1932 } else if (num_xfrms
> 0) {
1933 /* Flow transformed */
1934 dst_release(dst_orig
);
1936 /* Flow passes untransformed */
1941 xfrm_pols_put(pols
, drop_pols
);
1942 if (dst
&& dst
->xfrm
&&
1943 dst
->xfrm
->props
.mode
== XFRM_MODE_TUNNEL
)
1944 dst
->flags
|= DST_XFRM_TUNNEL
;
1948 if (!(flags
& XFRM_LOOKUP_ICMP
)) {
1956 dst_release(dst_orig
);
1957 xfrm_pols_put(pols
, drop_pols
);
1958 return ERR_PTR(err
);
1960 EXPORT_SYMBOL(xfrm_lookup
);
1963 xfrm_secpath_reject(int idx
, struct sk_buff
*skb
, const struct flowi
*fl
)
1965 struct xfrm_state
*x
;
1967 if (!skb
->sp
|| idx
< 0 || idx
>= skb
->sp
->len
)
1969 x
= skb
->sp
->xvec
[idx
];
1970 if (!x
->type
->reject
)
1972 return x
->type
->reject(x
, skb
, fl
);
1975 /* When skb is transformed back to its "native" form, we have to
1976 * check policy restrictions. At the moment we make this in maximally
1977 * stupid way. Shame on me. :-) Of course, connected sockets must
1978 * have policy cached at them.
1982 xfrm_state_ok(const struct xfrm_tmpl
*tmpl
, const struct xfrm_state
*x
,
1983 unsigned short family
)
1985 if (xfrm_state_kern(x
))
1986 return tmpl
->optional
&& !xfrm_state_addr_cmp(tmpl
, x
, tmpl
->encap_family
);
1987 return x
->id
.proto
== tmpl
->id
.proto
&&
1988 (x
->id
.spi
== tmpl
->id
.spi
|| !tmpl
->id
.spi
) &&
1989 (x
->props
.reqid
== tmpl
->reqid
|| !tmpl
->reqid
) &&
1990 x
->props
.mode
== tmpl
->mode
&&
1991 (tmpl
->allalgs
|| (tmpl
->aalgos
& (1<<x
->props
.aalgo
)) ||
1992 !(xfrm_id_proto_match(tmpl
->id
.proto
, IPSEC_PROTO_ANY
))) &&
1993 !(x
->props
.mode
!= XFRM_MODE_TRANSPORT
&&
1994 xfrm_state_addr_cmp(tmpl
, x
, family
));
1998 * 0 or more than 0 is returned when validation is succeeded (either bypass
1999 * because of optional transport mode, or next index of the mathced secpath
2000 * state with the template.
2001 * -1 is returned when no matching template is found.
2002 * Otherwise "-2 - errored_index" is returned.
2005 xfrm_policy_ok(const struct xfrm_tmpl
*tmpl
, const struct sec_path
*sp
, int start
,
2006 unsigned short family
)
2010 if (tmpl
->optional
) {
2011 if (tmpl
->mode
== XFRM_MODE_TRANSPORT
)
2015 for (; idx
< sp
->len
; idx
++) {
2016 if (xfrm_state_ok(tmpl
, sp
->xvec
[idx
], family
))
2018 if (sp
->xvec
[idx
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2027 int __xfrm_decode_session(struct sk_buff
*skb
, struct flowi
*fl
,
2028 unsigned int family
, int reverse
)
2030 struct xfrm_policy_afinfo
*afinfo
= xfrm_policy_get_afinfo(family
);
2033 if (unlikely(afinfo
== NULL
))
2034 return -EAFNOSUPPORT
;
2036 afinfo
->decode_session(skb
, fl
, reverse
);
2037 err
= security_xfrm_decode_session(skb
, &fl
->flowi_secid
);
2038 xfrm_policy_put_afinfo(afinfo
);
2041 EXPORT_SYMBOL(__xfrm_decode_session
);
2043 static inline int secpath_has_nontransport(const struct sec_path
*sp
, int k
, int *idxp
)
2045 for (; k
< sp
->len
; k
++) {
2046 if (sp
->xvec
[k
]->props
.mode
!= XFRM_MODE_TRANSPORT
) {
2055 int __xfrm_policy_check(struct sock
*sk
, int dir
, struct sk_buff
*skb
,
2056 unsigned short family
)
2058 struct net
*net
= dev_net(skb
->dev
);
2059 struct xfrm_policy
*pol
;
2060 struct xfrm_policy
*pols
[XFRM_POLICY_TYPE_MAX
];
2069 reverse
= dir
& ~XFRM_POLICY_MASK
;
2070 dir
&= XFRM_POLICY_MASK
;
2071 fl_dir
= policy_to_flow_dir(dir
);
2073 if (__xfrm_decode_session(skb
, &fl
, family
, reverse
) < 0) {
2074 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINHDRERROR
);
2078 nf_nat_decode_session(skb
, &fl
, family
);
2080 /* First, check used SA against their selectors. */
2084 for (i
=skb
->sp
->len
-1; i
>=0; i
--) {
2085 struct xfrm_state
*x
= skb
->sp
->xvec
[i
];
2086 if (!xfrm_selector_match(&x
->sel
, &fl
, family
)) {
2087 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINSTATEMISMATCH
);
2094 if (sk
&& sk
->sk_policy
[dir
]) {
2095 pol
= xfrm_sk_policy_lookup(sk
, dir
, &fl
);
2097 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2103 struct flow_cache_object
*flo
;
2105 flo
= flow_cache_lookup(net
, &fl
, family
, fl_dir
,
2106 xfrm_policy_lookup
, NULL
);
2107 if (IS_ERR_OR_NULL(flo
))
2108 pol
= ERR_CAST(flo
);
2110 pol
= container_of(flo
, struct xfrm_policy
, flo
);
2114 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2119 if (skb
->sp
&& secpath_has_nontransport(skb
->sp
, 0, &xerr_idx
)) {
2120 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2121 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINNOPOLS
);
2127 pol
->curlft
.use_time
= get_seconds();
2131 #ifdef CONFIG_XFRM_SUB_POLICY
2132 if (pols
[0]->type
!= XFRM_POLICY_TYPE_MAIN
) {
2133 pols
[1] = xfrm_policy_lookup_bytype(net
, XFRM_POLICY_TYPE_MAIN
,
2137 if (IS_ERR(pols
[1])) {
2138 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLERROR
);
2141 pols
[1]->curlft
.use_time
= get_seconds();
2147 if (pol
->action
== XFRM_POLICY_ALLOW
) {
2148 struct sec_path
*sp
;
2149 static struct sec_path dummy
;
2150 struct xfrm_tmpl
*tp
[XFRM_MAX_DEPTH
];
2151 struct xfrm_tmpl
*stp
[XFRM_MAX_DEPTH
];
2152 struct xfrm_tmpl
**tpp
= tp
;
2156 if ((sp
= skb
->sp
) == NULL
)
2159 for (pi
= 0; pi
< npols
; pi
++) {
2160 if (pols
[pi
] != pol
&&
2161 pols
[pi
]->action
!= XFRM_POLICY_ALLOW
) {
2162 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2165 if (ti
+ pols
[pi
]->xfrm_nr
>= XFRM_MAX_DEPTH
) {
2166 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINBUFFERERROR
);
2169 for (i
= 0; i
< pols
[pi
]->xfrm_nr
; i
++)
2170 tpp
[ti
++] = &pols
[pi
]->xfrm_vec
[i
];
2174 xfrm_tmpl_sort(stp
, tpp
, xfrm_nr
, family
);
2178 /* For each tunnel xfrm, find the first matching tmpl.
2179 * For each tmpl before that, find corresponding xfrm.
2180 * Order is _important_. Later we will implement
2181 * some barriers, but at the moment barriers
2182 * are implied between each two transformations.
2184 for (i
= xfrm_nr
-1, k
= 0; i
>= 0; i
--) {
2185 k
= xfrm_policy_ok(tpp
[i
], sp
, k
, family
);
2188 /* "-2 - errored_index" returned */
2190 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2195 if (secpath_has_nontransport(sp
, k
, &xerr_idx
)) {
2196 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINTMPLMISMATCH
);
2200 xfrm_pols_put(pols
, npols
);
2203 XFRM_INC_STATS(net
, LINUX_MIB_XFRMINPOLBLOCK
);
2206 xfrm_secpath_reject(xerr_idx
, skb
, &fl
);
2208 xfrm_pols_put(pols
, npols
);
2211 EXPORT_SYMBOL(__xfrm_policy_check
);
2213 int __xfrm_route_forward(struct sk_buff
*skb
, unsigned short family
)
2215 struct net
*net
= dev_net(skb
->dev
);
2217 struct dst_entry
*dst
;
2220 if (xfrm_decode_session(skb
, &fl
, family
) < 0) {
2221 XFRM_INC_STATS(net
, LINUX_MIB_XFRMFWDHDRERROR
);
2227 dst
= xfrm_lookup(net
, skb_dst(skb
), &fl
, NULL
, 0);
2232 skb_dst_set(skb
, dst
);
2235 EXPORT_SYMBOL(__xfrm_route_forward
);
2237 /* Optimize later using cookies and generation ids. */
2239 static struct dst_entry
*xfrm_dst_check(struct dst_entry
*dst
, u32 cookie
)
2241 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2242 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2243 * get validated by dst_ops->check on every use. We do this
2244 * because when a normal route referenced by an XFRM dst is
2245 * obsoleted we do not go looking around for all parent
2246 * referencing XFRM dsts so that we can invalidate them. It
2247 * is just too much work. Instead we make the checks here on
2248 * every use. For example:
2250 * XFRM dst A --> IPv4 dst X
2252 * X is the "xdst->route" of A (X is also the "dst->path" of A
2253 * in this example). If X is marked obsolete, "A" will not
2254 * notice. That's what we are validating here via the
2255 * stale_bundle() check.
2257 * When a policy's bundle is pruned, we dst_free() the XFRM
2258 * dst which causes it's ->obsolete field to be set to
2259 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2260 * this, we want to force a new route lookup.
2262 if (dst
->obsolete
< 0 && !stale_bundle(dst
))
2268 static int stale_bundle(struct dst_entry
*dst
)
2270 return !xfrm_bundle_ok((struct xfrm_dst
*)dst
);
2273 void xfrm_dst_ifdown(struct dst_entry
*dst
, struct net_device
*dev
)
2275 while ((dst
= dst
->child
) && dst
->xfrm
&& dst
->dev
== dev
) {
2276 dst
->dev
= dev_net(dev
)->loopback_dev
;
2281 EXPORT_SYMBOL(xfrm_dst_ifdown
);
2283 static void xfrm_link_failure(struct sk_buff
*skb
)
2285 /* Impossible. Such dst must be popped before reaches point of failure. */
2288 static struct dst_entry
*xfrm_negative_advice(struct dst_entry
*dst
)
2291 if (dst
->obsolete
) {
2299 static void __xfrm_garbage_collect(struct net
*net
)
2301 struct dst_entry
*head
, *next
;
2303 spin_lock_bh(&xfrm_policy_sk_bundle_lock
);
2304 head
= xfrm_policy_sk_bundles
;
2305 xfrm_policy_sk_bundles
= NULL
;
2306 spin_unlock_bh(&xfrm_policy_sk_bundle_lock
);
2315 static void xfrm_garbage_collect(struct net
*net
)
2318 __xfrm_garbage_collect(net
);
2321 static void xfrm_garbage_collect_deferred(struct net
*net
)
2323 flow_cache_flush_deferred();
2324 __xfrm_garbage_collect(net
);
2327 static void xfrm_init_pmtu(struct dst_entry
*dst
)
2330 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2331 u32 pmtu
, route_mtu_cached
;
2333 pmtu
= dst_mtu(dst
->child
);
2334 xdst
->child_mtu_cached
= pmtu
;
2336 pmtu
= xfrm_state_mtu(dst
->xfrm
, pmtu
);
2338 route_mtu_cached
= dst_mtu(xdst
->route
);
2339 xdst
->route_mtu_cached
= route_mtu_cached
;
2341 if (pmtu
> route_mtu_cached
)
2342 pmtu
= route_mtu_cached
;
2344 dst_metric_set(dst
, RTAX_MTU
, pmtu
);
2345 } while ((dst
= dst
->next
));
2348 /* Check that the bundle accepts the flow and its components are
2352 static int xfrm_bundle_ok(struct xfrm_dst
*first
)
2354 struct dst_entry
*dst
= &first
->u
.dst
;
2355 struct xfrm_dst
*last
;
2358 if (!dst_check(dst
->path
, ((struct xfrm_dst
*)dst
)->path_cookie
) ||
2359 (dst
->dev
&& !netif_running(dst
->dev
)))
2365 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
2367 if (dst
->xfrm
->km
.state
!= XFRM_STATE_VALID
)
2369 if (xdst
->xfrm_genid
!= dst
->xfrm
->genid
)
2371 if (xdst
->num_pols
> 0 &&
2372 xdst
->policy_genid
!= atomic_read(&xdst
->pols
[0]->genid
))
2375 mtu
= dst_mtu(dst
->child
);
2376 if (xdst
->child_mtu_cached
!= mtu
) {
2378 xdst
->child_mtu_cached
= mtu
;
2381 if (!dst_check(xdst
->route
, xdst
->route_cookie
))
2383 mtu
= dst_mtu(xdst
->route
);
2384 if (xdst
->route_mtu_cached
!= mtu
) {
2386 xdst
->route_mtu_cached
= mtu
;
2390 } while (dst
->xfrm
);
2395 mtu
= last
->child_mtu_cached
;
2399 mtu
= xfrm_state_mtu(dst
->xfrm
, mtu
);
2400 if (mtu
> last
->route_mtu_cached
)
2401 mtu
= last
->route_mtu_cached
;
2402 dst_metric_set(dst
, RTAX_MTU
, mtu
);
2407 last
= (struct xfrm_dst
*)last
->u
.dst
.next
;
2408 last
->child_mtu_cached
= mtu
;
2414 static unsigned int xfrm_default_advmss(const struct dst_entry
*dst
)
2416 return dst_metric_advmss(dst
->path
);
2419 static unsigned int xfrm_mtu(const struct dst_entry
*dst
)
2421 unsigned int mtu
= dst_metric_raw(dst
, RTAX_MTU
);
2423 return mtu
? : dst_mtu(dst
->path
);
2426 static struct neighbour
*xfrm_neigh_lookup(const struct dst_entry
*dst
,
2427 struct sk_buff
*skb
,
2430 return dst
->path
->ops
->neigh_lookup(dst
, skb
, daddr
);
2433 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2437 if (unlikely(afinfo
== NULL
))
2439 if (unlikely(afinfo
->family
>= NPROTO
))
2440 return -EAFNOSUPPORT
;
2441 spin_lock(&xfrm_policy_afinfo_lock
);
2442 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
))
2445 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2446 if (likely(dst_ops
->kmem_cachep
== NULL
))
2447 dst_ops
->kmem_cachep
= xfrm_dst_cache
;
2448 if (likely(dst_ops
->check
== NULL
))
2449 dst_ops
->check
= xfrm_dst_check
;
2450 if (likely(dst_ops
->default_advmss
== NULL
))
2451 dst_ops
->default_advmss
= xfrm_default_advmss
;
2452 if (likely(dst_ops
->mtu
== NULL
))
2453 dst_ops
->mtu
= xfrm_mtu
;
2454 if (likely(dst_ops
->negative_advice
== NULL
))
2455 dst_ops
->negative_advice
= xfrm_negative_advice
;
2456 if (likely(dst_ops
->link_failure
== NULL
))
2457 dst_ops
->link_failure
= xfrm_link_failure
;
2458 if (likely(dst_ops
->neigh_lookup
== NULL
))
2459 dst_ops
->neigh_lookup
= xfrm_neigh_lookup
;
2460 if (likely(afinfo
->garbage_collect
== NULL
))
2461 afinfo
->garbage_collect
= xfrm_garbage_collect_deferred
;
2462 rcu_assign_pointer(xfrm_policy_afinfo
[afinfo
->family
], afinfo
);
2464 spin_unlock(&xfrm_policy_afinfo_lock
);
2468 struct dst_ops
*xfrm_dst_ops
;
2470 switch (afinfo
->family
) {
2472 xfrm_dst_ops
= &net
->xfrm
.xfrm4_dst_ops
;
2474 #if IS_ENABLED(CONFIG_IPV6)
2476 xfrm_dst_ops
= &net
->xfrm
.xfrm6_dst_ops
;
2482 *xfrm_dst_ops
= *afinfo
->dst_ops
;
2488 EXPORT_SYMBOL(xfrm_policy_register_afinfo
);
2490 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo
*afinfo
)
2493 if (unlikely(afinfo
== NULL
))
2495 if (unlikely(afinfo
->family
>= NPROTO
))
2496 return -EAFNOSUPPORT
;
2497 spin_lock(&xfrm_policy_afinfo_lock
);
2498 if (likely(xfrm_policy_afinfo
[afinfo
->family
] != NULL
)) {
2499 if (unlikely(xfrm_policy_afinfo
[afinfo
->family
] != afinfo
))
2502 RCU_INIT_POINTER(xfrm_policy_afinfo
[afinfo
->family
],
2505 spin_unlock(&xfrm_policy_afinfo_lock
);
2507 struct dst_ops
*dst_ops
= afinfo
->dst_ops
;
2511 dst_ops
->kmem_cachep
= NULL
;
2512 dst_ops
->check
= NULL
;
2513 dst_ops
->negative_advice
= NULL
;
2514 dst_ops
->link_failure
= NULL
;
2515 afinfo
->garbage_collect
= NULL
;
2519 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo
);
2521 static void __net_init
xfrm_dst_ops_init(struct net
*net
)
2523 struct xfrm_policy_afinfo
*afinfo
;
2526 afinfo
= rcu_dereference(xfrm_policy_afinfo
[AF_INET
]);
2528 net
->xfrm
.xfrm4_dst_ops
= *afinfo
->dst_ops
;
2529 #if IS_ENABLED(CONFIG_IPV6)
2530 afinfo
= rcu_dereference(xfrm_policy_afinfo
[AF_INET6
]);
2532 net
->xfrm
.xfrm6_dst_ops
= *afinfo
->dst_ops
;
2537 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
2539 struct net_device
*dev
= ptr
;
2543 xfrm_garbage_collect(dev_net(dev
));
2548 static struct notifier_block xfrm_dev_notifier
= {
2549 .notifier_call
= xfrm_dev_event
,
2552 #ifdef CONFIG_XFRM_STATISTICS
2553 static int __net_init
xfrm_statistics_init(struct net
*net
)
2557 if (snmp_mib_init((void __percpu
**)net
->mib
.xfrm_statistics
,
2558 sizeof(struct linux_xfrm_mib
),
2559 __alignof__(struct linux_xfrm_mib
)) < 0)
2561 rv
= xfrm_proc_init(net
);
2563 snmp_mib_free((void __percpu
**)net
->mib
.xfrm_statistics
);
2567 static void xfrm_statistics_fini(struct net
*net
)
2569 xfrm_proc_fini(net
);
2570 snmp_mib_free((void __percpu
**)net
->mib
.xfrm_statistics
);
2573 static int __net_init
xfrm_statistics_init(struct net
*net
)
2578 static void xfrm_statistics_fini(struct net
*net
)
2583 static int __net_init
xfrm_policy_init(struct net
*net
)
2585 unsigned int hmask
, sz
;
2588 if (net_eq(net
, &init_net
))
2589 xfrm_dst_cache
= kmem_cache_create("xfrm_dst_cache",
2590 sizeof(struct xfrm_dst
),
2591 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2595 sz
= (hmask
+1) * sizeof(struct hlist_head
);
2597 net
->xfrm
.policy_byidx
= xfrm_hash_alloc(sz
);
2598 if (!net
->xfrm
.policy_byidx
)
2600 net
->xfrm
.policy_idx_hmask
= hmask
;
2602 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2603 struct xfrm_policy_hash
*htab
;
2605 net
->xfrm
.policy_count
[dir
] = 0;
2606 INIT_HLIST_HEAD(&net
->xfrm
.policy_inexact
[dir
]);
2608 htab
= &net
->xfrm
.policy_bydst
[dir
];
2609 htab
->table
= xfrm_hash_alloc(sz
);
2612 htab
->hmask
= hmask
;
2615 INIT_LIST_HEAD(&net
->xfrm
.policy_all
);
2616 INIT_WORK(&net
->xfrm
.policy_hash_work
, xfrm_hash_resize
);
2617 if (net_eq(net
, &init_net
))
2618 register_netdevice_notifier(&xfrm_dev_notifier
);
2622 for (dir
--; dir
>= 0; dir
--) {
2623 struct xfrm_policy_hash
*htab
;
2625 htab
= &net
->xfrm
.policy_bydst
[dir
];
2626 xfrm_hash_free(htab
->table
, sz
);
2628 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
2633 static void xfrm_policy_fini(struct net
*net
)
2635 struct xfrm_audit audit_info
;
2639 flush_work(&net
->xfrm
.policy_hash_work
);
2640 #ifdef CONFIG_XFRM_SUB_POLICY
2641 audit_info
.loginuid
= INVALID_UID
;
2642 audit_info
.sessionid
= -1;
2643 audit_info
.secid
= 0;
2644 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_SUB
, &audit_info
);
2646 audit_info
.loginuid
= INVALID_UID
;
2647 audit_info
.sessionid
= -1;
2648 audit_info
.secid
= 0;
2649 xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, &audit_info
);
2651 WARN_ON(!list_empty(&net
->xfrm
.policy_all
));
2653 for (dir
= 0; dir
< XFRM_POLICY_MAX
* 2; dir
++) {
2654 struct xfrm_policy_hash
*htab
;
2656 WARN_ON(!hlist_empty(&net
->xfrm
.policy_inexact
[dir
]));
2658 htab
= &net
->xfrm
.policy_bydst
[dir
];
2659 sz
= (htab
->hmask
+ 1);
2660 WARN_ON(!hlist_empty(htab
->table
));
2661 xfrm_hash_free(htab
->table
, sz
);
2664 sz
= (net
->xfrm
.policy_idx_hmask
+ 1) * sizeof(struct hlist_head
);
2665 WARN_ON(!hlist_empty(net
->xfrm
.policy_byidx
));
2666 xfrm_hash_free(net
->xfrm
.policy_byidx
, sz
);
2669 static int __net_init
xfrm_net_init(struct net
*net
)
2673 rv
= xfrm_statistics_init(net
);
2675 goto out_statistics
;
2676 rv
= xfrm_state_init(net
);
2679 rv
= xfrm_policy_init(net
);
2682 xfrm_dst_ops_init(net
);
2683 rv
= xfrm_sysctl_init(net
);
2689 xfrm_policy_fini(net
);
2691 xfrm_state_fini(net
);
2693 xfrm_statistics_fini(net
);
2698 static void __net_exit
xfrm_net_exit(struct net
*net
)
2700 xfrm_sysctl_fini(net
);
2701 xfrm_policy_fini(net
);
2702 xfrm_state_fini(net
);
2703 xfrm_statistics_fini(net
);
2706 static struct pernet_operations __net_initdata xfrm_net_ops
= {
2707 .init
= xfrm_net_init
,
2708 .exit
= xfrm_net_exit
,
2711 void __init
xfrm_init(void)
2713 register_pernet_subsys(&xfrm_net_ops
);
2717 #ifdef CONFIG_AUDITSYSCALL
2718 static void xfrm_audit_common_policyinfo(struct xfrm_policy
*xp
,
2719 struct audit_buffer
*audit_buf
)
2721 struct xfrm_sec_ctx
*ctx
= xp
->security
;
2722 struct xfrm_selector
*sel
= &xp
->selector
;
2725 audit_log_format(audit_buf
, " sec_alg=%u sec_doi=%u sec_obj=%s",
2726 ctx
->ctx_alg
, ctx
->ctx_doi
, ctx
->ctx_str
);
2728 switch(sel
->family
) {
2730 audit_log_format(audit_buf
, " src=%pI4", &sel
->saddr
.a4
);
2731 if (sel
->prefixlen_s
!= 32)
2732 audit_log_format(audit_buf
, " src_prefixlen=%d",
2734 audit_log_format(audit_buf
, " dst=%pI4", &sel
->daddr
.a4
);
2735 if (sel
->prefixlen_d
!= 32)
2736 audit_log_format(audit_buf
, " dst_prefixlen=%d",
2740 audit_log_format(audit_buf
, " src=%pI6", sel
->saddr
.a6
);
2741 if (sel
->prefixlen_s
!= 128)
2742 audit_log_format(audit_buf
, " src_prefixlen=%d",
2744 audit_log_format(audit_buf
, " dst=%pI6", sel
->daddr
.a6
);
2745 if (sel
->prefixlen_d
!= 128)
2746 audit_log_format(audit_buf
, " dst_prefixlen=%d",
2752 void xfrm_audit_policy_add(struct xfrm_policy
*xp
, int result
,
2753 kuid_t auid
, u32 sessionid
, u32 secid
)
2755 struct audit_buffer
*audit_buf
;
2757 audit_buf
= xfrm_audit_start("SPD-add");
2758 if (audit_buf
== NULL
)
2760 xfrm_audit_helper_usrinfo(auid
, sessionid
, secid
, audit_buf
);
2761 audit_log_format(audit_buf
, " res=%u", result
);
2762 xfrm_audit_common_policyinfo(xp
, audit_buf
);
2763 audit_log_end(audit_buf
);
2765 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add
);
2767 void xfrm_audit_policy_delete(struct xfrm_policy
*xp
, int result
,
2768 kuid_t auid
, u32 sessionid
, u32 secid
)
2770 struct audit_buffer
*audit_buf
;
2772 audit_buf
= xfrm_audit_start("SPD-delete");
2773 if (audit_buf
== NULL
)
2775 xfrm_audit_helper_usrinfo(auid
, sessionid
, secid
, audit_buf
);
2776 audit_log_format(audit_buf
, " res=%u", result
);
2777 xfrm_audit_common_policyinfo(xp
, audit_buf
);
2778 audit_log_end(audit_buf
);
2780 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete
);
2783 #ifdef CONFIG_XFRM_MIGRATE
2784 static bool xfrm_migrate_selector_match(const struct xfrm_selector
*sel_cmp
,
2785 const struct xfrm_selector
*sel_tgt
)
2787 if (sel_cmp
->proto
== IPSEC_ULPROTO_ANY
) {
2788 if (sel_tgt
->family
== sel_cmp
->family
&&
2789 xfrm_addr_cmp(&sel_tgt
->daddr
, &sel_cmp
->daddr
,
2790 sel_cmp
->family
) == 0 &&
2791 xfrm_addr_cmp(&sel_tgt
->saddr
, &sel_cmp
->saddr
,
2792 sel_cmp
->family
) == 0 &&
2793 sel_tgt
->prefixlen_d
== sel_cmp
->prefixlen_d
&&
2794 sel_tgt
->prefixlen_s
== sel_cmp
->prefixlen_s
) {
2798 if (memcmp(sel_tgt
, sel_cmp
, sizeof(*sel_tgt
)) == 0) {
2805 static struct xfrm_policy
* xfrm_migrate_policy_find(const struct xfrm_selector
*sel
,
2808 struct xfrm_policy
*pol
, *ret
= NULL
;
2809 struct hlist_node
*entry
;
2810 struct hlist_head
*chain
;
2813 read_lock_bh(&xfrm_policy_lock
);
2814 chain
= policy_hash_direct(&init_net
, &sel
->daddr
, &sel
->saddr
, sel
->family
, dir
);
2815 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
2816 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2817 pol
->type
== type
) {
2819 priority
= ret
->priority
;
2823 chain
= &init_net
.xfrm
.policy_inexact
[dir
];
2824 hlist_for_each_entry(pol
, entry
, chain
, bydst
) {
2825 if (xfrm_migrate_selector_match(sel
, &pol
->selector
) &&
2826 pol
->type
== type
&&
2827 pol
->priority
< priority
) {
2836 read_unlock_bh(&xfrm_policy_lock
);
2841 static int migrate_tmpl_match(const struct xfrm_migrate
*m
, const struct xfrm_tmpl
*t
)
2845 if (t
->mode
== m
->mode
&& t
->id
.proto
== m
->proto
&&
2846 (m
->reqid
== 0 || t
->reqid
== m
->reqid
)) {
2848 case XFRM_MODE_TUNNEL
:
2849 case XFRM_MODE_BEET
:
2850 if (xfrm_addr_cmp(&t
->id
.daddr
, &m
->old_daddr
,
2851 m
->old_family
) == 0 &&
2852 xfrm_addr_cmp(&t
->saddr
, &m
->old_saddr
,
2853 m
->old_family
) == 0) {
2857 case XFRM_MODE_TRANSPORT
:
2858 /* in case of transport mode, template does not store
2859 any IP addresses, hence we just compare mode and
2870 /* update endpoint address(es) of template(s) */
2871 static int xfrm_policy_migrate(struct xfrm_policy
*pol
,
2872 struct xfrm_migrate
*m
, int num_migrate
)
2874 struct xfrm_migrate
*mp
;
2877 write_lock_bh(&pol
->lock
);
2878 if (unlikely(pol
->walk
.dead
)) {
2879 /* target policy has been deleted */
2880 write_unlock_bh(&pol
->lock
);
2884 for (i
= 0; i
< pol
->xfrm_nr
; i
++) {
2885 for (j
= 0, mp
= m
; j
< num_migrate
; j
++, mp
++) {
2886 if (!migrate_tmpl_match(mp
, &pol
->xfrm_vec
[i
]))
2889 if (pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_TUNNEL
&&
2890 pol
->xfrm_vec
[i
].mode
!= XFRM_MODE_BEET
)
2892 /* update endpoints */
2893 memcpy(&pol
->xfrm_vec
[i
].id
.daddr
, &mp
->new_daddr
,
2894 sizeof(pol
->xfrm_vec
[i
].id
.daddr
));
2895 memcpy(&pol
->xfrm_vec
[i
].saddr
, &mp
->new_saddr
,
2896 sizeof(pol
->xfrm_vec
[i
].saddr
));
2897 pol
->xfrm_vec
[i
].encap_family
= mp
->new_family
;
2899 atomic_inc(&pol
->genid
);
2903 write_unlock_bh(&pol
->lock
);
2911 static int xfrm_migrate_check(const struct xfrm_migrate
*m
, int num_migrate
)
2915 if (num_migrate
< 1 || num_migrate
> XFRM_MAX_DEPTH
)
2918 for (i
= 0; i
< num_migrate
; i
++) {
2919 if ((xfrm_addr_cmp(&m
[i
].old_daddr
, &m
[i
].new_daddr
,
2920 m
[i
].old_family
) == 0) &&
2921 (xfrm_addr_cmp(&m
[i
].old_saddr
, &m
[i
].new_saddr
,
2922 m
[i
].old_family
) == 0))
2924 if (xfrm_addr_any(&m
[i
].new_daddr
, m
[i
].new_family
) ||
2925 xfrm_addr_any(&m
[i
].new_saddr
, m
[i
].new_family
))
2928 /* check if there is any duplicated entry */
2929 for (j
= i
+ 1; j
< num_migrate
; j
++) {
2930 if (!memcmp(&m
[i
].old_daddr
, &m
[j
].old_daddr
,
2931 sizeof(m
[i
].old_daddr
)) &&
2932 !memcmp(&m
[i
].old_saddr
, &m
[j
].old_saddr
,
2933 sizeof(m
[i
].old_saddr
)) &&
2934 m
[i
].proto
== m
[j
].proto
&&
2935 m
[i
].mode
== m
[j
].mode
&&
2936 m
[i
].reqid
== m
[j
].reqid
&&
2937 m
[i
].old_family
== m
[j
].old_family
)
2945 int xfrm_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2946 struct xfrm_migrate
*m
, int num_migrate
,
2947 struct xfrm_kmaddress
*k
)
2949 int i
, err
, nx_cur
= 0, nx_new
= 0;
2950 struct xfrm_policy
*pol
= NULL
;
2951 struct xfrm_state
*x
, *xc
;
2952 struct xfrm_state
*x_cur
[XFRM_MAX_DEPTH
];
2953 struct xfrm_state
*x_new
[XFRM_MAX_DEPTH
];
2954 struct xfrm_migrate
*mp
;
2956 if ((err
= xfrm_migrate_check(m
, num_migrate
)) < 0)
2959 /* Stage 1 - find policy */
2960 if ((pol
= xfrm_migrate_policy_find(sel
, dir
, type
)) == NULL
) {
2965 /* Stage 2 - find and update state(s) */
2966 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2967 if ((x
= xfrm_migrate_state_find(mp
))) {
2970 if ((xc
= xfrm_state_migrate(x
, mp
))) {
2980 /* Stage 3 - update policy */
2981 if ((err
= xfrm_policy_migrate(pol
, m
, num_migrate
)) < 0)
2984 /* Stage 4 - delete old state(s) */
2986 xfrm_states_put(x_cur
, nx_cur
);
2987 xfrm_states_delete(x_cur
, nx_cur
);
2990 /* Stage 5 - announce */
2991 km_migrate(sel
, dir
, type
, m
, num_migrate
, k
);
3003 xfrm_states_put(x_cur
, nx_cur
);
3005 xfrm_states_delete(x_new
, nx_new
);
3009 EXPORT_SYMBOL(xfrm_migrate
);