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