[NET]: Make /proc/net per network namespace
[linux-2.6/kmemtrace.git] / net / key / af_key.c
blob5b802bbb856e4f08b983284cad7f7e6dded1728c
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/capability.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/net_namespace.h>
30 #include <net/xfrm.h>
32 #include <net/sock.h>
34 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
35 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
38 /* List of all pfkey sockets. */
39 static HLIST_HEAD(pfkey_table);
40 static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait);
41 static DEFINE_RWLOCK(pfkey_table_lock);
42 static atomic_t pfkey_table_users = ATOMIC_INIT(0);
44 static atomic_t pfkey_socks_nr = ATOMIC_INIT(0);
46 struct pfkey_sock {
47 /* struct sock must be the first member of struct pfkey_sock */
48 struct sock sk;
49 int registered;
50 int promisc;
53 static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
55 return (struct pfkey_sock *)sk;
58 static void pfkey_sock_destruct(struct sock *sk)
60 skb_queue_purge(&sk->sk_receive_queue);
62 if (!sock_flag(sk, SOCK_DEAD)) {
63 printk("Attempt to release alive pfkey socket: %p\n", sk);
64 return;
67 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
68 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
70 atomic_dec(&pfkey_socks_nr);
73 static void pfkey_table_grab(void)
75 write_lock_bh(&pfkey_table_lock);
77 if (atomic_read(&pfkey_table_users)) {
78 DECLARE_WAITQUEUE(wait, current);
80 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
81 for(;;) {
82 set_current_state(TASK_UNINTERRUPTIBLE);
83 if (atomic_read(&pfkey_table_users) == 0)
84 break;
85 write_unlock_bh(&pfkey_table_lock);
86 schedule();
87 write_lock_bh(&pfkey_table_lock);
90 __set_current_state(TASK_RUNNING);
91 remove_wait_queue(&pfkey_table_wait, &wait);
95 static __inline__ void pfkey_table_ungrab(void)
97 write_unlock_bh(&pfkey_table_lock);
98 wake_up(&pfkey_table_wait);
101 static __inline__ void pfkey_lock_table(void)
103 /* read_lock() synchronizes us to pfkey_table_grab */
105 read_lock(&pfkey_table_lock);
106 atomic_inc(&pfkey_table_users);
107 read_unlock(&pfkey_table_lock);
110 static __inline__ void pfkey_unlock_table(void)
112 if (atomic_dec_and_test(&pfkey_table_users))
113 wake_up(&pfkey_table_wait);
117 static const struct proto_ops pfkey_ops;
119 static void pfkey_insert(struct sock *sk)
121 pfkey_table_grab();
122 sk_add_node(sk, &pfkey_table);
123 pfkey_table_ungrab();
126 static void pfkey_remove(struct sock *sk)
128 pfkey_table_grab();
129 sk_del_node_init(sk);
130 pfkey_table_ungrab();
133 static struct proto key_proto = {
134 .name = "KEY",
135 .owner = THIS_MODULE,
136 .obj_size = sizeof(struct pfkey_sock),
139 static int pfkey_create(struct socket *sock, int protocol)
141 struct sock *sk;
142 int err;
144 if (!capable(CAP_NET_ADMIN))
145 return -EPERM;
146 if (sock->type != SOCK_RAW)
147 return -ESOCKTNOSUPPORT;
148 if (protocol != PF_KEY_V2)
149 return -EPROTONOSUPPORT;
151 err = -ENOMEM;
152 sk = sk_alloc(PF_KEY, GFP_KERNEL, &key_proto, 1);
153 if (sk == NULL)
154 goto out;
156 sock->ops = &pfkey_ops;
157 sock_init_data(sock, sk);
159 sk->sk_family = PF_KEY;
160 sk->sk_destruct = pfkey_sock_destruct;
162 atomic_inc(&pfkey_socks_nr);
164 pfkey_insert(sk);
166 return 0;
167 out:
168 return err;
171 static int pfkey_release(struct socket *sock)
173 struct sock *sk = sock->sk;
175 if (!sk)
176 return 0;
178 pfkey_remove(sk);
180 sock_orphan(sk);
181 sock->sk = NULL;
182 skb_queue_purge(&sk->sk_write_queue);
183 sock_put(sk);
185 return 0;
188 static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
189 gfp_t allocation, struct sock *sk)
191 int err = -ENOBUFS;
193 sock_hold(sk);
194 if (*skb2 == NULL) {
195 if (atomic_read(&skb->users) != 1) {
196 *skb2 = skb_clone(skb, allocation);
197 } else {
198 *skb2 = skb;
199 atomic_inc(&skb->users);
202 if (*skb2 != NULL) {
203 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
204 skb_orphan(*skb2);
205 skb_set_owner_r(*skb2, sk);
206 skb_queue_tail(&sk->sk_receive_queue, *skb2);
207 sk->sk_data_ready(sk, (*skb2)->len);
208 *skb2 = NULL;
209 err = 0;
212 sock_put(sk);
213 return err;
216 /* Send SKB to all pfkey sockets matching selected criteria. */
217 #define BROADCAST_ALL 0
218 #define BROADCAST_ONE 1
219 #define BROADCAST_REGISTERED 2
220 #define BROADCAST_PROMISC_ONLY 4
221 static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
222 int broadcast_flags, struct sock *one_sk)
224 struct sock *sk;
225 struct hlist_node *node;
226 struct sk_buff *skb2 = NULL;
227 int err = -ESRCH;
229 /* XXX Do we need something like netlink_overrun? I think
230 * XXX PF_KEY socket apps will not mind current behavior.
232 if (!skb)
233 return -ENOMEM;
235 pfkey_lock_table();
236 sk_for_each(sk, node, &pfkey_table) {
237 struct pfkey_sock *pfk = pfkey_sk(sk);
238 int err2;
240 /* Yes, it means that if you are meant to receive this
241 * pfkey message you receive it twice as promiscuous
242 * socket.
244 if (pfk->promisc)
245 pfkey_broadcast_one(skb, &skb2, allocation, sk);
247 /* the exact target will be processed later */
248 if (sk == one_sk)
249 continue;
250 if (broadcast_flags != BROADCAST_ALL) {
251 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
252 continue;
253 if ((broadcast_flags & BROADCAST_REGISTERED) &&
254 !pfk->registered)
255 continue;
256 if (broadcast_flags & BROADCAST_ONE)
257 continue;
260 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
262 /* Error is cleare after succecful sending to at least one
263 * registered KM */
264 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
265 err = err2;
267 pfkey_unlock_table();
269 if (one_sk != NULL)
270 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
272 if (skb2)
273 kfree_skb(skb2);
274 kfree_skb(skb);
275 return err;
278 static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
280 *new = *orig;
283 static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
285 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
286 struct sadb_msg *hdr;
288 if (!skb)
289 return -ENOBUFS;
291 /* Woe be to the platform trying to support PFKEY yet
292 * having normal errnos outside the 1-255 range, inclusive.
294 err = -err;
295 if (err == ERESTARTSYS ||
296 err == ERESTARTNOHAND ||
297 err == ERESTARTNOINTR)
298 err = EINTR;
299 if (err >= 512)
300 err = EINVAL;
301 BUG_ON(err <= 0 || err >= 256);
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),
339 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
342 /* Verify sadb_address_{len,prefixlen} against sa_family. */
343 static int verify_address_len(void *p)
345 struct sadb_address *sp = p;
346 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
347 struct sockaddr_in *sin;
348 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
349 struct sockaddr_in6 *sin6;
350 #endif
351 int len;
353 switch (addr->sa_family) {
354 case AF_INET:
355 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), 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 = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
363 if (sp->sadb_address_len != len ||
364 sp->sadb_address_prefixlen > 128)
365 return -EINVAL;
366 break;
367 #endif
368 default:
369 /* It is user using kernel to keep track of security
370 * associations for another protocol, such as
371 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
372 * lengths.
374 * XXX Actually, association/policy database is not yet
375 * XXX able to cope with arbitrary sockaddr families.
376 * XXX When it can, remove this -EINVAL. -DaveM
378 return -EINVAL;
379 break;
382 return 0;
385 static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx)
387 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
388 sec_ctx->sadb_x_ctx_len,
389 sizeof(uint64_t));
392 static inline int verify_sec_ctx_len(void *p)
394 struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p;
395 int len;
397 if (sec_ctx->sadb_x_ctx_len > PAGE_SIZE)
398 return -EINVAL;
400 len = pfkey_sec_ctx_len(sec_ctx);
402 if (sec_ctx->sadb_x_sec_len != len)
403 return -EINVAL;
405 return 0;
408 static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx)
410 struct xfrm_user_sec_ctx *uctx = NULL;
411 int ctx_size = sec_ctx->sadb_x_ctx_len;
413 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
415 if (!uctx)
416 return NULL;
418 uctx->len = pfkey_sec_ctx_len(sec_ctx);
419 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
420 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
421 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
422 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
423 memcpy(uctx + 1, sec_ctx + 1,
424 uctx->ctx_len);
426 return uctx;
429 static int present_and_same_family(struct sadb_address *src,
430 struct sadb_address *dst)
432 struct sockaddr *s_addr, *d_addr;
434 if (!src || !dst)
435 return 0;
437 s_addr = (struct sockaddr *)(src + 1);
438 d_addr = (struct sockaddr *)(dst + 1);
439 if (s_addr->sa_family != d_addr->sa_family)
440 return 0;
441 if (s_addr->sa_family != AF_INET
442 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
443 && s_addr->sa_family != AF_INET6
444 #endif
446 return 0;
448 return 1;
451 static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
453 char *p = (char *) hdr;
454 int len = skb->len;
456 len -= sizeof(*hdr);
457 p += sizeof(*hdr);
458 while (len > 0) {
459 struct sadb_ext *ehdr = (struct sadb_ext *) p;
460 uint16_t ext_type;
461 int ext_len;
463 ext_len = ehdr->sadb_ext_len;
464 ext_len *= sizeof(uint64_t);
465 ext_type = ehdr->sadb_ext_type;
466 if (ext_len < sizeof(uint64_t) ||
467 ext_len > len ||
468 ext_type == SADB_EXT_RESERVED)
469 return -EINVAL;
471 if (ext_type <= SADB_EXT_MAX) {
472 int min = (int) sadb_ext_min_len[ext_type];
473 if (ext_len < min)
474 return -EINVAL;
475 if (ext_hdrs[ext_type-1] != NULL)
476 return -EINVAL;
477 if (ext_type == SADB_EXT_ADDRESS_SRC ||
478 ext_type == SADB_EXT_ADDRESS_DST ||
479 ext_type == SADB_EXT_ADDRESS_PROXY ||
480 ext_type == SADB_X_EXT_NAT_T_OA) {
481 if (verify_address_len(p))
482 return -EINVAL;
484 if (ext_type == SADB_X_EXT_SEC_CTX) {
485 if (verify_sec_ctx_len(p))
486 return -EINVAL;
488 ext_hdrs[ext_type-1] = p;
490 p += ext_len;
491 len -= ext_len;
494 return 0;
497 static uint16_t
498 pfkey_satype2proto(uint8_t satype)
500 switch (satype) {
501 case SADB_SATYPE_UNSPEC:
502 return IPSEC_PROTO_ANY;
503 case SADB_SATYPE_AH:
504 return IPPROTO_AH;
505 case SADB_SATYPE_ESP:
506 return IPPROTO_ESP;
507 case SADB_X_SATYPE_IPCOMP:
508 return IPPROTO_COMP;
509 break;
510 default:
511 return 0;
513 /* NOTREACHED */
516 static uint8_t
517 pfkey_proto2satype(uint16_t proto)
519 switch (proto) {
520 case IPPROTO_AH:
521 return SADB_SATYPE_AH;
522 case IPPROTO_ESP:
523 return SADB_SATYPE_ESP;
524 case IPPROTO_COMP:
525 return SADB_X_SATYPE_IPCOMP;
526 break;
527 default:
528 return 0;
530 /* NOTREACHED */
533 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
534 * say specifically 'just raw sockets' as we encode them as 255.
537 static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
539 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
542 static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
544 return (proto ? proto : IPSEC_PROTO_ANY);
547 static int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr,
548 xfrm_address_t *xaddr)
550 switch (((struct sockaddr*)(addr + 1))->sa_family) {
551 case AF_INET:
552 xaddr->a4 =
553 ((struct sockaddr_in *)(addr + 1))->sin_addr.s_addr;
554 return AF_INET;
555 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
556 case AF_INET6:
557 memcpy(xaddr->a6,
558 &((struct sockaddr_in6 *)(addr + 1))->sin6_addr,
559 sizeof(struct in6_addr));
560 return AF_INET6;
561 #endif
562 default:
563 return 0;
565 /* NOTREACHED */
568 static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
570 struct sadb_sa *sa;
571 struct sadb_address *addr;
572 uint16_t proto;
573 unsigned short family;
574 xfrm_address_t *xaddr;
576 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
577 if (sa == NULL)
578 return NULL;
580 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
581 if (proto == 0)
582 return NULL;
584 /* sadb_address_len should be checked by caller */
585 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
586 if (addr == NULL)
587 return NULL;
589 family = ((struct sockaddr *)(addr + 1))->sa_family;
590 switch (family) {
591 case AF_INET:
592 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
593 break;
594 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
595 case AF_INET6:
596 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
597 break;
598 #endif
599 default:
600 xaddr = NULL;
603 if (!xaddr)
604 return NULL;
606 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
609 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
610 static int
611 pfkey_sockaddr_size(sa_family_t family)
613 switch (family) {
614 case AF_INET:
615 return PFKEY_ALIGN8(sizeof(struct sockaddr_in));
616 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
617 case AF_INET6:
618 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6));
619 #endif
620 default:
621 return 0;
623 /* NOTREACHED */
626 static inline int pfkey_mode_from_xfrm(int mode)
628 switch(mode) {
629 case XFRM_MODE_TRANSPORT:
630 return IPSEC_MODE_TRANSPORT;
631 case XFRM_MODE_TUNNEL:
632 return IPSEC_MODE_TUNNEL;
633 case XFRM_MODE_BEET:
634 return IPSEC_MODE_BEET;
635 default:
636 return -1;
640 static inline int pfkey_mode_to_xfrm(int mode)
642 switch(mode) {
643 case IPSEC_MODE_ANY: /*XXX*/
644 case IPSEC_MODE_TRANSPORT:
645 return XFRM_MODE_TRANSPORT;
646 case IPSEC_MODE_TUNNEL:
647 return XFRM_MODE_TUNNEL;
648 case IPSEC_MODE_BEET:
649 return XFRM_MODE_BEET;
650 default:
651 return -1;
655 static struct sk_buff * pfkey_xfrm_state2msg(struct xfrm_state *x, int add_keys, int hsc)
657 struct sk_buff *skb;
658 struct sadb_msg *hdr;
659 struct sadb_sa *sa;
660 struct sadb_lifetime *lifetime;
661 struct sadb_address *addr;
662 struct sadb_key *key;
663 struct sadb_x_sa2 *sa2;
664 struct sockaddr_in *sin;
665 struct sadb_x_sec_ctx *sec_ctx;
666 struct xfrm_sec_ctx *xfrm_ctx;
667 int ctx_size = 0;
668 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
669 struct sockaddr_in6 *sin6;
670 #endif
671 int size;
672 int auth_key_size = 0;
673 int encrypt_key_size = 0;
674 int sockaddr_size;
675 struct xfrm_encap_tmpl *natt = NULL;
676 int mode;
678 /* address family check */
679 sockaddr_size = pfkey_sockaddr_size(x->props.family);
680 if (!sockaddr_size)
681 return ERR_PTR(-EINVAL);
683 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
684 key(AE), (identity(SD),) (sensitivity)> */
685 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
686 sizeof(struct sadb_lifetime) +
687 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
688 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
689 sizeof(struct sadb_address)*2 +
690 sockaddr_size*2 +
691 sizeof(struct sadb_x_sa2);
693 if ((xfrm_ctx = x->security)) {
694 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
695 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
698 /* identity & sensitivity */
700 if ((x->props.family == AF_INET &&
701 x->sel.saddr.a4 != x->props.saddr.a4)
702 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
703 || (x->props.family == AF_INET6 &&
704 memcmp (x->sel.saddr.a6, x->props.saddr.a6, sizeof (struct in6_addr)))
705 #endif
707 size += sizeof(struct sadb_address) + sockaddr_size;
709 if (add_keys) {
710 if (x->aalg && x->aalg->alg_key_len) {
711 auth_key_size =
712 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
713 size += sizeof(struct sadb_key) + auth_key_size;
715 if (x->ealg && x->ealg->alg_key_len) {
716 encrypt_key_size =
717 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
718 size += sizeof(struct sadb_key) + encrypt_key_size;
721 if (x->encap)
722 natt = x->encap;
724 if (natt && natt->encap_type) {
725 size += sizeof(struct sadb_x_nat_t_type);
726 size += sizeof(struct sadb_x_nat_t_port);
727 size += sizeof(struct sadb_x_nat_t_port);
730 skb = alloc_skb(size + 16, GFP_ATOMIC);
731 if (skb == NULL)
732 return ERR_PTR(-ENOBUFS);
734 /* call should fill header later */
735 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
736 memset(hdr, 0, size); /* XXX do we need this ? */
737 hdr->sadb_msg_len = size / sizeof(uint64_t);
739 /* sa */
740 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
741 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
742 sa->sadb_sa_exttype = SADB_EXT_SA;
743 sa->sadb_sa_spi = x->id.spi;
744 sa->sadb_sa_replay = x->props.replay_window;
745 switch (x->km.state) {
746 case XFRM_STATE_VALID:
747 sa->sadb_sa_state = x->km.dying ?
748 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
749 break;
750 case XFRM_STATE_ACQ:
751 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
752 break;
753 default:
754 sa->sadb_sa_state = SADB_SASTATE_DEAD;
755 break;
757 sa->sadb_sa_auth = 0;
758 if (x->aalg) {
759 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
760 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
762 sa->sadb_sa_encrypt = 0;
763 BUG_ON(x->ealg && x->calg);
764 if (x->ealg) {
765 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
766 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
768 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
769 if (x->calg) {
770 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
771 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
774 sa->sadb_sa_flags = 0;
775 if (x->props.flags & XFRM_STATE_NOECN)
776 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
777 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
778 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
779 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
780 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
782 /* hard time */
783 if (hsc & 2) {
784 lifetime = (struct sadb_lifetime *) skb_put(skb,
785 sizeof(struct sadb_lifetime));
786 lifetime->sadb_lifetime_len =
787 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
788 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
789 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
790 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
791 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
792 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
794 /* soft time */
795 if (hsc & 1) {
796 lifetime = (struct sadb_lifetime *) skb_put(skb,
797 sizeof(struct sadb_lifetime));
798 lifetime->sadb_lifetime_len =
799 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
800 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
801 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
802 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
803 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
804 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
806 /* current time */
807 lifetime = (struct sadb_lifetime *) skb_put(skb,
808 sizeof(struct sadb_lifetime));
809 lifetime->sadb_lifetime_len =
810 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
811 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
812 lifetime->sadb_lifetime_allocations = x->curlft.packets;
813 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
814 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
815 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
816 /* src address */
817 addr = (struct sadb_address*) skb_put(skb,
818 sizeof(struct sadb_address)+sockaddr_size);
819 addr->sadb_address_len =
820 (sizeof(struct sadb_address)+sockaddr_size)/
821 sizeof(uint64_t);
822 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
823 /* "if the ports are non-zero, then the sadb_address_proto field,
824 normally zero, MUST be filled in with the transport
825 protocol's number." - RFC2367 */
826 addr->sadb_address_proto = 0;
827 addr->sadb_address_reserved = 0;
828 if (x->props.family == AF_INET) {
829 addr->sadb_address_prefixlen = 32;
831 sin = (struct sockaddr_in *) (addr + 1);
832 sin->sin_family = AF_INET;
833 sin->sin_addr.s_addr = x->props.saddr.a4;
834 sin->sin_port = 0;
835 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
837 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
838 else if (x->props.family == AF_INET6) {
839 addr->sadb_address_prefixlen = 128;
841 sin6 = (struct sockaddr_in6 *) (addr + 1);
842 sin6->sin6_family = AF_INET6;
843 sin6->sin6_port = 0;
844 sin6->sin6_flowinfo = 0;
845 memcpy(&sin6->sin6_addr, x->props.saddr.a6,
846 sizeof(struct in6_addr));
847 sin6->sin6_scope_id = 0;
849 #endif
850 else
851 BUG();
853 /* dst address */
854 addr = (struct sadb_address*) skb_put(skb,
855 sizeof(struct sadb_address)+sockaddr_size);
856 addr->sadb_address_len =
857 (sizeof(struct sadb_address)+sockaddr_size)/
858 sizeof(uint64_t);
859 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
860 addr->sadb_address_proto = 0;
861 addr->sadb_address_prefixlen = 32; /* XXX */
862 addr->sadb_address_reserved = 0;
863 if (x->props.family == AF_INET) {
864 sin = (struct sockaddr_in *) (addr + 1);
865 sin->sin_family = AF_INET;
866 sin->sin_addr.s_addr = x->id.daddr.a4;
867 sin->sin_port = 0;
868 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
870 if (x->sel.saddr.a4 != x->props.saddr.a4) {
871 addr = (struct sadb_address*) skb_put(skb,
872 sizeof(struct sadb_address)+sockaddr_size);
873 addr->sadb_address_len =
874 (sizeof(struct sadb_address)+sockaddr_size)/
875 sizeof(uint64_t);
876 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
877 addr->sadb_address_proto =
878 pfkey_proto_from_xfrm(x->sel.proto);
879 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
880 addr->sadb_address_reserved = 0;
882 sin = (struct sockaddr_in *) (addr + 1);
883 sin->sin_family = AF_INET;
884 sin->sin_addr.s_addr = x->sel.saddr.a4;
885 sin->sin_port = x->sel.sport;
886 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
889 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
890 else if (x->props.family == AF_INET6) {
891 addr->sadb_address_prefixlen = 128;
893 sin6 = (struct sockaddr_in6 *) (addr + 1);
894 sin6->sin6_family = AF_INET6;
895 sin6->sin6_port = 0;
896 sin6->sin6_flowinfo = 0;
897 memcpy(&sin6->sin6_addr, x->id.daddr.a6, sizeof(struct in6_addr));
898 sin6->sin6_scope_id = 0;
900 if (memcmp (x->sel.saddr.a6, x->props.saddr.a6,
901 sizeof(struct in6_addr))) {
902 addr = (struct sadb_address *) skb_put(skb,
903 sizeof(struct sadb_address)+sockaddr_size);
904 addr->sadb_address_len =
905 (sizeof(struct sadb_address)+sockaddr_size)/
906 sizeof(uint64_t);
907 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
908 addr->sadb_address_proto =
909 pfkey_proto_from_xfrm(x->sel.proto);
910 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
911 addr->sadb_address_reserved = 0;
913 sin6 = (struct sockaddr_in6 *) (addr + 1);
914 sin6->sin6_family = AF_INET6;
915 sin6->sin6_port = x->sel.sport;
916 sin6->sin6_flowinfo = 0;
917 memcpy(&sin6->sin6_addr, x->sel.saddr.a6,
918 sizeof(struct in6_addr));
919 sin6->sin6_scope_id = 0;
922 #endif
923 else
924 BUG();
926 /* auth key */
927 if (add_keys && auth_key_size) {
928 key = (struct sadb_key *) skb_put(skb,
929 sizeof(struct sadb_key)+auth_key_size);
930 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
931 sizeof(uint64_t);
932 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
933 key->sadb_key_bits = x->aalg->alg_key_len;
934 key->sadb_key_reserved = 0;
935 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
937 /* encrypt key */
938 if (add_keys && encrypt_key_size) {
939 key = (struct sadb_key *) skb_put(skb,
940 sizeof(struct sadb_key)+encrypt_key_size);
941 key->sadb_key_len = (sizeof(struct sadb_key) +
942 encrypt_key_size) / sizeof(uint64_t);
943 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
944 key->sadb_key_bits = x->ealg->alg_key_len;
945 key->sadb_key_reserved = 0;
946 memcpy(key + 1, x->ealg->alg_key,
947 (x->ealg->alg_key_len+7)/8);
950 /* sa */
951 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
952 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
953 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
954 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
955 kfree_skb(skb);
956 return ERR_PTR(-EINVAL);
958 sa2->sadb_x_sa2_mode = mode;
959 sa2->sadb_x_sa2_reserved1 = 0;
960 sa2->sadb_x_sa2_reserved2 = 0;
961 sa2->sadb_x_sa2_sequence = 0;
962 sa2->sadb_x_sa2_reqid = x->props.reqid;
964 if (natt && natt->encap_type) {
965 struct sadb_x_nat_t_type *n_type;
966 struct sadb_x_nat_t_port *n_port;
968 /* type */
969 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
970 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
971 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
972 n_type->sadb_x_nat_t_type_type = natt->encap_type;
973 n_type->sadb_x_nat_t_type_reserved[0] = 0;
974 n_type->sadb_x_nat_t_type_reserved[1] = 0;
975 n_type->sadb_x_nat_t_type_reserved[2] = 0;
977 /* source port */
978 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
979 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
980 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
981 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
982 n_port->sadb_x_nat_t_port_reserved = 0;
984 /* dest port */
985 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
986 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
987 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
988 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
989 n_port->sadb_x_nat_t_port_reserved = 0;
992 /* security context */
993 if (xfrm_ctx) {
994 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
995 sizeof(struct sadb_x_sec_ctx) + ctx_size);
996 sec_ctx->sadb_x_sec_len =
997 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
998 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
999 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1000 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1001 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1002 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1003 xfrm_ctx->ctx_len);
1006 return skb;
1009 static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
1010 void **ext_hdrs)
1012 struct xfrm_state *x;
1013 struct sadb_lifetime *lifetime;
1014 struct sadb_sa *sa;
1015 struct sadb_key *key;
1016 struct sadb_x_sec_ctx *sec_ctx;
1017 uint16_t proto;
1018 int err;
1021 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
1022 if (!sa ||
1023 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1024 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1025 return ERR_PTR(-EINVAL);
1026 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1027 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1028 return ERR_PTR(-EINVAL);
1029 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1030 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1031 return ERR_PTR(-EINVAL);
1032 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1033 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1034 return ERR_PTR(-EINVAL);
1036 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1037 if (proto == 0)
1038 return ERR_PTR(-EINVAL);
1040 /* default error is no buffer space */
1041 err = -ENOBUFS;
1043 /* RFC2367:
1045 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1046 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1047 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1048 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1049 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1050 not true.
1052 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1053 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1055 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1056 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1057 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1058 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1059 return ERR_PTR(-EINVAL);
1060 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1061 if (key != NULL &&
1062 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1063 ((key->sadb_key_bits+7) / 8 == 0 ||
1064 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1065 return ERR_PTR(-EINVAL);
1066 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1067 if (key != NULL &&
1068 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1069 ((key->sadb_key_bits+7) / 8 == 0 ||
1070 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1071 return ERR_PTR(-EINVAL);
1073 x = xfrm_state_alloc();
1074 if (x == NULL)
1075 return ERR_PTR(-ENOBUFS);
1077 x->id.proto = proto;
1078 x->id.spi = sa->sadb_sa_spi;
1079 x->props.replay_window = sa->sadb_sa_replay;
1080 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1081 x->props.flags |= XFRM_STATE_NOECN;
1082 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1083 x->props.flags |= XFRM_STATE_DECAP_DSCP;
1084 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1085 x->props.flags |= XFRM_STATE_NOPMTUDISC;
1087 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
1088 if (lifetime != NULL) {
1089 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1090 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1091 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1092 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1094 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
1095 if (lifetime != NULL) {
1096 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1097 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1098 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1099 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1102 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
1103 if (sec_ctx != NULL) {
1104 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1106 if (!uctx)
1107 goto out;
1109 err = security_xfrm_state_alloc(x, uctx);
1110 kfree(uctx);
1112 if (err)
1113 goto out;
1116 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1117 if (sa->sadb_sa_auth) {
1118 int keysize = 0;
1119 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1120 if (!a) {
1121 err = -ENOSYS;
1122 goto out;
1124 if (key)
1125 keysize = (key->sadb_key_bits + 7) / 8;
1126 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1127 if (!x->aalg)
1128 goto out;
1129 strcpy(x->aalg->alg_name, a->name);
1130 x->aalg->alg_key_len = 0;
1131 if (key) {
1132 x->aalg->alg_key_len = key->sadb_key_bits;
1133 memcpy(x->aalg->alg_key, key+1, keysize);
1135 x->props.aalgo = sa->sadb_sa_auth;
1136 /* x->algo.flags = sa->sadb_sa_flags; */
1138 if (sa->sadb_sa_encrypt) {
1139 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1140 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1141 if (!a) {
1142 err = -ENOSYS;
1143 goto out;
1145 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1146 if (!x->calg)
1147 goto out;
1148 strcpy(x->calg->alg_name, a->name);
1149 x->props.calgo = sa->sadb_sa_encrypt;
1150 } else {
1151 int keysize = 0;
1152 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1153 if (!a) {
1154 err = -ENOSYS;
1155 goto out;
1157 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1158 if (key)
1159 keysize = (key->sadb_key_bits + 7) / 8;
1160 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1161 if (!x->ealg)
1162 goto out;
1163 strcpy(x->ealg->alg_name, a->name);
1164 x->ealg->alg_key_len = 0;
1165 if (key) {
1166 x->ealg->alg_key_len = key->sadb_key_bits;
1167 memcpy(x->ealg->alg_key, key+1, keysize);
1169 x->props.ealgo = sa->sadb_sa_encrypt;
1172 /* x->algo.flags = sa->sadb_sa_flags; */
1174 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1175 &x->props.saddr);
1176 if (!x->props.family) {
1177 err = -EAFNOSUPPORT;
1178 goto out;
1180 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1181 &x->id.daddr);
1183 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1184 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
1185 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1186 if (mode < 0) {
1187 err = -EINVAL;
1188 goto out;
1190 x->props.mode = mode;
1191 x->props.reqid = sa2->sadb_x_sa2_reqid;
1194 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1195 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1197 /* Nobody uses this, but we try. */
1198 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1199 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1202 if (!x->sel.family)
1203 x->sel.family = x->props.family;
1205 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1206 struct sadb_x_nat_t_type* n_type;
1207 struct xfrm_encap_tmpl *natt;
1209 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1210 if (!x->encap)
1211 goto out;
1213 natt = x->encap;
1214 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1215 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1217 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1218 struct sadb_x_nat_t_port* n_port =
1219 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1220 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1222 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1223 struct sadb_x_nat_t_port* n_port =
1224 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1225 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1229 err = xfrm_init_state(x);
1230 if (err)
1231 goto out;
1233 x->km.seq = hdr->sadb_msg_seq;
1234 return x;
1236 out:
1237 x->km.state = XFRM_STATE_DEAD;
1238 xfrm_state_put(x);
1239 return ERR_PTR(err);
1242 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1244 return -EOPNOTSUPP;
1247 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1249 struct sk_buff *resp_skb;
1250 struct sadb_x_sa2 *sa2;
1251 struct sadb_address *saddr, *daddr;
1252 struct sadb_msg *out_hdr;
1253 struct xfrm_state *x = NULL;
1254 int mode;
1255 u32 reqid;
1256 u8 proto;
1257 unsigned short family;
1258 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1260 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1261 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1262 return -EINVAL;
1264 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1265 if (proto == 0)
1266 return -EINVAL;
1268 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1269 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1270 if (mode < 0)
1271 return -EINVAL;
1272 reqid = sa2->sadb_x_sa2_reqid;
1273 } else {
1274 mode = 0;
1275 reqid = 0;
1278 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1279 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1281 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1282 switch (family) {
1283 case AF_INET:
1284 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1285 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1286 break;
1287 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1288 case AF_INET6:
1289 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1290 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1291 break;
1292 #endif
1295 if (hdr->sadb_msg_seq) {
1296 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1297 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1298 xfrm_state_put(x);
1299 x = NULL;
1303 if (!x)
1304 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1306 if (x == NULL)
1307 return -ENOENT;
1309 resp_skb = ERR_PTR(-ENOENT);
1311 spin_lock_bh(&x->lock);
1312 if (x->km.state != XFRM_STATE_DEAD) {
1313 struct sadb_spirange *range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1314 u32 min_spi, max_spi;
1316 if (range != NULL) {
1317 min_spi = range->sadb_spirange_min;
1318 max_spi = range->sadb_spirange_max;
1319 } else {
1320 min_spi = 0x100;
1321 max_spi = 0x0fffffff;
1323 xfrm_alloc_spi(x, htonl(min_spi), htonl(max_spi));
1324 if (x->id.spi)
1325 resp_skb = pfkey_xfrm_state2msg(x, 0, 3);
1327 spin_unlock_bh(&x->lock);
1329 if (IS_ERR(resp_skb)) {
1330 xfrm_state_put(x);
1331 return PTR_ERR(resp_skb);
1334 out_hdr = (struct sadb_msg *) resp_skb->data;
1335 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1336 out_hdr->sadb_msg_type = SADB_GETSPI;
1337 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1338 out_hdr->sadb_msg_errno = 0;
1339 out_hdr->sadb_msg_reserved = 0;
1340 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1341 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1343 xfrm_state_put(x);
1345 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1347 return 0;
1350 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1352 struct xfrm_state *x;
1354 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1355 return -EOPNOTSUPP;
1357 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1358 return 0;
1360 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1361 if (x == NULL)
1362 return 0;
1364 spin_lock_bh(&x->lock);
1365 if (x->km.state == XFRM_STATE_ACQ) {
1366 x->km.state = XFRM_STATE_ERROR;
1367 wake_up(&km_waitq);
1369 spin_unlock_bh(&x->lock);
1370 xfrm_state_put(x);
1371 return 0;
1374 static inline int event2poltype(int event)
1376 switch (event) {
1377 case XFRM_MSG_DELPOLICY:
1378 return SADB_X_SPDDELETE;
1379 case XFRM_MSG_NEWPOLICY:
1380 return SADB_X_SPDADD;
1381 case XFRM_MSG_UPDPOLICY:
1382 return SADB_X_SPDUPDATE;
1383 case XFRM_MSG_POLEXPIRE:
1384 // return SADB_X_SPDEXPIRE;
1385 default:
1386 printk("pfkey: Unknown policy event %d\n", event);
1387 break;
1390 return 0;
1393 static inline int event2keytype(int event)
1395 switch (event) {
1396 case XFRM_MSG_DELSA:
1397 return SADB_DELETE;
1398 case XFRM_MSG_NEWSA:
1399 return SADB_ADD;
1400 case XFRM_MSG_UPDSA:
1401 return SADB_UPDATE;
1402 case XFRM_MSG_EXPIRE:
1403 return SADB_EXPIRE;
1404 default:
1405 printk("pfkey: Unknown SA event %d\n", event);
1406 break;
1409 return 0;
1412 /* ADD/UPD/DEL */
1413 static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1415 struct sk_buff *skb;
1416 struct sadb_msg *hdr;
1417 int hsc = 3;
1419 if (c->event == XFRM_MSG_DELSA)
1420 hsc = 0;
1422 skb = pfkey_xfrm_state2msg(x, 0, hsc);
1424 if (IS_ERR(skb))
1425 return PTR_ERR(skb);
1427 hdr = (struct sadb_msg *) skb->data;
1428 hdr->sadb_msg_version = PF_KEY_V2;
1429 hdr->sadb_msg_type = event2keytype(c->event);
1430 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1431 hdr->sadb_msg_errno = 0;
1432 hdr->sadb_msg_reserved = 0;
1433 hdr->sadb_msg_seq = c->seq;
1434 hdr->sadb_msg_pid = c->pid;
1436 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1438 return 0;
1441 static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1443 struct xfrm_state *x;
1444 int err;
1445 struct km_event c;
1447 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1448 if (IS_ERR(x))
1449 return PTR_ERR(x);
1451 xfrm_state_hold(x);
1452 if (hdr->sadb_msg_type == SADB_ADD)
1453 err = xfrm_state_add(x);
1454 else
1455 err = xfrm_state_update(x);
1457 xfrm_audit_state_add(x, err ? 0 : 1,
1458 audit_get_loginuid(current->audit_context), 0);
1460 if (err < 0) {
1461 x->km.state = XFRM_STATE_DEAD;
1462 __xfrm_state_put(x);
1463 goto out;
1466 if (hdr->sadb_msg_type == SADB_ADD)
1467 c.event = XFRM_MSG_NEWSA;
1468 else
1469 c.event = XFRM_MSG_UPDSA;
1470 c.seq = hdr->sadb_msg_seq;
1471 c.pid = hdr->sadb_msg_pid;
1472 km_state_notify(x, &c);
1473 out:
1474 xfrm_state_put(x);
1475 return err;
1478 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1480 struct xfrm_state *x;
1481 struct km_event c;
1482 int err;
1484 if (!ext_hdrs[SADB_EXT_SA-1] ||
1485 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1486 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1487 return -EINVAL;
1489 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1490 if (x == NULL)
1491 return -ESRCH;
1493 if ((err = security_xfrm_state_delete(x)))
1494 goto out;
1496 if (xfrm_state_kern(x)) {
1497 err = -EPERM;
1498 goto out;
1501 err = xfrm_state_delete(x);
1503 if (err < 0)
1504 goto out;
1506 c.seq = hdr->sadb_msg_seq;
1507 c.pid = hdr->sadb_msg_pid;
1508 c.event = XFRM_MSG_DELSA;
1509 km_state_notify(x, &c);
1510 out:
1511 xfrm_audit_state_delete(x, err ? 0 : 1,
1512 audit_get_loginuid(current->audit_context), 0);
1513 xfrm_state_put(x);
1515 return err;
1518 static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1520 __u8 proto;
1521 struct sk_buff *out_skb;
1522 struct sadb_msg *out_hdr;
1523 struct xfrm_state *x;
1525 if (!ext_hdrs[SADB_EXT_SA-1] ||
1526 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1527 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1528 return -EINVAL;
1530 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1531 if (x == NULL)
1532 return -ESRCH;
1534 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1535 proto = x->id.proto;
1536 xfrm_state_put(x);
1537 if (IS_ERR(out_skb))
1538 return PTR_ERR(out_skb);
1540 out_hdr = (struct sadb_msg *) out_skb->data;
1541 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1542 out_hdr->sadb_msg_type = SADB_DUMP;
1543 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1544 out_hdr->sadb_msg_errno = 0;
1545 out_hdr->sadb_msg_reserved = 0;
1546 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1547 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1548 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1550 return 0;
1553 static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
1554 gfp_t allocation)
1556 struct sk_buff *skb;
1557 struct sadb_msg *hdr;
1558 int len, auth_len, enc_len, i;
1560 auth_len = xfrm_count_auth_supported();
1561 if (auth_len) {
1562 auth_len *= sizeof(struct sadb_alg);
1563 auth_len += sizeof(struct sadb_supported);
1566 enc_len = xfrm_count_enc_supported();
1567 if (enc_len) {
1568 enc_len *= sizeof(struct sadb_alg);
1569 enc_len += sizeof(struct sadb_supported);
1572 len = enc_len + auth_len + sizeof(struct sadb_msg);
1574 skb = alloc_skb(len + 16, allocation);
1575 if (!skb)
1576 goto out_put_algs;
1578 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1579 pfkey_hdr_dup(hdr, orig);
1580 hdr->sadb_msg_errno = 0;
1581 hdr->sadb_msg_len = len / sizeof(uint64_t);
1583 if (auth_len) {
1584 struct sadb_supported *sp;
1585 struct sadb_alg *ap;
1587 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1588 ap = (struct sadb_alg *) (sp + 1);
1590 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1591 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1593 for (i = 0; ; i++) {
1594 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1595 if (!aalg)
1596 break;
1597 if (aalg->available)
1598 *ap++ = aalg->desc;
1602 if (enc_len) {
1603 struct sadb_supported *sp;
1604 struct sadb_alg *ap;
1606 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1607 ap = (struct sadb_alg *) (sp + 1);
1609 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1610 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1612 for (i = 0; ; i++) {
1613 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1614 if (!ealg)
1615 break;
1616 if (ealg->available)
1617 *ap++ = ealg->desc;
1621 out_put_algs:
1622 return skb;
1625 static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1627 struct pfkey_sock *pfk = pfkey_sk(sk);
1628 struct sk_buff *supp_skb;
1630 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1631 return -EINVAL;
1633 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1634 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1635 return -EEXIST;
1636 pfk->registered |= (1<<hdr->sadb_msg_satype);
1639 xfrm_probe_algs();
1641 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1642 if (!supp_skb) {
1643 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1644 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1646 return -ENOBUFS;
1649 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1651 return 0;
1654 static int key_notify_sa_flush(struct km_event *c)
1656 struct sk_buff *skb;
1657 struct sadb_msg *hdr;
1659 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1660 if (!skb)
1661 return -ENOBUFS;
1662 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1663 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
1664 hdr->sadb_msg_type = SADB_FLUSH;
1665 hdr->sadb_msg_seq = c->seq;
1666 hdr->sadb_msg_pid = c->pid;
1667 hdr->sadb_msg_version = PF_KEY_V2;
1668 hdr->sadb_msg_errno = (uint8_t) 0;
1669 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1671 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1673 return 0;
1676 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1678 unsigned proto;
1679 struct km_event c;
1680 struct xfrm_audit audit_info;
1681 int err;
1683 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1684 if (proto == 0)
1685 return -EINVAL;
1687 audit_info.loginuid = audit_get_loginuid(current->audit_context);
1688 audit_info.secid = 0;
1689 err = xfrm_state_flush(proto, &audit_info);
1690 if (err)
1691 return err;
1692 c.data.proto = proto;
1693 c.seq = hdr->sadb_msg_seq;
1694 c.pid = hdr->sadb_msg_pid;
1695 c.event = XFRM_MSG_FLUSHSA;
1696 km_state_notify(NULL, &c);
1698 return 0;
1701 struct pfkey_dump_data
1703 struct sk_buff *skb;
1704 struct sadb_msg *hdr;
1705 struct sock *sk;
1708 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1710 struct pfkey_dump_data *data = ptr;
1711 struct sk_buff *out_skb;
1712 struct sadb_msg *out_hdr;
1714 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1715 if (IS_ERR(out_skb))
1716 return PTR_ERR(out_skb);
1718 out_hdr = (struct sadb_msg *) out_skb->data;
1719 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
1720 out_hdr->sadb_msg_type = SADB_DUMP;
1721 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1722 out_hdr->sadb_msg_errno = 0;
1723 out_hdr->sadb_msg_reserved = 0;
1724 out_hdr->sadb_msg_seq = count;
1725 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
1726 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
1727 return 0;
1730 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1732 u8 proto;
1733 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
1735 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1736 if (proto == 0)
1737 return -EINVAL;
1739 return xfrm_state_walk(proto, dump_sa, &data);
1742 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1744 struct pfkey_sock *pfk = pfkey_sk(sk);
1745 int satype = hdr->sadb_msg_satype;
1747 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1748 /* XXX we mangle packet... */
1749 hdr->sadb_msg_errno = 0;
1750 if (satype != 0 && satype != 1)
1751 return -EINVAL;
1752 pfk->promisc = satype;
1754 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1755 return 0;
1758 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1760 int i;
1761 u32 reqid = *(u32*)ptr;
1763 for (i=0; i<xp->xfrm_nr; i++) {
1764 if (xp->xfrm_vec[i].reqid == reqid)
1765 return -EEXIST;
1767 return 0;
1770 static u32 gen_reqid(void)
1772 u32 start;
1773 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1775 start = reqid;
1776 do {
1777 ++reqid;
1778 if (reqid == 0)
1779 reqid = IPSEC_MANUAL_REQID_MAX+1;
1780 if (xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, check_reqid,
1781 (void*)&reqid) != -EEXIST)
1782 return reqid;
1783 } while (reqid != start);
1784 return 0;
1787 static int
1788 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1790 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1791 struct sockaddr_in *sin;
1792 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1793 struct sockaddr_in6 *sin6;
1794 #endif
1795 int mode;
1797 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1798 return -ELOOP;
1800 if (rq->sadb_x_ipsecrequest_mode == 0)
1801 return -EINVAL;
1803 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1804 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1805 return -EINVAL;
1806 t->mode = mode;
1807 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1808 t->optional = 1;
1809 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1810 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1811 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1812 t->reqid = 0;
1813 if (!t->reqid && !(t->reqid = gen_reqid()))
1814 return -ENOBUFS;
1817 /* addresses present only in tunnel mode */
1818 if (t->mode == XFRM_MODE_TUNNEL) {
1819 struct sockaddr *sa;
1820 sa = (struct sockaddr *)(rq+1);
1821 switch(sa->sa_family) {
1822 case AF_INET:
1823 sin = (struct sockaddr_in*)sa;
1824 t->saddr.a4 = sin->sin_addr.s_addr;
1825 sin++;
1826 if (sin->sin_family != AF_INET)
1827 return -EINVAL;
1828 t->id.daddr.a4 = sin->sin_addr.s_addr;
1829 break;
1830 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1831 case AF_INET6:
1832 sin6 = (struct sockaddr_in6*)sa;
1833 memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1834 sin6++;
1835 if (sin6->sin6_family != AF_INET6)
1836 return -EINVAL;
1837 memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1838 break;
1839 #endif
1840 default:
1841 return -EINVAL;
1843 t->encap_family = sa->sa_family;
1844 } else
1845 t->encap_family = xp->family;
1847 /* No way to set this via kame pfkey */
1848 t->aalgos = t->ealgos = t->calgos = ~0;
1849 xp->xfrm_nr++;
1850 return 0;
1853 static int
1854 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1856 int err;
1857 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1858 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1860 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1861 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1862 return err;
1863 len -= rq->sadb_x_ipsecrequest_len;
1864 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1866 return 0;
1869 static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1871 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1873 if (xfrm_ctx) {
1874 int len = sizeof(struct sadb_x_sec_ctx);
1875 len += xfrm_ctx->ctx_len;
1876 return PFKEY_ALIGN8(len);
1878 return 0;
1881 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1883 struct xfrm_tmpl *t;
1884 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1885 int socklen = 0;
1886 int i;
1888 for (i=0; i<xp->xfrm_nr; i++) {
1889 t = xp->xfrm_vec + i;
1890 socklen += (t->encap_family == AF_INET ?
1891 sizeof(struct sockaddr_in) :
1892 sizeof(struct sockaddr_in6));
1895 return sizeof(struct sadb_msg) +
1896 (sizeof(struct sadb_lifetime) * 3) +
1897 (sizeof(struct sadb_address) * 2) +
1898 (sockaddr_size * 2) +
1899 sizeof(struct sadb_x_policy) +
1900 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1901 (socklen * 2) +
1902 pfkey_xfrm_policy2sec_ctx_size(xp);
1905 static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1907 struct sk_buff *skb;
1908 int size;
1910 size = pfkey_xfrm_policy2msg_size(xp);
1912 skb = alloc_skb(size + 16, GFP_ATOMIC);
1913 if (skb == NULL)
1914 return ERR_PTR(-ENOBUFS);
1916 return skb;
1919 static int pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1921 struct sadb_msg *hdr;
1922 struct sadb_address *addr;
1923 struct sadb_lifetime *lifetime;
1924 struct sadb_x_policy *pol;
1925 struct sockaddr_in *sin;
1926 struct sadb_x_sec_ctx *sec_ctx;
1927 struct xfrm_sec_ctx *xfrm_ctx;
1928 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1929 struct sockaddr_in6 *sin6;
1930 #endif
1931 int i;
1932 int size;
1933 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1934 int socklen = (xp->family == AF_INET ?
1935 sizeof(struct sockaddr_in) :
1936 sizeof(struct sockaddr_in6));
1938 size = pfkey_xfrm_policy2msg_size(xp);
1940 /* call should fill header later */
1941 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1942 memset(hdr, 0, size); /* XXX do we need this ? */
1944 /* src address */
1945 addr = (struct sadb_address*) skb_put(skb,
1946 sizeof(struct sadb_address)+sockaddr_size);
1947 addr->sadb_address_len =
1948 (sizeof(struct sadb_address)+sockaddr_size)/
1949 sizeof(uint64_t);
1950 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1951 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1952 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1953 addr->sadb_address_reserved = 0;
1954 /* src address */
1955 if (xp->family == AF_INET) {
1956 sin = (struct sockaddr_in *) (addr + 1);
1957 sin->sin_family = AF_INET;
1958 sin->sin_addr.s_addr = xp->selector.saddr.a4;
1959 sin->sin_port = xp->selector.sport;
1960 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1962 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1963 else if (xp->family == AF_INET6) {
1964 sin6 = (struct sockaddr_in6 *) (addr + 1);
1965 sin6->sin6_family = AF_INET6;
1966 sin6->sin6_port = xp->selector.sport;
1967 sin6->sin6_flowinfo = 0;
1968 memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
1969 sizeof(struct in6_addr));
1970 sin6->sin6_scope_id = 0;
1972 #endif
1973 else
1974 BUG();
1976 /* dst address */
1977 addr = (struct sadb_address*) skb_put(skb,
1978 sizeof(struct sadb_address)+sockaddr_size);
1979 addr->sadb_address_len =
1980 (sizeof(struct sadb_address)+sockaddr_size)/
1981 sizeof(uint64_t);
1982 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1983 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1984 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
1985 addr->sadb_address_reserved = 0;
1986 if (xp->family == AF_INET) {
1987 sin = (struct sockaddr_in *) (addr + 1);
1988 sin->sin_family = AF_INET;
1989 sin->sin_addr.s_addr = xp->selector.daddr.a4;
1990 sin->sin_port = xp->selector.dport;
1991 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1993 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1994 else if (xp->family == AF_INET6) {
1995 sin6 = (struct sockaddr_in6 *) (addr + 1);
1996 sin6->sin6_family = AF_INET6;
1997 sin6->sin6_port = xp->selector.dport;
1998 sin6->sin6_flowinfo = 0;
1999 memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
2000 sizeof(struct in6_addr));
2001 sin6->sin6_scope_id = 0;
2003 #endif
2004 else
2005 BUG();
2007 /* hard time */
2008 lifetime = (struct sadb_lifetime *) skb_put(skb,
2009 sizeof(struct sadb_lifetime));
2010 lifetime->sadb_lifetime_len =
2011 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2012 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2013 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2014 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2015 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2016 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2017 /* soft time */
2018 lifetime = (struct sadb_lifetime *) skb_put(skb,
2019 sizeof(struct sadb_lifetime));
2020 lifetime->sadb_lifetime_len =
2021 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2022 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2023 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2024 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2025 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2026 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2027 /* current time */
2028 lifetime = (struct sadb_lifetime *) skb_put(skb,
2029 sizeof(struct sadb_lifetime));
2030 lifetime->sadb_lifetime_len =
2031 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2032 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2033 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2034 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2035 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2036 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2038 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2039 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2040 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2041 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2042 if (xp->action == XFRM_POLICY_ALLOW) {
2043 if (xp->xfrm_nr)
2044 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2045 else
2046 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2048 pol->sadb_x_policy_dir = dir+1;
2049 pol->sadb_x_policy_id = xp->index;
2050 pol->sadb_x_policy_priority = xp->priority;
2052 for (i=0; i<xp->xfrm_nr; i++) {
2053 struct sadb_x_ipsecrequest *rq;
2054 struct xfrm_tmpl *t = xp->xfrm_vec + i;
2055 int req_size;
2056 int mode;
2058 req_size = sizeof(struct sadb_x_ipsecrequest);
2059 if (t->mode == XFRM_MODE_TUNNEL)
2060 req_size += ((t->encap_family == AF_INET ?
2061 sizeof(struct sockaddr_in) :
2062 sizeof(struct sockaddr_in6)) * 2);
2063 else
2064 size -= 2*socklen;
2065 rq = (void*)skb_put(skb, req_size);
2066 pol->sadb_x_policy_len += req_size/8;
2067 memset(rq, 0, sizeof(*rq));
2068 rq->sadb_x_ipsecrequest_len = req_size;
2069 rq->sadb_x_ipsecrequest_proto = t->id.proto;
2070 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2071 return -EINVAL;
2072 rq->sadb_x_ipsecrequest_mode = mode;
2073 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2074 if (t->reqid)
2075 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2076 if (t->optional)
2077 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2078 rq->sadb_x_ipsecrequest_reqid = t->reqid;
2079 if (t->mode == XFRM_MODE_TUNNEL) {
2080 switch (t->encap_family) {
2081 case AF_INET:
2082 sin = (void*)(rq+1);
2083 sin->sin_family = AF_INET;
2084 sin->sin_addr.s_addr = t->saddr.a4;
2085 sin->sin_port = 0;
2086 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2087 sin++;
2088 sin->sin_family = AF_INET;
2089 sin->sin_addr.s_addr = t->id.daddr.a4;
2090 sin->sin_port = 0;
2091 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2092 break;
2093 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2094 case AF_INET6:
2095 sin6 = (void*)(rq+1);
2096 sin6->sin6_family = AF_INET6;
2097 sin6->sin6_port = 0;
2098 sin6->sin6_flowinfo = 0;
2099 memcpy(&sin6->sin6_addr, t->saddr.a6,
2100 sizeof(struct in6_addr));
2101 sin6->sin6_scope_id = 0;
2103 sin6++;
2104 sin6->sin6_family = AF_INET6;
2105 sin6->sin6_port = 0;
2106 sin6->sin6_flowinfo = 0;
2107 memcpy(&sin6->sin6_addr, t->id.daddr.a6,
2108 sizeof(struct in6_addr));
2109 sin6->sin6_scope_id = 0;
2110 break;
2111 #endif
2112 default:
2113 break;
2118 /* security context */
2119 if ((xfrm_ctx = xp->security)) {
2120 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2122 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2123 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2124 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2125 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2126 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2127 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2128 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2129 xfrm_ctx->ctx_len);
2132 hdr->sadb_msg_len = size / sizeof(uint64_t);
2133 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
2135 return 0;
2138 static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2140 struct sk_buff *out_skb;
2141 struct sadb_msg *out_hdr;
2142 int err;
2144 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2145 if (IS_ERR(out_skb)) {
2146 err = PTR_ERR(out_skb);
2147 goto out;
2149 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2150 if (err < 0)
2151 return err;
2153 out_hdr = (struct sadb_msg *) out_skb->data;
2154 out_hdr->sadb_msg_version = PF_KEY_V2;
2156 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
2157 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2158 else
2159 out_hdr->sadb_msg_type = event2poltype(c->event);
2160 out_hdr->sadb_msg_errno = 0;
2161 out_hdr->sadb_msg_seq = c->seq;
2162 out_hdr->sadb_msg_pid = c->pid;
2163 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
2164 out:
2165 return 0;
2169 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2171 int err = 0;
2172 struct sadb_lifetime *lifetime;
2173 struct sadb_address *sa;
2174 struct sadb_x_policy *pol;
2175 struct xfrm_policy *xp;
2176 struct km_event c;
2177 struct sadb_x_sec_ctx *sec_ctx;
2179 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2180 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2181 !ext_hdrs[SADB_X_EXT_POLICY-1])
2182 return -EINVAL;
2184 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2185 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2186 return -EINVAL;
2187 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2188 return -EINVAL;
2190 xp = xfrm_policy_alloc(GFP_KERNEL);
2191 if (xp == NULL)
2192 return -ENOBUFS;
2194 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2195 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2196 xp->priority = pol->sadb_x_policy_priority;
2198 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2199 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2200 if (!xp->family) {
2201 err = -EINVAL;
2202 goto out;
2204 xp->selector.family = xp->family;
2205 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2206 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2207 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2208 if (xp->selector.sport)
2209 xp->selector.sport_mask = htons(0xffff);
2211 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2212 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2213 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2215 /* Amusing, we set this twice. KAME apps appear to set same value
2216 * in both addresses.
2218 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2220 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2221 if (xp->selector.dport)
2222 xp->selector.dport_mask = htons(0xffff);
2224 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2225 if (sec_ctx != NULL) {
2226 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2228 if (!uctx) {
2229 err = -ENOBUFS;
2230 goto out;
2233 err = security_xfrm_policy_alloc(xp, uctx);
2234 kfree(uctx);
2236 if (err)
2237 goto out;
2240 xp->lft.soft_byte_limit = XFRM_INF;
2241 xp->lft.hard_byte_limit = XFRM_INF;
2242 xp->lft.soft_packet_limit = XFRM_INF;
2243 xp->lft.hard_packet_limit = XFRM_INF;
2244 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2245 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2246 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2247 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2248 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2250 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2251 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2252 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2253 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2254 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2256 xp->xfrm_nr = 0;
2257 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2258 (err = parse_ipsecrequests(xp, pol)) < 0)
2259 goto out;
2261 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2262 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2264 xfrm_audit_policy_add(xp, err ? 0 : 1,
2265 audit_get_loginuid(current->audit_context), 0);
2267 if (err)
2268 goto out;
2270 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2271 c.event = XFRM_MSG_UPDPOLICY;
2272 else
2273 c.event = XFRM_MSG_NEWPOLICY;
2275 c.seq = hdr->sadb_msg_seq;
2276 c.pid = hdr->sadb_msg_pid;
2278 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2279 xfrm_pol_put(xp);
2280 return 0;
2282 out:
2283 security_xfrm_policy_free(xp);
2284 kfree(xp);
2285 return err;
2288 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2290 int err;
2291 struct sadb_address *sa;
2292 struct sadb_x_policy *pol;
2293 struct xfrm_policy *xp, tmp;
2294 struct xfrm_selector sel;
2295 struct km_event c;
2296 struct sadb_x_sec_ctx *sec_ctx;
2298 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2299 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2300 !ext_hdrs[SADB_X_EXT_POLICY-1])
2301 return -EINVAL;
2303 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2304 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2305 return -EINVAL;
2307 memset(&sel, 0, sizeof(sel));
2309 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2310 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2311 sel.prefixlen_s = sa->sadb_address_prefixlen;
2312 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2313 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2314 if (sel.sport)
2315 sel.sport_mask = htons(0xffff);
2317 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2318 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2319 sel.prefixlen_d = sa->sadb_address_prefixlen;
2320 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2321 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2322 if (sel.dport)
2323 sel.dport_mask = htons(0xffff);
2325 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2326 memset(&tmp, 0, sizeof(struct xfrm_policy));
2328 if (sec_ctx != NULL) {
2329 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2331 if (!uctx)
2332 return -ENOMEM;
2334 err = security_xfrm_policy_alloc(&tmp, uctx);
2335 kfree(uctx);
2337 if (err)
2338 return err;
2341 xp = xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN, pol->sadb_x_policy_dir-1,
2342 &sel, tmp.security, 1, &err);
2343 security_xfrm_policy_free(&tmp);
2345 if (xp == NULL)
2346 return -ENOENT;
2348 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2349 audit_get_loginuid(current->audit_context), 0);
2351 if (err)
2352 goto out;
2354 c.seq = hdr->sadb_msg_seq;
2355 c.pid = hdr->sadb_msg_pid;
2356 c.event = XFRM_MSG_DELPOLICY;
2357 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2359 out:
2360 xfrm_pol_put(xp);
2361 return err;
2364 static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2366 int err;
2367 struct sk_buff *out_skb;
2368 struct sadb_msg *out_hdr;
2369 err = 0;
2371 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2372 if (IS_ERR(out_skb)) {
2373 err = PTR_ERR(out_skb);
2374 goto out;
2376 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2377 if (err < 0)
2378 goto out;
2380 out_hdr = (struct sadb_msg *) out_skb->data;
2381 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2382 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2383 out_hdr->sadb_msg_satype = 0;
2384 out_hdr->sadb_msg_errno = 0;
2385 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2386 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2387 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
2388 err = 0;
2390 out:
2391 return err;
2394 #ifdef CONFIG_NET_KEY_MIGRATE
2395 static int pfkey_sockaddr_pair_size(sa_family_t family)
2397 switch (family) {
2398 case AF_INET:
2399 return PFKEY_ALIGN8(sizeof(struct sockaddr_in) * 2);
2400 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2401 case AF_INET6:
2402 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6) * 2);
2403 #endif
2404 default:
2405 return 0;
2407 /* NOTREACHED */
2410 static int parse_sockaddr_pair(struct sadb_x_ipsecrequest *rq,
2411 xfrm_address_t *saddr, xfrm_address_t *daddr,
2412 u16 *family)
2414 struct sockaddr *sa = (struct sockaddr *)(rq + 1);
2415 if (rq->sadb_x_ipsecrequest_len <
2416 pfkey_sockaddr_pair_size(sa->sa_family))
2417 return -EINVAL;
2419 switch (sa->sa_family) {
2420 case AF_INET:
2422 struct sockaddr_in *sin;
2423 sin = (struct sockaddr_in *)sa;
2424 if ((sin+1)->sin_family != AF_INET)
2425 return -EINVAL;
2426 memcpy(&saddr->a4, &sin->sin_addr, sizeof(saddr->a4));
2427 sin++;
2428 memcpy(&daddr->a4, &sin->sin_addr, sizeof(daddr->a4));
2429 *family = AF_INET;
2430 break;
2432 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2433 case AF_INET6:
2435 struct sockaddr_in6 *sin6;
2436 sin6 = (struct sockaddr_in6 *)sa;
2437 if ((sin6+1)->sin6_family != AF_INET6)
2438 return -EINVAL;
2439 memcpy(&saddr->a6, &sin6->sin6_addr,
2440 sizeof(saddr->a6));
2441 sin6++;
2442 memcpy(&daddr->a6, &sin6->sin6_addr,
2443 sizeof(daddr->a6));
2444 *family = AF_INET6;
2445 break;
2447 #endif
2448 default:
2449 return -EINVAL;
2452 return 0;
2455 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2456 struct xfrm_migrate *m)
2458 int err;
2459 struct sadb_x_ipsecrequest *rq2;
2460 int mode;
2462 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2463 len < rq1->sadb_x_ipsecrequest_len)
2464 return -EINVAL;
2466 /* old endoints */
2467 err = parse_sockaddr_pair(rq1, &m->old_saddr, &m->old_daddr,
2468 &m->old_family);
2469 if (err)
2470 return err;
2472 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2473 len -= rq1->sadb_x_ipsecrequest_len;
2475 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2476 len < rq2->sadb_x_ipsecrequest_len)
2477 return -EINVAL;
2479 /* new endpoints */
2480 err = parse_sockaddr_pair(rq2, &m->new_saddr, &m->new_daddr,
2481 &m->new_family);
2482 if (err)
2483 return err;
2485 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2486 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2487 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2488 return -EINVAL;
2490 m->proto = rq1->sadb_x_ipsecrequest_proto;
2491 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2492 return -EINVAL;
2493 m->mode = mode;
2494 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2496 return ((int)(rq1->sadb_x_ipsecrequest_len +
2497 rq2->sadb_x_ipsecrequest_len));
2500 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2501 struct sadb_msg *hdr, void **ext_hdrs)
2503 int i, len, ret, err = -EINVAL;
2504 u8 dir;
2505 struct sadb_address *sa;
2506 struct sadb_x_policy *pol;
2507 struct sadb_x_ipsecrequest *rq;
2508 struct xfrm_selector sel;
2509 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2511 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2512 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2513 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2514 err = -EINVAL;
2515 goto out;
2518 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2519 if (!pol) {
2520 err = -EINVAL;
2521 goto out;
2524 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2525 err = -EINVAL;
2526 goto out;
2529 dir = pol->sadb_x_policy_dir - 1;
2530 memset(&sel, 0, sizeof(sel));
2532 /* set source address info of selector */
2533 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2534 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2535 sel.prefixlen_s = sa->sadb_address_prefixlen;
2536 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2537 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2538 if (sel.sport)
2539 sel.sport_mask = htons(0xffff);
2541 /* set destination address info of selector */
2542 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2543 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2544 sel.prefixlen_d = sa->sadb_address_prefixlen;
2545 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2546 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2547 if (sel.dport)
2548 sel.dport_mask = htons(0xffff);
2550 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2552 /* extract ipsecrequests */
2553 i = 0;
2554 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2556 while (len > 0 && i < XFRM_MAX_DEPTH) {
2557 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2558 if (ret < 0) {
2559 err = ret;
2560 goto out;
2561 } else {
2562 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2563 len -= ret;
2564 i++;
2568 if (!i || len > 0) {
2569 err = -EINVAL;
2570 goto out;
2573 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i);
2575 out:
2576 return err;
2578 #else
2579 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2580 struct sadb_msg *hdr, void **ext_hdrs)
2582 return -ENOPROTOOPT;
2584 #endif
2587 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2589 unsigned int dir;
2590 int err = 0, delete;
2591 struct sadb_x_policy *pol;
2592 struct xfrm_policy *xp;
2593 struct km_event c;
2595 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2596 return -EINVAL;
2598 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2599 if (dir >= XFRM_POLICY_MAX)
2600 return -EINVAL;
2602 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2603 xp = xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN, dir, pol->sadb_x_policy_id,
2604 delete, &err);
2605 if (xp == NULL)
2606 return -ENOENT;
2608 if (delete) {
2609 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2610 audit_get_loginuid(current->audit_context), 0);
2612 if (err)
2613 goto out;
2614 c.seq = hdr->sadb_msg_seq;
2615 c.pid = hdr->sadb_msg_pid;
2616 c.data.byid = 1;
2617 c.event = XFRM_MSG_DELPOLICY;
2618 km_policy_notify(xp, dir, &c);
2619 } else {
2620 err = key_pol_get_resp(sk, xp, hdr, dir);
2623 out:
2624 xfrm_pol_put(xp);
2625 return err;
2628 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2630 struct pfkey_dump_data *data = ptr;
2631 struct sk_buff *out_skb;
2632 struct sadb_msg *out_hdr;
2633 int err;
2635 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2636 if (IS_ERR(out_skb))
2637 return PTR_ERR(out_skb);
2639 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2640 if (err < 0)
2641 return err;
2643 out_hdr = (struct sadb_msg *) out_skb->data;
2644 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
2645 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2646 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2647 out_hdr->sadb_msg_errno = 0;
2648 out_hdr->sadb_msg_seq = count;
2649 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
2650 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
2651 return 0;
2654 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2656 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
2658 return xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_sp, &data);
2661 static int key_notify_policy_flush(struct km_event *c)
2663 struct sk_buff *skb_out;
2664 struct sadb_msg *hdr;
2666 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
2667 if (!skb_out)
2668 return -ENOBUFS;
2669 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2670 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
2671 hdr->sadb_msg_seq = c->seq;
2672 hdr->sadb_msg_pid = c->pid;
2673 hdr->sadb_msg_version = PF_KEY_V2;
2674 hdr->sadb_msg_errno = (uint8_t) 0;
2675 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2676 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2677 return 0;
2681 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2683 struct km_event c;
2684 struct xfrm_audit audit_info;
2685 int err;
2687 audit_info.loginuid = audit_get_loginuid(current->audit_context);
2688 audit_info.secid = 0;
2689 err = xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN, &audit_info);
2690 if (err)
2691 return err;
2692 c.data.type = XFRM_POLICY_TYPE_MAIN;
2693 c.event = XFRM_MSG_FLUSHPOLICY;
2694 c.pid = hdr->sadb_msg_pid;
2695 c.seq = hdr->sadb_msg_seq;
2696 km_policy_notify(NULL, 0, &c);
2698 return 0;
2701 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2702 struct sadb_msg *hdr, void **ext_hdrs);
2703 static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2704 [SADB_RESERVED] = pfkey_reserved,
2705 [SADB_GETSPI] = pfkey_getspi,
2706 [SADB_UPDATE] = pfkey_add,
2707 [SADB_ADD] = pfkey_add,
2708 [SADB_DELETE] = pfkey_delete,
2709 [SADB_GET] = pfkey_get,
2710 [SADB_ACQUIRE] = pfkey_acquire,
2711 [SADB_REGISTER] = pfkey_register,
2712 [SADB_EXPIRE] = NULL,
2713 [SADB_FLUSH] = pfkey_flush,
2714 [SADB_DUMP] = pfkey_dump,
2715 [SADB_X_PROMISC] = pfkey_promisc,
2716 [SADB_X_PCHANGE] = NULL,
2717 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2718 [SADB_X_SPDADD] = pfkey_spdadd,
2719 [SADB_X_SPDDELETE] = pfkey_spddelete,
2720 [SADB_X_SPDGET] = pfkey_spdget,
2721 [SADB_X_SPDACQUIRE] = NULL,
2722 [SADB_X_SPDDUMP] = pfkey_spddump,
2723 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2724 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2725 [SADB_X_SPDDELETE2] = pfkey_spdget,
2726 [SADB_X_MIGRATE] = pfkey_migrate,
2729 static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2731 void *ext_hdrs[SADB_EXT_MAX];
2732 int err;
2734 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2735 BROADCAST_PROMISC_ONLY, NULL);
2737 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2738 err = parse_exthdrs(skb, hdr, ext_hdrs);
2739 if (!err) {
2740 err = -EOPNOTSUPP;
2741 if (pfkey_funcs[hdr->sadb_msg_type])
2742 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2744 return err;
2747 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2749 struct sadb_msg *hdr = NULL;
2751 if (skb->len < sizeof(*hdr)) {
2752 *errp = -EMSGSIZE;
2753 } else {
2754 hdr = (struct sadb_msg *) skb->data;
2755 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2756 hdr->sadb_msg_reserved != 0 ||
2757 (hdr->sadb_msg_type <= SADB_RESERVED ||
2758 hdr->sadb_msg_type > SADB_MAX)) {
2759 hdr = NULL;
2760 *errp = -EINVAL;
2761 } else if (hdr->sadb_msg_len != (skb->len /
2762 sizeof(uint64_t)) ||
2763 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2764 sizeof(uint64_t))) {
2765 hdr = NULL;
2766 *errp = -EMSGSIZE;
2767 } else {
2768 *errp = 0;
2771 return hdr;
2774 static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2776 return t->aalgos & (1 << d->desc.sadb_alg_id);
2779 static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2781 return t->ealgos & (1 << d->desc.sadb_alg_id);
2784 static int count_ah_combs(struct xfrm_tmpl *t)
2786 int i, sz = 0;
2788 for (i = 0; ; i++) {
2789 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2790 if (!aalg)
2791 break;
2792 if (aalg_tmpl_set(t, aalg) && aalg->available)
2793 sz += sizeof(struct sadb_comb);
2795 return sz + sizeof(struct sadb_prop);
2798 static int count_esp_combs(struct xfrm_tmpl *t)
2800 int i, k, sz = 0;
2802 for (i = 0; ; i++) {
2803 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2804 if (!ealg)
2805 break;
2807 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2808 continue;
2810 for (k = 1; ; k++) {
2811 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2812 if (!aalg)
2813 break;
2815 if (aalg_tmpl_set(t, aalg) && aalg->available)
2816 sz += sizeof(struct sadb_comb);
2819 return sz + sizeof(struct sadb_prop);
2822 static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2824 struct sadb_prop *p;
2825 int i;
2827 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2828 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2829 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2830 p->sadb_prop_replay = 32;
2831 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2833 for (i = 0; ; i++) {
2834 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2835 if (!aalg)
2836 break;
2838 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2839 struct sadb_comb *c;
2840 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2841 memset(c, 0, sizeof(*c));
2842 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2843 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2844 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2845 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2846 c->sadb_comb_hard_addtime = 24*60*60;
2847 c->sadb_comb_soft_addtime = 20*60*60;
2848 c->sadb_comb_hard_usetime = 8*60*60;
2849 c->sadb_comb_soft_usetime = 7*60*60;
2854 static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2856 struct sadb_prop *p;
2857 int i, k;
2859 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2860 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2861 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2862 p->sadb_prop_replay = 32;
2863 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2865 for (i=0; ; i++) {
2866 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2867 if (!ealg)
2868 break;
2870 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2871 continue;
2873 for (k = 1; ; k++) {
2874 struct sadb_comb *c;
2875 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2876 if (!aalg)
2877 break;
2878 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2879 continue;
2880 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2881 memset(c, 0, sizeof(*c));
2882 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2883 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2884 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2885 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2886 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2887 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2888 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2889 c->sadb_comb_hard_addtime = 24*60*60;
2890 c->sadb_comb_soft_addtime = 20*60*60;
2891 c->sadb_comb_hard_usetime = 8*60*60;
2892 c->sadb_comb_soft_usetime = 7*60*60;
2897 static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2899 return 0;
2902 static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
2904 struct sk_buff *out_skb;
2905 struct sadb_msg *out_hdr;
2906 int hard;
2907 int hsc;
2909 hard = c->data.hard;
2910 if (hard)
2911 hsc = 2;
2912 else
2913 hsc = 1;
2915 out_skb = pfkey_xfrm_state2msg(x, 0, hsc);
2916 if (IS_ERR(out_skb))
2917 return PTR_ERR(out_skb);
2919 out_hdr = (struct sadb_msg *) out_skb->data;
2920 out_hdr->sadb_msg_version = PF_KEY_V2;
2921 out_hdr->sadb_msg_type = SADB_EXPIRE;
2922 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2923 out_hdr->sadb_msg_errno = 0;
2924 out_hdr->sadb_msg_reserved = 0;
2925 out_hdr->sadb_msg_seq = 0;
2926 out_hdr->sadb_msg_pid = 0;
2928 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2929 return 0;
2932 static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2934 switch (c->event) {
2935 case XFRM_MSG_EXPIRE:
2936 return key_notify_sa_expire(x, c);
2937 case XFRM_MSG_DELSA:
2938 case XFRM_MSG_NEWSA:
2939 case XFRM_MSG_UPDSA:
2940 return key_notify_sa(x, c);
2941 case XFRM_MSG_FLUSHSA:
2942 return key_notify_sa_flush(c);
2943 case XFRM_MSG_NEWAE: /* not yet supported */
2944 break;
2945 default:
2946 printk("pfkey: Unknown SA event %d\n", c->event);
2947 break;
2950 return 0;
2953 static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2955 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
2956 return 0;
2958 switch (c->event) {
2959 case XFRM_MSG_POLEXPIRE:
2960 return key_notify_policy_expire(xp, c);
2961 case XFRM_MSG_DELPOLICY:
2962 case XFRM_MSG_NEWPOLICY:
2963 case XFRM_MSG_UPDPOLICY:
2964 return key_notify_policy(xp, dir, c);
2965 case XFRM_MSG_FLUSHPOLICY:
2966 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
2967 break;
2968 return key_notify_policy_flush(c);
2969 default:
2970 printk("pfkey: Unknown policy event %d\n", c->event);
2971 break;
2974 return 0;
2977 static u32 get_acqseq(void)
2979 u32 res;
2980 static u32 acqseq;
2981 static DEFINE_SPINLOCK(acqseq_lock);
2983 spin_lock_bh(&acqseq_lock);
2984 res = (++acqseq ? : ++acqseq);
2985 spin_unlock_bh(&acqseq_lock);
2986 return res;
2989 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2991 struct sk_buff *skb;
2992 struct sadb_msg *hdr;
2993 struct sadb_address *addr;
2994 struct sadb_x_policy *pol;
2995 struct sockaddr_in *sin;
2996 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2997 struct sockaddr_in6 *sin6;
2998 #endif
2999 int sockaddr_size;
3000 int size;
3001 struct sadb_x_sec_ctx *sec_ctx;
3002 struct xfrm_sec_ctx *xfrm_ctx;
3003 int ctx_size = 0;
3005 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3006 if (!sockaddr_size)
3007 return -EINVAL;
3009 size = sizeof(struct sadb_msg) +
3010 (sizeof(struct sadb_address) * 2) +
3011 (sockaddr_size * 2) +
3012 sizeof(struct sadb_x_policy);
3014 if (x->id.proto == IPPROTO_AH)
3015 size += count_ah_combs(t);
3016 else if (x->id.proto == IPPROTO_ESP)
3017 size += count_esp_combs(t);
3019 if ((xfrm_ctx = x->security)) {
3020 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3021 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3024 skb = alloc_skb(size + 16, GFP_ATOMIC);
3025 if (skb == NULL)
3026 return -ENOMEM;
3028 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3029 hdr->sadb_msg_version = PF_KEY_V2;
3030 hdr->sadb_msg_type = SADB_ACQUIRE;
3031 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3032 hdr->sadb_msg_len = size / sizeof(uint64_t);
3033 hdr->sadb_msg_errno = 0;
3034 hdr->sadb_msg_reserved = 0;
3035 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3036 hdr->sadb_msg_pid = 0;
3038 /* src address */
3039 addr = (struct sadb_address*) skb_put(skb,
3040 sizeof(struct sadb_address)+sockaddr_size);
3041 addr->sadb_address_len =
3042 (sizeof(struct sadb_address)+sockaddr_size)/
3043 sizeof(uint64_t);
3044 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3045 addr->sadb_address_proto = 0;
3046 addr->sadb_address_reserved = 0;
3047 if (x->props.family == AF_INET) {
3048 addr->sadb_address_prefixlen = 32;
3050 sin = (struct sockaddr_in *) (addr + 1);
3051 sin->sin_family = AF_INET;
3052 sin->sin_addr.s_addr = x->props.saddr.a4;
3053 sin->sin_port = 0;
3054 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3056 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3057 else if (x->props.family == AF_INET6) {
3058 addr->sadb_address_prefixlen = 128;
3060 sin6 = (struct sockaddr_in6 *) (addr + 1);
3061 sin6->sin6_family = AF_INET6;
3062 sin6->sin6_port = 0;
3063 sin6->sin6_flowinfo = 0;
3064 memcpy(&sin6->sin6_addr,
3065 x->props.saddr.a6, sizeof(struct in6_addr));
3066 sin6->sin6_scope_id = 0;
3068 #endif
3069 else
3070 BUG();
3072 /* dst address */
3073 addr = (struct sadb_address*) skb_put(skb,
3074 sizeof(struct sadb_address)+sockaddr_size);
3075 addr->sadb_address_len =
3076 (sizeof(struct sadb_address)+sockaddr_size)/
3077 sizeof(uint64_t);
3078 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3079 addr->sadb_address_proto = 0;
3080 addr->sadb_address_reserved = 0;
3081 if (x->props.family == AF_INET) {
3082 addr->sadb_address_prefixlen = 32;
3084 sin = (struct sockaddr_in *) (addr + 1);
3085 sin->sin_family = AF_INET;
3086 sin->sin_addr.s_addr = x->id.daddr.a4;
3087 sin->sin_port = 0;
3088 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3090 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3091 else if (x->props.family == AF_INET6) {
3092 addr->sadb_address_prefixlen = 128;
3094 sin6 = (struct sockaddr_in6 *) (addr + 1);
3095 sin6->sin6_family = AF_INET6;
3096 sin6->sin6_port = 0;
3097 sin6->sin6_flowinfo = 0;
3098 memcpy(&sin6->sin6_addr,
3099 x->id.daddr.a6, sizeof(struct in6_addr));
3100 sin6->sin6_scope_id = 0;
3102 #endif
3103 else
3104 BUG();
3106 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3107 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3108 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3109 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3110 pol->sadb_x_policy_dir = dir+1;
3111 pol->sadb_x_policy_id = xp->index;
3113 /* Set sadb_comb's. */
3114 if (x->id.proto == IPPROTO_AH)
3115 dump_ah_combs(skb, t);
3116 else if (x->id.proto == IPPROTO_ESP)
3117 dump_esp_combs(skb, t);
3119 /* security context */
3120 if (xfrm_ctx) {
3121 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3122 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3123 sec_ctx->sadb_x_sec_len =
3124 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3125 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3126 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3127 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3128 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3129 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3130 xfrm_ctx->ctx_len);
3133 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3136 static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
3137 u8 *data, int len, int *dir)
3139 struct xfrm_policy *xp;
3140 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
3141 struct sadb_x_sec_ctx *sec_ctx;
3143 switch (sk->sk_family) {
3144 case AF_INET:
3145 if (opt != IP_IPSEC_POLICY) {
3146 *dir = -EOPNOTSUPP;
3147 return NULL;
3149 break;
3150 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3151 case AF_INET6:
3152 if (opt != IPV6_IPSEC_POLICY) {
3153 *dir = -EOPNOTSUPP;
3154 return NULL;
3156 break;
3157 #endif
3158 default:
3159 *dir = -EINVAL;
3160 return NULL;
3163 *dir = -EINVAL;
3165 if (len < sizeof(struct sadb_x_policy) ||
3166 pol->sadb_x_policy_len*8 > len ||
3167 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3168 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3169 return NULL;
3171 xp = xfrm_policy_alloc(GFP_ATOMIC);
3172 if (xp == NULL) {
3173 *dir = -ENOBUFS;
3174 return NULL;
3177 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3178 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3180 xp->lft.soft_byte_limit = XFRM_INF;
3181 xp->lft.hard_byte_limit = XFRM_INF;
3182 xp->lft.soft_packet_limit = XFRM_INF;
3183 xp->lft.hard_packet_limit = XFRM_INF;
3184 xp->family = sk->sk_family;
3186 xp->xfrm_nr = 0;
3187 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3188 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3189 goto out;
3191 /* security context too */
3192 if (len >= (pol->sadb_x_policy_len*8 +
3193 sizeof(struct sadb_x_sec_ctx))) {
3194 char *p = (char *)pol;
3195 struct xfrm_user_sec_ctx *uctx;
3197 p += pol->sadb_x_policy_len*8;
3198 sec_ctx = (struct sadb_x_sec_ctx *)p;
3199 if (len < pol->sadb_x_policy_len*8 +
3200 sec_ctx->sadb_x_sec_len) {
3201 *dir = -EINVAL;
3202 goto out;
3204 if ((*dir = verify_sec_ctx_len(p)))
3205 goto out;
3206 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
3207 *dir = security_xfrm_policy_alloc(xp, uctx);
3208 kfree(uctx);
3210 if (*dir)
3211 goto out;
3214 *dir = pol->sadb_x_policy_dir-1;
3215 return xp;
3217 out:
3218 security_xfrm_policy_free(xp);
3219 kfree(xp);
3220 return NULL;
3223 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
3225 struct sk_buff *skb;
3226 struct sadb_msg *hdr;
3227 struct sadb_sa *sa;
3228 struct sadb_address *addr;
3229 struct sadb_x_nat_t_port *n_port;
3230 struct sockaddr_in *sin;
3231 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3232 struct sockaddr_in6 *sin6;
3233 #endif
3234 int sockaddr_size;
3235 int size;
3236 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3237 struct xfrm_encap_tmpl *natt = NULL;
3239 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3240 if (!sockaddr_size)
3241 return -EINVAL;
3243 if (!satype)
3244 return -EINVAL;
3246 if (!x->encap)
3247 return -EINVAL;
3249 natt = x->encap;
3251 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3253 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3254 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3257 size = sizeof(struct sadb_msg) +
3258 sizeof(struct sadb_sa) +
3259 (sizeof(struct sadb_address) * 2) +
3260 (sockaddr_size * 2) +
3261 (sizeof(struct sadb_x_nat_t_port) * 2);
3263 skb = alloc_skb(size + 16, GFP_ATOMIC);
3264 if (skb == NULL)
3265 return -ENOMEM;
3267 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3268 hdr->sadb_msg_version = PF_KEY_V2;
3269 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3270 hdr->sadb_msg_satype = satype;
3271 hdr->sadb_msg_len = size / sizeof(uint64_t);
3272 hdr->sadb_msg_errno = 0;
3273 hdr->sadb_msg_reserved = 0;
3274 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3275 hdr->sadb_msg_pid = 0;
3277 /* SA */
3278 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3279 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3280 sa->sadb_sa_exttype = SADB_EXT_SA;
3281 sa->sadb_sa_spi = x->id.spi;
3282 sa->sadb_sa_replay = 0;
3283 sa->sadb_sa_state = 0;
3284 sa->sadb_sa_auth = 0;
3285 sa->sadb_sa_encrypt = 0;
3286 sa->sadb_sa_flags = 0;
3288 /* ADDRESS_SRC (old addr) */
3289 addr = (struct sadb_address*)
3290 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3291 addr->sadb_address_len =
3292 (sizeof(struct sadb_address)+sockaddr_size)/
3293 sizeof(uint64_t);
3294 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3295 addr->sadb_address_proto = 0;
3296 addr->sadb_address_reserved = 0;
3297 if (x->props.family == AF_INET) {
3298 addr->sadb_address_prefixlen = 32;
3300 sin = (struct sockaddr_in *) (addr + 1);
3301 sin->sin_family = AF_INET;
3302 sin->sin_addr.s_addr = x->props.saddr.a4;
3303 sin->sin_port = 0;
3304 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3306 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3307 else if (x->props.family == AF_INET6) {
3308 addr->sadb_address_prefixlen = 128;
3310 sin6 = (struct sockaddr_in6 *) (addr + 1);
3311 sin6->sin6_family = AF_INET6;
3312 sin6->sin6_port = 0;
3313 sin6->sin6_flowinfo = 0;
3314 memcpy(&sin6->sin6_addr,
3315 x->props.saddr.a6, sizeof(struct in6_addr));
3316 sin6->sin6_scope_id = 0;
3318 #endif
3319 else
3320 BUG();
3322 /* NAT_T_SPORT (old port) */
3323 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3324 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3325 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3326 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3327 n_port->sadb_x_nat_t_port_reserved = 0;
3329 /* ADDRESS_DST (new addr) */
3330 addr = (struct sadb_address*)
3331 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3332 addr->sadb_address_len =
3333 (sizeof(struct sadb_address)+sockaddr_size)/
3334 sizeof(uint64_t);
3335 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3336 addr->sadb_address_proto = 0;
3337 addr->sadb_address_reserved = 0;
3338 if (x->props.family == AF_INET) {
3339 addr->sadb_address_prefixlen = 32;
3341 sin = (struct sockaddr_in *) (addr + 1);
3342 sin->sin_family = AF_INET;
3343 sin->sin_addr.s_addr = ipaddr->a4;
3344 sin->sin_port = 0;
3345 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3347 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3348 else if (x->props.family == AF_INET6) {
3349 addr->sadb_address_prefixlen = 128;
3351 sin6 = (struct sockaddr_in6 *) (addr + 1);
3352 sin6->sin6_family = AF_INET6;
3353 sin6->sin6_port = 0;
3354 sin6->sin6_flowinfo = 0;
3355 memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
3356 sin6->sin6_scope_id = 0;
3358 #endif
3359 else
3360 BUG();
3362 /* NAT_T_DPORT (new port) */
3363 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3364 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3365 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3366 n_port->sadb_x_nat_t_port_port = sport;
3367 n_port->sadb_x_nat_t_port_reserved = 0;
3369 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3372 #ifdef CONFIG_NET_KEY_MIGRATE
3373 static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3374 struct xfrm_selector *sel)
3376 struct sadb_address *addr;
3377 struct sockaddr_in *sin;
3378 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3379 struct sockaddr_in6 *sin6;
3380 #endif
3381 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3382 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3383 addr->sadb_address_exttype = type;
3384 addr->sadb_address_proto = sel->proto;
3385 addr->sadb_address_reserved = 0;
3387 switch (type) {
3388 case SADB_EXT_ADDRESS_SRC:
3389 if (sel->family == AF_INET) {
3390 addr->sadb_address_prefixlen = sel->prefixlen_s;
3391 sin = (struct sockaddr_in *)(addr + 1);
3392 sin->sin_family = AF_INET;
3393 memcpy(&sin->sin_addr.s_addr, &sel->saddr,
3394 sizeof(sin->sin_addr.s_addr));
3395 sin->sin_port = 0;
3396 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3398 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3399 else if (sel->family == AF_INET6) {
3400 addr->sadb_address_prefixlen = sel->prefixlen_s;
3401 sin6 = (struct sockaddr_in6 *)(addr + 1);
3402 sin6->sin6_family = AF_INET6;
3403 sin6->sin6_port = 0;
3404 sin6->sin6_flowinfo = 0;
3405 sin6->sin6_scope_id = 0;
3406 memcpy(&sin6->sin6_addr.s6_addr, &sel->saddr,
3407 sizeof(sin6->sin6_addr.s6_addr));
3409 #endif
3410 break;
3411 case SADB_EXT_ADDRESS_DST:
3412 if (sel->family == AF_INET) {
3413 addr->sadb_address_prefixlen = sel->prefixlen_d;
3414 sin = (struct sockaddr_in *)(addr + 1);
3415 sin->sin_family = AF_INET;
3416 memcpy(&sin->sin_addr.s_addr, &sel->daddr,
3417 sizeof(sin->sin_addr.s_addr));
3418 sin->sin_port = 0;
3419 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3421 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3422 else if (sel->family == AF_INET6) {
3423 addr->sadb_address_prefixlen = sel->prefixlen_d;
3424 sin6 = (struct sockaddr_in6 *)(addr + 1);
3425 sin6->sin6_family = AF_INET6;
3426 sin6->sin6_port = 0;
3427 sin6->sin6_flowinfo = 0;
3428 sin6->sin6_scope_id = 0;
3429 memcpy(&sin6->sin6_addr.s6_addr, &sel->daddr,
3430 sizeof(sin6->sin6_addr.s6_addr));
3432 #endif
3433 break;
3434 default:
3435 return -EINVAL;
3438 return 0;
3441 static int set_ipsecrequest(struct sk_buff *skb,
3442 uint8_t proto, uint8_t mode, int level,
3443 uint32_t reqid, uint8_t family,
3444 xfrm_address_t *src, xfrm_address_t *dst)
3446 struct sadb_x_ipsecrequest *rq;
3447 struct sockaddr_in *sin;
3448 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3449 struct sockaddr_in6 *sin6;
3450 #endif
3451 int size_req;
3453 size_req = sizeof(struct sadb_x_ipsecrequest) +
3454 pfkey_sockaddr_pair_size(family);
3456 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3457 memset(rq, 0, size_req);
3458 rq->sadb_x_ipsecrequest_len = size_req;
3459 rq->sadb_x_ipsecrequest_proto = proto;
3460 rq->sadb_x_ipsecrequest_mode = mode;
3461 rq->sadb_x_ipsecrequest_level = level;
3462 rq->sadb_x_ipsecrequest_reqid = reqid;
3464 switch (family) {
3465 case AF_INET:
3466 sin = (struct sockaddr_in *)(rq + 1);
3467 sin->sin_family = AF_INET;
3468 memcpy(&sin->sin_addr.s_addr, src,
3469 sizeof(sin->sin_addr.s_addr));
3470 sin++;
3471 sin->sin_family = AF_INET;
3472 memcpy(&sin->sin_addr.s_addr, dst,
3473 sizeof(sin->sin_addr.s_addr));
3474 break;
3475 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3476 case AF_INET6:
3477 sin6 = (struct sockaddr_in6 *)(rq + 1);
3478 sin6->sin6_family = AF_INET6;
3479 sin6->sin6_port = 0;
3480 sin6->sin6_flowinfo = 0;
3481 sin6->sin6_scope_id = 0;
3482 memcpy(&sin6->sin6_addr.s6_addr, src,
3483 sizeof(sin6->sin6_addr.s6_addr));
3484 sin6++;
3485 sin6->sin6_family = AF_INET6;
3486 sin6->sin6_port = 0;
3487 sin6->sin6_flowinfo = 0;
3488 sin6->sin6_scope_id = 0;
3489 memcpy(&sin6->sin6_addr.s6_addr, dst,
3490 sizeof(sin6->sin6_addr.s6_addr));
3491 break;
3492 #endif
3493 default:
3494 return -EINVAL;
3497 return 0;
3499 #endif
3501 #ifdef CONFIG_NET_KEY_MIGRATE
3502 static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3503 struct xfrm_migrate *m, int num_bundles)
3505 int i;
3506 int sasize_sel;
3507 int size = 0;
3508 int size_pol = 0;
3509 struct sk_buff *skb;
3510 struct sadb_msg *hdr;
3511 struct sadb_x_policy *pol;
3512 struct xfrm_migrate *mp;
3514 if (type != XFRM_POLICY_TYPE_MAIN)
3515 return 0;
3517 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3518 return -EINVAL;
3520 /* selector */
3521 sasize_sel = pfkey_sockaddr_size(sel->family);
3522 if (!sasize_sel)
3523 return -EINVAL;
3524 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3526 /* policy info */
3527 size_pol += sizeof(struct sadb_x_policy);
3529 /* ipsecrequests */
3530 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3531 /* old locator pair */
3532 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3533 pfkey_sockaddr_pair_size(mp->old_family);
3534 /* new locator pair */
3535 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3536 pfkey_sockaddr_pair_size(mp->new_family);
3539 size += sizeof(struct sadb_msg) + size_pol;
3541 /* alloc buffer */
3542 skb = alloc_skb(size, GFP_ATOMIC);
3543 if (skb == NULL)
3544 return -ENOMEM;
3546 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3547 hdr->sadb_msg_version = PF_KEY_V2;
3548 hdr->sadb_msg_type = SADB_X_MIGRATE;
3549 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3550 hdr->sadb_msg_len = size / 8;
3551 hdr->sadb_msg_errno = 0;
3552 hdr->sadb_msg_reserved = 0;
3553 hdr->sadb_msg_seq = 0;
3554 hdr->sadb_msg_pid = 0;
3556 /* selector src */
3557 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3559 /* selector dst */
3560 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3562 /* policy information */
3563 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3564 pol->sadb_x_policy_len = size_pol / 8;
3565 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3566 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3567 pol->sadb_x_policy_dir = dir + 1;
3568 pol->sadb_x_policy_id = 0;
3569 pol->sadb_x_policy_priority = 0;
3571 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3572 /* old ipsecrequest */
3573 int mode = pfkey_mode_from_xfrm(mp->mode);
3574 if (mode < 0)
3575 return -EINVAL;
3576 if (set_ipsecrequest(skb, mp->proto, mode,
3577 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3578 mp->reqid, mp->old_family,
3579 &mp->old_saddr, &mp->old_daddr) < 0) {
3580 return -EINVAL;
3583 /* new ipsecrequest */
3584 if (set_ipsecrequest(skb, mp->proto, mode,
3585 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3586 mp->reqid, mp->new_family,
3587 &mp->new_saddr, &mp->new_daddr) < 0) {
3588 return -EINVAL;
3592 /* broadcast migrate message to sockets */
3593 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
3595 return 0;
3597 #else
3598 static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3599 struct xfrm_migrate *m, int num_bundles)
3601 return -ENOPROTOOPT;
3603 #endif
3605 static int pfkey_sendmsg(struct kiocb *kiocb,
3606 struct socket *sock, struct msghdr *msg, size_t len)
3608 struct sock *sk = sock->sk;
3609 struct sk_buff *skb = NULL;
3610 struct sadb_msg *hdr = NULL;
3611 int err;
3613 err = -EOPNOTSUPP;
3614 if (msg->msg_flags & MSG_OOB)
3615 goto out;
3617 err = -EMSGSIZE;
3618 if ((unsigned)len > sk->sk_sndbuf - 32)
3619 goto out;
3621 err = -ENOBUFS;
3622 skb = alloc_skb(len, GFP_KERNEL);
3623 if (skb == NULL)
3624 goto out;
3626 err = -EFAULT;
3627 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3628 goto out;
3630 hdr = pfkey_get_base_msg(skb, &err);
3631 if (!hdr)
3632 goto out;
3634 mutex_lock(&xfrm_cfg_mutex);
3635 err = pfkey_process(sk, skb, hdr);
3636 mutex_unlock(&xfrm_cfg_mutex);
3638 out:
3639 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3640 err = 0;
3641 if (skb)
3642 kfree_skb(skb);
3644 return err ? : len;
3647 static int pfkey_recvmsg(struct kiocb *kiocb,
3648 struct socket *sock, struct msghdr *msg, size_t len,
3649 int flags)
3651 struct sock *sk = sock->sk;
3652 struct sk_buff *skb;
3653 int copied, err;
3655 err = -EINVAL;
3656 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3657 goto out;
3659 msg->msg_namelen = 0;
3660 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3661 if (skb == NULL)
3662 goto out;
3664 copied = skb->len;
3665 if (copied > len) {
3666 msg->msg_flags |= MSG_TRUNC;
3667 copied = len;
3670 skb_reset_transport_header(skb);
3671 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3672 if (err)
3673 goto out_free;
3675 sock_recv_timestamp(msg, sk, skb);
3677 err = (flags & MSG_TRUNC) ? skb->len : copied;
3679 out_free:
3680 skb_free_datagram(sk, skb);
3681 out:
3682 return err;
3685 static const struct proto_ops pfkey_ops = {
3686 .family = PF_KEY,
3687 .owner = THIS_MODULE,
3688 /* Operations that make no sense on pfkey sockets. */
3689 .bind = sock_no_bind,
3690 .connect = sock_no_connect,
3691 .socketpair = sock_no_socketpair,
3692 .accept = sock_no_accept,
3693 .getname = sock_no_getname,
3694 .ioctl = sock_no_ioctl,
3695 .listen = sock_no_listen,
3696 .shutdown = sock_no_shutdown,
3697 .setsockopt = sock_no_setsockopt,
3698 .getsockopt = sock_no_getsockopt,
3699 .mmap = sock_no_mmap,
3700 .sendpage = sock_no_sendpage,
3702 /* Now the operations that really occur. */
3703 .release = pfkey_release,
3704 .poll = datagram_poll,
3705 .sendmsg = pfkey_sendmsg,
3706 .recvmsg = pfkey_recvmsg,
3709 static struct net_proto_family pfkey_family_ops = {
3710 .family = PF_KEY,
3711 .create = pfkey_create,
3712 .owner = THIS_MODULE,
3715 #ifdef CONFIG_PROC_FS
3716 static int pfkey_read_proc(char *buffer, char **start, off_t offset,
3717 int length, int *eof, void *data)
3719 off_t pos = 0;
3720 off_t begin = 0;
3721 int len = 0;
3722 struct sock *s;
3723 struct hlist_node *node;
3725 len += sprintf(buffer,"sk RefCnt Rmem Wmem User Inode\n");
3727 read_lock(&pfkey_table_lock);
3729 sk_for_each(s, node, &pfkey_table) {
3730 len += sprintf(buffer+len,"%p %-6d %-6u %-6u %-6u %-6lu",
3732 atomic_read(&s->sk_refcnt),
3733 atomic_read(&s->sk_rmem_alloc),
3734 atomic_read(&s->sk_wmem_alloc),
3735 sock_i_uid(s),
3736 sock_i_ino(s)
3739 buffer[len++] = '\n';
3741 pos = begin + len;
3742 if (pos < offset) {
3743 len = 0;
3744 begin = pos;
3746 if(pos > offset + length)
3747 goto done;
3749 *eof = 1;
3751 done:
3752 read_unlock(&pfkey_table_lock);
3754 *start = buffer + (offset - begin);
3755 len -= (offset - begin);
3757 if (len > length)
3758 len = length;
3759 if (len < 0)
3760 len = 0;
3762 return len;
3764 #endif
3766 static struct xfrm_mgr pfkeyv2_mgr =
3768 .id = "pfkeyv2",
3769 .notify = pfkey_send_notify,
3770 .acquire = pfkey_send_acquire,
3771 .compile_policy = pfkey_compile_policy,
3772 .new_mapping = pfkey_send_new_mapping,
3773 .notify_policy = pfkey_send_policy_notify,
3774 .migrate = pfkey_send_migrate,
3777 static void __exit ipsec_pfkey_exit(void)
3779 xfrm_unregister_km(&pfkeyv2_mgr);
3780 remove_proc_entry("pfkey", init_net.proc_net);
3781 sock_unregister(PF_KEY);
3782 proto_unregister(&key_proto);
3785 static int __init ipsec_pfkey_init(void)
3787 int err = proto_register(&key_proto, 0);
3789 if (err != 0)
3790 goto out;
3792 err = sock_register(&pfkey_family_ops);
3793 if (err != 0)
3794 goto out_unregister_key_proto;
3795 #ifdef CONFIG_PROC_FS
3796 err = -ENOMEM;
3797 if (create_proc_read_entry("pfkey", 0, init_net.proc_net, pfkey_read_proc, NULL) == NULL)
3798 goto out_sock_unregister;
3799 #endif
3800 err = xfrm_register_km(&pfkeyv2_mgr);
3801 if (err != 0)
3802 goto out_remove_proc_entry;
3803 out:
3804 return err;
3805 out_remove_proc_entry:
3806 #ifdef CONFIG_PROC_FS
3807 remove_proc_entry("net/pfkey", NULL);
3808 out_sock_unregister:
3809 #endif
3810 sock_unregister(PF_KEY);
3811 out_unregister_key_proto:
3812 proto_unregister(&key_proto);
3813 goto out;
3816 module_init(ipsec_pfkey_init);
3817 module_exit(ipsec_pfkey_exit);
3818 MODULE_LICENSE("GPL");
3819 MODULE_ALIAS_NETPROTO(PF_KEY);