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