[IA64] ioc4_serial: fix printk format warnings
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / xfrm / xfrm_state.c
blobf2f7c638083e321b8500e449f9a84c19bf903d97
1 /*
2 * xfrm_state.c
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
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>
25 #include "xfrm_hash.h"
27 /* Each xfrm_state may be linked to two tables:
29 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
30 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
31 destination/tunnel endpoint. (output)
34 static DEFINE_SPINLOCK(xfrm_state_lock);
36 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
37 static unsigned int xfrm_state_genid;
39 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
40 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
42 #ifdef CONFIG_AUDITSYSCALL
43 static void xfrm_audit_state_replay(struct xfrm_state *x,
44 struct sk_buff *skb, __be32 net_seq);
45 #else
46 #define xfrm_audit_state_replay(x, s, sq) do { ; } while (0)
47 #endif /* CONFIG_AUDITSYSCALL */
49 static inline unsigned int xfrm_dst_hash(struct net *net,
50 xfrm_address_t *daddr,
51 xfrm_address_t *saddr,
52 u32 reqid,
53 unsigned short family)
55 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
58 static inline unsigned int xfrm_src_hash(struct net *net,
59 xfrm_address_t *daddr,
60 xfrm_address_t *saddr,
61 unsigned short family)
63 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
66 static inline unsigned int
67 xfrm_spi_hash(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
69 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
72 static void xfrm_hash_transfer(struct hlist_head *list,
73 struct hlist_head *ndsttable,
74 struct hlist_head *nsrctable,
75 struct hlist_head *nspitable,
76 unsigned int nhashmask)
78 struct hlist_node *entry, *tmp;
79 struct xfrm_state *x;
81 hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
82 unsigned int h;
84 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
85 x->props.reqid, x->props.family,
86 nhashmask);
87 hlist_add_head(&x->bydst, ndsttable+h);
89 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
90 x->props.family,
91 nhashmask);
92 hlist_add_head(&x->bysrc, nsrctable+h);
94 if (x->id.spi) {
95 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
96 x->id.proto, x->props.family,
97 nhashmask);
98 hlist_add_head(&x->byspi, nspitable+h);
103 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
105 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
108 static DEFINE_MUTEX(hash_resize_mutex);
110 static void xfrm_hash_resize(struct work_struct *work)
112 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
113 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
114 unsigned long nsize, osize;
115 unsigned int nhashmask, ohashmask;
116 int i;
118 mutex_lock(&hash_resize_mutex);
120 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
121 ndst = xfrm_hash_alloc(nsize);
122 if (!ndst)
123 goto out_unlock;
124 nsrc = xfrm_hash_alloc(nsize);
125 if (!nsrc) {
126 xfrm_hash_free(ndst, nsize);
127 goto out_unlock;
129 nspi = xfrm_hash_alloc(nsize);
130 if (!nspi) {
131 xfrm_hash_free(ndst, nsize);
132 xfrm_hash_free(nsrc, nsize);
133 goto out_unlock;
136 spin_lock_bh(&xfrm_state_lock);
138 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
139 for (i = net->xfrm.state_hmask; i >= 0; i--)
140 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
141 nhashmask);
143 odst = net->xfrm.state_bydst;
144 osrc = net->xfrm.state_bysrc;
145 ospi = net->xfrm.state_byspi;
146 ohashmask = net->xfrm.state_hmask;
148 net->xfrm.state_bydst = ndst;
149 net->xfrm.state_bysrc = nsrc;
150 net->xfrm.state_byspi = nspi;
151 net->xfrm.state_hmask = nhashmask;
153 spin_unlock_bh(&xfrm_state_lock);
155 osize = (ohashmask + 1) * sizeof(struct hlist_head);
156 xfrm_hash_free(odst, osize);
157 xfrm_hash_free(osrc, osize);
158 xfrm_hash_free(ospi, osize);
160 out_unlock:
161 mutex_unlock(&hash_resize_mutex);
164 static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
165 static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
167 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
169 int __xfrm_state_delete(struct xfrm_state *x);
171 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
172 void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
174 static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
176 struct xfrm_state_afinfo *afinfo;
177 if (unlikely(family >= NPROTO))
178 return NULL;
179 write_lock_bh(&xfrm_state_afinfo_lock);
180 afinfo = xfrm_state_afinfo[family];
181 if (unlikely(!afinfo))
182 write_unlock_bh(&xfrm_state_afinfo_lock);
183 return afinfo;
186 static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
187 __releases(xfrm_state_afinfo_lock)
189 write_unlock_bh(&xfrm_state_afinfo_lock);
192 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
194 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
195 const struct xfrm_type **typemap;
196 int err = 0;
198 if (unlikely(afinfo == NULL))
199 return -EAFNOSUPPORT;
200 typemap = afinfo->type_map;
202 if (likely(typemap[type->proto] == NULL))
203 typemap[type->proto] = type;
204 else
205 err = -EEXIST;
206 xfrm_state_unlock_afinfo(afinfo);
207 return err;
209 EXPORT_SYMBOL(xfrm_register_type);
211 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
213 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
214 const struct xfrm_type **typemap;
215 int err = 0;
217 if (unlikely(afinfo == NULL))
218 return -EAFNOSUPPORT;
219 typemap = afinfo->type_map;
221 if (unlikely(typemap[type->proto] != type))
222 err = -ENOENT;
223 else
224 typemap[type->proto] = NULL;
225 xfrm_state_unlock_afinfo(afinfo);
226 return err;
228 EXPORT_SYMBOL(xfrm_unregister_type);
230 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
232 struct xfrm_state_afinfo *afinfo;
233 const struct xfrm_type **typemap;
234 const struct xfrm_type *type;
235 int modload_attempted = 0;
237 retry:
238 afinfo = xfrm_state_get_afinfo(family);
239 if (unlikely(afinfo == NULL))
240 return NULL;
241 typemap = afinfo->type_map;
243 type = typemap[proto];
244 if (unlikely(type && !try_module_get(type->owner)))
245 type = NULL;
246 if (!type && !modload_attempted) {
247 xfrm_state_put_afinfo(afinfo);
248 request_module("xfrm-type-%d-%d", family, proto);
249 modload_attempted = 1;
250 goto retry;
253 xfrm_state_put_afinfo(afinfo);
254 return type;
257 static void xfrm_put_type(const struct xfrm_type *type)
259 module_put(type->owner);
262 int xfrm_register_mode(struct xfrm_mode *mode, int family)
264 struct xfrm_state_afinfo *afinfo;
265 struct xfrm_mode **modemap;
266 int err;
268 if (unlikely(mode->encap >= XFRM_MODE_MAX))
269 return -EINVAL;
271 afinfo = xfrm_state_lock_afinfo(family);
272 if (unlikely(afinfo == NULL))
273 return -EAFNOSUPPORT;
275 err = -EEXIST;
276 modemap = afinfo->mode_map;
277 if (modemap[mode->encap])
278 goto out;
280 err = -ENOENT;
281 if (!try_module_get(afinfo->owner))
282 goto out;
284 mode->afinfo = afinfo;
285 modemap[mode->encap] = mode;
286 err = 0;
288 out:
289 xfrm_state_unlock_afinfo(afinfo);
290 return err;
292 EXPORT_SYMBOL(xfrm_register_mode);
294 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
296 struct xfrm_state_afinfo *afinfo;
297 struct xfrm_mode **modemap;
298 int err;
300 if (unlikely(mode->encap >= XFRM_MODE_MAX))
301 return -EINVAL;
303 afinfo = xfrm_state_lock_afinfo(family);
304 if (unlikely(afinfo == NULL))
305 return -EAFNOSUPPORT;
307 err = -ENOENT;
308 modemap = afinfo->mode_map;
309 if (likely(modemap[mode->encap] == mode)) {
310 modemap[mode->encap] = NULL;
311 module_put(mode->afinfo->owner);
312 err = 0;
315 xfrm_state_unlock_afinfo(afinfo);
316 return err;
318 EXPORT_SYMBOL(xfrm_unregister_mode);
320 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
322 struct xfrm_state_afinfo *afinfo;
323 struct xfrm_mode *mode;
324 int modload_attempted = 0;
326 if (unlikely(encap >= XFRM_MODE_MAX))
327 return NULL;
329 retry:
330 afinfo = xfrm_state_get_afinfo(family);
331 if (unlikely(afinfo == NULL))
332 return NULL;
334 mode = afinfo->mode_map[encap];
335 if (unlikely(mode && !try_module_get(mode->owner)))
336 mode = NULL;
337 if (!mode && !modload_attempted) {
338 xfrm_state_put_afinfo(afinfo);
339 request_module("xfrm-mode-%d-%d", family, encap);
340 modload_attempted = 1;
341 goto retry;
344 xfrm_state_put_afinfo(afinfo);
345 return mode;
348 static void xfrm_put_mode(struct xfrm_mode *mode)
350 module_put(mode->owner);
353 static void xfrm_state_gc_destroy(struct xfrm_state *x)
355 del_timer_sync(&x->timer);
356 del_timer_sync(&x->rtimer);
357 kfree(x->aalg);
358 kfree(x->ealg);
359 kfree(x->calg);
360 kfree(x->encap);
361 kfree(x->coaddr);
362 if (x->inner_mode)
363 xfrm_put_mode(x->inner_mode);
364 if (x->inner_mode_iaf)
365 xfrm_put_mode(x->inner_mode_iaf);
366 if (x->outer_mode)
367 xfrm_put_mode(x->outer_mode);
368 if (x->type) {
369 x->type->destructor(x);
370 xfrm_put_type(x->type);
372 security_xfrm_state_free(x);
373 kfree(x);
376 static void xfrm_state_gc_task(struct work_struct *work)
378 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
379 struct xfrm_state *x;
380 struct hlist_node *entry, *tmp;
381 struct hlist_head gc_list;
383 spin_lock_bh(&xfrm_state_gc_lock);
384 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
385 spin_unlock_bh(&xfrm_state_gc_lock);
387 hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
388 xfrm_state_gc_destroy(x);
390 wake_up(&net->xfrm.km_waitq);
393 static inline unsigned long make_jiffies(long secs)
395 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
396 return MAX_SCHEDULE_TIMEOUT-1;
397 else
398 return secs*HZ;
401 static void xfrm_timer_handler(unsigned long data)
403 struct xfrm_state *x = (struct xfrm_state*)data;
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;
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;
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;
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;
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;
450 x->km.dying = warn;
451 if (warn)
452 km_state_expired(x, 0, 0);
453 resched:
454 if (next != LONG_MAX)
455 mod_timer(&x->timer, jiffies + make_jiffies(next));
457 goto out;
459 expired:
460 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
461 x->km.state = XFRM_STATE_EXPIRED;
462 wake_up(&net->xfrm.km_waitq);
463 next = 2;
464 goto resched;
467 err = __xfrm_state_delete(x);
468 if (!err && x->id.spi)
469 km_state_expired(x, 1, 0);
471 xfrm_audit_state_delete(x, err ? 0 : 1,
472 audit_get_loginuid(current),
473 audit_get_sessionid(current), 0);
475 out:
476 spin_unlock(&x->lock);
479 static void xfrm_replay_timer_handler(unsigned long data);
481 struct xfrm_state *xfrm_state_alloc(struct net *net)
483 struct xfrm_state *x;
485 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
487 if (x) {
488 write_pnet(&x->xs_net, net);
489 atomic_set(&x->refcnt, 1);
490 atomic_set(&x->tunnel_users, 0);
491 INIT_LIST_HEAD(&x->km.all);
492 INIT_HLIST_NODE(&x->bydst);
493 INIT_HLIST_NODE(&x->bysrc);
494 INIT_HLIST_NODE(&x->byspi);
495 setup_timer(&x->timer, xfrm_timer_handler, (unsigned long)x);
496 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
497 (unsigned long)x);
498 x->curlft.add_time = get_seconds();
499 x->lft.soft_byte_limit = XFRM_INF;
500 x->lft.soft_packet_limit = XFRM_INF;
501 x->lft.hard_byte_limit = XFRM_INF;
502 x->lft.hard_packet_limit = XFRM_INF;
503 x->replay_maxage = 0;
504 x->replay_maxdiff = 0;
505 x->inner_mode = NULL;
506 x->inner_mode_iaf = NULL;
507 spin_lock_init(&x->lock);
509 return x;
511 EXPORT_SYMBOL(xfrm_state_alloc);
513 void __xfrm_state_destroy(struct xfrm_state *x)
515 struct net *net = xs_net(x);
517 WARN_ON(x->km.state != XFRM_STATE_DEAD);
519 spin_lock_bh(&xfrm_state_gc_lock);
520 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
521 spin_unlock_bh(&xfrm_state_gc_lock);
522 schedule_work(&net->xfrm.state_gc_work);
524 EXPORT_SYMBOL(__xfrm_state_destroy);
526 int __xfrm_state_delete(struct xfrm_state *x)
528 struct net *net = xs_net(x);
529 int err = -ESRCH;
531 if (x->km.state != XFRM_STATE_DEAD) {
532 x->km.state = XFRM_STATE_DEAD;
533 spin_lock(&xfrm_state_lock);
534 list_del(&x->km.all);
535 hlist_del(&x->bydst);
536 hlist_del(&x->bysrc);
537 if (x->id.spi)
538 hlist_del(&x->byspi);
539 net->xfrm.state_num--;
540 spin_unlock(&xfrm_state_lock);
542 /* All xfrm_state objects are created by xfrm_state_alloc.
543 * The xfrm_state_alloc call gives a reference, and that
544 * is what we are dropping here.
546 xfrm_state_put(x);
547 err = 0;
550 return err;
552 EXPORT_SYMBOL(__xfrm_state_delete);
554 int xfrm_state_delete(struct xfrm_state *x)
556 int err;
558 spin_lock_bh(&x->lock);
559 err = __xfrm_state_delete(x);
560 spin_unlock_bh(&x->lock);
562 return err;
564 EXPORT_SYMBOL(xfrm_state_delete);
566 #ifdef CONFIG_SECURITY_NETWORK_XFRM
567 static inline int
568 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
570 int i, err = 0;
572 for (i = 0; i <= net->xfrm.state_hmask; i++) {
573 struct hlist_node *entry;
574 struct xfrm_state *x;
576 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
577 if (xfrm_id_proto_match(x->id.proto, proto) &&
578 (err = security_xfrm_state_delete(x)) != 0) {
579 xfrm_audit_state_delete(x, 0,
580 audit_info->loginuid,
581 audit_info->sessionid,
582 audit_info->secid);
583 return err;
588 return err;
590 #else
591 static inline int
592 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
594 return 0;
596 #endif
598 int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
600 int i, err = 0;
602 spin_lock_bh(&xfrm_state_lock);
603 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
604 if (err)
605 goto out;
607 for (i = 0; i <= net->xfrm.state_hmask; i++) {
608 struct hlist_node *entry;
609 struct xfrm_state *x;
610 restart:
611 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
612 if (!xfrm_state_kern(x) &&
613 xfrm_id_proto_match(x->id.proto, proto)) {
614 xfrm_state_hold(x);
615 spin_unlock_bh(&xfrm_state_lock);
617 err = xfrm_state_delete(x);
618 xfrm_audit_state_delete(x, err ? 0 : 1,
619 audit_info->loginuid,
620 audit_info->sessionid,
621 audit_info->secid);
622 xfrm_state_put(x);
624 spin_lock_bh(&xfrm_state_lock);
625 goto restart;
629 err = 0;
631 out:
632 spin_unlock_bh(&xfrm_state_lock);
633 wake_up(&net->xfrm.km_waitq);
634 return err;
636 EXPORT_SYMBOL(xfrm_state_flush);
638 void xfrm_sad_getinfo(struct xfrmk_sadinfo *si)
640 spin_lock_bh(&xfrm_state_lock);
641 si->sadcnt = init_net.xfrm.state_num;
642 si->sadhcnt = init_net.xfrm.state_hmask;
643 si->sadhmcnt = xfrm_state_hashmax;
644 spin_unlock_bh(&xfrm_state_lock);
646 EXPORT_SYMBOL(xfrm_sad_getinfo);
648 static int
649 xfrm_init_tempsel(struct xfrm_state *x, struct flowi *fl,
650 struct xfrm_tmpl *tmpl,
651 xfrm_address_t *daddr, xfrm_address_t *saddr,
652 unsigned short family)
654 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
655 if (!afinfo)
656 return -1;
657 afinfo->init_tempsel(x, fl, tmpl, daddr, saddr);
658 xfrm_state_put_afinfo(afinfo);
659 return 0;
662 static struct xfrm_state *__xfrm_state_lookup(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
664 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
665 struct xfrm_state *x;
666 struct hlist_node *entry;
668 hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
669 if (x->props.family != family ||
670 x->id.spi != spi ||
671 x->id.proto != proto ||
672 xfrm_addr_cmp(&x->id.daddr, daddr, family))
673 continue;
675 xfrm_state_hold(x);
676 return x;
679 return NULL;
682 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family)
684 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
685 struct xfrm_state *x;
686 struct hlist_node *entry;
688 hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
689 if (x->props.family != family ||
690 x->id.proto != proto ||
691 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
692 xfrm_addr_cmp(&x->props.saddr, saddr, family))
693 continue;
695 xfrm_state_hold(x);
696 return x;
699 return NULL;
702 static inline struct xfrm_state *
703 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
705 struct net *net = xs_net(x);
707 if (use_spi)
708 return __xfrm_state_lookup(net, &x->id.daddr, x->id.spi,
709 x->id.proto, family);
710 else
711 return __xfrm_state_lookup_byaddr(net, &x->id.daddr,
712 &x->props.saddr,
713 x->id.proto, family);
716 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
718 if (have_hash_collision &&
719 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
720 net->xfrm.state_num > net->xfrm.state_hmask)
721 schedule_work(&net->xfrm.state_hash_work);
724 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
725 struct flowi *fl, unsigned short family,
726 xfrm_address_t *daddr, xfrm_address_t *saddr,
727 struct xfrm_state **best, int *acq_in_progress,
728 int *error)
730 /* Resolution logic:
731 * 1. There is a valid state with matching selector. Done.
732 * 2. Valid state with inappropriate selector. Skip.
734 * Entering area of "sysdeps".
736 * 3. If state is not valid, selector is temporary, it selects
737 * only session which triggered previous resolution. Key
738 * manager will do something to install a state with proper
739 * selector.
741 if (x->km.state == XFRM_STATE_VALID) {
742 if ((x->sel.family &&
743 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
744 !security_xfrm_state_pol_flow_match(x, pol, fl))
745 return;
747 if (!*best ||
748 (*best)->km.dying > x->km.dying ||
749 ((*best)->km.dying == x->km.dying &&
750 (*best)->curlft.add_time < x->curlft.add_time))
751 *best = x;
752 } else if (x->km.state == XFRM_STATE_ACQ) {
753 *acq_in_progress = 1;
754 } else if (x->km.state == XFRM_STATE_ERROR ||
755 x->km.state == XFRM_STATE_EXPIRED) {
756 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
757 security_xfrm_state_pol_flow_match(x, pol, fl))
758 *error = -ESRCH;
762 struct xfrm_state *
763 xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
764 struct flowi *fl, struct xfrm_tmpl *tmpl,
765 struct xfrm_policy *pol, int *err,
766 unsigned short family)
768 static xfrm_address_t saddr_wildcard = { };
769 struct net *net = xp_net(pol);
770 unsigned int h, h_wildcard;
771 struct hlist_node *entry;
772 struct xfrm_state *x, *x0, *to_put;
773 int acquire_in_progress = 0;
774 int error = 0;
775 struct xfrm_state *best = NULL;
777 to_put = NULL;
779 spin_lock_bh(&xfrm_state_lock);
780 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, family);
781 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
782 if (x->props.family == family &&
783 x->props.reqid == tmpl->reqid &&
784 !(x->props.flags & XFRM_STATE_WILDRECV) &&
785 xfrm_state_addr_check(x, daddr, saddr, family) &&
786 tmpl->mode == x->props.mode &&
787 tmpl->id.proto == x->id.proto &&
788 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
789 xfrm_state_look_at(pol, x, fl, family, daddr, saddr,
790 &best, &acquire_in_progress, &error);
792 if (best)
793 goto found;
795 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, family);
796 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
797 if (x->props.family == family &&
798 x->props.reqid == tmpl->reqid &&
799 !(x->props.flags & XFRM_STATE_WILDRECV) &&
800 xfrm_state_addr_check(x, daddr, saddr, family) &&
801 tmpl->mode == x->props.mode &&
802 tmpl->id.proto == x->id.proto &&
803 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
804 xfrm_state_look_at(pol, x, fl, family, daddr, saddr,
805 &best, &acquire_in_progress, &error);
808 found:
809 x = best;
810 if (!x && !error && !acquire_in_progress) {
811 if (tmpl->id.spi &&
812 (x0 = __xfrm_state_lookup(net, daddr, tmpl->id.spi,
813 tmpl->id.proto, family)) != NULL) {
814 to_put = x0;
815 error = -EEXIST;
816 goto out;
818 x = xfrm_state_alloc(net);
819 if (x == NULL) {
820 error = -ENOMEM;
821 goto out;
823 /* Initialize temporary selector matching only
824 * to current session. */
825 xfrm_init_tempsel(x, fl, tmpl, daddr, saddr, family);
827 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
828 if (error) {
829 x->km.state = XFRM_STATE_DEAD;
830 to_put = x;
831 x = NULL;
832 goto out;
835 if (km_query(x, tmpl, pol) == 0) {
836 x->km.state = XFRM_STATE_ACQ;
837 list_add(&x->km.all, &net->xfrm.state_all);
838 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
839 h = xfrm_src_hash(net, daddr, saddr, family);
840 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
841 if (x->id.spi) {
842 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, family);
843 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
845 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
846 x->timer.expires = jiffies + net->xfrm.sysctl_acq_expires*HZ;
847 add_timer(&x->timer);
848 net->xfrm.state_num++;
849 xfrm_hash_grow_check(net, x->bydst.next != NULL);
850 } else {
851 x->km.state = XFRM_STATE_DEAD;
852 to_put = x;
853 x = NULL;
854 error = -ESRCH;
857 out:
858 if (x)
859 xfrm_state_hold(x);
860 else
861 *err = acquire_in_progress ? -EAGAIN : error;
862 spin_unlock_bh(&xfrm_state_lock);
863 if (to_put)
864 xfrm_state_put(to_put);
865 return x;
868 struct xfrm_state *
869 xfrm_stateonly_find(struct net *net,
870 xfrm_address_t *daddr, xfrm_address_t *saddr,
871 unsigned short family, u8 mode, u8 proto, u32 reqid)
873 unsigned int h;
874 struct xfrm_state *rx = NULL, *x = NULL;
875 struct hlist_node *entry;
877 spin_lock(&xfrm_state_lock);
878 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
879 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
880 if (x->props.family == family &&
881 x->props.reqid == reqid &&
882 !(x->props.flags & XFRM_STATE_WILDRECV) &&
883 xfrm_state_addr_check(x, daddr, saddr, family) &&
884 mode == x->props.mode &&
885 proto == x->id.proto &&
886 x->km.state == XFRM_STATE_VALID) {
887 rx = x;
888 break;
892 if (rx)
893 xfrm_state_hold(rx);
894 spin_unlock(&xfrm_state_lock);
897 return rx;
899 EXPORT_SYMBOL(xfrm_stateonly_find);
901 static void __xfrm_state_insert(struct xfrm_state *x)
903 struct net *net = xs_net(x);
904 unsigned int h;
906 x->genid = ++xfrm_state_genid;
908 list_add(&x->km.all, &net->xfrm.state_all);
910 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
911 x->props.reqid, x->props.family);
912 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
914 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
915 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
917 if (x->id.spi) {
918 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
919 x->props.family);
921 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
924 mod_timer(&x->timer, jiffies + HZ);
925 if (x->replay_maxage)
926 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
928 wake_up(&net->xfrm.km_waitq);
930 net->xfrm.state_num++;
932 xfrm_hash_grow_check(net, x->bydst.next != NULL);
935 /* xfrm_state_lock is held */
936 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
938 struct net *net = xs_net(xnew);
939 unsigned short family = xnew->props.family;
940 u32 reqid = xnew->props.reqid;
941 struct xfrm_state *x;
942 struct hlist_node *entry;
943 unsigned int h;
945 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
946 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
947 if (x->props.family == family &&
948 x->props.reqid == reqid &&
949 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
950 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
951 x->genid = xfrm_state_genid;
955 void xfrm_state_insert(struct xfrm_state *x)
957 spin_lock_bh(&xfrm_state_lock);
958 __xfrm_state_bump_genids(x);
959 __xfrm_state_insert(x);
960 spin_unlock_bh(&xfrm_state_lock);
962 EXPORT_SYMBOL(xfrm_state_insert);
964 /* xfrm_state_lock is held */
965 static struct xfrm_state *__find_acq_core(struct net *net, unsigned short family, u8 mode, u32 reqid, u8 proto, xfrm_address_t *daddr, xfrm_address_t *saddr, int create)
967 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
968 struct hlist_node *entry;
969 struct xfrm_state *x;
971 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
972 if (x->props.reqid != reqid ||
973 x->props.mode != mode ||
974 x->props.family != family ||
975 x->km.state != XFRM_STATE_ACQ ||
976 x->id.spi != 0 ||
977 x->id.proto != proto ||
978 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
979 xfrm_addr_cmp(&x->props.saddr, saddr, family))
980 continue;
982 xfrm_state_hold(x);
983 return x;
986 if (!create)
987 return NULL;
989 x = xfrm_state_alloc(net);
990 if (likely(x)) {
991 switch (family) {
992 case AF_INET:
993 x->sel.daddr.a4 = daddr->a4;
994 x->sel.saddr.a4 = saddr->a4;
995 x->sel.prefixlen_d = 32;
996 x->sel.prefixlen_s = 32;
997 x->props.saddr.a4 = saddr->a4;
998 x->id.daddr.a4 = daddr->a4;
999 break;
1001 case AF_INET6:
1002 ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
1003 (struct in6_addr *)daddr);
1004 ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
1005 (struct in6_addr *)saddr);
1006 x->sel.prefixlen_d = 128;
1007 x->sel.prefixlen_s = 128;
1008 ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
1009 (struct in6_addr *)saddr);
1010 ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
1011 (struct in6_addr *)daddr);
1012 break;
1015 x->km.state = XFRM_STATE_ACQ;
1016 x->id.proto = proto;
1017 x->props.family = family;
1018 x->props.mode = mode;
1019 x->props.reqid = reqid;
1020 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1021 xfrm_state_hold(x);
1022 x->timer.expires = jiffies + net->xfrm.sysctl_acq_expires*HZ;
1023 add_timer(&x->timer);
1024 list_add(&x->km.all, &net->xfrm.state_all);
1025 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1026 h = xfrm_src_hash(net, daddr, saddr, family);
1027 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
1029 net->xfrm.state_num++;
1031 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1034 return x;
1037 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 seq);
1039 int xfrm_state_add(struct xfrm_state *x)
1041 struct net *net = xs_net(x);
1042 struct xfrm_state *x1, *to_put;
1043 int family;
1044 int err;
1045 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1047 family = x->props.family;
1049 to_put = NULL;
1051 spin_lock_bh(&xfrm_state_lock);
1053 x1 = __xfrm_state_locate(x, use_spi, family);
1054 if (x1) {
1055 to_put = x1;
1056 x1 = NULL;
1057 err = -EEXIST;
1058 goto out;
1061 if (use_spi && x->km.seq) {
1062 x1 = __xfrm_find_acq_byseq(net, x->km.seq);
1063 if (x1 && ((x1->id.proto != x->id.proto) ||
1064 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
1065 to_put = x1;
1066 x1 = NULL;
1070 if (use_spi && !x1)
1071 x1 = __find_acq_core(net, family, x->props.mode, x->props.reqid,
1072 x->id.proto,
1073 &x->id.daddr, &x->props.saddr, 0);
1075 __xfrm_state_bump_genids(x);
1076 __xfrm_state_insert(x);
1077 err = 0;
1079 out:
1080 spin_unlock_bh(&xfrm_state_lock);
1082 if (x1) {
1083 xfrm_state_delete(x1);
1084 xfrm_state_put(x1);
1087 if (to_put)
1088 xfrm_state_put(to_put);
1090 return err;
1092 EXPORT_SYMBOL(xfrm_state_add);
1094 #ifdef CONFIG_XFRM_MIGRATE
1095 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
1097 struct net *net = xs_net(orig);
1098 int err = -ENOMEM;
1099 struct xfrm_state *x = xfrm_state_alloc(net);
1100 if (!x)
1101 goto error;
1103 memcpy(&x->id, &orig->id, sizeof(x->id));
1104 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1105 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1106 x->props.mode = orig->props.mode;
1107 x->props.replay_window = orig->props.replay_window;
1108 x->props.reqid = orig->props.reqid;
1109 x->props.family = orig->props.family;
1110 x->props.saddr = orig->props.saddr;
1112 if (orig->aalg) {
1113 x->aalg = xfrm_algo_clone(orig->aalg);
1114 if (!x->aalg)
1115 goto error;
1117 x->props.aalgo = orig->props.aalgo;
1119 if (orig->ealg) {
1120 x->ealg = xfrm_algo_clone(orig->ealg);
1121 if (!x->ealg)
1122 goto error;
1124 x->props.ealgo = orig->props.ealgo;
1126 if (orig->calg) {
1127 x->calg = xfrm_algo_clone(orig->calg);
1128 if (!x->calg)
1129 goto error;
1131 x->props.calgo = orig->props.calgo;
1133 if (orig->encap) {
1134 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1135 if (!x->encap)
1136 goto error;
1139 if (orig->coaddr) {
1140 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1141 GFP_KERNEL);
1142 if (!x->coaddr)
1143 goto error;
1146 err = xfrm_init_state(x);
1147 if (err)
1148 goto error;
1150 x->props.flags = orig->props.flags;
1152 x->curlft.add_time = orig->curlft.add_time;
1153 x->km.state = orig->km.state;
1154 x->km.seq = orig->km.seq;
1156 return x;
1158 error:
1159 if (errp)
1160 *errp = err;
1161 if (x) {
1162 kfree(x->aalg);
1163 kfree(x->ealg);
1164 kfree(x->calg);
1165 kfree(x->encap);
1166 kfree(x->coaddr);
1168 kfree(x);
1169 return NULL;
1172 /* xfrm_state_lock is held */
1173 struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1175 unsigned int h;
1176 struct xfrm_state *x;
1177 struct hlist_node *entry;
1179 if (m->reqid) {
1180 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
1181 m->reqid, m->old_family);
1182 hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
1183 if (x->props.mode != m->mode ||
1184 x->id.proto != m->proto)
1185 continue;
1186 if (m->reqid && x->props.reqid != m->reqid)
1187 continue;
1188 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1189 m->old_family) ||
1190 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1191 m->old_family))
1192 continue;
1193 xfrm_state_hold(x);
1194 return x;
1196 } else {
1197 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
1198 m->old_family);
1199 hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
1200 if (x->props.mode != m->mode ||
1201 x->id.proto != m->proto)
1202 continue;
1203 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1204 m->old_family) ||
1205 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1206 m->old_family))
1207 continue;
1208 xfrm_state_hold(x);
1209 return x;
1213 return NULL;
1215 EXPORT_SYMBOL(xfrm_migrate_state_find);
1217 struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1218 struct xfrm_migrate *m)
1220 struct xfrm_state *xc;
1221 int err;
1223 xc = xfrm_state_clone(x, &err);
1224 if (!xc)
1225 return NULL;
1227 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1228 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1230 /* add state */
1231 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1232 /* a care is needed when the destination address of the
1233 state is to be updated as it is a part of triplet */
1234 xfrm_state_insert(xc);
1235 } else {
1236 if ((err = xfrm_state_add(xc)) < 0)
1237 goto error;
1240 return xc;
1241 error:
1242 kfree(xc);
1243 return NULL;
1245 EXPORT_SYMBOL(xfrm_state_migrate);
1246 #endif
1248 int xfrm_state_update(struct xfrm_state *x)
1250 struct xfrm_state *x1, *to_put;
1251 int err;
1252 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1254 to_put = NULL;
1256 spin_lock_bh(&xfrm_state_lock);
1257 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1259 err = -ESRCH;
1260 if (!x1)
1261 goto out;
1263 if (xfrm_state_kern(x1)) {
1264 to_put = x1;
1265 err = -EEXIST;
1266 goto out;
1269 if (x1->km.state == XFRM_STATE_ACQ) {
1270 __xfrm_state_insert(x);
1271 x = NULL;
1273 err = 0;
1275 out:
1276 spin_unlock_bh(&xfrm_state_lock);
1278 if (to_put)
1279 xfrm_state_put(to_put);
1281 if (err)
1282 return err;
1284 if (!x) {
1285 xfrm_state_delete(x1);
1286 xfrm_state_put(x1);
1287 return 0;
1290 err = -EINVAL;
1291 spin_lock_bh(&x1->lock);
1292 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1293 if (x->encap && x1->encap)
1294 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1295 if (x->coaddr && x1->coaddr) {
1296 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1298 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1299 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1300 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1301 x1->km.dying = 0;
1303 mod_timer(&x1->timer, jiffies + HZ);
1304 if (x1->curlft.use_time)
1305 xfrm_state_check_expire(x1);
1307 err = 0;
1309 spin_unlock_bh(&x1->lock);
1311 xfrm_state_put(x1);
1313 return err;
1315 EXPORT_SYMBOL(xfrm_state_update);
1317 int xfrm_state_check_expire(struct xfrm_state *x)
1319 if (!x->curlft.use_time)
1320 x->curlft.use_time = get_seconds();
1322 if (x->km.state != XFRM_STATE_VALID)
1323 return -EINVAL;
1325 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1326 x->curlft.packets >= x->lft.hard_packet_limit) {
1327 x->km.state = XFRM_STATE_EXPIRED;
1328 mod_timer(&x->timer, jiffies);
1329 return -EINVAL;
1332 if (!x->km.dying &&
1333 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1334 x->curlft.packets >= x->lft.soft_packet_limit)) {
1335 x->km.dying = 1;
1336 km_state_expired(x, 0, 0);
1338 return 0;
1340 EXPORT_SYMBOL(xfrm_state_check_expire);
1342 struct xfrm_state *
1343 xfrm_state_lookup(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto,
1344 unsigned short family)
1346 struct xfrm_state *x;
1348 spin_lock_bh(&xfrm_state_lock);
1349 x = __xfrm_state_lookup(net, daddr, spi, proto, family);
1350 spin_unlock_bh(&xfrm_state_lock);
1351 return x;
1353 EXPORT_SYMBOL(xfrm_state_lookup);
1355 struct xfrm_state *
1356 xfrm_state_lookup_byaddr(struct net *net,
1357 xfrm_address_t *daddr, xfrm_address_t *saddr,
1358 u8 proto, unsigned short family)
1360 struct xfrm_state *x;
1362 spin_lock_bh(&xfrm_state_lock);
1363 x = __xfrm_state_lookup_byaddr(net, daddr, saddr, proto, family);
1364 spin_unlock_bh(&xfrm_state_lock);
1365 return x;
1367 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1369 struct xfrm_state *
1370 xfrm_find_acq(struct net *net, u8 mode, u32 reqid, u8 proto,
1371 xfrm_address_t *daddr, xfrm_address_t *saddr,
1372 int create, unsigned short family)
1374 struct xfrm_state *x;
1376 spin_lock_bh(&xfrm_state_lock);
1377 x = __find_acq_core(net, family, mode, reqid, proto, daddr, saddr, create);
1378 spin_unlock_bh(&xfrm_state_lock);
1380 return x;
1382 EXPORT_SYMBOL(xfrm_find_acq);
1384 #ifdef CONFIG_XFRM_SUB_POLICY
1386 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1387 unsigned short family)
1389 int err = 0;
1390 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1391 if (!afinfo)
1392 return -EAFNOSUPPORT;
1394 spin_lock_bh(&xfrm_state_lock);
1395 if (afinfo->tmpl_sort)
1396 err = afinfo->tmpl_sort(dst, src, n);
1397 spin_unlock_bh(&xfrm_state_lock);
1398 xfrm_state_put_afinfo(afinfo);
1399 return err;
1401 EXPORT_SYMBOL(xfrm_tmpl_sort);
1404 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1405 unsigned short family)
1407 int err = 0;
1408 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1409 if (!afinfo)
1410 return -EAFNOSUPPORT;
1412 spin_lock_bh(&xfrm_state_lock);
1413 if (afinfo->state_sort)
1414 err = afinfo->state_sort(dst, src, n);
1415 spin_unlock_bh(&xfrm_state_lock);
1416 xfrm_state_put_afinfo(afinfo);
1417 return err;
1419 EXPORT_SYMBOL(xfrm_state_sort);
1420 #endif
1422 /* Silly enough, but I'm lazy to build resolution list */
1424 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 seq)
1426 int i;
1428 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1429 struct hlist_node *entry;
1430 struct xfrm_state *x;
1432 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
1433 if (x->km.seq == seq &&
1434 x->km.state == XFRM_STATE_ACQ) {
1435 xfrm_state_hold(x);
1436 return x;
1440 return NULL;
1443 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 seq)
1445 struct xfrm_state *x;
1447 spin_lock_bh(&xfrm_state_lock);
1448 x = __xfrm_find_acq_byseq(net, seq);
1449 spin_unlock_bh(&xfrm_state_lock);
1450 return x;
1452 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1454 u32 xfrm_get_acqseq(void)
1456 u32 res;
1457 static u32 acqseq;
1458 static DEFINE_SPINLOCK(acqseq_lock);
1460 spin_lock_bh(&acqseq_lock);
1461 res = (++acqseq ? : ++acqseq);
1462 spin_unlock_bh(&acqseq_lock);
1463 return res;
1465 EXPORT_SYMBOL(xfrm_get_acqseq);
1467 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1469 struct net *net = xs_net(x);
1470 unsigned int h;
1471 struct xfrm_state *x0;
1472 int err = -ENOENT;
1473 __be32 minspi = htonl(low);
1474 __be32 maxspi = htonl(high);
1476 spin_lock_bh(&x->lock);
1477 if (x->km.state == XFRM_STATE_DEAD)
1478 goto unlock;
1480 err = 0;
1481 if (x->id.spi)
1482 goto unlock;
1484 err = -ENOENT;
1486 if (minspi == maxspi) {
1487 x0 = xfrm_state_lookup(net, &x->id.daddr, minspi, x->id.proto, x->props.family);
1488 if (x0) {
1489 xfrm_state_put(x0);
1490 goto unlock;
1492 x->id.spi = minspi;
1493 } else {
1494 u32 spi = 0;
1495 for (h=0; h<high-low+1; h++) {
1496 spi = low + net_random()%(high-low+1);
1497 x0 = xfrm_state_lookup(net, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1498 if (x0 == NULL) {
1499 x->id.spi = htonl(spi);
1500 break;
1502 xfrm_state_put(x0);
1505 if (x->id.spi) {
1506 spin_lock_bh(&xfrm_state_lock);
1507 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1508 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
1509 spin_unlock_bh(&xfrm_state_lock);
1511 err = 0;
1514 unlock:
1515 spin_unlock_bh(&x->lock);
1517 return err;
1519 EXPORT_SYMBOL(xfrm_alloc_spi);
1521 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1522 int (*func)(struct xfrm_state *, int, void*),
1523 void *data)
1525 struct xfrm_state *state;
1526 struct xfrm_state_walk *x;
1527 int err = 0;
1529 if (walk->seq != 0 && list_empty(&walk->all))
1530 return 0;
1532 spin_lock_bh(&xfrm_state_lock);
1533 if (list_empty(&walk->all))
1534 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
1535 else
1536 x = list_entry(&walk->all, struct xfrm_state_walk, all);
1537 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
1538 if (x->state == XFRM_STATE_DEAD)
1539 continue;
1540 state = container_of(x, struct xfrm_state, km);
1541 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
1542 continue;
1543 err = func(state, walk->seq, data);
1544 if (err) {
1545 list_move_tail(&walk->all, &x->all);
1546 goto out;
1548 walk->seq++;
1550 if (walk->seq == 0) {
1551 err = -ENOENT;
1552 goto out;
1554 list_del_init(&walk->all);
1555 out:
1556 spin_unlock_bh(&xfrm_state_lock);
1557 return err;
1559 EXPORT_SYMBOL(xfrm_state_walk);
1561 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1563 INIT_LIST_HEAD(&walk->all);
1564 walk->proto = proto;
1565 walk->state = XFRM_STATE_DEAD;
1566 walk->seq = 0;
1568 EXPORT_SYMBOL(xfrm_state_walk_init);
1570 void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1572 if (list_empty(&walk->all))
1573 return;
1575 spin_lock_bh(&xfrm_state_lock);
1576 list_del(&walk->all);
1577 spin_unlock_bh(&xfrm_state_lock);
1579 EXPORT_SYMBOL(xfrm_state_walk_done);
1582 void xfrm_replay_notify(struct xfrm_state *x, int event)
1584 struct km_event c;
1585 /* we send notify messages in case
1586 * 1. we updated on of the sequence numbers, and the seqno difference
1587 * is at least x->replay_maxdiff, in this case we also update the
1588 * timeout of our timer function
1589 * 2. if x->replay_maxage has elapsed since last update,
1590 * and there were changes
1592 * The state structure must be locked!
1595 switch (event) {
1596 case XFRM_REPLAY_UPDATE:
1597 if (x->replay_maxdiff &&
1598 (x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
1599 (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) {
1600 if (x->xflags & XFRM_TIME_DEFER)
1601 event = XFRM_REPLAY_TIMEOUT;
1602 else
1603 return;
1606 break;
1608 case XFRM_REPLAY_TIMEOUT:
1609 if ((x->replay.seq == x->preplay.seq) &&
1610 (x->replay.bitmap == x->preplay.bitmap) &&
1611 (x->replay.oseq == x->preplay.oseq)) {
1612 x->xflags |= XFRM_TIME_DEFER;
1613 return;
1616 break;
1619 memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
1620 c.event = XFRM_MSG_NEWAE;
1621 c.data.aevent = event;
1622 km_state_notify(x, &c);
1624 if (x->replay_maxage &&
1625 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
1626 x->xflags &= ~XFRM_TIME_DEFER;
1629 static void xfrm_replay_timer_handler(unsigned long data)
1631 struct xfrm_state *x = (struct xfrm_state*)data;
1633 spin_lock(&x->lock);
1635 if (x->km.state == XFRM_STATE_VALID) {
1636 if (xfrm_aevent_is_on(xs_net(x)))
1637 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
1638 else
1639 x->xflags |= XFRM_TIME_DEFER;
1642 spin_unlock(&x->lock);
1645 int xfrm_replay_check(struct xfrm_state *x,
1646 struct sk_buff *skb, __be32 net_seq)
1648 u32 diff;
1649 u32 seq = ntohl(net_seq);
1651 if (unlikely(seq == 0))
1652 goto err;
1654 if (likely(seq > x->replay.seq))
1655 return 0;
1657 diff = x->replay.seq - seq;
1658 if (diff >= min_t(unsigned int, x->props.replay_window,
1659 sizeof(x->replay.bitmap) * 8)) {
1660 x->stats.replay_window++;
1661 goto err;
1664 if (x->replay.bitmap & (1U << diff)) {
1665 x->stats.replay++;
1666 goto err;
1668 return 0;
1670 err:
1671 xfrm_audit_state_replay(x, skb, net_seq);
1672 return -EINVAL;
1675 void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
1677 u32 diff;
1678 u32 seq = ntohl(net_seq);
1680 if (seq > x->replay.seq) {
1681 diff = seq - x->replay.seq;
1682 if (diff < x->props.replay_window)
1683 x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
1684 else
1685 x->replay.bitmap = 1;
1686 x->replay.seq = seq;
1687 } else {
1688 diff = x->replay.seq - seq;
1689 x->replay.bitmap |= (1U << diff);
1692 if (xfrm_aevent_is_on(xs_net(x)))
1693 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
1696 static LIST_HEAD(xfrm_km_list);
1697 static DEFINE_RWLOCK(xfrm_km_lock);
1699 void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1701 struct xfrm_mgr *km;
1703 read_lock(&xfrm_km_lock);
1704 list_for_each_entry(km, &xfrm_km_list, list)
1705 if (km->notify_policy)
1706 km->notify_policy(xp, dir, c);
1707 read_unlock(&xfrm_km_lock);
1710 void km_state_notify(struct xfrm_state *x, struct km_event *c)
1712 struct xfrm_mgr *km;
1713 read_lock(&xfrm_km_lock);
1714 list_for_each_entry(km, &xfrm_km_list, list)
1715 if (km->notify)
1716 km->notify(x, c);
1717 read_unlock(&xfrm_km_lock);
1720 EXPORT_SYMBOL(km_policy_notify);
1721 EXPORT_SYMBOL(km_state_notify);
1723 void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
1725 struct net *net = xs_net(x);
1726 struct km_event c;
1728 c.data.hard = hard;
1729 c.pid = pid;
1730 c.event = XFRM_MSG_EXPIRE;
1731 km_state_notify(x, &c);
1733 if (hard)
1734 wake_up(&net->xfrm.km_waitq);
1737 EXPORT_SYMBOL(km_state_expired);
1739 * We send to all registered managers regardless of failure
1740 * We are happy with one success
1742 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1744 int err = -EINVAL, acqret;
1745 struct xfrm_mgr *km;
1747 read_lock(&xfrm_km_lock);
1748 list_for_each_entry(km, &xfrm_km_list, list) {
1749 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1750 if (!acqret)
1751 err = acqret;
1753 read_unlock(&xfrm_km_lock);
1754 return err;
1756 EXPORT_SYMBOL(km_query);
1758 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1760 int err = -EINVAL;
1761 struct xfrm_mgr *km;
1763 read_lock(&xfrm_km_lock);
1764 list_for_each_entry(km, &xfrm_km_list, list) {
1765 if (km->new_mapping)
1766 err = km->new_mapping(x, ipaddr, sport);
1767 if (!err)
1768 break;
1770 read_unlock(&xfrm_km_lock);
1771 return err;
1773 EXPORT_SYMBOL(km_new_mapping);
1775 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
1777 struct net *net = xp_net(pol);
1778 struct km_event c;
1780 c.data.hard = hard;
1781 c.pid = pid;
1782 c.event = XFRM_MSG_POLEXPIRE;
1783 km_policy_notify(pol, dir, &c);
1785 if (hard)
1786 wake_up(&net->xfrm.km_waitq);
1788 EXPORT_SYMBOL(km_policy_expired);
1790 #ifdef CONFIG_XFRM_MIGRATE
1791 int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1792 struct xfrm_migrate *m, int num_migrate,
1793 struct xfrm_kmaddress *k)
1795 int err = -EINVAL;
1796 int ret;
1797 struct xfrm_mgr *km;
1799 read_lock(&xfrm_km_lock);
1800 list_for_each_entry(km, &xfrm_km_list, list) {
1801 if (km->migrate) {
1802 ret = km->migrate(sel, dir, type, m, num_migrate, k);
1803 if (!ret)
1804 err = ret;
1807 read_unlock(&xfrm_km_lock);
1808 return err;
1810 EXPORT_SYMBOL(km_migrate);
1811 #endif
1813 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1815 int err = -EINVAL;
1816 int ret;
1817 struct xfrm_mgr *km;
1819 read_lock(&xfrm_km_lock);
1820 list_for_each_entry(km, &xfrm_km_list, list) {
1821 if (km->report) {
1822 ret = km->report(net, proto, sel, addr);
1823 if (!ret)
1824 err = ret;
1827 read_unlock(&xfrm_km_lock);
1828 return err;
1830 EXPORT_SYMBOL(km_report);
1832 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1834 int err;
1835 u8 *data;
1836 struct xfrm_mgr *km;
1837 struct xfrm_policy *pol = NULL;
1839 if (optlen <= 0 || optlen > PAGE_SIZE)
1840 return -EMSGSIZE;
1842 data = kmalloc(optlen, GFP_KERNEL);
1843 if (!data)
1844 return -ENOMEM;
1846 err = -EFAULT;
1847 if (copy_from_user(data, optval, optlen))
1848 goto out;
1850 err = -EINVAL;
1851 read_lock(&xfrm_km_lock);
1852 list_for_each_entry(km, &xfrm_km_list, list) {
1853 pol = km->compile_policy(sk, optname, data,
1854 optlen, &err);
1855 if (err >= 0)
1856 break;
1858 read_unlock(&xfrm_km_lock);
1860 if (err >= 0) {
1861 xfrm_sk_policy_insert(sk, err, pol);
1862 xfrm_pol_put(pol);
1863 err = 0;
1866 out:
1867 kfree(data);
1868 return err;
1870 EXPORT_SYMBOL(xfrm_user_policy);
1872 int xfrm_register_km(struct xfrm_mgr *km)
1874 write_lock_bh(&xfrm_km_lock);
1875 list_add_tail(&km->list, &xfrm_km_list);
1876 write_unlock_bh(&xfrm_km_lock);
1877 return 0;
1879 EXPORT_SYMBOL(xfrm_register_km);
1881 int xfrm_unregister_km(struct xfrm_mgr *km)
1883 write_lock_bh(&xfrm_km_lock);
1884 list_del(&km->list);
1885 write_unlock_bh(&xfrm_km_lock);
1886 return 0;
1888 EXPORT_SYMBOL(xfrm_unregister_km);
1890 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1892 int err = 0;
1893 if (unlikely(afinfo == NULL))
1894 return -EINVAL;
1895 if (unlikely(afinfo->family >= NPROTO))
1896 return -EAFNOSUPPORT;
1897 write_lock_bh(&xfrm_state_afinfo_lock);
1898 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1899 err = -ENOBUFS;
1900 else
1901 xfrm_state_afinfo[afinfo->family] = afinfo;
1902 write_unlock_bh(&xfrm_state_afinfo_lock);
1903 return err;
1905 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1907 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1909 int err = 0;
1910 if (unlikely(afinfo == NULL))
1911 return -EINVAL;
1912 if (unlikely(afinfo->family >= NPROTO))
1913 return -EAFNOSUPPORT;
1914 write_lock_bh(&xfrm_state_afinfo_lock);
1915 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1916 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1917 err = -EINVAL;
1918 else
1919 xfrm_state_afinfo[afinfo->family] = NULL;
1921 write_unlock_bh(&xfrm_state_afinfo_lock);
1922 return err;
1924 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1926 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1928 struct xfrm_state_afinfo *afinfo;
1929 if (unlikely(family >= NPROTO))
1930 return NULL;
1931 read_lock(&xfrm_state_afinfo_lock);
1932 afinfo = xfrm_state_afinfo[family];
1933 if (unlikely(!afinfo))
1934 read_unlock(&xfrm_state_afinfo_lock);
1935 return afinfo;
1938 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1939 __releases(xfrm_state_afinfo_lock)
1941 read_unlock(&xfrm_state_afinfo_lock);
1944 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1945 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1947 if (x->tunnel) {
1948 struct xfrm_state *t = x->tunnel;
1950 if (atomic_read(&t->tunnel_users) == 2)
1951 xfrm_state_delete(t);
1952 atomic_dec(&t->tunnel_users);
1953 xfrm_state_put(t);
1954 x->tunnel = NULL;
1957 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1959 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1961 int res;
1963 spin_lock_bh(&x->lock);
1964 if (x->km.state == XFRM_STATE_VALID &&
1965 x->type && x->type->get_mtu)
1966 res = x->type->get_mtu(x, mtu);
1967 else
1968 res = mtu - x->props.header_len;
1969 spin_unlock_bh(&x->lock);
1970 return res;
1973 int xfrm_init_state(struct xfrm_state *x)
1975 struct xfrm_state_afinfo *afinfo;
1976 struct xfrm_mode *inner_mode;
1977 int family = x->props.family;
1978 int err;
1980 err = -EAFNOSUPPORT;
1981 afinfo = xfrm_state_get_afinfo(family);
1982 if (!afinfo)
1983 goto error;
1985 err = 0;
1986 if (afinfo->init_flags)
1987 err = afinfo->init_flags(x);
1989 xfrm_state_put_afinfo(afinfo);
1991 if (err)
1992 goto error;
1994 err = -EPROTONOSUPPORT;
1996 if (x->sel.family != AF_UNSPEC) {
1997 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1998 if (inner_mode == NULL)
1999 goto error;
2001 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2002 family != x->sel.family) {
2003 xfrm_put_mode(inner_mode);
2004 goto error;
2007 x->inner_mode = inner_mode;
2008 } else {
2009 struct xfrm_mode *inner_mode_iaf;
2010 int iafamily = AF_INET;
2012 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2013 if (inner_mode == NULL)
2014 goto error;
2016 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2017 xfrm_put_mode(inner_mode);
2018 goto error;
2020 x->inner_mode = inner_mode;
2022 if (x->props.family == AF_INET)
2023 iafamily = AF_INET6;
2025 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2026 if (inner_mode_iaf) {
2027 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2028 x->inner_mode_iaf = inner_mode_iaf;
2029 else
2030 xfrm_put_mode(inner_mode_iaf);
2034 x->type = xfrm_get_type(x->id.proto, family);
2035 if (x->type == NULL)
2036 goto error;
2038 err = x->type->init_state(x);
2039 if (err)
2040 goto error;
2042 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2043 if (x->outer_mode == NULL)
2044 goto error;
2046 x->km.state = XFRM_STATE_VALID;
2048 error:
2049 return err;
2052 EXPORT_SYMBOL(xfrm_init_state);
2054 int __net_init xfrm_state_init(struct net *net)
2056 unsigned int sz;
2058 INIT_LIST_HEAD(&net->xfrm.state_all);
2060 sz = sizeof(struct hlist_head) * 8;
2062 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2063 if (!net->xfrm.state_bydst)
2064 goto out_bydst;
2065 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2066 if (!net->xfrm.state_bysrc)
2067 goto out_bysrc;
2068 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2069 if (!net->xfrm.state_byspi)
2070 goto out_byspi;
2071 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2073 net->xfrm.state_num = 0;
2074 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2075 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
2076 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
2077 init_waitqueue_head(&net->xfrm.km_waitq);
2078 return 0;
2080 out_byspi:
2081 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2082 out_bysrc:
2083 xfrm_hash_free(net->xfrm.state_bydst, sz);
2084 out_bydst:
2085 return -ENOMEM;
2088 void xfrm_state_fini(struct net *net)
2090 struct xfrm_audit audit_info;
2091 unsigned int sz;
2093 flush_work(&net->xfrm.state_hash_work);
2094 audit_info.loginuid = -1;
2095 audit_info.sessionid = -1;
2096 audit_info.secid = 0;
2097 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2098 flush_work(&net->xfrm.state_gc_work);
2100 WARN_ON(!list_empty(&net->xfrm.state_all));
2102 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2103 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2104 xfrm_hash_free(net->xfrm.state_byspi, sz);
2105 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2106 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2107 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2108 xfrm_hash_free(net->xfrm.state_bydst, sz);
2111 #ifdef CONFIG_AUDITSYSCALL
2112 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2113 struct audit_buffer *audit_buf)
2115 struct xfrm_sec_ctx *ctx = x->security;
2116 u32 spi = ntohl(x->id.spi);
2118 if (ctx)
2119 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2120 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2122 switch(x->props.family) {
2123 case AF_INET:
2124 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2125 &x->props.saddr.a4, &x->id.daddr.a4);
2126 break;
2127 case AF_INET6:
2128 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2129 x->props.saddr.a6, x->id.daddr.a6);
2130 break;
2133 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2136 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2137 struct audit_buffer *audit_buf)
2139 struct iphdr *iph4;
2140 struct ipv6hdr *iph6;
2142 switch (family) {
2143 case AF_INET:
2144 iph4 = ip_hdr(skb);
2145 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2146 &iph4->saddr, &iph4->daddr);
2147 break;
2148 case AF_INET6:
2149 iph6 = ipv6_hdr(skb);
2150 audit_log_format(audit_buf,
2151 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2152 &iph6->saddr,&iph6->daddr,
2153 iph6->flow_lbl[0] & 0x0f,
2154 iph6->flow_lbl[1],
2155 iph6->flow_lbl[2]);
2156 break;
2160 void xfrm_audit_state_add(struct xfrm_state *x, int result,
2161 uid_t auid, u32 sessionid, u32 secid)
2163 struct audit_buffer *audit_buf;
2165 audit_buf = xfrm_audit_start("SAD-add");
2166 if (audit_buf == NULL)
2167 return;
2168 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2169 xfrm_audit_helper_sainfo(x, audit_buf);
2170 audit_log_format(audit_buf, " res=%u", result);
2171 audit_log_end(audit_buf);
2173 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2175 void xfrm_audit_state_delete(struct xfrm_state *x, int result,
2176 uid_t auid, u32 sessionid, u32 secid)
2178 struct audit_buffer *audit_buf;
2180 audit_buf = xfrm_audit_start("SAD-delete");
2181 if (audit_buf == NULL)
2182 return;
2183 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2184 xfrm_audit_helper_sainfo(x, audit_buf);
2185 audit_log_format(audit_buf, " res=%u", result);
2186 audit_log_end(audit_buf);
2188 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2190 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2191 struct sk_buff *skb)
2193 struct audit_buffer *audit_buf;
2194 u32 spi;
2196 audit_buf = xfrm_audit_start("SA-replay-overflow");
2197 if (audit_buf == NULL)
2198 return;
2199 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2200 /* don't record the sequence number because it's inherent in this kind
2201 * of audit message */
2202 spi = ntohl(x->id.spi);
2203 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2204 audit_log_end(audit_buf);
2206 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2208 static void xfrm_audit_state_replay(struct xfrm_state *x,
2209 struct sk_buff *skb, __be32 net_seq)
2211 struct audit_buffer *audit_buf;
2212 u32 spi;
2214 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2215 if (audit_buf == NULL)
2216 return;
2217 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2218 spi = ntohl(x->id.spi);
2219 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2220 spi, spi, ntohl(net_seq));
2221 audit_log_end(audit_buf);
2224 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2226 struct audit_buffer *audit_buf;
2228 audit_buf = xfrm_audit_start("SA-notfound");
2229 if (audit_buf == NULL)
2230 return;
2231 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2232 audit_log_end(audit_buf);
2234 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2236 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2237 __be32 net_spi, __be32 net_seq)
2239 struct audit_buffer *audit_buf;
2240 u32 spi;
2242 audit_buf = xfrm_audit_start("SA-notfound");
2243 if (audit_buf == NULL)
2244 return;
2245 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2246 spi = ntohl(net_spi);
2247 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2248 spi, spi, ntohl(net_seq));
2249 audit_log_end(audit_buf);
2251 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2253 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2254 struct sk_buff *skb, u8 proto)
2256 struct audit_buffer *audit_buf;
2257 __be32 net_spi;
2258 __be32 net_seq;
2260 audit_buf = xfrm_audit_start("SA-icv-failure");
2261 if (audit_buf == NULL)
2262 return;
2263 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2264 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2265 u32 spi = ntohl(net_spi);
2266 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2267 spi, spi, ntohl(net_seq));
2269 audit_log_end(audit_buf);
2271 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2272 #endif /* CONFIG_AUDITSYSCALL */