Linux-2.6.12-rc2
[linux-2.6/kvm.git] / net / key / af_key.c
blobce980aa94ed8e45420223acaf9e1b25e4f03eb45
1 /*
2 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Maxim Giryaev <gem@asplinux.ru>
10 * David S. Miller <davem@redhat.com>
11 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
12 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
14 * Derek Atkins <derek@ihtfp.com>
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/socket.h>
21 #include <linux/pfkeyv2.h>
22 #include <linux/ipsec.h>
23 #include <linux/skbuff.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/in.h>
26 #include <linux/in6.h>
27 #include <linux/proc_fs.h>
28 #include <linux/init.h>
29 #include <net/xfrm.h>
31 #include <net/sock.h>
33 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
34 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
37 /* List of all pfkey sockets. */
38 static HLIST_HEAD(pfkey_table);
39 static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait);
40 static DEFINE_RWLOCK(pfkey_table_lock);
41 static atomic_t pfkey_table_users = ATOMIC_INIT(0);
43 static atomic_t pfkey_socks_nr = ATOMIC_INIT(0);
45 struct pfkey_sock {
46 /* struct sock must be the first member of struct pfkey_sock */
47 struct sock sk;
48 int registered;
49 int promisc;
52 static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
54 return (struct pfkey_sock *)sk;
57 static void pfkey_sock_destruct(struct sock *sk)
59 skb_queue_purge(&sk->sk_receive_queue);
61 if (!sock_flag(sk, SOCK_DEAD)) {
62 printk("Attempt to release alive pfkey socket: %p\n", sk);
63 return;
66 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
67 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
69 atomic_dec(&pfkey_socks_nr);
72 static void pfkey_table_grab(void)
74 write_lock_bh(&pfkey_table_lock);
76 if (atomic_read(&pfkey_table_users)) {
77 DECLARE_WAITQUEUE(wait, current);
79 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
80 for(;;) {
81 set_current_state(TASK_UNINTERRUPTIBLE);
82 if (atomic_read(&pfkey_table_users) == 0)
83 break;
84 write_unlock_bh(&pfkey_table_lock);
85 schedule();
86 write_lock_bh(&pfkey_table_lock);
89 __set_current_state(TASK_RUNNING);
90 remove_wait_queue(&pfkey_table_wait, &wait);
94 static __inline__ void pfkey_table_ungrab(void)
96 write_unlock_bh(&pfkey_table_lock);
97 wake_up(&pfkey_table_wait);
100 static __inline__ void pfkey_lock_table(void)
102 /* read_lock() synchronizes us to pfkey_table_grab */
104 read_lock(&pfkey_table_lock);
105 atomic_inc(&pfkey_table_users);
106 read_unlock(&pfkey_table_lock);
109 static __inline__ void pfkey_unlock_table(void)
111 if (atomic_dec_and_test(&pfkey_table_users))
112 wake_up(&pfkey_table_wait);
116 static struct proto_ops pfkey_ops;
118 static void pfkey_insert(struct sock *sk)
120 pfkey_table_grab();
121 sk_add_node(sk, &pfkey_table);
122 pfkey_table_ungrab();
125 static void pfkey_remove(struct sock *sk)
127 pfkey_table_grab();
128 sk_del_node_init(sk);
129 pfkey_table_ungrab();
132 static struct proto key_proto = {
133 .name = "KEY",
134 .owner = THIS_MODULE,
135 .obj_size = sizeof(struct pfkey_sock),
138 static int pfkey_create(struct socket *sock, int protocol)
140 struct sock *sk;
141 int err;
143 if (!capable(CAP_NET_ADMIN))
144 return -EPERM;
145 if (sock->type != SOCK_RAW)
146 return -ESOCKTNOSUPPORT;
147 if (protocol != PF_KEY_V2)
148 return -EPROTONOSUPPORT;
150 err = -ENOMEM;
151 sk = sk_alloc(PF_KEY, GFP_KERNEL, &key_proto, 1);
152 if (sk == NULL)
153 goto out;
155 sock->ops = &pfkey_ops;
156 sock_init_data(sock, sk);
158 sk->sk_family = PF_KEY;
159 sk->sk_destruct = pfkey_sock_destruct;
161 atomic_inc(&pfkey_socks_nr);
163 pfkey_insert(sk);
165 return 0;
166 out:
167 return err;
170 static int pfkey_release(struct socket *sock)
172 struct sock *sk = sock->sk;
174 if (!sk)
175 return 0;
177 pfkey_remove(sk);
179 sock_orphan(sk);
180 sock->sk = NULL;
181 skb_queue_purge(&sk->sk_write_queue);
182 sock_put(sk);
184 return 0;
187 static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
188 int allocation, struct sock *sk)
190 int err = -ENOBUFS;
192 sock_hold(sk);
193 if (*skb2 == NULL) {
194 if (atomic_read(&skb->users) != 1) {
195 *skb2 = skb_clone(skb, allocation);
196 } else {
197 *skb2 = skb;
198 atomic_inc(&skb->users);
201 if (*skb2 != NULL) {
202 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
203 skb_orphan(*skb2);
204 skb_set_owner_r(*skb2, sk);
205 skb_queue_tail(&sk->sk_receive_queue, *skb2);
206 sk->sk_data_ready(sk, (*skb2)->len);
207 *skb2 = NULL;
208 err = 0;
211 sock_put(sk);
212 return err;
215 /* Send SKB to all pfkey sockets matching selected criteria. */
216 #define BROADCAST_ALL 0
217 #define BROADCAST_ONE 1
218 #define BROADCAST_REGISTERED 2
219 #define BROADCAST_PROMISC_ONLY 4
220 static int pfkey_broadcast(struct sk_buff *skb, int allocation,
221 int broadcast_flags, struct sock *one_sk)
223 struct sock *sk;
224 struct hlist_node *node;
225 struct sk_buff *skb2 = NULL;
226 int err = -ESRCH;
228 /* XXX Do we need something like netlink_overrun? I think
229 * XXX PF_KEY socket apps will not mind current behavior.
231 if (!skb)
232 return -ENOMEM;
234 pfkey_lock_table();
235 sk_for_each(sk, node, &pfkey_table) {
236 struct pfkey_sock *pfk = pfkey_sk(sk);
237 int err2;
239 /* Yes, it means that if you are meant to receive this
240 * pfkey message you receive it twice as promiscuous
241 * socket.
243 if (pfk->promisc)
244 pfkey_broadcast_one(skb, &skb2, allocation, sk);
246 /* the exact target will be processed later */
247 if (sk == one_sk)
248 continue;
249 if (broadcast_flags != BROADCAST_ALL) {
250 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
251 continue;
252 if ((broadcast_flags & BROADCAST_REGISTERED) &&
253 !pfk->registered)
254 continue;
255 if (broadcast_flags & BROADCAST_ONE)
256 continue;
259 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
261 /* Error is cleare after succecful sending to at least one
262 * registered KM */
263 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
264 err = err2;
266 pfkey_unlock_table();
268 if (one_sk != NULL)
269 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
271 if (skb2)
272 kfree_skb(skb2);
273 kfree_skb(skb);
274 return err;
277 static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
279 *new = *orig;
282 static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
284 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
285 struct sadb_msg *hdr;
287 if (!skb)
288 return -ENOBUFS;
290 /* Woe be to the platform trying to support PFKEY yet
291 * having normal errnos outside the 1-255 range, inclusive.
293 err = -err;
294 if (err == ERESTARTSYS ||
295 err == ERESTARTNOHAND ||
296 err == ERESTARTNOINTR)
297 err = EINTR;
298 if (err >= 512)
299 err = EINVAL;
300 if (err <= 0 || err >= 256)
301 BUG();
303 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
304 pfkey_hdr_dup(hdr, orig);
305 hdr->sadb_msg_errno = (uint8_t) err;
306 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
307 sizeof(uint64_t));
309 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);
311 return 0;
314 static u8 sadb_ext_min_len[] = {
315 [SADB_EXT_RESERVED] = (u8) 0,
316 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
317 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
318 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
319 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
320 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
321 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
322 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
323 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
324 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
325 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
326 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
327 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
328 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
329 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
330 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
331 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
332 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
333 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
334 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
335 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
336 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
337 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
338 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
341 /* Verify sadb_address_{len,prefixlen} against sa_family. */
342 static int verify_address_len(void *p)
344 struct sadb_address *sp = p;
345 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
346 struct sockaddr_in *sin;
347 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
348 struct sockaddr_in6 *sin6;
349 #endif
350 int len;
352 switch (addr->sa_family) {
353 case AF_INET:
354 len = sizeof(*sp) + sizeof(*sin) + (sizeof(uint64_t) - 1);
355 len /= sizeof(uint64_t);
356 if (sp->sadb_address_len != len ||
357 sp->sadb_address_prefixlen > 32)
358 return -EINVAL;
359 break;
360 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
361 case AF_INET6:
362 len = sizeof(*sp) + sizeof(*sin6) + (sizeof(uint64_t) - 1);
363 len /= sizeof(uint64_t);
364 if (sp->sadb_address_len != len ||
365 sp->sadb_address_prefixlen > 128)
366 return -EINVAL;
367 break;
368 #endif
369 default:
370 /* It is user using kernel to keep track of security
371 * associations for another protocol, such as
372 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
373 * lengths.
375 * XXX Actually, association/policy database is not yet
376 * XXX able to cope with arbitrary sockaddr families.
377 * XXX When it can, remove this -EINVAL. -DaveM
379 return -EINVAL;
380 break;
383 return 0;
386 static int present_and_same_family(struct sadb_address *src,
387 struct sadb_address *dst)
389 struct sockaddr *s_addr, *d_addr;
391 if (!src || !dst)
392 return 0;
394 s_addr = (struct sockaddr *)(src + 1);
395 d_addr = (struct sockaddr *)(dst + 1);
396 if (s_addr->sa_family != d_addr->sa_family)
397 return 0;
398 if (s_addr->sa_family != AF_INET
399 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
400 && s_addr->sa_family != AF_INET6
401 #endif
403 return 0;
405 return 1;
408 static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
410 char *p = (char *) hdr;
411 int len = skb->len;
413 len -= sizeof(*hdr);
414 p += sizeof(*hdr);
415 while (len > 0) {
416 struct sadb_ext *ehdr = (struct sadb_ext *) p;
417 uint16_t ext_type;
418 int ext_len;
420 ext_len = ehdr->sadb_ext_len;
421 ext_len *= sizeof(uint64_t);
422 ext_type = ehdr->sadb_ext_type;
423 if (ext_len < sizeof(uint64_t) ||
424 ext_len > len ||
425 ext_type == SADB_EXT_RESERVED)
426 return -EINVAL;
428 if (ext_type <= SADB_EXT_MAX) {
429 int min = (int) sadb_ext_min_len[ext_type];
430 if (ext_len < min)
431 return -EINVAL;
432 if (ext_hdrs[ext_type-1] != NULL)
433 return -EINVAL;
434 if (ext_type == SADB_EXT_ADDRESS_SRC ||
435 ext_type == SADB_EXT_ADDRESS_DST ||
436 ext_type == SADB_EXT_ADDRESS_PROXY ||
437 ext_type == SADB_X_EXT_NAT_T_OA) {
438 if (verify_address_len(p))
439 return -EINVAL;
441 ext_hdrs[ext_type-1] = p;
443 p += ext_len;
444 len -= ext_len;
447 return 0;
450 static uint16_t
451 pfkey_satype2proto(uint8_t satype)
453 switch (satype) {
454 case SADB_SATYPE_UNSPEC:
455 return IPSEC_PROTO_ANY;
456 case SADB_SATYPE_AH:
457 return IPPROTO_AH;
458 case SADB_SATYPE_ESP:
459 return IPPROTO_ESP;
460 case SADB_X_SATYPE_IPCOMP:
461 return IPPROTO_COMP;
462 break;
463 default:
464 return 0;
466 /* NOTREACHED */
469 static uint8_t
470 pfkey_proto2satype(uint16_t proto)
472 switch (proto) {
473 case IPPROTO_AH:
474 return SADB_SATYPE_AH;
475 case IPPROTO_ESP:
476 return SADB_SATYPE_ESP;
477 case IPPROTO_COMP:
478 return SADB_X_SATYPE_IPCOMP;
479 break;
480 default:
481 return 0;
483 /* NOTREACHED */
486 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
487 * say specifically 'just raw sockets' as we encode them as 255.
490 static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
492 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
495 static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
497 return (proto ? proto : IPSEC_PROTO_ANY);
500 static int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr,
501 xfrm_address_t *xaddr)
503 switch (((struct sockaddr*)(addr + 1))->sa_family) {
504 case AF_INET:
505 xaddr->a4 =
506 ((struct sockaddr_in *)(addr + 1))->sin_addr.s_addr;
507 return AF_INET;
508 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
509 case AF_INET6:
510 memcpy(xaddr->a6,
511 &((struct sockaddr_in6 *)(addr + 1))->sin6_addr,
512 sizeof(struct in6_addr));
513 return AF_INET6;
514 #endif
515 default:
516 return 0;
518 /* NOTREACHED */
521 static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
523 struct sadb_sa *sa;
524 struct sadb_address *addr;
525 uint16_t proto;
526 unsigned short family;
527 xfrm_address_t *xaddr;
529 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
530 if (sa == NULL)
531 return NULL;
533 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
534 if (proto == 0)
535 return NULL;
537 /* sadb_address_len should be checked by caller */
538 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
539 if (addr == NULL)
540 return NULL;
542 family = ((struct sockaddr *)(addr + 1))->sa_family;
543 switch (family) {
544 case AF_INET:
545 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
546 break;
547 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
548 case AF_INET6:
549 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
550 break;
551 #endif
552 default:
553 xaddr = NULL;
556 if (!xaddr)
557 return NULL;
559 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
562 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
563 static int
564 pfkey_sockaddr_size(sa_family_t family)
566 switch (family) {
567 case AF_INET:
568 return PFKEY_ALIGN8(sizeof(struct sockaddr_in));
569 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
570 case AF_INET6:
571 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6));
572 #endif
573 default:
574 return 0;
576 /* NOTREACHED */
579 static struct sk_buff * pfkey_xfrm_state2msg(struct xfrm_state *x, int add_keys, int hsc)
581 struct sk_buff *skb;
582 struct sadb_msg *hdr;
583 struct sadb_sa *sa;
584 struct sadb_lifetime *lifetime;
585 struct sadb_address *addr;
586 struct sadb_key *key;
587 struct sadb_x_sa2 *sa2;
588 struct sockaddr_in *sin;
589 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
590 struct sockaddr_in6 *sin6;
591 #endif
592 int size;
593 int auth_key_size = 0;
594 int encrypt_key_size = 0;
595 int sockaddr_size;
596 struct xfrm_encap_tmpl *natt = NULL;
598 /* address family check */
599 sockaddr_size = pfkey_sockaddr_size(x->props.family);
600 if (!sockaddr_size)
601 return ERR_PTR(-EINVAL);
603 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
604 key(AE), (identity(SD),) (sensitivity)> */
605 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
606 sizeof(struct sadb_lifetime) +
607 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
608 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
609 sizeof(struct sadb_address)*2 +
610 sockaddr_size*2 +
611 sizeof(struct sadb_x_sa2);
612 /* identity & sensitivity */
614 if ((x->props.family == AF_INET &&
615 x->sel.saddr.a4 != x->props.saddr.a4)
616 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
617 || (x->props.family == AF_INET6 &&
618 memcmp (x->sel.saddr.a6, x->props.saddr.a6, sizeof (struct in6_addr)))
619 #endif
621 size += sizeof(struct sadb_address) + sockaddr_size;
623 if (add_keys) {
624 if (x->aalg && x->aalg->alg_key_len) {
625 auth_key_size =
626 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
627 size += sizeof(struct sadb_key) + auth_key_size;
629 if (x->ealg && x->ealg->alg_key_len) {
630 encrypt_key_size =
631 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
632 size += sizeof(struct sadb_key) + encrypt_key_size;
635 if (x->encap)
636 natt = x->encap;
638 if (natt && natt->encap_type) {
639 size += sizeof(struct sadb_x_nat_t_type);
640 size += sizeof(struct sadb_x_nat_t_port);
641 size += sizeof(struct sadb_x_nat_t_port);
644 skb = alloc_skb(size + 16, GFP_ATOMIC);
645 if (skb == NULL)
646 return ERR_PTR(-ENOBUFS);
648 /* call should fill header later */
649 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
650 memset(hdr, 0, size); /* XXX do we need this ? */
651 hdr->sadb_msg_len = size / sizeof(uint64_t);
653 /* sa */
654 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
655 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
656 sa->sadb_sa_exttype = SADB_EXT_SA;
657 sa->sadb_sa_spi = x->id.spi;
658 sa->sadb_sa_replay = x->props.replay_window;
659 sa->sadb_sa_state = SADB_SASTATE_DYING;
660 if (x->km.state == XFRM_STATE_VALID && !x->km.dying)
661 sa->sadb_sa_state = SADB_SASTATE_MATURE;
662 else if (x->km.state == XFRM_STATE_ACQ)
663 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
664 else if (x->km.state == XFRM_STATE_EXPIRED)
665 sa->sadb_sa_state = SADB_SASTATE_DEAD;
666 sa->sadb_sa_auth = 0;
667 if (x->aalg) {
668 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
669 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
671 sa->sadb_sa_encrypt = 0;
672 BUG_ON(x->ealg && x->calg);
673 if (x->ealg) {
674 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
675 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
677 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
678 if (x->calg) {
679 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
680 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
683 sa->sadb_sa_flags = 0;
684 if (x->props.flags & XFRM_STATE_NOECN)
685 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
686 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
687 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
689 /* hard time */
690 if (hsc & 2) {
691 lifetime = (struct sadb_lifetime *) skb_put(skb,
692 sizeof(struct sadb_lifetime));
693 lifetime->sadb_lifetime_len =
694 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
695 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
696 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
697 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
698 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
699 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
701 /* soft time */
702 if (hsc & 1) {
703 lifetime = (struct sadb_lifetime *) skb_put(skb,
704 sizeof(struct sadb_lifetime));
705 lifetime->sadb_lifetime_len =
706 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
707 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
708 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
709 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
710 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
711 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
713 /* current time */
714 lifetime = (struct sadb_lifetime *) skb_put(skb,
715 sizeof(struct sadb_lifetime));
716 lifetime->sadb_lifetime_len =
717 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
718 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
719 lifetime->sadb_lifetime_allocations = x->curlft.packets;
720 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
721 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
722 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
723 /* src address */
724 addr = (struct sadb_address*) skb_put(skb,
725 sizeof(struct sadb_address)+sockaddr_size);
726 addr->sadb_address_len =
727 (sizeof(struct sadb_address)+sockaddr_size)/
728 sizeof(uint64_t);
729 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
730 /* "if the ports are non-zero, then the sadb_address_proto field,
731 normally zero, MUST be filled in with the transport
732 protocol's number." - RFC2367 */
733 addr->sadb_address_proto = 0;
734 addr->sadb_address_reserved = 0;
735 if (x->props.family == AF_INET) {
736 addr->sadb_address_prefixlen = 32;
738 sin = (struct sockaddr_in *) (addr + 1);
739 sin->sin_family = AF_INET;
740 sin->sin_addr.s_addr = x->props.saddr.a4;
741 sin->sin_port = 0;
742 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
744 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
745 else if (x->props.family == AF_INET6) {
746 addr->sadb_address_prefixlen = 128;
748 sin6 = (struct sockaddr_in6 *) (addr + 1);
749 sin6->sin6_family = AF_INET6;
750 sin6->sin6_port = 0;
751 sin6->sin6_flowinfo = 0;
752 memcpy(&sin6->sin6_addr, x->props.saddr.a6,
753 sizeof(struct in6_addr));
754 sin6->sin6_scope_id = 0;
756 #endif
757 else
758 BUG();
760 /* dst address */
761 addr = (struct sadb_address*) skb_put(skb,
762 sizeof(struct sadb_address)+sockaddr_size);
763 addr->sadb_address_len =
764 (sizeof(struct sadb_address)+sockaddr_size)/
765 sizeof(uint64_t);
766 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
767 addr->sadb_address_proto = 0;
768 addr->sadb_address_prefixlen = 32; /* XXX */
769 addr->sadb_address_reserved = 0;
770 if (x->props.family == AF_INET) {
771 sin = (struct sockaddr_in *) (addr + 1);
772 sin->sin_family = AF_INET;
773 sin->sin_addr.s_addr = x->id.daddr.a4;
774 sin->sin_port = 0;
775 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
777 if (x->sel.saddr.a4 != x->props.saddr.a4) {
778 addr = (struct sadb_address*) skb_put(skb,
779 sizeof(struct sadb_address)+sockaddr_size);
780 addr->sadb_address_len =
781 (sizeof(struct sadb_address)+sockaddr_size)/
782 sizeof(uint64_t);
783 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
784 addr->sadb_address_proto =
785 pfkey_proto_from_xfrm(x->sel.proto);
786 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
787 addr->sadb_address_reserved = 0;
789 sin = (struct sockaddr_in *) (addr + 1);
790 sin->sin_family = AF_INET;
791 sin->sin_addr.s_addr = x->sel.saddr.a4;
792 sin->sin_port = x->sel.sport;
793 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
796 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
797 else if (x->props.family == AF_INET6) {
798 addr->sadb_address_prefixlen = 128;
800 sin6 = (struct sockaddr_in6 *) (addr + 1);
801 sin6->sin6_family = AF_INET6;
802 sin6->sin6_port = 0;
803 sin6->sin6_flowinfo = 0;
804 memcpy(&sin6->sin6_addr, x->id.daddr.a6, sizeof(struct in6_addr));
805 sin6->sin6_scope_id = 0;
807 if (memcmp (x->sel.saddr.a6, x->props.saddr.a6,
808 sizeof(struct in6_addr))) {
809 addr = (struct sadb_address *) skb_put(skb,
810 sizeof(struct sadb_address)+sockaddr_size);
811 addr->sadb_address_len =
812 (sizeof(struct sadb_address)+sockaddr_size)/
813 sizeof(uint64_t);
814 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
815 addr->sadb_address_proto =
816 pfkey_proto_from_xfrm(x->sel.proto);
817 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
818 addr->sadb_address_reserved = 0;
820 sin6 = (struct sockaddr_in6 *) (addr + 1);
821 sin6->sin6_family = AF_INET6;
822 sin6->sin6_port = x->sel.sport;
823 sin6->sin6_flowinfo = 0;
824 memcpy(&sin6->sin6_addr, x->sel.saddr.a6,
825 sizeof(struct in6_addr));
826 sin6->sin6_scope_id = 0;
829 #endif
830 else
831 BUG();
833 /* auth key */
834 if (add_keys && auth_key_size) {
835 key = (struct sadb_key *) skb_put(skb,
836 sizeof(struct sadb_key)+auth_key_size);
837 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
838 sizeof(uint64_t);
839 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
840 key->sadb_key_bits = x->aalg->alg_key_len;
841 key->sadb_key_reserved = 0;
842 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
844 /* encrypt key */
845 if (add_keys && encrypt_key_size) {
846 key = (struct sadb_key *) skb_put(skb,
847 sizeof(struct sadb_key)+encrypt_key_size);
848 key->sadb_key_len = (sizeof(struct sadb_key) +
849 encrypt_key_size) / sizeof(uint64_t);
850 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
851 key->sadb_key_bits = x->ealg->alg_key_len;
852 key->sadb_key_reserved = 0;
853 memcpy(key + 1, x->ealg->alg_key,
854 (x->ealg->alg_key_len+7)/8);
857 /* sa */
858 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
859 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
860 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
861 sa2->sadb_x_sa2_mode = x->props.mode + 1;
862 sa2->sadb_x_sa2_reserved1 = 0;
863 sa2->sadb_x_sa2_reserved2 = 0;
864 sa2->sadb_x_sa2_sequence = 0;
865 sa2->sadb_x_sa2_reqid = x->props.reqid;
867 if (natt && natt->encap_type) {
868 struct sadb_x_nat_t_type *n_type;
869 struct sadb_x_nat_t_port *n_port;
871 /* type */
872 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
873 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
874 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
875 n_type->sadb_x_nat_t_type_type = natt->encap_type;
876 n_type->sadb_x_nat_t_type_reserved[0] = 0;
877 n_type->sadb_x_nat_t_type_reserved[1] = 0;
878 n_type->sadb_x_nat_t_type_reserved[2] = 0;
880 /* source port */
881 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
882 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
883 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
884 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
885 n_port->sadb_x_nat_t_port_reserved = 0;
887 /* dest port */
888 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
889 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
890 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
891 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
892 n_port->sadb_x_nat_t_port_reserved = 0;
895 return skb;
898 static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
899 void **ext_hdrs)
901 struct xfrm_state *x;
902 struct sadb_lifetime *lifetime;
903 struct sadb_sa *sa;
904 struct sadb_key *key;
905 uint16_t proto;
906 int err;
909 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
910 if (!sa ||
911 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
912 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
913 return ERR_PTR(-EINVAL);
914 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
915 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
916 return ERR_PTR(-EINVAL);
917 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
918 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
919 return ERR_PTR(-EINVAL);
920 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
921 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
922 return ERR_PTR(-EINVAL);
924 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
925 if (proto == 0)
926 return ERR_PTR(-EINVAL);
928 /* default error is no buffer space */
929 err = -ENOBUFS;
931 /* RFC2367:
933 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
934 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
935 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
936 Therefore, the sadb_sa_state field of all submitted SAs MUST be
937 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
938 not true.
940 However, KAME setkey always uses SADB_SASTATE_LARVAL.
941 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
943 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
944 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
945 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
946 sa->sadb_sa_encrypt > SADB_EALG_MAX)
947 return ERR_PTR(-EINVAL);
948 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
949 if (key != NULL &&
950 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
951 ((key->sadb_key_bits+7) / 8 == 0 ||
952 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
953 return ERR_PTR(-EINVAL);
954 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
955 if (key != NULL &&
956 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
957 ((key->sadb_key_bits+7) / 8 == 0 ||
958 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
959 return ERR_PTR(-EINVAL);
961 x = xfrm_state_alloc();
962 if (x == NULL)
963 return ERR_PTR(-ENOBUFS);
965 x->id.proto = proto;
966 x->id.spi = sa->sadb_sa_spi;
967 x->props.replay_window = sa->sadb_sa_replay;
968 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
969 x->props.flags |= XFRM_STATE_NOECN;
970 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
971 x->props.flags |= XFRM_STATE_DECAP_DSCP;
973 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
974 if (lifetime != NULL) {
975 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
976 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
977 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
978 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
980 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
981 if (lifetime != NULL) {
982 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
983 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
984 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
985 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
987 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
988 if (sa->sadb_sa_auth) {
989 int keysize = 0;
990 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
991 if (!a) {
992 err = -ENOSYS;
993 goto out;
995 if (key)
996 keysize = (key->sadb_key_bits + 7) / 8;
997 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
998 if (!x->aalg)
999 goto out;
1000 strcpy(x->aalg->alg_name, a->name);
1001 x->aalg->alg_key_len = 0;
1002 if (key) {
1003 x->aalg->alg_key_len = key->sadb_key_bits;
1004 memcpy(x->aalg->alg_key, key+1, keysize);
1006 x->props.aalgo = sa->sadb_sa_auth;
1007 /* x->algo.flags = sa->sadb_sa_flags; */
1009 if (sa->sadb_sa_encrypt) {
1010 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1011 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1012 if (!a) {
1013 err = -ENOSYS;
1014 goto out;
1016 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1017 if (!x->calg)
1018 goto out;
1019 strcpy(x->calg->alg_name, a->name);
1020 x->props.calgo = sa->sadb_sa_encrypt;
1021 } else {
1022 int keysize = 0;
1023 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1024 if (!a) {
1025 err = -ENOSYS;
1026 goto out;
1028 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1029 if (key)
1030 keysize = (key->sadb_key_bits + 7) / 8;
1031 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1032 if (!x->ealg)
1033 goto out;
1034 strcpy(x->ealg->alg_name, a->name);
1035 x->ealg->alg_key_len = 0;
1036 if (key) {
1037 x->ealg->alg_key_len = key->sadb_key_bits;
1038 memcpy(x->ealg->alg_key, key+1, keysize);
1040 x->props.ealgo = sa->sadb_sa_encrypt;
1043 /* x->algo.flags = sa->sadb_sa_flags; */
1045 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1046 &x->props.saddr);
1047 if (!x->props.family) {
1048 err = -EAFNOSUPPORT;
1049 goto out;
1051 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1052 &x->id.daddr);
1054 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1055 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
1056 x->props.mode = sa2->sadb_x_sa2_mode;
1057 if (x->props.mode)
1058 x->props.mode--;
1059 x->props.reqid = sa2->sadb_x_sa2_reqid;
1062 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1063 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1065 /* Nobody uses this, but we try. */
1066 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1067 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1070 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1071 struct sadb_x_nat_t_type* n_type;
1072 struct xfrm_encap_tmpl *natt;
1074 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1075 if (!x->encap)
1076 goto out;
1078 natt = x->encap;
1079 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1080 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1082 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1083 struct sadb_x_nat_t_port* n_port =
1084 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1085 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1087 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1088 struct sadb_x_nat_t_port* n_port =
1089 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1090 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1094 x->type = xfrm_get_type(proto, x->props.family);
1095 if (x->type == NULL) {
1096 err = -ENOPROTOOPT;
1097 goto out;
1099 if (x->type->init_state(x, NULL)) {
1100 err = -EINVAL;
1101 goto out;
1103 x->km.seq = hdr->sadb_msg_seq;
1104 x->km.state = XFRM_STATE_VALID;
1105 return x;
1107 out:
1108 x->km.state = XFRM_STATE_DEAD;
1109 xfrm_state_put(x);
1110 return ERR_PTR(err);
1113 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1115 return -EOPNOTSUPP;
1118 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1120 struct sk_buff *resp_skb;
1121 struct sadb_x_sa2 *sa2;
1122 struct sadb_address *saddr, *daddr;
1123 struct sadb_msg *out_hdr;
1124 struct xfrm_state *x = NULL;
1125 u8 mode;
1126 u32 reqid;
1127 u8 proto;
1128 unsigned short family;
1129 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1131 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1132 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1133 return -EINVAL;
1135 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1136 if (proto == 0)
1137 return -EINVAL;
1139 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1140 mode = sa2->sadb_x_sa2_mode - 1;
1141 reqid = sa2->sadb_x_sa2_reqid;
1142 } else {
1143 mode = 0;
1144 reqid = 0;
1147 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1148 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1150 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1151 switch (family) {
1152 case AF_INET:
1153 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1154 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1155 break;
1156 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1157 case AF_INET6:
1158 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1159 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1160 break;
1161 #endif
1164 if (hdr->sadb_msg_seq) {
1165 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1166 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1167 xfrm_state_put(x);
1168 x = NULL;
1172 if (!x)
1173 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1175 if (x == NULL)
1176 return -ENOENT;
1178 resp_skb = ERR_PTR(-ENOENT);
1180 spin_lock_bh(&x->lock);
1181 if (x->km.state != XFRM_STATE_DEAD) {
1182 struct sadb_spirange *range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1183 u32 min_spi, max_spi;
1185 if (range != NULL) {
1186 min_spi = range->sadb_spirange_min;
1187 max_spi = range->sadb_spirange_max;
1188 } else {
1189 min_spi = 0x100;
1190 max_spi = 0x0fffffff;
1192 xfrm_alloc_spi(x, htonl(min_spi), htonl(max_spi));
1193 if (x->id.spi)
1194 resp_skb = pfkey_xfrm_state2msg(x, 0, 3);
1196 spin_unlock_bh(&x->lock);
1198 if (IS_ERR(resp_skb)) {
1199 xfrm_state_put(x);
1200 return PTR_ERR(resp_skb);
1203 out_hdr = (struct sadb_msg *) resp_skb->data;
1204 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1205 out_hdr->sadb_msg_type = SADB_GETSPI;
1206 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1207 out_hdr->sadb_msg_errno = 0;
1208 out_hdr->sadb_msg_reserved = 0;
1209 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1210 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1212 xfrm_state_put(x);
1214 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1216 return 0;
1219 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1221 struct xfrm_state *x;
1223 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1224 return -EOPNOTSUPP;
1226 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1227 return 0;
1229 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1230 if (x == NULL)
1231 return 0;
1233 spin_lock_bh(&x->lock);
1234 if (x->km.state == XFRM_STATE_ACQ) {
1235 x->km.state = XFRM_STATE_ERROR;
1236 wake_up(&km_waitq);
1238 spin_unlock_bh(&x->lock);
1239 xfrm_state_put(x);
1240 return 0;
1244 static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1246 struct sk_buff *out_skb;
1247 struct sadb_msg *out_hdr;
1248 struct xfrm_state *x;
1249 int err;
1251 xfrm_probe_algs();
1253 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1254 if (IS_ERR(x))
1255 return PTR_ERR(x);
1257 if (hdr->sadb_msg_type == SADB_ADD)
1258 err = xfrm_state_add(x);
1259 else
1260 err = xfrm_state_update(x);
1262 if (err < 0) {
1263 x->km.state = XFRM_STATE_DEAD;
1264 xfrm_state_put(x);
1265 return err;
1268 out_skb = pfkey_xfrm_state2msg(x, 0, 3);
1269 if (IS_ERR(out_skb))
1270 return PTR_ERR(out_skb); /* XXX Should we return 0 here ? */
1272 out_hdr = (struct sadb_msg *) out_skb->data;
1273 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1274 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
1275 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1276 out_hdr->sadb_msg_errno = 0;
1277 out_hdr->sadb_msg_reserved = 0;
1278 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1279 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1281 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
1283 return 0;
1286 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1288 struct xfrm_state *x;
1290 if (!ext_hdrs[SADB_EXT_SA-1] ||
1291 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1292 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1293 return -EINVAL;
1295 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1296 if (x == NULL)
1297 return -ESRCH;
1299 if (xfrm_state_kern(x)) {
1300 xfrm_state_put(x);
1301 return -EPERM;
1304 xfrm_state_delete(x);
1305 xfrm_state_put(x);
1307 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
1308 BROADCAST_ALL, sk);
1310 return 0;
1313 static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1315 __u8 proto;
1316 struct sk_buff *out_skb;
1317 struct sadb_msg *out_hdr;
1318 struct xfrm_state *x;
1320 if (!ext_hdrs[SADB_EXT_SA-1] ||
1321 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1322 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1323 return -EINVAL;
1325 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1326 if (x == NULL)
1327 return -ESRCH;
1329 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1330 proto = x->id.proto;
1331 xfrm_state_put(x);
1332 if (IS_ERR(out_skb))
1333 return PTR_ERR(out_skb);
1335 out_hdr = (struct sadb_msg *) out_skb->data;
1336 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1337 out_hdr->sadb_msg_type = SADB_DUMP;
1338 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1339 out_hdr->sadb_msg_errno = 0;
1340 out_hdr->sadb_msg_reserved = 0;
1341 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1342 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1343 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1345 return 0;
1348 static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig, int allocation)
1350 struct sk_buff *skb;
1351 struct sadb_msg *hdr;
1352 int len, auth_len, enc_len, i;
1354 auth_len = xfrm_count_auth_supported();
1355 if (auth_len) {
1356 auth_len *= sizeof(struct sadb_alg);
1357 auth_len += sizeof(struct sadb_supported);
1360 enc_len = xfrm_count_enc_supported();
1361 if (enc_len) {
1362 enc_len *= sizeof(struct sadb_alg);
1363 enc_len += sizeof(struct sadb_supported);
1366 len = enc_len + auth_len + sizeof(struct sadb_msg);
1368 skb = alloc_skb(len + 16, allocation);
1369 if (!skb)
1370 goto out_put_algs;
1372 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1373 pfkey_hdr_dup(hdr, orig);
1374 hdr->sadb_msg_errno = 0;
1375 hdr->sadb_msg_len = len / sizeof(uint64_t);
1377 if (auth_len) {
1378 struct sadb_supported *sp;
1379 struct sadb_alg *ap;
1381 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1382 ap = (struct sadb_alg *) (sp + 1);
1384 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1385 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1387 for (i = 0; ; i++) {
1388 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1389 if (!aalg)
1390 break;
1391 if (aalg->available)
1392 *ap++ = aalg->desc;
1396 if (enc_len) {
1397 struct sadb_supported *sp;
1398 struct sadb_alg *ap;
1400 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1401 ap = (struct sadb_alg *) (sp + 1);
1403 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1404 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1406 for (i = 0; ; i++) {
1407 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1408 if (!ealg)
1409 break;
1410 if (ealg->available)
1411 *ap++ = ealg->desc;
1415 out_put_algs:
1416 return skb;
1419 static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1421 struct pfkey_sock *pfk = pfkey_sk(sk);
1422 struct sk_buff *supp_skb;
1424 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1425 return -EINVAL;
1427 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1428 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1429 return -EEXIST;
1430 pfk->registered |= (1<<hdr->sadb_msg_satype);
1433 xfrm_probe_algs();
1435 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1436 if (!supp_skb) {
1437 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1438 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1440 return -ENOBUFS;
1443 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1445 return 0;
1448 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1450 unsigned proto;
1451 struct sk_buff *skb_out;
1452 struct sadb_msg *hdr_out;
1454 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1455 if (proto == 0)
1456 return -EINVAL;
1458 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
1459 if (!skb_out)
1460 return -ENOBUFS;
1462 xfrm_state_flush(proto);
1464 hdr_out = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
1465 pfkey_hdr_dup(hdr_out, hdr);
1466 hdr_out->sadb_msg_errno = (uint8_t) 0;
1467 hdr_out->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1469 pfkey_broadcast(skb_out, GFP_KERNEL, BROADCAST_ALL, NULL);
1471 return 0;
1474 struct pfkey_dump_data
1476 struct sk_buff *skb;
1477 struct sadb_msg *hdr;
1478 struct sock *sk;
1481 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1483 struct pfkey_dump_data *data = ptr;
1484 struct sk_buff *out_skb;
1485 struct sadb_msg *out_hdr;
1487 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1488 if (IS_ERR(out_skb))
1489 return PTR_ERR(out_skb);
1491 out_hdr = (struct sadb_msg *) out_skb->data;
1492 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
1493 out_hdr->sadb_msg_type = SADB_DUMP;
1494 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1495 out_hdr->sadb_msg_errno = 0;
1496 out_hdr->sadb_msg_reserved = 0;
1497 out_hdr->sadb_msg_seq = count;
1498 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
1499 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
1500 return 0;
1503 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1505 u8 proto;
1506 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
1508 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1509 if (proto == 0)
1510 return -EINVAL;
1512 return xfrm_state_walk(proto, dump_sa, &data);
1515 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1517 struct pfkey_sock *pfk = pfkey_sk(sk);
1518 int satype = hdr->sadb_msg_satype;
1520 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1521 /* XXX we mangle packet... */
1522 hdr->sadb_msg_errno = 0;
1523 if (satype != 0 && satype != 1)
1524 return -EINVAL;
1525 pfk->promisc = satype;
1527 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1528 return 0;
1531 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1533 int i;
1534 u32 reqid = *(u32*)ptr;
1536 for (i=0; i<xp->xfrm_nr; i++) {
1537 if (xp->xfrm_vec[i].reqid == reqid)
1538 return -EEXIST;
1540 return 0;
1543 static u32 gen_reqid(void)
1545 u32 start;
1546 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1548 start = reqid;
1549 do {
1550 ++reqid;
1551 if (reqid == 0)
1552 reqid = IPSEC_MANUAL_REQID_MAX+1;
1553 if (xfrm_policy_walk(check_reqid, (void*)&reqid) != -EEXIST)
1554 return reqid;
1555 } while (reqid != start);
1556 return 0;
1559 static int
1560 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1562 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1563 struct sockaddr_in *sin;
1564 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1565 struct sockaddr_in6 *sin6;
1566 #endif
1568 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1569 return -ELOOP;
1571 if (rq->sadb_x_ipsecrequest_mode == 0)
1572 return -EINVAL;
1574 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1575 t->mode = rq->sadb_x_ipsecrequest_mode-1;
1576 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1577 t->optional = 1;
1578 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1579 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1580 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1581 t->reqid = 0;
1582 if (!t->reqid && !(t->reqid = gen_reqid()))
1583 return -ENOBUFS;
1586 /* addresses present only in tunnel mode */
1587 if (t->mode) {
1588 switch (xp->family) {
1589 case AF_INET:
1590 sin = (void*)(rq+1);
1591 if (sin->sin_family != AF_INET)
1592 return -EINVAL;
1593 t->saddr.a4 = sin->sin_addr.s_addr;
1594 sin++;
1595 if (sin->sin_family != AF_INET)
1596 return -EINVAL;
1597 t->id.daddr.a4 = sin->sin_addr.s_addr;
1598 break;
1599 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1600 case AF_INET6:
1601 sin6 = (void *)(rq+1);
1602 if (sin6->sin6_family != AF_INET6)
1603 return -EINVAL;
1604 memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1605 sin6++;
1606 if (sin6->sin6_family != AF_INET6)
1607 return -EINVAL;
1608 memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1609 break;
1610 #endif
1611 default:
1612 return -EINVAL;
1615 /* No way to set this via kame pfkey */
1616 t->aalgos = t->ealgos = t->calgos = ~0;
1617 xp->xfrm_nr++;
1618 return 0;
1621 static int
1622 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1624 int err;
1625 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1626 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1628 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1629 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1630 return err;
1631 len -= rq->sadb_x_ipsecrequest_len;
1632 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1634 return 0;
1637 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1639 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1640 int socklen = (xp->family == AF_INET ?
1641 sizeof(struct sockaddr_in) :
1642 sizeof(struct sockaddr_in6));
1644 return sizeof(struct sadb_msg) +
1645 (sizeof(struct sadb_lifetime) * 3) +
1646 (sizeof(struct sadb_address) * 2) +
1647 (sockaddr_size * 2) +
1648 sizeof(struct sadb_x_policy) +
1649 (xp->xfrm_nr * (sizeof(struct sadb_x_ipsecrequest) +
1650 (socklen * 2)));
1653 static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1655 struct sk_buff *skb;
1656 int size;
1658 size = pfkey_xfrm_policy2msg_size(xp);
1660 skb = alloc_skb(size + 16, GFP_ATOMIC);
1661 if (skb == NULL)
1662 return ERR_PTR(-ENOBUFS);
1664 return skb;
1667 static void pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1669 struct sadb_msg *hdr;
1670 struct sadb_address *addr;
1671 struct sadb_lifetime *lifetime;
1672 struct sadb_x_policy *pol;
1673 struct sockaddr_in *sin;
1674 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1675 struct sockaddr_in6 *sin6;
1676 #endif
1677 int i;
1678 int size;
1679 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1680 int socklen = (xp->family == AF_INET ?
1681 sizeof(struct sockaddr_in) :
1682 sizeof(struct sockaddr_in6));
1684 size = pfkey_xfrm_policy2msg_size(xp);
1686 /* call should fill header later */
1687 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1688 memset(hdr, 0, size); /* XXX do we need this ? */
1690 /* src address */
1691 addr = (struct sadb_address*) skb_put(skb,
1692 sizeof(struct sadb_address)+sockaddr_size);
1693 addr->sadb_address_len =
1694 (sizeof(struct sadb_address)+sockaddr_size)/
1695 sizeof(uint64_t);
1696 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1697 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1698 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1699 addr->sadb_address_reserved = 0;
1700 /* src address */
1701 if (xp->family == AF_INET) {
1702 sin = (struct sockaddr_in *) (addr + 1);
1703 sin->sin_family = AF_INET;
1704 sin->sin_addr.s_addr = xp->selector.saddr.a4;
1705 sin->sin_port = xp->selector.sport;
1706 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1708 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1709 else if (xp->family == AF_INET6) {
1710 sin6 = (struct sockaddr_in6 *) (addr + 1);
1711 sin6->sin6_family = AF_INET6;
1712 sin6->sin6_port = xp->selector.sport;
1713 sin6->sin6_flowinfo = 0;
1714 memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
1715 sizeof(struct in6_addr));
1716 sin6->sin6_scope_id = 0;
1718 #endif
1719 else
1720 BUG();
1722 /* dst address */
1723 addr = (struct sadb_address*) skb_put(skb,
1724 sizeof(struct sadb_address)+sockaddr_size);
1725 addr->sadb_address_len =
1726 (sizeof(struct sadb_address)+sockaddr_size)/
1727 sizeof(uint64_t);
1728 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1729 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1730 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
1731 addr->sadb_address_reserved = 0;
1732 if (xp->family == AF_INET) {
1733 sin = (struct sockaddr_in *) (addr + 1);
1734 sin->sin_family = AF_INET;
1735 sin->sin_addr.s_addr = xp->selector.daddr.a4;
1736 sin->sin_port = xp->selector.dport;
1737 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1739 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1740 else if (xp->family == AF_INET6) {
1741 sin6 = (struct sockaddr_in6 *) (addr + 1);
1742 sin6->sin6_family = AF_INET6;
1743 sin6->sin6_port = xp->selector.dport;
1744 sin6->sin6_flowinfo = 0;
1745 memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
1746 sizeof(struct in6_addr));
1747 sin6->sin6_scope_id = 0;
1749 #endif
1750 else
1751 BUG();
1753 /* hard time */
1754 lifetime = (struct sadb_lifetime *) skb_put(skb,
1755 sizeof(struct sadb_lifetime));
1756 lifetime->sadb_lifetime_len =
1757 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1758 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1759 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
1760 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
1761 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
1762 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
1763 /* soft time */
1764 lifetime = (struct sadb_lifetime *) skb_put(skb,
1765 sizeof(struct sadb_lifetime));
1766 lifetime->sadb_lifetime_len =
1767 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1768 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
1769 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
1770 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
1771 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
1772 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
1773 /* current time */
1774 lifetime = (struct sadb_lifetime *) skb_put(skb,
1775 sizeof(struct sadb_lifetime));
1776 lifetime->sadb_lifetime_len =
1777 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1778 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
1779 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
1780 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
1781 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
1782 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
1784 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
1785 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
1786 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1787 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
1788 if (xp->action == XFRM_POLICY_ALLOW) {
1789 if (xp->xfrm_nr)
1790 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1791 else
1792 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
1794 pol->sadb_x_policy_dir = dir+1;
1795 pol->sadb_x_policy_id = xp->index;
1796 pol->sadb_x_policy_priority = xp->priority;
1798 for (i=0; i<xp->xfrm_nr; i++) {
1799 struct sadb_x_ipsecrequest *rq;
1800 struct xfrm_tmpl *t = xp->xfrm_vec + i;
1801 int req_size;
1803 req_size = sizeof(struct sadb_x_ipsecrequest);
1804 if (t->mode)
1805 req_size += 2*socklen;
1806 else
1807 size -= 2*socklen;
1808 rq = (void*)skb_put(skb, req_size);
1809 pol->sadb_x_policy_len += req_size/8;
1810 memset(rq, 0, sizeof(*rq));
1811 rq->sadb_x_ipsecrequest_len = req_size;
1812 rq->sadb_x_ipsecrequest_proto = t->id.proto;
1813 rq->sadb_x_ipsecrequest_mode = t->mode+1;
1814 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
1815 if (t->reqid)
1816 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
1817 if (t->optional)
1818 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
1819 rq->sadb_x_ipsecrequest_reqid = t->reqid;
1820 if (t->mode) {
1821 switch (xp->family) {
1822 case AF_INET:
1823 sin = (void*)(rq+1);
1824 sin->sin_family = AF_INET;
1825 sin->sin_addr.s_addr = t->saddr.a4;
1826 sin->sin_port = 0;
1827 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1828 sin++;
1829 sin->sin_family = AF_INET;
1830 sin->sin_addr.s_addr = t->id.daddr.a4;
1831 sin->sin_port = 0;
1832 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1833 break;
1834 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1835 case AF_INET6:
1836 sin6 = (void*)(rq+1);
1837 sin6->sin6_family = AF_INET6;
1838 sin6->sin6_port = 0;
1839 sin6->sin6_flowinfo = 0;
1840 memcpy(&sin6->sin6_addr, t->saddr.a6,
1841 sizeof(struct in6_addr));
1842 sin6->sin6_scope_id = 0;
1844 sin6++;
1845 sin6->sin6_family = AF_INET6;
1846 sin6->sin6_port = 0;
1847 sin6->sin6_flowinfo = 0;
1848 memcpy(&sin6->sin6_addr, t->id.daddr.a6,
1849 sizeof(struct in6_addr));
1850 sin6->sin6_scope_id = 0;
1851 break;
1852 #endif
1853 default:
1854 break;
1858 hdr->sadb_msg_len = size / sizeof(uint64_t);
1859 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
1862 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1864 int err;
1865 struct sadb_lifetime *lifetime;
1866 struct sadb_address *sa;
1867 struct sadb_x_policy *pol;
1868 struct xfrm_policy *xp;
1869 struct sk_buff *out_skb;
1870 struct sadb_msg *out_hdr;
1872 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1873 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
1874 !ext_hdrs[SADB_X_EXT_POLICY-1])
1875 return -EINVAL;
1877 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
1878 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
1879 return -EINVAL;
1880 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
1881 return -EINVAL;
1883 xp = xfrm_policy_alloc(GFP_KERNEL);
1884 if (xp == NULL)
1885 return -ENOBUFS;
1887 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
1888 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
1889 xp->priority = pol->sadb_x_policy_priority;
1891 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1892 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
1893 if (!xp->family) {
1894 err = -EINVAL;
1895 goto out;
1897 xp->selector.family = xp->family;
1898 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
1899 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
1900 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
1901 if (xp->selector.sport)
1902 xp->selector.sport_mask = ~0;
1904 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1905 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
1906 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
1908 /* Amusing, we set this twice. KAME apps appear to set same value
1909 * in both addresses.
1911 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
1913 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
1914 if (xp->selector.dport)
1915 xp->selector.dport_mask = ~0;
1917 xp->lft.soft_byte_limit = XFRM_INF;
1918 xp->lft.hard_byte_limit = XFRM_INF;
1919 xp->lft.soft_packet_limit = XFRM_INF;
1920 xp->lft.hard_packet_limit = XFRM_INF;
1921 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
1922 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1923 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1924 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1925 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1927 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
1928 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1929 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1930 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1931 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1933 xp->xfrm_nr = 0;
1934 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
1935 (err = parse_ipsecrequests(xp, pol)) < 0)
1936 goto out;
1938 out_skb = pfkey_xfrm_policy2msg_prep(xp);
1939 if (IS_ERR(out_skb)) {
1940 err = PTR_ERR(out_skb);
1941 goto out;
1944 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
1945 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
1946 if (err) {
1947 kfree_skb(out_skb);
1948 goto out;
1951 pfkey_xfrm_policy2msg(out_skb, xp, pol->sadb_x_policy_dir-1);
1953 xfrm_pol_put(xp);
1955 out_hdr = (struct sadb_msg *) out_skb->data;
1956 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1957 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
1958 out_hdr->sadb_msg_satype = 0;
1959 out_hdr->sadb_msg_errno = 0;
1960 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1961 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1962 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
1963 return 0;
1965 out:
1966 kfree(xp);
1967 return err;
1970 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1972 int err;
1973 struct sadb_address *sa;
1974 struct sadb_x_policy *pol;
1975 struct xfrm_policy *xp;
1976 struct sk_buff *out_skb;
1977 struct sadb_msg *out_hdr;
1978 struct xfrm_selector sel;
1980 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1981 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
1982 !ext_hdrs[SADB_X_EXT_POLICY-1])
1983 return -EINVAL;
1985 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
1986 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
1987 return -EINVAL;
1989 memset(&sel, 0, sizeof(sel));
1991 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1992 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
1993 sel.prefixlen_s = sa->sadb_address_prefixlen;
1994 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
1995 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
1996 if (sel.sport)
1997 sel.sport_mask = ~0;
1999 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2000 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2001 sel.prefixlen_d = sa->sadb_address_prefixlen;
2002 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2003 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2004 if (sel.dport)
2005 sel.dport_mask = ~0;
2007 xp = xfrm_policy_bysel(pol->sadb_x_policy_dir-1, &sel, 1);
2008 if (xp == NULL)
2009 return -ENOENT;
2011 err = 0;
2013 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2014 if (IS_ERR(out_skb)) {
2015 err = PTR_ERR(out_skb);
2016 goto out;
2018 pfkey_xfrm_policy2msg(out_skb, xp, pol->sadb_x_policy_dir-1);
2020 out_hdr = (struct sadb_msg *) out_skb->data;
2021 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2022 out_hdr->sadb_msg_type = SADB_X_SPDDELETE;
2023 out_hdr->sadb_msg_satype = 0;
2024 out_hdr->sadb_msg_errno = 0;
2025 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2026 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2027 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
2028 err = 0;
2030 out:
2031 xfrm_pol_put(xp);
2032 return err;
2035 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2037 int err;
2038 struct sadb_x_policy *pol;
2039 struct xfrm_policy *xp;
2040 struct sk_buff *out_skb;
2041 struct sadb_msg *out_hdr;
2043 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2044 return -EINVAL;
2046 xp = xfrm_policy_byid(0, pol->sadb_x_policy_id,
2047 hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2048 if (xp == NULL)
2049 return -ENOENT;
2051 err = 0;
2053 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2054 if (IS_ERR(out_skb)) {
2055 err = PTR_ERR(out_skb);
2056 goto out;
2058 pfkey_xfrm_policy2msg(out_skb, xp, pol->sadb_x_policy_dir-1);
2060 out_hdr = (struct sadb_msg *) out_skb->data;
2061 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2062 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2063 out_hdr->sadb_msg_satype = 0;
2064 out_hdr->sadb_msg_errno = 0;
2065 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2066 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2067 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
2068 err = 0;
2070 out:
2071 xfrm_pol_put(xp);
2072 return err;
2075 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2077 struct pfkey_dump_data *data = ptr;
2078 struct sk_buff *out_skb;
2079 struct sadb_msg *out_hdr;
2081 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2082 if (IS_ERR(out_skb))
2083 return PTR_ERR(out_skb);
2085 pfkey_xfrm_policy2msg(out_skb, xp, dir);
2087 out_hdr = (struct sadb_msg *) out_skb->data;
2088 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
2089 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2090 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2091 out_hdr->sadb_msg_errno = 0;
2092 out_hdr->sadb_msg_seq = count;
2093 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
2094 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
2095 return 0;
2098 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2100 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
2102 return xfrm_policy_walk(dump_sp, &data);
2105 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2107 struct sk_buff *skb_out;
2108 struct sadb_msg *hdr_out;
2110 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
2111 if (!skb_out)
2112 return -ENOBUFS;
2114 xfrm_policy_flush();
2116 hdr_out = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2117 pfkey_hdr_dup(hdr_out, hdr);
2118 hdr_out->sadb_msg_errno = (uint8_t) 0;
2119 hdr_out->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2120 pfkey_broadcast(skb_out, GFP_KERNEL, BROADCAST_ALL, NULL);
2122 return 0;
2125 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2126 struct sadb_msg *hdr, void **ext_hdrs);
2127 static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2128 [SADB_RESERVED] = pfkey_reserved,
2129 [SADB_GETSPI] = pfkey_getspi,
2130 [SADB_UPDATE] = pfkey_add,
2131 [SADB_ADD] = pfkey_add,
2132 [SADB_DELETE] = pfkey_delete,
2133 [SADB_GET] = pfkey_get,
2134 [SADB_ACQUIRE] = pfkey_acquire,
2135 [SADB_REGISTER] = pfkey_register,
2136 [SADB_EXPIRE] = NULL,
2137 [SADB_FLUSH] = pfkey_flush,
2138 [SADB_DUMP] = pfkey_dump,
2139 [SADB_X_PROMISC] = pfkey_promisc,
2140 [SADB_X_PCHANGE] = NULL,
2141 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2142 [SADB_X_SPDADD] = pfkey_spdadd,
2143 [SADB_X_SPDDELETE] = pfkey_spddelete,
2144 [SADB_X_SPDGET] = pfkey_spdget,
2145 [SADB_X_SPDACQUIRE] = NULL,
2146 [SADB_X_SPDDUMP] = pfkey_spddump,
2147 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2148 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2149 [SADB_X_SPDDELETE2] = pfkey_spdget,
2152 static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2154 void *ext_hdrs[SADB_EXT_MAX];
2155 int err;
2157 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2158 BROADCAST_PROMISC_ONLY, NULL);
2160 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2161 err = parse_exthdrs(skb, hdr, ext_hdrs);
2162 if (!err) {
2163 err = -EOPNOTSUPP;
2164 if (pfkey_funcs[hdr->sadb_msg_type])
2165 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2167 return err;
2170 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2172 struct sadb_msg *hdr = NULL;
2174 if (skb->len < sizeof(*hdr)) {
2175 *errp = -EMSGSIZE;
2176 } else {
2177 hdr = (struct sadb_msg *) skb->data;
2178 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2179 hdr->sadb_msg_reserved != 0 ||
2180 (hdr->sadb_msg_type <= SADB_RESERVED ||
2181 hdr->sadb_msg_type > SADB_MAX)) {
2182 hdr = NULL;
2183 *errp = -EINVAL;
2184 } else if (hdr->sadb_msg_len != (skb->len /
2185 sizeof(uint64_t)) ||
2186 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2187 sizeof(uint64_t))) {
2188 hdr = NULL;
2189 *errp = -EMSGSIZE;
2190 } else {
2191 *errp = 0;
2194 return hdr;
2197 static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2199 return t->aalgos & (1 << d->desc.sadb_alg_id);
2202 static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2204 return t->ealgos & (1 << d->desc.sadb_alg_id);
2207 static int count_ah_combs(struct xfrm_tmpl *t)
2209 int i, sz = 0;
2211 for (i = 0; ; i++) {
2212 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2213 if (!aalg)
2214 break;
2215 if (aalg_tmpl_set(t, aalg) && aalg->available)
2216 sz += sizeof(struct sadb_comb);
2218 return sz + sizeof(struct sadb_prop);
2221 static int count_esp_combs(struct xfrm_tmpl *t)
2223 int i, k, sz = 0;
2225 for (i = 0; ; i++) {
2226 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2227 if (!ealg)
2228 break;
2230 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2231 continue;
2233 for (k = 1; ; k++) {
2234 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2235 if (!aalg)
2236 break;
2238 if (aalg_tmpl_set(t, aalg) && aalg->available)
2239 sz += sizeof(struct sadb_comb);
2242 return sz + sizeof(struct sadb_prop);
2245 static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2247 struct sadb_prop *p;
2248 int i;
2250 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2251 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2252 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2253 p->sadb_prop_replay = 32;
2254 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2256 for (i = 0; ; i++) {
2257 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2258 if (!aalg)
2259 break;
2261 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2262 struct sadb_comb *c;
2263 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2264 memset(c, 0, sizeof(*c));
2265 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2266 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2267 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2268 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2269 c->sadb_comb_hard_addtime = 24*60*60;
2270 c->sadb_comb_soft_addtime = 20*60*60;
2271 c->sadb_comb_hard_usetime = 8*60*60;
2272 c->sadb_comb_soft_usetime = 7*60*60;
2277 static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2279 struct sadb_prop *p;
2280 int i, k;
2282 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2283 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2284 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2285 p->sadb_prop_replay = 32;
2286 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2288 for (i=0; ; i++) {
2289 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2290 if (!ealg)
2291 break;
2293 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2294 continue;
2296 for (k = 1; ; k++) {
2297 struct sadb_comb *c;
2298 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2299 if (!aalg)
2300 break;
2301 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2302 continue;
2303 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2304 memset(c, 0, sizeof(*c));
2305 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2306 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2307 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2308 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2309 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2310 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2311 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2312 c->sadb_comb_hard_addtime = 24*60*60;
2313 c->sadb_comb_soft_addtime = 20*60*60;
2314 c->sadb_comb_hard_usetime = 8*60*60;
2315 c->sadb_comb_soft_usetime = 7*60*60;
2320 static int pfkey_send_notify(struct xfrm_state *x, int hard)
2322 struct sk_buff *out_skb;
2323 struct sadb_msg *out_hdr;
2324 int hsc = (hard ? 2 : 1);
2326 out_skb = pfkey_xfrm_state2msg(x, 0, hsc);
2327 if (IS_ERR(out_skb))
2328 return PTR_ERR(out_skb);
2330 out_hdr = (struct sadb_msg *) out_skb->data;
2331 out_hdr->sadb_msg_version = PF_KEY_V2;
2332 out_hdr->sadb_msg_type = SADB_EXPIRE;
2333 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2334 out_hdr->sadb_msg_errno = 0;
2335 out_hdr->sadb_msg_reserved = 0;
2336 out_hdr->sadb_msg_seq = 0;
2337 out_hdr->sadb_msg_pid = 0;
2339 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2340 return 0;
2343 static u32 get_acqseq(void)
2345 u32 res;
2346 static u32 acqseq;
2347 static DEFINE_SPINLOCK(acqseq_lock);
2349 spin_lock_bh(&acqseq_lock);
2350 res = (++acqseq ? : ++acqseq);
2351 spin_unlock_bh(&acqseq_lock);
2352 return res;
2355 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2357 struct sk_buff *skb;
2358 struct sadb_msg *hdr;
2359 struct sadb_address *addr;
2360 struct sadb_x_policy *pol;
2361 struct sockaddr_in *sin;
2362 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2363 struct sockaddr_in6 *sin6;
2364 #endif
2365 int sockaddr_size;
2366 int size;
2368 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2369 if (!sockaddr_size)
2370 return -EINVAL;
2372 size = sizeof(struct sadb_msg) +
2373 (sizeof(struct sadb_address) * 2) +
2374 (sockaddr_size * 2) +
2375 sizeof(struct sadb_x_policy);
2377 if (x->id.proto == IPPROTO_AH)
2378 size += count_ah_combs(t);
2379 else if (x->id.proto == IPPROTO_ESP)
2380 size += count_esp_combs(t);
2382 skb = alloc_skb(size + 16, GFP_ATOMIC);
2383 if (skb == NULL)
2384 return -ENOMEM;
2386 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2387 hdr->sadb_msg_version = PF_KEY_V2;
2388 hdr->sadb_msg_type = SADB_ACQUIRE;
2389 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2390 hdr->sadb_msg_len = size / sizeof(uint64_t);
2391 hdr->sadb_msg_errno = 0;
2392 hdr->sadb_msg_reserved = 0;
2393 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2394 hdr->sadb_msg_pid = 0;
2396 /* src address */
2397 addr = (struct sadb_address*) skb_put(skb,
2398 sizeof(struct sadb_address)+sockaddr_size);
2399 addr->sadb_address_len =
2400 (sizeof(struct sadb_address)+sockaddr_size)/
2401 sizeof(uint64_t);
2402 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2403 addr->sadb_address_proto = 0;
2404 addr->sadb_address_reserved = 0;
2405 if (x->props.family == AF_INET) {
2406 addr->sadb_address_prefixlen = 32;
2408 sin = (struct sockaddr_in *) (addr + 1);
2409 sin->sin_family = AF_INET;
2410 sin->sin_addr.s_addr = x->props.saddr.a4;
2411 sin->sin_port = 0;
2412 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2414 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2415 else if (x->props.family == AF_INET6) {
2416 addr->sadb_address_prefixlen = 128;
2418 sin6 = (struct sockaddr_in6 *) (addr + 1);
2419 sin6->sin6_family = AF_INET6;
2420 sin6->sin6_port = 0;
2421 sin6->sin6_flowinfo = 0;
2422 memcpy(&sin6->sin6_addr,
2423 x->props.saddr.a6, sizeof(struct in6_addr));
2424 sin6->sin6_scope_id = 0;
2426 #endif
2427 else
2428 BUG();
2430 /* dst address */
2431 addr = (struct sadb_address*) skb_put(skb,
2432 sizeof(struct sadb_address)+sockaddr_size);
2433 addr->sadb_address_len =
2434 (sizeof(struct sadb_address)+sockaddr_size)/
2435 sizeof(uint64_t);
2436 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2437 addr->sadb_address_proto = 0;
2438 addr->sadb_address_reserved = 0;
2439 if (x->props.family == AF_INET) {
2440 addr->sadb_address_prefixlen = 32;
2442 sin = (struct sockaddr_in *) (addr + 1);
2443 sin->sin_family = AF_INET;
2444 sin->sin_addr.s_addr = x->id.daddr.a4;
2445 sin->sin_port = 0;
2446 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2448 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2449 else if (x->props.family == AF_INET6) {
2450 addr->sadb_address_prefixlen = 128;
2452 sin6 = (struct sockaddr_in6 *) (addr + 1);
2453 sin6->sin6_family = AF_INET6;
2454 sin6->sin6_port = 0;
2455 sin6->sin6_flowinfo = 0;
2456 memcpy(&sin6->sin6_addr,
2457 x->id.daddr.a6, sizeof(struct in6_addr));
2458 sin6->sin6_scope_id = 0;
2460 #endif
2461 else
2462 BUG();
2464 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2465 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2466 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2467 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2468 pol->sadb_x_policy_dir = dir+1;
2469 pol->sadb_x_policy_id = xp->index;
2471 /* Set sadb_comb's. */
2472 if (x->id.proto == IPPROTO_AH)
2473 dump_ah_combs(skb, t);
2474 else if (x->id.proto == IPPROTO_ESP)
2475 dump_esp_combs(skb, t);
2477 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2480 static struct xfrm_policy *pfkey_compile_policy(u16 family, int opt,
2481 u8 *data, int len, int *dir)
2483 struct xfrm_policy *xp;
2484 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
2486 switch (family) {
2487 case AF_INET:
2488 if (opt != IP_IPSEC_POLICY) {
2489 *dir = -EOPNOTSUPP;
2490 return NULL;
2492 break;
2493 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2494 case AF_INET6:
2495 if (opt != IPV6_IPSEC_POLICY) {
2496 *dir = -EOPNOTSUPP;
2497 return NULL;
2499 break;
2500 #endif
2501 default:
2502 *dir = -EINVAL;
2503 return NULL;
2506 *dir = -EINVAL;
2508 if (len < sizeof(struct sadb_x_policy) ||
2509 pol->sadb_x_policy_len*8 > len ||
2510 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
2511 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
2512 return NULL;
2514 xp = xfrm_policy_alloc(GFP_ATOMIC);
2515 if (xp == NULL) {
2516 *dir = -ENOBUFS;
2517 return NULL;
2520 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2521 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2523 xp->lft.soft_byte_limit = XFRM_INF;
2524 xp->lft.hard_byte_limit = XFRM_INF;
2525 xp->lft.soft_packet_limit = XFRM_INF;
2526 xp->lft.hard_packet_limit = XFRM_INF;
2527 xp->family = family;
2529 xp->xfrm_nr = 0;
2530 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2531 (*dir = parse_ipsecrequests(xp, pol)) < 0)
2532 goto out;
2534 *dir = pol->sadb_x_policy_dir-1;
2535 return xp;
2537 out:
2538 kfree(xp);
2539 return NULL;
2542 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport)
2544 struct sk_buff *skb;
2545 struct sadb_msg *hdr;
2546 struct sadb_sa *sa;
2547 struct sadb_address *addr;
2548 struct sadb_x_nat_t_port *n_port;
2549 struct sockaddr_in *sin;
2550 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2551 struct sockaddr_in6 *sin6;
2552 #endif
2553 int sockaddr_size;
2554 int size;
2555 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
2556 struct xfrm_encap_tmpl *natt = NULL;
2558 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2559 if (!sockaddr_size)
2560 return -EINVAL;
2562 if (!satype)
2563 return -EINVAL;
2565 if (!x->encap)
2566 return -EINVAL;
2568 natt = x->encap;
2570 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
2572 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
2573 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
2576 size = sizeof(struct sadb_msg) +
2577 sizeof(struct sadb_sa) +
2578 (sizeof(struct sadb_address) * 2) +
2579 (sockaddr_size * 2) +
2580 (sizeof(struct sadb_x_nat_t_port) * 2);
2582 skb = alloc_skb(size + 16, GFP_ATOMIC);
2583 if (skb == NULL)
2584 return -ENOMEM;
2586 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2587 hdr->sadb_msg_version = PF_KEY_V2;
2588 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
2589 hdr->sadb_msg_satype = satype;
2590 hdr->sadb_msg_len = size / sizeof(uint64_t);
2591 hdr->sadb_msg_errno = 0;
2592 hdr->sadb_msg_reserved = 0;
2593 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2594 hdr->sadb_msg_pid = 0;
2596 /* SA */
2597 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
2598 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
2599 sa->sadb_sa_exttype = SADB_EXT_SA;
2600 sa->sadb_sa_spi = x->id.spi;
2601 sa->sadb_sa_replay = 0;
2602 sa->sadb_sa_state = 0;
2603 sa->sadb_sa_auth = 0;
2604 sa->sadb_sa_encrypt = 0;
2605 sa->sadb_sa_flags = 0;
2607 /* ADDRESS_SRC (old addr) */
2608 addr = (struct sadb_address*)
2609 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
2610 addr->sadb_address_len =
2611 (sizeof(struct sadb_address)+sockaddr_size)/
2612 sizeof(uint64_t);
2613 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2614 addr->sadb_address_proto = 0;
2615 addr->sadb_address_reserved = 0;
2616 if (x->props.family == AF_INET) {
2617 addr->sadb_address_prefixlen = 32;
2619 sin = (struct sockaddr_in *) (addr + 1);
2620 sin->sin_family = AF_INET;
2621 sin->sin_addr.s_addr = x->props.saddr.a4;
2622 sin->sin_port = 0;
2623 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2625 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2626 else if (x->props.family == AF_INET6) {
2627 addr->sadb_address_prefixlen = 128;
2629 sin6 = (struct sockaddr_in6 *) (addr + 1);
2630 sin6->sin6_family = AF_INET6;
2631 sin6->sin6_port = 0;
2632 sin6->sin6_flowinfo = 0;
2633 memcpy(&sin6->sin6_addr,
2634 x->props.saddr.a6, sizeof(struct in6_addr));
2635 sin6->sin6_scope_id = 0;
2637 #endif
2638 else
2639 BUG();
2641 /* NAT_T_SPORT (old port) */
2642 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
2643 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
2644 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
2645 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
2646 n_port->sadb_x_nat_t_port_reserved = 0;
2648 /* ADDRESS_DST (new addr) */
2649 addr = (struct sadb_address*)
2650 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
2651 addr->sadb_address_len =
2652 (sizeof(struct sadb_address)+sockaddr_size)/
2653 sizeof(uint64_t);
2654 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2655 addr->sadb_address_proto = 0;
2656 addr->sadb_address_reserved = 0;
2657 if (x->props.family == AF_INET) {
2658 addr->sadb_address_prefixlen = 32;
2660 sin = (struct sockaddr_in *) (addr + 1);
2661 sin->sin_family = AF_INET;
2662 sin->sin_addr.s_addr = ipaddr->a4;
2663 sin->sin_port = 0;
2664 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2666 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2667 else if (x->props.family == AF_INET6) {
2668 addr->sadb_address_prefixlen = 128;
2670 sin6 = (struct sockaddr_in6 *) (addr + 1);
2671 sin6->sin6_family = AF_INET6;
2672 sin6->sin6_port = 0;
2673 sin6->sin6_flowinfo = 0;
2674 memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
2675 sin6->sin6_scope_id = 0;
2677 #endif
2678 else
2679 BUG();
2681 /* NAT_T_DPORT (new port) */
2682 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
2683 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
2684 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
2685 n_port->sadb_x_nat_t_port_port = sport;
2686 n_port->sadb_x_nat_t_port_reserved = 0;
2688 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2691 static int pfkey_sendmsg(struct kiocb *kiocb,
2692 struct socket *sock, struct msghdr *msg, size_t len)
2694 struct sock *sk = sock->sk;
2695 struct sk_buff *skb = NULL;
2696 struct sadb_msg *hdr = NULL;
2697 int err;
2699 err = -EOPNOTSUPP;
2700 if (msg->msg_flags & MSG_OOB)
2701 goto out;
2703 err = -EMSGSIZE;
2704 if ((unsigned)len > sk->sk_sndbuf - 32)
2705 goto out;
2707 err = -ENOBUFS;
2708 skb = alloc_skb(len, GFP_KERNEL);
2709 if (skb == NULL)
2710 goto out;
2712 err = -EFAULT;
2713 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
2714 goto out;
2716 hdr = pfkey_get_base_msg(skb, &err);
2717 if (!hdr)
2718 goto out;
2720 down(&xfrm_cfg_sem);
2721 err = pfkey_process(sk, skb, hdr);
2722 up(&xfrm_cfg_sem);
2724 out:
2725 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
2726 err = 0;
2727 if (skb)
2728 kfree_skb(skb);
2730 return err ? : len;
2733 static int pfkey_recvmsg(struct kiocb *kiocb,
2734 struct socket *sock, struct msghdr *msg, size_t len,
2735 int flags)
2737 struct sock *sk = sock->sk;
2738 struct sk_buff *skb;
2739 int copied, err;
2741 err = -EINVAL;
2742 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
2743 goto out;
2745 msg->msg_namelen = 0;
2746 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
2747 if (skb == NULL)
2748 goto out;
2750 copied = skb->len;
2751 if (copied > len) {
2752 msg->msg_flags |= MSG_TRUNC;
2753 copied = len;
2756 skb->h.raw = skb->data;
2757 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2758 if (err)
2759 goto out_free;
2761 sock_recv_timestamp(msg, sk, skb);
2763 err = (flags & MSG_TRUNC) ? skb->len : copied;
2765 out_free:
2766 skb_free_datagram(sk, skb);
2767 out:
2768 return err;
2771 static struct proto_ops pfkey_ops = {
2772 .family = PF_KEY,
2773 .owner = THIS_MODULE,
2774 /* Operations that make no sense on pfkey sockets. */
2775 .bind = sock_no_bind,
2776 .connect = sock_no_connect,
2777 .socketpair = sock_no_socketpair,
2778 .accept = sock_no_accept,
2779 .getname = sock_no_getname,
2780 .ioctl = sock_no_ioctl,
2781 .listen = sock_no_listen,
2782 .shutdown = sock_no_shutdown,
2783 .setsockopt = sock_no_setsockopt,
2784 .getsockopt = sock_no_getsockopt,
2785 .mmap = sock_no_mmap,
2786 .sendpage = sock_no_sendpage,
2788 /* Now the operations that really occur. */
2789 .release = pfkey_release,
2790 .poll = datagram_poll,
2791 .sendmsg = pfkey_sendmsg,
2792 .recvmsg = pfkey_recvmsg,
2795 static struct net_proto_family pfkey_family_ops = {
2796 .family = PF_KEY,
2797 .create = pfkey_create,
2798 .owner = THIS_MODULE,
2801 #ifdef CONFIG_PROC_FS
2802 static int pfkey_read_proc(char *buffer, char **start, off_t offset,
2803 int length, int *eof, void *data)
2805 off_t pos = 0;
2806 off_t begin = 0;
2807 int len = 0;
2808 struct sock *s;
2809 struct hlist_node *node;
2811 len += sprintf(buffer,"sk RefCnt Rmem Wmem User Inode\n");
2813 read_lock(&pfkey_table_lock);
2815 sk_for_each(s, node, &pfkey_table) {
2816 len += sprintf(buffer+len,"%p %-6d %-6u %-6u %-6u %-6lu",
2818 atomic_read(&s->sk_refcnt),
2819 atomic_read(&s->sk_rmem_alloc),
2820 atomic_read(&s->sk_wmem_alloc),
2821 sock_i_uid(s),
2822 sock_i_ino(s)
2825 buffer[len++] = '\n';
2827 pos = begin + len;
2828 if (pos < offset) {
2829 len = 0;
2830 begin = pos;
2832 if(pos > offset + length)
2833 goto done;
2835 *eof = 1;
2837 done:
2838 read_unlock(&pfkey_table_lock);
2840 *start = buffer + (offset - begin);
2841 len -= (offset - begin);
2843 if (len > length)
2844 len = length;
2845 if (len < 0)
2846 len = 0;
2848 return len;
2850 #endif
2852 static struct xfrm_mgr pfkeyv2_mgr =
2854 .id = "pfkeyv2",
2855 .notify = pfkey_send_notify,
2856 .acquire = pfkey_send_acquire,
2857 .compile_policy = pfkey_compile_policy,
2858 .new_mapping = pfkey_send_new_mapping,
2861 static void __exit ipsec_pfkey_exit(void)
2863 xfrm_unregister_km(&pfkeyv2_mgr);
2864 remove_proc_entry("net/pfkey", NULL);
2865 sock_unregister(PF_KEY);
2866 proto_unregister(&key_proto);
2869 static int __init ipsec_pfkey_init(void)
2871 int err = proto_register(&key_proto, 0);
2873 if (err != 0)
2874 goto out;
2876 err = sock_register(&pfkey_family_ops);
2877 if (err != 0)
2878 goto out_unregister_key_proto;
2879 #ifdef CONFIG_PROC_FS
2880 err = -ENOMEM;
2881 if (create_proc_read_entry("net/pfkey", 0, NULL, pfkey_read_proc, NULL) == NULL)
2882 goto out_sock_unregister;
2883 #endif
2884 err = xfrm_register_km(&pfkeyv2_mgr);
2885 if (err != 0)
2886 goto out_remove_proc_entry;
2887 out:
2888 return err;
2889 out_remove_proc_entry:
2890 #ifdef CONFIG_PROC_FS
2891 remove_proc_entry("net/pfkey", NULL);
2892 out_sock_unregister:
2893 #endif
2894 sock_unregister(PF_KEY);
2895 out_unregister_key_proto:
2896 proto_unregister(&key_proto);
2897 goto out;
2900 module_init(ipsec_pfkey_init);
2901 module_exit(ipsec_pfkey_exit);
2902 MODULE_LICENSE("GPL");
2903 MODULE_ALIAS_NETPROTO(PF_KEY);