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