]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/xfrm/xfrm_policy.c
[NET]: Make device event notification network namespace safe
[net-next-2.6.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
1da177e4
LT
16#include <linux/slab.h>
17#include <linux/kmod.h>
18#include <linux/list.h>
19#include <linux/spinlock.h>
20#include <linux/workqueue.h>
21#include <linux/notifier.h>
22#include <linux/netdevice.h>
eb9c7ebe 23#include <linux/netfilter.h>
1da177e4 24#include <linux/module.h>
2518c7c2 25#include <linux/cache.h>
1da177e4
LT
26#include <net/xfrm.h>
27#include <net/ip.h>
28
44e36b42
DM
29#include "xfrm_hash.h"
30
aad0e0b9 31int sysctl_xfrm_larval_drop __read_mostly;
14e50e57 32
4a3e2f71
AV
33DEFINE_MUTEX(xfrm_cfg_mutex);
34EXPORT_SYMBOL(xfrm_cfg_mutex);
1da177e4
LT
35
36static DEFINE_RWLOCK(xfrm_policy_lock);
37
2518c7c2
DM
38unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
39EXPORT_SYMBOL(xfrm_policy_count);
1da177e4
LT
40
41static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
42static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
43
e18b890b 44static struct kmem_cache *xfrm_dst_cache __read_mostly;
1da177e4
LT
45
46static struct work_struct xfrm_policy_gc_work;
2518c7c2 47static HLIST_HEAD(xfrm_policy_gc_list);
1da177e4
LT
48static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
49
50static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
51static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
546be240
HX
52static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
53static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
1da177e4 54
77681021
AM
55static inline int
56__xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
57{
58 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
59 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
60 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
61 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
62 (fl->proto == sel->proto || !sel->proto) &&
63 (fl->oif == sel->ifindex || !sel->ifindex);
64}
65
66static inline int
67__xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
68{
69 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
70 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
71 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
72 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
73 (fl->proto == sel->proto || !sel->proto) &&
74 (fl->oif == sel->ifindex || !sel->ifindex);
75}
76
77int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
78 unsigned short family)
79{
80 switch (family) {
81 case AF_INET:
82 return __xfrm4_selector_match(sel, fl);
83 case AF_INET6:
84 return __xfrm6_selector_match(sel, fl);
85 }
86 return 0;
87}
88
1da177e4
LT
89int xfrm_register_type(struct xfrm_type *type, unsigned short family)
90{
546be240
HX
91 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
92 struct xfrm_type **typemap;
1da177e4
LT
93 int err = 0;
94
95 if (unlikely(afinfo == NULL))
96 return -EAFNOSUPPORT;
97 typemap = afinfo->type_map;
98
546be240
HX
99 if (likely(typemap[type->proto] == NULL))
100 typemap[type->proto] = type;
1da177e4
LT
101 else
102 err = -EEXIST;
546be240 103 xfrm_policy_unlock_afinfo(afinfo);
1da177e4
LT
104 return err;
105}
106EXPORT_SYMBOL(xfrm_register_type);
107
108int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
109{
546be240
HX
110 struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
111 struct xfrm_type **typemap;
1da177e4
LT
112 int err = 0;
113
114 if (unlikely(afinfo == NULL))
115 return -EAFNOSUPPORT;
116 typemap = afinfo->type_map;
117
546be240 118 if (unlikely(typemap[type->proto] != type))
1da177e4
LT
119 err = -ENOENT;
120 else
546be240
HX
121 typemap[type->proto] = NULL;
122 xfrm_policy_unlock_afinfo(afinfo);
1da177e4
LT
123 return err;
124}
125EXPORT_SYMBOL(xfrm_unregister_type);
126
127struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
128{
129 struct xfrm_policy_afinfo *afinfo;
546be240 130 struct xfrm_type **typemap;
1da177e4
LT
131 struct xfrm_type *type;
132 int modload_attempted = 0;
133
134retry:
135 afinfo = xfrm_policy_get_afinfo(family);
136 if (unlikely(afinfo == NULL))
137 return NULL;
138 typemap = afinfo->type_map;
139
546be240 140 type = typemap[proto];
1da177e4
LT
141 if (unlikely(type && !try_module_get(type->owner)))
142 type = NULL;
1da177e4
LT
143 if (!type && !modload_attempted) {
144 xfrm_policy_put_afinfo(afinfo);
145 request_module("xfrm-type-%d-%d",
146 (int) family, (int) proto);
147 modload_attempted = 1;
148 goto retry;
149 }
150
151 xfrm_policy_put_afinfo(afinfo);
152 return type;
153}
1da177e4 154
a716c119 155int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
1da177e4
LT
156 unsigned short family)
157{
158 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
159 int err = 0;
160
161 if (unlikely(afinfo == NULL))
162 return -EAFNOSUPPORT;
163
164 if (likely(afinfo->dst_lookup != NULL))
165 err = afinfo->dst_lookup(dst, fl);
166 else
167 err = -EINVAL;
168 xfrm_policy_put_afinfo(afinfo);
169 return err;
170}
171EXPORT_SYMBOL(xfrm_dst_lookup);
172
173void xfrm_put_type(struct xfrm_type *type)
174{
175 module_put(type->owner);
176}
177
b59f45d0
HX
178int xfrm_register_mode(struct xfrm_mode *mode, int family)
179{
180 struct xfrm_policy_afinfo *afinfo;
181 struct xfrm_mode **modemap;
182 int err;
183
184 if (unlikely(mode->encap >= XFRM_MODE_MAX))
185 return -EINVAL;
186
187 afinfo = xfrm_policy_lock_afinfo(family);
188 if (unlikely(afinfo == NULL))
189 return -EAFNOSUPPORT;
190
191 err = -EEXIST;
192 modemap = afinfo->mode_map;
193 if (likely(modemap[mode->encap] == NULL)) {
194 modemap[mode->encap] = mode;
195 err = 0;
196 }
197
198 xfrm_policy_unlock_afinfo(afinfo);
199 return err;
200}
201EXPORT_SYMBOL(xfrm_register_mode);
202
203int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
204{
205 struct xfrm_policy_afinfo *afinfo;
206 struct xfrm_mode **modemap;
207 int err;
208
209 if (unlikely(mode->encap >= XFRM_MODE_MAX))
210 return -EINVAL;
211
212 afinfo = xfrm_policy_lock_afinfo(family);
213 if (unlikely(afinfo == NULL))
214 return -EAFNOSUPPORT;
215
216 err = -ENOENT;
217 modemap = afinfo->mode_map;
218 if (likely(modemap[mode->encap] == mode)) {
219 modemap[mode->encap] = NULL;
220 err = 0;
221 }
222
223 xfrm_policy_unlock_afinfo(afinfo);
224 return err;
225}
226EXPORT_SYMBOL(xfrm_unregister_mode);
227
228struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
229{
230 struct xfrm_policy_afinfo *afinfo;
231 struct xfrm_mode *mode;
232 int modload_attempted = 0;
233
234 if (unlikely(encap >= XFRM_MODE_MAX))
235 return NULL;
236
237retry:
238 afinfo = xfrm_policy_get_afinfo(family);
239 if (unlikely(afinfo == NULL))
240 return NULL;
241
242 mode = afinfo->mode_map[encap];
243 if (unlikely(mode && !try_module_get(mode->owner)))
244 mode = NULL;
245 if (!mode && !modload_attempted) {
246 xfrm_policy_put_afinfo(afinfo);
247 request_module("xfrm-mode-%d-%d", family, encap);
248 modload_attempted = 1;
249 goto retry;
250 }
251
252 xfrm_policy_put_afinfo(afinfo);
253 return mode;
254}
255
256void xfrm_put_mode(struct xfrm_mode *mode)
257{
258 module_put(mode->owner);
259}
260
1da177e4
LT
261static inline unsigned long make_jiffies(long secs)
262{
263 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
264 return MAX_SCHEDULE_TIMEOUT-1;
265 else
a716c119 266 return secs*HZ;
1da177e4
LT
267}
268
269static void xfrm_policy_timer(unsigned long data)
270{
271 struct xfrm_policy *xp = (struct xfrm_policy*)data;
9d729f72 272 unsigned long now = get_seconds();
1da177e4
LT
273 long next = LONG_MAX;
274 int warn = 0;
275 int dir;
276
277 read_lock(&xp->lock);
278
279 if (xp->dead)
280 goto out;
281
77d8d7a6 282 dir = xfrm_policy_id2dir(xp->index);
1da177e4
LT
283
284 if (xp->lft.hard_add_expires_seconds) {
285 long tmo = xp->lft.hard_add_expires_seconds +
286 xp->curlft.add_time - now;
287 if (tmo <= 0)
288 goto expired;
289 if (tmo < next)
290 next = tmo;
291 }
292 if (xp->lft.hard_use_expires_seconds) {
293 long tmo = xp->lft.hard_use_expires_seconds +
294 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
295 if (tmo <= 0)
296 goto expired;
297 if (tmo < next)
298 next = tmo;
299 }
300 if (xp->lft.soft_add_expires_seconds) {
301 long tmo = xp->lft.soft_add_expires_seconds +
302 xp->curlft.add_time - now;
303 if (tmo <= 0) {
304 warn = 1;
305 tmo = XFRM_KM_TIMEOUT;
306 }
307 if (tmo < next)
308 next = tmo;
309 }
310 if (xp->lft.soft_use_expires_seconds) {
311 long tmo = xp->lft.soft_use_expires_seconds +
312 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
313 if (tmo <= 0) {
314 warn = 1;
315 tmo = XFRM_KM_TIMEOUT;
316 }
317 if (tmo < next)
318 next = tmo;
319 }
320
321 if (warn)
6c5c8ca7 322 km_policy_expired(xp, dir, 0, 0);
1da177e4
LT
323 if (next != LONG_MAX &&
324 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
325 xfrm_pol_hold(xp);
326
327out:
328 read_unlock(&xp->lock);
329 xfrm_pol_put(xp);
330 return;
331
332expired:
333 read_unlock(&xp->lock);
4666faab 334 if (!xfrm_policy_delete(xp, dir))
6c5c8ca7 335 km_policy_expired(xp, dir, 1, 0);
1da177e4
LT
336 xfrm_pol_put(xp);
337}
338
339
340/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
341 * SPD calls.
342 */
343
dd0fc66f 344struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
1da177e4
LT
345{
346 struct xfrm_policy *policy;
347
0da974f4 348 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
1da177e4
LT
349
350 if (policy) {
2518c7c2
DM
351 INIT_HLIST_NODE(&policy->bydst);
352 INIT_HLIST_NODE(&policy->byidx);
1da177e4 353 rwlock_init(&policy->lock);
2518c7c2 354 atomic_set(&policy->refcnt, 1);
1da177e4
LT
355 init_timer(&policy->timer);
356 policy->timer.data = (unsigned long)policy;
357 policy->timer.function = xfrm_policy_timer;
358 }
359 return policy;
360}
361EXPORT_SYMBOL(xfrm_policy_alloc);
362
363/* Destroy xfrm_policy: descendant resources must be released to this moment. */
364
365void __xfrm_policy_destroy(struct xfrm_policy *policy)
366{
09a62660 367 BUG_ON(!policy->dead);
1da177e4 368
09a62660 369 BUG_ON(policy->bundles);
1da177e4
LT
370
371 if (del_timer(&policy->timer))
372 BUG();
373
df71837d 374 security_xfrm_policy_free(policy);
1da177e4
LT
375 kfree(policy);
376}
377EXPORT_SYMBOL(__xfrm_policy_destroy);
378
379static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
380{
381 struct dst_entry *dst;
382
383 while ((dst = policy->bundles) != NULL) {
384 policy->bundles = dst->next;
385 dst_free(dst);
386 }
387
388 if (del_timer(&policy->timer))
389 atomic_dec(&policy->refcnt);
390
391 if (atomic_read(&policy->refcnt) > 1)
392 flow_cache_flush();
393
394 xfrm_pol_put(policy);
395}
396
c4028958 397static void xfrm_policy_gc_task(struct work_struct *work)
1da177e4
LT
398{
399 struct xfrm_policy *policy;
2518c7c2
DM
400 struct hlist_node *entry, *tmp;
401 struct hlist_head gc_list;
1da177e4
LT
402
403 spin_lock_bh(&xfrm_policy_gc_lock);
2518c7c2
DM
404 gc_list.first = xfrm_policy_gc_list.first;
405 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
1da177e4
LT
406 spin_unlock_bh(&xfrm_policy_gc_lock);
407
2518c7c2 408 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
1da177e4 409 xfrm_policy_gc_kill(policy);
1da177e4
LT
410}
411
412/* Rule must be locked. Release descentant resources, announce
413 * entry dead. The rule must be unlinked from lists to the moment.
414 */
415
416static void xfrm_policy_kill(struct xfrm_policy *policy)
417{
418 int dead;
419
420 write_lock_bh(&policy->lock);
421 dead = policy->dead;
422 policy->dead = 1;
423 write_unlock_bh(&policy->lock);
424
425 if (unlikely(dead)) {
426 WARN_ON(1);
427 return;
428 }
429
430 spin_lock(&xfrm_policy_gc_lock);
2518c7c2 431 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
1da177e4
LT
432 spin_unlock(&xfrm_policy_gc_lock);
433
434 schedule_work(&xfrm_policy_gc_work);
435}
436
2518c7c2
DM
437struct xfrm_policy_hash {
438 struct hlist_head *table;
439 unsigned int hmask;
440};
441
442static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
443static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
444static struct hlist_head *xfrm_policy_byidx __read_mostly;
445static unsigned int xfrm_idx_hmask __read_mostly;
446static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
447
2518c7c2
DM
448static inline unsigned int idx_hash(u32 index)
449{
450 return __idx_hash(index, xfrm_idx_hmask);
451}
452
2518c7c2
DM
453static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
454{
455 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
456 unsigned int hash = __sel_hash(sel, family, hmask);
457
458 return (hash == hmask + 1 ?
459 &xfrm_policy_inexact[dir] :
460 xfrm_policy_bydst[dir].table + hash);
461}
462
463static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
464{
465 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
466 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
467
468 return xfrm_policy_bydst[dir].table + hash;
469}
470
2518c7c2
DM
471static void xfrm_dst_hash_transfer(struct hlist_head *list,
472 struct hlist_head *ndsttable,
473 unsigned int nhashmask)
474{
475 struct hlist_node *entry, *tmp;
476 struct xfrm_policy *pol;
477
478 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
479 unsigned int h;
480
481 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
482 pol->family, nhashmask);
483 hlist_add_head(&pol->bydst, ndsttable+h);
484 }
485}
486
487static void xfrm_idx_hash_transfer(struct hlist_head *list,
488 struct hlist_head *nidxtable,
489 unsigned int nhashmask)
490{
491 struct hlist_node *entry, *tmp;
492 struct xfrm_policy *pol;
493
494 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
495 unsigned int h;
496
497 h = __idx_hash(pol->index, nhashmask);
498 hlist_add_head(&pol->byidx, nidxtable+h);
499 }
500}
501
502static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
503{
504 return ((old_hmask + 1) << 1) - 1;
505}
506
507static void xfrm_bydst_resize(int dir)
508{
509 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
510 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
511 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
512 struct hlist_head *odst = xfrm_policy_bydst[dir].table;
44e36b42 513 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
2518c7c2
DM
514 int i;
515
516 if (!ndst)
517 return;
518
519 write_lock_bh(&xfrm_policy_lock);
520
521 for (i = hmask; i >= 0; i--)
522 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
523
524 xfrm_policy_bydst[dir].table = ndst;
525 xfrm_policy_bydst[dir].hmask = nhashmask;
526
527 write_unlock_bh(&xfrm_policy_lock);
528
44e36b42 529 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
2518c7c2
DM
530}
531
532static void xfrm_byidx_resize(int total)
533{
534 unsigned int hmask = xfrm_idx_hmask;
535 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
536 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
537 struct hlist_head *oidx = xfrm_policy_byidx;
44e36b42 538 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
2518c7c2
DM
539 int i;
540
541 if (!nidx)
542 return;
543
544 write_lock_bh(&xfrm_policy_lock);
545
546 for (i = hmask; i >= 0; i--)
547 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
548
549 xfrm_policy_byidx = nidx;
550 xfrm_idx_hmask = nhashmask;
551
552 write_unlock_bh(&xfrm_policy_lock);
553
44e36b42 554 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
2518c7c2
DM
555}
556
557static inline int xfrm_bydst_should_resize(int dir, int *total)
558{
559 unsigned int cnt = xfrm_policy_count[dir];
560 unsigned int hmask = xfrm_policy_bydst[dir].hmask;
561
562 if (total)
563 *total += cnt;
564
565 if ((hmask + 1) < xfrm_policy_hashmax &&
566 cnt > hmask)
567 return 1;
568
569 return 0;
570}
571
572static inline int xfrm_byidx_should_resize(int total)
573{
574 unsigned int hmask = xfrm_idx_hmask;
575
576 if ((hmask + 1) < xfrm_policy_hashmax &&
577 total > hmask)
578 return 1;
579
580 return 0;
581}
582
5a6d3416 583void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
ecfd6b18
JHS
584{
585 read_lock_bh(&xfrm_policy_lock);
586 si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
587 si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
588 si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
589 si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
590 si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
591 si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
592 si->spdhcnt = xfrm_idx_hmask;
593 si->spdhmcnt = xfrm_policy_hashmax;
594 read_unlock_bh(&xfrm_policy_lock);
595}
596EXPORT_SYMBOL(xfrm_spd_getinfo);
2518c7c2 597
ecfd6b18 598static DEFINE_MUTEX(hash_resize_mutex);
c4028958 599static void xfrm_hash_resize(struct work_struct *__unused)
2518c7c2
DM
600{
601 int dir, total;
602
603 mutex_lock(&hash_resize_mutex);
604
605 total = 0;
606 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
607 if (xfrm_bydst_should_resize(dir, &total))
608 xfrm_bydst_resize(dir);
609 }
610 if (xfrm_byidx_should_resize(total))
611 xfrm_byidx_resize(total);
612
613 mutex_unlock(&hash_resize_mutex);
614}
615
c4028958 616static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
2518c7c2 617
1da177e4
LT
618/* Generate new index... KAME seems to generate them ordered by cost
619 * of an absolute inpredictability of ordering of rules. This will not pass. */
4e81bb83 620static u32 xfrm_gen_index(u8 type, int dir)
1da177e4 621{
1da177e4
LT
622 static u32 idx_generator;
623
624 for (;;) {
2518c7c2
DM
625 struct hlist_node *entry;
626 struct hlist_head *list;
627 struct xfrm_policy *p;
628 u32 idx;
629 int found;
630
1da177e4
LT
631 idx = (idx_generator | dir);
632 idx_generator += 8;
633 if (idx == 0)
634 idx = 8;
2518c7c2
DM
635 list = xfrm_policy_byidx + idx_hash(idx);
636 found = 0;
637 hlist_for_each_entry(p, entry, list, byidx) {
638 if (p->index == idx) {
639 found = 1;
1da177e4 640 break;
2518c7c2 641 }
1da177e4 642 }
2518c7c2 643 if (!found)
1da177e4
LT
644 return idx;
645 }
646}
647
2518c7c2
DM
648static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
649{
650 u32 *p1 = (u32 *) s1;
651 u32 *p2 = (u32 *) s2;
652 int len = sizeof(struct xfrm_selector) / sizeof(u32);
653 int i;
654
655 for (i = 0; i < len; i++) {
656 if (p1[i] != p2[i])
657 return 1;
658 }
659
660 return 0;
661}
662
1da177e4
LT
663int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
664{
2518c7c2
DM
665 struct xfrm_policy *pol;
666 struct xfrm_policy *delpol;
667 struct hlist_head *chain;
a6c7ab55 668 struct hlist_node *entry, *newpos;
9b78a82c 669 struct dst_entry *gc_list;
1da177e4
LT
670
671 write_lock_bh(&xfrm_policy_lock);
2518c7c2
DM
672 chain = policy_hash_bysel(&policy->selector, policy->family, dir);
673 delpol = NULL;
674 newpos = NULL;
2518c7c2 675 hlist_for_each_entry(pol, entry, chain, bydst) {
a6c7ab55 676 if (pol->type == policy->type &&
2518c7c2 677 !selector_cmp(&pol->selector, &policy->selector) &&
a6c7ab55
HX
678 xfrm_sec_ctx_match(pol->security, policy->security) &&
679 !WARN_ON(delpol)) {
1da177e4
LT
680 if (excl) {
681 write_unlock_bh(&xfrm_policy_lock);
682 return -EEXIST;
683 }
1da177e4
LT
684 delpol = pol;
685 if (policy->priority > pol->priority)
686 continue;
687 } else if (policy->priority >= pol->priority) {
a6c7ab55 688 newpos = &pol->bydst;
1da177e4
LT
689 continue;
690 }
1da177e4
LT
691 if (delpol)
692 break;
1da177e4
LT
693 }
694 if (newpos)
2518c7c2
DM
695 hlist_add_after(newpos, &policy->bydst);
696 else
697 hlist_add_head(&policy->bydst, chain);
1da177e4 698 xfrm_pol_hold(policy);
2518c7c2 699 xfrm_policy_count[dir]++;
1da177e4 700 atomic_inc(&flow_cache_genid);
2518c7c2
DM
701 if (delpol) {
702 hlist_del(&delpol->bydst);
703 hlist_del(&delpol->byidx);
704 xfrm_policy_count[dir]--;
705 }
4e81bb83 706 policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
2518c7c2 707 hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
9d729f72 708 policy->curlft.add_time = get_seconds();
1da177e4
LT
709 policy->curlft.use_time = 0;
710 if (!mod_timer(&policy->timer, jiffies + HZ))
711 xfrm_pol_hold(policy);
712 write_unlock_bh(&xfrm_policy_lock);
713
9b78a82c 714 if (delpol)
1da177e4 715 xfrm_policy_kill(delpol);
2518c7c2
DM
716 else if (xfrm_bydst_should_resize(dir, NULL))
717 schedule_work(&xfrm_hash_work);
9b78a82c
DM
718
719 read_lock_bh(&xfrm_policy_lock);
720 gc_list = NULL;
2518c7c2
DM
721 entry = &policy->bydst;
722 hlist_for_each_entry_continue(policy, entry, bydst) {
9b78a82c
DM
723 struct dst_entry *dst;
724
725 write_lock(&policy->lock);
726 dst = policy->bundles;
727 if (dst) {
728 struct dst_entry *tail = dst;
729 while (tail->next)
730 tail = tail->next;
731 tail->next = gc_list;
732 gc_list = dst;
733
734 policy->bundles = NULL;
735 }
736 write_unlock(&policy->lock);
1da177e4 737 }
9b78a82c
DM
738 read_unlock_bh(&xfrm_policy_lock);
739
740 while (gc_list) {
741 struct dst_entry *dst = gc_list;
742
743 gc_list = dst->next;
744 dst_free(dst);
745 }
746
1da177e4
LT
747 return 0;
748}
749EXPORT_SYMBOL(xfrm_policy_insert);
750
4e81bb83
MN
751struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
752 struct xfrm_selector *sel,
ef41aaa0
EP
753 struct xfrm_sec_ctx *ctx, int delete,
754 int *err)
1da177e4 755{
2518c7c2
DM
756 struct xfrm_policy *pol, *ret;
757 struct hlist_head *chain;
758 struct hlist_node *entry;
1da177e4 759
ef41aaa0 760 *err = 0;
1da177e4 761 write_lock_bh(&xfrm_policy_lock);
2518c7c2
DM
762 chain = policy_hash_bysel(sel, sel->family, dir);
763 ret = NULL;
764 hlist_for_each_entry(pol, entry, chain, bydst) {
765 if (pol->type == type &&
766 !selector_cmp(sel, &pol->selector) &&
767 xfrm_sec_ctx_match(ctx, pol->security)) {
1da177e4 768 xfrm_pol_hold(pol);
2518c7c2 769 if (delete) {
ef41aaa0
EP
770 *err = security_xfrm_policy_delete(pol);
771 if (*err) {
772 write_unlock_bh(&xfrm_policy_lock);
773 return pol;
774 }
2518c7c2
DM
775 hlist_del(&pol->bydst);
776 hlist_del(&pol->byidx);
777 xfrm_policy_count[dir]--;
778 }
779 ret = pol;
1da177e4
LT
780 break;
781 }
782 }
783 write_unlock_bh(&xfrm_policy_lock);
784
2518c7c2 785 if (ret && delete) {
1da177e4 786 atomic_inc(&flow_cache_genid);
2518c7c2 787 xfrm_policy_kill(ret);
1da177e4 788 }
2518c7c2 789 return ret;
1da177e4 790}
df71837d 791EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
1da177e4 792
ef41aaa0
EP
793struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
794 int *err)
1da177e4 795{
2518c7c2
DM
796 struct xfrm_policy *pol, *ret;
797 struct hlist_head *chain;
798 struct hlist_node *entry;
1da177e4 799
b5505c6e
HX
800 *err = -ENOENT;
801 if (xfrm_policy_id2dir(id) != dir)
802 return NULL;
803
ef41aaa0 804 *err = 0;
1da177e4 805 write_lock_bh(&xfrm_policy_lock);
2518c7c2
DM
806 chain = xfrm_policy_byidx + idx_hash(id);
807 ret = NULL;
808 hlist_for_each_entry(pol, entry, chain, byidx) {
809 if (pol->type == type && pol->index == id) {
1da177e4 810 xfrm_pol_hold(pol);
2518c7c2 811 if (delete) {
ef41aaa0
EP
812 *err = security_xfrm_policy_delete(pol);
813 if (*err) {
814 write_unlock_bh(&xfrm_policy_lock);
815 return pol;
816 }
2518c7c2
DM
817 hlist_del(&pol->bydst);
818 hlist_del(&pol->byidx);
819 xfrm_policy_count[dir]--;
820 }
821 ret = pol;
1da177e4
LT
822 break;
823 }
824 }
825 write_unlock_bh(&xfrm_policy_lock);
826
2518c7c2 827 if (ret && delete) {
1da177e4 828 atomic_inc(&flow_cache_genid);
2518c7c2 829 xfrm_policy_kill(ret);
1da177e4 830 }
2518c7c2 831 return ret;
1da177e4
LT
832}
833EXPORT_SYMBOL(xfrm_policy_byid);
834
4aa2e62c
JL
835#ifdef CONFIG_SECURITY_NETWORK_XFRM
836static inline int
837xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
1da177e4 838{
4aa2e62c
JL
839 int dir, err = 0;
840
841 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
842 struct xfrm_policy *pol;
843 struct hlist_node *entry;
844 int i;
845
846 hlist_for_each_entry(pol, entry,
847 &xfrm_policy_inexact[dir], bydst) {
848 if (pol->type != type)
849 continue;
850 err = security_xfrm_policy_delete(pol);
851 if (err) {
ab5f5e8b
JL
852 xfrm_audit_policy_delete(pol, 0,
853 audit_info->loginuid,
854 audit_info->secid);
4aa2e62c
JL
855 return err;
856 }
7dc12d6d 857 }
4aa2e62c
JL
858 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
859 hlist_for_each_entry(pol, entry,
860 xfrm_policy_bydst[dir].table + i,
861 bydst) {
862 if (pol->type != type)
863 continue;
864 err = security_xfrm_policy_delete(pol);
865 if (err) {
ab5f5e8b
JL
866 xfrm_audit_policy_delete(pol, 0,
867 audit_info->loginuid,
868 audit_info->secid);
4aa2e62c
JL
869 return err;
870 }
871 }
872 }
873 }
874 return err;
875}
876#else
877static inline int
878xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
879{
880 return 0;
881}
882#endif
883
884int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
885{
886 int dir, err = 0;
1da177e4
LT
887
888 write_lock_bh(&xfrm_policy_lock);
4aa2e62c
JL
889
890 err = xfrm_policy_flush_secctx_check(type, audit_info);
891 if (err)
892 goto out;
893
1da177e4 894 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
2518c7c2
DM
895 struct xfrm_policy *pol;
896 struct hlist_node *entry;
ae8c0577 897 int i, killed;
2518c7c2 898
ae8c0577 899 killed = 0;
2518c7c2
DM
900 again1:
901 hlist_for_each_entry(pol, entry,
902 &xfrm_policy_inexact[dir], bydst) {
903 if (pol->type != type)
904 continue;
905 hlist_del(&pol->bydst);
906 hlist_del(&pol->byidx);
1da177e4
LT
907 write_unlock_bh(&xfrm_policy_lock);
908
ab5f5e8b
JL
909 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
910 audit_info->secid);
161a09e7 911
2518c7c2 912 xfrm_policy_kill(pol);
ae8c0577 913 killed++;
1da177e4
LT
914
915 write_lock_bh(&xfrm_policy_lock);
2518c7c2
DM
916 goto again1;
917 }
918
919 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
920 again2:
921 hlist_for_each_entry(pol, entry,
922 xfrm_policy_bydst[dir].table + i,
923 bydst) {
924 if (pol->type != type)
925 continue;
926 hlist_del(&pol->bydst);
927 hlist_del(&pol->byidx);
928 write_unlock_bh(&xfrm_policy_lock);
929
ab5f5e8b
JL
930 xfrm_audit_policy_delete(pol, 1,
931 audit_info->loginuid,
932 audit_info->secid);
2518c7c2 933 xfrm_policy_kill(pol);
ae8c0577 934 killed++;
2518c7c2
DM
935
936 write_lock_bh(&xfrm_policy_lock);
937 goto again2;
938 }
1da177e4 939 }
2518c7c2 940
ae8c0577 941 xfrm_policy_count[dir] -= killed;
1da177e4
LT
942 }
943 atomic_inc(&flow_cache_genid);
4aa2e62c 944out:
1da177e4 945 write_unlock_bh(&xfrm_policy_lock);
4aa2e62c 946 return err;
1da177e4
LT
947}
948EXPORT_SYMBOL(xfrm_policy_flush);
949
4e81bb83 950int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
1da177e4
LT
951 void *data)
952{
baf5d743 953 struct xfrm_policy *pol, *last = NULL;
2518c7c2 954 struct hlist_node *entry;
baf5d743 955 int dir, last_dir = 0, count, error;
1da177e4
LT
956
957 read_lock_bh(&xfrm_policy_lock);
2518c7c2 958 count = 0;
1da177e4
LT
959
960 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
2518c7c2
DM
961 struct hlist_head *table = xfrm_policy_bydst[dir].table;
962 int i;
963
964 hlist_for_each_entry(pol, entry,
965 &xfrm_policy_inexact[dir], bydst) {
966 if (pol->type != type)
967 continue;
baf5d743
JHS
968 if (last) {
969 error = func(last, last_dir % XFRM_POLICY_MAX,
970 count, data);
971 if (error)
972 goto out;
973 }
974 last = pol;
975 last_dir = dir;
976 count++;
1da177e4 977 }
2518c7c2
DM
978 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
979 hlist_for_each_entry(pol, entry, table + i, bydst) {
980 if (pol->type != type)
981 continue;
baf5d743
JHS
982 if (last) {
983 error = func(last, last_dir % XFRM_POLICY_MAX,
984 count, data);
985 if (error)
986 goto out;
987 }
988 last = pol;
989 last_dir = dir;
990 count++;
2518c7c2
DM
991 }
992 }
1da177e4 993 }
baf5d743
JHS
994 if (count == 0) {
995 error = -ENOENT;
996 goto out;
997 }
998 error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
1da177e4
LT
999out:
1000 read_unlock_bh(&xfrm_policy_lock);
1001 return error;
1002}
1003EXPORT_SYMBOL(xfrm_policy_walk);
1004
134b0fc5
JM
1005/*
1006 * Find policy to apply to this flow.
1007 *
1008 * Returns 0 if policy found, else an -errno.
1009 */
2518c7c2
DM
1010static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
1011 u8 type, u16 family, int dir)
1da177e4 1012{
2518c7c2 1013 struct xfrm_selector *sel = &pol->selector;
134b0fc5 1014 int match, ret = -ESRCH;
1da177e4 1015
2518c7c2
DM
1016 if (pol->family != family ||
1017 pol->type != type)
134b0fc5 1018 return ret;
1da177e4 1019
2518c7c2 1020 match = xfrm_selector_match(sel, fl, family);
134b0fc5
JM
1021 if (match)
1022 ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
2518c7c2 1023
134b0fc5 1024 return ret;
2518c7c2 1025}
1da177e4 1026
2518c7c2
DM
1027static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
1028 u16 family, u8 dir)
1029{
134b0fc5 1030 int err;
2518c7c2
DM
1031 struct xfrm_policy *pol, *ret;
1032 xfrm_address_t *daddr, *saddr;
1033 struct hlist_node *entry;
1034 struct hlist_head *chain;
acba48e1 1035 u32 priority = ~0U;
df71837d 1036
2518c7c2
DM
1037 daddr = xfrm_flowi_daddr(fl, family);
1038 saddr = xfrm_flowi_saddr(fl, family);
1039 if (unlikely(!daddr || !saddr))
1040 return NULL;
1041
1042 read_lock_bh(&xfrm_policy_lock);
1043 chain = policy_hash_direct(daddr, saddr, family, dir);
1044 ret = NULL;
1045 hlist_for_each_entry(pol, entry, chain, bydst) {
134b0fc5
JM
1046 err = xfrm_policy_match(pol, fl, type, family, dir);
1047 if (err) {
1048 if (err == -ESRCH)
1049 continue;
1050 else {
1051 ret = ERR_PTR(err);
1052 goto fail;
1053 }
1054 } else {
2518c7c2 1055 ret = pol;
acba48e1 1056 priority = ret->priority;
2518c7c2
DM
1057 break;
1058 }
1059 }
acba48e1
DM
1060 chain = &xfrm_policy_inexact[dir];
1061 hlist_for_each_entry(pol, entry, chain, bydst) {
134b0fc5
JM
1062 err = xfrm_policy_match(pol, fl, type, family, dir);
1063 if (err) {
1064 if (err == -ESRCH)
1065 continue;
1066 else {
1067 ret = ERR_PTR(err);
1068 goto fail;
1069 }
1070 } else if (pol->priority < priority) {
acba48e1
DM
1071 ret = pol;
1072 break;
1da177e4
LT
1073 }
1074 }
acba48e1
DM
1075 if (ret)
1076 xfrm_pol_hold(ret);
134b0fc5 1077fail:
1da177e4 1078 read_unlock_bh(&xfrm_policy_lock);
4e81bb83 1079
2518c7c2 1080 return ret;
4e81bb83
MN
1081}
1082
134b0fc5 1083static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
4e81bb83
MN
1084 void **objp, atomic_t **obj_refp)
1085{
1086 struct xfrm_policy *pol;
134b0fc5 1087 int err = 0;
4e81bb83
MN
1088
1089#ifdef CONFIG_XFRM_SUB_POLICY
1090 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
134b0fc5
JM
1091 if (IS_ERR(pol)) {
1092 err = PTR_ERR(pol);
1093 pol = NULL;
1094 }
1095 if (pol || err)
4e81bb83
MN
1096 goto end;
1097#endif
1098 pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
134b0fc5
JM
1099 if (IS_ERR(pol)) {
1100 err = PTR_ERR(pol);
1101 pol = NULL;
1102 }
4e81bb83 1103#ifdef CONFIG_XFRM_SUB_POLICY
2518c7c2 1104end:
4e81bb83 1105#endif
1da177e4
LT
1106 if ((*objp = (void *) pol) != NULL)
1107 *obj_refp = &pol->refcnt;
134b0fc5 1108 return err;
1da177e4
LT
1109}
1110
df71837d
TJ
1111static inline int policy_to_flow_dir(int dir)
1112{
1113 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
a716c119
YH
1114 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1115 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1116 return dir;
1117 switch (dir) {
1118 default:
1119 case XFRM_POLICY_IN:
1120 return FLOW_DIR_IN;
1121 case XFRM_POLICY_OUT:
1122 return FLOW_DIR_OUT;
1123 case XFRM_POLICY_FWD:
1124 return FLOW_DIR_FWD;
3ff50b79 1125 }
df71837d
TJ
1126}
1127
e0d1caa7 1128static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
1da177e4
LT
1129{
1130 struct xfrm_policy *pol;
1131
1132 read_lock_bh(&xfrm_policy_lock);
1133 if ((pol = sk->sk_policy[dir]) != NULL) {
a716c119 1134 int match = xfrm_selector_match(&pol->selector, fl,
1da177e4 1135 sk->sk_family);
a716c119 1136 int err = 0;
df71837d 1137
3bccfbc7
VY
1138 if (match) {
1139 err = security_xfrm_policy_lookup(pol, fl->secid,
1140 policy_to_flow_dir(dir));
1141 if (!err)
1142 xfrm_pol_hold(pol);
1143 else if (err == -ESRCH)
1144 pol = NULL;
1145 else
1146 pol = ERR_PTR(err);
1147 } else
1da177e4
LT
1148 pol = NULL;
1149 }
1150 read_unlock_bh(&xfrm_policy_lock);
1151 return pol;
1152}
1153
1154static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1155{
2518c7c2
DM
1156 struct hlist_head *chain = policy_hash_bysel(&pol->selector,
1157 pol->family, dir);
4e81bb83 1158
2518c7c2
DM
1159 hlist_add_head(&pol->bydst, chain);
1160 hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
1161 xfrm_policy_count[dir]++;
1da177e4 1162 xfrm_pol_hold(pol);
2518c7c2
DM
1163
1164 if (xfrm_bydst_should_resize(dir, NULL))
1165 schedule_work(&xfrm_hash_work);
1da177e4
LT
1166}
1167
1168static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1169 int dir)
1170{
2518c7c2
DM
1171 if (hlist_unhashed(&pol->bydst))
1172 return NULL;
1da177e4 1173
2518c7c2
DM
1174 hlist_del(&pol->bydst);
1175 hlist_del(&pol->byidx);
1176 xfrm_policy_count[dir]--;
1177
1178 return pol;
1da177e4
LT
1179}
1180
4666faab 1181int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1da177e4
LT
1182{
1183 write_lock_bh(&xfrm_policy_lock);
1184 pol = __xfrm_policy_unlink(pol, dir);
1185 write_unlock_bh(&xfrm_policy_lock);
1186 if (pol) {
1187 if (dir < XFRM_POLICY_MAX)
1188 atomic_inc(&flow_cache_genid);
1189 xfrm_policy_kill(pol);
4666faab 1190 return 0;
1da177e4 1191 }
4666faab 1192 return -ENOENT;
1da177e4 1193}
a70fcb0b 1194EXPORT_SYMBOL(xfrm_policy_delete);
1da177e4
LT
1195
1196int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1197{
1198 struct xfrm_policy *old_pol;
1199
4e81bb83
MN
1200#ifdef CONFIG_XFRM_SUB_POLICY
1201 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1202 return -EINVAL;
1203#endif
1204
1da177e4
LT
1205 write_lock_bh(&xfrm_policy_lock);
1206 old_pol = sk->sk_policy[dir];
1207 sk->sk_policy[dir] = pol;
1208 if (pol) {
9d729f72 1209 pol->curlft.add_time = get_seconds();
4e81bb83 1210 pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
1da177e4
LT
1211 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1212 }
1213 if (old_pol)
1214 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1215 write_unlock_bh(&xfrm_policy_lock);
1216
1217 if (old_pol) {
1218 xfrm_policy_kill(old_pol);
1219 }
1220 return 0;
1221}
1222
1223static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1224{
1225 struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
1226
1227 if (newp) {
1228 newp->selector = old->selector;
df71837d
TJ
1229 if (security_xfrm_policy_clone(old, newp)) {
1230 kfree(newp);
1231 return NULL; /* ENOMEM */
1232 }
1da177e4
LT
1233 newp->lft = old->lft;
1234 newp->curlft = old->curlft;
1235 newp->action = old->action;
1236 newp->flags = old->flags;
1237 newp->xfrm_nr = old->xfrm_nr;
1238 newp->index = old->index;
4e81bb83 1239 newp->type = old->type;
1da177e4
LT
1240 memcpy(newp->xfrm_vec, old->xfrm_vec,
1241 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1242 write_lock_bh(&xfrm_policy_lock);
1243 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1244 write_unlock_bh(&xfrm_policy_lock);
1245 xfrm_pol_put(newp);
1246 }
1247 return newp;
1248}
1249
1250int __xfrm_sk_clone_policy(struct sock *sk)
1251{
1252 struct xfrm_policy *p0 = sk->sk_policy[0],
1253 *p1 = sk->sk_policy[1];
1254
1255 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1256 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1257 return -ENOMEM;
1258 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1259 return -ENOMEM;
1260 return 0;
1261}
1262
a1e59abf
PM
1263static int
1264xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
1265 unsigned short family)
1266{
1267 int err;
1268 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1269
1270 if (unlikely(afinfo == NULL))
1271 return -EINVAL;
1272 err = afinfo->get_saddr(local, remote);
1273 xfrm_policy_put_afinfo(afinfo);
1274 return err;
1275}
1276
1da177e4
LT
1277/* Resolve list of templates for the flow, given policy. */
1278
1279static int
4e81bb83
MN
1280xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1281 struct xfrm_state **xfrm,
1282 unsigned short family)
1da177e4
LT
1283{
1284 int nx;
1285 int i, error;
1286 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1287 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
a1e59abf 1288 xfrm_address_t tmp;
1da177e4
LT
1289
1290 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1291 struct xfrm_state *x;
1292 xfrm_address_t *remote = daddr;
1293 xfrm_address_t *local = saddr;
1294 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1295
48b8d783
JK
1296 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1297 tmpl->mode == XFRM_MODE_BEET) {
1da177e4
LT
1298 remote = &tmpl->id.daddr;
1299 local = &tmpl->saddr;
76b3f055 1300 family = tmpl->encap_family;
a1e59abf
PM
1301 if (xfrm_addr_any(local, family)) {
1302 error = xfrm_get_saddr(&tmp, remote, family);
1303 if (error)
1304 goto fail;
1305 local = &tmp;
1306 }
1da177e4
LT
1307 }
1308
1309 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1310
1311 if (x && x->km.state == XFRM_STATE_VALID) {
1312 xfrm[nx++] = x;
1313 daddr = remote;
1314 saddr = local;
1315 continue;
1316 }
1317 if (x) {
1318 error = (x->km.state == XFRM_STATE_ERROR ?
1319 -EINVAL : -EAGAIN);
1320 xfrm_state_put(x);
1321 }
1322
1323 if (!tmpl->optional)
1324 goto fail;
1325 }
1326 return nx;
1327
1328fail:
1329 for (nx--; nx>=0; nx--)
1330 xfrm_state_put(xfrm[nx]);
1331 return error;
1332}
1333
4e81bb83
MN
1334static int
1335xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1336 struct xfrm_state **xfrm,
1337 unsigned short family)
1338{
41a49cc3
MN
1339 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1340 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
4e81bb83
MN
1341 int cnx = 0;
1342 int error;
1343 int ret;
1344 int i;
1345
1346 for (i = 0; i < npols; i++) {
1347 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1348 error = -ENOBUFS;
1349 goto fail;
1350 }
41a49cc3
MN
1351
1352 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
4e81bb83
MN
1353 if (ret < 0) {
1354 error = ret;
1355 goto fail;
1356 } else
1357 cnx += ret;
1358 }
1359
41a49cc3
MN
1360 /* found states are sorted for outbound processing */
1361 if (npols > 1)
1362 xfrm_state_sort(xfrm, tpp, cnx, family);
1363
4e81bb83
MN
1364 return cnx;
1365
1366 fail:
1367 for (cnx--; cnx>=0; cnx--)
41a49cc3 1368 xfrm_state_put(tpp[cnx]);
4e81bb83
MN
1369 return error;
1370
1371}
1372
1da177e4
LT
1373/* Check that the bundle accepts the flow and its components are
1374 * still valid.
1375 */
1376
1377static struct dst_entry *
1378xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1379{
1380 struct dst_entry *x;
1381 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1382 if (unlikely(afinfo == NULL))
1383 return ERR_PTR(-EINVAL);
1384 x = afinfo->find_bundle(fl, policy);
1385 xfrm_policy_put_afinfo(afinfo);
1386 return x;
1387}
1388
1389/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1390 * all the metrics... Shortly, bundle a bundle.
1391 */
1392
1393static int
1394xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
1395 struct flowi *fl, struct dst_entry **dst_p,
1396 unsigned short family)
1397{
1398 int err;
1399 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1400 if (unlikely(afinfo == NULL))
1401 return -EINVAL;
1402 err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
1403 xfrm_policy_put_afinfo(afinfo);
1404 return err;
1405}
1406
157bfc25
MN
1407static int inline
1408xfrm_dst_alloc_copy(void **target, void *src, int size)
1409{
1410 if (!*target) {
1411 *target = kmalloc(size, GFP_ATOMIC);
1412 if (!*target)
1413 return -ENOMEM;
1414 }
1415 memcpy(*target, src, size);
1416 return 0;
1417}
1418
1419static int inline
1420xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
1421{
1422#ifdef CONFIG_XFRM_SUB_POLICY
1423 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1424 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1425 sel, sizeof(*sel));
1426#else
1427 return 0;
1428#endif
1429}
1430
1431static int inline
1432xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
1433{
1434#ifdef CONFIG_XFRM_SUB_POLICY
1435 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1436 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1437#else
1438 return 0;
1439#endif
1440}
1da177e4
LT
1441
1442static int stale_bundle(struct dst_entry *dst);
1443
1444/* Main function: finds/creates a bundle for given flow.
1445 *
1446 * At the moment we eat a raw IP route. Mostly to speed up lookups
1447 * on interfaces with disabled IPsec.
1448 */
14e50e57
DM
1449int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1450 struct sock *sk, int flags)
1da177e4
LT
1451{
1452 struct xfrm_policy *policy;
4e81bb83
MN
1453 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1454 int npols;
1455 int pol_dead;
1456 int xfrm_nr;
1457 int pi;
1da177e4
LT
1458 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1459 struct dst_entry *dst, *dst_orig = *dst_p;
1460 int nx = 0;
1461 int err;
1462 u32 genid;
42cf93cd 1463 u16 family;
df71837d 1464 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
e0d1caa7 1465
1da177e4
LT
1466restart:
1467 genid = atomic_read(&flow_cache_genid);
1468 policy = NULL;
4e81bb83
MN
1469 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1470 pols[pi] = NULL;
1471 npols = 0;
1472 pol_dead = 0;
1473 xfrm_nr = 0;
1474
f7944fb1 1475 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
e0d1caa7 1476 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
3bccfbc7
VY
1477 if (IS_ERR(policy))
1478 return PTR_ERR(policy);
1479 }
1da177e4
LT
1480
1481 if (!policy) {
1482 /* To accelerate a bit... */
2518c7c2
DM
1483 if ((dst_orig->flags & DST_NOXFRM) ||
1484 !xfrm_policy_count[XFRM_POLICY_OUT])
1da177e4
LT
1485 return 0;
1486
e0d1caa7 1487 policy = flow_cache_lookup(fl, dst_orig->ops->family,
42cf93cd 1488 dir, xfrm_policy_lookup);
134b0fc5
JM
1489 if (IS_ERR(policy))
1490 return PTR_ERR(policy);
1da177e4
LT
1491 }
1492
1493 if (!policy)
1494 return 0;
1495
42cf93cd 1496 family = dst_orig->ops->family;
9d729f72 1497 policy->curlft.use_time = get_seconds();
4e81bb83
MN
1498 pols[0] = policy;
1499 npols ++;
1500 xfrm_nr += pols[0]->xfrm_nr;
1da177e4
LT
1501
1502 switch (policy->action) {
1503 case XFRM_POLICY_BLOCK:
1504 /* Prohibit the flow */
e104411b
PM
1505 err = -EPERM;
1506 goto error;
1da177e4
LT
1507
1508 case XFRM_POLICY_ALLOW:
4e81bb83 1509#ifndef CONFIG_XFRM_SUB_POLICY
1da177e4
LT
1510 if (policy->xfrm_nr == 0) {
1511 /* Flow passes not transformed. */
1512 xfrm_pol_put(policy);
1513 return 0;
1514 }
4e81bb83 1515#endif
1da177e4
LT
1516
1517 /* Try to find matching bundle.
1518 *
1519 * LATER: help from flow cache. It is optional, this
1520 * is required only for output policy.
1521 */
1522 dst = xfrm_find_bundle(fl, policy, family);
1523 if (IS_ERR(dst)) {
e104411b
PM
1524 err = PTR_ERR(dst);
1525 goto error;
1da177e4
LT
1526 }
1527
1528 if (dst)
1529 break;
1530
4e81bb83
MN
1531#ifdef CONFIG_XFRM_SUB_POLICY
1532 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1533 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1534 fl, family,
1535 XFRM_POLICY_OUT);
1536 if (pols[1]) {
134b0fc5
JM
1537 if (IS_ERR(pols[1])) {
1538 err = PTR_ERR(pols[1]);
1539 goto error;
1540 }
4e81bb83
MN
1541 if (pols[1]->action == XFRM_POLICY_BLOCK) {
1542 err = -EPERM;
1543 goto error;
1544 }
1545 npols ++;
1546 xfrm_nr += pols[1]->xfrm_nr;
1547 }
1548 }
1549
1550 /*
1551 * Because neither flowi nor bundle information knows about
1552 * transformation template size. On more than one policy usage
1553 * we can realize whether all of them is bypass or not after
1554 * they are searched. See above not-transformed bypass
1555 * is surrounded by non-sub policy configuration, too.
1556 */
1557 if (xfrm_nr == 0) {
1558 /* Flow passes not transformed. */
1559 xfrm_pols_put(pols, npols);
1560 return 0;
1561 }
1562
1563#endif
1564 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1da177e4
LT
1565
1566 if (unlikely(nx<0)) {
1567 err = nx;
14e50e57
DM
1568 if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
1569 /* EREMOTE tells the caller to generate
1570 * a one-shot blackhole route.
1571 */
1572 xfrm_pol_put(policy);
1573 return -EREMOTE;
1574 }
1da177e4
LT
1575 if (err == -EAGAIN && flags) {
1576 DECLARE_WAITQUEUE(wait, current);
1577
1578 add_wait_queue(&km_waitq, &wait);
1579 set_current_state(TASK_INTERRUPTIBLE);
1580 schedule();
1581 set_current_state(TASK_RUNNING);
1582 remove_wait_queue(&km_waitq, &wait);
1583
4e81bb83 1584 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
1da177e4
LT
1585
1586 if (nx == -EAGAIN && signal_pending(current)) {
1587 err = -ERESTART;
1588 goto error;
1589 }
1590 if (nx == -EAGAIN ||
1591 genid != atomic_read(&flow_cache_genid)) {
4e81bb83 1592 xfrm_pols_put(pols, npols);
1da177e4
LT
1593 goto restart;
1594 }
1595 err = nx;
1596 }
1597 if (err < 0)
1598 goto error;
1599 }
1600 if (nx == 0) {
1601 /* Flow passes not transformed. */
4e81bb83 1602 xfrm_pols_put(pols, npols);
1da177e4
LT
1603 return 0;
1604 }
1605
1606 dst = dst_orig;
1607 err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
1608
1609 if (unlikely(err)) {
1610 int i;
1611 for (i=0; i<nx; i++)
1612 xfrm_state_put(xfrm[i]);
1613 goto error;
1614 }
1615
4e81bb83
MN
1616 for (pi = 0; pi < npols; pi++) {
1617 read_lock_bh(&pols[pi]->lock);
1618 pol_dead |= pols[pi]->dead;
1619 read_unlock_bh(&pols[pi]->lock);
1620 }
1621
1da177e4 1622 write_lock_bh(&policy->lock);
4e81bb83 1623 if (unlikely(pol_dead || stale_bundle(dst))) {
1da177e4
LT
1624 /* Wow! While we worked on resolving, this
1625 * policy has gone. Retry. It is not paranoia,
1626 * we just cannot enlist new bundle to dead object.
1627 * We can't enlist stable bundles either.
1628 */
1629 write_unlock_bh(&policy->lock);
1da177e4
LT
1630 if (dst)
1631 dst_free(dst);
00de651d
HX
1632
1633 err = -EHOSTUNREACH;
1634 goto error;
1da177e4 1635 }
157bfc25
MN
1636
1637 if (npols > 1)
1638 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1639 else
1640 err = xfrm_dst_update_origin(dst, fl);
1641 if (unlikely(err)) {
1642 write_unlock_bh(&policy->lock);
1643 if (dst)
1644 dst_free(dst);
1645 goto error;
1646 }
1647
1da177e4
LT
1648 dst->next = policy->bundles;
1649 policy->bundles = dst;
1650 dst_hold(dst);
1651 write_unlock_bh(&policy->lock);
1652 }
1653 *dst_p = dst;
1654 dst_release(dst_orig);
a716c119 1655 xfrm_pols_put(pols, npols);
1da177e4
LT
1656 return 0;
1657
1658error:
1659 dst_release(dst_orig);
4e81bb83 1660 xfrm_pols_put(pols, npols);
1da177e4
LT
1661 *dst_p = NULL;
1662 return err;
1663}
14e50e57
DM
1664EXPORT_SYMBOL(__xfrm_lookup);
1665
1666int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
1667 struct sock *sk, int flags)
1668{
1669 int err = __xfrm_lookup(dst_p, fl, sk, flags);
1670
1671 if (err == -EREMOTE) {
1672 dst_release(*dst_p);
1673 *dst_p = NULL;
1674 err = -EAGAIN;
1675 }
1676
1677 return err;
1678}
1da177e4
LT
1679EXPORT_SYMBOL(xfrm_lookup);
1680
df0ba92a
MN
1681static inline int
1682xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1683{
1684 struct xfrm_state *x;
1685 int err;
1686
1687 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1688 return 0;
1689 x = skb->sp->xvec[idx];
1690 if (!x->type->reject)
1691 return 0;
1692 xfrm_state_hold(x);
1693 err = x->type->reject(x, skb, fl);
1694 xfrm_state_put(x);
1695 return err;
1696}
1697
1da177e4
LT
1698/* When skb is transformed back to its "native" form, we have to
1699 * check policy restrictions. At the moment we make this in maximally
1700 * stupid way. Shame on me. :-) Of course, connected sockets must
1701 * have policy cached at them.
1702 */
1703
1704static inline int
a716c119 1705xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
1da177e4
LT
1706 unsigned short family)
1707{
1708 if (xfrm_state_kern(x))
928ba416 1709 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
1da177e4
LT
1710 return x->id.proto == tmpl->id.proto &&
1711 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1712 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1713 x->props.mode == tmpl->mode &&
f3bd4840
MN
1714 ((tmpl->aalgos & (1<<x->props.aalgo)) ||
1715 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
7e49e6de
MN
1716 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1717 xfrm_state_addr_cmp(tmpl, x, family));
1da177e4
LT
1718}
1719
df0ba92a
MN
1720/*
1721 * 0 or more than 0 is returned when validation is succeeded (either bypass
1722 * because of optional transport mode, or next index of the mathced secpath
1723 * state with the template.
1724 * -1 is returned when no matching template is found.
1725 * Otherwise "-2 - errored_index" is returned.
1726 */
1da177e4
LT
1727static inline int
1728xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1729 unsigned short family)
1730{
1731 int idx = start;
1732
1733 if (tmpl->optional) {
7e49e6de 1734 if (tmpl->mode == XFRM_MODE_TRANSPORT)
1da177e4
LT
1735 return start;
1736 } else
1737 start = -1;
1738 for (; idx < sp->len; idx++) {
dbe5b4aa 1739 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
1da177e4 1740 return ++idx;
df0ba92a
MN
1741 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1742 if (start == -1)
1743 start = -2-idx;
1da177e4 1744 break;
df0ba92a 1745 }
1da177e4
LT
1746 }
1747 return start;
1748}
1749
3e3850e9
PM
1750int
1751xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
1da177e4
LT
1752{
1753 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
e0d1caa7 1754 int err;
1da177e4
LT
1755
1756 if (unlikely(afinfo == NULL))
1757 return -EAFNOSUPPORT;
1758
1759 afinfo->decode_session(skb, fl);
beb8d13b 1760 err = security_xfrm_decode_session(skb, &fl->secid);
1da177e4 1761 xfrm_policy_put_afinfo(afinfo);
e0d1caa7 1762 return err;
1da177e4 1763}
3e3850e9 1764EXPORT_SYMBOL(xfrm_decode_session);
1da177e4 1765
df0ba92a 1766static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
1da177e4
LT
1767{
1768 for (; k < sp->len; k++) {
df0ba92a 1769 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
d1d9facf 1770 *idxp = k;
1da177e4 1771 return 1;
df0ba92a 1772 }
1da177e4
LT
1773 }
1774
1775 return 0;
1776}
1777
a716c119 1778int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
1da177e4
LT
1779 unsigned short family)
1780{
1781 struct xfrm_policy *pol;
4e81bb83
MN
1782 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1783 int npols = 0;
1784 int xfrm_nr;
1785 int pi;
1da177e4 1786 struct flowi fl;
df71837d 1787 u8 fl_dir = policy_to_flow_dir(dir);
df0ba92a 1788 int xerr_idx = -1;
1da177e4 1789
3e3850e9 1790 if (xfrm_decode_session(skb, &fl, family) < 0)
1da177e4 1791 return 0;
eb9c7ebe 1792 nf_nat_decode_session(skb, &fl, family);
1da177e4
LT
1793
1794 /* First, check used SA against their selectors. */
1795 if (skb->sp) {
1796 int i;
1797
1798 for (i=skb->sp->len-1; i>=0; i--) {
dbe5b4aa
HX
1799 struct xfrm_state *x = skb->sp->xvec[i];
1800 if (!xfrm_selector_match(&x->sel, &fl, family))
1da177e4 1801 return 0;
1da177e4
LT
1802 }
1803 }
1804
1805 pol = NULL;
3bccfbc7 1806 if (sk && sk->sk_policy[dir]) {
e0d1caa7 1807 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
3bccfbc7
VY
1808 if (IS_ERR(pol))
1809 return 0;
1810 }
1da177e4
LT
1811
1812 if (!pol)
e0d1caa7 1813 pol = flow_cache_lookup(&fl, family, fl_dir,
1da177e4
LT
1814 xfrm_policy_lookup);
1815
134b0fc5
JM
1816 if (IS_ERR(pol))
1817 return 0;
1818
df0ba92a 1819 if (!pol) {
d1d9facf 1820 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
df0ba92a
MN
1821 xfrm_secpath_reject(xerr_idx, skb, &fl);
1822 return 0;
1823 }
1824 return 1;
1825 }
1da177e4 1826
9d729f72 1827 pol->curlft.use_time = get_seconds();
1da177e4 1828
4e81bb83
MN
1829 pols[0] = pol;
1830 npols ++;
1831#ifdef CONFIG_XFRM_SUB_POLICY
1832 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1833 pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
1834 &fl, family,
1835 XFRM_POLICY_IN);
1836 if (pols[1]) {
134b0fc5
JM
1837 if (IS_ERR(pols[1]))
1838 return 0;
9d729f72 1839 pols[1]->curlft.use_time = get_seconds();
4e81bb83
MN
1840 npols ++;
1841 }
1842 }
1843#endif
1844
1da177e4
LT
1845 if (pol->action == XFRM_POLICY_ALLOW) {
1846 struct sec_path *sp;
1847 static struct sec_path dummy;
4e81bb83 1848 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
41a49cc3 1849 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
4e81bb83
MN
1850 struct xfrm_tmpl **tpp = tp;
1851 int ti = 0;
1da177e4
LT
1852 int i, k;
1853
1854 if ((sp = skb->sp) == NULL)
1855 sp = &dummy;
1856
4e81bb83
MN
1857 for (pi = 0; pi < npols; pi++) {
1858 if (pols[pi] != pol &&
1859 pols[pi]->action != XFRM_POLICY_ALLOW)
1860 goto reject;
1861 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
1862 goto reject_error;
1863 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1864 tpp[ti++] = &pols[pi]->xfrm_vec[i];
1865 }
1866 xfrm_nr = ti;
41a49cc3
MN
1867 if (npols > 1) {
1868 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
1869 tpp = stp;
1870 }
4e81bb83 1871
1da177e4
LT
1872 /* For each tunnel xfrm, find the first matching tmpl.
1873 * For each tmpl before that, find corresponding xfrm.
1874 * Order is _important_. Later we will implement
1875 * some barriers, but at the moment barriers
1876 * are implied between each two transformations.
1877 */
4e81bb83
MN
1878 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
1879 k = xfrm_policy_ok(tpp[i], sp, k, family);
df0ba92a 1880 if (k < 0) {
d1d9facf
JM
1881 if (k < -1)
1882 /* "-2 - errored_index" returned */
1883 xerr_idx = -(2+k);
1da177e4 1884 goto reject;
df0ba92a 1885 }
1da177e4
LT
1886 }
1887
d1d9facf 1888 if (secpath_has_nontransport(sp, k, &xerr_idx))
1da177e4
LT
1889 goto reject;
1890
4e81bb83 1891 xfrm_pols_put(pols, npols);
1da177e4
LT
1892 return 1;
1893 }
1894
1895reject:
df0ba92a 1896 xfrm_secpath_reject(xerr_idx, skb, &fl);
4e81bb83
MN
1897reject_error:
1898 xfrm_pols_put(pols, npols);
1da177e4
LT
1899 return 0;
1900}
1901EXPORT_SYMBOL(__xfrm_policy_check);
1902
1903int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
1904{
1905 struct flowi fl;
1906
3e3850e9 1907 if (xfrm_decode_session(skb, &fl, family) < 0)
1da177e4
LT
1908 return 0;
1909
1910 return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
1911}
1912EXPORT_SYMBOL(__xfrm_route_forward);
1913
d49c73c7
DM
1914/* Optimize later using cookies and generation ids. */
1915
1da177e4
LT
1916static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
1917{
d49c73c7
DM
1918 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
1919 * to "-1" to force all XFRM destinations to get validated by
1920 * dst_ops->check on every use. We do this because when a
1921 * normal route referenced by an XFRM dst is obsoleted we do
1922 * not go looking around for all parent referencing XFRM dsts
1923 * so that we can invalidate them. It is just too much work.
1924 * Instead we make the checks here on every use. For example:
1925 *
1926 * XFRM dst A --> IPv4 dst X
1927 *
1928 * X is the "xdst->route" of A (X is also the "dst->path" of A
1929 * in this example). If X is marked obsolete, "A" will not
1930 * notice. That's what we are validating here via the
1931 * stale_bundle() check.
1932 *
1933 * When a policy's bundle is pruned, we dst_free() the XFRM
1934 * dst which causes it's ->obsolete field to be set to a
1935 * positive non-zero integer. If an XFRM dst has been pruned
1936 * like this, we want to force a new route lookup.
399c180a 1937 */
d49c73c7
DM
1938 if (dst->obsolete < 0 && !stale_bundle(dst))
1939 return dst;
1940
1da177e4
LT
1941 return NULL;
1942}
1943
1944static int stale_bundle(struct dst_entry *dst)
1945{
5b368e61 1946 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
1da177e4
LT
1947}
1948
aabc9761 1949void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
1da177e4 1950{
1da177e4
LT
1951 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
1952 dst->dev = &loopback_dev;
1953 dev_hold(&loopback_dev);
1954 dev_put(dev);
1955 }
1956}
aabc9761 1957EXPORT_SYMBOL(xfrm_dst_ifdown);
1da177e4
LT
1958
1959static void xfrm_link_failure(struct sk_buff *skb)
1960{
1961 /* Impossible. Such dst must be popped before reaches point of failure. */
1962 return;
1963}
1964
1965static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
1966{
1967 if (dst) {
1968 if (dst->obsolete) {
1969 dst_release(dst);
1970 dst = NULL;
1971 }
1972 }
1973 return dst;
1974}
1975
2518c7c2
DM
1976static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
1977{
1978 struct dst_entry *dst, **dstp;
1979
1980 write_lock(&pol->lock);
1981 dstp = &pol->bundles;
1982 while ((dst=*dstp) != NULL) {
1983 if (func(dst)) {
1984 *dstp = dst->next;
1985 dst->next = *gc_list_p;
1986 *gc_list_p = dst;
1987 } else {
1988 dstp = &dst->next;
1989 }
1990 }
1991 write_unlock(&pol->lock);
1992}
1993
1da177e4
LT
1994static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
1995{
2518c7c2
DM
1996 struct dst_entry *gc_list = NULL;
1997 int dir;
1da177e4
LT
1998
1999 read_lock_bh(&xfrm_policy_lock);
2518c7c2
DM
2000 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2001 struct xfrm_policy *pol;
2002 struct hlist_node *entry;
2003 struct hlist_head *table;
2004 int i;
4e81bb83 2005
2518c7c2
DM
2006 hlist_for_each_entry(pol, entry,
2007 &xfrm_policy_inexact[dir], bydst)
2008 prune_one_bundle(pol, func, &gc_list);
2009
2010 table = xfrm_policy_bydst[dir].table;
2011 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
2012 hlist_for_each_entry(pol, entry, table + i, bydst)
2013 prune_one_bundle(pol, func, &gc_list);
1da177e4
LT
2014 }
2015 }
2016 read_unlock_bh(&xfrm_policy_lock);
2017
2018 while (gc_list) {
2518c7c2 2019 struct dst_entry *dst = gc_list;
1da177e4
LT
2020 gc_list = dst->next;
2021 dst_free(dst);
2022 }
2023}
2024
2025static int unused_bundle(struct dst_entry *dst)
2026{
2027 return !atomic_read(&dst->__refcnt);
2028}
2029
2030static void __xfrm_garbage_collect(void)
2031{
2032 xfrm_prune_bundles(unused_bundle);
2033}
2034
1c095399 2035static int xfrm_flush_bundles(void)
1da177e4
LT
2036{
2037 xfrm_prune_bundles(stale_bundle);
2038 return 0;
2039}
2040
2041void xfrm_init_pmtu(struct dst_entry *dst)
2042{
2043 do {
2044 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2045 u32 pmtu, route_mtu_cached;
2046
2047 pmtu = dst_mtu(dst->child);
2048 xdst->child_mtu_cached = pmtu;
2049
2050 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2051
2052 route_mtu_cached = dst_mtu(xdst->route);
2053 xdst->route_mtu_cached = route_mtu_cached;
2054
2055 if (pmtu > route_mtu_cached)
2056 pmtu = route_mtu_cached;
2057
2058 dst->metrics[RTAX_MTU-1] = pmtu;
2059 } while ((dst = dst->next));
2060}
2061
2062EXPORT_SYMBOL(xfrm_init_pmtu);
2063
2064/* Check that the bundle accepts the flow and its components are
2065 * still valid.
2066 */
2067
5b368e61
VY
2068int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
2069 struct flowi *fl, int family, int strict)
1da177e4
LT
2070{
2071 struct dst_entry *dst = &first->u.dst;
2072 struct xfrm_dst *last;
2073 u32 mtu;
2074
92d63dec 2075 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
1da177e4
LT
2076 (dst->dev && !netif_running(dst->dev)))
2077 return 0;
157bfc25
MN
2078#ifdef CONFIG_XFRM_SUB_POLICY
2079 if (fl) {
2080 if (first->origin && !flow_cache_uli_match(first->origin, fl))
2081 return 0;
2082 if (first->partner &&
2083 !xfrm_selector_match(first->partner, fl, family))
2084 return 0;
2085 }
2086#endif
1da177e4
LT
2087
2088 last = NULL;
2089
2090 do {
2091 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2092
2093 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
2094 return 0;
67f83cbf
VY
2095 if (fl && pol &&
2096 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
e0d1caa7 2097 return 0;
1da177e4
LT
2098 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2099 return 0;
9d4a706d
DM
2100 if (xdst->genid != dst->xfrm->genid)
2101 return 0;
e53820de
MN
2102
2103 if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
2104 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
2105 return 0;
1da177e4
LT
2106
2107 mtu = dst_mtu(dst->child);
2108 if (xdst->child_mtu_cached != mtu) {
2109 last = xdst;
2110 xdst->child_mtu_cached = mtu;
2111 }
2112
92d63dec 2113 if (!dst_check(xdst->route, xdst->route_cookie))
1da177e4
LT
2114 return 0;
2115 mtu = dst_mtu(xdst->route);
2116 if (xdst->route_mtu_cached != mtu) {
2117 last = xdst;
2118 xdst->route_mtu_cached = mtu;
2119 }
2120
2121 dst = dst->child;
2122 } while (dst->xfrm);
2123
2124 if (likely(!last))
2125 return 1;
2126
2127 mtu = last->child_mtu_cached;
2128 for (;;) {
2129 dst = &last->u.dst;
2130
2131 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2132 if (mtu > last->route_mtu_cached)
2133 mtu = last->route_mtu_cached;
2134 dst->metrics[RTAX_MTU-1] = mtu;
2135
2136 if (last == first)
2137 break;
2138
bd0bf076 2139 last = (struct xfrm_dst *)last->u.dst.next;
1da177e4
LT
2140 last->child_mtu_cached = mtu;
2141 }
2142
2143 return 1;
2144}
2145
2146EXPORT_SYMBOL(xfrm_bundle_ok);
2147
1da177e4
LT
2148int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2149{
2150 int err = 0;
2151 if (unlikely(afinfo == NULL))
2152 return -EINVAL;
2153 if (unlikely(afinfo->family >= NPROTO))
2154 return -EAFNOSUPPORT;
e959d812 2155 write_lock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
2156 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2157 err = -ENOBUFS;
2158 else {
2159 struct dst_ops *dst_ops = afinfo->dst_ops;
2160 if (likely(dst_ops->kmem_cachep == NULL))
2161 dst_ops->kmem_cachep = xfrm_dst_cache;
2162 if (likely(dst_ops->check == NULL))
2163 dst_ops->check = xfrm_dst_check;
1da177e4
LT
2164 if (likely(dst_ops->negative_advice == NULL))
2165 dst_ops->negative_advice = xfrm_negative_advice;
2166 if (likely(dst_ops->link_failure == NULL))
2167 dst_ops->link_failure = xfrm_link_failure;
1da177e4
LT
2168 if (likely(afinfo->garbage_collect == NULL))
2169 afinfo->garbage_collect = __xfrm_garbage_collect;
2170 xfrm_policy_afinfo[afinfo->family] = afinfo;
2171 }
e959d812 2172 write_unlock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
2173 return err;
2174}
2175EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2176
2177int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2178{
2179 int err = 0;
2180 if (unlikely(afinfo == NULL))
2181 return -EINVAL;
2182 if (unlikely(afinfo->family >= NPROTO))
2183 return -EAFNOSUPPORT;
e959d812 2184 write_lock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
2185 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2186 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2187 err = -EINVAL;
2188 else {
2189 struct dst_ops *dst_ops = afinfo->dst_ops;
2190 xfrm_policy_afinfo[afinfo->family] = NULL;
2191 dst_ops->kmem_cachep = NULL;
2192 dst_ops->check = NULL;
1da177e4
LT
2193 dst_ops->negative_advice = NULL;
2194 dst_ops->link_failure = NULL;
1da177e4
LT
2195 afinfo->garbage_collect = NULL;
2196 }
2197 }
e959d812 2198 write_unlock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
2199 return err;
2200}
2201EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2202
2203static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2204{
2205 struct xfrm_policy_afinfo *afinfo;
2206 if (unlikely(family >= NPROTO))
2207 return NULL;
2208 read_lock(&xfrm_policy_afinfo_lock);
2209 afinfo = xfrm_policy_afinfo[family];
546be240
HX
2210 if (unlikely(!afinfo))
2211 read_unlock(&xfrm_policy_afinfo_lock);
1da177e4
LT
2212 return afinfo;
2213}
2214
2215static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2216{
546be240
HX
2217 read_unlock(&xfrm_policy_afinfo_lock);
2218}
2219
2220static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
2221{
2222 struct xfrm_policy_afinfo *afinfo;
2223 if (unlikely(family >= NPROTO))
2224 return NULL;
2225 write_lock_bh(&xfrm_policy_afinfo_lock);
2226 afinfo = xfrm_policy_afinfo[family];
2227 if (unlikely(!afinfo))
2228 write_unlock_bh(&xfrm_policy_afinfo_lock);
2229 return afinfo;
2230}
2231
2232static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
2233{
2234 write_unlock_bh(&xfrm_policy_afinfo_lock);
1da177e4
LT
2235}
2236
2237static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2238{
e9dc8653
EB
2239 struct net_device *dev = ptr;
2240
2241 if (dev->nd_net != &init_net)
2242 return NOTIFY_DONE;
2243
1da177e4
LT
2244 switch (event) {
2245 case NETDEV_DOWN:
2246 xfrm_flush_bundles();
2247 }
2248 return NOTIFY_DONE;
2249}
2250
2251static struct notifier_block xfrm_dev_notifier = {
2252 xfrm_dev_event,
2253 NULL,
2254 0
2255};
2256
2257static void __init xfrm_policy_init(void)
2258{
2518c7c2
DM
2259 unsigned int hmask, sz;
2260 int dir;
2261
1da177e4
LT
2262 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2263 sizeof(struct xfrm_dst),
e5d679f3 2264 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
20c2df83 2265 NULL);
1da177e4 2266
2518c7c2
DM
2267 hmask = 8 - 1;
2268 sz = (hmask+1) * sizeof(struct hlist_head);
2269
44e36b42 2270 xfrm_policy_byidx = xfrm_hash_alloc(sz);
2518c7c2
DM
2271 xfrm_idx_hmask = hmask;
2272 if (!xfrm_policy_byidx)
2273 panic("XFRM: failed to allocate byidx hash\n");
2274
2275 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2276 struct xfrm_policy_hash *htab;
2277
2278 INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
2279
2280 htab = &xfrm_policy_bydst[dir];
44e36b42 2281 htab->table = xfrm_hash_alloc(sz);
2518c7c2
DM
2282 htab->hmask = hmask;
2283 if (!htab->table)
2284 panic("XFRM: failed to allocate bydst hash\n");
2285 }
2286
c4028958 2287 INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
1da177e4
LT
2288 register_netdevice_notifier(&xfrm_dev_notifier);
2289}
2290
2291void __init xfrm_init(void)
2292{
2293 xfrm_state_init();
2294 xfrm_policy_init();
2295 xfrm_input_init();
2296}
2297
ab5f5e8b
JL
2298#ifdef CONFIG_AUDITSYSCALL
2299static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2300 struct audit_buffer *audit_buf)
2301{
2302 if (xp->security)
2303 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2304 xp->security->ctx_alg, xp->security->ctx_doi,
2305 xp->security->ctx_str);
2306
2307 switch(xp->selector.family) {
2308 case AF_INET:
2309 audit_log_format(audit_buf, " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
2310 NIPQUAD(xp->selector.saddr.a4),
2311 NIPQUAD(xp->selector.daddr.a4));
2312 break;
2313 case AF_INET6:
2314 {
2315 struct in6_addr saddr6, daddr6;
2316
2317 memcpy(&saddr6, xp->selector.saddr.a6,
2318 sizeof(struct in6_addr));
2319 memcpy(&daddr6, xp->selector.daddr.a6,
2320 sizeof(struct in6_addr));
2321 audit_log_format(audit_buf,
2322 " src=" NIP6_FMT " dst=" NIP6_FMT,
2323 NIP6(saddr6), NIP6(daddr6));
2324 }
2325 break;
2326 }
2327}
2328
2329void
2330xfrm_audit_policy_add(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
2331{
2332 struct audit_buffer *audit_buf;
2333 extern int audit_enabled;
2334
2335 if (audit_enabled == 0)
2336 return;
2337 audit_buf = xfrm_audit_start(sid, auid);
2338 if (audit_buf == NULL)
2339 return;
2340 audit_log_format(audit_buf, " op=SPD-add res=%u", result);
2341 xfrm_audit_common_policyinfo(xp, audit_buf);
2342 audit_log_end(audit_buf);
2343}
2344EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2345
2346void
2347xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
2348{
2349 struct audit_buffer *audit_buf;
2350 extern int audit_enabled;
2351
2352 if (audit_enabled == 0)
2353 return;
2354 audit_buf = xfrm_audit_start(sid, auid);
2355 if (audit_buf == NULL)
2356 return;
2357 audit_log_format(audit_buf, " op=SPD-delete res=%u", result);
2358 xfrm_audit_common_policyinfo(xp, audit_buf);
2359 audit_log_end(audit_buf);
2360}
2361EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2362#endif
2363
80c9abaa
SS
2364#ifdef CONFIG_XFRM_MIGRATE
2365static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2366 struct xfrm_selector *sel_tgt)
2367{
2368 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2369 if (sel_tgt->family == sel_cmp->family &&
2370 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
a716c119 2371 sel_cmp->family) == 0 &&
80c9abaa
SS
2372 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2373 sel_cmp->family) == 0 &&
2374 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2375 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2376 return 1;
2377 }
2378 } else {
2379 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2380 return 1;
2381 }
2382 }
2383 return 0;
2384}
2385
2386static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2387 u8 dir, u8 type)
2388{
2389 struct xfrm_policy *pol, *ret = NULL;
2390 struct hlist_node *entry;
2391 struct hlist_head *chain;
2392 u32 priority = ~0U;
2393
2394 read_lock_bh(&xfrm_policy_lock);
2395 chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
2396 hlist_for_each_entry(pol, entry, chain, bydst) {
2397 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2398 pol->type == type) {
2399 ret = pol;
2400 priority = ret->priority;
2401 break;
2402 }
2403 }
2404 chain = &xfrm_policy_inexact[dir];
2405 hlist_for_each_entry(pol, entry, chain, bydst) {
2406 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2407 pol->type == type &&
2408 pol->priority < priority) {
2409 ret = pol;
2410 break;
2411 }
2412 }
2413
2414 if (ret)
2415 xfrm_pol_hold(ret);
2416
2417 read_unlock_bh(&xfrm_policy_lock);
2418
2419 return ret;
2420}
2421
2422static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2423{
2424 int match = 0;
2425
2426 if (t->mode == m->mode && t->id.proto == m->proto &&
2427 (m->reqid == 0 || t->reqid == m->reqid)) {
2428 switch (t->mode) {
2429 case XFRM_MODE_TUNNEL:
2430 case XFRM_MODE_BEET:
2431 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2432 m->old_family) == 0 &&
2433 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2434 m->old_family) == 0) {
2435 match = 1;
2436 }
2437 break;
2438 case XFRM_MODE_TRANSPORT:
2439 /* in case of transport mode, template does not store
2440 any IP addresses, hence we just compare mode and
2441 protocol */
2442 match = 1;
2443 break;
2444 default:
2445 break;
2446 }
2447 }
2448 return match;
2449}
2450
2451/* update endpoint address(es) of template(s) */
2452static int xfrm_policy_migrate(struct xfrm_policy *pol,
2453 struct xfrm_migrate *m, int num_migrate)
2454{
2455 struct xfrm_migrate *mp;
2456 struct dst_entry *dst;
2457 int i, j, n = 0;
2458
2459 write_lock_bh(&pol->lock);
2460 if (unlikely(pol->dead)) {
2461 /* target policy has been deleted */
2462 write_unlock_bh(&pol->lock);
2463 return -ENOENT;
2464 }
2465
2466 for (i = 0; i < pol->xfrm_nr; i++) {
2467 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2468 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2469 continue;
2470 n++;
2471 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL)
2472 continue;
2473 /* update endpoints */
2474 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2475 sizeof(pol->xfrm_vec[i].id.daddr));
2476 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2477 sizeof(pol->xfrm_vec[i].saddr));
2478 pol->xfrm_vec[i].encap_family = mp->new_family;
2479 /* flush bundles */
2480 while ((dst = pol->bundles) != NULL) {
2481 pol->bundles = dst->next;
2482 dst_free(dst);
2483 }
2484 }
2485 }
2486
2487 write_unlock_bh(&pol->lock);
2488
2489 if (!n)
2490 return -ENODATA;
2491
2492 return 0;
2493}
2494
2495static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2496{
2497 int i, j;
2498
2499 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2500 return -EINVAL;
2501
2502 for (i = 0; i < num_migrate; i++) {
2503 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2504 m[i].old_family) == 0) &&
2505 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2506 m[i].old_family) == 0))
2507 return -EINVAL;
2508 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2509 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2510 return -EINVAL;
2511
2512 /* check if there is any duplicated entry */
2513 for (j = i + 1; j < num_migrate; j++) {
2514 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2515 sizeof(m[i].old_daddr)) &&
2516 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2517 sizeof(m[i].old_saddr)) &&
2518 m[i].proto == m[j].proto &&
2519 m[i].mode == m[j].mode &&
2520 m[i].reqid == m[j].reqid &&
2521 m[i].old_family == m[j].old_family)
2522 return -EINVAL;
2523 }
2524 }
2525
2526 return 0;
2527}
2528
2529int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2530 struct xfrm_migrate *m, int num_migrate)
2531{
2532 int i, err, nx_cur = 0, nx_new = 0;
2533 struct xfrm_policy *pol = NULL;
2534 struct xfrm_state *x, *xc;
2535 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2536 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2537 struct xfrm_migrate *mp;
2538
2539 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2540 goto out;
2541
2542 /* Stage 1 - find policy */
2543 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2544 err = -ENOENT;
2545 goto out;
2546 }
2547
2548 /* Stage 2 - find and update state(s) */
2549 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2550 if ((x = xfrm_migrate_state_find(mp))) {
2551 x_cur[nx_cur] = x;
2552 nx_cur++;
2553 if ((xc = xfrm_state_migrate(x, mp))) {
2554 x_new[nx_new] = xc;
2555 nx_new++;
2556 } else {
2557 err = -ENODATA;
2558 goto restore_state;
2559 }
2560 }
2561 }
2562
2563 /* Stage 3 - update policy */
2564 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2565 goto restore_state;
2566
2567 /* Stage 4 - delete old state(s) */
2568 if (nx_cur) {
2569 xfrm_states_put(x_cur, nx_cur);
2570 xfrm_states_delete(x_cur, nx_cur);
2571 }
2572
2573 /* Stage 5 - announce */
2574 km_migrate(sel, dir, type, m, num_migrate);
2575
2576 xfrm_pol_put(pol);
2577
2578 return 0;
2579out:
2580 return err;
2581
2582restore_state:
2583 if (pol)
2584 xfrm_pol_put(pol);
2585 if (nx_cur)
2586 xfrm_states_put(x_cur, nx_cur);
2587 if (nx_new)
2588 xfrm_states_delete(x_new, nx_new);
2589
2590 return err;
2591}
e610e679 2592EXPORT_SYMBOL(xfrm_migrate);
80c9abaa 2593#endif