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