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