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