nvme.h: add an enum for cns values
[linux-2.6-block.git] / net / xfrm / xfrm_policy.c
CommitLineData
a716c119 1/*
1da177e4
LT
2 * xfrm_policy.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
df71837d 13 *
1da177e4
LT
14 */
15
66cdb3ca 16#include <linux/err.h>
1da177e4
LT
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>
eb9c7ebe 24#include <linux/netfilter.h>
1da177e4 25#include <linux/module.h>
2518c7c2 26#include <linux/cache.h>
68277acc 27#include <linux/audit.h>
25ee3286 28#include <net/dst.h>
6ce74ec7 29#include <net/flow.h>
1da177e4
LT
30#include <net/xfrm.h>
31#include <net/ip.h>
558f82ef
MN
32#ifdef CONFIG_XFRM_STATISTICS
33#include <net/snmp.h>
34#endif
1da177e4 35
44e36b42
DM
36#include "xfrm_hash.h"
37
a0073fe1
SK
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
41
b8c203b2
SK
42struct xfrm_flo {
43 struct dst_entry *dst_orig;
44 u8 flags;
45};
46
418a99ac
PJ
47static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
48static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
49 __read_mostly;
1da177e4 50
e18b890b 51static struct kmem_cache *xfrm_dst_cache __read_mostly;
30846090 52static __read_mostly seqcount_t xfrm_policy_hash_generation;
1da177e4 53
25ee3286 54static void xfrm_init_pmtu(struct dst_entry *dst);
80c802f3 55static int stale_bundle(struct dst_entry *dst);
12fdb4d3 56static int xfrm_bundle_ok(struct xfrm_dst *xdst);
a0073fe1 57static void xfrm_policy_queue_process(unsigned long arg);
1da177e4 58
12bfa8bd 59static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
29fa0b30
WY
60static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
61 int dir);
62
e37cc8ad
FW
63static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
64{
65 return atomic_inc_not_zero(&policy->refcnt);
66}
67
bc9b35ad 68static inline bool
200ce96e 69__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
77681021 70{
7e1dc7b6
DM
71 const struct flowi4 *fl4 = &fl->u.ip4;
72
26bff940
AD
73 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
74 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
7e1dc7b6
DM
75 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
76 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
77 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
78 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
77681021
AM
79}
80
bc9b35ad 81static inline bool
200ce96e 82__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
77681021 83{
7e1dc7b6
DM
84 const struct flowi6 *fl6 = &fl->u.ip6;
85
86 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
87 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
88 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
89 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
90 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
91 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
77681021
AM
92}
93
bc9b35ad
DM
94bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
95 unsigned short family)
77681021
AM
96{
97 switch (family) {
98 case AF_INET:
99 return __xfrm4_selector_match(sel, fl);
100 case AF_INET6:
101 return __xfrm6_selector_match(sel, fl);
102 }
bc9b35ad 103 return false;
77681021
AM
104}
105
ef8531b6
ED
106static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
107{
108 struct xfrm_policy_afinfo *afinfo;
109
110 if (unlikely(family >= NPROTO))
111 return NULL;
112 rcu_read_lock();
113 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
114 if (unlikely(!afinfo))
115 rcu_read_unlock();
116 return afinfo;
117}
118
119static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
120{
121 rcu_read_unlock();
122}
123
42a7b32b
DA
124static inline struct dst_entry *__xfrm_dst_lookup(struct net *net,
125 int tos, int oif,
6418c4e0
DM
126 const xfrm_address_t *saddr,
127 const xfrm_address_t *daddr,
9bb182a7
YH
128 int family)
129{
130 struct xfrm_policy_afinfo *afinfo;
131 struct dst_entry *dst;
132
133 afinfo = xfrm_policy_get_afinfo(family);
134 if (unlikely(afinfo == NULL))
135 return ERR_PTR(-EAFNOSUPPORT);
136
42a7b32b 137 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr);
9bb182a7
YH
138
139 xfrm_policy_put_afinfo(afinfo);
140
141 return dst;
142}
143
42a7b32b
DA
144static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
145 int tos, int oif,
9bb182a7
YH
146 xfrm_address_t *prev_saddr,
147 xfrm_address_t *prev_daddr,
25ee3286 148 int family)
1da177e4 149{
c5b3cf46 150 struct net *net = xs_net(x);
66cdb3ca
HX
151 xfrm_address_t *saddr = &x->props.saddr;
152 xfrm_address_t *daddr = &x->id.daddr;
66cdb3ca
HX
153 struct dst_entry *dst;
154
9bb182a7 155 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
66cdb3ca 156 saddr = x->coaddr;
9bb182a7
YH
157 daddr = prev_daddr;
158 }
159 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
160 saddr = prev_saddr;
66cdb3ca 161 daddr = x->coaddr;
9bb182a7 162 }
1da177e4 163
42a7b32b 164 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family);
9bb182a7
YH
165
166 if (!IS_ERR(dst)) {
167 if (prev_saddr != saddr)
168 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
169 if (prev_daddr != daddr)
170 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
171 }
1da177e4 172
66cdb3ca 173 return dst;
1da177e4 174}
1da177e4 175
1da177e4
LT
176static inline unsigned long make_jiffies(long secs)
177{
178 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
179 return MAX_SCHEDULE_TIMEOUT-1;
180 else
a716c119 181 return secs*HZ;
1da177e4
LT
182}
183
184static void xfrm_policy_timer(unsigned long data)
185{
3e94c2dc 186 struct xfrm_policy *xp = (struct xfrm_policy *)data;
9d729f72 187 unsigned long now = get_seconds();
1da177e4
LT
188 long next = LONG_MAX;
189 int warn = 0;
190 int dir;
191
192 read_lock(&xp->lock);
193
ea2dea9d 194 if (unlikely(xp->walk.dead))
1da177e4
LT
195 goto out;
196
77d8d7a6 197 dir = xfrm_policy_id2dir(xp->index);
1da177e4
LT
198
199 if (xp->lft.hard_add_expires_seconds) {
200 long tmo = xp->lft.hard_add_expires_seconds +
201 xp->curlft.add_time - now;
202 if (tmo <= 0)
203 goto expired;
204 if (tmo < next)
205 next = tmo;
206 }
207 if (xp->lft.hard_use_expires_seconds) {
208 long tmo = xp->lft.hard_use_expires_seconds +
209 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
210 if (tmo <= 0)
211 goto expired;
212 if (tmo < next)
213 next = tmo;
214 }
215 if (xp->lft.soft_add_expires_seconds) {
216 long tmo = xp->lft.soft_add_expires_seconds +
217 xp->curlft.add_time - now;
218 if (tmo <= 0) {
219 warn = 1;
220 tmo = XFRM_KM_TIMEOUT;
221 }
222 if (tmo < next)
223 next = tmo;
224 }
225 if (xp->lft.soft_use_expires_seconds) {
226 long tmo = xp->lft.soft_use_expires_seconds +
227 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
228 if (tmo <= 0) {
229 warn = 1;
230 tmo = XFRM_KM_TIMEOUT;
231 }
232 if (tmo < next)
233 next = tmo;
234 }
235
236 if (warn)
6c5c8ca7 237 km_policy_expired(xp, dir, 0, 0);
1da177e4
LT
238 if (next != LONG_MAX &&
239 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
240 xfrm_pol_hold(xp);
241
242out:
243 read_unlock(&xp->lock);
244 xfrm_pol_put(xp);
245 return;
246
247expired:
248 read_unlock(&xp->lock);
4666faab 249 if (!xfrm_policy_delete(xp, dir))
6c5c8ca7 250 km_policy_expired(xp, dir, 1, 0);
1da177e4
LT
251 xfrm_pol_put(xp);
252}
253
fe1a5f03
TT
254static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
255{
256 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
257
258 if (unlikely(pol->walk.dead))
259 flo = NULL;
260 else
261 xfrm_pol_hold(pol);
262
263 return flo;
264}
265
266static int xfrm_policy_flo_check(struct flow_cache_object *flo)
267{
268 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
269
270 return !pol->walk.dead;
271}
272
273static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
274{
275 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
276}
277
278static const struct flow_cache_ops xfrm_policy_fc_ops = {
279 .get = xfrm_policy_flo_get,
280 .check = xfrm_policy_flo_check,
281 .delete = xfrm_policy_flo_delete,
282};
1da177e4
LT
283
284/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
285 * SPD calls.
286 */
287
0331b1f3 288struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
1da177e4
LT
289{
290 struct xfrm_policy *policy;
291
0da974f4 292 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
1da177e4
LT
293
294 if (policy) {
0331b1f3 295 write_pnet(&policy->xp_net, net);
12a169e7 296 INIT_LIST_HEAD(&policy->walk.all);
2518c7c2
DM
297 INIT_HLIST_NODE(&policy->bydst);
298 INIT_HLIST_NODE(&policy->byidx);
1da177e4 299 rwlock_init(&policy->lock);
2518c7c2 300 atomic_set(&policy->refcnt, 1);
a0073fe1 301 skb_queue_head_init(&policy->polq.hold_queue);
b24b8a24
PE
302 setup_timer(&policy->timer, xfrm_policy_timer,
303 (unsigned long)policy);
a0073fe1
SK
304 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
305 (unsigned long)policy);
fe1a5f03 306 policy->flo.ops = &xfrm_policy_fc_ops;
1da177e4
LT
307 }
308 return policy;
309}
310EXPORT_SYMBOL(xfrm_policy_alloc);
311
56f04730
ED
312static void xfrm_policy_destroy_rcu(struct rcu_head *head)
313{
314 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
315
316 security_xfrm_policy_free(policy->security);
317 kfree(policy);
318}
319
1da177e4
LT
320/* Destroy xfrm_policy: descendant resources must be released to this moment. */
321
64c31b3f 322void xfrm_policy_destroy(struct xfrm_policy *policy)
1da177e4 323{
12a169e7 324 BUG_ON(!policy->walk.dead);
1da177e4 325
0659eea9 326 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
1da177e4
LT
327 BUG();
328
56f04730 329 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
1da177e4 330}
64c31b3f 331EXPORT_SYMBOL(xfrm_policy_destroy);
1da177e4 332
1da177e4
LT
333/* Rule must be locked. Release descentant resources, announce
334 * entry dead. The rule must be unlinked from lists to the moment.
335 */
336
337static void xfrm_policy_kill(struct xfrm_policy *policy)
338{
12a169e7 339 policy->walk.dead = 1;
1da177e4 340
285ead17 341 atomic_inc(&policy->genid);
1da177e4 342
e7d8f6cb
SK
343 if (del_timer(&policy->polq.hold_timer))
344 xfrm_pol_put(policy);
1ee5e667 345 skb_queue_purge(&policy->polq.hold_queue);
a0073fe1 346
285ead17
TT
347 if (del_timer(&policy->timer))
348 xfrm_pol_put(policy);
349
350 xfrm_pol_put(policy);
1da177e4
LT
351}
352
2518c7c2
DM
353static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
354
e92303f8 355static inline unsigned int idx_hash(struct net *net, u32 index)
2518c7c2 356{
e92303f8 357 return __idx_hash(index, net->xfrm.policy_idx_hmask);
2518c7c2
DM
358}
359
b58555f1
CG
360/* calculate policy hash thresholds */
361static void __get_hash_thresh(struct net *net,
362 unsigned short family, int dir,
363 u8 *dbits, u8 *sbits)
364{
365 switch (family) {
366 case AF_INET:
367 *dbits = net->xfrm.policy_bydst[dir].dbits4;
368 *sbits = net->xfrm.policy_bydst[dir].sbits4;
369 break;
370
371 case AF_INET6:
372 *dbits = net->xfrm.policy_bydst[dir].dbits6;
373 *sbits = net->xfrm.policy_bydst[dir].sbits6;
374 break;
375
376 default:
377 *dbits = 0;
378 *sbits = 0;
379 }
380}
381
5f803b58
DM
382static struct hlist_head *policy_hash_bysel(struct net *net,
383 const struct xfrm_selector *sel,
384 unsigned short family, int dir)
2518c7c2 385{
1121994c 386 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
b58555f1
CG
387 unsigned int hash;
388 u8 dbits;
389 u8 sbits;
390
391 __get_hash_thresh(net, family, dir, &dbits, &sbits);
392 hash = __sel_hash(sel, family, hmask, dbits, sbits);
2518c7c2 393
e1e551bc
FW
394 if (hash == hmask + 1)
395 return &net->xfrm.policy_inexact[dir];
396
397 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
398 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
2518c7c2
DM
399}
400
5f803b58
DM
401static struct hlist_head *policy_hash_direct(struct net *net,
402 const xfrm_address_t *daddr,
403 const xfrm_address_t *saddr,
404 unsigned short family, int dir)
2518c7c2 405{
1121994c 406 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
b58555f1
CG
407 unsigned int hash;
408 u8 dbits;
409 u8 sbits;
410
411 __get_hash_thresh(net, family, dir, &dbits, &sbits);
412 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
2518c7c2 413
e1e551bc
FW
414 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
415 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
2518c7c2
DM
416}
417
b58555f1
CG
418static void xfrm_dst_hash_transfer(struct net *net,
419 struct hlist_head *list,
2518c7c2 420 struct hlist_head *ndsttable,
b58555f1
CG
421 unsigned int nhashmask,
422 int dir)
2518c7c2 423{
b67bfe0d 424 struct hlist_node *tmp, *entry0 = NULL;
2518c7c2 425 struct xfrm_policy *pol;
b791160b 426 unsigned int h0 = 0;
b58555f1
CG
427 u8 dbits;
428 u8 sbits;
2518c7c2 429
b791160b 430redo:
b67bfe0d 431 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
2518c7c2
DM
432 unsigned int h;
433
b58555f1 434 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
2518c7c2 435 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
b58555f1 436 pol->family, nhashmask, dbits, sbits);
b791160b 437 if (!entry0) {
a5eefc1d
FW
438 hlist_del_rcu(&pol->bydst);
439 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
b791160b
YH
440 h0 = h;
441 } else {
442 if (h != h0)
443 continue;
a5eefc1d
FW
444 hlist_del_rcu(&pol->bydst);
445 hlist_add_behind_rcu(&pol->bydst, entry0);
b791160b 446 }
b67bfe0d 447 entry0 = &pol->bydst;
b791160b
YH
448 }
449 if (!hlist_empty(list)) {
450 entry0 = NULL;
451 goto redo;
2518c7c2
DM
452 }
453}
454
455static void xfrm_idx_hash_transfer(struct hlist_head *list,
456 struct hlist_head *nidxtable,
457 unsigned int nhashmask)
458{
b67bfe0d 459 struct hlist_node *tmp;
2518c7c2
DM
460 struct xfrm_policy *pol;
461
b67bfe0d 462 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
2518c7c2
DM
463 unsigned int h;
464
465 h = __idx_hash(pol->index, nhashmask);
466 hlist_add_head(&pol->byidx, nidxtable+h);
467 }
468}
469
470static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
471{
472 return ((old_hmask + 1) << 1) - 1;
473}
474
66caf628 475static void xfrm_bydst_resize(struct net *net, int dir)
2518c7c2 476{
66caf628 477 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
2518c7c2
DM
478 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
479 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
44e36b42 480 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
e1e551bc 481 struct hlist_head *odst;
2518c7c2
DM
482 int i;
483
484 if (!ndst)
485 return;
486
9d0380df 487 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
30846090
FW
488 write_seqcount_begin(&xfrm_policy_hash_generation);
489
490 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
491 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
2518c7c2 492
e1e551bc
FW
493 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
494 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
495
2518c7c2 496 for (i = hmask; i >= 0; i--)
b58555f1 497 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
2518c7c2 498
e1e551bc 499 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
66caf628 500 net->xfrm.policy_bydst[dir].hmask = nhashmask;
2518c7c2 501
30846090 502 write_seqcount_end(&xfrm_policy_hash_generation);
9d0380df 503 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
2518c7c2 504
e1e551bc
FW
505 synchronize_rcu();
506
44e36b42 507 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
2518c7c2
DM
508}
509
66caf628 510static void xfrm_byidx_resize(struct net *net, int total)
2518c7c2 511{
66caf628 512 unsigned int hmask = net->xfrm.policy_idx_hmask;
2518c7c2
DM
513 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
514 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
66caf628 515 struct hlist_head *oidx = net->xfrm.policy_byidx;
44e36b42 516 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
2518c7c2
DM
517 int i;
518
519 if (!nidx)
520 return;
521
9d0380df 522 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
2518c7c2
DM
523
524 for (i = hmask; i >= 0; i--)
525 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
526
66caf628
AD
527 net->xfrm.policy_byidx = nidx;
528 net->xfrm.policy_idx_hmask = nhashmask;
2518c7c2 529
9d0380df 530 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
2518c7c2 531
44e36b42 532 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
2518c7c2
DM
533}
534
66caf628 535static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
2518c7c2 536{
66caf628
AD
537 unsigned int cnt = net->xfrm.policy_count[dir];
538 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
2518c7c2
DM
539
540 if (total)
541 *total += cnt;
542
543 if ((hmask + 1) < xfrm_policy_hashmax &&
544 cnt > hmask)
545 return 1;
546
547 return 0;
548}
549
66caf628 550static inline int xfrm_byidx_should_resize(struct net *net, int total)
2518c7c2 551{
66caf628 552 unsigned int hmask = net->xfrm.policy_idx_hmask;
2518c7c2
DM
553
554 if ((hmask + 1) < xfrm_policy_hashmax &&
555 total > hmask)
556 return 1;
557
558 return 0;
559}
560
e071041b 561void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
ecfd6b18 562{
e071041b
AD
563 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
564 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
565 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
566 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
567 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
568 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
569 si->spdhcnt = net->xfrm.policy_idx_hmask;
ecfd6b18 570 si->spdhmcnt = xfrm_policy_hashmax;
ecfd6b18
JHS
571}
572EXPORT_SYMBOL(xfrm_spd_getinfo);
2518c7c2 573
ecfd6b18 574static DEFINE_MUTEX(hash_resize_mutex);
66caf628 575static void xfrm_hash_resize(struct work_struct *work)
2518c7c2 576{
66caf628 577 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
2518c7c2
DM
578 int dir, total;
579
580 mutex_lock(&hash_resize_mutex);
581
582 total = 0;
53c2e285 583 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
66caf628
AD
584 if (xfrm_bydst_should_resize(net, dir, &total))
585 xfrm_bydst_resize(net, dir);
2518c7c2 586 }
66caf628
AD
587 if (xfrm_byidx_should_resize(net, total))
588 xfrm_byidx_resize(net, total);
2518c7c2
DM
589
590 mutex_unlock(&hash_resize_mutex);
591}
592
880a6fab
CG
593static void xfrm_hash_rebuild(struct work_struct *work)
594{
595 struct net *net = container_of(work, struct net,
596 xfrm.policy_hthresh.work);
597 unsigned int hmask;
598 struct xfrm_policy *pol;
599 struct xfrm_policy *policy;
600 struct hlist_head *chain;
601 struct hlist_head *odst;
602 struct hlist_node *newpos;
603 int i;
604 int dir;
605 unsigned seq;
606 u8 lbits4, rbits4, lbits6, rbits6;
607
608 mutex_lock(&hash_resize_mutex);
609
610 /* read selector prefixlen thresholds */
611 do {
612 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
613
614 lbits4 = net->xfrm.policy_hthresh.lbits4;
615 rbits4 = net->xfrm.policy_hthresh.rbits4;
616 lbits6 = net->xfrm.policy_hthresh.lbits6;
617 rbits6 = net->xfrm.policy_hthresh.rbits6;
618 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
619
9d0380df 620 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
880a6fab
CG
621
622 /* reset the bydst and inexact table in all directions */
53c2e285 623 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
880a6fab
CG
624 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
625 hmask = net->xfrm.policy_bydst[dir].hmask;
626 odst = net->xfrm.policy_bydst[dir].table;
627 for (i = hmask; i >= 0; i--)
628 INIT_HLIST_HEAD(odst + i);
629 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
630 /* dir out => dst = remote, src = local */
631 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
632 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
633 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
634 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
635 } else {
636 /* dir in/fwd => dst = local, src = remote */
637 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
638 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
639 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
640 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
641 }
642 }
643
644 /* re-insert all policies by order of creation */
645 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
6916fb3b
TB
646 if (xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
647 /* skip socket policies */
648 continue;
649 }
880a6fab
CG
650 newpos = NULL;
651 chain = policy_hash_bysel(net, &policy->selector,
652 policy->family,
653 xfrm_policy_id2dir(policy->index));
654 hlist_for_each_entry(pol, chain, bydst) {
655 if (policy->priority >= pol->priority)
656 newpos = &pol->bydst;
657 else
658 break;
659 }
660 if (newpos)
661 hlist_add_behind(&policy->bydst, newpos);
662 else
663 hlist_add_head(&policy->bydst, chain);
664 }
665
9d0380df 666 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
880a6fab
CG
667
668 mutex_unlock(&hash_resize_mutex);
669}
670
671void xfrm_policy_hash_rebuild(struct net *net)
672{
673 schedule_work(&net->xfrm.policy_hthresh.work);
674}
675EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
676
1da177e4
LT
677/* Generate new index... KAME seems to generate them ordered by cost
678 * of an absolute inpredictability of ordering of rules. This will not pass. */
e682adf0 679static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
1da177e4 680{
1da177e4
LT
681 static u32 idx_generator;
682
683 for (;;) {
2518c7c2
DM
684 struct hlist_head *list;
685 struct xfrm_policy *p;
686 u32 idx;
687 int found;
688
e682adf0
FD
689 if (!index) {
690 idx = (idx_generator | dir);
691 idx_generator += 8;
692 } else {
693 idx = index;
694 index = 0;
695 }
696
1da177e4
LT
697 if (idx == 0)
698 idx = 8;
1121994c 699 list = net->xfrm.policy_byidx + idx_hash(net, idx);
2518c7c2 700 found = 0;
b67bfe0d 701 hlist_for_each_entry(p, list, byidx) {
2518c7c2
DM
702 if (p->index == idx) {
703 found = 1;
1da177e4 704 break;
2518c7c2 705 }
1da177e4 706 }
2518c7c2 707 if (!found)
1da177e4
LT
708 return idx;
709 }
710}
711
2518c7c2
DM
712static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
713{
714 u32 *p1 = (u32 *) s1;
715 u32 *p2 = (u32 *) s2;
716 int len = sizeof(struct xfrm_selector) / sizeof(u32);
717 int i;
718
719 for (i = 0; i < len; i++) {
720 if (p1[i] != p2[i])
721 return 1;
722 }
723
724 return 0;
725}
726
a0073fe1
SK
727static void xfrm_policy_requeue(struct xfrm_policy *old,
728 struct xfrm_policy *new)
729{
730 struct xfrm_policy_queue *pq = &old->polq;
731 struct sk_buff_head list;
732
de2ad486
LR
733 if (skb_queue_empty(&pq->hold_queue))
734 return;
735
a0073fe1
SK
736 __skb_queue_head_init(&list);
737
738 spin_lock_bh(&pq->hold_queue.lock);
739 skb_queue_splice_init(&pq->hold_queue, &list);
e7d8f6cb
SK
740 if (del_timer(&pq->hold_timer))
741 xfrm_pol_put(old);
a0073fe1
SK
742 spin_unlock_bh(&pq->hold_queue.lock);
743
a0073fe1
SK
744 pq = &new->polq;
745
746 spin_lock_bh(&pq->hold_queue.lock);
747 skb_queue_splice(&list, &pq->hold_queue);
748 pq->timeout = XFRM_QUEUE_TMO_MIN;
e7d8f6cb
SK
749 if (!mod_timer(&pq->hold_timer, jiffies))
750 xfrm_pol_hold(new);
a0073fe1
SK
751 spin_unlock_bh(&pq->hold_queue.lock);
752}
753
7cb8a939
SK
754static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
755 struct xfrm_policy *pol)
756{
757 u32 mark = policy->mark.v & policy->mark.m;
758
759 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
760 return true;
761
762 if ((mark & pol->mark.m) == pol->mark.v &&
763 policy->priority == pol->priority)
764 return true;
765
766 return false;
767}
768
1da177e4
LT
769int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
770{
1121994c 771 struct net *net = xp_net(policy);
2518c7c2
DM
772 struct xfrm_policy *pol;
773 struct xfrm_policy *delpol;
774 struct hlist_head *chain;
b67bfe0d 775 struct hlist_node *newpos;
1da177e4 776
9d0380df 777 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1121994c 778 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
2518c7c2
DM
779 delpol = NULL;
780 newpos = NULL;
b67bfe0d 781 hlist_for_each_entry(pol, chain, bydst) {
a6c7ab55 782 if (pol->type == policy->type &&
2518c7c2 783 !selector_cmp(&pol->selector, &policy->selector) &&
7cb8a939 784 xfrm_policy_mark_match(policy, pol) &&
a6c7ab55
HX
785 xfrm_sec_ctx_match(pol->security, policy->security) &&
786 !WARN_ON(delpol)) {
1da177e4 787 if (excl) {
9d0380df 788 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1da177e4
LT
789 return -EEXIST;
790 }
1da177e4
LT
791 delpol = pol;
792 if (policy->priority > pol->priority)
793 continue;
794 } else if (policy->priority >= pol->priority) {
a6c7ab55 795 newpos = &pol->bydst;
1da177e4
LT
796 continue;
797 }
1da177e4
LT
798 if (delpol)
799 break;
1da177e4
LT
800 }
801 if (newpos)
1d023284 802 hlist_add_behind(&policy->bydst, newpos);
2518c7c2
DM
803 else
804 hlist_add_head(&policy->bydst, chain);
12bfa8bd 805 __xfrm_policy_link(policy, dir);
ca925cf1 806 atomic_inc(&net->xfrm.flow_cache_genid);
ca4c3fc2 807
808 /* After previous checking, family can either be AF_INET or AF_INET6 */
809 if (policy->family == AF_INET)
810 rt_genid_bump_ipv4(net);
811 else
812 rt_genid_bump_ipv6(net);
813
a0073fe1
SK
814 if (delpol) {
815 xfrm_policy_requeue(delpol, policy);
29fa0b30 816 __xfrm_policy_unlink(delpol, dir);
a0073fe1 817 }
e682adf0 818 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
1121994c 819 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
9d729f72 820 policy->curlft.add_time = get_seconds();
1da177e4
LT
821 policy->curlft.use_time = 0;
822 if (!mod_timer(&policy->timer, jiffies + HZ))
823 xfrm_pol_hold(policy);
9d0380df 824 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1da177e4 825
9b78a82c 826 if (delpol)
1da177e4 827 xfrm_policy_kill(delpol);
1121994c
AD
828 else if (xfrm_bydst_should_resize(net, dir, NULL))
829 schedule_work(&net->xfrm.policy_hash_work);
9b78a82c 830
1da177e4
LT
831 return 0;
832}
833EXPORT_SYMBOL(xfrm_policy_insert);
834
8ca2e93b
JHS
835struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
836 int dir, struct xfrm_selector *sel,
ef41aaa0
EP
837 struct xfrm_sec_ctx *ctx, int delete,
838 int *err)
1da177e4 839{
2518c7c2
DM
840 struct xfrm_policy *pol, *ret;
841 struct hlist_head *chain;
1da177e4 842
ef41aaa0 843 *err = 0;
9d0380df 844 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
8d1211a6 845 chain = policy_hash_bysel(net, sel, sel->family, dir);
2518c7c2 846 ret = NULL;
b67bfe0d 847 hlist_for_each_entry(pol, chain, bydst) {
2518c7c2 848 if (pol->type == type &&
34f8d884 849 (mark & pol->mark.m) == pol->mark.v &&
2518c7c2
DM
850 !selector_cmp(sel, &pol->selector) &&
851 xfrm_sec_ctx_match(ctx, pol->security)) {
1da177e4 852 xfrm_pol_hold(pol);
2518c7c2 853 if (delete) {
03e1ad7b
PM
854 *err = security_xfrm_policy_delete(
855 pol->security);
ef41aaa0 856 if (*err) {
9d0380df 857 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
ef41aaa0
EP
858 return pol;
859 }
29fa0b30 860 __xfrm_policy_unlink(pol, dir);
2518c7c2
DM
861 }
862 ret = pol;
1da177e4
LT
863 break;
864 }
865 }
9d0380df 866 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1da177e4 867
fe1a5f03 868 if (ret && delete)
2518c7c2 869 xfrm_policy_kill(ret);
2518c7c2 870 return ret;
1da177e4 871}
df71837d 872EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
1da177e4 873
8ca2e93b
JHS
874struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
875 int dir, u32 id, int delete, int *err)
1da177e4 876{
2518c7c2
DM
877 struct xfrm_policy *pol, *ret;
878 struct hlist_head *chain;
1da177e4 879
b5505c6e
HX
880 *err = -ENOENT;
881 if (xfrm_policy_id2dir(id) != dir)
882 return NULL;
883
ef41aaa0 884 *err = 0;
9d0380df 885 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
8d1211a6 886 chain = net->xfrm.policy_byidx + idx_hash(net, id);
2518c7c2 887 ret = NULL;
b67bfe0d 888 hlist_for_each_entry(pol, chain, byidx) {
34f8d884
JHS
889 if (pol->type == type && pol->index == id &&
890 (mark & pol->mark.m) == pol->mark.v) {
1da177e4 891 xfrm_pol_hold(pol);
2518c7c2 892 if (delete) {
03e1ad7b
PM
893 *err = security_xfrm_policy_delete(
894 pol->security);
ef41aaa0 895 if (*err) {
9d0380df 896 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
ef41aaa0
EP
897 return pol;
898 }
29fa0b30 899 __xfrm_policy_unlink(pol, dir);
2518c7c2
DM
900 }
901 ret = pol;
1da177e4
LT
902 break;
903 }
904 }
9d0380df 905 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1da177e4 906
fe1a5f03 907 if (ret && delete)
2518c7c2 908 xfrm_policy_kill(ret);
2518c7c2 909 return ret;
1da177e4
LT
910}
911EXPORT_SYMBOL(xfrm_policy_byid);
912
4aa2e62c
JL
913#ifdef CONFIG_SECURITY_NETWORK_XFRM
914static inline int
2e71029e 915xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
1da177e4 916{
4aa2e62c
JL
917 int dir, err = 0;
918
919 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
920 struct xfrm_policy *pol;
4aa2e62c
JL
921 int i;
922
b67bfe0d 923 hlist_for_each_entry(pol,
33ffbbd5 924 &net->xfrm.policy_inexact[dir], bydst) {
4aa2e62c
JL
925 if (pol->type != type)
926 continue;
03e1ad7b 927 err = security_xfrm_policy_delete(pol->security);
4aa2e62c 928 if (err) {
2e71029e 929 xfrm_audit_policy_delete(pol, 0, task_valid);
4aa2e62c
JL
930 return err;
931 }
7dc12d6d 932 }
33ffbbd5 933 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
b67bfe0d 934 hlist_for_each_entry(pol,
33ffbbd5 935 net->xfrm.policy_bydst[dir].table + i,
4aa2e62c
JL
936 bydst) {
937 if (pol->type != type)
938 continue;
03e1ad7b
PM
939 err = security_xfrm_policy_delete(
940 pol->security);
4aa2e62c 941 if (err) {
ab5f5e8b 942 xfrm_audit_policy_delete(pol, 0,
2e71029e 943 task_valid);
4aa2e62c
JL
944 return err;
945 }
946 }
947 }
948 }
949 return err;
950}
951#else
952static inline int
2e71029e 953xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
4aa2e62c
JL
954{
955 return 0;
956}
957#endif
958
2e71029e 959int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
4aa2e62c 960{
2f1eb65f 961 int dir, err = 0, cnt = 0;
1da177e4 962
9d0380df 963 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
4aa2e62c 964
2e71029e 965 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
4aa2e62c
JL
966 if (err)
967 goto out;
968
1da177e4 969 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
2518c7c2 970 struct xfrm_policy *pol;
29fa0b30 971 int i;
2518c7c2
DM
972
973 again1:
b67bfe0d 974 hlist_for_each_entry(pol,
33ffbbd5 975 &net->xfrm.policy_inexact[dir], bydst) {
2518c7c2
DM
976 if (pol->type != type)
977 continue;
ea2dea9d 978 __xfrm_policy_unlink(pol, dir);
9d0380df 979 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
ea2dea9d 980 cnt++;
1da177e4 981
2e71029e 982 xfrm_audit_policy_delete(pol, 1, task_valid);
161a09e7 983
2518c7c2 984 xfrm_policy_kill(pol);
1da177e4 985
4141b36a 986 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
2518c7c2
DM
987 goto again1;
988 }
989
33ffbbd5 990 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
2518c7c2 991 again2:
b67bfe0d 992 hlist_for_each_entry(pol,
33ffbbd5 993 net->xfrm.policy_bydst[dir].table + i,
2518c7c2
DM
994 bydst) {
995 if (pol->type != type)
996 continue;
ea2dea9d 997 __xfrm_policy_unlink(pol, dir);
9d0380df 998 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
ea2dea9d 999 cnt++;
2518c7c2 1000
2e71029e 1001 xfrm_audit_policy_delete(pol, 1, task_valid);
2518c7c2
DM
1002 xfrm_policy_kill(pol);
1003
9d0380df 1004 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
2518c7c2
DM
1005 goto again2;
1006 }
1da177e4 1007 }
2518c7c2 1008
1da177e4 1009 }
2f1eb65f
JHS
1010 if (!cnt)
1011 err = -ESRCH;
4aa2e62c 1012out:
9d0380df 1013 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
4aa2e62c 1014 return err;
1da177e4
LT
1015}
1016EXPORT_SYMBOL(xfrm_policy_flush);
1017
cdcbca7c 1018int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
4c563f76 1019 int (*func)(struct xfrm_policy *, int, int, void*),
1da177e4
LT
1020 void *data)
1021{
12a169e7
HX
1022 struct xfrm_policy *pol;
1023 struct xfrm_policy_walk_entry *x;
4c563f76
TT
1024 int error = 0;
1025
1026 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1027 walk->type != XFRM_POLICY_TYPE_ANY)
1028 return -EINVAL;
1da177e4 1029
12a169e7 1030 if (list_empty(&walk->walk.all) && walk->seq != 0)
4c563f76
TT
1031 return 0;
1032
9d0380df 1033 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
12a169e7 1034 if (list_empty(&walk->walk.all))
cdcbca7c 1035 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
12a169e7 1036 else
80077702
LR
1037 x = list_first_entry(&walk->walk.all,
1038 struct xfrm_policy_walk_entry, all);
1039
cdcbca7c 1040 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
12a169e7 1041 if (x->dead)
4c563f76 1042 continue;
12a169e7
HX
1043 pol = container_of(x, struct xfrm_policy, walk);
1044 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1045 walk->type != pol->type)
1046 continue;
1047 error = func(pol, xfrm_policy_id2dir(pol->index),
1048 walk->seq, data);
1049 if (error) {
1050 list_move_tail(&walk->walk.all, &x->all);
1051 goto out;
2518c7c2 1052 }
12a169e7 1053 walk->seq++;
1da177e4 1054 }
12a169e7 1055 if (walk->seq == 0) {
baf5d743
JHS
1056 error = -ENOENT;
1057 goto out;
1058 }
12a169e7 1059 list_del_init(&walk->walk.all);
1da177e4 1060out:
9d0380df 1061 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1da177e4
LT
1062 return error;
1063}
1064EXPORT_SYMBOL(xfrm_policy_walk);
1065
12a169e7
HX
1066void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1067{
1068 INIT_LIST_HEAD(&walk->walk.all);
1069 walk->walk.dead = 1;
1070 walk->type = type;
1071 walk->seq = 0;
1072}
1073EXPORT_SYMBOL(xfrm_policy_walk_init);
1074
283bc9f3 1075void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
12a169e7
HX
1076{
1077 if (list_empty(&walk->walk.all))
1078 return;
1079
9d0380df 1080 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
12a169e7 1081 list_del(&walk->walk.all);
9d0380df 1082 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
12a169e7
HX
1083}
1084EXPORT_SYMBOL(xfrm_policy_walk_done);
1085
134b0fc5
JM
1086/*
1087 * Find policy to apply to this flow.
1088 *
1089 * Returns 0 if policy found, else an -errno.
1090 */
f299d557
DM
1091static int xfrm_policy_match(const struct xfrm_policy *pol,
1092 const struct flowi *fl,
2518c7c2 1093 u8 type, u16 family, int dir)
1da177e4 1094{
f299d557 1095 const struct xfrm_selector *sel = &pol->selector;
bc9b35ad
DM
1096 int ret = -ESRCH;
1097 bool match;
1da177e4 1098
2518c7c2 1099 if (pol->family != family ||
1d28f42c 1100 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
2518c7c2 1101 pol->type != type)
134b0fc5 1102 return ret;
1da177e4 1103
2518c7c2 1104 match = xfrm_selector_match(sel, fl, family);
134b0fc5 1105 if (match)
1d28f42c 1106 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
03e1ad7b 1107 dir);
2518c7c2 1108
134b0fc5 1109 return ret;
2518c7c2 1110}
1da177e4 1111
52479b62 1112static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
062cdb43 1113 const struct flowi *fl,
2518c7c2
DM
1114 u16 family, u8 dir)
1115{
134b0fc5 1116 int err;
2518c7c2 1117 struct xfrm_policy *pol, *ret;
0b597e7e 1118 const xfrm_address_t *daddr, *saddr;
2518c7c2 1119 struct hlist_head *chain;
30846090
FW
1120 unsigned int sequence;
1121 u32 priority;
df71837d 1122
2518c7c2
DM
1123 daddr = xfrm_flowi_daddr(fl, family);
1124 saddr = xfrm_flowi_saddr(fl, family);
1125 if (unlikely(!daddr || !saddr))
1126 return NULL;
1127
a7c44247 1128 rcu_read_lock();
30846090
FW
1129 retry:
1130 do {
1131 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1132 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1133 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1134
1135 priority = ~0U;
2518c7c2 1136 ret = NULL;
a5eefc1d 1137 hlist_for_each_entry_rcu(pol, chain, bydst) {
134b0fc5
JM
1138 err = xfrm_policy_match(pol, fl, type, family, dir);
1139 if (err) {
1140 if (err == -ESRCH)
1141 continue;
1142 else {
1143 ret = ERR_PTR(err);
1144 goto fail;
1145 }
1146 } else {
2518c7c2 1147 ret = pol;
acba48e1 1148 priority = ret->priority;
2518c7c2
DM
1149 break;
1150 }
1151 }
52479b62 1152 chain = &net->xfrm.policy_inexact[dir];
a5eefc1d 1153 hlist_for_each_entry_rcu(pol, chain, bydst) {
8faf491e
LR
1154 if ((pol->priority >= priority) && ret)
1155 break;
1156
134b0fc5
JM
1157 err = xfrm_policy_match(pol, fl, type, family, dir);
1158 if (err) {
1159 if (err == -ESRCH)
1160 continue;
1161 else {
1162 ret = ERR_PTR(err);
1163 goto fail;
1164 }
8faf491e 1165 } else {
acba48e1
DM
1166 ret = pol;
1167 break;
1da177e4
LT
1168 }
1169 }
586f2eb4 1170
30846090
FW
1171 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1172 goto retry;
1173
e37cc8ad
FW
1174 if (ret && !xfrm_pol_hold_rcu(ret))
1175 goto retry;
134b0fc5 1176fail:
a7c44247 1177 rcu_read_unlock();
4e81bb83 1178
2518c7c2 1179 return ret;
4e81bb83
MN
1180}
1181
80c802f3 1182static struct xfrm_policy *
73ff93cd 1183__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
80c802f3
TT
1184{
1185#ifdef CONFIG_XFRM_SUB_POLICY
1186 struct xfrm_policy *pol;
1187
1188 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1189 if (pol != NULL)
1190 return pol;
1191#endif
1192 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1193}
1194
b5fb82c4
BZ
1195static int flow_to_policy_dir(int dir)
1196{
1197 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1198 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1199 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1200 return dir;
1201
1202 switch (dir) {
1203 default:
1204 case FLOW_DIR_IN:
1205 return XFRM_POLICY_IN;
1206 case FLOW_DIR_OUT:
1207 return XFRM_POLICY_OUT;
1208 case FLOW_DIR_FWD:
1209 return XFRM_POLICY_FWD;
1210 }
1211}
1212
fe1a5f03 1213static struct flow_cache_object *
dee9f4bc 1214xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
fe1a5f03 1215 u8 dir, struct flow_cache_object *old_obj, void *ctx)
4e81bb83
MN
1216{
1217 struct xfrm_policy *pol;
fe1a5f03
TT
1218
1219 if (old_obj)
1220 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
4e81bb83 1221
b5fb82c4 1222 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
80c802f3 1223 if (IS_ERR_OR_NULL(pol))
fe1a5f03 1224 return ERR_CAST(pol);
fe1a5f03 1225
fe1a5f03
TT
1226 /* Resolver returns two references:
1227 * one for cache and one for caller of flow_cache_lookup() */
1228 xfrm_pol_hold(pol);
1229
1230 return &pol->flo;
1da177e4
LT
1231}
1232
df71837d
TJ
1233static inline int policy_to_flow_dir(int dir)
1234{
1235 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
a716c119
YH
1236 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1237 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1238 return dir;
1239 switch (dir) {
1240 default:
1241 case XFRM_POLICY_IN:
1242 return FLOW_DIR_IN;
1243 case XFRM_POLICY_OUT:
1244 return FLOW_DIR_OUT;
1245 case XFRM_POLICY_FWD:
1246 return FLOW_DIR_FWD;
3ff50b79 1247 }
df71837d
TJ
1248}
1249
6f9c9615 1250static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
dee9f4bc 1251 const struct flowi *fl)
1da177e4
LT
1252{
1253 struct xfrm_policy *pol;
1254
d188ba86 1255 rcu_read_lock();
ae33786f 1256 again:
d188ba86
ED
1257 pol = rcu_dereference(sk->sk_policy[dir]);
1258 if (pol != NULL) {
bc9b35ad
DM
1259 bool match = xfrm_selector_match(&pol->selector, fl,
1260 sk->sk_family);
a716c119 1261 int err = 0;
df71837d 1262
3bccfbc7 1263 if (match) {
34f8d884
JHS
1264 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1265 pol = NULL;
1266 goto out;
1267 }
03e1ad7b 1268 err = security_xfrm_policy_lookup(pol->security,
1d28f42c 1269 fl->flowi_secid,
03e1ad7b 1270 policy_to_flow_dir(dir));
ae33786f
FW
1271 if (!err && !xfrm_pol_hold_rcu(pol))
1272 goto again;
3bccfbc7
VY
1273 else if (err == -ESRCH)
1274 pol = NULL;
1275 else
1276 pol = ERR_PTR(err);
1277 } else
1da177e4
LT
1278 pol = NULL;
1279 }
34f8d884 1280out:
d188ba86 1281 rcu_read_unlock();
1da177e4
LT
1282 return pol;
1283}
1284
1285static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1286{
98806f75 1287 struct net *net = xp_net(pol);
4e81bb83 1288
98806f75 1289 list_add(&pol->walk.all, &net->xfrm.policy_all);
98806f75 1290 net->xfrm.policy_count[dir]++;
1da177e4
LT
1291 xfrm_pol_hold(pol);
1292}
1293
1294static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1295 int dir)
1296{
98806f75
AD
1297 struct net *net = xp_net(pol);
1298
53c2e285 1299 if (list_empty(&pol->walk.all))
2518c7c2 1300 return NULL;
1da177e4 1301
53c2e285
HX
1302 /* Socket policies are not hashed. */
1303 if (!hlist_unhashed(&pol->bydst)) {
a5eefc1d 1304 hlist_del_rcu(&pol->bydst);
53c2e285
HX
1305 hlist_del(&pol->byidx);
1306 }
1307
1308 list_del_init(&pol->walk.all);
98806f75 1309 net->xfrm.policy_count[dir]--;
2518c7c2
DM
1310
1311 return pol;
1da177e4
LT
1312}
1313
53c2e285
HX
1314static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1315{
1316 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1317}
1318
1319static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1320{
1321 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1322}
1323
4666faab 1324int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1da177e4 1325{
283bc9f3
FD
1326 struct net *net = xp_net(pol);
1327
9d0380df 1328 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1da177e4 1329 pol = __xfrm_policy_unlink(pol, dir);
9d0380df 1330 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1da177e4 1331 if (pol) {
1da177e4 1332 xfrm_policy_kill(pol);
4666faab 1333 return 0;
1da177e4 1334 }
4666faab 1335 return -ENOENT;
1da177e4 1336}
a70fcb0b 1337EXPORT_SYMBOL(xfrm_policy_delete);
1da177e4
LT
1338
1339int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1340{
1121994c 1341 struct net *net = xp_net(pol);
1da177e4
LT
1342 struct xfrm_policy *old_pol;
1343
4e81bb83
MN
1344#ifdef CONFIG_XFRM_SUB_POLICY
1345 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1346 return -EINVAL;
1347#endif
1348
9d0380df 1349 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
d188ba86
ED
1350 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1351 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
1da177e4 1352 if (pol) {
9d729f72 1353 pol->curlft.add_time = get_seconds();
e682adf0 1354 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
53c2e285 1355 xfrm_sk_policy_link(pol, dir);
1da177e4 1356 }
d188ba86 1357 rcu_assign_pointer(sk->sk_policy[dir], pol);
a0073fe1
SK
1358 if (old_pol) {
1359 if (pol)
1360 xfrm_policy_requeue(old_pol, pol);
1361
ea2dea9d
TT
1362 /* Unlinking succeeds always. This is the only function
1363 * allowed to delete or replace socket policy.
1364 */
53c2e285 1365 xfrm_sk_policy_unlink(old_pol, dir);
a0073fe1 1366 }
9d0380df 1367 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1da177e4
LT
1368
1369 if (old_pol) {
1370 xfrm_policy_kill(old_pol);
1371 }
1372 return 0;
1373}
1374
d3e40a9f 1375static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
1da177e4 1376{
0331b1f3 1377 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
283bc9f3 1378 struct net *net = xp_net(old);
1da177e4
LT
1379
1380 if (newp) {
1381 newp->selector = old->selector;
03e1ad7b
PM
1382 if (security_xfrm_policy_clone(old->security,
1383 &newp->security)) {
df71837d
TJ
1384 kfree(newp);
1385 return NULL; /* ENOMEM */
1386 }
1da177e4
LT
1387 newp->lft = old->lft;
1388 newp->curlft = old->curlft;
fb977e2c 1389 newp->mark = old->mark;
1da177e4
LT
1390 newp->action = old->action;
1391 newp->flags = old->flags;
1392 newp->xfrm_nr = old->xfrm_nr;
1393 newp->index = old->index;
4e81bb83 1394 newp->type = old->type;
1da177e4
LT
1395 memcpy(newp->xfrm_vec, old->xfrm_vec,
1396 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
9d0380df 1397 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
53c2e285 1398 xfrm_sk_policy_link(newp, dir);
9d0380df 1399 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1da177e4
LT
1400 xfrm_pol_put(newp);
1401 }
1402 return newp;
1403}
1404
d188ba86 1405int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
1da177e4 1406{
d188ba86
ED
1407 const struct xfrm_policy *p;
1408 struct xfrm_policy *np;
1409 int i, ret = 0;
1410
1411 rcu_read_lock();
1412 for (i = 0; i < 2; i++) {
1413 p = rcu_dereference(osk->sk_policy[i]);
1414 if (p) {
1415 np = clone_policy(p, i);
1416 if (unlikely(!np)) {
1417 ret = -ENOMEM;
1418 break;
1419 }
1420 rcu_assign_pointer(sk->sk_policy[i], np);
1421 }
1422 }
1423 rcu_read_unlock();
1424 return ret;
1da177e4
LT
1425}
1426
a1e59abf 1427static int
42a7b32b
DA
1428xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1429 xfrm_address_t *remote, unsigned short family)
a1e59abf
PM
1430{
1431 int err;
1432 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1433
1434 if (unlikely(afinfo == NULL))
1435 return -EINVAL;
42a7b32b 1436 err = afinfo->get_saddr(net, oif, local, remote);
a1e59abf
PM
1437 xfrm_policy_put_afinfo(afinfo);
1438 return err;
1439}
1440
1da177e4
LT
1441/* Resolve list of templates for the flow, given policy. */
1442
1443static int
a6c2e611
DM
1444xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1445 struct xfrm_state **xfrm, unsigned short family)
1da177e4 1446{
fbda33b2 1447 struct net *net = xp_net(policy);
1da177e4
LT
1448 int nx;
1449 int i, error;
1450 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1451 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
a1e59abf 1452 xfrm_address_t tmp;
1da177e4 1453
9b7a787d 1454 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
1da177e4
LT
1455 struct xfrm_state *x;
1456 xfrm_address_t *remote = daddr;
1457 xfrm_address_t *local = saddr;
1458 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1459
48b8d783
JK
1460 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1461 tmpl->mode == XFRM_MODE_BEET) {
1da177e4
LT
1462 remote = &tmpl->id.daddr;
1463 local = &tmpl->saddr;
8444cf71 1464 if (xfrm_addr_any(local, tmpl->encap_family)) {
42a7b32b
DA
1465 error = xfrm_get_saddr(net, fl->flowi_oif,
1466 &tmp, remote,
1467 tmpl->encap_family);
a1e59abf
PM
1468 if (error)
1469 goto fail;
1470 local = &tmp;
1471 }
1da177e4
LT
1472 }
1473
1474 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1475
1476 if (x && x->km.state == XFRM_STATE_VALID) {
1477 xfrm[nx++] = x;
1478 daddr = remote;
1479 saddr = local;
1480 continue;
1481 }
1482 if (x) {
1483 error = (x->km.state == XFRM_STATE_ERROR ?
1484 -EINVAL : -EAGAIN);
1485 xfrm_state_put(x);
42054569 1486 } else if (error == -ESRCH) {
a4322266 1487 error = -EAGAIN;
42054569 1488 }
1da177e4
LT
1489
1490 if (!tmpl->optional)
1491 goto fail;
1492 }
1493 return nx;
1494
1495fail:
9b7a787d 1496 for (nx--; nx >= 0; nx--)
1da177e4
LT
1497 xfrm_state_put(xfrm[nx]);
1498 return error;
1499}
1500
4e81bb83 1501static int
a6c2e611
DM
1502xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1503 struct xfrm_state **xfrm, unsigned short family)
4e81bb83 1504{
41a49cc3
MN
1505 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1506 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
4e81bb83
MN
1507 int cnx = 0;
1508 int error;
1509 int ret;
1510 int i;
1511
1512 for (i = 0; i < npols; i++) {
1513 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1514 error = -ENOBUFS;
1515 goto fail;
1516 }
41a49cc3
MN
1517
1518 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
4e81bb83
MN
1519 if (ret < 0) {
1520 error = ret;
1521 goto fail;
1522 } else
1523 cnx += ret;
1524 }
1525
41a49cc3
MN
1526 /* found states are sorted for outbound processing */
1527 if (npols > 1)
1528 xfrm_state_sort(xfrm, tpp, cnx, family);
1529
4e81bb83
MN
1530 return cnx;
1531
1532 fail:
9b7a787d 1533 for (cnx--; cnx >= 0; cnx--)
41a49cc3 1534 xfrm_state_put(tpp[cnx]);
4e81bb83
MN
1535 return error;
1536
1537}
1538
1da177e4
LT
1539/* Check that the bundle accepts the flow and its components are
1540 * still valid.
1541 */
1542
05d84025 1543static inline int xfrm_get_tos(const struct flowi *fl, int family)
25ee3286
HX
1544{
1545 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1546 int tos;
1da177e4 1547
25ee3286
HX
1548 if (!afinfo)
1549 return -EINVAL;
1550
1551 tos = afinfo->get_tos(fl);
1552
1553 xfrm_policy_put_afinfo(afinfo);
1554
1555 return tos;
1556}
1557
80c802f3
TT
1558static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1559{
1560 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1561 struct dst_entry *dst = &xdst->u.dst;
1562
1563 if (xdst->route == NULL) {
1564 /* Dummy bundle - if it has xfrms we were not
1565 * able to build bundle as template resolution failed.
1566 * It means we need to try again resolving. */
1567 if (xdst->num_xfrms > 0)
1568 return NULL;
a0073fe1
SK
1569 } else if (dst->flags & DST_XFRM_QUEUE) {
1570 return NULL;
80c802f3
TT
1571 } else {
1572 /* Real bundle */
1573 if (stale_bundle(dst))
1574 return NULL;
1575 }
1576
1577 dst_hold(dst);
1578 return flo;
1579}
1580
1581static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1582{
1583 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1584 struct dst_entry *dst = &xdst->u.dst;
1585
1586 if (!xdst->route)
1587 return 0;
1588 if (stale_bundle(dst))
1589 return 0;
1590
1591 return 1;
1592}
1593
1594static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1595{
1596 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1597 struct dst_entry *dst = &xdst->u.dst;
1598
1599 dst_free(dst);
1600}
1601
1602static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1603 .get = xfrm_bundle_flo_get,
1604 .check = xfrm_bundle_flo_check,
1605 .delete = xfrm_bundle_flo_delete,
1606};
1607
d7c7544c 1608static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
1da177e4 1609{
1da177e4 1610 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
d7c7544c 1611 struct dst_ops *dst_ops;
25ee3286
HX
1612 struct xfrm_dst *xdst;
1613
1614 if (!afinfo)
1615 return ERR_PTR(-EINVAL);
1616
d7c7544c
AD
1617 switch (family) {
1618 case AF_INET:
1619 dst_ops = &net->xfrm.xfrm4_dst_ops;
1620 break;
dfd56b8b 1621#if IS_ENABLED(CONFIG_IPV6)
d7c7544c
AD
1622 case AF_INET6:
1623 dst_ops = &net->xfrm.xfrm6_dst_ops;
1624 break;
1625#endif
1626 default:
1627 BUG();
1628 }
f5b0a874 1629 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
25ee3286 1630
d4cae562 1631 if (likely(xdst)) {
141e369d
SK
1632 struct dst_entry *dst = &xdst->u.dst;
1633
1634 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
0b150932 1635 xdst->flo.ops = &xfrm_bundle_fc_ops;
d4cae562 1636 } else
0b150932 1637 xdst = ERR_PTR(-ENOBUFS);
80c802f3 1638
d4cae562
MB
1639 xfrm_policy_put_afinfo(afinfo);
1640
25ee3286
HX
1641 return xdst;
1642}
1643
a1b05140
MN
1644static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1645 int nfheader_len)
1646{
1647 struct xfrm_policy_afinfo *afinfo =
1648 xfrm_policy_get_afinfo(dst->ops->family);
1649 int err;
1650
1651 if (!afinfo)
1652 return -EINVAL;
1653
1654 err = afinfo->init_path(path, dst, nfheader_len);
1655
1656 xfrm_policy_put_afinfo(afinfo);
1657
1658 return err;
1659}
1660
87c1e12b 1661static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
0c7b3eef 1662 const struct flowi *fl)
25ee3286
HX
1663{
1664 struct xfrm_policy_afinfo *afinfo =
1665 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1666 int err;
1667
1668 if (!afinfo)
1da177e4 1669 return -EINVAL;
25ee3286 1670
87c1e12b 1671 err = afinfo->fill_dst(xdst, dev, fl);
25ee3286 1672
1da177e4 1673 xfrm_policy_put_afinfo(afinfo);
25ee3286 1674
1da177e4
LT
1675 return err;
1676}
1677
80c802f3 1678
25ee3286
HX
1679/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1680 * all the metrics... Shortly, bundle a bundle.
1681 */
1682
1683static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1684 struct xfrm_state **xfrm, int nx,
98313ada 1685 const struct flowi *fl,
25ee3286
HX
1686 struct dst_entry *dst)
1687{
d7c7544c 1688 struct net *net = xp_net(policy);
25ee3286
HX
1689 unsigned long now = jiffies;
1690 struct net_device *dev;
43a4dea4 1691 struct xfrm_mode *inner_mode;
25ee3286
HX
1692 struct dst_entry *dst_prev = NULL;
1693 struct dst_entry *dst0 = NULL;
1694 int i = 0;
1695 int err;
1696 int header_len = 0;
a1b05140 1697 int nfheader_len = 0;
25ee3286
HX
1698 int trailer_len = 0;
1699 int tos;
1700 int family = policy->selector.family;
9bb182a7
YH
1701 xfrm_address_t saddr, daddr;
1702
1703 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
25ee3286
HX
1704
1705 tos = xfrm_get_tos(fl, family);
1706 err = tos;
1707 if (tos < 0)
1708 goto put_states;
1709
1710 dst_hold(dst);
1711
1712 for (; i < nx; i++) {
d7c7544c 1713 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
25ee3286
HX
1714 struct dst_entry *dst1 = &xdst->u.dst;
1715
1716 err = PTR_ERR(xdst);
1717 if (IS_ERR(xdst)) {
1718 dst_release(dst);
1719 goto put_states;
1720 }
1721
43a4dea4
SK
1722 if (xfrm[i]->sel.family == AF_UNSPEC) {
1723 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1724 xfrm_af2proto(family));
1725 if (!inner_mode) {
1726 err = -EAFNOSUPPORT;
1727 dst_release(dst);
1728 goto put_states;
1729 }
1730 } else
1731 inner_mode = xfrm[i]->inner_mode;
1732
25ee3286
HX
1733 if (!dst_prev)
1734 dst0 = dst1;
1735 else {
1736 dst_prev->child = dst_clone(dst1);
1737 dst1->flags |= DST_NOHASH;
1738 }
1739
1740 xdst->route = dst;
defb3519 1741 dst_copy_metrics(dst1, dst);
25ee3286
HX
1742
1743 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1744 family = xfrm[i]->props.family;
42a7b32b
DA
1745 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1746 &saddr, &daddr, family);
25ee3286
HX
1747 err = PTR_ERR(dst);
1748 if (IS_ERR(dst))
1749 goto put_states;
1750 } else
1751 dst_hold(dst);
1752
1753 dst1->xfrm = xfrm[i];
80c802f3 1754 xdst->xfrm_genid = xfrm[i]->genid;
25ee3286 1755
f5b0a874 1756 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
25ee3286
HX
1757 dst1->flags |= DST_HOST;
1758 dst1->lastuse = now;
1759
1760 dst1->input = dst_discard;
43a4dea4 1761 dst1->output = inner_mode->afinfo->output;
25ee3286
HX
1762
1763 dst1->next = dst_prev;
1764 dst_prev = dst1;
1765
1766 header_len += xfrm[i]->props.header_len;
a1b05140
MN
1767 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1768 nfheader_len += xfrm[i]->props.header_len;
25ee3286
HX
1769 trailer_len += xfrm[i]->props.trailer_len;
1770 }
1771
1772 dst_prev->child = dst;
1773 dst0->path = dst;
1774
1775 err = -ENODEV;
1776 dev = dst->dev;
1777 if (!dev)
1778 goto free_dst;
1779
a1b05140 1780 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
25ee3286
HX
1781 xfrm_init_pmtu(dst_prev);
1782
1783 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1784 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1785
87c1e12b 1786 err = xfrm_fill_dst(xdst, dev, fl);
25ee3286
HX
1787 if (err)
1788 goto free_dst;
1789
1790 dst_prev->header_len = header_len;
1791 dst_prev->trailer_len = trailer_len;
1792 header_len -= xdst->u.dst.xfrm->props.header_len;
1793 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1794 }
1795
1796out:
1797 return dst0;
1798
1799put_states:
1800 for (; i < nx; i++)
1801 xfrm_state_put(xfrm[i]);
1802free_dst:
1803 if (dst0)
1804 dst_free(dst0);
1805 dst0 = ERR_PTR(err);
1806 goto out;
1807}
1808
da7c224b 1809#ifdef CONFIG_XFRM_SUB_POLICY
be7928d2 1810static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
157bfc25
MN
1811{
1812 if (!*target) {
1813 *target = kmalloc(size, GFP_ATOMIC);
1814 if (!*target)
1815 return -ENOMEM;
1816 }
be7928d2 1817
157bfc25
MN
1818 memcpy(*target, src, size);
1819 return 0;
1820}
da7c224b 1821#endif
157bfc25 1822
be7928d2
DB
1823static int xfrm_dst_update_parent(struct dst_entry *dst,
1824 const struct xfrm_selector *sel)
157bfc25
MN
1825{
1826#ifdef CONFIG_XFRM_SUB_POLICY
1827 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1828 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1829 sel, sizeof(*sel));
1830#else
1831 return 0;
1832#endif
1833}
1834
be7928d2
DB
1835static int xfrm_dst_update_origin(struct dst_entry *dst,
1836 const struct flowi *fl)
157bfc25
MN
1837{
1838#ifdef CONFIG_XFRM_SUB_POLICY
1839 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1840 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1841#else
1842 return 0;
1843#endif
1844}
1da177e4 1845
73ff93cd 1846static int xfrm_expand_policies(const struct flowi *fl, u16 family,
80c802f3
TT
1847 struct xfrm_policy **pols,
1848 int *num_pols, int *num_xfrms)
1849{
1850 int i;
1851
1852 if (*num_pols == 0 || !pols[0]) {
1853 *num_pols = 0;
1854 *num_xfrms = 0;
1855 return 0;
1856 }
1857 if (IS_ERR(pols[0]))
1858 return PTR_ERR(pols[0]);
1859
1860 *num_xfrms = pols[0]->xfrm_nr;
1861
1862#ifdef CONFIG_XFRM_SUB_POLICY
1863 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1864 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1865 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1866 XFRM_POLICY_TYPE_MAIN,
1867 fl, family,
1868 XFRM_POLICY_OUT);
1869 if (pols[1]) {
1870 if (IS_ERR(pols[1])) {
1871 xfrm_pols_put(pols, *num_pols);
1872 return PTR_ERR(pols[1]);
1873 }
02d0892f 1874 (*num_pols)++;
80c802f3
TT
1875 (*num_xfrms) += pols[1]->xfrm_nr;
1876 }
1877 }
1878#endif
1879 for (i = 0; i < *num_pols; i++) {
1880 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1881 *num_xfrms = -1;
1882 break;
1883 }
1884 }
1885
1886 return 0;
1887
1888}
1889
1890static struct xfrm_dst *
1891xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
4ca2e685 1892 const struct flowi *fl, u16 family,
80c802f3
TT
1893 struct dst_entry *dst_orig)
1894{
1895 struct net *net = xp_net(pols[0]);
1896 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1897 struct dst_entry *dst;
1898 struct xfrm_dst *xdst;
1899 int err;
1900
1901 /* Try to instantiate a bundle */
1902 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
d809ec89
TT
1903 if (err <= 0) {
1904 if (err != 0 && err != -EAGAIN)
80c802f3
TT
1905 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1906 return ERR_PTR(err);
1907 }
1908
1909 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1910 if (IS_ERR(dst)) {
1911 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1912 return ERR_CAST(dst);
1913 }
1914
1915 xdst = (struct xfrm_dst *)dst;
1916 xdst->num_xfrms = err;
1917 if (num_pols > 1)
1918 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1919 else
1920 err = xfrm_dst_update_origin(dst, fl);
1921 if (unlikely(err)) {
1922 dst_free(dst);
1923 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1924 return ERR_PTR(err);
1925 }
1926
1927 xdst->num_pols = num_pols;
3e94c2dc 1928 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
80c802f3
TT
1929 xdst->policy_genid = atomic_read(&pols[0]->genid);
1930
1931 return xdst;
1932}
1933
a0073fe1
SK
1934static void xfrm_policy_queue_process(unsigned long arg)
1935{
a0073fe1
SK
1936 struct sk_buff *skb;
1937 struct sock *sk;
1938 struct dst_entry *dst;
a0073fe1 1939 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
3f5312ae 1940 struct net *net = xp_net(pol);
a0073fe1
SK
1941 struct xfrm_policy_queue *pq = &pol->polq;
1942 struct flowi fl;
1943 struct sk_buff_head list;
1944
1945 spin_lock(&pq->hold_queue.lock);
1946 skb = skb_peek(&pq->hold_queue);
2bb53e25
SK
1947 if (!skb) {
1948 spin_unlock(&pq->hold_queue.lock);
1949 goto out;
1950 }
a0073fe1
SK
1951 dst = skb_dst(skb);
1952 sk = skb->sk;
1953 xfrm_decode_session(skb, &fl, dst->ops->family);
1954 spin_unlock(&pq->hold_queue.lock);
1955
1956 dst_hold(dst->path);
3f5312ae 1957 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
a0073fe1
SK
1958 if (IS_ERR(dst))
1959 goto purge_queue;
1960
1961 if (dst->flags & DST_XFRM_QUEUE) {
1962 dst_release(dst);
1963
1964 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1965 goto purge_queue;
1966
1967 pq->timeout = pq->timeout << 1;
e7d8f6cb
SK
1968 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1969 xfrm_pol_hold(pol);
1970 goto out;
a0073fe1
SK
1971 }
1972
1973 dst_release(dst);
1974
1975 __skb_queue_head_init(&list);
1976
1977 spin_lock(&pq->hold_queue.lock);
1978 pq->timeout = 0;
1979 skb_queue_splice_init(&pq->hold_queue, &list);
1980 spin_unlock(&pq->hold_queue.lock);
1981
1982 while (!skb_queue_empty(&list)) {
1983 skb = __skb_dequeue(&list);
1984
1985 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1986 dst_hold(skb_dst(skb)->path);
3f5312ae 1987 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
a0073fe1 1988 if (IS_ERR(dst)) {
a0073fe1
SK
1989 kfree_skb(skb);
1990 continue;
1991 }
1992
1993 nf_reset(skb);
1994 skb_dst_drop(skb);
1995 skb_dst_set(skb, dst);
1996
13206b6b 1997 dst_output(net, skb->sk, skb);
a0073fe1
SK
1998 }
1999
e7d8f6cb
SK
2000out:
2001 xfrm_pol_put(pol);
a0073fe1
SK
2002 return;
2003
2004purge_queue:
2005 pq->timeout = 0;
1ee5e667 2006 skb_queue_purge(&pq->hold_queue);
e7d8f6cb 2007 xfrm_pol_put(pol);
a0073fe1
SK
2008}
2009
ede2059d 2010static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
a0073fe1
SK
2011{
2012 unsigned long sched_next;
2013 struct dst_entry *dst = skb_dst(skb);
2014 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
e7d8f6cb
SK
2015 struct xfrm_policy *pol = xdst->pols[0];
2016 struct xfrm_policy_queue *pq = &pol->polq;
4d53eff4 2017
39bb5e62 2018 if (unlikely(skb_fclone_busy(sk, skb))) {
4d53eff4
SK
2019 kfree_skb(skb);
2020 return 0;
2021 }
a0073fe1
SK
2022
2023 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2024 kfree_skb(skb);
2025 return -EAGAIN;
2026 }
2027
2028 skb_dst_force(skb);
a0073fe1
SK
2029
2030 spin_lock_bh(&pq->hold_queue.lock);
2031
2032 if (!pq->timeout)
2033 pq->timeout = XFRM_QUEUE_TMO_MIN;
2034
2035 sched_next = jiffies + pq->timeout;
2036
2037 if (del_timer(&pq->hold_timer)) {
2038 if (time_before(pq->hold_timer.expires, sched_next))
2039 sched_next = pq->hold_timer.expires;
e7d8f6cb 2040 xfrm_pol_put(pol);
a0073fe1
SK
2041 }
2042
2043 __skb_queue_tail(&pq->hold_queue, skb);
e7d8f6cb
SK
2044 if (!mod_timer(&pq->hold_timer, sched_next))
2045 xfrm_pol_hold(pol);
a0073fe1
SK
2046
2047 spin_unlock_bh(&pq->hold_queue.lock);
2048
2049 return 0;
2050}
2051
2052static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
b8c203b2 2053 struct xfrm_flo *xflo,
a0073fe1
SK
2054 const struct flowi *fl,
2055 int num_xfrms,
2056 u16 family)
2057{
2058 int err;
2059 struct net_device *dev;
b8c203b2 2060 struct dst_entry *dst;
a0073fe1
SK
2061 struct dst_entry *dst1;
2062 struct xfrm_dst *xdst;
2063
2064 xdst = xfrm_alloc_dst(net, family);
2065 if (IS_ERR(xdst))
2066 return xdst;
2067
b8c203b2
SK
2068 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2069 net->xfrm.sysctl_larval_drop ||
2070 num_xfrms <= 0)
a0073fe1
SK
2071 return xdst;
2072
b8c203b2 2073 dst = xflo->dst_orig;
a0073fe1
SK
2074 dst1 = &xdst->u.dst;
2075 dst_hold(dst);
2076 xdst->route = dst;
2077
2078 dst_copy_metrics(dst1, dst);
2079
2080 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2081 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2082 dst1->lastuse = jiffies;
2083
2084 dst1->input = dst_discard;
2085 dst1->output = xdst_queue_output;
2086
2087 dst_hold(dst);
2088 dst1->child = dst;
2089 dst1->path = dst;
2090
2091 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2092
2093 err = -ENODEV;
2094 dev = dst->dev;
2095 if (!dev)
2096 goto free_dst;
2097
2098 err = xfrm_fill_dst(xdst, dev, fl);
2099 if (err)
2100 goto free_dst;
2101
2102out:
2103 return xdst;
2104
2105free_dst:
2106 dst_release(dst1);
2107 xdst = ERR_PTR(err);
2108 goto out;
2109}
2110
80c802f3 2111static struct flow_cache_object *
dee9f4bc 2112xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
80c802f3
TT
2113 struct flow_cache_object *oldflo, void *ctx)
2114{
b8c203b2 2115 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
80c802f3
TT
2116 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2117 struct xfrm_dst *xdst, *new_xdst;
2118 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2119
2120 /* Check if the policies from old bundle are usable */
2121 xdst = NULL;
2122 if (oldflo) {
2123 xdst = container_of(oldflo, struct xfrm_dst, flo);
2124 num_pols = xdst->num_pols;
2125 num_xfrms = xdst->num_xfrms;
2126 pol_dead = 0;
2127 for (i = 0; i < num_pols; i++) {
2128 pols[i] = xdst->pols[i];
2129 pol_dead |= pols[i]->walk.dead;
2130 }
2131 if (pol_dead) {
2132 dst_free(&xdst->u.dst);
2133 xdst = NULL;
2134 num_pols = 0;
2135 num_xfrms = 0;
2136 oldflo = NULL;
2137 }
2138 }
2139
2140 /* Resolve policies to use if we couldn't get them from
2141 * previous cache entry */
2142 if (xdst == NULL) {
2143 num_pols = 1;
b5fb82c4
BZ
2144 pols[0] = __xfrm_policy_lookup(net, fl, family,
2145 flow_to_policy_dir(dir));
80c802f3
TT
2146 err = xfrm_expand_policies(fl, family, pols,
2147 &num_pols, &num_xfrms);
2148 if (err < 0)
2149 goto inc_error;
2150 if (num_pols == 0)
2151 return NULL;
2152 if (num_xfrms <= 0)
2153 goto make_dummy_bundle;
2154 }
2155
b8c203b2
SK
2156 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2157 xflo->dst_orig);
80c802f3
TT
2158 if (IS_ERR(new_xdst)) {
2159 err = PTR_ERR(new_xdst);
2160 if (err != -EAGAIN)
2161 goto error;
2162 if (oldflo == NULL)
2163 goto make_dummy_bundle;
2164 dst_hold(&xdst->u.dst);
2165 return oldflo;
d809ec89
TT
2166 } else if (new_xdst == NULL) {
2167 num_xfrms = 0;
2168 if (oldflo == NULL)
2169 goto make_dummy_bundle;
2170 xdst->num_xfrms = 0;
2171 dst_hold(&xdst->u.dst);
2172 return oldflo;
80c802f3
TT
2173 }
2174
2175 /* Kill the previous bundle */
2176 if (xdst) {
2177 /* The policies were stolen for newly generated bundle */
2178 xdst->num_pols = 0;
2179 dst_free(&xdst->u.dst);
2180 }
2181
2182 /* Flow cache does not have reference, it dst_free()'s,
2183 * but we do need to return one reference for original caller */
2184 dst_hold(&new_xdst->u.dst);
2185 return &new_xdst->flo;
2186
2187make_dummy_bundle:
2188 /* We found policies, but there's no bundles to instantiate:
2189 * either because the policy blocks, has no transformations or
2190 * we could not build template (no xfrm_states).*/
b8c203b2 2191 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
80c802f3
TT
2192 if (IS_ERR(xdst)) {
2193 xfrm_pols_put(pols, num_pols);
2194 return ERR_CAST(xdst);
2195 }
2196 xdst->num_pols = num_pols;
2197 xdst->num_xfrms = num_xfrms;
3e94c2dc 2198 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
80c802f3
TT
2199
2200 dst_hold(&xdst->u.dst);
2201 return &xdst->flo;
2202
2203inc_error:
2204 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2205error:
2206 if (xdst != NULL)
2207 dst_free(&xdst->u.dst);
2208 else
2209 xfrm_pols_put(pols, num_pols);
2210 return ERR_PTR(err);
2211}
1da177e4 2212
2774c131
DM
2213static struct dst_entry *make_blackhole(struct net *net, u16 family,
2214 struct dst_entry *dst_orig)
2215{
2216 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2217 struct dst_entry *ret;
2218
2219 if (!afinfo) {
2220 dst_release(dst_orig);
433a1954 2221 return ERR_PTR(-EINVAL);
2774c131
DM
2222 } else {
2223 ret = afinfo->blackhole_route(net, dst_orig);
2224 }
2225 xfrm_policy_put_afinfo(afinfo);
2226
2227 return ret;
2228}
2229
1da177e4
LT
2230/* Main function: finds/creates a bundle for given flow.
2231 *
2232 * At the moment we eat a raw IP route. Mostly to speed up lookups
2233 * on interfaces with disabled IPsec.
2234 */
452edd59
DM
2235struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2236 const struct flowi *fl,
6f9c9615 2237 const struct sock *sk, int flags)
1da177e4 2238{
4e81bb83 2239 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
80c802f3
TT
2240 struct flow_cache_object *flo;
2241 struct xfrm_dst *xdst;
452edd59 2242 struct dst_entry *dst, *route;
80c802f3 2243 u16 family = dst_orig->ops->family;
df71837d 2244 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
4b021628 2245 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
e0d1caa7 2246
80c802f3
TT
2247 dst = NULL;
2248 xdst = NULL;
2249 route = NULL;
4e81bb83 2250
bd5eb35f 2251 sk = sk_const_to_full_sk(sk);
f7944fb1 2252 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
80c802f3
TT
2253 num_pols = 1;
2254 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2255 err = xfrm_expand_policies(fl, family, pols,
2256 &num_pols, &num_xfrms);
2257 if (err < 0)
75b8c133 2258 goto dropdst;
80c802f3
TT
2259
2260 if (num_pols) {
2261 if (num_xfrms <= 0) {
2262 drop_pols = num_pols;
2263 goto no_transform;
2264 }
2265
2266 xdst = xfrm_resolve_and_create_bundle(
2267 pols, num_pols, fl,
2268 family, dst_orig);
2269 if (IS_ERR(xdst)) {
2270 xfrm_pols_put(pols, num_pols);
2271 err = PTR_ERR(xdst);
2272 goto dropdst;
d809ec89
TT
2273 } else if (xdst == NULL) {
2274 num_xfrms = 0;
2275 drop_pols = num_pols;
2276 goto no_transform;
80c802f3
TT
2277 }
2278
b7eea454
SK
2279 dst_hold(&xdst->u.dst);
2280 xdst->u.dst.flags |= DST_NOCACHE;
80c802f3 2281 route = xdst->route;
0aa64774 2282 }
3bccfbc7 2283 }
1da177e4 2284
80c802f3 2285 if (xdst == NULL) {
b8c203b2
SK
2286 struct xfrm_flo xflo;
2287
2288 xflo.dst_orig = dst_orig;
2289 xflo.flags = flags;
2290
1da177e4 2291 /* To accelerate a bit... */
2518c7c2 2292 if ((dst_orig->flags & DST_NOXFRM) ||
52479b62 2293 !net->xfrm.policy_count[XFRM_POLICY_OUT])
8b7817f3 2294 goto nopol;
1da177e4 2295
80c802f3 2296 flo = flow_cache_lookup(net, fl, family, dir,
b8c203b2 2297 xfrm_bundle_lookup, &xflo);
80c802f3
TT
2298 if (flo == NULL)
2299 goto nopol;
fe1a5f03 2300 if (IS_ERR(flo)) {
80c802f3 2301 err = PTR_ERR(flo);
75b8c133 2302 goto dropdst;
d66e37a9 2303 }
80c802f3
TT
2304 xdst = container_of(flo, struct xfrm_dst, flo);
2305
2306 num_pols = xdst->num_pols;
2307 num_xfrms = xdst->num_xfrms;
3e94c2dc 2308 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
80c802f3
TT
2309 route = xdst->route;
2310 }
2311
2312 dst = &xdst->u.dst;
2313 if (route == NULL && num_xfrms > 0) {
2314 /* The only case when xfrm_bundle_lookup() returns a
2315 * bundle with null route, is when the template could
2316 * not be resolved. It means policies are there, but
2317 * bundle could not be created, since we don't yet
2318 * have the xfrm_state's. We need to wait for KM to
2319 * negotiate new SA's or bail out with error.*/
2320 if (net->xfrm.sysctl_larval_drop) {
80c802f3 2321 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
ac37e251
W
2322 err = -EREMOTE;
2323 goto error;
80c802f3 2324 }
80c802f3 2325
5b8ef341 2326 err = -EAGAIN;
80c802f3
TT
2327
2328 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2329 goto error;
1da177e4
LT
2330 }
2331
80c802f3
TT
2332no_transform:
2333 if (num_pols == 0)
8b7817f3 2334 goto nopol;
1da177e4 2335
80c802f3
TT
2336 if ((flags & XFRM_LOOKUP_ICMP) &&
2337 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2338 err = -ENOENT;
8b7817f3 2339 goto error;
80c802f3 2340 }
8b7817f3 2341
80c802f3
TT
2342 for (i = 0; i < num_pols; i++)
2343 pols[i]->curlft.use_time = get_seconds();
8b7817f3 2344
80c802f3 2345 if (num_xfrms < 0) {
1da177e4 2346 /* Prohibit the flow */
59c9940e 2347 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
e104411b
PM
2348 err = -EPERM;
2349 goto error;
80c802f3
TT
2350 } else if (num_xfrms > 0) {
2351 /* Flow transformed */
80c802f3
TT
2352 dst_release(dst_orig);
2353 } else {
2354 /* Flow passes untransformed */
2355 dst_release(dst);
452edd59 2356 dst = dst_orig;
1da177e4 2357 }
80c802f3
TT
2358ok:
2359 xfrm_pols_put(pols, drop_pols);
0c183379
G
2360 if (dst && dst->xfrm &&
2361 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2362 dst->flags |= DST_XFRM_TUNNEL;
452edd59 2363 return dst;
1da177e4 2364
80c802f3 2365nopol:
452edd59
DM
2366 if (!(flags & XFRM_LOOKUP_ICMP)) {
2367 dst = dst_orig;
80c802f3 2368 goto ok;
452edd59 2369 }
80c802f3 2370 err = -ENOENT;
1da177e4 2371error:
80c802f3 2372 dst_release(dst);
75b8c133 2373dropdst:
ac37e251
W
2374 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2375 dst_release(dst_orig);
80c802f3 2376 xfrm_pols_put(pols, drop_pols);
452edd59 2377 return ERR_PTR(err);
1da177e4
LT
2378}
2379EXPORT_SYMBOL(xfrm_lookup);
2380
f92ee619
SK
2381/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2382 * Otherwise we may send out blackholed packets.
2383 */
2384struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2385 const struct flowi *fl,
6f9c9615 2386 const struct sock *sk, int flags)
f92ee619 2387{
b8c203b2 2388 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
ac37e251
W
2389 flags | XFRM_LOOKUP_QUEUE |
2390 XFRM_LOOKUP_KEEP_DST_REF);
f92ee619
SK
2391
2392 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2393 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2394
2395 return dst;
2396}
2397EXPORT_SYMBOL(xfrm_lookup_route);
2398
df0ba92a 2399static inline int
8f029de2 2400xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
df0ba92a
MN
2401{
2402 struct xfrm_state *x;
df0ba92a
MN
2403
2404 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2405 return 0;
2406 x = skb->sp->xvec[idx];
2407 if (!x->type->reject)
2408 return 0;
1ecafede 2409 return x->type->reject(x, skb, fl);
df0ba92a
MN
2410}
2411
1da177e4
LT
2412/* When skb is transformed back to its "native" form, we have to
2413 * check policy restrictions. At the moment we make this in maximally
2414 * stupid way. Shame on me. :-) Of course, connected sockets must
2415 * have policy cached at them.
2416 */
2417
2418static inline int
7db454b9 2419xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
1da177e4
LT
2420 unsigned short family)
2421{
2422 if (xfrm_state_kern(x))
928ba416 2423 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
1da177e4
LT
2424 return x->id.proto == tmpl->id.proto &&
2425 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2426 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2427 x->props.mode == tmpl->mode &&
c5d18e98 2428 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
f3bd4840 2429 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
7e49e6de
MN
2430 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2431 xfrm_state_addr_cmp(tmpl, x, family));
1da177e4
LT
2432}
2433
df0ba92a
MN
2434/*
2435 * 0 or more than 0 is returned when validation is succeeded (either bypass
2436 * because of optional transport mode, or next index of the mathced secpath
2437 * state with the template.
2438 * -1 is returned when no matching template is found.
2439 * Otherwise "-2 - errored_index" is returned.
2440 */
1da177e4 2441static inline int
22cccb7e 2442xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
1da177e4
LT
2443 unsigned short family)
2444{
2445 int idx = start;
2446
2447 if (tmpl->optional) {
7e49e6de 2448 if (tmpl->mode == XFRM_MODE_TRANSPORT)
1da177e4
LT
2449 return start;
2450 } else
2451 start = -1;
2452 for (; idx < sp->len; idx++) {
dbe5b4aa 2453 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
1da177e4 2454 return ++idx;
df0ba92a
MN
2455 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2456 if (start == -1)
2457 start = -2-idx;
1da177e4 2458 break;
df0ba92a 2459 }
1da177e4
LT
2460 }
2461 return start;
2462}
2463
d5422efe
HX
2464int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2465 unsigned int family, int reverse)
1da177e4
LT
2466{
2467 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
e0d1caa7 2468 int err;
1da177e4
LT
2469
2470 if (unlikely(afinfo == NULL))
2471 return -EAFNOSUPPORT;
2472
d5422efe 2473 afinfo->decode_session(skb, fl, reverse);
1d28f42c 2474 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
1da177e4 2475 xfrm_policy_put_afinfo(afinfo);
e0d1caa7 2476 return err;
1da177e4 2477}
d5422efe 2478EXPORT_SYMBOL(__xfrm_decode_session);
1da177e4 2479
9a7386ec 2480static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
1da177e4
LT
2481{
2482 for (; k < sp->len; k++) {
df0ba92a 2483 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
d1d9facf 2484 *idxp = k;
1da177e4 2485 return 1;
df0ba92a 2486 }
1da177e4
LT
2487 }
2488
2489 return 0;
2490}
2491
a716c119 2492int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1da177e4
LT
2493 unsigned short family)
2494{
f6e1e25d 2495 struct net *net = dev_net(skb->dev);
1da177e4 2496 struct xfrm_policy *pol;
4e81bb83
MN
2497 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2498 int npols = 0;
2499 int xfrm_nr;
2500 int pi;
d5422efe 2501 int reverse;
1da177e4 2502 struct flowi fl;
d5422efe 2503 u8 fl_dir;
df0ba92a 2504 int xerr_idx = -1;
1da177e4 2505
d5422efe
HX
2506 reverse = dir & ~XFRM_POLICY_MASK;
2507 dir &= XFRM_POLICY_MASK;
2508 fl_dir = policy_to_flow_dir(dir);
2509
0aa64774 2510 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
59c9940e 2511 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
1da177e4 2512 return 0;
0aa64774
MN
2513 }
2514
eb9c7ebe 2515 nf_nat_decode_session(skb, &fl, family);
1da177e4
LT
2516
2517 /* First, check used SA against their selectors. */
2518 if (skb->sp) {
2519 int i;
2520
9b7a787d 2521 for (i = skb->sp->len-1; i >= 0; i--) {
dbe5b4aa 2522 struct xfrm_state *x = skb->sp->xvec[i];
0aa64774 2523 if (!xfrm_selector_match(&x->sel, &fl, family)) {
59c9940e 2524 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
1da177e4 2525 return 0;
0aa64774 2526 }
1da177e4
LT
2527 }
2528 }
2529
2530 pol = NULL;
bd5eb35f 2531 sk = sk_to_full_sk(sk);
3bccfbc7 2532 if (sk && sk->sk_policy[dir]) {
e0d1caa7 2533 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
0aa64774 2534 if (IS_ERR(pol)) {
59c9940e 2535 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
3bccfbc7 2536 return 0;
0aa64774 2537 }
3bccfbc7 2538 }
1da177e4 2539
fe1a5f03
TT
2540 if (!pol) {
2541 struct flow_cache_object *flo;
2542
2543 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2544 xfrm_policy_lookup, NULL);
2545 if (IS_ERR_OR_NULL(flo))
2546 pol = ERR_CAST(flo);
2547 else
2548 pol = container_of(flo, struct xfrm_policy, flo);
2549 }
1da177e4 2550
0aa64774 2551 if (IS_ERR(pol)) {
59c9940e 2552 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
134b0fc5 2553 return 0;
0aa64774 2554 }
134b0fc5 2555
df0ba92a 2556 if (!pol) {
d1d9facf 2557 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
df0ba92a 2558 xfrm_secpath_reject(xerr_idx, skb, &fl);
59c9940e 2559 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
df0ba92a
MN
2560 return 0;
2561 }
2562 return 1;
2563 }
1da177e4 2564
9d729f72 2565 pol->curlft.use_time = get_seconds();
1da177e4 2566
4e81bb83 2567 pols[0] = pol;
02d0892f 2568 npols++;
4e81bb83
MN
2569#ifdef CONFIG_XFRM_SUB_POLICY
2570 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
f6e1e25d 2571 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
4e81bb83
MN
2572 &fl, family,
2573 XFRM_POLICY_IN);
2574 if (pols[1]) {
0aa64774 2575 if (IS_ERR(pols[1])) {
59c9940e 2576 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
134b0fc5 2577 return 0;
0aa64774 2578 }
9d729f72 2579 pols[1]->curlft.use_time = get_seconds();
02d0892f 2580 npols++;
4e81bb83
MN
2581 }
2582 }
2583#endif
2584
1da177e4
LT
2585 if (pol->action == XFRM_POLICY_ALLOW) {
2586 struct sec_path *sp;
2587 static struct sec_path dummy;
4e81bb83 2588 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
41a49cc3 2589 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
4e81bb83
MN
2590 struct xfrm_tmpl **tpp = tp;
2591 int ti = 0;
1da177e4
LT
2592 int i, k;
2593
2594 if ((sp = skb->sp) == NULL)
2595 sp = &dummy;
2596
4e81bb83
MN
2597 for (pi = 0; pi < npols; pi++) {
2598 if (pols[pi] != pol &&
0aa64774 2599 pols[pi]->action != XFRM_POLICY_ALLOW) {
59c9940e 2600 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
4e81bb83 2601 goto reject;
0aa64774
MN
2602 }
2603 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
59c9940e 2604 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
4e81bb83 2605 goto reject_error;
0aa64774 2606 }
4e81bb83
MN
2607 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2608 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2609 }
2610 xfrm_nr = ti;
41a49cc3 2611 if (npols > 1) {
283bc9f3 2612 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
41a49cc3
MN
2613 tpp = stp;
2614 }
4e81bb83 2615
1da177e4
LT
2616 /* For each tunnel xfrm, find the first matching tmpl.
2617 * For each tmpl before that, find corresponding xfrm.
2618 * Order is _important_. Later we will implement
2619 * some barriers, but at the moment barriers
2620 * are implied between each two transformations.
2621 */
4e81bb83
MN
2622 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2623 k = xfrm_policy_ok(tpp[i], sp, k, family);
df0ba92a 2624 if (k < 0) {
d1d9facf
JM
2625 if (k < -1)
2626 /* "-2 - errored_index" returned */
2627 xerr_idx = -(2+k);
59c9940e 2628 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
1da177e4 2629 goto reject;
df0ba92a 2630 }
1da177e4
LT
2631 }
2632
0aa64774 2633 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
59c9940e 2634 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
1da177e4 2635 goto reject;
0aa64774 2636 }
1da177e4 2637
4e81bb83 2638 xfrm_pols_put(pols, npols);
1da177e4
LT
2639 return 1;
2640 }
59c9940e 2641 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
1da177e4
LT
2642
2643reject:
df0ba92a 2644 xfrm_secpath_reject(xerr_idx, skb, &fl);
4e81bb83
MN
2645reject_error:
2646 xfrm_pols_put(pols, npols);
1da177e4
LT
2647 return 0;
2648}
2649EXPORT_SYMBOL(__xfrm_policy_check);
2650
2651int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2652{
99a66657 2653 struct net *net = dev_net(skb->dev);
1da177e4 2654 struct flowi fl;
adf30907 2655 struct dst_entry *dst;
73137147 2656 int res = 1;
1da177e4 2657
0aa64774 2658 if (xfrm_decode_session(skb, &fl, family) < 0) {
72032fdb 2659 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
1da177e4 2660 return 0;
0aa64774 2661 }
1da177e4 2662
fafeeb6c 2663 skb_dst_force(skb);
adf30907 2664
b8c203b2 2665 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
452edd59 2666 if (IS_ERR(dst)) {
73137147 2667 res = 0;
452edd59
DM
2668 dst = NULL;
2669 }
adf30907
ED
2670 skb_dst_set(skb, dst);
2671 return res;
1da177e4
LT
2672}
2673EXPORT_SYMBOL(__xfrm_route_forward);
2674
d49c73c7
DM
2675/* Optimize later using cookies and generation ids. */
2676
1da177e4
LT
2677static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2678{
d49c73c7 2679 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
f5b0a874
DM
2680 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2681 * get validated by dst_ops->check on every use. We do this
2682 * because when a normal route referenced by an XFRM dst is
2683 * obsoleted we do not go looking around for all parent
2684 * referencing XFRM dsts so that we can invalidate them. It
2685 * is just too much work. Instead we make the checks here on
2686 * every use. For example:
d49c73c7
DM
2687 *
2688 * XFRM dst A --> IPv4 dst X
2689 *
2690 * X is the "xdst->route" of A (X is also the "dst->path" of A
2691 * in this example). If X is marked obsolete, "A" will not
2692 * notice. That's what we are validating here via the
2693 * stale_bundle() check.
2694 *
2695 * When a policy's bundle is pruned, we dst_free() the XFRM
f5b0a874
DM
2696 * dst which causes it's ->obsolete field to be set to
2697 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2698 * this, we want to force a new route lookup.
399c180a 2699 */
d49c73c7
DM
2700 if (dst->obsolete < 0 && !stale_bundle(dst))
2701 return dst;
2702
1da177e4
LT
2703 return NULL;
2704}
2705
2706static int stale_bundle(struct dst_entry *dst)
2707{
12fdb4d3 2708 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
1da177e4
LT
2709}
2710
aabc9761 2711void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1da177e4 2712{
1da177e4 2713 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
c346dca1 2714 dst->dev = dev_net(dev)->loopback_dev;
de3cb747 2715 dev_hold(dst->dev);
1da177e4
LT
2716 dev_put(dev);
2717 }
2718}
aabc9761 2719EXPORT_SYMBOL(xfrm_dst_ifdown);
1da177e4
LT
2720
2721static void xfrm_link_failure(struct sk_buff *skb)
2722{
2723 /* Impossible. Such dst must be popped before reaches point of failure. */
1da177e4
LT
2724}
2725
2726static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2727{
2728 if (dst) {
2729 if (dst->obsolete) {
2730 dst_release(dst);
2731 dst = NULL;
2732 }
2733 }
2734 return dst;
2735}
2736
e4c17216 2737void xfrm_garbage_collect(struct net *net)
c0ed1c14 2738{
ca925cf1 2739 flow_cache_flush(net);
c0ed1c14 2740}
e4c17216 2741EXPORT_SYMBOL(xfrm_garbage_collect);
c0ed1c14
SK
2742
2743static void xfrm_garbage_collect_deferred(struct net *net)
2744{
ca925cf1 2745 flow_cache_flush_deferred(net);
c0ed1c14
SK
2746}
2747
25ee3286 2748static void xfrm_init_pmtu(struct dst_entry *dst)
1da177e4
LT
2749{
2750 do {
2751 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2752 u32 pmtu, route_mtu_cached;
2753
2754 pmtu = dst_mtu(dst->child);
2755 xdst->child_mtu_cached = pmtu;
2756
2757 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2758
2759 route_mtu_cached = dst_mtu(xdst->route);
2760 xdst->route_mtu_cached = route_mtu_cached;
2761
2762 if (pmtu > route_mtu_cached)
2763 pmtu = route_mtu_cached;
2764
defb3519 2765 dst_metric_set(dst, RTAX_MTU, pmtu);
1da177e4
LT
2766 } while ((dst = dst->next));
2767}
2768
1da177e4
LT
2769/* Check that the bundle accepts the flow and its components are
2770 * still valid.
2771 */
2772
12fdb4d3 2773static int xfrm_bundle_ok(struct xfrm_dst *first)
1da177e4
LT
2774{
2775 struct dst_entry *dst = &first->u.dst;
2776 struct xfrm_dst *last;
2777 u32 mtu;
2778
92d63dec 2779 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1da177e4
LT
2780 (dst->dev && !netif_running(dst->dev)))
2781 return 0;
2782
a0073fe1
SK
2783 if (dst->flags & DST_XFRM_QUEUE)
2784 return 1;
2785
1da177e4
LT
2786 last = NULL;
2787
2788 do {
2789 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2790
1da177e4
LT
2791 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2792 return 0;
80c802f3
TT
2793 if (xdst->xfrm_genid != dst->xfrm->genid)
2794 return 0;
b1312c89
TT
2795 if (xdst->num_pols > 0 &&
2796 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
9d4a706d 2797 return 0;
e53820de 2798
1da177e4
LT
2799 mtu = dst_mtu(dst->child);
2800 if (xdst->child_mtu_cached != mtu) {
2801 last = xdst;
2802 xdst->child_mtu_cached = mtu;
2803 }
2804
92d63dec 2805 if (!dst_check(xdst->route, xdst->route_cookie))
1da177e4
LT
2806 return 0;
2807 mtu = dst_mtu(xdst->route);
2808 if (xdst->route_mtu_cached != mtu) {
2809 last = xdst;
2810 xdst->route_mtu_cached = mtu;
2811 }
2812
2813 dst = dst->child;
2814 } while (dst->xfrm);
2815
2816 if (likely(!last))
2817 return 1;
2818
2819 mtu = last->child_mtu_cached;
2820 for (;;) {
2821 dst = &last->u.dst;
2822
2823 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2824 if (mtu > last->route_mtu_cached)
2825 mtu = last->route_mtu_cached;
defb3519 2826 dst_metric_set(dst, RTAX_MTU, mtu);
1da177e4
LT
2827
2828 if (last == first)
2829 break;
2830
bd0bf076 2831 last = (struct xfrm_dst *)last->u.dst.next;
1da177e4
LT
2832 last->child_mtu_cached = mtu;
2833 }
2834
2835 return 1;
2836}
2837
0dbaee3b
DM
2838static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2839{
2840 return dst_metric_advmss(dst->path);
2841}
2842
ebb762f2 2843static unsigned int xfrm_mtu(const struct dst_entry *dst)
d33e4553 2844{
618f9bc7
SK
2845 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2846
2847 return mtu ? : dst_mtu(dst->path);
d33e4553
DM
2848}
2849
f894cbf8
DM
2850static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2851 struct sk_buff *skb,
2852 const void *daddr)
d3aaeb38 2853{
f894cbf8 2854 return dst->path->ops->neigh_lookup(dst, skb, daddr);
d3aaeb38
DM
2855}
2856
1da177e4
LT
2857int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2858{
2859 int err = 0;
2860 if (unlikely(afinfo == NULL))
2861 return -EINVAL;
2862 if (unlikely(afinfo->family >= NPROTO))
2863 return -EAFNOSUPPORT;
ef8531b6 2864 spin_lock(&xfrm_policy_afinfo_lock);
1da177e4 2865 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
f31e8d4f 2866 err = -EEXIST;
1da177e4
LT
2867 else {
2868 struct dst_ops *dst_ops = afinfo->dst_ops;
2869 if (likely(dst_ops->kmem_cachep == NULL))
2870 dst_ops->kmem_cachep = xfrm_dst_cache;
2871 if (likely(dst_ops->check == NULL))
2872 dst_ops->check = xfrm_dst_check;
0dbaee3b
DM
2873 if (likely(dst_ops->default_advmss == NULL))
2874 dst_ops->default_advmss = xfrm_default_advmss;
ebb762f2
SK
2875 if (likely(dst_ops->mtu == NULL))
2876 dst_ops->mtu = xfrm_mtu;
1da177e4
LT
2877 if (likely(dst_ops->negative_advice == NULL))
2878 dst_ops->negative_advice = xfrm_negative_advice;
2879 if (likely(dst_ops->link_failure == NULL))
2880 dst_ops->link_failure = xfrm_link_failure;
d3aaeb38
DM
2881 if (likely(dst_ops->neigh_lookup == NULL))
2882 dst_ops->neigh_lookup = xfrm_neigh_lookup;
1da177e4 2883 if (likely(afinfo->garbage_collect == NULL))
c0ed1c14 2884 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
418a99ac 2885 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
1da177e4 2886 }
ef8531b6 2887 spin_unlock(&xfrm_policy_afinfo_lock);
d7c7544c 2888
1da177e4
LT
2889 return err;
2890}
2891EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2892
2893int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2894{
2895 int err = 0;
2896 if (unlikely(afinfo == NULL))
2897 return -EINVAL;
2898 if (unlikely(afinfo->family >= NPROTO))
2899 return -EAFNOSUPPORT;
ef8531b6 2900 spin_lock(&xfrm_policy_afinfo_lock);
1da177e4
LT
2901 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2902 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2903 err = -EINVAL;
ef8531b6
ED
2904 else
2905 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2906 NULL);
2907 }
2908 spin_unlock(&xfrm_policy_afinfo_lock);
2909 if (!err) {
2910 struct dst_ops *dst_ops = afinfo->dst_ops;
2911
2912 synchronize_rcu();
2913
2914 dst_ops->kmem_cachep = NULL;
2915 dst_ops->check = NULL;
2916 dst_ops->negative_advice = NULL;
2917 dst_ops->link_failure = NULL;
2918 afinfo->garbage_collect = NULL;
1da177e4 2919 }
1da177e4
LT
2920 return err;
2921}
2922EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2923
1da177e4
LT
2924static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2925{
351638e7 2926 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
e9dc8653 2927
1da177e4
LT
2928 switch (event) {
2929 case NETDEV_DOWN:
c0ed1c14 2930 xfrm_garbage_collect(dev_net(dev));
1da177e4
LT
2931 }
2932 return NOTIFY_DONE;
2933}
2934
2935static struct notifier_block xfrm_dev_notifier = {
d5917a35 2936 .notifier_call = xfrm_dev_event,
1da177e4
LT
2937};
2938
558f82ef 2939#ifdef CONFIG_XFRM_STATISTICS
59c9940e 2940static int __net_init xfrm_statistics_init(struct net *net)
558f82ef 2941{
c68cd1a0 2942 int rv;
698365fa
WC
2943 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2944 if (!net->mib.xfrm_statistics)
558f82ef 2945 return -ENOMEM;
c68cd1a0
AD
2946 rv = xfrm_proc_init(net);
2947 if (rv < 0)
698365fa 2948 free_percpu(net->mib.xfrm_statistics);
c68cd1a0 2949 return rv;
558f82ef 2950}
59c9940e
AD
2951
2952static void xfrm_statistics_fini(struct net *net)
2953{
c68cd1a0 2954 xfrm_proc_fini(net);
698365fa 2955 free_percpu(net->mib.xfrm_statistics);
59c9940e
AD
2956}
2957#else
2958static int __net_init xfrm_statistics_init(struct net *net)
2959{
2960 return 0;
2961}
2962
2963static void xfrm_statistics_fini(struct net *net)
2964{
2965}
558f82ef
MN
2966#endif
2967
d62ddc21 2968static int __net_init xfrm_policy_init(struct net *net)
1da177e4 2969{
2518c7c2
DM
2970 unsigned int hmask, sz;
2971 int dir;
2972
d62ddc21
AD
2973 if (net_eq(net, &init_net))
2974 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
1da177e4 2975 sizeof(struct xfrm_dst),
e5d679f3 2976 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
20c2df83 2977 NULL);
1da177e4 2978
2518c7c2
DM
2979 hmask = 8 - 1;
2980 sz = (hmask+1) * sizeof(struct hlist_head);
2981
93b851c1
AD
2982 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2983 if (!net->xfrm.policy_byidx)
2984 goto out_byidx;
8100bea7 2985 net->xfrm.policy_idx_hmask = hmask;
2518c7c2 2986
53c2e285 2987 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
2518c7c2
DM
2988 struct xfrm_policy_hash *htab;
2989
dc2caba7 2990 net->xfrm.policy_count[dir] = 0;
53c2e285 2991 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
8b18f8ea 2992 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
2518c7c2 2993
a35f6c5d 2994 htab = &net->xfrm.policy_bydst[dir];
44e36b42 2995 htab->table = xfrm_hash_alloc(sz);
2518c7c2 2996 if (!htab->table)
a35f6c5d
AD
2997 goto out_bydst;
2998 htab->hmask = hmask;
b58555f1
CG
2999 htab->dbits4 = 32;
3000 htab->sbits4 = 32;
3001 htab->dbits6 = 128;
3002 htab->sbits6 = 128;
2518c7c2 3003 }
880a6fab
CG
3004 net->xfrm.policy_hthresh.lbits4 = 32;
3005 net->xfrm.policy_hthresh.rbits4 = 32;
3006 net->xfrm.policy_hthresh.lbits6 = 128;
3007 net->xfrm.policy_hthresh.rbits6 = 128;
3008
3009 seqlock_init(&net->xfrm.policy_hthresh.lock);
2518c7c2 3010
adfcf0b2 3011 INIT_LIST_HEAD(&net->xfrm.policy_all);
66caf628 3012 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
880a6fab 3013 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
d62ddc21
AD
3014 if (net_eq(net, &init_net))
3015 register_netdevice_notifier(&xfrm_dev_notifier);
3016 return 0;
93b851c1 3017
a35f6c5d
AD
3018out_bydst:
3019 for (dir--; dir >= 0; dir--) {
3020 struct xfrm_policy_hash *htab;
3021
3022 htab = &net->xfrm.policy_bydst[dir];
3023 xfrm_hash_free(htab->table, sz);
3024 }
3025 xfrm_hash_free(net->xfrm.policy_byidx, sz);
93b851c1
AD
3026out_byidx:
3027 return -ENOMEM;
d62ddc21
AD
3028}
3029
3030static void xfrm_policy_fini(struct net *net)
3031{
93b851c1 3032 unsigned int sz;
8b18f8ea 3033 int dir;
93b851c1 3034
7c2776ee
AD
3035 flush_work(&net->xfrm.policy_hash_work);
3036#ifdef CONFIG_XFRM_SUB_POLICY
2e71029e 3037 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
7c2776ee 3038#endif
2e71029e 3039 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
7c2776ee 3040
adfcf0b2 3041 WARN_ON(!list_empty(&net->xfrm.policy_all));
93b851c1 3042
53c2e285 3043 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
a35f6c5d
AD
3044 struct xfrm_policy_hash *htab;
3045
8b18f8ea 3046 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
a35f6c5d
AD
3047
3048 htab = &net->xfrm.policy_bydst[dir];
5b653b2a 3049 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
a35f6c5d
AD
3050 WARN_ON(!hlist_empty(htab->table));
3051 xfrm_hash_free(htab->table, sz);
8b18f8ea
AD
3052 }
3053
8100bea7 3054 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
93b851c1
AD
3055 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3056 xfrm_hash_free(net->xfrm.policy_byidx, sz);
1da177e4
LT
3057}
3058
d62ddc21
AD
3059static int __net_init xfrm_net_init(struct net *net)
3060{
3061 int rv;
3062
59c9940e
AD
3063 rv = xfrm_statistics_init(net);
3064 if (rv < 0)
3065 goto out_statistics;
d62ddc21
AD
3066 rv = xfrm_state_init(net);
3067 if (rv < 0)
3068 goto out_state;
3069 rv = xfrm_policy_init(net);
3070 if (rv < 0)
3071 goto out_policy;
b27aeadb
AD
3072 rv = xfrm_sysctl_init(net);
3073 if (rv < 0)
3074 goto out_sysctl;
4a93f509
SK
3075 rv = flow_cache_init(net);
3076 if (rv < 0)
3077 goto out;
283bc9f3
FD
3078
3079 /* Initialize the per-net locks here */
3080 spin_lock_init(&net->xfrm.xfrm_state_lock);
9d0380df 3081 spin_lock_init(&net->xfrm.xfrm_policy_lock);
283bc9f3
FD
3082 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3083
d62ddc21
AD
3084 return 0;
3085
4a93f509
SK
3086out:
3087 xfrm_sysctl_fini(net);
b27aeadb
AD
3088out_sysctl:
3089 xfrm_policy_fini(net);
d62ddc21
AD
3090out_policy:
3091 xfrm_state_fini(net);
3092out_state:
59c9940e
AD
3093 xfrm_statistics_fini(net);
3094out_statistics:
d62ddc21
AD
3095 return rv;
3096}
3097
3098static void __net_exit xfrm_net_exit(struct net *net)
3099{
4a93f509 3100 flow_cache_fini(net);
b27aeadb 3101 xfrm_sysctl_fini(net);
d62ddc21
AD
3102 xfrm_policy_fini(net);
3103 xfrm_state_fini(net);
59c9940e 3104 xfrm_statistics_fini(net);
d62ddc21
AD
3105}
3106
3107static struct pernet_operations __net_initdata xfrm_net_ops = {
3108 .init = xfrm_net_init,
3109 .exit = xfrm_net_exit,
3110};
3111
1da177e4
LT
3112void __init xfrm_init(void)
3113{
d62ddc21 3114 register_pernet_subsys(&xfrm_net_ops);
30846090 3115 seqcount_init(&xfrm_policy_hash_generation);
1da177e4
LT
3116 xfrm_input_init();
3117}
3118
ab5f5e8b 3119#ifdef CONFIG_AUDITSYSCALL
1486cbd7
IJ
3120static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3121 struct audit_buffer *audit_buf)
ab5f5e8b 3122{
875179fa
PM
3123 struct xfrm_sec_ctx *ctx = xp->security;
3124 struct xfrm_selector *sel = &xp->selector;
3125
3126 if (ctx)
ab5f5e8b 3127 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
875179fa 3128 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
ab5f5e8b 3129
9b7a787d 3130 switch (sel->family) {
ab5f5e8b 3131 case AF_INET:
21454aaa 3132 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
875179fa
PM
3133 if (sel->prefixlen_s != 32)
3134 audit_log_format(audit_buf, " src_prefixlen=%d",
3135 sel->prefixlen_s);
21454aaa 3136 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
875179fa
PM
3137 if (sel->prefixlen_d != 32)
3138 audit_log_format(audit_buf, " dst_prefixlen=%d",
3139 sel->prefixlen_d);
ab5f5e8b
JL
3140 break;
3141 case AF_INET6:
5b095d98 3142 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
875179fa
PM
3143 if (sel->prefixlen_s != 128)
3144 audit_log_format(audit_buf, " src_prefixlen=%d",
3145 sel->prefixlen_s);
5b095d98 3146 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
875179fa
PM
3147 if (sel->prefixlen_d != 128)
3148 audit_log_format(audit_buf, " dst_prefixlen=%d",
3149 sel->prefixlen_d);
ab5f5e8b
JL
3150 break;
3151 }
3152}
3153
2e71029e 3154void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
ab5f5e8b
JL
3155{
3156 struct audit_buffer *audit_buf;
ab5f5e8b 3157
afeb14b4 3158 audit_buf = xfrm_audit_start("SPD-add");
ab5f5e8b
JL
3159 if (audit_buf == NULL)
3160 return;
2e71029e 3161 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
afeb14b4 3162 audit_log_format(audit_buf, " res=%u", result);
ab5f5e8b
JL
3163 xfrm_audit_common_policyinfo(xp, audit_buf);
3164 audit_log_end(audit_buf);
3165}
3166EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3167
68277acc 3168void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
2e71029e 3169 bool task_valid)
ab5f5e8b
JL
3170{
3171 struct audit_buffer *audit_buf;
ab5f5e8b 3172
afeb14b4 3173 audit_buf = xfrm_audit_start("SPD-delete");
ab5f5e8b
JL
3174 if (audit_buf == NULL)
3175 return;
2e71029e 3176 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
afeb14b4 3177 audit_log_format(audit_buf, " res=%u", result);
ab5f5e8b
JL
3178 xfrm_audit_common_policyinfo(xp, audit_buf);
3179 audit_log_end(audit_buf);
3180}
3181EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3182#endif
3183
80c9abaa 3184#ifdef CONFIG_XFRM_MIGRATE
bc9b35ad
DM
3185static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3186 const struct xfrm_selector *sel_tgt)
80c9abaa
SS
3187{
3188 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3189 if (sel_tgt->family == sel_cmp->family &&
70e94e66
YH
3190 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3191 sel_cmp->family) &&
3192 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3193 sel_cmp->family) &&
80c9abaa
SS
3194 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3195 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
bc9b35ad 3196 return true;
80c9abaa
SS
3197 }
3198 } else {
3199 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
bc9b35ad 3200 return true;
80c9abaa
SS
3201 }
3202 }
bc9b35ad 3203 return false;
80c9abaa
SS
3204}
3205
3e94c2dc
WC
3206static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3207 u8 dir, u8 type, struct net *net)
80c9abaa
SS
3208{
3209 struct xfrm_policy *pol, *ret = NULL;
80c9abaa
SS
3210 struct hlist_head *chain;
3211 u32 priority = ~0U;
3212
9d0380df 3213 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
8d549c4f 3214 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
b67bfe0d 3215 hlist_for_each_entry(pol, chain, bydst) {
80c9abaa
SS
3216 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3217 pol->type == type) {
3218 ret = pol;
3219 priority = ret->priority;
3220 break;
3221 }
3222 }
8d549c4f 3223 chain = &net->xfrm.policy_inexact[dir];
b67bfe0d 3224 hlist_for_each_entry(pol, chain, bydst) {
8faf491e
LR
3225 if ((pol->priority >= priority) && ret)
3226 break;
3227
80c9abaa 3228 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
8faf491e 3229 pol->type == type) {
80c9abaa
SS
3230 ret = pol;
3231 break;
3232 }
3233 }
3234
586f2eb4 3235 xfrm_pol_hold(ret);
80c9abaa 3236
9d0380df 3237 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
80c9abaa
SS
3238
3239 return ret;
3240}
3241
dd701754 3242static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
80c9abaa
SS
3243{
3244 int match = 0;
3245
3246 if (t->mode == m->mode && t->id.proto == m->proto &&
3247 (m->reqid == 0 || t->reqid == m->reqid)) {
3248 switch (t->mode) {
3249 case XFRM_MODE_TUNNEL:
3250 case XFRM_MODE_BEET:
70e94e66
YH
3251 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3252 m->old_family) &&
3253 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3254 m->old_family)) {
80c9abaa
SS
3255 match = 1;
3256 }
3257 break;
3258 case XFRM_MODE_TRANSPORT:
3259 /* in case of transport mode, template does not store
3260 any IP addresses, hence we just compare mode and
3261 protocol */
3262 match = 1;
3263 break;
3264 default:
3265 break;
3266 }
3267 }
3268 return match;
3269}
3270
3271/* update endpoint address(es) of template(s) */
3272static int xfrm_policy_migrate(struct xfrm_policy *pol,
3273 struct xfrm_migrate *m, int num_migrate)
3274{
3275 struct xfrm_migrate *mp;
80c9abaa
SS
3276 int i, j, n = 0;
3277
3278 write_lock_bh(&pol->lock);
12a169e7 3279 if (unlikely(pol->walk.dead)) {
80c9abaa
SS
3280 /* target policy has been deleted */
3281 write_unlock_bh(&pol->lock);
3282 return -ENOENT;
3283 }
3284
3285 for (i = 0; i < pol->xfrm_nr; i++) {
3286 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3287 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3288 continue;
3289 n++;
1bfcb10f
HX
3290 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3291 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
80c9abaa
SS
3292 continue;
3293 /* update endpoints */
3294 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3295 sizeof(pol->xfrm_vec[i].id.daddr));
3296 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3297 sizeof(pol->xfrm_vec[i].saddr));
3298 pol->xfrm_vec[i].encap_family = mp->new_family;
3299 /* flush bundles */
80c802f3 3300 atomic_inc(&pol->genid);
80c9abaa
SS
3301 }
3302 }
3303
3304 write_unlock_bh(&pol->lock);
3305
3306 if (!n)
3307 return -ENODATA;
3308
3309 return 0;
3310}
3311
dd701754 3312static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
80c9abaa
SS
3313{
3314 int i, j;
3315
3316 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3317 return -EINVAL;
3318
3319 for (i = 0; i < num_migrate; i++) {
70e94e66
YH
3320 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3321 m[i].old_family) &&
3322 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3323 m[i].old_family))
80c9abaa
SS
3324 return -EINVAL;
3325 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3326 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3327 return -EINVAL;
3328
3329 /* check if there is any duplicated entry */
3330 for (j = i + 1; j < num_migrate; j++) {
3331 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3332 sizeof(m[i].old_daddr)) &&
3333 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3334 sizeof(m[i].old_saddr)) &&
3335 m[i].proto == m[j].proto &&
3336 m[i].mode == m[j].mode &&
3337 m[i].reqid == m[j].reqid &&
3338 m[i].old_family == m[j].old_family)
3339 return -EINVAL;
3340 }
3341 }
3342
3343 return 0;
3344}
3345
b4b7c0b3 3346int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
13c1d189 3347 struct xfrm_migrate *m, int num_migrate,
8d549c4f 3348 struct xfrm_kmaddress *k, struct net *net)
80c9abaa
SS
3349{
3350 int i, err, nx_cur = 0, nx_new = 0;
3351 struct xfrm_policy *pol = NULL;
3352 struct xfrm_state *x, *xc;
3353 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3354 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3355 struct xfrm_migrate *mp;
3356
3357 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3358 goto out;
3359
3360 /* Stage 1 - find policy */
8d549c4f 3361 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
80c9abaa
SS
3362 err = -ENOENT;
3363 goto out;
3364 }
3365
3366 /* Stage 2 - find and update state(s) */
3367 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
283bc9f3 3368 if ((x = xfrm_migrate_state_find(mp, net))) {
80c9abaa
SS
3369 x_cur[nx_cur] = x;
3370 nx_cur++;
3371 if ((xc = xfrm_state_migrate(x, mp))) {
3372 x_new[nx_new] = xc;
3373 nx_new++;
3374 } else {
3375 err = -ENODATA;
3376 goto restore_state;
3377 }
3378 }
3379 }
3380
3381 /* Stage 3 - update policy */
3382 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3383 goto restore_state;
3384
3385 /* Stage 4 - delete old state(s) */
3386 if (nx_cur) {
3387 xfrm_states_put(x_cur, nx_cur);
3388 xfrm_states_delete(x_cur, nx_cur);
3389 }
3390
3391 /* Stage 5 - announce */
13c1d189 3392 km_migrate(sel, dir, type, m, num_migrate, k);
80c9abaa
SS
3393
3394 xfrm_pol_put(pol);
3395
3396 return 0;
3397out:
3398 return err;
3399
3400restore_state:
3401 if (pol)
3402 xfrm_pol_put(pol);
3403 if (nx_cur)
3404 xfrm_states_put(x_cur, nx_cur);
3405 if (nx_new)
3406 xfrm_states_delete(x_new, nx_new);
3407
3408 return err;
3409}
e610e679 3410EXPORT_SYMBOL(xfrm_migrate);
80c9abaa 3411#endif