x86: cpu/common*.c, merge switch_to_new_gdt()
[linux-2.6/mini2440.git] / net / key / af_key.c
blobd628df97e02ed5b81002e725bdda6f200649fc08
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;
52 struct {
53 uint8_t msg_version;
54 uint32_t msg_pid;
55 int (*dump)(struct pfkey_sock *sk);
56 void (*done)(struct pfkey_sock *sk);
57 union {
58 struct xfrm_policy_walk policy;
59 struct xfrm_state_walk state;
60 } u;
61 } dump;
64 static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
66 return (struct pfkey_sock *)sk;
69 static int pfkey_can_dump(struct sock *sk)
71 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
72 return 1;
73 return 0;
76 static int pfkey_do_dump(struct pfkey_sock *pfk)
78 int rc;
80 rc = pfk->dump.dump(pfk);
81 if (rc == -ENOBUFS)
82 return 0;
84 pfk->dump.done(pfk);
85 pfk->dump.dump = NULL;
86 pfk->dump.done = NULL;
87 return rc;
90 static void pfkey_sock_destruct(struct sock *sk)
92 skb_queue_purge(&sk->sk_receive_queue);
94 if (!sock_flag(sk, SOCK_DEAD)) {
95 printk("Attempt to release alive pfkey socket: %p\n", sk);
96 return;
99 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
100 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
102 atomic_dec(&pfkey_socks_nr);
105 static void pfkey_table_grab(void)
107 write_lock_bh(&pfkey_table_lock);
109 if (atomic_read(&pfkey_table_users)) {
110 DECLARE_WAITQUEUE(wait, current);
112 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
113 for(;;) {
114 set_current_state(TASK_UNINTERRUPTIBLE);
115 if (atomic_read(&pfkey_table_users) == 0)
116 break;
117 write_unlock_bh(&pfkey_table_lock);
118 schedule();
119 write_lock_bh(&pfkey_table_lock);
122 __set_current_state(TASK_RUNNING);
123 remove_wait_queue(&pfkey_table_wait, &wait);
127 static __inline__ void pfkey_table_ungrab(void)
129 write_unlock_bh(&pfkey_table_lock);
130 wake_up(&pfkey_table_wait);
133 static __inline__ void pfkey_lock_table(void)
135 /* read_lock() synchronizes us to pfkey_table_grab */
137 read_lock(&pfkey_table_lock);
138 atomic_inc(&pfkey_table_users);
139 read_unlock(&pfkey_table_lock);
142 static __inline__ void pfkey_unlock_table(void)
144 if (atomic_dec_and_test(&pfkey_table_users))
145 wake_up(&pfkey_table_wait);
149 static const struct proto_ops pfkey_ops;
151 static void pfkey_insert(struct sock *sk)
153 pfkey_table_grab();
154 sk_add_node(sk, &pfkey_table);
155 pfkey_table_ungrab();
158 static void pfkey_remove(struct sock *sk)
160 pfkey_table_grab();
161 sk_del_node_init(sk);
162 pfkey_table_ungrab();
165 static struct proto key_proto = {
166 .name = "KEY",
167 .owner = THIS_MODULE,
168 .obj_size = sizeof(struct pfkey_sock),
171 static int pfkey_create(struct net *net, struct socket *sock, int protocol)
173 struct sock *sk;
174 int err;
176 if (net != &init_net)
177 return -EAFNOSUPPORT;
179 if (!capable(CAP_NET_ADMIN))
180 return -EPERM;
181 if (sock->type != SOCK_RAW)
182 return -ESOCKTNOSUPPORT;
183 if (protocol != PF_KEY_V2)
184 return -EPROTONOSUPPORT;
186 err = -ENOMEM;
187 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto);
188 if (sk == NULL)
189 goto out;
191 sock->ops = &pfkey_ops;
192 sock_init_data(sock, sk);
194 sk->sk_family = PF_KEY;
195 sk->sk_destruct = pfkey_sock_destruct;
197 atomic_inc(&pfkey_socks_nr);
199 pfkey_insert(sk);
201 return 0;
202 out:
203 return err;
206 static int pfkey_release(struct socket *sock)
208 struct sock *sk = sock->sk;
210 if (!sk)
211 return 0;
213 pfkey_remove(sk);
215 sock_orphan(sk);
216 sock->sk = NULL;
217 skb_queue_purge(&sk->sk_write_queue);
218 sock_put(sk);
220 return 0;
223 static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
224 gfp_t allocation, struct sock *sk)
226 int err = -ENOBUFS;
228 sock_hold(sk);
229 if (*skb2 == NULL) {
230 if (atomic_read(&skb->users) != 1) {
231 *skb2 = skb_clone(skb, allocation);
232 } else {
233 *skb2 = skb;
234 atomic_inc(&skb->users);
237 if (*skb2 != NULL) {
238 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
239 skb_orphan(*skb2);
240 skb_set_owner_r(*skb2, sk);
241 skb_queue_tail(&sk->sk_receive_queue, *skb2);
242 sk->sk_data_ready(sk, (*skb2)->len);
243 *skb2 = NULL;
244 err = 0;
247 sock_put(sk);
248 return err;
251 /* Send SKB to all pfkey sockets matching selected criteria. */
252 #define BROADCAST_ALL 0
253 #define BROADCAST_ONE 1
254 #define BROADCAST_REGISTERED 2
255 #define BROADCAST_PROMISC_ONLY 4
256 static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
257 int broadcast_flags, struct sock *one_sk)
259 struct sock *sk;
260 struct hlist_node *node;
261 struct sk_buff *skb2 = NULL;
262 int err = -ESRCH;
264 /* XXX Do we need something like netlink_overrun? I think
265 * XXX PF_KEY socket apps will not mind current behavior.
267 if (!skb)
268 return -ENOMEM;
270 pfkey_lock_table();
271 sk_for_each(sk, node, &pfkey_table) {
272 struct pfkey_sock *pfk = pfkey_sk(sk);
273 int err2;
275 /* Yes, it means that if you are meant to receive this
276 * pfkey message you receive it twice as promiscuous
277 * socket.
279 if (pfk->promisc)
280 pfkey_broadcast_one(skb, &skb2, allocation, sk);
282 /* the exact target will be processed later */
283 if (sk == one_sk)
284 continue;
285 if (broadcast_flags != BROADCAST_ALL) {
286 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
287 continue;
288 if ((broadcast_flags & BROADCAST_REGISTERED) &&
289 !pfk->registered)
290 continue;
291 if (broadcast_flags & BROADCAST_ONE)
292 continue;
295 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
297 /* Error is cleare after succecful sending to at least one
298 * registered KM */
299 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
300 err = err2;
302 pfkey_unlock_table();
304 if (one_sk != NULL)
305 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
307 if (skb2)
308 kfree_skb(skb2);
309 kfree_skb(skb);
310 return err;
313 static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
315 *new = *orig;
318 static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
320 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
321 struct sadb_msg *hdr;
323 if (!skb)
324 return -ENOBUFS;
326 /* Woe be to the platform trying to support PFKEY yet
327 * having normal errnos outside the 1-255 range, inclusive.
329 err = -err;
330 if (err == ERESTARTSYS ||
331 err == ERESTARTNOHAND ||
332 err == ERESTARTNOINTR)
333 err = EINTR;
334 if (err >= 512)
335 err = EINVAL;
336 BUG_ON(err <= 0 || err >= 256);
338 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
339 pfkey_hdr_dup(hdr, orig);
340 hdr->sadb_msg_errno = (uint8_t) err;
341 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
342 sizeof(uint64_t));
344 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);
346 return 0;
349 static u8 sadb_ext_min_len[] = {
350 [SADB_EXT_RESERVED] = (u8) 0,
351 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
352 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
353 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
354 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
355 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
356 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
357 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
358 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
359 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
360 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
361 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
362 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
363 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
364 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
365 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
366 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
367 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
368 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
369 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
370 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
371 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
372 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
373 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
374 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
377 /* Verify sadb_address_{len,prefixlen} against sa_family. */
378 static int verify_address_len(void *p)
380 struct sadb_address *sp = p;
381 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
382 struct sockaddr_in *sin;
383 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
384 struct sockaddr_in6 *sin6;
385 #endif
386 int len;
388 switch (addr->sa_family) {
389 case AF_INET:
390 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
391 if (sp->sadb_address_len != len ||
392 sp->sadb_address_prefixlen > 32)
393 return -EINVAL;
394 break;
395 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
396 case AF_INET6:
397 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
398 if (sp->sadb_address_len != len ||
399 sp->sadb_address_prefixlen > 128)
400 return -EINVAL;
401 break;
402 #endif
403 default:
404 /* It is user using kernel to keep track of security
405 * associations for another protocol, such as
406 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
407 * lengths.
409 * XXX Actually, association/policy database is not yet
410 * XXX able to cope with arbitrary sockaddr families.
411 * XXX When it can, remove this -EINVAL. -DaveM
413 return -EINVAL;
414 break;
417 return 0;
420 static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx)
422 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
423 sec_ctx->sadb_x_ctx_len,
424 sizeof(uint64_t));
427 static inline int verify_sec_ctx_len(void *p)
429 struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p;
430 int len = sec_ctx->sadb_x_ctx_len;
432 if (len > PAGE_SIZE)
433 return -EINVAL;
435 len = pfkey_sec_ctx_len(sec_ctx);
437 if (sec_ctx->sadb_x_sec_len != len)
438 return -EINVAL;
440 return 0;
443 static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx)
445 struct xfrm_user_sec_ctx *uctx = NULL;
446 int ctx_size = sec_ctx->sadb_x_ctx_len;
448 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
450 if (!uctx)
451 return NULL;
453 uctx->len = pfkey_sec_ctx_len(sec_ctx);
454 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
455 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
456 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
457 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
458 memcpy(uctx + 1, sec_ctx + 1,
459 uctx->ctx_len);
461 return uctx;
464 static int present_and_same_family(struct sadb_address *src,
465 struct sadb_address *dst)
467 struct sockaddr *s_addr, *d_addr;
469 if (!src || !dst)
470 return 0;
472 s_addr = (struct sockaddr *)(src + 1);
473 d_addr = (struct sockaddr *)(dst + 1);
474 if (s_addr->sa_family != d_addr->sa_family)
475 return 0;
476 if (s_addr->sa_family != AF_INET
477 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
478 && s_addr->sa_family != AF_INET6
479 #endif
481 return 0;
483 return 1;
486 static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
488 char *p = (char *) hdr;
489 int len = skb->len;
491 len -= sizeof(*hdr);
492 p += sizeof(*hdr);
493 while (len > 0) {
494 struct sadb_ext *ehdr = (struct sadb_ext *) p;
495 uint16_t ext_type;
496 int ext_len;
498 ext_len = ehdr->sadb_ext_len;
499 ext_len *= sizeof(uint64_t);
500 ext_type = ehdr->sadb_ext_type;
501 if (ext_len < sizeof(uint64_t) ||
502 ext_len > len ||
503 ext_type == SADB_EXT_RESERVED)
504 return -EINVAL;
506 if (ext_type <= SADB_EXT_MAX) {
507 int min = (int) sadb_ext_min_len[ext_type];
508 if (ext_len < min)
509 return -EINVAL;
510 if (ext_hdrs[ext_type-1] != NULL)
511 return -EINVAL;
512 if (ext_type == SADB_EXT_ADDRESS_SRC ||
513 ext_type == SADB_EXT_ADDRESS_DST ||
514 ext_type == SADB_EXT_ADDRESS_PROXY ||
515 ext_type == SADB_X_EXT_NAT_T_OA) {
516 if (verify_address_len(p))
517 return -EINVAL;
519 if (ext_type == SADB_X_EXT_SEC_CTX) {
520 if (verify_sec_ctx_len(p))
521 return -EINVAL;
523 ext_hdrs[ext_type-1] = p;
525 p += ext_len;
526 len -= ext_len;
529 return 0;
532 static uint16_t
533 pfkey_satype2proto(uint8_t satype)
535 switch (satype) {
536 case SADB_SATYPE_UNSPEC:
537 return IPSEC_PROTO_ANY;
538 case SADB_SATYPE_AH:
539 return IPPROTO_AH;
540 case SADB_SATYPE_ESP:
541 return IPPROTO_ESP;
542 case SADB_X_SATYPE_IPCOMP:
543 return IPPROTO_COMP;
544 break;
545 default:
546 return 0;
548 /* NOTREACHED */
551 static uint8_t
552 pfkey_proto2satype(uint16_t proto)
554 switch (proto) {
555 case IPPROTO_AH:
556 return SADB_SATYPE_AH;
557 case IPPROTO_ESP:
558 return SADB_SATYPE_ESP;
559 case IPPROTO_COMP:
560 return SADB_X_SATYPE_IPCOMP;
561 break;
562 default:
563 return 0;
565 /* NOTREACHED */
568 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
569 * say specifically 'just raw sockets' as we encode them as 255.
572 static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
574 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
577 static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
579 return (proto ? proto : IPSEC_PROTO_ANY);
582 static inline int pfkey_sockaddr_len(sa_family_t family)
584 switch (family) {
585 case AF_INET:
586 return sizeof(struct sockaddr_in);
587 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
588 case AF_INET6:
589 return sizeof(struct sockaddr_in6);
590 #endif
592 return 0;
595 static
596 int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
598 switch (sa->sa_family) {
599 case AF_INET:
600 xaddr->a4 =
601 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
602 return AF_INET;
603 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
604 case AF_INET6:
605 memcpy(xaddr->a6,
606 &((struct sockaddr_in6 *)sa)->sin6_addr,
607 sizeof(struct in6_addr));
608 return AF_INET6;
609 #endif
611 return 0;
614 static
615 int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr, xfrm_address_t *xaddr)
617 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
618 xaddr);
621 static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
623 struct sadb_sa *sa;
624 struct sadb_address *addr;
625 uint16_t proto;
626 unsigned short family;
627 xfrm_address_t *xaddr;
629 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
630 if (sa == NULL)
631 return NULL;
633 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
634 if (proto == 0)
635 return NULL;
637 /* sadb_address_len should be checked by caller */
638 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
639 if (addr == NULL)
640 return NULL;
642 family = ((struct sockaddr *)(addr + 1))->sa_family;
643 switch (family) {
644 case AF_INET:
645 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
646 break;
647 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
648 case AF_INET6:
649 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
650 break;
651 #endif
652 default:
653 xaddr = NULL;
656 if (!xaddr)
657 return NULL;
659 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
662 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
664 static int
665 pfkey_sockaddr_size(sa_family_t family)
667 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
670 static inline int pfkey_mode_from_xfrm(int mode)
672 switch(mode) {
673 case XFRM_MODE_TRANSPORT:
674 return IPSEC_MODE_TRANSPORT;
675 case XFRM_MODE_TUNNEL:
676 return IPSEC_MODE_TUNNEL;
677 case XFRM_MODE_BEET:
678 return IPSEC_MODE_BEET;
679 default:
680 return -1;
684 static inline int pfkey_mode_to_xfrm(int mode)
686 switch(mode) {
687 case IPSEC_MODE_ANY: /*XXX*/
688 case IPSEC_MODE_TRANSPORT:
689 return XFRM_MODE_TRANSPORT;
690 case IPSEC_MODE_TUNNEL:
691 return XFRM_MODE_TUNNEL;
692 case IPSEC_MODE_BEET:
693 return XFRM_MODE_BEET;
694 default:
695 return -1;
699 static unsigned int pfkey_sockaddr_fill(xfrm_address_t *xaddr, __be16 port,
700 struct sockaddr *sa,
701 unsigned short family)
703 switch (family) {
704 case AF_INET:
706 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
707 sin->sin_family = AF_INET;
708 sin->sin_port = port;
709 sin->sin_addr.s_addr = xaddr->a4;
710 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
711 return 32;
713 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
714 case AF_INET6:
716 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
717 sin6->sin6_family = AF_INET6;
718 sin6->sin6_port = port;
719 sin6->sin6_flowinfo = 0;
720 ipv6_addr_copy(&sin6->sin6_addr, (struct in6_addr *)xaddr->a6);
721 sin6->sin6_scope_id = 0;
722 return 128;
724 #endif
726 return 0;
729 static struct sk_buff *__pfkey_xfrm_state2msg(struct xfrm_state *x,
730 int add_keys, int hsc)
732 struct sk_buff *skb;
733 struct sadb_msg *hdr;
734 struct sadb_sa *sa;
735 struct sadb_lifetime *lifetime;
736 struct sadb_address *addr;
737 struct sadb_key *key;
738 struct sadb_x_sa2 *sa2;
739 struct sadb_x_sec_ctx *sec_ctx;
740 struct xfrm_sec_ctx *xfrm_ctx;
741 int ctx_size = 0;
742 int size;
743 int auth_key_size = 0;
744 int encrypt_key_size = 0;
745 int sockaddr_size;
746 struct xfrm_encap_tmpl *natt = NULL;
747 int mode;
749 /* address family check */
750 sockaddr_size = pfkey_sockaddr_size(x->props.family);
751 if (!sockaddr_size)
752 return ERR_PTR(-EINVAL);
754 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
755 key(AE), (identity(SD),) (sensitivity)> */
756 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
757 sizeof(struct sadb_lifetime) +
758 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
759 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
760 sizeof(struct sadb_address)*2 +
761 sockaddr_size*2 +
762 sizeof(struct sadb_x_sa2);
764 if ((xfrm_ctx = x->security)) {
765 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
766 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
769 /* identity & sensitivity */
770 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr, x->props.family))
771 size += sizeof(struct sadb_address) + sockaddr_size;
773 if (add_keys) {
774 if (x->aalg && x->aalg->alg_key_len) {
775 auth_key_size =
776 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
777 size += sizeof(struct sadb_key) + auth_key_size;
779 if (x->ealg && x->ealg->alg_key_len) {
780 encrypt_key_size =
781 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
782 size += sizeof(struct sadb_key) + encrypt_key_size;
785 if (x->encap)
786 natt = x->encap;
788 if (natt && natt->encap_type) {
789 size += sizeof(struct sadb_x_nat_t_type);
790 size += sizeof(struct sadb_x_nat_t_port);
791 size += sizeof(struct sadb_x_nat_t_port);
794 skb = alloc_skb(size + 16, GFP_ATOMIC);
795 if (skb == NULL)
796 return ERR_PTR(-ENOBUFS);
798 /* call should fill header later */
799 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
800 memset(hdr, 0, size); /* XXX do we need this ? */
801 hdr->sadb_msg_len = size / sizeof(uint64_t);
803 /* sa */
804 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
805 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
806 sa->sadb_sa_exttype = SADB_EXT_SA;
807 sa->sadb_sa_spi = x->id.spi;
808 sa->sadb_sa_replay = x->props.replay_window;
809 switch (x->km.state) {
810 case XFRM_STATE_VALID:
811 sa->sadb_sa_state = x->km.dying ?
812 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
813 break;
814 case XFRM_STATE_ACQ:
815 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
816 break;
817 default:
818 sa->sadb_sa_state = SADB_SASTATE_DEAD;
819 break;
821 sa->sadb_sa_auth = 0;
822 if (x->aalg) {
823 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
824 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
826 sa->sadb_sa_encrypt = 0;
827 BUG_ON(x->ealg && x->calg);
828 if (x->ealg) {
829 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
830 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
832 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
833 if (x->calg) {
834 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
835 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
838 sa->sadb_sa_flags = 0;
839 if (x->props.flags & XFRM_STATE_NOECN)
840 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
841 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
842 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
843 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
844 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
846 /* hard time */
847 if (hsc & 2) {
848 lifetime = (struct sadb_lifetime *) skb_put(skb,
849 sizeof(struct sadb_lifetime));
850 lifetime->sadb_lifetime_len =
851 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
852 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
853 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
854 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
855 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
856 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
858 /* soft time */
859 if (hsc & 1) {
860 lifetime = (struct sadb_lifetime *) skb_put(skb,
861 sizeof(struct sadb_lifetime));
862 lifetime->sadb_lifetime_len =
863 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
864 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
865 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
866 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
867 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
868 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
870 /* current time */
871 lifetime = (struct sadb_lifetime *) skb_put(skb,
872 sizeof(struct sadb_lifetime));
873 lifetime->sadb_lifetime_len =
874 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
875 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
876 lifetime->sadb_lifetime_allocations = x->curlft.packets;
877 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
878 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
879 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
880 /* src address */
881 addr = (struct sadb_address*) skb_put(skb,
882 sizeof(struct sadb_address)+sockaddr_size);
883 addr->sadb_address_len =
884 (sizeof(struct sadb_address)+sockaddr_size)/
885 sizeof(uint64_t);
886 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
887 /* "if the ports are non-zero, then the sadb_address_proto field,
888 normally zero, MUST be filled in with the transport
889 protocol's number." - RFC2367 */
890 addr->sadb_address_proto = 0;
891 addr->sadb_address_reserved = 0;
893 addr->sadb_address_prefixlen =
894 pfkey_sockaddr_fill(&x->props.saddr, 0,
895 (struct sockaddr *) (addr + 1),
896 x->props.family);
897 if (!addr->sadb_address_prefixlen)
898 BUG();
900 /* dst address */
901 addr = (struct sadb_address*) skb_put(skb,
902 sizeof(struct sadb_address)+sockaddr_size);
903 addr->sadb_address_len =
904 (sizeof(struct sadb_address)+sockaddr_size)/
905 sizeof(uint64_t);
906 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
907 addr->sadb_address_proto = 0;
908 addr->sadb_address_reserved = 0;
910 addr->sadb_address_prefixlen =
911 pfkey_sockaddr_fill(&x->id.daddr, 0,
912 (struct sockaddr *) (addr + 1),
913 x->props.family);
914 if (!addr->sadb_address_prefixlen)
915 BUG();
917 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr,
918 x->props.family)) {
919 addr = (struct sadb_address*) skb_put(skb,
920 sizeof(struct sadb_address)+sockaddr_size);
921 addr->sadb_address_len =
922 (sizeof(struct sadb_address)+sockaddr_size)/
923 sizeof(uint64_t);
924 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
925 addr->sadb_address_proto =
926 pfkey_proto_from_xfrm(x->sel.proto);
927 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
928 addr->sadb_address_reserved = 0;
930 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
931 (struct sockaddr *) (addr + 1),
932 x->props.family);
935 /* auth key */
936 if (add_keys && auth_key_size) {
937 key = (struct sadb_key *) skb_put(skb,
938 sizeof(struct sadb_key)+auth_key_size);
939 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
940 sizeof(uint64_t);
941 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
942 key->sadb_key_bits = x->aalg->alg_key_len;
943 key->sadb_key_reserved = 0;
944 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
946 /* encrypt key */
947 if (add_keys && encrypt_key_size) {
948 key = (struct sadb_key *) skb_put(skb,
949 sizeof(struct sadb_key)+encrypt_key_size);
950 key->sadb_key_len = (sizeof(struct sadb_key) +
951 encrypt_key_size) / sizeof(uint64_t);
952 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
953 key->sadb_key_bits = x->ealg->alg_key_len;
954 key->sadb_key_reserved = 0;
955 memcpy(key + 1, x->ealg->alg_key,
956 (x->ealg->alg_key_len+7)/8);
959 /* sa */
960 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
961 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
962 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
963 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
964 kfree_skb(skb);
965 return ERR_PTR(-EINVAL);
967 sa2->sadb_x_sa2_mode = mode;
968 sa2->sadb_x_sa2_reserved1 = 0;
969 sa2->sadb_x_sa2_reserved2 = 0;
970 sa2->sadb_x_sa2_sequence = 0;
971 sa2->sadb_x_sa2_reqid = x->props.reqid;
973 if (natt && natt->encap_type) {
974 struct sadb_x_nat_t_type *n_type;
975 struct sadb_x_nat_t_port *n_port;
977 /* type */
978 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
979 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
980 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
981 n_type->sadb_x_nat_t_type_type = natt->encap_type;
982 n_type->sadb_x_nat_t_type_reserved[0] = 0;
983 n_type->sadb_x_nat_t_type_reserved[1] = 0;
984 n_type->sadb_x_nat_t_type_reserved[2] = 0;
986 /* source port */
987 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
988 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
989 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
990 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
991 n_port->sadb_x_nat_t_port_reserved = 0;
993 /* dest port */
994 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
995 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
996 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
997 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
998 n_port->sadb_x_nat_t_port_reserved = 0;
1001 /* security context */
1002 if (xfrm_ctx) {
1003 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1004 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1005 sec_ctx->sadb_x_sec_len =
1006 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1007 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1008 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1009 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1010 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1011 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1012 xfrm_ctx->ctx_len);
1015 return skb;
1019 static inline struct sk_buff *pfkey_xfrm_state2msg(struct xfrm_state *x)
1021 struct sk_buff *skb;
1023 skb = __pfkey_xfrm_state2msg(x, 1, 3);
1025 return skb;
1028 static inline struct sk_buff *pfkey_xfrm_state2msg_expire(struct xfrm_state *x,
1029 int hsc)
1031 return __pfkey_xfrm_state2msg(x, 0, hsc);
1034 static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
1035 void **ext_hdrs)
1037 struct xfrm_state *x;
1038 struct sadb_lifetime *lifetime;
1039 struct sadb_sa *sa;
1040 struct sadb_key *key;
1041 struct sadb_x_sec_ctx *sec_ctx;
1042 uint16_t proto;
1043 int err;
1046 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
1047 if (!sa ||
1048 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1049 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1050 return ERR_PTR(-EINVAL);
1051 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1052 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1053 return ERR_PTR(-EINVAL);
1054 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1055 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1056 return ERR_PTR(-EINVAL);
1057 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1058 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1059 return ERR_PTR(-EINVAL);
1061 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1062 if (proto == 0)
1063 return ERR_PTR(-EINVAL);
1065 /* default error is no buffer space */
1066 err = -ENOBUFS;
1068 /* RFC2367:
1070 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1071 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1072 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1073 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1074 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1075 not true.
1077 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1078 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1080 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1081 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1082 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1083 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1084 return ERR_PTR(-EINVAL);
1085 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1086 if (key != NULL &&
1087 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1088 ((key->sadb_key_bits+7) / 8 == 0 ||
1089 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1090 return ERR_PTR(-EINVAL);
1091 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1092 if (key != NULL &&
1093 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1094 ((key->sadb_key_bits+7) / 8 == 0 ||
1095 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1096 return ERR_PTR(-EINVAL);
1098 x = xfrm_state_alloc();
1099 if (x == NULL)
1100 return ERR_PTR(-ENOBUFS);
1102 x->id.proto = proto;
1103 x->id.spi = sa->sadb_sa_spi;
1104 x->props.replay_window = sa->sadb_sa_replay;
1105 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1106 x->props.flags |= XFRM_STATE_NOECN;
1107 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1108 x->props.flags |= XFRM_STATE_DECAP_DSCP;
1109 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1110 x->props.flags |= XFRM_STATE_NOPMTUDISC;
1112 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
1113 if (lifetime != NULL) {
1114 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1115 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1116 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1117 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1119 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
1120 if (lifetime != NULL) {
1121 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1122 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1123 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1124 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1127 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
1128 if (sec_ctx != NULL) {
1129 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1131 if (!uctx)
1132 goto out;
1134 err = security_xfrm_state_alloc(x, uctx);
1135 kfree(uctx);
1137 if (err)
1138 goto out;
1141 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1142 if (sa->sadb_sa_auth) {
1143 int keysize = 0;
1144 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1145 if (!a) {
1146 err = -ENOSYS;
1147 goto out;
1149 if (key)
1150 keysize = (key->sadb_key_bits + 7) / 8;
1151 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1152 if (!x->aalg)
1153 goto out;
1154 strcpy(x->aalg->alg_name, a->name);
1155 x->aalg->alg_key_len = 0;
1156 if (key) {
1157 x->aalg->alg_key_len = key->sadb_key_bits;
1158 memcpy(x->aalg->alg_key, key+1, keysize);
1160 x->props.aalgo = sa->sadb_sa_auth;
1161 /* x->algo.flags = sa->sadb_sa_flags; */
1163 if (sa->sadb_sa_encrypt) {
1164 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1165 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1166 if (!a) {
1167 err = -ENOSYS;
1168 goto out;
1170 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1171 if (!x->calg)
1172 goto out;
1173 strcpy(x->calg->alg_name, a->name);
1174 x->props.calgo = sa->sadb_sa_encrypt;
1175 } else {
1176 int keysize = 0;
1177 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1178 if (!a) {
1179 err = -ENOSYS;
1180 goto out;
1182 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1183 if (key)
1184 keysize = (key->sadb_key_bits + 7) / 8;
1185 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1186 if (!x->ealg)
1187 goto out;
1188 strcpy(x->ealg->alg_name, a->name);
1189 x->ealg->alg_key_len = 0;
1190 if (key) {
1191 x->ealg->alg_key_len = key->sadb_key_bits;
1192 memcpy(x->ealg->alg_key, key+1, keysize);
1194 x->props.ealgo = sa->sadb_sa_encrypt;
1197 /* x->algo.flags = sa->sadb_sa_flags; */
1199 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1200 &x->props.saddr);
1201 if (!x->props.family) {
1202 err = -EAFNOSUPPORT;
1203 goto out;
1205 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1206 &x->id.daddr);
1208 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1209 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
1210 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1211 if (mode < 0) {
1212 err = -EINVAL;
1213 goto out;
1215 x->props.mode = mode;
1216 x->props.reqid = sa2->sadb_x_sa2_reqid;
1219 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1220 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1222 /* Nobody uses this, but we try. */
1223 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1224 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1227 if (!x->sel.family)
1228 x->sel.family = x->props.family;
1230 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1231 struct sadb_x_nat_t_type* n_type;
1232 struct xfrm_encap_tmpl *natt;
1234 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1235 if (!x->encap)
1236 goto out;
1238 natt = x->encap;
1239 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1240 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1242 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1243 struct sadb_x_nat_t_port* n_port =
1244 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1245 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1247 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1248 struct sadb_x_nat_t_port* n_port =
1249 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1250 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1254 err = xfrm_init_state(x);
1255 if (err)
1256 goto out;
1258 x->km.seq = hdr->sadb_msg_seq;
1259 return x;
1261 out:
1262 x->km.state = XFRM_STATE_DEAD;
1263 xfrm_state_put(x);
1264 return ERR_PTR(err);
1267 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1269 return -EOPNOTSUPP;
1272 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1274 struct sk_buff *resp_skb;
1275 struct sadb_x_sa2 *sa2;
1276 struct sadb_address *saddr, *daddr;
1277 struct sadb_msg *out_hdr;
1278 struct sadb_spirange *range;
1279 struct xfrm_state *x = NULL;
1280 int mode;
1281 int err;
1282 u32 min_spi, max_spi;
1283 u32 reqid;
1284 u8 proto;
1285 unsigned short family;
1286 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1288 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1289 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1290 return -EINVAL;
1292 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1293 if (proto == 0)
1294 return -EINVAL;
1296 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1297 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1298 if (mode < 0)
1299 return -EINVAL;
1300 reqid = sa2->sadb_x_sa2_reqid;
1301 } else {
1302 mode = 0;
1303 reqid = 0;
1306 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1307 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1309 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1310 switch (family) {
1311 case AF_INET:
1312 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1313 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1314 break;
1315 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1316 case AF_INET6:
1317 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1318 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1319 break;
1320 #endif
1323 if (hdr->sadb_msg_seq) {
1324 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1325 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1326 xfrm_state_put(x);
1327 x = NULL;
1331 if (!x)
1332 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1334 if (x == NULL)
1335 return -ENOENT;
1337 min_spi = 0x100;
1338 max_spi = 0x0fffffff;
1340 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1341 if (range) {
1342 min_spi = range->sadb_spirange_min;
1343 max_spi = range->sadb_spirange_max;
1346 err = xfrm_alloc_spi(x, min_spi, max_spi);
1347 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
1349 if (IS_ERR(resp_skb)) {
1350 xfrm_state_put(x);
1351 return PTR_ERR(resp_skb);
1354 out_hdr = (struct sadb_msg *) resp_skb->data;
1355 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1356 out_hdr->sadb_msg_type = SADB_GETSPI;
1357 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1358 out_hdr->sadb_msg_errno = 0;
1359 out_hdr->sadb_msg_reserved = 0;
1360 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1361 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1363 xfrm_state_put(x);
1365 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1367 return 0;
1370 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1372 struct xfrm_state *x;
1374 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1375 return -EOPNOTSUPP;
1377 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1378 return 0;
1380 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1381 if (x == NULL)
1382 return 0;
1384 spin_lock_bh(&x->lock);
1385 if (x->km.state == XFRM_STATE_ACQ) {
1386 x->km.state = XFRM_STATE_ERROR;
1387 wake_up(&km_waitq);
1389 spin_unlock_bh(&x->lock);
1390 xfrm_state_put(x);
1391 return 0;
1394 static inline int event2poltype(int event)
1396 switch (event) {
1397 case XFRM_MSG_DELPOLICY:
1398 return SADB_X_SPDDELETE;
1399 case XFRM_MSG_NEWPOLICY:
1400 return SADB_X_SPDADD;
1401 case XFRM_MSG_UPDPOLICY:
1402 return SADB_X_SPDUPDATE;
1403 case XFRM_MSG_POLEXPIRE:
1404 // return SADB_X_SPDEXPIRE;
1405 default:
1406 printk("pfkey: Unknown policy event %d\n", event);
1407 break;
1410 return 0;
1413 static inline int event2keytype(int event)
1415 switch (event) {
1416 case XFRM_MSG_DELSA:
1417 return SADB_DELETE;
1418 case XFRM_MSG_NEWSA:
1419 return SADB_ADD;
1420 case XFRM_MSG_UPDSA:
1421 return SADB_UPDATE;
1422 case XFRM_MSG_EXPIRE:
1423 return SADB_EXPIRE;
1424 default:
1425 printk("pfkey: Unknown SA event %d\n", event);
1426 break;
1429 return 0;
1432 /* ADD/UPD/DEL */
1433 static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1435 struct sk_buff *skb;
1436 struct sadb_msg *hdr;
1438 skb = pfkey_xfrm_state2msg(x);
1440 if (IS_ERR(skb))
1441 return PTR_ERR(skb);
1443 hdr = (struct sadb_msg *) skb->data;
1444 hdr->sadb_msg_version = PF_KEY_V2;
1445 hdr->sadb_msg_type = event2keytype(c->event);
1446 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1447 hdr->sadb_msg_errno = 0;
1448 hdr->sadb_msg_reserved = 0;
1449 hdr->sadb_msg_seq = c->seq;
1450 hdr->sadb_msg_pid = c->pid;
1452 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1454 return 0;
1457 static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1459 struct xfrm_state *x;
1460 int err;
1461 struct km_event c;
1463 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1464 if (IS_ERR(x))
1465 return PTR_ERR(x);
1467 xfrm_state_hold(x);
1468 if (hdr->sadb_msg_type == SADB_ADD)
1469 err = xfrm_state_add(x);
1470 else
1471 err = xfrm_state_update(x);
1473 xfrm_audit_state_add(x, err ? 0 : 1,
1474 audit_get_loginuid(current),
1475 audit_get_sessionid(current), 0);
1477 if (err < 0) {
1478 x->km.state = XFRM_STATE_DEAD;
1479 __xfrm_state_put(x);
1480 goto out;
1483 if (hdr->sadb_msg_type == SADB_ADD)
1484 c.event = XFRM_MSG_NEWSA;
1485 else
1486 c.event = XFRM_MSG_UPDSA;
1487 c.seq = hdr->sadb_msg_seq;
1488 c.pid = hdr->sadb_msg_pid;
1489 km_state_notify(x, &c);
1490 out:
1491 xfrm_state_put(x);
1492 return err;
1495 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1497 struct xfrm_state *x;
1498 struct km_event c;
1499 int err;
1501 if (!ext_hdrs[SADB_EXT_SA-1] ||
1502 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1503 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1504 return -EINVAL;
1506 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1507 if (x == NULL)
1508 return -ESRCH;
1510 if ((err = security_xfrm_state_delete(x)))
1511 goto out;
1513 if (xfrm_state_kern(x)) {
1514 err = -EPERM;
1515 goto out;
1518 err = xfrm_state_delete(x);
1520 if (err < 0)
1521 goto out;
1523 c.seq = hdr->sadb_msg_seq;
1524 c.pid = hdr->sadb_msg_pid;
1525 c.event = XFRM_MSG_DELSA;
1526 km_state_notify(x, &c);
1527 out:
1528 xfrm_audit_state_delete(x, err ? 0 : 1,
1529 audit_get_loginuid(current),
1530 audit_get_sessionid(current), 0);
1531 xfrm_state_put(x);
1533 return err;
1536 static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1538 __u8 proto;
1539 struct sk_buff *out_skb;
1540 struct sadb_msg *out_hdr;
1541 struct xfrm_state *x;
1543 if (!ext_hdrs[SADB_EXT_SA-1] ||
1544 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1545 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1546 return -EINVAL;
1548 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1549 if (x == NULL)
1550 return -ESRCH;
1552 out_skb = pfkey_xfrm_state2msg(x);
1553 proto = x->id.proto;
1554 xfrm_state_put(x);
1555 if (IS_ERR(out_skb))
1556 return PTR_ERR(out_skb);
1558 out_hdr = (struct sadb_msg *) out_skb->data;
1559 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1560 out_hdr->sadb_msg_type = SADB_GET;
1561 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1562 out_hdr->sadb_msg_errno = 0;
1563 out_hdr->sadb_msg_reserved = 0;
1564 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1565 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1566 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1568 return 0;
1571 static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
1572 gfp_t allocation)
1574 struct sk_buff *skb;
1575 struct sadb_msg *hdr;
1576 int len, auth_len, enc_len, i;
1578 auth_len = xfrm_count_auth_supported();
1579 if (auth_len) {
1580 auth_len *= sizeof(struct sadb_alg);
1581 auth_len += sizeof(struct sadb_supported);
1584 enc_len = xfrm_count_enc_supported();
1585 if (enc_len) {
1586 enc_len *= sizeof(struct sadb_alg);
1587 enc_len += sizeof(struct sadb_supported);
1590 len = enc_len + auth_len + sizeof(struct sadb_msg);
1592 skb = alloc_skb(len + 16, allocation);
1593 if (!skb)
1594 goto out_put_algs;
1596 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1597 pfkey_hdr_dup(hdr, orig);
1598 hdr->sadb_msg_errno = 0;
1599 hdr->sadb_msg_len = len / sizeof(uint64_t);
1601 if (auth_len) {
1602 struct sadb_supported *sp;
1603 struct sadb_alg *ap;
1605 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1606 ap = (struct sadb_alg *) (sp + 1);
1608 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1609 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1611 for (i = 0; ; i++) {
1612 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1613 if (!aalg)
1614 break;
1615 if (aalg->available)
1616 *ap++ = aalg->desc;
1620 if (enc_len) {
1621 struct sadb_supported *sp;
1622 struct sadb_alg *ap;
1624 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1625 ap = (struct sadb_alg *) (sp + 1);
1627 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1628 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1630 for (i = 0; ; i++) {
1631 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1632 if (!ealg)
1633 break;
1634 if (ealg->available)
1635 *ap++ = ealg->desc;
1639 out_put_algs:
1640 return skb;
1643 static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1645 struct pfkey_sock *pfk = pfkey_sk(sk);
1646 struct sk_buff *supp_skb;
1648 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1649 return -EINVAL;
1651 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1652 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1653 return -EEXIST;
1654 pfk->registered |= (1<<hdr->sadb_msg_satype);
1657 xfrm_probe_algs();
1659 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1660 if (!supp_skb) {
1661 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1662 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1664 return -ENOBUFS;
1667 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1669 return 0;
1672 static int key_notify_sa_flush(struct km_event *c)
1674 struct sk_buff *skb;
1675 struct sadb_msg *hdr;
1677 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1678 if (!skb)
1679 return -ENOBUFS;
1680 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1681 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
1682 hdr->sadb_msg_type = SADB_FLUSH;
1683 hdr->sadb_msg_seq = c->seq;
1684 hdr->sadb_msg_pid = c->pid;
1685 hdr->sadb_msg_version = PF_KEY_V2;
1686 hdr->sadb_msg_errno = (uint8_t) 0;
1687 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1689 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1691 return 0;
1694 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1696 unsigned proto;
1697 struct km_event c;
1698 struct xfrm_audit audit_info;
1699 int err;
1701 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1702 if (proto == 0)
1703 return -EINVAL;
1705 audit_info.loginuid = audit_get_loginuid(current);
1706 audit_info.sessionid = audit_get_sessionid(current);
1707 audit_info.secid = 0;
1708 err = xfrm_state_flush(proto, &audit_info);
1709 if (err)
1710 return err;
1711 c.data.proto = proto;
1712 c.seq = hdr->sadb_msg_seq;
1713 c.pid = hdr->sadb_msg_pid;
1714 c.event = XFRM_MSG_FLUSHSA;
1715 km_state_notify(NULL, &c);
1717 return 0;
1720 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1722 struct pfkey_sock *pfk = ptr;
1723 struct sk_buff *out_skb;
1724 struct sadb_msg *out_hdr;
1726 if (!pfkey_can_dump(&pfk->sk))
1727 return -ENOBUFS;
1729 out_skb = pfkey_xfrm_state2msg(x);
1730 if (IS_ERR(out_skb))
1731 return PTR_ERR(out_skb);
1733 out_hdr = (struct sadb_msg *) out_skb->data;
1734 out_hdr->sadb_msg_version = pfk->dump.msg_version;
1735 out_hdr->sadb_msg_type = SADB_DUMP;
1736 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1737 out_hdr->sadb_msg_errno = 0;
1738 out_hdr->sadb_msg_reserved = 0;
1739 out_hdr->sadb_msg_seq = count;
1740 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
1741 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, &pfk->sk);
1742 return 0;
1745 static int pfkey_dump_sa(struct pfkey_sock *pfk)
1747 return xfrm_state_walk(&pfk->dump.u.state, dump_sa, (void *) pfk);
1750 static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1752 xfrm_state_walk_done(&pfk->dump.u.state);
1755 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1757 u8 proto;
1758 struct pfkey_sock *pfk = pfkey_sk(sk);
1760 if (pfk->dump.dump != NULL)
1761 return -EBUSY;
1763 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1764 if (proto == 0)
1765 return -EINVAL;
1767 pfk->dump.msg_version = hdr->sadb_msg_version;
1768 pfk->dump.msg_pid = hdr->sadb_msg_pid;
1769 pfk->dump.dump = pfkey_dump_sa;
1770 pfk->dump.done = pfkey_dump_sa_done;
1771 xfrm_state_walk_init(&pfk->dump.u.state, proto);
1773 return pfkey_do_dump(pfk);
1776 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1778 struct pfkey_sock *pfk = pfkey_sk(sk);
1779 int satype = hdr->sadb_msg_satype;
1781 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1782 /* XXX we mangle packet... */
1783 hdr->sadb_msg_errno = 0;
1784 if (satype != 0 && satype != 1)
1785 return -EINVAL;
1786 pfk->promisc = satype;
1788 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1789 return 0;
1792 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1794 int i;
1795 u32 reqid = *(u32*)ptr;
1797 for (i=0; i<xp->xfrm_nr; i++) {
1798 if (xp->xfrm_vec[i].reqid == reqid)
1799 return -EEXIST;
1801 return 0;
1804 static u32 gen_reqid(void)
1806 struct xfrm_policy_walk walk;
1807 u32 start;
1808 int rc;
1809 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1811 start = reqid;
1812 do {
1813 ++reqid;
1814 if (reqid == 0)
1815 reqid = IPSEC_MANUAL_REQID_MAX+1;
1816 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
1817 rc = xfrm_policy_walk(&walk, check_reqid, (void*)&reqid);
1818 xfrm_policy_walk_done(&walk);
1819 if (rc != -EEXIST)
1820 return reqid;
1821 } while (reqid != start);
1822 return 0;
1825 static int
1826 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1828 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1829 int mode;
1831 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1832 return -ELOOP;
1834 if (rq->sadb_x_ipsecrequest_mode == 0)
1835 return -EINVAL;
1837 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1838 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1839 return -EINVAL;
1840 t->mode = mode;
1841 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1842 t->optional = 1;
1843 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1844 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1845 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1846 t->reqid = 0;
1847 if (!t->reqid && !(t->reqid = gen_reqid()))
1848 return -ENOBUFS;
1851 /* addresses present only in tunnel mode */
1852 if (t->mode == XFRM_MODE_TUNNEL) {
1853 u8 *sa = (u8 *) (rq + 1);
1854 int family, socklen;
1856 family = pfkey_sockaddr_extract((struct sockaddr *)sa,
1857 &t->saddr);
1858 if (!family)
1859 return -EINVAL;
1861 socklen = pfkey_sockaddr_len(family);
1862 if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen),
1863 &t->id.daddr) != family)
1864 return -EINVAL;
1865 t->encap_family = family;
1866 } else
1867 t->encap_family = xp->family;
1869 /* No way to set this via kame pfkey */
1870 t->allalgs = 1;
1871 xp->xfrm_nr++;
1872 return 0;
1875 static int
1876 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1878 int err;
1879 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1880 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1882 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1883 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1884 return err;
1885 len -= rq->sadb_x_ipsecrequest_len;
1886 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1888 return 0;
1891 static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1893 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1895 if (xfrm_ctx) {
1896 int len = sizeof(struct sadb_x_sec_ctx);
1897 len += xfrm_ctx->ctx_len;
1898 return PFKEY_ALIGN8(len);
1900 return 0;
1903 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1905 struct xfrm_tmpl *t;
1906 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1907 int socklen = 0;
1908 int i;
1910 for (i=0; i<xp->xfrm_nr; i++) {
1911 t = xp->xfrm_vec + i;
1912 socklen += pfkey_sockaddr_len(t->encap_family);
1915 return sizeof(struct sadb_msg) +
1916 (sizeof(struct sadb_lifetime) * 3) +
1917 (sizeof(struct sadb_address) * 2) +
1918 (sockaddr_size * 2) +
1919 sizeof(struct sadb_x_policy) +
1920 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1921 (socklen * 2) +
1922 pfkey_xfrm_policy2sec_ctx_size(xp);
1925 static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1927 struct sk_buff *skb;
1928 int size;
1930 size = pfkey_xfrm_policy2msg_size(xp);
1932 skb = alloc_skb(size + 16, GFP_ATOMIC);
1933 if (skb == NULL)
1934 return ERR_PTR(-ENOBUFS);
1936 return skb;
1939 static int pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1941 struct sadb_msg *hdr;
1942 struct sadb_address *addr;
1943 struct sadb_lifetime *lifetime;
1944 struct sadb_x_policy *pol;
1945 struct sadb_x_sec_ctx *sec_ctx;
1946 struct xfrm_sec_ctx *xfrm_ctx;
1947 int i;
1948 int size;
1949 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1950 int socklen = pfkey_sockaddr_len(xp->family);
1952 size = pfkey_xfrm_policy2msg_size(xp);
1954 /* call should fill header later */
1955 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1956 memset(hdr, 0, size); /* XXX do we need this ? */
1958 /* src address */
1959 addr = (struct sadb_address*) skb_put(skb,
1960 sizeof(struct sadb_address)+sockaddr_size);
1961 addr->sadb_address_len =
1962 (sizeof(struct sadb_address)+sockaddr_size)/
1963 sizeof(uint64_t);
1964 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1965 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1966 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1967 addr->sadb_address_reserved = 0;
1968 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
1969 xp->selector.sport,
1970 (struct sockaddr *) (addr + 1),
1971 xp->family))
1972 BUG();
1974 /* dst address */
1975 addr = (struct sadb_address*) skb_put(skb,
1976 sizeof(struct sadb_address)+sockaddr_size);
1977 addr->sadb_address_len =
1978 (sizeof(struct sadb_address)+sockaddr_size)/
1979 sizeof(uint64_t);
1980 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1981 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1982 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
1983 addr->sadb_address_reserved = 0;
1985 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
1986 (struct sockaddr *) (addr + 1),
1987 xp->family);
1989 /* hard time */
1990 lifetime = (struct sadb_lifetime *) skb_put(skb,
1991 sizeof(struct sadb_lifetime));
1992 lifetime->sadb_lifetime_len =
1993 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1994 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1995 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
1996 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
1997 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
1998 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
1999 /* soft time */
2000 lifetime = (struct sadb_lifetime *) skb_put(skb,
2001 sizeof(struct sadb_lifetime));
2002 lifetime->sadb_lifetime_len =
2003 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2004 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2005 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2006 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2007 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2008 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2009 /* current time */
2010 lifetime = (struct sadb_lifetime *) skb_put(skb,
2011 sizeof(struct sadb_lifetime));
2012 lifetime->sadb_lifetime_len =
2013 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2014 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2015 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2016 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2017 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2018 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2020 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2021 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2022 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2023 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2024 if (xp->action == XFRM_POLICY_ALLOW) {
2025 if (xp->xfrm_nr)
2026 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2027 else
2028 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2030 pol->sadb_x_policy_dir = dir+1;
2031 pol->sadb_x_policy_id = xp->index;
2032 pol->sadb_x_policy_priority = xp->priority;
2034 for (i=0; i<xp->xfrm_nr; i++) {
2035 struct sadb_x_ipsecrequest *rq;
2036 struct xfrm_tmpl *t = xp->xfrm_vec + i;
2037 int req_size;
2038 int mode;
2040 req_size = sizeof(struct sadb_x_ipsecrequest);
2041 if (t->mode == XFRM_MODE_TUNNEL) {
2042 socklen = pfkey_sockaddr_len(t->encap_family);
2043 req_size += socklen * 2;
2044 } else {
2045 size -= 2*socklen;
2046 socklen = 0;
2048 rq = (void*)skb_put(skb, req_size);
2049 pol->sadb_x_policy_len += req_size/8;
2050 memset(rq, 0, sizeof(*rq));
2051 rq->sadb_x_ipsecrequest_len = req_size;
2052 rq->sadb_x_ipsecrequest_proto = t->id.proto;
2053 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2054 return -EINVAL;
2055 rq->sadb_x_ipsecrequest_mode = mode;
2056 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2057 if (t->reqid)
2058 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2059 if (t->optional)
2060 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2061 rq->sadb_x_ipsecrequest_reqid = t->reqid;
2063 if (t->mode == XFRM_MODE_TUNNEL) {
2064 u8 *sa = (void *)(rq + 1);
2065 pfkey_sockaddr_fill(&t->saddr, 0,
2066 (struct sockaddr *)sa,
2067 t->encap_family);
2068 pfkey_sockaddr_fill(&t->id.daddr, 0,
2069 (struct sockaddr *) (sa + socklen),
2070 t->encap_family);
2074 /* security context */
2075 if ((xfrm_ctx = xp->security)) {
2076 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2078 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2079 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2080 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2081 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2082 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2083 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2084 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2085 xfrm_ctx->ctx_len);
2088 hdr->sadb_msg_len = size / sizeof(uint64_t);
2089 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
2091 return 0;
2094 static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2096 struct sk_buff *out_skb;
2097 struct sadb_msg *out_hdr;
2098 int err;
2100 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2101 if (IS_ERR(out_skb)) {
2102 err = PTR_ERR(out_skb);
2103 goto out;
2105 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2106 if (err < 0)
2107 return err;
2109 out_hdr = (struct sadb_msg *) out_skb->data;
2110 out_hdr->sadb_msg_version = PF_KEY_V2;
2112 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
2113 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2114 else
2115 out_hdr->sadb_msg_type = event2poltype(c->event);
2116 out_hdr->sadb_msg_errno = 0;
2117 out_hdr->sadb_msg_seq = c->seq;
2118 out_hdr->sadb_msg_pid = c->pid;
2119 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
2120 out:
2121 return 0;
2125 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2127 int err = 0;
2128 struct sadb_lifetime *lifetime;
2129 struct sadb_address *sa;
2130 struct sadb_x_policy *pol;
2131 struct xfrm_policy *xp;
2132 struct km_event c;
2133 struct sadb_x_sec_ctx *sec_ctx;
2135 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2136 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2137 !ext_hdrs[SADB_X_EXT_POLICY-1])
2138 return -EINVAL;
2140 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2141 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2142 return -EINVAL;
2143 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2144 return -EINVAL;
2146 xp = xfrm_policy_alloc(GFP_KERNEL);
2147 if (xp == NULL)
2148 return -ENOBUFS;
2150 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2151 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2152 xp->priority = pol->sadb_x_policy_priority;
2154 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2155 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2156 if (!xp->family) {
2157 err = -EINVAL;
2158 goto out;
2160 xp->selector.family = xp->family;
2161 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2162 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2163 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2164 if (xp->selector.sport)
2165 xp->selector.sport_mask = htons(0xffff);
2167 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2168 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2169 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2171 /* Amusing, we set this twice. KAME apps appear to set same value
2172 * in both addresses.
2174 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2176 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2177 if (xp->selector.dport)
2178 xp->selector.dport_mask = htons(0xffff);
2180 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2181 if (sec_ctx != NULL) {
2182 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2184 if (!uctx) {
2185 err = -ENOBUFS;
2186 goto out;
2189 err = security_xfrm_policy_alloc(&xp->security, uctx);
2190 kfree(uctx);
2192 if (err)
2193 goto out;
2196 xp->lft.soft_byte_limit = XFRM_INF;
2197 xp->lft.hard_byte_limit = XFRM_INF;
2198 xp->lft.soft_packet_limit = XFRM_INF;
2199 xp->lft.hard_packet_limit = XFRM_INF;
2200 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2201 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2202 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2203 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2204 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2206 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2207 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2208 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2209 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2210 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2212 xp->xfrm_nr = 0;
2213 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2214 (err = parse_ipsecrequests(xp, pol)) < 0)
2215 goto out;
2217 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2218 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2220 xfrm_audit_policy_add(xp, err ? 0 : 1,
2221 audit_get_loginuid(current),
2222 audit_get_sessionid(current), 0);
2224 if (err)
2225 goto out;
2227 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2228 c.event = XFRM_MSG_UPDPOLICY;
2229 else
2230 c.event = XFRM_MSG_NEWPOLICY;
2232 c.seq = hdr->sadb_msg_seq;
2233 c.pid = hdr->sadb_msg_pid;
2235 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2236 xfrm_pol_put(xp);
2237 return 0;
2239 out:
2240 xp->dead = 1;
2241 xfrm_policy_destroy(xp);
2242 return err;
2245 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2247 int err;
2248 struct sadb_address *sa;
2249 struct sadb_x_policy *pol;
2250 struct xfrm_policy *xp;
2251 struct xfrm_selector sel;
2252 struct km_event c;
2253 struct sadb_x_sec_ctx *sec_ctx;
2254 struct xfrm_sec_ctx *pol_ctx = NULL;
2256 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2257 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2258 !ext_hdrs[SADB_X_EXT_POLICY-1])
2259 return -EINVAL;
2261 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2262 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2263 return -EINVAL;
2265 memset(&sel, 0, sizeof(sel));
2267 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2268 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2269 sel.prefixlen_s = sa->sadb_address_prefixlen;
2270 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2271 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2272 if (sel.sport)
2273 sel.sport_mask = htons(0xffff);
2275 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2276 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2277 sel.prefixlen_d = sa->sadb_address_prefixlen;
2278 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2279 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2280 if (sel.dport)
2281 sel.dport_mask = htons(0xffff);
2283 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2284 if (sec_ctx != NULL) {
2285 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2287 if (!uctx)
2288 return -ENOMEM;
2290 err = security_xfrm_policy_alloc(&pol_ctx, uctx);
2291 kfree(uctx);
2292 if (err)
2293 return err;
2296 xp = xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN,
2297 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2298 1, &err);
2299 security_xfrm_policy_free(pol_ctx);
2300 if (xp == NULL)
2301 return -ENOENT;
2303 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2304 audit_get_loginuid(current),
2305 audit_get_sessionid(current), 0);
2307 if (err)
2308 goto out;
2310 c.seq = hdr->sadb_msg_seq;
2311 c.pid = hdr->sadb_msg_pid;
2312 c.event = XFRM_MSG_DELPOLICY;
2313 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2315 out:
2316 xfrm_pol_put(xp);
2317 return err;
2320 static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2322 int err;
2323 struct sk_buff *out_skb;
2324 struct sadb_msg *out_hdr;
2325 err = 0;
2327 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2328 if (IS_ERR(out_skb)) {
2329 err = PTR_ERR(out_skb);
2330 goto out;
2332 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2333 if (err < 0)
2334 goto out;
2336 out_hdr = (struct sadb_msg *) out_skb->data;
2337 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2338 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2339 out_hdr->sadb_msg_satype = 0;
2340 out_hdr->sadb_msg_errno = 0;
2341 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2342 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2343 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
2344 err = 0;
2346 out:
2347 return err;
2350 #ifdef CONFIG_NET_KEY_MIGRATE
2351 static int pfkey_sockaddr_pair_size(sa_family_t family)
2353 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
2356 static int parse_sockaddr_pair(struct sadb_x_ipsecrequest *rq,
2357 xfrm_address_t *saddr, xfrm_address_t *daddr,
2358 u16 *family)
2360 u8 *sa = (u8 *) (rq + 1);
2361 int af, socklen;
2363 if (rq->sadb_x_ipsecrequest_len <
2364 pfkey_sockaddr_pair_size(((struct sockaddr *)sa)->sa_family))
2365 return -EINVAL;
2367 af = pfkey_sockaddr_extract((struct sockaddr *) sa,
2368 saddr);
2369 if (!af)
2370 return -EINVAL;
2372 socklen = pfkey_sockaddr_len(af);
2373 if (pfkey_sockaddr_extract((struct sockaddr *) (sa + socklen),
2374 daddr) != af)
2375 return -EINVAL;
2377 *family = af;
2378 return 0;
2381 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2382 struct xfrm_migrate *m)
2384 int err;
2385 struct sadb_x_ipsecrequest *rq2;
2386 int mode;
2388 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2389 len < rq1->sadb_x_ipsecrequest_len)
2390 return -EINVAL;
2392 /* old endoints */
2393 err = parse_sockaddr_pair(rq1, &m->old_saddr, &m->old_daddr,
2394 &m->old_family);
2395 if (err)
2396 return err;
2398 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2399 len -= rq1->sadb_x_ipsecrequest_len;
2401 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2402 len < rq2->sadb_x_ipsecrequest_len)
2403 return -EINVAL;
2405 /* new endpoints */
2406 err = parse_sockaddr_pair(rq2, &m->new_saddr, &m->new_daddr,
2407 &m->new_family);
2408 if (err)
2409 return err;
2411 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2412 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2413 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2414 return -EINVAL;
2416 m->proto = rq1->sadb_x_ipsecrequest_proto;
2417 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2418 return -EINVAL;
2419 m->mode = mode;
2420 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2422 return ((int)(rq1->sadb_x_ipsecrequest_len +
2423 rq2->sadb_x_ipsecrequest_len));
2426 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2427 struct sadb_msg *hdr, void **ext_hdrs)
2429 int i, len, ret, err = -EINVAL;
2430 u8 dir;
2431 struct sadb_address *sa;
2432 struct sadb_x_policy *pol;
2433 struct sadb_x_ipsecrequest *rq;
2434 struct xfrm_selector sel;
2435 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2437 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2438 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2439 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2440 err = -EINVAL;
2441 goto out;
2444 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2445 if (!pol) {
2446 err = -EINVAL;
2447 goto out;
2450 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2451 err = -EINVAL;
2452 goto out;
2455 dir = pol->sadb_x_policy_dir - 1;
2456 memset(&sel, 0, sizeof(sel));
2458 /* set source address info of selector */
2459 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2460 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2461 sel.prefixlen_s = sa->sadb_address_prefixlen;
2462 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2463 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2464 if (sel.sport)
2465 sel.sport_mask = htons(0xffff);
2467 /* set destination address info of selector */
2468 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2469 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2470 sel.prefixlen_d = sa->sadb_address_prefixlen;
2471 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2472 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2473 if (sel.dport)
2474 sel.dport_mask = htons(0xffff);
2476 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2478 /* extract ipsecrequests */
2479 i = 0;
2480 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2482 while (len > 0 && i < XFRM_MAX_DEPTH) {
2483 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2484 if (ret < 0) {
2485 err = ret;
2486 goto out;
2487 } else {
2488 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2489 len -= ret;
2490 i++;
2494 if (!i || len > 0) {
2495 err = -EINVAL;
2496 goto out;
2499 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i);
2501 out:
2502 return err;
2504 #else
2505 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2506 struct sadb_msg *hdr, void **ext_hdrs)
2508 return -ENOPROTOOPT;
2510 #endif
2513 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2515 unsigned int dir;
2516 int err = 0, delete;
2517 struct sadb_x_policy *pol;
2518 struct xfrm_policy *xp;
2519 struct km_event c;
2521 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2522 return -EINVAL;
2524 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2525 if (dir >= XFRM_POLICY_MAX)
2526 return -EINVAL;
2528 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2529 xp = xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN, dir, pol->sadb_x_policy_id,
2530 delete, &err);
2531 if (xp == NULL)
2532 return -ENOENT;
2534 if (delete) {
2535 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2536 audit_get_loginuid(current),
2537 audit_get_sessionid(current), 0);
2539 if (err)
2540 goto out;
2541 c.seq = hdr->sadb_msg_seq;
2542 c.pid = hdr->sadb_msg_pid;
2543 c.data.byid = 1;
2544 c.event = XFRM_MSG_DELPOLICY;
2545 km_policy_notify(xp, dir, &c);
2546 } else {
2547 err = key_pol_get_resp(sk, xp, hdr, dir);
2550 out:
2551 xfrm_pol_put(xp);
2552 return err;
2555 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2557 struct pfkey_sock *pfk = ptr;
2558 struct sk_buff *out_skb;
2559 struct sadb_msg *out_hdr;
2560 int err;
2562 if (!pfkey_can_dump(&pfk->sk))
2563 return -ENOBUFS;
2565 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2566 if (IS_ERR(out_skb))
2567 return PTR_ERR(out_skb);
2569 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2570 if (err < 0)
2571 return err;
2573 out_hdr = (struct sadb_msg *) out_skb->data;
2574 out_hdr->sadb_msg_version = pfk->dump.msg_version;
2575 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2576 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2577 out_hdr->sadb_msg_errno = 0;
2578 out_hdr->sadb_msg_seq = count;
2579 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
2580 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, &pfk->sk);
2581 return 0;
2584 static int pfkey_dump_sp(struct pfkey_sock *pfk)
2586 return xfrm_policy_walk(&pfk->dump.u.policy, dump_sp, (void *) pfk);
2589 static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2591 xfrm_policy_walk_done(&pfk->dump.u.policy);
2594 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2596 struct pfkey_sock *pfk = pfkey_sk(sk);
2598 if (pfk->dump.dump != NULL)
2599 return -EBUSY;
2601 pfk->dump.msg_version = hdr->sadb_msg_version;
2602 pfk->dump.msg_pid = hdr->sadb_msg_pid;
2603 pfk->dump.dump = pfkey_dump_sp;
2604 pfk->dump.done = pfkey_dump_sp_done;
2605 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2607 return pfkey_do_dump(pfk);
2610 static int key_notify_policy_flush(struct km_event *c)
2612 struct sk_buff *skb_out;
2613 struct sadb_msg *hdr;
2615 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
2616 if (!skb_out)
2617 return -ENOBUFS;
2618 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2619 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
2620 hdr->sadb_msg_seq = c->seq;
2621 hdr->sadb_msg_pid = c->pid;
2622 hdr->sadb_msg_version = PF_KEY_V2;
2623 hdr->sadb_msg_errno = (uint8_t) 0;
2624 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2625 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2626 return 0;
2630 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2632 struct km_event c;
2633 struct xfrm_audit audit_info;
2634 int err;
2636 audit_info.loginuid = audit_get_loginuid(current);
2637 audit_info.sessionid = audit_get_sessionid(current);
2638 audit_info.secid = 0;
2639 err = xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN, &audit_info);
2640 if (err)
2641 return err;
2642 c.data.type = XFRM_POLICY_TYPE_MAIN;
2643 c.event = XFRM_MSG_FLUSHPOLICY;
2644 c.pid = hdr->sadb_msg_pid;
2645 c.seq = hdr->sadb_msg_seq;
2646 km_policy_notify(NULL, 0, &c);
2648 return 0;
2651 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2652 struct sadb_msg *hdr, void **ext_hdrs);
2653 static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2654 [SADB_RESERVED] = pfkey_reserved,
2655 [SADB_GETSPI] = pfkey_getspi,
2656 [SADB_UPDATE] = pfkey_add,
2657 [SADB_ADD] = pfkey_add,
2658 [SADB_DELETE] = pfkey_delete,
2659 [SADB_GET] = pfkey_get,
2660 [SADB_ACQUIRE] = pfkey_acquire,
2661 [SADB_REGISTER] = pfkey_register,
2662 [SADB_EXPIRE] = NULL,
2663 [SADB_FLUSH] = pfkey_flush,
2664 [SADB_DUMP] = pfkey_dump,
2665 [SADB_X_PROMISC] = pfkey_promisc,
2666 [SADB_X_PCHANGE] = NULL,
2667 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2668 [SADB_X_SPDADD] = pfkey_spdadd,
2669 [SADB_X_SPDDELETE] = pfkey_spddelete,
2670 [SADB_X_SPDGET] = pfkey_spdget,
2671 [SADB_X_SPDACQUIRE] = NULL,
2672 [SADB_X_SPDDUMP] = pfkey_spddump,
2673 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2674 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2675 [SADB_X_SPDDELETE2] = pfkey_spdget,
2676 [SADB_X_MIGRATE] = pfkey_migrate,
2679 static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2681 void *ext_hdrs[SADB_EXT_MAX];
2682 int err;
2684 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2685 BROADCAST_PROMISC_ONLY, NULL);
2687 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2688 err = parse_exthdrs(skb, hdr, ext_hdrs);
2689 if (!err) {
2690 err = -EOPNOTSUPP;
2691 if (pfkey_funcs[hdr->sadb_msg_type])
2692 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2694 return err;
2697 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2699 struct sadb_msg *hdr = NULL;
2701 if (skb->len < sizeof(*hdr)) {
2702 *errp = -EMSGSIZE;
2703 } else {
2704 hdr = (struct sadb_msg *) skb->data;
2705 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2706 hdr->sadb_msg_reserved != 0 ||
2707 (hdr->sadb_msg_type <= SADB_RESERVED ||
2708 hdr->sadb_msg_type > SADB_MAX)) {
2709 hdr = NULL;
2710 *errp = -EINVAL;
2711 } else if (hdr->sadb_msg_len != (skb->len /
2712 sizeof(uint64_t)) ||
2713 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2714 sizeof(uint64_t))) {
2715 hdr = NULL;
2716 *errp = -EMSGSIZE;
2717 } else {
2718 *errp = 0;
2721 return hdr;
2724 static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2726 unsigned int id = d->desc.sadb_alg_id;
2728 if (id >= sizeof(t->aalgos) * 8)
2729 return 0;
2731 return (t->aalgos >> id) & 1;
2734 static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2736 unsigned int id = d->desc.sadb_alg_id;
2738 if (id >= sizeof(t->ealgos) * 8)
2739 return 0;
2741 return (t->ealgos >> id) & 1;
2744 static int count_ah_combs(struct xfrm_tmpl *t)
2746 int i, sz = 0;
2748 for (i = 0; ; i++) {
2749 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2750 if (!aalg)
2751 break;
2752 if (aalg_tmpl_set(t, aalg) && aalg->available)
2753 sz += sizeof(struct sadb_comb);
2755 return sz + sizeof(struct sadb_prop);
2758 static int count_esp_combs(struct xfrm_tmpl *t)
2760 int i, k, sz = 0;
2762 for (i = 0; ; i++) {
2763 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2764 if (!ealg)
2765 break;
2767 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2768 continue;
2770 for (k = 1; ; k++) {
2771 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2772 if (!aalg)
2773 break;
2775 if (aalg_tmpl_set(t, aalg) && aalg->available)
2776 sz += sizeof(struct sadb_comb);
2779 return sz + sizeof(struct sadb_prop);
2782 static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2784 struct sadb_prop *p;
2785 int i;
2787 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2788 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2789 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2790 p->sadb_prop_replay = 32;
2791 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2793 for (i = 0; ; i++) {
2794 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2795 if (!aalg)
2796 break;
2798 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2799 struct sadb_comb *c;
2800 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2801 memset(c, 0, sizeof(*c));
2802 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2803 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2804 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2805 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2806 c->sadb_comb_hard_addtime = 24*60*60;
2807 c->sadb_comb_soft_addtime = 20*60*60;
2808 c->sadb_comb_hard_usetime = 8*60*60;
2809 c->sadb_comb_soft_usetime = 7*60*60;
2814 static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2816 struct sadb_prop *p;
2817 int i, k;
2819 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2820 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2821 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2822 p->sadb_prop_replay = 32;
2823 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2825 for (i=0; ; i++) {
2826 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2827 if (!ealg)
2828 break;
2830 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2831 continue;
2833 for (k = 1; ; k++) {
2834 struct sadb_comb *c;
2835 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2836 if (!aalg)
2837 break;
2838 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2839 continue;
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_encrypt = ealg->desc.sadb_alg_id;
2847 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2848 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2849 c->sadb_comb_hard_addtime = 24*60*60;
2850 c->sadb_comb_soft_addtime = 20*60*60;
2851 c->sadb_comb_hard_usetime = 8*60*60;
2852 c->sadb_comb_soft_usetime = 7*60*60;
2857 static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2859 return 0;
2862 static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
2864 struct sk_buff *out_skb;
2865 struct sadb_msg *out_hdr;
2866 int hard;
2867 int hsc;
2869 hard = c->data.hard;
2870 if (hard)
2871 hsc = 2;
2872 else
2873 hsc = 1;
2875 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
2876 if (IS_ERR(out_skb))
2877 return PTR_ERR(out_skb);
2879 out_hdr = (struct sadb_msg *) out_skb->data;
2880 out_hdr->sadb_msg_version = PF_KEY_V2;
2881 out_hdr->sadb_msg_type = SADB_EXPIRE;
2882 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2883 out_hdr->sadb_msg_errno = 0;
2884 out_hdr->sadb_msg_reserved = 0;
2885 out_hdr->sadb_msg_seq = 0;
2886 out_hdr->sadb_msg_pid = 0;
2888 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2889 return 0;
2892 static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2894 if (atomic_read(&pfkey_socks_nr) == 0)
2895 return 0;
2897 switch (c->event) {
2898 case XFRM_MSG_EXPIRE:
2899 return key_notify_sa_expire(x, c);
2900 case XFRM_MSG_DELSA:
2901 case XFRM_MSG_NEWSA:
2902 case XFRM_MSG_UPDSA:
2903 return key_notify_sa(x, c);
2904 case XFRM_MSG_FLUSHSA:
2905 return key_notify_sa_flush(c);
2906 case XFRM_MSG_NEWAE: /* not yet supported */
2907 break;
2908 default:
2909 printk("pfkey: Unknown SA event %d\n", c->event);
2910 break;
2913 return 0;
2916 static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2918 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
2919 return 0;
2921 switch (c->event) {
2922 case XFRM_MSG_POLEXPIRE:
2923 return key_notify_policy_expire(xp, c);
2924 case XFRM_MSG_DELPOLICY:
2925 case XFRM_MSG_NEWPOLICY:
2926 case XFRM_MSG_UPDPOLICY:
2927 return key_notify_policy(xp, dir, c);
2928 case XFRM_MSG_FLUSHPOLICY:
2929 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
2930 break;
2931 return key_notify_policy_flush(c);
2932 default:
2933 printk("pfkey: Unknown policy event %d\n", c->event);
2934 break;
2937 return 0;
2940 static u32 get_acqseq(void)
2942 u32 res;
2943 static u32 acqseq;
2944 static DEFINE_SPINLOCK(acqseq_lock);
2946 spin_lock_bh(&acqseq_lock);
2947 res = (++acqseq ? : ++acqseq);
2948 spin_unlock_bh(&acqseq_lock);
2949 return res;
2952 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2954 struct sk_buff *skb;
2955 struct sadb_msg *hdr;
2956 struct sadb_address *addr;
2957 struct sadb_x_policy *pol;
2958 int sockaddr_size;
2959 int size;
2960 struct sadb_x_sec_ctx *sec_ctx;
2961 struct xfrm_sec_ctx *xfrm_ctx;
2962 int ctx_size = 0;
2964 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2965 if (!sockaddr_size)
2966 return -EINVAL;
2968 size = sizeof(struct sadb_msg) +
2969 (sizeof(struct sadb_address) * 2) +
2970 (sockaddr_size * 2) +
2971 sizeof(struct sadb_x_policy);
2973 if (x->id.proto == IPPROTO_AH)
2974 size += count_ah_combs(t);
2975 else if (x->id.proto == IPPROTO_ESP)
2976 size += count_esp_combs(t);
2978 if ((xfrm_ctx = x->security)) {
2979 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
2980 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
2983 skb = alloc_skb(size + 16, GFP_ATOMIC);
2984 if (skb == NULL)
2985 return -ENOMEM;
2987 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2988 hdr->sadb_msg_version = PF_KEY_V2;
2989 hdr->sadb_msg_type = SADB_ACQUIRE;
2990 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2991 hdr->sadb_msg_len = size / sizeof(uint64_t);
2992 hdr->sadb_msg_errno = 0;
2993 hdr->sadb_msg_reserved = 0;
2994 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2995 hdr->sadb_msg_pid = 0;
2997 /* src address */
2998 addr = (struct sadb_address*) skb_put(skb,
2999 sizeof(struct sadb_address)+sockaddr_size);
3000 addr->sadb_address_len =
3001 (sizeof(struct sadb_address)+sockaddr_size)/
3002 sizeof(uint64_t);
3003 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3004 addr->sadb_address_proto = 0;
3005 addr->sadb_address_reserved = 0;
3006 addr->sadb_address_prefixlen =
3007 pfkey_sockaddr_fill(&x->props.saddr, 0,
3008 (struct sockaddr *) (addr + 1),
3009 x->props.family);
3010 if (!addr->sadb_address_prefixlen)
3011 BUG();
3013 /* dst address */
3014 addr = (struct sadb_address*) skb_put(skb,
3015 sizeof(struct sadb_address)+sockaddr_size);
3016 addr->sadb_address_len =
3017 (sizeof(struct sadb_address)+sockaddr_size)/
3018 sizeof(uint64_t);
3019 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3020 addr->sadb_address_proto = 0;
3021 addr->sadb_address_reserved = 0;
3022 addr->sadb_address_prefixlen =
3023 pfkey_sockaddr_fill(&x->id.daddr, 0,
3024 (struct sockaddr *) (addr + 1),
3025 x->props.family);
3026 if (!addr->sadb_address_prefixlen)
3027 BUG();
3029 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3030 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3031 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3032 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3033 pol->sadb_x_policy_dir = dir+1;
3034 pol->sadb_x_policy_id = xp->index;
3036 /* Set sadb_comb's. */
3037 if (x->id.proto == IPPROTO_AH)
3038 dump_ah_combs(skb, t);
3039 else if (x->id.proto == IPPROTO_ESP)
3040 dump_esp_combs(skb, t);
3042 /* security context */
3043 if (xfrm_ctx) {
3044 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3045 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3046 sec_ctx->sadb_x_sec_len =
3047 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3048 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3049 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3050 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3051 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3052 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3053 xfrm_ctx->ctx_len);
3056 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3059 static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
3060 u8 *data, int len, int *dir)
3062 struct xfrm_policy *xp;
3063 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
3064 struct sadb_x_sec_ctx *sec_ctx;
3066 switch (sk->sk_family) {
3067 case AF_INET:
3068 if (opt != IP_IPSEC_POLICY) {
3069 *dir = -EOPNOTSUPP;
3070 return NULL;
3072 break;
3073 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3074 case AF_INET6:
3075 if (opt != IPV6_IPSEC_POLICY) {
3076 *dir = -EOPNOTSUPP;
3077 return NULL;
3079 break;
3080 #endif
3081 default:
3082 *dir = -EINVAL;
3083 return NULL;
3086 *dir = -EINVAL;
3088 if (len < sizeof(struct sadb_x_policy) ||
3089 pol->sadb_x_policy_len*8 > len ||
3090 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3091 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3092 return NULL;
3094 xp = xfrm_policy_alloc(GFP_ATOMIC);
3095 if (xp == NULL) {
3096 *dir = -ENOBUFS;
3097 return NULL;
3100 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3101 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3103 xp->lft.soft_byte_limit = XFRM_INF;
3104 xp->lft.hard_byte_limit = XFRM_INF;
3105 xp->lft.soft_packet_limit = XFRM_INF;
3106 xp->lft.hard_packet_limit = XFRM_INF;
3107 xp->family = sk->sk_family;
3109 xp->xfrm_nr = 0;
3110 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3111 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3112 goto out;
3114 /* security context too */
3115 if (len >= (pol->sadb_x_policy_len*8 +
3116 sizeof(struct sadb_x_sec_ctx))) {
3117 char *p = (char *)pol;
3118 struct xfrm_user_sec_ctx *uctx;
3120 p += pol->sadb_x_policy_len*8;
3121 sec_ctx = (struct sadb_x_sec_ctx *)p;
3122 if (len < pol->sadb_x_policy_len*8 +
3123 sec_ctx->sadb_x_sec_len) {
3124 *dir = -EINVAL;
3125 goto out;
3127 if ((*dir = verify_sec_ctx_len(p)))
3128 goto out;
3129 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
3130 *dir = security_xfrm_policy_alloc(&xp->security, uctx);
3131 kfree(uctx);
3133 if (*dir)
3134 goto out;
3137 *dir = pol->sadb_x_policy_dir-1;
3138 return xp;
3140 out:
3141 xfrm_policy_destroy(xp);
3142 return NULL;
3145 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
3147 struct sk_buff *skb;
3148 struct sadb_msg *hdr;
3149 struct sadb_sa *sa;
3150 struct sadb_address *addr;
3151 struct sadb_x_nat_t_port *n_port;
3152 int sockaddr_size;
3153 int size;
3154 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3155 struct xfrm_encap_tmpl *natt = NULL;
3157 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3158 if (!sockaddr_size)
3159 return -EINVAL;
3161 if (!satype)
3162 return -EINVAL;
3164 if (!x->encap)
3165 return -EINVAL;
3167 natt = x->encap;
3169 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3171 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3172 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3175 size = sizeof(struct sadb_msg) +
3176 sizeof(struct sadb_sa) +
3177 (sizeof(struct sadb_address) * 2) +
3178 (sockaddr_size * 2) +
3179 (sizeof(struct sadb_x_nat_t_port) * 2);
3181 skb = alloc_skb(size + 16, GFP_ATOMIC);
3182 if (skb == NULL)
3183 return -ENOMEM;
3185 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3186 hdr->sadb_msg_version = PF_KEY_V2;
3187 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3188 hdr->sadb_msg_satype = satype;
3189 hdr->sadb_msg_len = size / sizeof(uint64_t);
3190 hdr->sadb_msg_errno = 0;
3191 hdr->sadb_msg_reserved = 0;
3192 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3193 hdr->sadb_msg_pid = 0;
3195 /* SA */
3196 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3197 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3198 sa->sadb_sa_exttype = SADB_EXT_SA;
3199 sa->sadb_sa_spi = x->id.spi;
3200 sa->sadb_sa_replay = 0;
3201 sa->sadb_sa_state = 0;
3202 sa->sadb_sa_auth = 0;
3203 sa->sadb_sa_encrypt = 0;
3204 sa->sadb_sa_flags = 0;
3206 /* ADDRESS_SRC (old addr) */
3207 addr = (struct sadb_address*)
3208 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3209 addr->sadb_address_len =
3210 (sizeof(struct sadb_address)+sockaddr_size)/
3211 sizeof(uint64_t);
3212 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3213 addr->sadb_address_proto = 0;
3214 addr->sadb_address_reserved = 0;
3215 addr->sadb_address_prefixlen =
3216 pfkey_sockaddr_fill(&x->props.saddr, 0,
3217 (struct sockaddr *) (addr + 1),
3218 x->props.family);
3219 if (!addr->sadb_address_prefixlen)
3220 BUG();
3222 /* NAT_T_SPORT (old port) */
3223 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3224 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3225 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3226 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3227 n_port->sadb_x_nat_t_port_reserved = 0;
3229 /* ADDRESS_DST (new addr) */
3230 addr = (struct sadb_address*)
3231 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3232 addr->sadb_address_len =
3233 (sizeof(struct sadb_address)+sockaddr_size)/
3234 sizeof(uint64_t);
3235 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3236 addr->sadb_address_proto = 0;
3237 addr->sadb_address_reserved = 0;
3238 addr->sadb_address_prefixlen =
3239 pfkey_sockaddr_fill(ipaddr, 0,
3240 (struct sockaddr *) (addr + 1),
3241 x->props.family);
3242 if (!addr->sadb_address_prefixlen)
3243 BUG();
3245 /* NAT_T_DPORT (new port) */
3246 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3247 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3248 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3249 n_port->sadb_x_nat_t_port_port = sport;
3250 n_port->sadb_x_nat_t_port_reserved = 0;
3252 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3255 #ifdef CONFIG_NET_KEY_MIGRATE
3256 static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3257 struct xfrm_selector *sel)
3259 struct sadb_address *addr;
3260 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3261 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3262 addr->sadb_address_exttype = type;
3263 addr->sadb_address_proto = sel->proto;
3264 addr->sadb_address_reserved = 0;
3266 switch (type) {
3267 case SADB_EXT_ADDRESS_SRC:
3268 addr->sadb_address_prefixlen = sel->prefixlen_s;
3269 pfkey_sockaddr_fill(&sel->saddr, 0,
3270 (struct sockaddr *)(addr + 1),
3271 sel->family);
3272 break;
3273 case SADB_EXT_ADDRESS_DST:
3274 addr->sadb_address_prefixlen = sel->prefixlen_d;
3275 pfkey_sockaddr_fill(&sel->daddr, 0,
3276 (struct sockaddr *)(addr + 1),
3277 sel->family);
3278 break;
3279 default:
3280 return -EINVAL;
3283 return 0;
3286 static int set_ipsecrequest(struct sk_buff *skb,
3287 uint8_t proto, uint8_t mode, int level,
3288 uint32_t reqid, uint8_t family,
3289 xfrm_address_t *src, xfrm_address_t *dst)
3291 struct sadb_x_ipsecrequest *rq;
3292 u8 *sa;
3293 int socklen = pfkey_sockaddr_len(family);
3294 int size_req;
3296 size_req = sizeof(struct sadb_x_ipsecrequest) +
3297 pfkey_sockaddr_pair_size(family);
3299 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3300 memset(rq, 0, size_req);
3301 rq->sadb_x_ipsecrequest_len = size_req;
3302 rq->sadb_x_ipsecrequest_proto = proto;
3303 rq->sadb_x_ipsecrequest_mode = mode;
3304 rq->sadb_x_ipsecrequest_level = level;
3305 rq->sadb_x_ipsecrequest_reqid = reqid;
3307 sa = (u8 *) (rq + 1);
3308 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3309 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
3310 return -EINVAL;
3312 return 0;
3314 #endif
3316 #ifdef CONFIG_NET_KEY_MIGRATE
3317 static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3318 struct xfrm_migrate *m, int num_bundles)
3320 int i;
3321 int sasize_sel;
3322 int size = 0;
3323 int size_pol = 0;
3324 struct sk_buff *skb;
3325 struct sadb_msg *hdr;
3326 struct sadb_x_policy *pol;
3327 struct xfrm_migrate *mp;
3329 if (type != XFRM_POLICY_TYPE_MAIN)
3330 return 0;
3332 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3333 return -EINVAL;
3335 /* selector */
3336 sasize_sel = pfkey_sockaddr_size(sel->family);
3337 if (!sasize_sel)
3338 return -EINVAL;
3339 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3341 /* policy info */
3342 size_pol += sizeof(struct sadb_x_policy);
3344 /* ipsecrequests */
3345 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3346 /* old locator pair */
3347 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3348 pfkey_sockaddr_pair_size(mp->old_family);
3349 /* new locator pair */
3350 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3351 pfkey_sockaddr_pair_size(mp->new_family);
3354 size += sizeof(struct sadb_msg) + size_pol;
3356 /* alloc buffer */
3357 skb = alloc_skb(size, GFP_ATOMIC);
3358 if (skb == NULL)
3359 return -ENOMEM;
3361 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3362 hdr->sadb_msg_version = PF_KEY_V2;
3363 hdr->sadb_msg_type = SADB_X_MIGRATE;
3364 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3365 hdr->sadb_msg_len = size / 8;
3366 hdr->sadb_msg_errno = 0;
3367 hdr->sadb_msg_reserved = 0;
3368 hdr->sadb_msg_seq = 0;
3369 hdr->sadb_msg_pid = 0;
3371 /* selector src */
3372 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3374 /* selector dst */
3375 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3377 /* policy information */
3378 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3379 pol->sadb_x_policy_len = size_pol / 8;
3380 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3381 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3382 pol->sadb_x_policy_dir = dir + 1;
3383 pol->sadb_x_policy_id = 0;
3384 pol->sadb_x_policy_priority = 0;
3386 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3387 /* old ipsecrequest */
3388 int mode = pfkey_mode_from_xfrm(mp->mode);
3389 if (mode < 0)
3390 goto err;
3391 if (set_ipsecrequest(skb, mp->proto, mode,
3392 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3393 mp->reqid, mp->old_family,
3394 &mp->old_saddr, &mp->old_daddr) < 0)
3395 goto err;
3397 /* new ipsecrequest */
3398 if (set_ipsecrequest(skb, mp->proto, mode,
3399 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3400 mp->reqid, mp->new_family,
3401 &mp->new_saddr, &mp->new_daddr) < 0)
3402 goto err;
3405 /* broadcast migrate message to sockets */
3406 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
3408 return 0;
3410 err:
3411 kfree_skb(skb);
3412 return -EINVAL;
3414 #else
3415 static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3416 struct xfrm_migrate *m, int num_bundles)
3418 return -ENOPROTOOPT;
3420 #endif
3422 static int pfkey_sendmsg(struct kiocb *kiocb,
3423 struct socket *sock, struct msghdr *msg, size_t len)
3425 struct sock *sk = sock->sk;
3426 struct sk_buff *skb = NULL;
3427 struct sadb_msg *hdr = NULL;
3428 int err;
3430 err = -EOPNOTSUPP;
3431 if (msg->msg_flags & MSG_OOB)
3432 goto out;
3434 err = -EMSGSIZE;
3435 if ((unsigned)len > sk->sk_sndbuf - 32)
3436 goto out;
3438 err = -ENOBUFS;
3439 skb = alloc_skb(len, GFP_KERNEL);
3440 if (skb == NULL)
3441 goto out;
3443 err = -EFAULT;
3444 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3445 goto out;
3447 hdr = pfkey_get_base_msg(skb, &err);
3448 if (!hdr)
3449 goto out;
3451 mutex_lock(&xfrm_cfg_mutex);
3452 err = pfkey_process(sk, skb, hdr);
3453 mutex_unlock(&xfrm_cfg_mutex);
3455 out:
3456 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3457 err = 0;
3458 if (skb)
3459 kfree_skb(skb);
3461 return err ? : len;
3464 static int pfkey_recvmsg(struct kiocb *kiocb,
3465 struct socket *sock, struct msghdr *msg, size_t len,
3466 int flags)
3468 struct sock *sk = sock->sk;
3469 struct pfkey_sock *pfk = pfkey_sk(sk);
3470 struct sk_buff *skb;
3471 int copied, err;
3473 err = -EINVAL;
3474 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3475 goto out;
3477 msg->msg_namelen = 0;
3478 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3479 if (skb == NULL)
3480 goto out;
3482 copied = skb->len;
3483 if (copied > len) {
3484 msg->msg_flags |= MSG_TRUNC;
3485 copied = len;
3488 skb_reset_transport_header(skb);
3489 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3490 if (err)
3491 goto out_free;
3493 sock_recv_timestamp(msg, sk, skb);
3495 err = (flags & MSG_TRUNC) ? skb->len : copied;
3497 if (pfk->dump.dump != NULL &&
3498 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3499 pfkey_do_dump(pfk);
3501 out_free:
3502 skb_free_datagram(sk, skb);
3503 out:
3504 return err;
3507 static const struct proto_ops pfkey_ops = {
3508 .family = PF_KEY,
3509 .owner = THIS_MODULE,
3510 /* Operations that make no sense on pfkey sockets. */
3511 .bind = sock_no_bind,
3512 .connect = sock_no_connect,
3513 .socketpair = sock_no_socketpair,
3514 .accept = sock_no_accept,
3515 .getname = sock_no_getname,
3516 .ioctl = sock_no_ioctl,
3517 .listen = sock_no_listen,
3518 .shutdown = sock_no_shutdown,
3519 .setsockopt = sock_no_setsockopt,
3520 .getsockopt = sock_no_getsockopt,
3521 .mmap = sock_no_mmap,
3522 .sendpage = sock_no_sendpage,
3524 /* Now the operations that really occur. */
3525 .release = pfkey_release,
3526 .poll = datagram_poll,
3527 .sendmsg = pfkey_sendmsg,
3528 .recvmsg = pfkey_recvmsg,
3531 static struct net_proto_family pfkey_family_ops = {
3532 .family = PF_KEY,
3533 .create = pfkey_create,
3534 .owner = THIS_MODULE,
3537 #ifdef CONFIG_PROC_FS
3538 static int pfkey_seq_show(struct seq_file *f, void *v)
3540 struct sock *s;
3542 s = (struct sock *)v;
3543 if (v == SEQ_START_TOKEN)
3544 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3545 else
3546 seq_printf(f ,"%p %-6d %-6u %-6u %-6u %-6lu\n",
3548 atomic_read(&s->sk_refcnt),
3549 atomic_read(&s->sk_rmem_alloc),
3550 atomic_read(&s->sk_wmem_alloc),
3551 sock_i_uid(s),
3552 sock_i_ino(s)
3554 return 0;
3557 static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3559 struct sock *s;
3560 struct hlist_node *node;
3561 loff_t pos = *ppos;
3563 read_lock(&pfkey_table_lock);
3564 if (pos == 0)
3565 return SEQ_START_TOKEN;
3567 sk_for_each(s, node, &pfkey_table)
3568 if (pos-- == 1)
3569 return s;
3571 return NULL;
3574 static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3576 ++*ppos;
3577 return (v == SEQ_START_TOKEN) ?
3578 sk_head(&pfkey_table) :
3579 sk_next((struct sock *)v);
3582 static void pfkey_seq_stop(struct seq_file *f, void *v)
3584 read_unlock(&pfkey_table_lock);
3587 static struct seq_operations pfkey_seq_ops = {
3588 .start = pfkey_seq_start,
3589 .next = pfkey_seq_next,
3590 .stop = pfkey_seq_stop,
3591 .show = pfkey_seq_show,
3594 static int pfkey_seq_open(struct inode *inode, struct file *file)
3596 return seq_open(file, &pfkey_seq_ops);
3599 static struct file_operations pfkey_proc_ops = {
3600 .open = pfkey_seq_open,
3601 .read = seq_read,
3602 .llseek = seq_lseek,
3603 .release = seq_release,
3606 static int pfkey_init_proc(void)
3608 struct proc_dir_entry *e;
3610 e = proc_net_fops_create(&init_net, "pfkey", 0, &pfkey_proc_ops);
3611 if (e == NULL)
3612 return -ENOMEM;
3614 return 0;
3617 static void pfkey_exit_proc(void)
3619 proc_net_remove(&init_net, "pfkey");
3621 #else
3622 static inline int pfkey_init_proc(void)
3624 return 0;
3627 static inline void pfkey_exit_proc(void)
3630 #endif
3632 static struct xfrm_mgr pfkeyv2_mgr =
3634 .id = "pfkeyv2",
3635 .notify = pfkey_send_notify,
3636 .acquire = pfkey_send_acquire,
3637 .compile_policy = pfkey_compile_policy,
3638 .new_mapping = pfkey_send_new_mapping,
3639 .notify_policy = pfkey_send_policy_notify,
3640 .migrate = pfkey_send_migrate,
3643 static void __exit ipsec_pfkey_exit(void)
3645 xfrm_unregister_km(&pfkeyv2_mgr);
3646 pfkey_exit_proc();
3647 sock_unregister(PF_KEY);
3648 proto_unregister(&key_proto);
3651 static int __init ipsec_pfkey_init(void)
3653 int err = proto_register(&key_proto, 0);
3655 if (err != 0)
3656 goto out;
3658 err = sock_register(&pfkey_family_ops);
3659 if (err != 0)
3660 goto out_unregister_key_proto;
3661 err = pfkey_init_proc();
3662 if (err != 0)
3663 goto out_sock_unregister;
3664 err = xfrm_register_km(&pfkeyv2_mgr);
3665 if (err != 0)
3666 goto out_remove_proc_entry;
3667 out:
3668 return err;
3669 out_remove_proc_entry:
3670 pfkey_exit_proc();
3671 out_sock_unregister:
3672 sock_unregister(PF_KEY);
3673 out_unregister_key_proto:
3674 proto_unregister(&key_proto);
3675 goto out;
3678 module_init(ipsec_pfkey_init);
3679 module_exit(ipsec_pfkey_exit);
3680 MODULE_LICENSE("GPL");
3681 MODULE_ALIAS_NETPROTO(PF_KEY);