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