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