Merge tag 'md-3.3-fixes' of git://neil.brown.name/md
[platform/adaptation/renesas_rcar/renesas_kernel.git] / net / xfrm / xfrm_state.c
1 /*
2  * xfrm_state.c
3  *
4  * Changes:
5  *      Mitsuru KANDA @USAGI
6  *      Kazunori MIYAZAWA @USAGI
7  *      Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8  *              IPv6 support
9  *      YOSHIFUJI Hideaki @USAGI
10  *              Split up af-specific functions
11  *      Derek Atkins <derek@ihtfp.com>
12  *              Add UDP Encapsulation
13  *
14  */
15
16 #include <linux/workqueue.h>
17 #include <net/xfrm.h>
18 #include <linux/pfkeyv2.h>
19 #include <linux/ipsec.h>
20 #include <linux/module.h>
21 #include <linux/cache.h>
22 #include <linux/audit.h>
23 #include <asm/uaccess.h>
24 #include <linux/ktime.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
28
29 #include "xfrm_hash.h"
30
31 /* Each xfrm_state may be linked to two tables:
32
33    1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
34    2. Hash table by (daddr,family,reqid) to find what SAs exist for given
35       destination/tunnel endpoint. (output)
36  */
37
38 static DEFINE_SPINLOCK(xfrm_state_lock);
39
40 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
41
42 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
43 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
44
45 static inline unsigned int xfrm_dst_hash(struct net *net,
46                                          const xfrm_address_t *daddr,
47                                          const xfrm_address_t *saddr,
48                                          u32 reqid,
49                                          unsigned short family)
50 {
51         return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
52 }
53
54 static inline unsigned int xfrm_src_hash(struct net *net,
55                                          const xfrm_address_t *daddr,
56                                          const xfrm_address_t *saddr,
57                                          unsigned short family)
58 {
59         return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
60 }
61
62 static inline unsigned int
63 xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
64               __be32 spi, u8 proto, unsigned short family)
65 {
66         return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
67 }
68
69 static void xfrm_hash_transfer(struct hlist_head *list,
70                                struct hlist_head *ndsttable,
71                                struct hlist_head *nsrctable,
72                                struct hlist_head *nspitable,
73                                unsigned int nhashmask)
74 {
75         struct hlist_node *entry, *tmp;
76         struct xfrm_state *x;
77
78         hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
79                 unsigned int h;
80
81                 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
82                                     x->props.reqid, x->props.family,
83                                     nhashmask);
84                 hlist_add_head(&x->bydst, ndsttable+h);
85
86                 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
87                                     x->props.family,
88                                     nhashmask);
89                 hlist_add_head(&x->bysrc, nsrctable+h);
90
91                 if (x->id.spi) {
92                         h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
93                                             x->id.proto, x->props.family,
94                                             nhashmask);
95                         hlist_add_head(&x->byspi, nspitable+h);
96                 }
97         }
98 }
99
100 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
101 {
102         return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
103 }
104
105 static DEFINE_MUTEX(hash_resize_mutex);
106
107 static void xfrm_hash_resize(struct work_struct *work)
108 {
109         struct net *net = container_of(work, struct net, xfrm.state_hash_work);
110         struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
111         unsigned long nsize, osize;
112         unsigned int nhashmask, ohashmask;
113         int i;
114
115         mutex_lock(&hash_resize_mutex);
116
117         nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
118         ndst = xfrm_hash_alloc(nsize);
119         if (!ndst)
120                 goto out_unlock;
121         nsrc = xfrm_hash_alloc(nsize);
122         if (!nsrc) {
123                 xfrm_hash_free(ndst, nsize);
124                 goto out_unlock;
125         }
126         nspi = xfrm_hash_alloc(nsize);
127         if (!nspi) {
128                 xfrm_hash_free(ndst, nsize);
129                 xfrm_hash_free(nsrc, nsize);
130                 goto out_unlock;
131         }
132
133         spin_lock_bh(&xfrm_state_lock);
134
135         nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
136         for (i = net->xfrm.state_hmask; i >= 0; i--)
137                 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
138                                    nhashmask);
139
140         odst = net->xfrm.state_bydst;
141         osrc = net->xfrm.state_bysrc;
142         ospi = net->xfrm.state_byspi;
143         ohashmask = net->xfrm.state_hmask;
144
145         net->xfrm.state_bydst = ndst;
146         net->xfrm.state_bysrc = nsrc;
147         net->xfrm.state_byspi = nspi;
148         net->xfrm.state_hmask = nhashmask;
149
150         spin_unlock_bh(&xfrm_state_lock);
151
152         osize = (ohashmask + 1) * sizeof(struct hlist_head);
153         xfrm_hash_free(odst, osize);
154         xfrm_hash_free(osrc, osize);
155         xfrm_hash_free(ospi, osize);
156
157 out_unlock:
158         mutex_unlock(&hash_resize_mutex);
159 }
160
161 static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
162 static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
163
164 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
165
166 int __xfrm_state_delete(struct xfrm_state *x);
167
168 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
169 void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
170
171 static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
172 {
173         struct xfrm_state_afinfo *afinfo;
174         if (unlikely(family >= NPROTO))
175                 return NULL;
176         write_lock_bh(&xfrm_state_afinfo_lock);
177         afinfo = xfrm_state_afinfo[family];
178         if (unlikely(!afinfo))
179                 write_unlock_bh(&xfrm_state_afinfo_lock);
180         return afinfo;
181 }
182
183 static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
184         __releases(xfrm_state_afinfo_lock)
185 {
186         write_unlock_bh(&xfrm_state_afinfo_lock);
187 }
188
189 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
190 {
191         struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
192         const struct xfrm_type **typemap;
193         int err = 0;
194
195         if (unlikely(afinfo == NULL))
196                 return -EAFNOSUPPORT;
197         typemap = afinfo->type_map;
198
199         if (likely(typemap[type->proto] == NULL))
200                 typemap[type->proto] = type;
201         else
202                 err = -EEXIST;
203         xfrm_state_unlock_afinfo(afinfo);
204         return err;
205 }
206 EXPORT_SYMBOL(xfrm_register_type);
207
208 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
209 {
210         struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
211         const struct xfrm_type **typemap;
212         int err = 0;
213
214         if (unlikely(afinfo == NULL))
215                 return -EAFNOSUPPORT;
216         typemap = afinfo->type_map;
217
218         if (unlikely(typemap[type->proto] != type))
219                 err = -ENOENT;
220         else
221                 typemap[type->proto] = NULL;
222         xfrm_state_unlock_afinfo(afinfo);
223         return err;
224 }
225 EXPORT_SYMBOL(xfrm_unregister_type);
226
227 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
228 {
229         struct xfrm_state_afinfo *afinfo;
230         const struct xfrm_type **typemap;
231         const struct xfrm_type *type;
232         int modload_attempted = 0;
233
234 retry:
235         afinfo = xfrm_state_get_afinfo(family);
236         if (unlikely(afinfo == NULL))
237                 return NULL;
238         typemap = afinfo->type_map;
239
240         type = typemap[proto];
241         if (unlikely(type && !try_module_get(type->owner)))
242                 type = NULL;
243         if (!type && !modload_attempted) {
244                 xfrm_state_put_afinfo(afinfo);
245                 request_module("xfrm-type-%d-%d", family, proto);
246                 modload_attempted = 1;
247                 goto retry;
248         }
249
250         xfrm_state_put_afinfo(afinfo);
251         return type;
252 }
253
254 static void xfrm_put_type(const struct xfrm_type *type)
255 {
256         module_put(type->owner);
257 }
258
259 int xfrm_register_mode(struct xfrm_mode *mode, int family)
260 {
261         struct xfrm_state_afinfo *afinfo;
262         struct xfrm_mode **modemap;
263         int err;
264
265         if (unlikely(mode->encap >= XFRM_MODE_MAX))
266                 return -EINVAL;
267
268         afinfo = xfrm_state_lock_afinfo(family);
269         if (unlikely(afinfo == NULL))
270                 return -EAFNOSUPPORT;
271
272         err = -EEXIST;
273         modemap = afinfo->mode_map;
274         if (modemap[mode->encap])
275                 goto out;
276
277         err = -ENOENT;
278         if (!try_module_get(afinfo->owner))
279                 goto out;
280
281         mode->afinfo = afinfo;
282         modemap[mode->encap] = mode;
283         err = 0;
284
285 out:
286         xfrm_state_unlock_afinfo(afinfo);
287         return err;
288 }
289 EXPORT_SYMBOL(xfrm_register_mode);
290
291 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
292 {
293         struct xfrm_state_afinfo *afinfo;
294         struct xfrm_mode **modemap;
295         int err;
296
297         if (unlikely(mode->encap >= XFRM_MODE_MAX))
298                 return -EINVAL;
299
300         afinfo = xfrm_state_lock_afinfo(family);
301         if (unlikely(afinfo == NULL))
302                 return -EAFNOSUPPORT;
303
304         err = -ENOENT;
305         modemap = afinfo->mode_map;
306         if (likely(modemap[mode->encap] == mode)) {
307                 modemap[mode->encap] = NULL;
308                 module_put(mode->afinfo->owner);
309                 err = 0;
310         }
311
312         xfrm_state_unlock_afinfo(afinfo);
313         return err;
314 }
315 EXPORT_SYMBOL(xfrm_unregister_mode);
316
317 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
318 {
319         struct xfrm_state_afinfo *afinfo;
320         struct xfrm_mode *mode;
321         int modload_attempted = 0;
322
323         if (unlikely(encap >= XFRM_MODE_MAX))
324                 return NULL;
325
326 retry:
327         afinfo = xfrm_state_get_afinfo(family);
328         if (unlikely(afinfo == NULL))
329                 return NULL;
330
331         mode = afinfo->mode_map[encap];
332         if (unlikely(mode && !try_module_get(mode->owner)))
333                 mode = NULL;
334         if (!mode && !modload_attempted) {
335                 xfrm_state_put_afinfo(afinfo);
336                 request_module("xfrm-mode-%d-%d", family, encap);
337                 modload_attempted = 1;
338                 goto retry;
339         }
340
341         xfrm_state_put_afinfo(afinfo);
342         return mode;
343 }
344
345 static void xfrm_put_mode(struct xfrm_mode *mode)
346 {
347         module_put(mode->owner);
348 }
349
350 static void xfrm_state_gc_destroy(struct xfrm_state *x)
351 {
352         tasklet_hrtimer_cancel(&x->mtimer);
353         del_timer_sync(&x->rtimer);
354         kfree(x->aalg);
355         kfree(x->ealg);
356         kfree(x->calg);
357         kfree(x->encap);
358         kfree(x->coaddr);
359         kfree(x->replay_esn);
360         kfree(x->preplay_esn);
361         if (x->inner_mode)
362                 xfrm_put_mode(x->inner_mode);
363         if (x->inner_mode_iaf)
364                 xfrm_put_mode(x->inner_mode_iaf);
365         if (x->outer_mode)
366                 xfrm_put_mode(x->outer_mode);
367         if (x->type) {
368                 x->type->destructor(x);
369                 xfrm_put_type(x->type);
370         }
371         security_xfrm_state_free(x);
372         kfree(x);
373 }
374
375 static void xfrm_state_gc_task(struct work_struct *work)
376 {
377         struct net *net = container_of(work, struct net, xfrm.state_gc_work);
378         struct xfrm_state *x;
379         struct hlist_node *entry, *tmp;
380         struct hlist_head gc_list;
381
382         spin_lock_bh(&xfrm_state_gc_lock);
383         hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
384         spin_unlock_bh(&xfrm_state_gc_lock);
385
386         hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
387                 xfrm_state_gc_destroy(x);
388
389         wake_up(&net->xfrm.km_waitq);
390 }
391
392 static inline unsigned long make_jiffies(long secs)
393 {
394         if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
395                 return MAX_SCHEDULE_TIMEOUT-1;
396         else
397                 return secs*HZ;
398 }
399
400 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
401 {
402         struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
403         struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
404         struct net *net = xs_net(x);
405         unsigned long now = get_seconds();
406         long next = LONG_MAX;
407         int warn = 0;
408         int err = 0;
409
410         spin_lock(&x->lock);
411         if (x->km.state == XFRM_STATE_DEAD)
412                 goto out;
413         if (x->km.state == XFRM_STATE_EXPIRED)
414                 goto expired;
415         if (x->lft.hard_add_expires_seconds) {
416                 long tmo = x->lft.hard_add_expires_seconds +
417                         x->curlft.add_time - now;
418                 if (tmo <= 0)
419                         goto expired;
420                 if (tmo < next)
421                         next = tmo;
422         }
423         if (x->lft.hard_use_expires_seconds) {
424                 long tmo = x->lft.hard_use_expires_seconds +
425                         (x->curlft.use_time ? : now) - now;
426                 if (tmo <= 0)
427                         goto expired;
428                 if (tmo < next)
429                         next = tmo;
430         }
431         if (x->km.dying)
432                 goto resched;
433         if (x->lft.soft_add_expires_seconds) {
434                 long tmo = x->lft.soft_add_expires_seconds +
435                         x->curlft.add_time - now;
436                 if (tmo <= 0)
437                         warn = 1;
438                 else if (tmo < next)
439                         next = tmo;
440         }
441         if (x->lft.soft_use_expires_seconds) {
442                 long tmo = x->lft.soft_use_expires_seconds +
443                         (x->curlft.use_time ? : now) - now;
444                 if (tmo <= 0)
445                         warn = 1;
446                 else if (tmo < next)
447                         next = tmo;
448         }
449
450         x->km.dying = warn;
451         if (warn)
452                 km_state_expired(x, 0, 0);
453 resched:
454         if (next != LONG_MAX){
455                 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
456         }
457
458         goto out;
459
460 expired:
461         if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
462                 x->km.state = XFRM_STATE_EXPIRED;
463                 wake_up(&net->xfrm.km_waitq);
464                 next = 2;
465                 goto resched;
466         }
467
468         err = __xfrm_state_delete(x);
469         if (!err && x->id.spi)
470                 km_state_expired(x, 1, 0);
471
472         xfrm_audit_state_delete(x, err ? 0 : 1,
473                                 audit_get_loginuid(current),
474                                 audit_get_sessionid(current), 0);
475
476 out:
477         spin_unlock(&x->lock);
478         return HRTIMER_NORESTART;
479 }
480
481 static void xfrm_replay_timer_handler(unsigned long data);
482
483 struct xfrm_state *xfrm_state_alloc(struct net *net)
484 {
485         struct xfrm_state *x;
486
487         x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
488
489         if (x) {
490                 write_pnet(&x->xs_net, net);
491                 atomic_set(&x->refcnt, 1);
492                 atomic_set(&x->tunnel_users, 0);
493                 INIT_LIST_HEAD(&x->km.all);
494                 INIT_HLIST_NODE(&x->bydst);
495                 INIT_HLIST_NODE(&x->bysrc);
496                 INIT_HLIST_NODE(&x->byspi);
497                 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
498                 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
499                                 (unsigned long)x);
500                 x->curlft.add_time = get_seconds();
501                 x->lft.soft_byte_limit = XFRM_INF;
502                 x->lft.soft_packet_limit = XFRM_INF;
503                 x->lft.hard_byte_limit = XFRM_INF;
504                 x->lft.hard_packet_limit = XFRM_INF;
505                 x->replay_maxage = 0;
506                 x->replay_maxdiff = 0;
507                 x->inner_mode = NULL;
508                 x->inner_mode_iaf = NULL;
509                 spin_lock_init(&x->lock);
510         }
511         return x;
512 }
513 EXPORT_SYMBOL(xfrm_state_alloc);
514
515 void __xfrm_state_destroy(struct xfrm_state *x)
516 {
517         struct net *net = xs_net(x);
518
519         WARN_ON(x->km.state != XFRM_STATE_DEAD);
520
521         spin_lock_bh(&xfrm_state_gc_lock);
522         hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
523         spin_unlock_bh(&xfrm_state_gc_lock);
524         schedule_work(&net->xfrm.state_gc_work);
525 }
526 EXPORT_SYMBOL(__xfrm_state_destroy);
527
528 int __xfrm_state_delete(struct xfrm_state *x)
529 {
530         struct net *net = xs_net(x);
531         int err = -ESRCH;
532
533         if (x->km.state != XFRM_STATE_DEAD) {
534                 x->km.state = XFRM_STATE_DEAD;
535                 spin_lock(&xfrm_state_lock);
536                 list_del(&x->km.all);
537                 hlist_del(&x->bydst);
538                 hlist_del(&x->bysrc);
539                 if (x->id.spi)
540                         hlist_del(&x->byspi);
541                 net->xfrm.state_num--;
542                 spin_unlock(&xfrm_state_lock);
543
544                 /* All xfrm_state objects are created by xfrm_state_alloc.
545                  * The xfrm_state_alloc call gives a reference, and that
546                  * is what we are dropping here.
547                  */
548                 xfrm_state_put(x);
549                 err = 0;
550         }
551
552         return err;
553 }
554 EXPORT_SYMBOL(__xfrm_state_delete);
555
556 int xfrm_state_delete(struct xfrm_state *x)
557 {
558         int err;
559
560         spin_lock_bh(&x->lock);
561         err = __xfrm_state_delete(x);
562         spin_unlock_bh(&x->lock);
563
564         return err;
565 }
566 EXPORT_SYMBOL(xfrm_state_delete);
567
568 #ifdef CONFIG_SECURITY_NETWORK_XFRM
569 static inline int
570 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
571 {
572         int i, err = 0;
573
574         for (i = 0; i <= net->xfrm.state_hmask; i++) {
575                 struct hlist_node *entry;
576                 struct xfrm_state *x;
577
578                 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
579                         if (xfrm_id_proto_match(x->id.proto, proto) &&
580                            (err = security_xfrm_state_delete(x)) != 0) {
581                                 xfrm_audit_state_delete(x, 0,
582                                                         audit_info->loginuid,
583                                                         audit_info->sessionid,
584                                                         audit_info->secid);
585                                 return err;
586                         }
587                 }
588         }
589
590         return err;
591 }
592 #else
593 static inline int
594 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
595 {
596         return 0;
597 }
598 #endif
599
600 int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
601 {
602         int i, err = 0, cnt = 0;
603
604         spin_lock_bh(&xfrm_state_lock);
605         err = xfrm_state_flush_secctx_check(net, proto, audit_info);
606         if (err)
607                 goto out;
608
609         err = -ESRCH;
610         for (i = 0; i <= net->xfrm.state_hmask; i++) {
611                 struct hlist_node *entry;
612                 struct xfrm_state *x;
613 restart:
614                 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
615                         if (!xfrm_state_kern(x) &&
616                             xfrm_id_proto_match(x->id.proto, proto)) {
617                                 xfrm_state_hold(x);
618                                 spin_unlock_bh(&xfrm_state_lock);
619
620                                 err = xfrm_state_delete(x);
621                                 xfrm_audit_state_delete(x, err ? 0 : 1,
622                                                         audit_info->loginuid,
623                                                         audit_info->sessionid,
624                                                         audit_info->secid);
625                                 xfrm_state_put(x);
626                                 if (!err)
627                                         cnt++;
628
629                                 spin_lock_bh(&xfrm_state_lock);
630                                 goto restart;
631                         }
632                 }
633         }
634         if (cnt)
635                 err = 0;
636
637 out:
638         spin_unlock_bh(&xfrm_state_lock);
639         wake_up(&net->xfrm.km_waitq);
640         return err;
641 }
642 EXPORT_SYMBOL(xfrm_state_flush);
643
644 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
645 {
646         spin_lock_bh(&xfrm_state_lock);
647         si->sadcnt = net->xfrm.state_num;
648         si->sadhcnt = net->xfrm.state_hmask;
649         si->sadhmcnt = xfrm_state_hashmax;
650         spin_unlock_bh(&xfrm_state_lock);
651 }
652 EXPORT_SYMBOL(xfrm_sad_getinfo);
653
654 static int
655 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
656                     const struct xfrm_tmpl *tmpl,
657                     const xfrm_address_t *daddr, const xfrm_address_t *saddr,
658                     unsigned short family)
659 {
660         struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
661         if (!afinfo)
662                 return -1;
663         afinfo->init_tempsel(&x->sel, fl);
664
665         if (family != tmpl->encap_family) {
666                 xfrm_state_put_afinfo(afinfo);
667                 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
668                 if (!afinfo)
669                         return -1;
670         }
671         afinfo->init_temprop(x, tmpl, daddr, saddr);
672         xfrm_state_put_afinfo(afinfo);
673         return 0;
674 }
675
676 static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
677                                               const xfrm_address_t *daddr,
678                                               __be32 spi, u8 proto,
679                                               unsigned short family)
680 {
681         unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
682         struct xfrm_state *x;
683         struct hlist_node *entry;
684
685         hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
686                 if (x->props.family != family ||
687                     x->id.spi       != spi ||
688                     x->id.proto     != proto ||
689                     xfrm_addr_cmp(&x->id.daddr, daddr, family))
690                         continue;
691
692                 if ((mark & x->mark.m) != x->mark.v)
693                         continue;
694                 xfrm_state_hold(x);
695                 return x;
696         }
697
698         return NULL;
699 }
700
701 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
702                                                      const xfrm_address_t *daddr,
703                                                      const xfrm_address_t *saddr,
704                                                      u8 proto, unsigned short family)
705 {
706         unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
707         struct xfrm_state *x;
708         struct hlist_node *entry;
709
710         hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
711                 if (x->props.family != family ||
712                     x->id.proto     != proto ||
713                     xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
714                     xfrm_addr_cmp(&x->props.saddr, saddr, family))
715                         continue;
716
717                 if ((mark & x->mark.m) != x->mark.v)
718                         continue;
719                 xfrm_state_hold(x);
720                 return x;
721         }
722
723         return NULL;
724 }
725
726 static inline struct xfrm_state *
727 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
728 {
729         struct net *net = xs_net(x);
730         u32 mark = x->mark.v & x->mark.m;
731
732         if (use_spi)
733                 return __xfrm_state_lookup(net, mark, &x->id.daddr,
734                                            x->id.spi, x->id.proto, family);
735         else
736                 return __xfrm_state_lookup_byaddr(net, mark,
737                                                   &x->id.daddr,
738                                                   &x->props.saddr,
739                                                   x->id.proto, family);
740 }
741
742 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
743 {
744         if (have_hash_collision &&
745             (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
746             net->xfrm.state_num > net->xfrm.state_hmask)
747                 schedule_work(&net->xfrm.state_hash_work);
748 }
749
750 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
751                                const struct flowi *fl, unsigned short family,
752                                struct xfrm_state **best, int *acq_in_progress,
753                                int *error)
754 {
755         /* Resolution logic:
756          * 1. There is a valid state with matching selector. Done.
757          * 2. Valid state with inappropriate selector. Skip.
758          *
759          * Entering area of "sysdeps".
760          *
761          * 3. If state is not valid, selector is temporary, it selects
762          *    only session which triggered previous resolution. Key
763          *    manager will do something to install a state with proper
764          *    selector.
765          */
766         if (x->km.state == XFRM_STATE_VALID) {
767                 if ((x->sel.family &&
768                      !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
769                     !security_xfrm_state_pol_flow_match(x, pol, fl))
770                         return;
771
772                 if (!*best ||
773                     (*best)->km.dying > x->km.dying ||
774                     ((*best)->km.dying == x->km.dying &&
775                      (*best)->curlft.add_time < x->curlft.add_time))
776                         *best = x;
777         } else if (x->km.state == XFRM_STATE_ACQ) {
778                 *acq_in_progress = 1;
779         } else if (x->km.state == XFRM_STATE_ERROR ||
780                    x->km.state == XFRM_STATE_EXPIRED) {
781                 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
782                     security_xfrm_state_pol_flow_match(x, pol, fl))
783                         *error = -ESRCH;
784         }
785 }
786
787 struct xfrm_state *
788 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
789                 const struct flowi *fl, struct xfrm_tmpl *tmpl,
790                 struct xfrm_policy *pol, int *err,
791                 unsigned short family)
792 {
793         static xfrm_address_t saddr_wildcard = { };
794         struct net *net = xp_net(pol);
795         unsigned int h, h_wildcard;
796         struct hlist_node *entry;
797         struct xfrm_state *x, *x0, *to_put;
798         int acquire_in_progress = 0;
799         int error = 0;
800         struct xfrm_state *best = NULL;
801         u32 mark = pol->mark.v & pol->mark.m;
802         unsigned short encap_family = tmpl->encap_family;
803
804         to_put = NULL;
805
806         spin_lock_bh(&xfrm_state_lock);
807         h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
808         hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
809                 if (x->props.family == encap_family &&
810                     x->props.reqid == tmpl->reqid &&
811                     (mark & x->mark.m) == x->mark.v &&
812                     !(x->props.flags & XFRM_STATE_WILDRECV) &&
813                     xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
814                     tmpl->mode == x->props.mode &&
815                     tmpl->id.proto == x->id.proto &&
816                     (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
817                         xfrm_state_look_at(pol, x, fl, encap_family,
818                                            &best, &acquire_in_progress, &error);
819         }
820         if (best)
821                 goto found;
822
823         h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
824         hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
825                 if (x->props.family == encap_family &&
826                     x->props.reqid == tmpl->reqid &&
827                     (mark & x->mark.m) == x->mark.v &&
828                     !(x->props.flags & XFRM_STATE_WILDRECV) &&
829                     xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
830                     tmpl->mode == x->props.mode &&
831                     tmpl->id.proto == x->id.proto &&
832                     (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
833                         xfrm_state_look_at(pol, x, fl, encap_family,
834                                            &best, &acquire_in_progress, &error);
835         }
836
837 found:
838         x = best;
839         if (!x && !error && !acquire_in_progress) {
840                 if (tmpl->id.spi &&
841                     (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
842                                               tmpl->id.proto, encap_family)) != NULL) {
843                         to_put = x0;
844                         error = -EEXIST;
845                         goto out;
846                 }
847                 x = xfrm_state_alloc(net);
848                 if (x == NULL) {
849                         error = -ENOMEM;
850                         goto out;
851                 }
852                 /* Initialize temporary state matching only
853                  * to current session. */
854                 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
855                 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
856
857                 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
858                 if (error) {
859                         x->km.state = XFRM_STATE_DEAD;
860                         to_put = x;
861                         x = NULL;
862                         goto out;
863                 }
864
865                 if (km_query(x, tmpl, pol) == 0) {
866                         x->km.state = XFRM_STATE_ACQ;
867                         list_add(&x->km.all, &net->xfrm.state_all);
868                         hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
869                         h = xfrm_src_hash(net, daddr, saddr, encap_family);
870                         hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
871                         if (x->id.spi) {
872                                 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
873                                 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
874                         }
875                         x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
876                         tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
877                         net->xfrm.state_num++;
878                         xfrm_hash_grow_check(net, x->bydst.next != NULL);
879                 } else {
880                         x->km.state = XFRM_STATE_DEAD;
881                         to_put = x;
882                         x = NULL;
883                         error = -ESRCH;
884                 }
885         }
886 out:
887         if (x)
888                 xfrm_state_hold(x);
889         else
890                 *err = acquire_in_progress ? -EAGAIN : error;
891         spin_unlock_bh(&xfrm_state_lock);
892         if (to_put)
893                 xfrm_state_put(to_put);
894         return x;
895 }
896
897 struct xfrm_state *
898 xfrm_stateonly_find(struct net *net, u32 mark,
899                     xfrm_address_t *daddr, xfrm_address_t *saddr,
900                     unsigned short family, u8 mode, u8 proto, u32 reqid)
901 {
902         unsigned int h;
903         struct xfrm_state *rx = NULL, *x = NULL;
904         struct hlist_node *entry;
905
906         spin_lock(&xfrm_state_lock);
907         h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
908         hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
909                 if (x->props.family == family &&
910                     x->props.reqid == reqid &&
911                     (mark & x->mark.m) == x->mark.v &&
912                     !(x->props.flags & XFRM_STATE_WILDRECV) &&
913                     xfrm_state_addr_check(x, daddr, saddr, family) &&
914                     mode == x->props.mode &&
915                     proto == x->id.proto &&
916                     x->km.state == XFRM_STATE_VALID) {
917                         rx = x;
918                         break;
919                 }
920         }
921
922         if (rx)
923                 xfrm_state_hold(rx);
924         spin_unlock(&xfrm_state_lock);
925
926
927         return rx;
928 }
929 EXPORT_SYMBOL(xfrm_stateonly_find);
930
931 static void __xfrm_state_insert(struct xfrm_state *x)
932 {
933         struct net *net = xs_net(x);
934         unsigned int h;
935
936         list_add(&x->km.all, &net->xfrm.state_all);
937
938         h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
939                           x->props.reqid, x->props.family);
940         hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
941
942         h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
943         hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
944
945         if (x->id.spi) {
946                 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
947                                   x->props.family);
948
949                 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
950         }
951
952         tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
953         if (x->replay_maxage)
954                 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
955
956         wake_up(&net->xfrm.km_waitq);
957
958         net->xfrm.state_num++;
959
960         xfrm_hash_grow_check(net, x->bydst.next != NULL);
961 }
962
963 /* xfrm_state_lock is held */
964 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
965 {
966         struct net *net = xs_net(xnew);
967         unsigned short family = xnew->props.family;
968         u32 reqid = xnew->props.reqid;
969         struct xfrm_state *x;
970         struct hlist_node *entry;
971         unsigned int h;
972         u32 mark = xnew->mark.v & xnew->mark.m;
973
974         h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
975         hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
976                 if (x->props.family     == family &&
977                     x->props.reqid      == reqid &&
978                     (mark & x->mark.m) == x->mark.v &&
979                     !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
980                     !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
981                         x->genid++;
982         }
983 }
984
985 void xfrm_state_insert(struct xfrm_state *x)
986 {
987         spin_lock_bh(&xfrm_state_lock);
988         __xfrm_state_bump_genids(x);
989         __xfrm_state_insert(x);
990         spin_unlock_bh(&xfrm_state_lock);
991 }
992 EXPORT_SYMBOL(xfrm_state_insert);
993
994 /* xfrm_state_lock is held */
995 static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m,
996                                           unsigned short family, u8 mode,
997                                           u32 reqid, u8 proto,
998                                           const xfrm_address_t *daddr,
999                                           const xfrm_address_t *saddr, int create)
1000 {
1001         unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1002         struct hlist_node *entry;
1003         struct xfrm_state *x;
1004         u32 mark = m->v & m->m;
1005
1006         hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
1007                 if (x->props.reqid  != reqid ||
1008                     x->props.mode   != mode ||
1009                     x->props.family != family ||
1010                     x->km.state     != XFRM_STATE_ACQ ||
1011                     x->id.spi       != 0 ||
1012                     x->id.proto     != proto ||
1013                     (mark & x->mark.m) != x->mark.v ||
1014                     xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
1015                     xfrm_addr_cmp(&x->props.saddr, saddr, family))
1016                         continue;
1017
1018                 xfrm_state_hold(x);
1019                 return x;
1020         }
1021
1022         if (!create)
1023                 return NULL;
1024
1025         x = xfrm_state_alloc(net);
1026         if (likely(x)) {
1027                 switch (family) {
1028                 case AF_INET:
1029                         x->sel.daddr.a4 = daddr->a4;
1030                         x->sel.saddr.a4 = saddr->a4;
1031                         x->sel.prefixlen_d = 32;
1032                         x->sel.prefixlen_s = 32;
1033                         x->props.saddr.a4 = saddr->a4;
1034                         x->id.daddr.a4 = daddr->a4;
1035                         break;
1036
1037                 case AF_INET6:
1038                         *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
1039                         *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
1040                         x->sel.prefixlen_d = 128;
1041                         x->sel.prefixlen_s = 128;
1042                         *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
1043                         *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
1044                         break;
1045                 }
1046
1047                 x->km.state = XFRM_STATE_ACQ;
1048                 x->id.proto = proto;
1049                 x->props.family = family;
1050                 x->props.mode = mode;
1051                 x->props.reqid = reqid;
1052                 x->mark.v = m->v;
1053                 x->mark.m = m->m;
1054                 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1055                 xfrm_state_hold(x);
1056                 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
1057                 list_add(&x->km.all, &net->xfrm.state_all);
1058                 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1059                 h = xfrm_src_hash(net, daddr, saddr, family);
1060                 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
1061
1062                 net->xfrm.state_num++;
1063
1064                 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1065         }
1066
1067         return x;
1068 }
1069
1070 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1071
1072 int xfrm_state_add(struct xfrm_state *x)
1073 {
1074         struct net *net = xs_net(x);
1075         struct xfrm_state *x1, *to_put;
1076         int family;
1077         int err;
1078         u32 mark = x->mark.v & x->mark.m;
1079         int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1080
1081         family = x->props.family;
1082
1083         to_put = NULL;
1084
1085         spin_lock_bh(&xfrm_state_lock);
1086
1087         x1 = __xfrm_state_locate(x, use_spi, family);
1088         if (x1) {
1089                 to_put = x1;
1090                 x1 = NULL;
1091                 err = -EEXIST;
1092                 goto out;
1093         }
1094
1095         if (use_spi && x->km.seq) {
1096                 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1097                 if (x1 && ((x1->id.proto != x->id.proto) ||
1098                     xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
1099                         to_put = x1;
1100                         x1 = NULL;
1101                 }
1102         }
1103
1104         if (use_spi && !x1)
1105                 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1106                                      x->props.reqid, x->id.proto,
1107                                      &x->id.daddr, &x->props.saddr, 0);
1108
1109         __xfrm_state_bump_genids(x);
1110         __xfrm_state_insert(x);
1111         err = 0;
1112
1113 out:
1114         spin_unlock_bh(&xfrm_state_lock);
1115
1116         if (x1) {
1117                 xfrm_state_delete(x1);
1118                 xfrm_state_put(x1);
1119         }
1120
1121         if (to_put)
1122                 xfrm_state_put(to_put);
1123
1124         return err;
1125 }
1126 EXPORT_SYMBOL(xfrm_state_add);
1127
1128 #ifdef CONFIG_XFRM_MIGRATE
1129 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
1130 {
1131         struct net *net = xs_net(orig);
1132         int err = -ENOMEM;
1133         struct xfrm_state *x = xfrm_state_alloc(net);
1134         if (!x)
1135                 goto out;
1136
1137         memcpy(&x->id, &orig->id, sizeof(x->id));
1138         memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1139         memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1140         x->props.mode = orig->props.mode;
1141         x->props.replay_window = orig->props.replay_window;
1142         x->props.reqid = orig->props.reqid;
1143         x->props.family = orig->props.family;
1144         x->props.saddr = orig->props.saddr;
1145
1146         if (orig->aalg) {
1147                 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1148                 if (!x->aalg)
1149                         goto error;
1150         }
1151         x->props.aalgo = orig->props.aalgo;
1152
1153         if (orig->ealg) {
1154                 x->ealg = xfrm_algo_clone(orig->ealg);
1155                 if (!x->ealg)
1156                         goto error;
1157         }
1158         x->props.ealgo = orig->props.ealgo;
1159
1160         if (orig->calg) {
1161                 x->calg = xfrm_algo_clone(orig->calg);
1162                 if (!x->calg)
1163                         goto error;
1164         }
1165         x->props.calgo = orig->props.calgo;
1166
1167         if (orig->encap) {
1168                 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1169                 if (!x->encap)
1170                         goto error;
1171         }
1172
1173         if (orig->coaddr) {
1174                 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1175                                     GFP_KERNEL);
1176                 if (!x->coaddr)
1177                         goto error;
1178         }
1179
1180         if (orig->replay_esn) {
1181                 err = xfrm_replay_clone(x, orig);
1182                 if (err)
1183                         goto error;
1184         }
1185
1186         memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1187
1188         err = xfrm_init_state(x);
1189         if (err)
1190                 goto error;
1191
1192         x->props.flags = orig->props.flags;
1193
1194         x->curlft.add_time = orig->curlft.add_time;
1195         x->km.state = orig->km.state;
1196         x->km.seq = orig->km.seq;
1197
1198         return x;
1199
1200  error:
1201         xfrm_state_put(x);
1202 out:
1203         if (errp)
1204                 *errp = err;
1205         return NULL;
1206 }
1207
1208 /* xfrm_state_lock is held */
1209 struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1210 {
1211         unsigned int h;
1212         struct xfrm_state *x;
1213         struct hlist_node *entry;
1214
1215         if (m->reqid) {
1216                 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
1217                                   m->reqid, m->old_family);
1218                 hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
1219                         if (x->props.mode != m->mode ||
1220                             x->id.proto != m->proto)
1221                                 continue;
1222                         if (m->reqid && x->props.reqid != m->reqid)
1223                                 continue;
1224                         if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1225                                           m->old_family) ||
1226                             xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1227                                           m->old_family))
1228                                 continue;
1229                         xfrm_state_hold(x);
1230                         return x;
1231                 }
1232         } else {
1233                 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
1234                                   m->old_family);
1235                 hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
1236                         if (x->props.mode != m->mode ||
1237                             x->id.proto != m->proto)
1238                                 continue;
1239                         if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1240                                           m->old_family) ||
1241                             xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1242                                           m->old_family))
1243                                 continue;
1244                         xfrm_state_hold(x);
1245                         return x;
1246                 }
1247         }
1248
1249         return NULL;
1250 }
1251 EXPORT_SYMBOL(xfrm_migrate_state_find);
1252
1253 struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1254                                        struct xfrm_migrate *m)
1255 {
1256         struct xfrm_state *xc;
1257         int err;
1258
1259         xc = xfrm_state_clone(x, &err);
1260         if (!xc)
1261                 return NULL;
1262
1263         memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1264         memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1265
1266         /* add state */
1267         if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1268                 /* a care is needed when the destination address of the
1269                    state is to be updated as it is a part of triplet */
1270                 xfrm_state_insert(xc);
1271         } else {
1272                 if ((err = xfrm_state_add(xc)) < 0)
1273                         goto error;
1274         }
1275
1276         return xc;
1277 error:
1278         xfrm_state_put(xc);
1279         return NULL;
1280 }
1281 EXPORT_SYMBOL(xfrm_state_migrate);
1282 #endif
1283
1284 int xfrm_state_update(struct xfrm_state *x)
1285 {
1286         struct xfrm_state *x1, *to_put;
1287         int err;
1288         int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1289
1290         to_put = NULL;
1291
1292         spin_lock_bh(&xfrm_state_lock);
1293         x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1294
1295         err = -ESRCH;
1296         if (!x1)
1297                 goto out;
1298
1299         if (xfrm_state_kern(x1)) {
1300                 to_put = x1;
1301                 err = -EEXIST;
1302                 goto out;
1303         }
1304
1305         if (x1->km.state == XFRM_STATE_ACQ) {
1306                 __xfrm_state_insert(x);
1307                 x = NULL;
1308         }
1309         err = 0;
1310
1311 out:
1312         spin_unlock_bh(&xfrm_state_lock);
1313
1314         if (to_put)
1315                 xfrm_state_put(to_put);
1316
1317         if (err)
1318                 return err;
1319
1320         if (!x) {
1321                 xfrm_state_delete(x1);
1322                 xfrm_state_put(x1);
1323                 return 0;
1324         }
1325
1326         err = -EINVAL;
1327         spin_lock_bh(&x1->lock);
1328         if (likely(x1->km.state == XFRM_STATE_VALID)) {
1329                 if (x->encap && x1->encap)
1330                         memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1331                 if (x->coaddr && x1->coaddr) {
1332                         memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1333                 }
1334                 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1335                         memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1336                 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1337                 x1->km.dying = 0;
1338
1339                 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
1340                 if (x1->curlft.use_time)
1341                         xfrm_state_check_expire(x1);
1342
1343                 err = 0;
1344                 x->km.state = XFRM_STATE_DEAD;
1345                 __xfrm_state_put(x);
1346         }
1347         spin_unlock_bh(&x1->lock);
1348
1349         xfrm_state_put(x1);
1350
1351         return err;
1352 }
1353 EXPORT_SYMBOL(xfrm_state_update);
1354
1355 int xfrm_state_check_expire(struct xfrm_state *x)
1356 {
1357         if (!x->curlft.use_time)
1358                 x->curlft.use_time = get_seconds();
1359
1360         if (x->km.state != XFRM_STATE_VALID)
1361                 return -EINVAL;
1362
1363         if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1364             x->curlft.packets >= x->lft.hard_packet_limit) {
1365                 x->km.state = XFRM_STATE_EXPIRED;
1366                 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
1367                 return -EINVAL;
1368         }
1369
1370         if (!x->km.dying &&
1371             (x->curlft.bytes >= x->lft.soft_byte_limit ||
1372              x->curlft.packets >= x->lft.soft_packet_limit)) {
1373                 x->km.dying = 1;
1374                 km_state_expired(x, 0, 0);
1375         }
1376         return 0;
1377 }
1378 EXPORT_SYMBOL(xfrm_state_check_expire);
1379
1380 struct xfrm_state *
1381 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1382                   u8 proto, unsigned short family)
1383 {
1384         struct xfrm_state *x;
1385
1386         spin_lock_bh(&xfrm_state_lock);
1387         x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1388         spin_unlock_bh(&xfrm_state_lock);
1389         return x;
1390 }
1391 EXPORT_SYMBOL(xfrm_state_lookup);
1392
1393 struct xfrm_state *
1394 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1395                          const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1396                          u8 proto, unsigned short family)
1397 {
1398         struct xfrm_state *x;
1399
1400         spin_lock_bh(&xfrm_state_lock);
1401         x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1402         spin_unlock_bh(&xfrm_state_lock);
1403         return x;
1404 }
1405 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1406
1407 struct xfrm_state *
1408 xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto,
1409               const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1410               int create, unsigned short family)
1411 {
1412         struct xfrm_state *x;
1413
1414         spin_lock_bh(&xfrm_state_lock);
1415         x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
1416         spin_unlock_bh(&xfrm_state_lock);
1417
1418         return x;
1419 }
1420 EXPORT_SYMBOL(xfrm_find_acq);
1421
1422 #ifdef CONFIG_XFRM_SUB_POLICY
1423 int
1424 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1425                unsigned short family)
1426 {
1427         int err = 0;
1428         struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1429         if (!afinfo)
1430                 return -EAFNOSUPPORT;
1431
1432         spin_lock_bh(&xfrm_state_lock);
1433         if (afinfo->tmpl_sort)
1434                 err = afinfo->tmpl_sort(dst, src, n);
1435         spin_unlock_bh(&xfrm_state_lock);
1436         xfrm_state_put_afinfo(afinfo);
1437         return err;
1438 }
1439 EXPORT_SYMBOL(xfrm_tmpl_sort);
1440
1441 int
1442 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1443                 unsigned short family)
1444 {
1445         int err = 0;
1446         struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1447         if (!afinfo)
1448                 return -EAFNOSUPPORT;
1449
1450         spin_lock_bh(&xfrm_state_lock);
1451         if (afinfo->state_sort)
1452                 err = afinfo->state_sort(dst, src, n);
1453         spin_unlock_bh(&xfrm_state_lock);
1454         xfrm_state_put_afinfo(afinfo);
1455         return err;
1456 }
1457 EXPORT_SYMBOL(xfrm_state_sort);
1458 #endif
1459
1460 /* Silly enough, but I'm lazy to build resolution list */
1461
1462 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1463 {
1464         int i;
1465
1466         for (i = 0; i <= net->xfrm.state_hmask; i++) {
1467                 struct hlist_node *entry;
1468                 struct xfrm_state *x;
1469
1470                 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
1471                         if (x->km.seq == seq &&
1472                             (mark & x->mark.m) == x->mark.v &&
1473                             x->km.state == XFRM_STATE_ACQ) {
1474                                 xfrm_state_hold(x);
1475                                 return x;
1476                         }
1477                 }
1478         }
1479         return NULL;
1480 }
1481
1482 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1483 {
1484         struct xfrm_state *x;
1485
1486         spin_lock_bh(&xfrm_state_lock);
1487         x = __xfrm_find_acq_byseq(net, mark, seq);
1488         spin_unlock_bh(&xfrm_state_lock);
1489         return x;
1490 }
1491 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1492
1493 u32 xfrm_get_acqseq(void)
1494 {
1495         u32 res;
1496         static atomic_t acqseq;
1497
1498         do {
1499                 res = atomic_inc_return(&acqseq);
1500         } while (!res);
1501
1502         return res;
1503 }
1504 EXPORT_SYMBOL(xfrm_get_acqseq);
1505
1506 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1507 {
1508         struct net *net = xs_net(x);
1509         unsigned int h;
1510         struct xfrm_state *x0;
1511         int err = -ENOENT;
1512         __be32 minspi = htonl(low);
1513         __be32 maxspi = htonl(high);
1514         u32 mark = x->mark.v & x->mark.m;
1515
1516         spin_lock_bh(&x->lock);
1517         if (x->km.state == XFRM_STATE_DEAD)
1518                 goto unlock;
1519
1520         err = 0;
1521         if (x->id.spi)
1522                 goto unlock;
1523
1524         err = -ENOENT;
1525
1526         if (minspi == maxspi) {
1527                 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
1528                 if (x0) {
1529                         xfrm_state_put(x0);
1530                         goto unlock;
1531                 }
1532                 x->id.spi = minspi;
1533         } else {
1534                 u32 spi = 0;
1535                 for (h=0; h<high-low+1; h++) {
1536                         spi = low + net_random()%(high-low+1);
1537                         x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1538                         if (x0 == NULL) {
1539                                 x->id.spi = htonl(spi);
1540                                 break;
1541                         }
1542                         xfrm_state_put(x0);
1543                 }
1544         }
1545         if (x->id.spi) {
1546                 spin_lock_bh(&xfrm_state_lock);
1547                 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1548                 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
1549                 spin_unlock_bh(&xfrm_state_lock);
1550
1551                 err = 0;
1552         }
1553
1554 unlock:
1555         spin_unlock_bh(&x->lock);
1556
1557         return err;
1558 }
1559 EXPORT_SYMBOL(xfrm_alloc_spi);
1560
1561 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1562                     int (*func)(struct xfrm_state *, int, void*),
1563                     void *data)
1564 {
1565         struct xfrm_state *state;
1566         struct xfrm_state_walk *x;
1567         int err = 0;
1568
1569         if (walk->seq != 0 && list_empty(&walk->all))
1570                 return 0;
1571
1572         spin_lock_bh(&xfrm_state_lock);
1573         if (list_empty(&walk->all))
1574                 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
1575         else
1576                 x = list_entry(&walk->all, struct xfrm_state_walk, all);
1577         list_for_each_entry_from(x, &net->xfrm.state_all, all) {
1578                 if (x->state == XFRM_STATE_DEAD)
1579                         continue;
1580                 state = container_of(x, struct xfrm_state, km);
1581                 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
1582                         continue;
1583                 err = func(state, walk->seq, data);
1584                 if (err) {
1585                         list_move_tail(&walk->all, &x->all);
1586                         goto out;
1587                 }
1588                 walk->seq++;
1589         }
1590         if (walk->seq == 0) {
1591                 err = -ENOENT;
1592                 goto out;
1593         }
1594         list_del_init(&walk->all);
1595 out:
1596         spin_unlock_bh(&xfrm_state_lock);
1597         return err;
1598 }
1599 EXPORT_SYMBOL(xfrm_state_walk);
1600
1601 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1602 {
1603         INIT_LIST_HEAD(&walk->all);
1604         walk->proto = proto;
1605         walk->state = XFRM_STATE_DEAD;
1606         walk->seq = 0;
1607 }
1608 EXPORT_SYMBOL(xfrm_state_walk_init);
1609
1610 void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1611 {
1612         if (list_empty(&walk->all))
1613                 return;
1614
1615         spin_lock_bh(&xfrm_state_lock);
1616         list_del(&walk->all);
1617         spin_unlock_bh(&xfrm_state_lock);
1618 }
1619 EXPORT_SYMBOL(xfrm_state_walk_done);
1620
1621 static void xfrm_replay_timer_handler(unsigned long data)
1622 {
1623         struct xfrm_state *x = (struct xfrm_state*)data;
1624
1625         spin_lock(&x->lock);
1626
1627         if (x->km.state == XFRM_STATE_VALID) {
1628                 if (xfrm_aevent_is_on(xs_net(x)))
1629                         x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
1630                 else
1631                         x->xflags |= XFRM_TIME_DEFER;
1632         }
1633
1634         spin_unlock(&x->lock);
1635 }
1636
1637 static LIST_HEAD(xfrm_km_list);
1638 static DEFINE_RWLOCK(xfrm_km_lock);
1639
1640 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
1641 {
1642         struct xfrm_mgr *km;
1643
1644         read_lock(&xfrm_km_lock);
1645         list_for_each_entry(km, &xfrm_km_list, list)
1646                 if (km->notify_policy)
1647                         km->notify_policy(xp, dir, c);
1648         read_unlock(&xfrm_km_lock);
1649 }
1650
1651 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
1652 {
1653         struct xfrm_mgr *km;
1654         read_lock(&xfrm_km_lock);
1655         list_for_each_entry(km, &xfrm_km_list, list)
1656                 if (km->notify)
1657                         km->notify(x, c);
1658         read_unlock(&xfrm_km_lock);
1659 }
1660
1661 EXPORT_SYMBOL(km_policy_notify);
1662 EXPORT_SYMBOL(km_state_notify);
1663
1664 void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
1665 {
1666         struct net *net = xs_net(x);
1667         struct km_event c;
1668
1669         c.data.hard = hard;
1670         c.pid = pid;
1671         c.event = XFRM_MSG_EXPIRE;
1672         km_state_notify(x, &c);
1673
1674         if (hard)
1675                 wake_up(&net->xfrm.km_waitq);
1676 }
1677
1678 EXPORT_SYMBOL(km_state_expired);
1679 /*
1680  * We send to all registered managers regardless of failure
1681  * We are happy with one success
1682 */
1683 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1684 {
1685         int err = -EINVAL, acqret;
1686         struct xfrm_mgr *km;
1687
1688         read_lock(&xfrm_km_lock);
1689         list_for_each_entry(km, &xfrm_km_list, list) {
1690                 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1691                 if (!acqret)
1692                         err = acqret;
1693         }
1694         read_unlock(&xfrm_km_lock);
1695         return err;
1696 }
1697 EXPORT_SYMBOL(km_query);
1698
1699 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1700 {
1701         int err = -EINVAL;
1702         struct xfrm_mgr *km;
1703
1704         read_lock(&xfrm_km_lock);
1705         list_for_each_entry(km, &xfrm_km_list, list) {
1706                 if (km->new_mapping)
1707                         err = km->new_mapping(x, ipaddr, sport);
1708                 if (!err)
1709                         break;
1710         }
1711         read_unlock(&xfrm_km_lock);
1712         return err;
1713 }
1714 EXPORT_SYMBOL(km_new_mapping);
1715
1716 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
1717 {
1718         struct net *net = xp_net(pol);
1719         struct km_event c;
1720
1721         c.data.hard = hard;
1722         c.pid = pid;
1723         c.event = XFRM_MSG_POLEXPIRE;
1724         km_policy_notify(pol, dir, &c);
1725
1726         if (hard)
1727                 wake_up(&net->xfrm.km_waitq);
1728 }
1729 EXPORT_SYMBOL(km_policy_expired);
1730
1731 #ifdef CONFIG_XFRM_MIGRATE
1732 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1733                const struct xfrm_migrate *m, int num_migrate,
1734                const struct xfrm_kmaddress *k)
1735 {
1736         int err = -EINVAL;
1737         int ret;
1738         struct xfrm_mgr *km;
1739
1740         read_lock(&xfrm_km_lock);
1741         list_for_each_entry(km, &xfrm_km_list, list) {
1742                 if (km->migrate) {
1743                         ret = km->migrate(sel, dir, type, m, num_migrate, k);
1744                         if (!ret)
1745                                 err = ret;
1746                 }
1747         }
1748         read_unlock(&xfrm_km_lock);
1749         return err;
1750 }
1751 EXPORT_SYMBOL(km_migrate);
1752 #endif
1753
1754 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1755 {
1756         int err = -EINVAL;
1757         int ret;
1758         struct xfrm_mgr *km;
1759
1760         read_lock(&xfrm_km_lock);
1761         list_for_each_entry(km, &xfrm_km_list, list) {
1762                 if (km->report) {
1763                         ret = km->report(net, proto, sel, addr);
1764                         if (!ret)
1765                                 err = ret;
1766                 }
1767         }
1768         read_unlock(&xfrm_km_lock);
1769         return err;
1770 }
1771 EXPORT_SYMBOL(km_report);
1772
1773 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1774 {
1775         int err;
1776         u8 *data;
1777         struct xfrm_mgr *km;
1778         struct xfrm_policy *pol = NULL;
1779
1780         if (optlen <= 0 || optlen > PAGE_SIZE)
1781                 return -EMSGSIZE;
1782
1783         data = kmalloc(optlen, GFP_KERNEL);
1784         if (!data)
1785                 return -ENOMEM;
1786
1787         err = -EFAULT;
1788         if (copy_from_user(data, optval, optlen))
1789                 goto out;
1790
1791         err = -EINVAL;
1792         read_lock(&xfrm_km_lock);
1793         list_for_each_entry(km, &xfrm_km_list, list) {
1794                 pol = km->compile_policy(sk, optname, data,
1795                                          optlen, &err);
1796                 if (err >= 0)
1797                         break;
1798         }
1799         read_unlock(&xfrm_km_lock);
1800
1801         if (err >= 0) {
1802                 xfrm_sk_policy_insert(sk, err, pol);
1803                 xfrm_pol_put(pol);
1804                 err = 0;
1805         }
1806
1807 out:
1808         kfree(data);
1809         return err;
1810 }
1811 EXPORT_SYMBOL(xfrm_user_policy);
1812
1813 int xfrm_register_km(struct xfrm_mgr *km)
1814 {
1815         write_lock_bh(&xfrm_km_lock);
1816         list_add_tail(&km->list, &xfrm_km_list);
1817         write_unlock_bh(&xfrm_km_lock);
1818         return 0;
1819 }
1820 EXPORT_SYMBOL(xfrm_register_km);
1821
1822 int xfrm_unregister_km(struct xfrm_mgr *km)
1823 {
1824         write_lock_bh(&xfrm_km_lock);
1825         list_del(&km->list);
1826         write_unlock_bh(&xfrm_km_lock);
1827         return 0;
1828 }
1829 EXPORT_SYMBOL(xfrm_unregister_km);
1830
1831 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1832 {
1833         int err = 0;
1834         if (unlikely(afinfo == NULL))
1835                 return -EINVAL;
1836         if (unlikely(afinfo->family >= NPROTO))
1837                 return -EAFNOSUPPORT;
1838         write_lock_bh(&xfrm_state_afinfo_lock);
1839         if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1840                 err = -ENOBUFS;
1841         else
1842                 xfrm_state_afinfo[afinfo->family] = afinfo;
1843         write_unlock_bh(&xfrm_state_afinfo_lock);
1844         return err;
1845 }
1846 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1847
1848 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1849 {
1850         int err = 0;
1851         if (unlikely(afinfo == NULL))
1852                 return -EINVAL;
1853         if (unlikely(afinfo->family >= NPROTO))
1854                 return -EAFNOSUPPORT;
1855         write_lock_bh(&xfrm_state_afinfo_lock);
1856         if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1857                 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1858                         err = -EINVAL;
1859                 else
1860                         xfrm_state_afinfo[afinfo->family] = NULL;
1861         }
1862         write_unlock_bh(&xfrm_state_afinfo_lock);
1863         return err;
1864 }
1865 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1866
1867 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1868 {
1869         struct xfrm_state_afinfo *afinfo;
1870         if (unlikely(family >= NPROTO))
1871                 return NULL;
1872         read_lock(&xfrm_state_afinfo_lock);
1873         afinfo = xfrm_state_afinfo[family];
1874         if (unlikely(!afinfo))
1875                 read_unlock(&xfrm_state_afinfo_lock);
1876         return afinfo;
1877 }
1878
1879 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1880         __releases(xfrm_state_afinfo_lock)
1881 {
1882         read_unlock(&xfrm_state_afinfo_lock);
1883 }
1884
1885 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1886 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1887 {
1888         if (x->tunnel) {
1889                 struct xfrm_state *t = x->tunnel;
1890
1891                 if (atomic_read(&t->tunnel_users) == 2)
1892                         xfrm_state_delete(t);
1893                 atomic_dec(&t->tunnel_users);
1894                 xfrm_state_put(t);
1895                 x->tunnel = NULL;
1896         }
1897 }
1898 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1899
1900 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1901 {
1902         int res;
1903
1904         spin_lock_bh(&x->lock);
1905         if (x->km.state == XFRM_STATE_VALID &&
1906             x->type && x->type->get_mtu)
1907                 res = x->type->get_mtu(x, mtu);
1908         else
1909                 res = mtu - x->props.header_len;
1910         spin_unlock_bh(&x->lock);
1911         return res;
1912 }
1913
1914 int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
1915 {
1916         struct xfrm_state_afinfo *afinfo;
1917         struct xfrm_mode *inner_mode;
1918         int family = x->props.family;
1919         int err;
1920
1921         err = -EAFNOSUPPORT;
1922         afinfo = xfrm_state_get_afinfo(family);
1923         if (!afinfo)
1924                 goto error;
1925
1926         err = 0;
1927         if (afinfo->init_flags)
1928                 err = afinfo->init_flags(x);
1929
1930         xfrm_state_put_afinfo(afinfo);
1931
1932         if (err)
1933                 goto error;
1934
1935         err = -EPROTONOSUPPORT;
1936
1937         if (x->sel.family != AF_UNSPEC) {
1938                 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1939                 if (inner_mode == NULL)
1940                         goto error;
1941
1942                 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1943                     family != x->sel.family) {
1944                         xfrm_put_mode(inner_mode);
1945                         goto error;
1946                 }
1947
1948                 x->inner_mode = inner_mode;
1949         } else {
1950                 struct xfrm_mode *inner_mode_iaf;
1951                 int iafamily = AF_INET;
1952
1953                 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
1954                 if (inner_mode == NULL)
1955                         goto error;
1956
1957                 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1958                         xfrm_put_mode(inner_mode);
1959                         goto error;
1960                 }
1961                 x->inner_mode = inner_mode;
1962
1963                 if (x->props.family == AF_INET)
1964                         iafamily = AF_INET6;
1965
1966                 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1967                 if (inner_mode_iaf) {
1968                         if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1969                                 x->inner_mode_iaf = inner_mode_iaf;
1970                         else
1971                                 xfrm_put_mode(inner_mode_iaf);
1972                 }
1973         }
1974
1975         x->type = xfrm_get_type(x->id.proto, family);
1976         if (x->type == NULL)
1977                 goto error;
1978
1979         err = x->type->init_state(x);
1980         if (err)
1981                 goto error;
1982
1983         x->outer_mode = xfrm_get_mode(x->props.mode, family);
1984         if (x->outer_mode == NULL)
1985                 goto error;
1986
1987         if (init_replay) {
1988                 err = xfrm_init_replay(x);
1989                 if (err)
1990                         goto error;
1991         }
1992
1993         x->km.state = XFRM_STATE_VALID;
1994
1995 error:
1996         return err;
1997 }
1998
1999 EXPORT_SYMBOL(__xfrm_init_state);
2000
2001 int xfrm_init_state(struct xfrm_state *x)
2002 {
2003         return __xfrm_init_state(x, true);
2004 }
2005
2006 EXPORT_SYMBOL(xfrm_init_state);
2007
2008 int __net_init xfrm_state_init(struct net *net)
2009 {
2010         unsigned int sz;
2011
2012         INIT_LIST_HEAD(&net->xfrm.state_all);
2013
2014         sz = sizeof(struct hlist_head) * 8;
2015
2016         net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2017         if (!net->xfrm.state_bydst)
2018                 goto out_bydst;
2019         net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2020         if (!net->xfrm.state_bysrc)
2021                 goto out_bysrc;
2022         net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2023         if (!net->xfrm.state_byspi)
2024                 goto out_byspi;
2025         net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2026
2027         net->xfrm.state_num = 0;
2028         INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2029         INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
2030         INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
2031         init_waitqueue_head(&net->xfrm.km_waitq);
2032         return 0;
2033
2034 out_byspi:
2035         xfrm_hash_free(net->xfrm.state_bysrc, sz);
2036 out_bysrc:
2037         xfrm_hash_free(net->xfrm.state_bydst, sz);
2038 out_bydst:
2039         return -ENOMEM;
2040 }
2041
2042 void xfrm_state_fini(struct net *net)
2043 {
2044         struct xfrm_audit audit_info;
2045         unsigned int sz;
2046
2047         flush_work(&net->xfrm.state_hash_work);
2048         audit_info.loginuid = -1;
2049         audit_info.sessionid = -1;
2050         audit_info.secid = 0;
2051         xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2052         flush_work(&net->xfrm.state_gc_work);
2053
2054         WARN_ON(!list_empty(&net->xfrm.state_all));
2055
2056         sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2057         WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2058         xfrm_hash_free(net->xfrm.state_byspi, sz);
2059         WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2060         xfrm_hash_free(net->xfrm.state_bysrc, sz);
2061         WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2062         xfrm_hash_free(net->xfrm.state_bydst, sz);
2063 }
2064
2065 #ifdef CONFIG_AUDITSYSCALL
2066 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2067                                      struct audit_buffer *audit_buf)
2068 {
2069         struct xfrm_sec_ctx *ctx = x->security;
2070         u32 spi = ntohl(x->id.spi);
2071
2072         if (ctx)
2073                 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2074                                  ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2075
2076         switch(x->props.family) {
2077         case AF_INET:
2078                 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2079                                  &x->props.saddr.a4, &x->id.daddr.a4);
2080                 break;
2081         case AF_INET6:
2082                 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2083                                  x->props.saddr.a6, x->id.daddr.a6);
2084                 break;
2085         }
2086
2087         audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2088 }
2089
2090 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2091                                       struct audit_buffer *audit_buf)
2092 {
2093         const struct iphdr *iph4;
2094         const struct ipv6hdr *iph6;
2095
2096         switch (family) {
2097         case AF_INET:
2098                 iph4 = ip_hdr(skb);
2099                 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2100                                  &iph4->saddr, &iph4->daddr);
2101                 break;
2102         case AF_INET6:
2103                 iph6 = ipv6_hdr(skb);
2104                 audit_log_format(audit_buf,
2105                                  " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2106                                  &iph6->saddr,&iph6->daddr,
2107                                  iph6->flow_lbl[0] & 0x0f,
2108                                  iph6->flow_lbl[1],
2109                                  iph6->flow_lbl[2]);
2110                 break;
2111         }
2112 }
2113
2114 void xfrm_audit_state_add(struct xfrm_state *x, int result,
2115                           uid_t auid, u32 sessionid, u32 secid)
2116 {
2117         struct audit_buffer *audit_buf;
2118
2119         audit_buf = xfrm_audit_start("SAD-add");
2120         if (audit_buf == NULL)
2121                 return;
2122         xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2123         xfrm_audit_helper_sainfo(x, audit_buf);
2124         audit_log_format(audit_buf, " res=%u", result);
2125         audit_log_end(audit_buf);
2126 }
2127 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2128
2129 void xfrm_audit_state_delete(struct xfrm_state *x, int result,
2130                              uid_t auid, u32 sessionid, u32 secid)
2131 {
2132         struct audit_buffer *audit_buf;
2133
2134         audit_buf = xfrm_audit_start("SAD-delete");
2135         if (audit_buf == NULL)
2136                 return;
2137         xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2138         xfrm_audit_helper_sainfo(x, audit_buf);
2139         audit_log_format(audit_buf, " res=%u", result);
2140         audit_log_end(audit_buf);
2141 }
2142 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2143
2144 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2145                                       struct sk_buff *skb)
2146 {
2147         struct audit_buffer *audit_buf;
2148         u32 spi;
2149
2150         audit_buf = xfrm_audit_start("SA-replay-overflow");
2151         if (audit_buf == NULL)
2152                 return;
2153         xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2154         /* don't record the sequence number because it's inherent in this kind
2155          * of audit message */
2156         spi = ntohl(x->id.spi);
2157         audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2158         audit_log_end(audit_buf);
2159 }
2160 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2161
2162 void xfrm_audit_state_replay(struct xfrm_state *x,
2163                              struct sk_buff *skb, __be32 net_seq)
2164 {
2165         struct audit_buffer *audit_buf;
2166         u32 spi;
2167
2168         audit_buf = xfrm_audit_start("SA-replayed-pkt");
2169         if (audit_buf == NULL)
2170                 return;
2171         xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2172         spi = ntohl(x->id.spi);
2173         audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2174                          spi, spi, ntohl(net_seq));
2175         audit_log_end(audit_buf);
2176 }
2177 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2178
2179 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2180 {
2181         struct audit_buffer *audit_buf;
2182
2183         audit_buf = xfrm_audit_start("SA-notfound");
2184         if (audit_buf == NULL)
2185                 return;
2186         xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2187         audit_log_end(audit_buf);
2188 }
2189 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2190
2191 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2192                                __be32 net_spi, __be32 net_seq)
2193 {
2194         struct audit_buffer *audit_buf;
2195         u32 spi;
2196
2197         audit_buf = xfrm_audit_start("SA-notfound");
2198         if (audit_buf == NULL)
2199                 return;
2200         xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2201         spi = ntohl(net_spi);
2202         audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2203                          spi, spi, ntohl(net_seq));
2204         audit_log_end(audit_buf);
2205 }
2206 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2207
2208 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2209                               struct sk_buff *skb, u8 proto)
2210 {
2211         struct audit_buffer *audit_buf;
2212         __be32 net_spi;
2213         __be32 net_seq;
2214
2215         audit_buf = xfrm_audit_start("SA-icv-failure");
2216         if (audit_buf == NULL)
2217                 return;
2218         xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2219         if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2220                 u32 spi = ntohl(net_spi);
2221                 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2222                                  spi, spi, ntohl(net_seq));
2223         }
2224         audit_log_end(audit_buf);
2225 }
2226 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2227 #endif /* CONFIG_AUDITSYSCALL */