jme: Fix device PM wakeup API usage
[linux/fpc-iii.git] / net / key / af_key.c
bloba9f25fdeeb4bcd4aeffe9e1fb2cce36b0820eab5
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 <linux/slab.h>
30 #include <net/net_namespace.h>
31 #include <net/netns/generic.h>
32 #include <net/xfrm.h>
34 #include <net/sock.h>
36 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
37 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
39 static int pfkey_net_id __read_mostly;
40 struct netns_pfkey {
41 /* List of all pfkey sockets. */
42 struct hlist_head table;
43 atomic_t socks_nr;
45 static DEFINE_MUTEX(pfkey_mutex);
47 #define DUMMY_MARK 0
48 static const struct xfrm_mark dummy_mark = {0, 0};
49 struct pfkey_sock {
50 /* struct sock must be the first member of struct pfkey_sock */
51 struct sock sk;
52 int registered;
53 int promisc;
55 struct {
56 uint8_t msg_version;
57 uint32_t msg_portid;
58 int (*dump)(struct pfkey_sock *sk);
59 void (*done)(struct pfkey_sock *sk);
60 union {
61 struct xfrm_policy_walk policy;
62 struct xfrm_state_walk state;
63 } u;
64 struct sk_buff *skb;
65 } dump;
68 static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
70 return (struct pfkey_sock *)sk;
73 static int pfkey_can_dump(const struct sock *sk)
75 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
76 return 1;
77 return 0;
80 static void pfkey_terminate_dump(struct pfkey_sock *pfk)
82 if (pfk->dump.dump) {
83 if (pfk->dump.skb) {
84 kfree_skb(pfk->dump.skb);
85 pfk->dump.skb = NULL;
87 pfk->dump.done(pfk);
88 pfk->dump.dump = NULL;
89 pfk->dump.done = NULL;
93 static void pfkey_sock_destruct(struct sock *sk)
95 struct net *net = sock_net(sk);
96 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
98 pfkey_terminate_dump(pfkey_sk(sk));
99 skb_queue_purge(&sk->sk_receive_queue);
101 if (!sock_flag(sk, SOCK_DEAD)) {
102 pr_err("Attempt to release alive pfkey socket: %p\n", sk);
103 return;
106 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
107 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
109 atomic_dec(&net_pfkey->socks_nr);
112 static const struct proto_ops pfkey_ops;
114 static void pfkey_insert(struct sock *sk)
116 struct net *net = sock_net(sk);
117 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
119 mutex_lock(&pfkey_mutex);
120 sk_add_node_rcu(sk, &net_pfkey->table);
121 mutex_unlock(&pfkey_mutex);
124 static void pfkey_remove(struct sock *sk)
126 mutex_lock(&pfkey_mutex);
127 sk_del_node_init_rcu(sk);
128 mutex_unlock(&pfkey_mutex);
131 static struct proto key_proto = {
132 .name = "KEY",
133 .owner = THIS_MODULE,
134 .obj_size = sizeof(struct pfkey_sock),
137 static int pfkey_create(struct net *net, struct socket *sock, int protocol,
138 int kern)
140 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
141 struct sock *sk;
142 int err;
144 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
145 return -EPERM;
146 if (sock->type != SOCK_RAW)
147 return -ESOCKTNOSUPPORT;
148 if (protocol != PF_KEY_V2)
149 return -EPROTONOSUPPORT;
151 err = -ENOMEM;
152 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto);
153 if (sk == NULL)
154 goto out;
156 sock->ops = &pfkey_ops;
157 sock_init_data(sock, sk);
159 sk->sk_family = PF_KEY;
160 sk->sk_destruct = pfkey_sock_destruct;
162 atomic_inc(&net_pfkey->socks_nr);
164 pfkey_insert(sk);
166 return 0;
167 out:
168 return err;
171 static int pfkey_release(struct socket *sock)
173 struct sock *sk = sock->sk;
175 if (!sk)
176 return 0;
178 pfkey_remove(sk);
180 sock_orphan(sk);
181 sock->sk = NULL;
182 skb_queue_purge(&sk->sk_write_queue);
184 synchronize_rcu();
185 sock_put(sk);
187 return 0;
190 static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
191 gfp_t allocation, struct sock *sk)
193 int err = -ENOBUFS;
195 sock_hold(sk);
196 if (*skb2 == NULL) {
197 if (atomic_read(&skb->users) != 1) {
198 *skb2 = skb_clone(skb, allocation);
199 } else {
200 *skb2 = skb;
201 atomic_inc(&skb->users);
204 if (*skb2 != NULL) {
205 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
206 skb_set_owner_r(*skb2, sk);
207 skb_queue_tail(&sk->sk_receive_queue, *skb2);
208 sk->sk_data_ready(sk);
209 *skb2 = NULL;
210 err = 0;
213 sock_put(sk);
214 return err;
217 /* Send SKB to all pfkey sockets matching selected criteria. */
218 #define BROADCAST_ALL 0
219 #define BROADCAST_ONE 1
220 #define BROADCAST_REGISTERED 2
221 #define BROADCAST_PROMISC_ONLY 4
222 static int pfkey_broadcast(struct sk_buff *skb,
223 int broadcast_flags, struct sock *one_sk,
224 struct net *net)
226 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
227 struct sock *sk;
228 struct sk_buff *skb2 = NULL;
229 int err = -ESRCH;
231 /* XXX Do we need something like netlink_overrun? I think
232 * XXX PF_KEY socket apps will not mind current behavior.
234 if (!skb)
235 return -ENOMEM;
237 rcu_read_lock();
238 sk_for_each_rcu(sk, &net_pfkey->table) {
239 struct pfkey_sock *pfk = pfkey_sk(sk);
240 int err2;
242 /* Yes, it means that if you are meant to receive this
243 * pfkey message you receive it twice as promiscuous
244 * socket.
246 if (pfk->promisc)
247 pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk);
249 /* the exact target will be processed later */
250 if (sk == one_sk)
251 continue;
252 if (broadcast_flags != BROADCAST_ALL) {
253 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
254 continue;
255 if ((broadcast_flags & BROADCAST_REGISTERED) &&
256 !pfk->registered)
257 continue;
258 if (broadcast_flags & BROADCAST_ONE)
259 continue;
262 err2 = pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk);
264 /* Error is cleare after succecful sending to at least one
265 * registered KM */
266 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
267 err = err2;
269 rcu_read_unlock();
271 if (one_sk != NULL)
272 err = pfkey_broadcast_one(skb, &skb2, GFP_KERNEL, one_sk);
274 kfree_skb(skb2);
275 kfree_skb(skb);
276 return err;
279 static int pfkey_do_dump(struct pfkey_sock *pfk)
281 struct sadb_msg *hdr;
282 int rc;
284 rc = pfk->dump.dump(pfk);
285 if (rc == -ENOBUFS)
286 return 0;
288 if (pfk->dump.skb) {
289 if (!pfkey_can_dump(&pfk->sk))
290 return 0;
292 hdr = (struct sadb_msg *) pfk->dump.skb->data;
293 hdr->sadb_msg_seq = 0;
294 hdr->sadb_msg_errno = rc;
295 pfkey_broadcast(pfk->dump.skb, BROADCAST_ONE,
296 &pfk->sk, sock_net(&pfk->sk));
297 pfk->dump.skb = NULL;
300 pfkey_terminate_dump(pfk);
301 return rc;
304 static inline void pfkey_hdr_dup(struct sadb_msg *new,
305 const struct sadb_msg *orig)
307 *new = *orig;
310 static int pfkey_error(const struct sadb_msg *orig, int err, struct sock *sk)
312 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
313 struct sadb_msg *hdr;
315 if (!skb)
316 return -ENOBUFS;
318 /* Woe be to the platform trying to support PFKEY yet
319 * having normal errnos outside the 1-255 range, inclusive.
321 err = -err;
322 if (err == ERESTARTSYS ||
323 err == ERESTARTNOHAND ||
324 err == ERESTARTNOINTR)
325 err = EINTR;
326 if (err >= 512)
327 err = EINVAL;
328 BUG_ON(err <= 0 || err >= 256);
330 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
331 pfkey_hdr_dup(hdr, orig);
332 hdr->sadb_msg_errno = (uint8_t) err;
333 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
334 sizeof(uint64_t));
336 pfkey_broadcast(skb, BROADCAST_ONE, sk, sock_net(sk));
338 return 0;
341 static const u8 sadb_ext_min_len[] = {
342 [SADB_EXT_RESERVED] = (u8) 0,
343 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
344 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
345 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
346 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
347 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
348 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
349 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
350 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
351 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
352 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
353 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
354 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
355 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
356 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
357 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
358 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
359 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
360 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
361 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
362 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
363 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
364 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
365 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
366 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
367 [SADB_X_EXT_KMADDRESS] = (u8) sizeof(struct sadb_x_kmaddress),
368 [SADB_X_EXT_FILTER] = (u8) sizeof(struct sadb_x_filter),
371 /* Verify sadb_address_{len,prefixlen} against sa_family. */
372 static int verify_address_len(const void *p)
374 const struct sadb_address *sp = p;
375 const struct sockaddr *addr = (const struct sockaddr *)(sp + 1);
376 const struct sockaddr_in *sin;
377 #if IS_ENABLED(CONFIG_IPV6)
378 const struct sockaddr_in6 *sin6;
379 #endif
380 int len;
382 switch (addr->sa_family) {
383 case AF_INET:
384 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
385 if (sp->sadb_address_len != len ||
386 sp->sadb_address_prefixlen > 32)
387 return -EINVAL;
388 break;
389 #if IS_ENABLED(CONFIG_IPV6)
390 case AF_INET6:
391 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
392 if (sp->sadb_address_len != len ||
393 sp->sadb_address_prefixlen > 128)
394 return -EINVAL;
395 break;
396 #endif
397 default:
398 /* It is user using kernel to keep track of security
399 * associations for another protocol, such as
400 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
401 * lengths.
403 * XXX Actually, association/policy database is not yet
404 * XXX able to cope with arbitrary sockaddr families.
405 * XXX When it can, remove this -EINVAL. -DaveM
407 return -EINVAL;
408 break;
411 return 0;
414 static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
416 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
417 sec_ctx->sadb_x_ctx_len,
418 sizeof(uint64_t));
421 static inline int verify_sec_ctx_len(const void *p)
423 const struct sadb_x_sec_ctx *sec_ctx = p;
424 int len = sec_ctx->sadb_x_ctx_len;
426 if (len > PAGE_SIZE)
427 return -EINVAL;
429 len = pfkey_sec_ctx_len(sec_ctx);
431 if (sec_ctx->sadb_x_sec_len != len)
432 return -EINVAL;
434 return 0;
437 static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx,
438 gfp_t gfp)
440 struct xfrm_user_sec_ctx *uctx = NULL;
441 int ctx_size = sec_ctx->sadb_x_ctx_len;
443 uctx = kmalloc((sizeof(*uctx)+ctx_size), gfp);
445 if (!uctx)
446 return NULL;
448 uctx->len = pfkey_sec_ctx_len(sec_ctx);
449 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
450 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
451 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
452 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
453 memcpy(uctx + 1, sec_ctx + 1,
454 uctx->ctx_len);
456 return uctx;
459 static int present_and_same_family(const struct sadb_address *src,
460 const struct sadb_address *dst)
462 const struct sockaddr *s_addr, *d_addr;
464 if (!src || !dst)
465 return 0;
467 s_addr = (const struct sockaddr *)(src + 1);
468 d_addr = (const struct sockaddr *)(dst + 1);
469 if (s_addr->sa_family != d_addr->sa_family)
470 return 0;
471 if (s_addr->sa_family != AF_INET
472 #if IS_ENABLED(CONFIG_IPV6)
473 && s_addr->sa_family != AF_INET6
474 #endif
476 return 0;
478 return 1;
481 static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
483 const char *p = (char *) hdr;
484 int len = skb->len;
486 len -= sizeof(*hdr);
487 p += sizeof(*hdr);
488 while (len > 0) {
489 const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
490 uint16_t ext_type;
491 int ext_len;
493 ext_len = ehdr->sadb_ext_len;
494 ext_len *= sizeof(uint64_t);
495 ext_type = ehdr->sadb_ext_type;
496 if (ext_len < sizeof(uint64_t) ||
497 ext_len > len ||
498 ext_type == SADB_EXT_RESERVED)
499 return -EINVAL;
501 if (ext_type <= SADB_EXT_MAX) {
502 int min = (int) sadb_ext_min_len[ext_type];
503 if (ext_len < min)
504 return -EINVAL;
505 if (ext_hdrs[ext_type-1] != NULL)
506 return -EINVAL;
507 if (ext_type == SADB_EXT_ADDRESS_SRC ||
508 ext_type == SADB_EXT_ADDRESS_DST ||
509 ext_type == SADB_EXT_ADDRESS_PROXY ||
510 ext_type == SADB_X_EXT_NAT_T_OA) {
511 if (verify_address_len(p))
512 return -EINVAL;
514 if (ext_type == SADB_X_EXT_SEC_CTX) {
515 if (verify_sec_ctx_len(p))
516 return -EINVAL;
518 ext_hdrs[ext_type-1] = (void *) p;
520 p += ext_len;
521 len -= ext_len;
524 return 0;
527 static uint16_t
528 pfkey_satype2proto(uint8_t satype)
530 switch (satype) {
531 case SADB_SATYPE_UNSPEC:
532 return IPSEC_PROTO_ANY;
533 case SADB_SATYPE_AH:
534 return IPPROTO_AH;
535 case SADB_SATYPE_ESP:
536 return IPPROTO_ESP;
537 case SADB_X_SATYPE_IPCOMP:
538 return IPPROTO_COMP;
539 break;
540 default:
541 return 0;
543 /* NOTREACHED */
546 static uint8_t
547 pfkey_proto2satype(uint16_t proto)
549 switch (proto) {
550 case IPPROTO_AH:
551 return SADB_SATYPE_AH;
552 case IPPROTO_ESP:
553 return SADB_SATYPE_ESP;
554 case IPPROTO_COMP:
555 return SADB_X_SATYPE_IPCOMP;
556 break;
557 default:
558 return 0;
560 /* NOTREACHED */
563 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
564 * say specifically 'just raw sockets' as we encode them as 255.
567 static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
569 return proto == IPSEC_PROTO_ANY ? 0 : proto;
572 static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
574 return proto ? proto : IPSEC_PROTO_ANY;
577 static inline int pfkey_sockaddr_len(sa_family_t family)
579 switch (family) {
580 case AF_INET:
581 return sizeof(struct sockaddr_in);
582 #if IS_ENABLED(CONFIG_IPV6)
583 case AF_INET6:
584 return sizeof(struct sockaddr_in6);
585 #endif
587 return 0;
590 static
591 int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
593 switch (sa->sa_family) {
594 case AF_INET:
595 xaddr->a4 =
596 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
597 return AF_INET;
598 #if IS_ENABLED(CONFIG_IPV6)
599 case AF_INET6:
600 memcpy(xaddr->a6,
601 &((struct sockaddr_in6 *)sa)->sin6_addr,
602 sizeof(struct in6_addr));
603 return AF_INET6;
604 #endif
606 return 0;
609 static
610 int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
612 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
613 xaddr);
616 static struct xfrm_state *pfkey_xfrm_state_lookup(struct net *net, const struct sadb_msg *hdr, void * const *ext_hdrs)
618 const struct sadb_sa *sa;
619 const struct sadb_address *addr;
620 uint16_t proto;
621 unsigned short family;
622 xfrm_address_t *xaddr;
624 sa = ext_hdrs[SADB_EXT_SA - 1];
625 if (sa == NULL)
626 return NULL;
628 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
629 if (proto == 0)
630 return NULL;
632 /* sadb_address_len should be checked by caller */
633 addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
634 if (addr == NULL)
635 return NULL;
637 family = ((const struct sockaddr *)(addr + 1))->sa_family;
638 switch (family) {
639 case AF_INET:
640 xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
641 break;
642 #if IS_ENABLED(CONFIG_IPV6)
643 case AF_INET6:
644 xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
645 break;
646 #endif
647 default:
648 xaddr = NULL;
651 if (!xaddr)
652 return NULL;
654 return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
657 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
659 static int
660 pfkey_sockaddr_size(sa_family_t family)
662 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
665 static inline int pfkey_mode_from_xfrm(int mode)
667 switch(mode) {
668 case XFRM_MODE_TRANSPORT:
669 return IPSEC_MODE_TRANSPORT;
670 case XFRM_MODE_TUNNEL:
671 return IPSEC_MODE_TUNNEL;
672 case XFRM_MODE_BEET:
673 return IPSEC_MODE_BEET;
674 default:
675 return -1;
679 static inline int pfkey_mode_to_xfrm(int mode)
681 switch(mode) {
682 case IPSEC_MODE_ANY: /*XXX*/
683 case IPSEC_MODE_TRANSPORT:
684 return XFRM_MODE_TRANSPORT;
685 case IPSEC_MODE_TUNNEL:
686 return XFRM_MODE_TUNNEL;
687 case IPSEC_MODE_BEET:
688 return XFRM_MODE_BEET;
689 default:
690 return -1;
694 static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
695 struct sockaddr *sa,
696 unsigned short family)
698 switch (family) {
699 case AF_INET:
701 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
702 sin->sin_family = AF_INET;
703 sin->sin_port = port;
704 sin->sin_addr.s_addr = xaddr->a4;
705 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
706 return 32;
708 #if IS_ENABLED(CONFIG_IPV6)
709 case AF_INET6:
711 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
712 sin6->sin6_family = AF_INET6;
713 sin6->sin6_port = port;
714 sin6->sin6_flowinfo = 0;
715 sin6->sin6_addr = *(struct in6_addr *)xaddr->a6;
716 sin6->sin6_scope_id = 0;
717 return 128;
719 #endif
721 return 0;
724 static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
725 int add_keys, int hsc)
727 struct sk_buff *skb;
728 struct sadb_msg *hdr;
729 struct sadb_sa *sa;
730 struct sadb_lifetime *lifetime;
731 struct sadb_address *addr;
732 struct sadb_key *key;
733 struct sadb_x_sa2 *sa2;
734 struct sadb_x_sec_ctx *sec_ctx;
735 struct xfrm_sec_ctx *xfrm_ctx;
736 int ctx_size = 0;
737 int size;
738 int auth_key_size = 0;
739 int encrypt_key_size = 0;
740 int sockaddr_size;
741 struct xfrm_encap_tmpl *natt = NULL;
742 int mode;
744 /* address family check */
745 sockaddr_size = pfkey_sockaddr_size(x->props.family);
746 if (!sockaddr_size)
747 return ERR_PTR(-EINVAL);
749 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
750 key(AE), (identity(SD),) (sensitivity)> */
751 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
752 sizeof(struct sadb_lifetime) +
753 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
754 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
755 sizeof(struct sadb_address)*2 +
756 sockaddr_size*2 +
757 sizeof(struct sadb_x_sa2);
759 if ((xfrm_ctx = x->security)) {
760 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
761 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
764 /* identity & sensitivity */
765 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family))
766 size += sizeof(struct sadb_address) + sockaddr_size;
768 if (add_keys) {
769 if (x->aalg && x->aalg->alg_key_len) {
770 auth_key_size =
771 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
772 size += sizeof(struct sadb_key) + auth_key_size;
774 if (x->ealg && x->ealg->alg_key_len) {
775 encrypt_key_size =
776 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
777 size += sizeof(struct sadb_key) + encrypt_key_size;
780 if (x->encap)
781 natt = x->encap;
783 if (natt && natt->encap_type) {
784 size += sizeof(struct sadb_x_nat_t_type);
785 size += sizeof(struct sadb_x_nat_t_port);
786 size += sizeof(struct sadb_x_nat_t_port);
789 skb = alloc_skb(size + 16, GFP_ATOMIC);
790 if (skb == NULL)
791 return ERR_PTR(-ENOBUFS);
793 /* call should fill header later */
794 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
795 memset(hdr, 0, size); /* XXX do we need this ? */
796 hdr->sadb_msg_len = size / sizeof(uint64_t);
798 /* sa */
799 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
800 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
801 sa->sadb_sa_exttype = SADB_EXT_SA;
802 sa->sadb_sa_spi = x->id.spi;
803 sa->sadb_sa_replay = x->props.replay_window;
804 switch (x->km.state) {
805 case XFRM_STATE_VALID:
806 sa->sadb_sa_state = x->km.dying ?
807 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
808 break;
809 case XFRM_STATE_ACQ:
810 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
811 break;
812 default:
813 sa->sadb_sa_state = SADB_SASTATE_DEAD;
814 break;
816 sa->sadb_sa_auth = 0;
817 if (x->aalg) {
818 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
819 sa->sadb_sa_auth = (a && a->pfkey_supported) ?
820 a->desc.sadb_alg_id : 0;
822 sa->sadb_sa_encrypt = 0;
823 BUG_ON(x->ealg && x->calg);
824 if (x->ealg) {
825 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
826 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
827 a->desc.sadb_alg_id : 0;
829 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
830 if (x->calg) {
831 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
832 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
833 a->desc.sadb_alg_id : 0;
836 sa->sadb_sa_flags = 0;
837 if (x->props.flags & XFRM_STATE_NOECN)
838 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
839 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
840 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
841 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
842 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
844 /* hard time */
845 if (hsc & 2) {
846 lifetime = (struct sadb_lifetime *) skb_put(skb,
847 sizeof(struct sadb_lifetime));
848 lifetime->sadb_lifetime_len =
849 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
850 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
851 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
852 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
853 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
854 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
856 /* soft time */
857 if (hsc & 1) {
858 lifetime = (struct sadb_lifetime *) skb_put(skb,
859 sizeof(struct sadb_lifetime));
860 lifetime->sadb_lifetime_len =
861 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
862 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
863 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
864 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
865 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
866 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
868 /* current time */
869 lifetime = (struct sadb_lifetime *) skb_put(skb,
870 sizeof(struct sadb_lifetime));
871 lifetime->sadb_lifetime_len =
872 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
873 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
874 lifetime->sadb_lifetime_allocations = x->curlft.packets;
875 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
876 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
877 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
878 /* src address */
879 addr = (struct sadb_address*) skb_put(skb,
880 sizeof(struct sadb_address)+sockaddr_size);
881 addr->sadb_address_len =
882 (sizeof(struct sadb_address)+sockaddr_size)/
883 sizeof(uint64_t);
884 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
885 /* "if the ports are non-zero, then the sadb_address_proto field,
886 normally zero, MUST be filled in with the transport
887 protocol's number." - RFC2367 */
888 addr->sadb_address_proto = 0;
889 addr->sadb_address_reserved = 0;
891 addr->sadb_address_prefixlen =
892 pfkey_sockaddr_fill(&x->props.saddr, 0,
893 (struct sockaddr *) (addr + 1),
894 x->props.family);
895 if (!addr->sadb_address_prefixlen)
896 BUG();
898 /* dst address */
899 addr = (struct sadb_address*) skb_put(skb,
900 sizeof(struct sadb_address)+sockaddr_size);
901 addr->sadb_address_len =
902 (sizeof(struct sadb_address)+sockaddr_size)/
903 sizeof(uint64_t);
904 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
905 addr->sadb_address_proto = 0;
906 addr->sadb_address_reserved = 0;
908 addr->sadb_address_prefixlen =
909 pfkey_sockaddr_fill(&x->id.daddr, 0,
910 (struct sockaddr *) (addr + 1),
911 x->props.family);
912 if (!addr->sadb_address_prefixlen)
913 BUG();
915 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
916 x->props.family)) {
917 addr = (struct sadb_address*) skb_put(skb,
918 sizeof(struct sadb_address)+sockaddr_size);
919 addr->sadb_address_len =
920 (sizeof(struct sadb_address)+sockaddr_size)/
921 sizeof(uint64_t);
922 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
923 addr->sadb_address_proto =
924 pfkey_proto_from_xfrm(x->sel.proto);
925 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
926 addr->sadb_address_reserved = 0;
928 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
929 (struct sockaddr *) (addr + 1),
930 x->props.family);
933 /* auth key */
934 if (add_keys && auth_key_size) {
935 key = (struct sadb_key *) skb_put(skb,
936 sizeof(struct sadb_key)+auth_key_size);
937 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
938 sizeof(uint64_t);
939 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
940 key->sadb_key_bits = x->aalg->alg_key_len;
941 key->sadb_key_reserved = 0;
942 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
944 /* encrypt key */
945 if (add_keys && encrypt_key_size) {
946 key = (struct sadb_key *) skb_put(skb,
947 sizeof(struct sadb_key)+encrypt_key_size);
948 key->sadb_key_len = (sizeof(struct sadb_key) +
949 encrypt_key_size) / sizeof(uint64_t);
950 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
951 key->sadb_key_bits = x->ealg->alg_key_len;
952 key->sadb_key_reserved = 0;
953 memcpy(key + 1, x->ealg->alg_key,
954 (x->ealg->alg_key_len+7)/8);
957 /* sa */
958 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
959 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
960 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
961 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
962 kfree_skb(skb);
963 return ERR_PTR(-EINVAL);
965 sa2->sadb_x_sa2_mode = mode;
966 sa2->sadb_x_sa2_reserved1 = 0;
967 sa2->sadb_x_sa2_reserved2 = 0;
968 sa2->sadb_x_sa2_sequence = 0;
969 sa2->sadb_x_sa2_reqid = x->props.reqid;
971 if (natt && natt->encap_type) {
972 struct sadb_x_nat_t_type *n_type;
973 struct sadb_x_nat_t_port *n_port;
975 /* type */
976 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
977 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
978 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
979 n_type->sadb_x_nat_t_type_type = natt->encap_type;
980 n_type->sadb_x_nat_t_type_reserved[0] = 0;
981 n_type->sadb_x_nat_t_type_reserved[1] = 0;
982 n_type->sadb_x_nat_t_type_reserved[2] = 0;
984 /* source port */
985 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
986 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
987 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
988 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
989 n_port->sadb_x_nat_t_port_reserved = 0;
991 /* dest port */
992 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
993 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
994 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
995 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
996 n_port->sadb_x_nat_t_port_reserved = 0;
999 /* security context */
1000 if (xfrm_ctx) {
1001 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1002 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1003 sec_ctx->sadb_x_sec_len =
1004 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1005 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1006 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1007 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1008 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1009 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1010 xfrm_ctx->ctx_len);
1013 return skb;
1017 static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
1019 struct sk_buff *skb;
1021 skb = __pfkey_xfrm_state2msg(x, 1, 3);
1023 return skb;
1026 static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
1027 int hsc)
1029 return __pfkey_xfrm_state2msg(x, 0, hsc);
1032 static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
1033 const struct sadb_msg *hdr,
1034 void * const *ext_hdrs)
1036 struct xfrm_state *x;
1037 const struct sadb_lifetime *lifetime;
1038 const struct sadb_sa *sa;
1039 const struct sadb_key *key;
1040 const struct sadb_x_sec_ctx *sec_ctx;
1041 uint16_t proto;
1042 int err;
1045 sa = ext_hdrs[SADB_EXT_SA - 1];
1046 if (!sa ||
1047 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1048 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1049 return ERR_PTR(-EINVAL);
1050 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1051 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1052 return ERR_PTR(-EINVAL);
1053 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1054 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1055 return ERR_PTR(-EINVAL);
1056 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1057 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1058 return ERR_PTR(-EINVAL);
1060 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1061 if (proto == 0)
1062 return ERR_PTR(-EINVAL);
1064 /* default error is no buffer space */
1065 err = -ENOBUFS;
1067 /* RFC2367:
1069 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1070 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1071 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1072 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1073 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1074 not true.
1076 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1077 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1079 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1080 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1081 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1082 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1083 return ERR_PTR(-EINVAL);
1084 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
1085 if (key != NULL &&
1086 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1087 ((key->sadb_key_bits+7) / 8 == 0 ||
1088 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1089 return ERR_PTR(-EINVAL);
1090 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1091 if (key != NULL &&
1092 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1093 ((key->sadb_key_bits+7) / 8 == 0 ||
1094 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1095 return ERR_PTR(-EINVAL);
1097 x = xfrm_state_alloc(net);
1098 if (x == NULL)
1099 return ERR_PTR(-ENOBUFS);
1101 x->id.proto = proto;
1102 x->id.spi = sa->sadb_sa_spi;
1103 x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay,
1104 (sizeof(x->replay.bitmap) * 8));
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 = 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 = 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 = 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, GFP_KERNEL);
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 = 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 || !a->pfkey_supported) {
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->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
1161 x->props.aalgo = sa->sadb_sa_auth;
1162 /* x->algo.flags = sa->sadb_sa_flags; */
1164 if (sa->sadb_sa_encrypt) {
1165 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1166 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1167 if (!a || !a->pfkey_supported) {
1168 err = -ENOSYS;
1169 goto out;
1171 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1172 if (!x->calg)
1173 goto out;
1174 strcpy(x->calg->alg_name, a->name);
1175 x->props.calgo = sa->sadb_sa_encrypt;
1176 } else {
1177 int keysize = 0;
1178 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1179 if (!a || !a->pfkey_supported) {
1180 err = -ENOSYS;
1181 goto out;
1183 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1184 if (key)
1185 keysize = (key->sadb_key_bits + 7) / 8;
1186 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1187 if (!x->ealg)
1188 goto out;
1189 strcpy(x->ealg->alg_name, a->name);
1190 x->ealg->alg_key_len = 0;
1191 if (key) {
1192 x->ealg->alg_key_len = key->sadb_key_bits;
1193 memcpy(x->ealg->alg_key, key+1, keysize);
1195 x->props.ealgo = sa->sadb_sa_encrypt;
1198 /* x->algo.flags = sa->sadb_sa_flags; */
1200 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1201 &x->props.saddr);
1202 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1203 &x->id.daddr);
1205 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1206 const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
1207 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1208 if (mode < 0) {
1209 err = -EINVAL;
1210 goto out;
1212 x->props.mode = mode;
1213 x->props.reqid = sa2->sadb_x_sa2_reqid;
1216 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1217 const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1219 /* Nobody uses this, but we try. */
1220 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1221 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1224 if (!x->sel.family)
1225 x->sel.family = x->props.family;
1227 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1228 const struct sadb_x_nat_t_type* n_type;
1229 struct xfrm_encap_tmpl *natt;
1231 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1232 if (!x->encap)
1233 goto out;
1235 natt = x->encap;
1236 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1237 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1239 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1240 const struct sadb_x_nat_t_port *n_port =
1241 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1242 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1244 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1245 const struct sadb_x_nat_t_port *n_port =
1246 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1247 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1249 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
1252 err = xfrm_init_state(x);
1253 if (err)
1254 goto out;
1256 x->km.seq = hdr->sadb_msg_seq;
1257 return x;
1259 out:
1260 x->km.state = XFRM_STATE_DEAD;
1261 xfrm_state_put(x);
1262 return ERR_PTR(err);
1265 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1267 return -EOPNOTSUPP;
1270 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1272 struct net *net = sock_net(sk);
1273 struct sk_buff *resp_skb;
1274 struct sadb_x_sa2 *sa2;
1275 struct sadb_address *saddr, *daddr;
1276 struct sadb_msg *out_hdr;
1277 struct sadb_spirange *range;
1278 struct xfrm_state *x = NULL;
1279 int mode;
1280 int err;
1281 u32 min_spi, max_spi;
1282 u32 reqid;
1283 u8 proto;
1284 unsigned short family;
1285 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1287 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1288 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1289 return -EINVAL;
1291 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1292 if (proto == 0)
1293 return -EINVAL;
1295 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1296 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1297 if (mode < 0)
1298 return -EINVAL;
1299 reqid = sa2->sadb_x_sa2_reqid;
1300 } else {
1301 mode = 0;
1302 reqid = 0;
1305 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1306 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1308 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1309 switch (family) {
1310 case AF_INET:
1311 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1312 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1313 break;
1314 #if IS_ENABLED(CONFIG_IPV6)
1315 case AF_INET6:
1316 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1317 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1318 break;
1319 #endif
1322 if (hdr->sadb_msg_seq) {
1323 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
1324 if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
1325 xfrm_state_put(x);
1326 x = NULL;
1330 if (!x)
1331 x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
1333 if (x == NULL)
1334 return -ENOENT;
1336 min_spi = 0x100;
1337 max_spi = 0x0fffffff;
1339 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1340 if (range) {
1341 min_spi = range->sadb_spirange_min;
1342 max_spi = range->sadb_spirange_max;
1345 err = verify_spi_info(x->id.proto, min_spi, max_spi);
1346 if (err) {
1347 xfrm_state_put(x);
1348 return err;
1351 err = xfrm_alloc_spi(x, min_spi, max_spi);
1352 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
1354 if (IS_ERR(resp_skb)) {
1355 xfrm_state_put(x);
1356 return PTR_ERR(resp_skb);
1359 out_hdr = (struct sadb_msg *) resp_skb->data;
1360 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1361 out_hdr->sadb_msg_type = SADB_GETSPI;
1362 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1363 out_hdr->sadb_msg_errno = 0;
1364 out_hdr->sadb_msg_reserved = 0;
1365 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1366 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1368 xfrm_state_put(x);
1370 pfkey_broadcast(resp_skb, BROADCAST_ONE, sk, net);
1372 return 0;
1375 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1377 struct net *net = sock_net(sk);
1378 struct xfrm_state *x;
1380 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1381 return -EOPNOTSUPP;
1383 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1384 return 0;
1386 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
1387 if (x == NULL)
1388 return 0;
1390 spin_lock_bh(&x->lock);
1391 if (x->km.state == XFRM_STATE_ACQ)
1392 x->km.state = XFRM_STATE_ERROR;
1394 spin_unlock_bh(&x->lock);
1395 xfrm_state_put(x);
1396 return 0;
1399 static inline int event2poltype(int event)
1401 switch (event) {
1402 case XFRM_MSG_DELPOLICY:
1403 return SADB_X_SPDDELETE;
1404 case XFRM_MSG_NEWPOLICY:
1405 return SADB_X_SPDADD;
1406 case XFRM_MSG_UPDPOLICY:
1407 return SADB_X_SPDUPDATE;
1408 case XFRM_MSG_POLEXPIRE:
1409 // return SADB_X_SPDEXPIRE;
1410 default:
1411 pr_err("pfkey: Unknown policy event %d\n", event);
1412 break;
1415 return 0;
1418 static inline int event2keytype(int event)
1420 switch (event) {
1421 case XFRM_MSG_DELSA:
1422 return SADB_DELETE;
1423 case XFRM_MSG_NEWSA:
1424 return SADB_ADD;
1425 case XFRM_MSG_UPDSA:
1426 return SADB_UPDATE;
1427 case XFRM_MSG_EXPIRE:
1428 return SADB_EXPIRE;
1429 default:
1430 pr_err("pfkey: Unknown SA event %d\n", event);
1431 break;
1434 return 0;
1437 /* ADD/UPD/DEL */
1438 static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
1440 struct sk_buff *skb;
1441 struct sadb_msg *hdr;
1443 skb = pfkey_xfrm_state2msg(x);
1445 if (IS_ERR(skb))
1446 return PTR_ERR(skb);
1448 hdr = (struct sadb_msg *) skb->data;
1449 hdr->sadb_msg_version = PF_KEY_V2;
1450 hdr->sadb_msg_type = event2keytype(c->event);
1451 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1452 hdr->sadb_msg_errno = 0;
1453 hdr->sadb_msg_reserved = 0;
1454 hdr->sadb_msg_seq = c->seq;
1455 hdr->sadb_msg_pid = c->portid;
1457 pfkey_broadcast(skb, BROADCAST_ALL, NULL, xs_net(x));
1459 return 0;
1462 static int pfkey_add(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1464 struct net *net = sock_net(sk);
1465 struct xfrm_state *x;
1466 int err;
1467 struct km_event c;
1469 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
1470 if (IS_ERR(x))
1471 return PTR_ERR(x);
1473 xfrm_state_hold(x);
1474 if (hdr->sadb_msg_type == SADB_ADD)
1475 err = xfrm_state_add(x);
1476 else
1477 err = xfrm_state_update(x);
1479 xfrm_audit_state_add(x, err ? 0 : 1, true);
1481 if (err < 0) {
1482 x->km.state = XFRM_STATE_DEAD;
1483 __xfrm_state_put(x);
1484 goto out;
1487 if (hdr->sadb_msg_type == SADB_ADD)
1488 c.event = XFRM_MSG_NEWSA;
1489 else
1490 c.event = XFRM_MSG_UPDSA;
1491 c.seq = hdr->sadb_msg_seq;
1492 c.portid = hdr->sadb_msg_pid;
1493 km_state_notify(x, &c);
1494 out:
1495 xfrm_state_put(x);
1496 return err;
1499 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1501 struct net *net = sock_net(sk);
1502 struct xfrm_state *x;
1503 struct km_event c;
1504 int err;
1506 if (!ext_hdrs[SADB_EXT_SA-1] ||
1507 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1508 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1509 return -EINVAL;
1511 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
1512 if (x == NULL)
1513 return -ESRCH;
1515 if ((err = security_xfrm_state_delete(x)))
1516 goto out;
1518 if (xfrm_state_kern(x)) {
1519 err = -EPERM;
1520 goto out;
1523 err = xfrm_state_delete(x);
1525 if (err < 0)
1526 goto out;
1528 c.seq = hdr->sadb_msg_seq;
1529 c.portid = hdr->sadb_msg_pid;
1530 c.event = XFRM_MSG_DELSA;
1531 km_state_notify(x, &c);
1532 out:
1533 xfrm_audit_state_delete(x, err ? 0 : 1, true);
1534 xfrm_state_put(x);
1536 return err;
1539 static int pfkey_get(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1541 struct net *net = sock_net(sk);
1542 __u8 proto;
1543 struct sk_buff *out_skb;
1544 struct sadb_msg *out_hdr;
1545 struct xfrm_state *x;
1547 if (!ext_hdrs[SADB_EXT_SA-1] ||
1548 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1549 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1550 return -EINVAL;
1552 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
1553 if (x == NULL)
1554 return -ESRCH;
1556 out_skb = pfkey_xfrm_state2msg(x);
1557 proto = x->id.proto;
1558 xfrm_state_put(x);
1559 if (IS_ERR(out_skb))
1560 return PTR_ERR(out_skb);
1562 out_hdr = (struct sadb_msg *) out_skb->data;
1563 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1564 out_hdr->sadb_msg_type = SADB_GET;
1565 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1566 out_hdr->sadb_msg_errno = 0;
1567 out_hdr->sadb_msg_reserved = 0;
1568 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1569 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1570 pfkey_broadcast(out_skb, BROADCAST_ONE, sk, sock_net(sk));
1572 return 0;
1575 static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
1576 gfp_t allocation)
1578 struct sk_buff *skb;
1579 struct sadb_msg *hdr;
1580 int len, auth_len, enc_len, i;
1582 auth_len = xfrm_count_pfkey_auth_supported();
1583 if (auth_len) {
1584 auth_len *= sizeof(struct sadb_alg);
1585 auth_len += sizeof(struct sadb_supported);
1588 enc_len = xfrm_count_pfkey_enc_supported();
1589 if (enc_len) {
1590 enc_len *= sizeof(struct sadb_alg);
1591 enc_len += sizeof(struct sadb_supported);
1594 len = enc_len + auth_len + sizeof(struct sadb_msg);
1596 skb = alloc_skb(len + 16, allocation);
1597 if (!skb)
1598 goto out_put_algs;
1600 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1601 pfkey_hdr_dup(hdr, orig);
1602 hdr->sadb_msg_errno = 0;
1603 hdr->sadb_msg_len = len / sizeof(uint64_t);
1605 if (auth_len) {
1606 struct sadb_supported *sp;
1607 struct sadb_alg *ap;
1609 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1610 ap = (struct sadb_alg *) (sp + 1);
1612 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1613 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1615 for (i = 0; ; i++) {
1616 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1617 if (!aalg)
1618 break;
1619 if (!aalg->pfkey_supported)
1620 continue;
1621 if (aalg->available)
1622 *ap++ = aalg->desc;
1626 if (enc_len) {
1627 struct sadb_supported *sp;
1628 struct sadb_alg *ap;
1630 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1631 ap = (struct sadb_alg *) (sp + 1);
1633 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1634 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1636 for (i = 0; ; i++) {
1637 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1638 if (!ealg)
1639 break;
1640 if (!ealg->pfkey_supported)
1641 continue;
1642 if (ealg->available)
1643 *ap++ = ealg->desc;
1647 out_put_algs:
1648 return skb;
1651 static int pfkey_register(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1653 struct pfkey_sock *pfk = pfkey_sk(sk);
1654 struct sk_buff *supp_skb;
1656 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1657 return -EINVAL;
1659 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1660 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1661 return -EEXIST;
1662 pfk->registered |= (1<<hdr->sadb_msg_satype);
1665 xfrm_probe_algs();
1667 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1668 if (!supp_skb) {
1669 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1670 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1672 return -ENOBUFS;
1675 pfkey_broadcast(supp_skb, BROADCAST_REGISTERED, sk, sock_net(sk));
1677 return 0;
1680 static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
1682 struct sk_buff *skb;
1683 struct sadb_msg *hdr;
1685 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1686 if (!skb)
1687 return -ENOBUFS;
1689 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1690 memcpy(hdr, ihdr, sizeof(struct sadb_msg));
1691 hdr->sadb_msg_errno = (uint8_t) 0;
1692 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1694 return pfkey_broadcast(skb, BROADCAST_ONE, sk, sock_net(sk));
1697 static int key_notify_sa_flush(const struct km_event *c)
1699 struct sk_buff *skb;
1700 struct sadb_msg *hdr;
1702 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1703 if (!skb)
1704 return -ENOBUFS;
1705 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1706 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
1707 hdr->sadb_msg_type = SADB_FLUSH;
1708 hdr->sadb_msg_seq = c->seq;
1709 hdr->sadb_msg_pid = c->portid;
1710 hdr->sadb_msg_version = PF_KEY_V2;
1711 hdr->sadb_msg_errno = (uint8_t) 0;
1712 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1713 hdr->sadb_msg_reserved = 0;
1715 pfkey_broadcast(skb, BROADCAST_ALL, NULL, c->net);
1717 return 0;
1720 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1722 struct net *net = sock_net(sk);
1723 unsigned int proto;
1724 struct km_event c;
1725 int err, err2;
1727 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1728 if (proto == 0)
1729 return -EINVAL;
1731 err = xfrm_state_flush(net, proto, true);
1732 err2 = unicast_flush_resp(sk, hdr);
1733 if (err || err2) {
1734 if (err == -ESRCH) /* empty table - go quietly */
1735 err = 0;
1736 return err ? err : err2;
1739 c.data.proto = proto;
1740 c.seq = hdr->sadb_msg_seq;
1741 c.portid = hdr->sadb_msg_pid;
1742 c.event = XFRM_MSG_FLUSHSA;
1743 c.net = net;
1744 km_state_notify(NULL, &c);
1746 return 0;
1749 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1751 struct pfkey_sock *pfk = ptr;
1752 struct sk_buff *out_skb;
1753 struct sadb_msg *out_hdr;
1755 if (!pfkey_can_dump(&pfk->sk))
1756 return -ENOBUFS;
1758 out_skb = pfkey_xfrm_state2msg(x);
1759 if (IS_ERR(out_skb))
1760 return PTR_ERR(out_skb);
1762 out_hdr = (struct sadb_msg *) out_skb->data;
1763 out_hdr->sadb_msg_version = pfk->dump.msg_version;
1764 out_hdr->sadb_msg_type = SADB_DUMP;
1765 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1766 out_hdr->sadb_msg_errno = 0;
1767 out_hdr->sadb_msg_reserved = 0;
1768 out_hdr->sadb_msg_seq = count + 1;
1769 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
1771 if (pfk->dump.skb)
1772 pfkey_broadcast(pfk->dump.skb, BROADCAST_ONE,
1773 &pfk->sk, sock_net(&pfk->sk));
1774 pfk->dump.skb = out_skb;
1776 return 0;
1779 static int pfkey_dump_sa(struct pfkey_sock *pfk)
1781 struct net *net = sock_net(&pfk->sk);
1782 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
1785 static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1787 struct net *net = sock_net(&pfk->sk);
1789 xfrm_state_walk_done(&pfk->dump.u.state, net);
1792 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1794 u8 proto;
1795 struct xfrm_address_filter *filter = NULL;
1796 struct pfkey_sock *pfk = pfkey_sk(sk);
1798 if (pfk->dump.dump != NULL)
1799 return -EBUSY;
1801 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1802 if (proto == 0)
1803 return -EINVAL;
1805 if (ext_hdrs[SADB_X_EXT_FILTER - 1]) {
1806 struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1];
1808 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
1809 if (filter == NULL)
1810 return -ENOMEM;
1812 memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr,
1813 sizeof(xfrm_address_t));
1814 memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr,
1815 sizeof(xfrm_address_t));
1816 filter->family = xfilter->sadb_x_filter_family;
1817 filter->splen = xfilter->sadb_x_filter_splen;
1818 filter->dplen = xfilter->sadb_x_filter_dplen;
1821 pfk->dump.msg_version = hdr->sadb_msg_version;
1822 pfk->dump.msg_portid = hdr->sadb_msg_pid;
1823 pfk->dump.dump = pfkey_dump_sa;
1824 pfk->dump.done = pfkey_dump_sa_done;
1825 xfrm_state_walk_init(&pfk->dump.u.state, proto, filter);
1827 return pfkey_do_dump(pfk);
1830 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1832 struct pfkey_sock *pfk = pfkey_sk(sk);
1833 int satype = hdr->sadb_msg_satype;
1834 bool reset_errno = false;
1836 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1837 reset_errno = true;
1838 if (satype != 0 && satype != 1)
1839 return -EINVAL;
1840 pfk->promisc = satype;
1842 if (reset_errno && skb_cloned(skb))
1843 skb = skb_copy(skb, GFP_KERNEL);
1844 else
1845 skb = skb_clone(skb, GFP_KERNEL);
1847 if (reset_errno && skb) {
1848 struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
1849 new_hdr->sadb_msg_errno = 0;
1852 pfkey_broadcast(skb, BROADCAST_ALL, NULL, sock_net(sk));
1853 return 0;
1856 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1858 int i;
1859 u32 reqid = *(u32*)ptr;
1861 for (i=0; i<xp->xfrm_nr; i++) {
1862 if (xp->xfrm_vec[i].reqid == reqid)
1863 return -EEXIST;
1865 return 0;
1868 static u32 gen_reqid(struct net *net)
1870 struct xfrm_policy_walk walk;
1871 u32 start;
1872 int rc;
1873 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1875 start = reqid;
1876 do {
1877 ++reqid;
1878 if (reqid == 0)
1879 reqid = IPSEC_MANUAL_REQID_MAX+1;
1880 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
1881 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
1882 xfrm_policy_walk_done(&walk, net);
1883 if (rc != -EEXIST)
1884 return reqid;
1885 } while (reqid != start);
1886 return 0;
1889 static int
1890 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1892 struct net *net = xp_net(xp);
1893 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1894 int mode;
1896 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1897 return -ELOOP;
1899 if (rq->sadb_x_ipsecrequest_mode == 0)
1900 return -EINVAL;
1902 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1903 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1904 return -EINVAL;
1905 t->mode = mode;
1906 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1907 t->optional = 1;
1908 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1909 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1910 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1911 t->reqid = 0;
1912 if (!t->reqid && !(t->reqid = gen_reqid(net)))
1913 return -ENOBUFS;
1916 /* addresses present only in tunnel mode */
1917 if (t->mode == XFRM_MODE_TUNNEL) {
1918 u8 *sa = (u8 *) (rq + 1);
1919 int family, socklen;
1921 family = pfkey_sockaddr_extract((struct sockaddr *)sa,
1922 &t->saddr);
1923 if (!family)
1924 return -EINVAL;
1926 socklen = pfkey_sockaddr_len(family);
1927 if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen),
1928 &t->id.daddr) != family)
1929 return -EINVAL;
1930 t->encap_family = family;
1931 } else
1932 t->encap_family = xp->family;
1934 /* No way to set this via kame pfkey */
1935 t->allalgs = 1;
1936 xp->xfrm_nr++;
1937 return 0;
1940 static int
1941 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1943 int err;
1944 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1945 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1947 if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
1948 return -EINVAL;
1950 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1951 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1952 return err;
1953 len -= rq->sadb_x_ipsecrequest_len;
1954 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1956 return 0;
1959 static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
1961 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1963 if (xfrm_ctx) {
1964 int len = sizeof(struct sadb_x_sec_ctx);
1965 len += xfrm_ctx->ctx_len;
1966 return PFKEY_ALIGN8(len);
1968 return 0;
1971 static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
1973 const struct xfrm_tmpl *t;
1974 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1975 int socklen = 0;
1976 int i;
1978 for (i=0; i<xp->xfrm_nr; i++) {
1979 t = xp->xfrm_vec + i;
1980 socklen += pfkey_sockaddr_len(t->encap_family);
1983 return sizeof(struct sadb_msg) +
1984 (sizeof(struct sadb_lifetime) * 3) +
1985 (sizeof(struct sadb_address) * 2) +
1986 (sockaddr_size * 2) +
1987 sizeof(struct sadb_x_policy) +
1988 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1989 (socklen * 2) +
1990 pfkey_xfrm_policy2sec_ctx_size(xp);
1993 static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
1995 struct sk_buff *skb;
1996 int size;
1998 size = pfkey_xfrm_policy2msg_size(xp);
2000 skb = alloc_skb(size + 16, GFP_ATOMIC);
2001 if (skb == NULL)
2002 return ERR_PTR(-ENOBUFS);
2004 return skb;
2007 static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
2009 struct sadb_msg *hdr;
2010 struct sadb_address *addr;
2011 struct sadb_lifetime *lifetime;
2012 struct sadb_x_policy *pol;
2013 struct sadb_x_sec_ctx *sec_ctx;
2014 struct xfrm_sec_ctx *xfrm_ctx;
2015 int i;
2016 int size;
2017 int sockaddr_size = pfkey_sockaddr_size(xp->family);
2018 int socklen = pfkey_sockaddr_len(xp->family);
2020 size = pfkey_xfrm_policy2msg_size(xp);
2022 /* call should fill header later */
2023 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2024 memset(hdr, 0, size); /* XXX do we need this ? */
2026 /* src address */
2027 addr = (struct sadb_address*) skb_put(skb,
2028 sizeof(struct sadb_address)+sockaddr_size);
2029 addr->sadb_address_len =
2030 (sizeof(struct sadb_address)+sockaddr_size)/
2031 sizeof(uint64_t);
2032 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2033 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2034 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2035 addr->sadb_address_reserved = 0;
2036 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2037 xp->selector.sport,
2038 (struct sockaddr *) (addr + 1),
2039 xp->family))
2040 BUG();
2042 /* dst address */
2043 addr = (struct sadb_address*) skb_put(skb,
2044 sizeof(struct sadb_address)+sockaddr_size);
2045 addr->sadb_address_len =
2046 (sizeof(struct sadb_address)+sockaddr_size)/
2047 sizeof(uint64_t);
2048 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2049 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2050 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
2051 addr->sadb_address_reserved = 0;
2053 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2054 (struct sockaddr *) (addr + 1),
2055 xp->family);
2057 /* hard time */
2058 lifetime = (struct sadb_lifetime *) skb_put(skb,
2059 sizeof(struct sadb_lifetime));
2060 lifetime->sadb_lifetime_len =
2061 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2062 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2063 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2064 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2065 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2066 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2067 /* soft time */
2068 lifetime = (struct sadb_lifetime *) skb_put(skb,
2069 sizeof(struct sadb_lifetime));
2070 lifetime->sadb_lifetime_len =
2071 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2072 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2073 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2074 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2075 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2076 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2077 /* current time */
2078 lifetime = (struct sadb_lifetime *) skb_put(skb,
2079 sizeof(struct sadb_lifetime));
2080 lifetime->sadb_lifetime_len =
2081 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2082 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2083 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2084 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2085 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2086 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2088 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2089 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2090 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2091 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2092 if (xp->action == XFRM_POLICY_ALLOW) {
2093 if (xp->xfrm_nr)
2094 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2095 else
2096 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2098 pol->sadb_x_policy_dir = dir+1;
2099 pol->sadb_x_policy_reserved = 0;
2100 pol->sadb_x_policy_id = xp->index;
2101 pol->sadb_x_policy_priority = xp->priority;
2103 for (i=0; i<xp->xfrm_nr; i++) {
2104 const struct xfrm_tmpl *t = xp->xfrm_vec + i;
2105 struct sadb_x_ipsecrequest *rq;
2106 int req_size;
2107 int mode;
2109 req_size = sizeof(struct sadb_x_ipsecrequest);
2110 if (t->mode == XFRM_MODE_TUNNEL) {
2111 socklen = pfkey_sockaddr_len(t->encap_family);
2112 req_size += socklen * 2;
2113 } else {
2114 size -= 2*socklen;
2116 rq = (void*)skb_put(skb, req_size);
2117 pol->sadb_x_policy_len += req_size/8;
2118 memset(rq, 0, sizeof(*rq));
2119 rq->sadb_x_ipsecrequest_len = req_size;
2120 rq->sadb_x_ipsecrequest_proto = t->id.proto;
2121 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2122 return -EINVAL;
2123 rq->sadb_x_ipsecrequest_mode = mode;
2124 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2125 if (t->reqid)
2126 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2127 if (t->optional)
2128 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2129 rq->sadb_x_ipsecrequest_reqid = t->reqid;
2131 if (t->mode == XFRM_MODE_TUNNEL) {
2132 u8 *sa = (void *)(rq + 1);
2133 pfkey_sockaddr_fill(&t->saddr, 0,
2134 (struct sockaddr *)sa,
2135 t->encap_family);
2136 pfkey_sockaddr_fill(&t->id.daddr, 0,
2137 (struct sockaddr *) (sa + socklen),
2138 t->encap_family);
2142 /* security context */
2143 if ((xfrm_ctx = xp->security)) {
2144 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2146 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2147 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2148 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2149 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2150 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2151 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2152 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2153 xfrm_ctx->ctx_len);
2156 hdr->sadb_msg_len = size / sizeof(uint64_t);
2157 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
2159 return 0;
2162 static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2164 struct sk_buff *out_skb;
2165 struct sadb_msg *out_hdr;
2166 int err;
2168 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2169 if (IS_ERR(out_skb))
2170 return PTR_ERR(out_skb);
2172 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2173 if (err < 0)
2174 return err;
2176 out_hdr = (struct sadb_msg *) out_skb->data;
2177 out_hdr->sadb_msg_version = PF_KEY_V2;
2179 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
2180 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2181 else
2182 out_hdr->sadb_msg_type = event2poltype(c->event);
2183 out_hdr->sadb_msg_errno = 0;
2184 out_hdr->sadb_msg_seq = c->seq;
2185 out_hdr->sadb_msg_pid = c->portid;
2186 pfkey_broadcast(out_skb, BROADCAST_ALL, NULL, xp_net(xp));
2187 return 0;
2191 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2193 struct net *net = sock_net(sk);
2194 int err = 0;
2195 struct sadb_lifetime *lifetime;
2196 struct sadb_address *sa;
2197 struct sadb_x_policy *pol;
2198 struct xfrm_policy *xp;
2199 struct km_event c;
2200 struct sadb_x_sec_ctx *sec_ctx;
2202 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2203 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2204 !ext_hdrs[SADB_X_EXT_POLICY-1])
2205 return -EINVAL;
2207 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2208 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2209 return -EINVAL;
2210 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2211 return -EINVAL;
2213 xp = xfrm_policy_alloc(net, GFP_KERNEL);
2214 if (xp == NULL)
2215 return -ENOBUFS;
2217 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2218 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2219 xp->priority = pol->sadb_x_policy_priority;
2221 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
2222 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2223 xp->selector.family = xp->family;
2224 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2225 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2226 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2227 if (xp->selector.sport)
2228 xp->selector.sport_mask = htons(0xffff);
2230 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
2231 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2232 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2234 /* Amusing, we set this twice. KAME apps appear to set same value
2235 * in both addresses.
2237 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2239 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2240 if (xp->selector.dport)
2241 xp->selector.dport_mask = htons(0xffff);
2243 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
2244 if (sec_ctx != NULL) {
2245 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
2247 if (!uctx) {
2248 err = -ENOBUFS;
2249 goto out;
2252 err = security_xfrm_policy_alloc(&xp->security, uctx, GFP_KERNEL);
2253 kfree(uctx);
2255 if (err)
2256 goto out;
2259 xp->lft.soft_byte_limit = XFRM_INF;
2260 xp->lft.hard_byte_limit = XFRM_INF;
2261 xp->lft.soft_packet_limit = XFRM_INF;
2262 xp->lft.hard_packet_limit = XFRM_INF;
2263 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2264 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2265 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2266 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2267 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2269 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2270 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2271 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2272 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2273 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2275 xp->xfrm_nr = 0;
2276 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2277 (err = parse_ipsecrequests(xp, pol)) < 0)
2278 goto out;
2280 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2281 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2283 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
2285 if (err)
2286 goto out;
2288 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2289 c.event = XFRM_MSG_UPDPOLICY;
2290 else
2291 c.event = XFRM_MSG_NEWPOLICY;
2293 c.seq = hdr->sadb_msg_seq;
2294 c.portid = hdr->sadb_msg_pid;
2296 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2297 xfrm_pol_put(xp);
2298 return 0;
2300 out:
2301 xp->walk.dead = 1;
2302 xfrm_policy_destroy(xp);
2303 return err;
2306 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2308 struct net *net = sock_net(sk);
2309 int err;
2310 struct sadb_address *sa;
2311 struct sadb_x_policy *pol;
2312 struct xfrm_policy *xp;
2313 struct xfrm_selector sel;
2314 struct km_event c;
2315 struct sadb_x_sec_ctx *sec_ctx;
2316 struct xfrm_sec_ctx *pol_ctx = NULL;
2318 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2319 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2320 !ext_hdrs[SADB_X_EXT_POLICY-1])
2321 return -EINVAL;
2323 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2324 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2325 return -EINVAL;
2327 memset(&sel, 0, sizeof(sel));
2329 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
2330 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2331 sel.prefixlen_s = sa->sadb_address_prefixlen;
2332 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2333 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2334 if (sel.sport)
2335 sel.sport_mask = htons(0xffff);
2337 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
2338 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2339 sel.prefixlen_d = sa->sadb_address_prefixlen;
2340 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2341 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2342 if (sel.dport)
2343 sel.dport_mask = htons(0xffff);
2345 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
2346 if (sec_ctx != NULL) {
2347 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
2349 if (!uctx)
2350 return -ENOMEM;
2352 err = security_xfrm_policy_alloc(&pol_ctx, uctx, GFP_KERNEL);
2353 kfree(uctx);
2354 if (err)
2355 return err;
2358 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
2359 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2360 1, &err);
2361 security_xfrm_policy_free(pol_ctx);
2362 if (xp == NULL)
2363 return -ENOENT;
2365 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2367 if (err)
2368 goto out;
2370 c.seq = hdr->sadb_msg_seq;
2371 c.portid = hdr->sadb_msg_pid;
2372 c.data.byid = 0;
2373 c.event = XFRM_MSG_DELPOLICY;
2374 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2376 out:
2377 xfrm_pol_put(xp);
2378 if (err == 0)
2379 xfrm_garbage_collect(net);
2380 return err;
2383 static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, const struct sadb_msg *hdr, int dir)
2385 int err;
2386 struct sk_buff *out_skb;
2387 struct sadb_msg *out_hdr;
2388 err = 0;
2390 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2391 if (IS_ERR(out_skb)) {
2392 err = PTR_ERR(out_skb);
2393 goto out;
2395 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2396 if (err < 0)
2397 goto out;
2399 out_hdr = (struct sadb_msg *) out_skb->data;
2400 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2401 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2402 out_hdr->sadb_msg_satype = 0;
2403 out_hdr->sadb_msg_errno = 0;
2404 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2405 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2406 pfkey_broadcast(out_skb, BROADCAST_ONE, sk, xp_net(xp));
2407 err = 0;
2409 out:
2410 return err;
2413 #ifdef CONFIG_NET_KEY_MIGRATE
2414 static int pfkey_sockaddr_pair_size(sa_family_t family)
2416 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
2419 static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
2420 xfrm_address_t *saddr, xfrm_address_t *daddr,
2421 u16 *family)
2423 int af, socklen;
2425 if (ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
2426 return -EINVAL;
2428 af = pfkey_sockaddr_extract(sa, saddr);
2429 if (!af)
2430 return -EINVAL;
2432 socklen = pfkey_sockaddr_len(af);
2433 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
2434 daddr) != af)
2435 return -EINVAL;
2437 *family = af;
2438 return 0;
2441 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2442 struct xfrm_migrate *m)
2444 int err;
2445 struct sadb_x_ipsecrequest *rq2;
2446 int mode;
2448 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2449 len < rq1->sadb_x_ipsecrequest_len)
2450 return -EINVAL;
2452 /* old endoints */
2453 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
2454 rq1->sadb_x_ipsecrequest_len,
2455 &m->old_saddr, &m->old_daddr,
2456 &m->old_family);
2457 if (err)
2458 return err;
2460 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2461 len -= rq1->sadb_x_ipsecrequest_len;
2463 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2464 len < rq2->sadb_x_ipsecrequest_len)
2465 return -EINVAL;
2467 /* new endpoints */
2468 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
2469 rq2->sadb_x_ipsecrequest_len,
2470 &m->new_saddr, &m->new_daddr,
2471 &m->new_family);
2472 if (err)
2473 return err;
2475 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2476 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2477 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2478 return -EINVAL;
2480 m->proto = rq1->sadb_x_ipsecrequest_proto;
2481 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2482 return -EINVAL;
2483 m->mode = mode;
2484 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2486 return ((int)(rq1->sadb_x_ipsecrequest_len +
2487 rq2->sadb_x_ipsecrequest_len));
2490 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2491 const struct sadb_msg *hdr, void * const *ext_hdrs)
2493 int i, len, ret, err = -EINVAL;
2494 u8 dir;
2495 struct sadb_address *sa;
2496 struct sadb_x_kmaddress *kma;
2497 struct sadb_x_policy *pol;
2498 struct sadb_x_ipsecrequest *rq;
2499 struct xfrm_selector sel;
2500 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2501 struct xfrm_kmaddress k;
2502 struct net *net = sock_net(sk);
2504 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2505 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2506 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2507 err = -EINVAL;
2508 goto out;
2511 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
2512 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2514 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2515 err = -EINVAL;
2516 goto out;
2519 if (kma) {
2520 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2521 k.reserved = kma->sadb_x_kmaddress_reserved;
2522 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2523 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2524 &k.local, &k.remote, &k.family);
2525 if (ret < 0) {
2526 err = ret;
2527 goto out;
2531 dir = pol->sadb_x_policy_dir - 1;
2532 memset(&sel, 0, sizeof(sel));
2534 /* set source address info of selector */
2535 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2536 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2537 sel.prefixlen_s = sa->sadb_address_prefixlen;
2538 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2539 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2540 if (sel.sport)
2541 sel.sport_mask = htons(0xffff);
2543 /* set destination address info of selector */
2544 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
2545 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2546 sel.prefixlen_d = sa->sadb_address_prefixlen;
2547 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2548 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2549 if (sel.dport)
2550 sel.dport_mask = htons(0xffff);
2552 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2554 /* extract ipsecrequests */
2555 i = 0;
2556 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2558 while (len > 0 && i < XFRM_MAX_DEPTH) {
2559 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2560 if (ret < 0) {
2561 err = ret;
2562 goto out;
2563 } else {
2564 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2565 len -= ret;
2566 i++;
2570 if (!i || len > 0) {
2571 err = -EINVAL;
2572 goto out;
2575 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
2576 kma ? &k : NULL, net);
2578 out:
2579 return err;
2581 #else
2582 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2583 const struct sadb_msg *hdr, void * const *ext_hdrs)
2585 return -ENOPROTOOPT;
2587 #endif
2590 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2592 struct net *net = sock_net(sk);
2593 unsigned int dir;
2594 int err = 0, delete;
2595 struct sadb_x_policy *pol;
2596 struct xfrm_policy *xp;
2597 struct km_event c;
2599 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2600 return -EINVAL;
2602 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2603 if (dir >= XFRM_POLICY_MAX)
2604 return -EINVAL;
2606 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2607 xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
2608 dir, pol->sadb_x_policy_id, delete, &err);
2609 if (xp == NULL)
2610 return -ENOENT;
2612 if (delete) {
2613 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2615 if (err)
2616 goto out;
2617 c.seq = hdr->sadb_msg_seq;
2618 c.portid = hdr->sadb_msg_pid;
2619 c.data.byid = 1;
2620 c.event = XFRM_MSG_DELPOLICY;
2621 km_policy_notify(xp, dir, &c);
2622 } else {
2623 err = key_pol_get_resp(sk, xp, hdr, dir);
2626 out:
2627 xfrm_pol_put(xp);
2628 if (delete && err == 0)
2629 xfrm_garbage_collect(net);
2630 return err;
2633 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2635 struct pfkey_sock *pfk = ptr;
2636 struct sk_buff *out_skb;
2637 struct sadb_msg *out_hdr;
2638 int err;
2640 if (!pfkey_can_dump(&pfk->sk))
2641 return -ENOBUFS;
2643 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2644 if (IS_ERR(out_skb))
2645 return PTR_ERR(out_skb);
2647 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2648 if (err < 0)
2649 return err;
2651 out_hdr = (struct sadb_msg *) out_skb->data;
2652 out_hdr->sadb_msg_version = pfk->dump.msg_version;
2653 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2654 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2655 out_hdr->sadb_msg_errno = 0;
2656 out_hdr->sadb_msg_seq = count + 1;
2657 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
2659 if (pfk->dump.skb)
2660 pfkey_broadcast(pfk->dump.skb, BROADCAST_ONE,
2661 &pfk->sk, sock_net(&pfk->sk));
2662 pfk->dump.skb = out_skb;
2664 return 0;
2667 static int pfkey_dump_sp(struct pfkey_sock *pfk)
2669 struct net *net = sock_net(&pfk->sk);
2670 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
2673 static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2675 struct net *net = sock_net((struct sock *)pfk);
2677 xfrm_policy_walk_done(&pfk->dump.u.policy, net);
2680 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2682 struct pfkey_sock *pfk = pfkey_sk(sk);
2684 if (pfk->dump.dump != NULL)
2685 return -EBUSY;
2687 pfk->dump.msg_version = hdr->sadb_msg_version;
2688 pfk->dump.msg_portid = hdr->sadb_msg_pid;
2689 pfk->dump.dump = pfkey_dump_sp;
2690 pfk->dump.done = pfkey_dump_sp_done;
2691 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2693 return pfkey_do_dump(pfk);
2696 static int key_notify_policy_flush(const struct km_event *c)
2698 struct sk_buff *skb_out;
2699 struct sadb_msg *hdr;
2701 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
2702 if (!skb_out)
2703 return -ENOBUFS;
2704 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2705 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
2706 hdr->sadb_msg_seq = c->seq;
2707 hdr->sadb_msg_pid = c->portid;
2708 hdr->sadb_msg_version = PF_KEY_V2;
2709 hdr->sadb_msg_errno = (uint8_t) 0;
2710 hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2711 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2712 hdr->sadb_msg_reserved = 0;
2713 pfkey_broadcast(skb_out, BROADCAST_ALL, NULL, c->net);
2714 return 0;
2718 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2720 struct net *net = sock_net(sk);
2721 struct km_event c;
2722 int err, err2;
2724 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true);
2725 err2 = unicast_flush_resp(sk, hdr);
2726 if (err || err2) {
2727 if (err == -ESRCH) /* empty table - old silent behavior */
2728 return 0;
2729 return err;
2732 c.data.type = XFRM_POLICY_TYPE_MAIN;
2733 c.event = XFRM_MSG_FLUSHPOLICY;
2734 c.portid = hdr->sadb_msg_pid;
2735 c.seq = hdr->sadb_msg_seq;
2736 c.net = net;
2737 km_policy_notify(NULL, 0, &c);
2739 return 0;
2742 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2743 const struct sadb_msg *hdr, void * const *ext_hdrs);
2744 static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2745 [SADB_RESERVED] = pfkey_reserved,
2746 [SADB_GETSPI] = pfkey_getspi,
2747 [SADB_UPDATE] = pfkey_add,
2748 [SADB_ADD] = pfkey_add,
2749 [SADB_DELETE] = pfkey_delete,
2750 [SADB_GET] = pfkey_get,
2751 [SADB_ACQUIRE] = pfkey_acquire,
2752 [SADB_REGISTER] = pfkey_register,
2753 [SADB_EXPIRE] = NULL,
2754 [SADB_FLUSH] = pfkey_flush,
2755 [SADB_DUMP] = pfkey_dump,
2756 [SADB_X_PROMISC] = pfkey_promisc,
2757 [SADB_X_PCHANGE] = NULL,
2758 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2759 [SADB_X_SPDADD] = pfkey_spdadd,
2760 [SADB_X_SPDDELETE] = pfkey_spddelete,
2761 [SADB_X_SPDGET] = pfkey_spdget,
2762 [SADB_X_SPDACQUIRE] = NULL,
2763 [SADB_X_SPDDUMP] = pfkey_spddump,
2764 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2765 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2766 [SADB_X_SPDDELETE2] = pfkey_spdget,
2767 [SADB_X_MIGRATE] = pfkey_migrate,
2770 static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
2772 void *ext_hdrs[SADB_EXT_MAX];
2773 int err;
2775 pfkey_broadcast(skb_clone(skb, GFP_KERNEL),
2776 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
2778 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2779 err = parse_exthdrs(skb, hdr, ext_hdrs);
2780 if (!err) {
2781 err = -EOPNOTSUPP;
2782 if (pfkey_funcs[hdr->sadb_msg_type])
2783 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2785 return err;
2788 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2790 struct sadb_msg *hdr = NULL;
2792 if (skb->len < sizeof(*hdr)) {
2793 *errp = -EMSGSIZE;
2794 } else {
2795 hdr = (struct sadb_msg *) skb->data;
2796 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2797 hdr->sadb_msg_reserved != 0 ||
2798 (hdr->sadb_msg_type <= SADB_RESERVED ||
2799 hdr->sadb_msg_type > SADB_MAX)) {
2800 hdr = NULL;
2801 *errp = -EINVAL;
2802 } else if (hdr->sadb_msg_len != (skb->len /
2803 sizeof(uint64_t)) ||
2804 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2805 sizeof(uint64_t))) {
2806 hdr = NULL;
2807 *errp = -EMSGSIZE;
2808 } else {
2809 *errp = 0;
2812 return hdr;
2815 static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
2816 const struct xfrm_algo_desc *d)
2818 unsigned int id = d->desc.sadb_alg_id;
2820 if (id >= sizeof(t->aalgos) * 8)
2821 return 0;
2823 return (t->aalgos >> id) & 1;
2826 static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
2827 const struct xfrm_algo_desc *d)
2829 unsigned int id = d->desc.sadb_alg_id;
2831 if (id >= sizeof(t->ealgos) * 8)
2832 return 0;
2834 return (t->ealgos >> id) & 1;
2837 static int count_ah_combs(const struct xfrm_tmpl *t)
2839 int i, sz = 0;
2841 for (i = 0; ; i++) {
2842 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2843 if (!aalg)
2844 break;
2845 if (!aalg->pfkey_supported)
2846 continue;
2847 if (aalg_tmpl_set(t, aalg) && aalg->available)
2848 sz += sizeof(struct sadb_comb);
2850 return sz + sizeof(struct sadb_prop);
2853 static int count_esp_combs(const struct xfrm_tmpl *t)
2855 int i, k, sz = 0;
2857 for (i = 0; ; i++) {
2858 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2859 if (!ealg)
2860 break;
2862 if (!ealg->pfkey_supported)
2863 continue;
2865 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2866 continue;
2868 for (k = 1; ; k++) {
2869 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2870 if (!aalg)
2871 break;
2873 if (!aalg->pfkey_supported)
2874 continue;
2876 if (aalg_tmpl_set(t, aalg) && aalg->available)
2877 sz += sizeof(struct sadb_comb);
2880 return sz + sizeof(struct sadb_prop);
2883 static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
2885 struct sadb_prop *p;
2886 int i;
2888 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2889 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2890 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2891 p->sadb_prop_replay = 32;
2892 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2894 for (i = 0; ; i++) {
2895 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2896 if (!aalg)
2897 break;
2899 if (!aalg->pfkey_supported)
2900 continue;
2902 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2903 struct sadb_comb *c;
2904 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2905 memset(c, 0, sizeof(*c));
2906 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2907 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2908 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2909 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2910 c->sadb_comb_hard_addtime = 24*60*60;
2911 c->sadb_comb_soft_addtime = 20*60*60;
2912 c->sadb_comb_hard_usetime = 8*60*60;
2913 c->sadb_comb_soft_usetime = 7*60*60;
2918 static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
2920 struct sadb_prop *p;
2921 int i, k;
2923 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2924 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2925 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2926 p->sadb_prop_replay = 32;
2927 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2929 for (i=0; ; i++) {
2930 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2931 if (!ealg)
2932 break;
2934 if (!ealg->pfkey_supported)
2935 continue;
2937 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2938 continue;
2940 for (k = 1; ; k++) {
2941 struct sadb_comb *c;
2942 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2943 if (!aalg)
2944 break;
2945 if (!aalg->pfkey_supported)
2946 continue;
2947 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2948 continue;
2949 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2950 memset(c, 0, sizeof(*c));
2951 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2952 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2953 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2954 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2955 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2956 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2957 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2958 c->sadb_comb_hard_addtime = 24*60*60;
2959 c->sadb_comb_soft_addtime = 20*60*60;
2960 c->sadb_comb_hard_usetime = 8*60*60;
2961 c->sadb_comb_soft_usetime = 7*60*60;
2966 static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
2968 return 0;
2971 static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
2973 struct sk_buff *out_skb;
2974 struct sadb_msg *out_hdr;
2975 int hard;
2976 int hsc;
2978 hard = c->data.hard;
2979 if (hard)
2980 hsc = 2;
2981 else
2982 hsc = 1;
2984 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
2985 if (IS_ERR(out_skb))
2986 return PTR_ERR(out_skb);
2988 out_hdr = (struct sadb_msg *) out_skb->data;
2989 out_hdr->sadb_msg_version = PF_KEY_V2;
2990 out_hdr->sadb_msg_type = SADB_EXPIRE;
2991 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2992 out_hdr->sadb_msg_errno = 0;
2993 out_hdr->sadb_msg_reserved = 0;
2994 out_hdr->sadb_msg_seq = 0;
2995 out_hdr->sadb_msg_pid = 0;
2997 pfkey_broadcast(out_skb, BROADCAST_REGISTERED, NULL, xs_net(x));
2998 return 0;
3001 static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
3003 struct net *net = x ? xs_net(x) : c->net;
3004 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3006 if (atomic_read(&net_pfkey->socks_nr) == 0)
3007 return 0;
3009 switch (c->event) {
3010 case XFRM_MSG_EXPIRE:
3011 return key_notify_sa_expire(x, c);
3012 case XFRM_MSG_DELSA:
3013 case XFRM_MSG_NEWSA:
3014 case XFRM_MSG_UPDSA:
3015 return key_notify_sa(x, c);
3016 case XFRM_MSG_FLUSHSA:
3017 return key_notify_sa_flush(c);
3018 case XFRM_MSG_NEWAE: /* not yet supported */
3019 break;
3020 default:
3021 pr_err("pfkey: Unknown SA event %d\n", c->event);
3022 break;
3025 return 0;
3028 static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3030 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3031 return 0;
3033 switch (c->event) {
3034 case XFRM_MSG_POLEXPIRE:
3035 return key_notify_policy_expire(xp, c);
3036 case XFRM_MSG_DELPOLICY:
3037 case XFRM_MSG_NEWPOLICY:
3038 case XFRM_MSG_UPDPOLICY:
3039 return key_notify_policy(xp, dir, c);
3040 case XFRM_MSG_FLUSHPOLICY:
3041 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3042 break;
3043 return key_notify_policy_flush(c);
3044 default:
3045 pr_err("pfkey: Unknown policy event %d\n", c->event);
3046 break;
3049 return 0;
3052 static u32 get_acqseq(void)
3054 u32 res;
3055 static atomic_t acqseq;
3057 do {
3058 res = atomic_inc_return(&acqseq);
3059 } while (!res);
3060 return res;
3063 static bool pfkey_is_alive(const struct km_event *c)
3065 struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id);
3066 struct sock *sk;
3067 bool is_alive = false;
3069 rcu_read_lock();
3070 sk_for_each_rcu(sk, &net_pfkey->table) {
3071 if (pfkey_sk(sk)->registered) {
3072 is_alive = true;
3073 break;
3076 rcu_read_unlock();
3078 return is_alive;
3081 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
3083 struct sk_buff *skb;
3084 struct sadb_msg *hdr;
3085 struct sadb_address *addr;
3086 struct sadb_x_policy *pol;
3087 int sockaddr_size;
3088 int size;
3089 struct sadb_x_sec_ctx *sec_ctx;
3090 struct xfrm_sec_ctx *xfrm_ctx;
3091 int ctx_size = 0;
3093 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3094 if (!sockaddr_size)
3095 return -EINVAL;
3097 size = sizeof(struct sadb_msg) +
3098 (sizeof(struct sadb_address) * 2) +
3099 (sockaddr_size * 2) +
3100 sizeof(struct sadb_x_policy);
3102 if (x->id.proto == IPPROTO_AH)
3103 size += count_ah_combs(t);
3104 else if (x->id.proto == IPPROTO_ESP)
3105 size += count_esp_combs(t);
3107 if ((xfrm_ctx = x->security)) {
3108 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3109 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3112 skb = alloc_skb(size + 16, GFP_ATOMIC);
3113 if (skb == NULL)
3114 return -ENOMEM;
3116 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3117 hdr->sadb_msg_version = PF_KEY_V2;
3118 hdr->sadb_msg_type = SADB_ACQUIRE;
3119 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3120 hdr->sadb_msg_len = size / sizeof(uint64_t);
3121 hdr->sadb_msg_errno = 0;
3122 hdr->sadb_msg_reserved = 0;
3123 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3124 hdr->sadb_msg_pid = 0;
3126 /* src address */
3127 addr = (struct sadb_address*) skb_put(skb,
3128 sizeof(struct sadb_address)+sockaddr_size);
3129 addr->sadb_address_len =
3130 (sizeof(struct sadb_address)+sockaddr_size)/
3131 sizeof(uint64_t);
3132 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3133 addr->sadb_address_proto = 0;
3134 addr->sadb_address_reserved = 0;
3135 addr->sadb_address_prefixlen =
3136 pfkey_sockaddr_fill(&x->props.saddr, 0,
3137 (struct sockaddr *) (addr + 1),
3138 x->props.family);
3139 if (!addr->sadb_address_prefixlen)
3140 BUG();
3142 /* dst address */
3143 addr = (struct sadb_address*) skb_put(skb,
3144 sizeof(struct sadb_address)+sockaddr_size);
3145 addr->sadb_address_len =
3146 (sizeof(struct sadb_address)+sockaddr_size)/
3147 sizeof(uint64_t);
3148 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3149 addr->sadb_address_proto = 0;
3150 addr->sadb_address_reserved = 0;
3151 addr->sadb_address_prefixlen =
3152 pfkey_sockaddr_fill(&x->id.daddr, 0,
3153 (struct sockaddr *) (addr + 1),
3154 x->props.family);
3155 if (!addr->sadb_address_prefixlen)
3156 BUG();
3158 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3159 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3160 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3161 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3162 pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1;
3163 pol->sadb_x_policy_reserved = 0;
3164 pol->sadb_x_policy_id = xp->index;
3165 pol->sadb_x_policy_priority = xp->priority;
3167 /* Set sadb_comb's. */
3168 if (x->id.proto == IPPROTO_AH)
3169 dump_ah_combs(skb, t);
3170 else if (x->id.proto == IPPROTO_ESP)
3171 dump_esp_combs(skb, t);
3173 /* security context */
3174 if (xfrm_ctx) {
3175 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3176 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3177 sec_ctx->sadb_x_sec_len =
3178 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3179 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3180 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3181 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3182 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3183 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3184 xfrm_ctx->ctx_len);
3187 return pfkey_broadcast(skb, BROADCAST_REGISTERED, NULL, xs_net(x));
3190 static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
3191 u8 *data, int len, int *dir)
3193 struct net *net = sock_net(sk);
3194 struct xfrm_policy *xp;
3195 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
3196 struct sadb_x_sec_ctx *sec_ctx;
3198 switch (sk->sk_family) {
3199 case AF_INET:
3200 if (opt != IP_IPSEC_POLICY) {
3201 *dir = -EOPNOTSUPP;
3202 return NULL;
3204 break;
3205 #if IS_ENABLED(CONFIG_IPV6)
3206 case AF_INET6:
3207 if (opt != IPV6_IPSEC_POLICY) {
3208 *dir = -EOPNOTSUPP;
3209 return NULL;
3211 break;
3212 #endif
3213 default:
3214 *dir = -EINVAL;
3215 return NULL;
3218 *dir = -EINVAL;
3220 if (len < sizeof(struct sadb_x_policy) ||
3221 pol->sadb_x_policy_len*8 > len ||
3222 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3223 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3224 return NULL;
3226 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3227 if (xp == NULL) {
3228 *dir = -ENOBUFS;
3229 return NULL;
3232 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3233 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3235 xp->lft.soft_byte_limit = XFRM_INF;
3236 xp->lft.hard_byte_limit = XFRM_INF;
3237 xp->lft.soft_packet_limit = XFRM_INF;
3238 xp->lft.hard_packet_limit = XFRM_INF;
3239 xp->family = sk->sk_family;
3241 xp->xfrm_nr = 0;
3242 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3243 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3244 goto out;
3246 /* security context too */
3247 if (len >= (pol->sadb_x_policy_len*8 +
3248 sizeof(struct sadb_x_sec_ctx))) {
3249 char *p = (char *)pol;
3250 struct xfrm_user_sec_ctx *uctx;
3252 p += pol->sadb_x_policy_len*8;
3253 sec_ctx = (struct sadb_x_sec_ctx *)p;
3254 if (len < pol->sadb_x_policy_len*8 +
3255 sec_ctx->sadb_x_sec_len) {
3256 *dir = -EINVAL;
3257 goto out;
3259 if ((*dir = verify_sec_ctx_len(p)))
3260 goto out;
3261 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_ATOMIC);
3262 *dir = security_xfrm_policy_alloc(&xp->security, uctx, GFP_ATOMIC);
3263 kfree(uctx);
3265 if (*dir)
3266 goto out;
3269 *dir = pol->sadb_x_policy_dir-1;
3270 return xp;
3272 out:
3273 xp->walk.dead = 1;
3274 xfrm_policy_destroy(xp);
3275 return NULL;
3278 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
3280 struct sk_buff *skb;
3281 struct sadb_msg *hdr;
3282 struct sadb_sa *sa;
3283 struct sadb_address *addr;
3284 struct sadb_x_nat_t_port *n_port;
3285 int sockaddr_size;
3286 int size;
3287 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3288 struct xfrm_encap_tmpl *natt = NULL;
3290 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3291 if (!sockaddr_size)
3292 return -EINVAL;
3294 if (!satype)
3295 return -EINVAL;
3297 if (!x->encap)
3298 return -EINVAL;
3300 natt = x->encap;
3302 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3304 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3305 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3308 size = sizeof(struct sadb_msg) +
3309 sizeof(struct sadb_sa) +
3310 (sizeof(struct sadb_address) * 2) +
3311 (sockaddr_size * 2) +
3312 (sizeof(struct sadb_x_nat_t_port) * 2);
3314 skb = alloc_skb(size + 16, GFP_ATOMIC);
3315 if (skb == NULL)
3316 return -ENOMEM;
3318 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3319 hdr->sadb_msg_version = PF_KEY_V2;
3320 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3321 hdr->sadb_msg_satype = satype;
3322 hdr->sadb_msg_len = size / sizeof(uint64_t);
3323 hdr->sadb_msg_errno = 0;
3324 hdr->sadb_msg_reserved = 0;
3325 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3326 hdr->sadb_msg_pid = 0;
3328 /* SA */
3329 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3330 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3331 sa->sadb_sa_exttype = SADB_EXT_SA;
3332 sa->sadb_sa_spi = x->id.spi;
3333 sa->sadb_sa_replay = 0;
3334 sa->sadb_sa_state = 0;
3335 sa->sadb_sa_auth = 0;
3336 sa->sadb_sa_encrypt = 0;
3337 sa->sadb_sa_flags = 0;
3339 /* ADDRESS_SRC (old addr) */
3340 addr = (struct sadb_address*)
3341 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3342 addr->sadb_address_len =
3343 (sizeof(struct sadb_address)+sockaddr_size)/
3344 sizeof(uint64_t);
3345 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3346 addr->sadb_address_proto = 0;
3347 addr->sadb_address_reserved = 0;
3348 addr->sadb_address_prefixlen =
3349 pfkey_sockaddr_fill(&x->props.saddr, 0,
3350 (struct sockaddr *) (addr + 1),
3351 x->props.family);
3352 if (!addr->sadb_address_prefixlen)
3353 BUG();
3355 /* NAT_T_SPORT (old port) */
3356 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3357 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3358 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3359 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3360 n_port->sadb_x_nat_t_port_reserved = 0;
3362 /* ADDRESS_DST (new addr) */
3363 addr = (struct sadb_address*)
3364 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3365 addr->sadb_address_len =
3366 (sizeof(struct sadb_address)+sockaddr_size)/
3367 sizeof(uint64_t);
3368 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3369 addr->sadb_address_proto = 0;
3370 addr->sadb_address_reserved = 0;
3371 addr->sadb_address_prefixlen =
3372 pfkey_sockaddr_fill(ipaddr, 0,
3373 (struct sockaddr *) (addr + 1),
3374 x->props.family);
3375 if (!addr->sadb_address_prefixlen)
3376 BUG();
3378 /* NAT_T_DPORT (new port) */
3379 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3380 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3381 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3382 n_port->sadb_x_nat_t_port_port = sport;
3383 n_port->sadb_x_nat_t_port_reserved = 0;
3385 return pfkey_broadcast(skb, BROADCAST_REGISTERED, NULL, xs_net(x));
3388 #ifdef CONFIG_NET_KEY_MIGRATE
3389 static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3390 const struct xfrm_selector *sel)
3392 struct sadb_address *addr;
3393 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3394 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3395 addr->sadb_address_exttype = type;
3396 addr->sadb_address_proto = sel->proto;
3397 addr->sadb_address_reserved = 0;
3399 switch (type) {
3400 case SADB_EXT_ADDRESS_SRC:
3401 addr->sadb_address_prefixlen = sel->prefixlen_s;
3402 pfkey_sockaddr_fill(&sel->saddr, 0,
3403 (struct sockaddr *)(addr + 1),
3404 sel->family);
3405 break;
3406 case SADB_EXT_ADDRESS_DST:
3407 addr->sadb_address_prefixlen = sel->prefixlen_d;
3408 pfkey_sockaddr_fill(&sel->daddr, 0,
3409 (struct sockaddr *)(addr + 1),
3410 sel->family);
3411 break;
3412 default:
3413 return -EINVAL;
3416 return 0;
3420 static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
3422 struct sadb_x_kmaddress *kma;
3423 u8 *sa;
3424 int family = k->family;
3425 int socklen = pfkey_sockaddr_len(family);
3426 int size_req;
3428 size_req = (sizeof(struct sadb_x_kmaddress) +
3429 pfkey_sockaddr_pair_size(family));
3431 kma = (struct sadb_x_kmaddress *)skb_put(skb, size_req);
3432 memset(kma, 0, size_req);
3433 kma->sadb_x_kmaddress_len = size_req / 8;
3434 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3435 kma->sadb_x_kmaddress_reserved = k->reserved;
3437 sa = (u8 *)(kma + 1);
3438 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3439 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3440 return -EINVAL;
3442 return 0;
3445 static int set_ipsecrequest(struct sk_buff *skb,
3446 uint8_t proto, uint8_t mode, int level,
3447 uint32_t reqid, uint8_t family,
3448 const xfrm_address_t *src, const xfrm_address_t *dst)
3450 struct sadb_x_ipsecrequest *rq;
3451 u8 *sa;
3452 int socklen = pfkey_sockaddr_len(family);
3453 int size_req;
3455 size_req = sizeof(struct sadb_x_ipsecrequest) +
3456 pfkey_sockaddr_pair_size(family);
3458 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3459 memset(rq, 0, size_req);
3460 rq->sadb_x_ipsecrequest_len = size_req;
3461 rq->sadb_x_ipsecrequest_proto = proto;
3462 rq->sadb_x_ipsecrequest_mode = mode;
3463 rq->sadb_x_ipsecrequest_level = level;
3464 rq->sadb_x_ipsecrequest_reqid = reqid;
3466 sa = (u8 *) (rq + 1);
3467 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3468 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
3469 return -EINVAL;
3471 return 0;
3473 #endif
3475 #ifdef CONFIG_NET_KEY_MIGRATE
3476 static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3477 const struct xfrm_migrate *m, int num_bundles,
3478 const struct xfrm_kmaddress *k)
3480 int i;
3481 int sasize_sel;
3482 int size = 0;
3483 int size_pol = 0;
3484 struct sk_buff *skb;
3485 struct sadb_msg *hdr;
3486 struct sadb_x_policy *pol;
3487 const struct xfrm_migrate *mp;
3489 if (type != XFRM_POLICY_TYPE_MAIN)
3490 return 0;
3492 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3493 return -EINVAL;
3495 if (k != NULL) {
3496 /* addresses for KM */
3497 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3498 pfkey_sockaddr_pair_size(k->family));
3501 /* selector */
3502 sasize_sel = pfkey_sockaddr_size(sel->family);
3503 if (!sasize_sel)
3504 return -EINVAL;
3505 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3507 /* policy info */
3508 size_pol += sizeof(struct sadb_x_policy);
3510 /* ipsecrequests */
3511 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3512 /* old locator pair */
3513 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3514 pfkey_sockaddr_pair_size(mp->old_family);
3515 /* new locator pair */
3516 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3517 pfkey_sockaddr_pair_size(mp->new_family);
3520 size += sizeof(struct sadb_msg) + size_pol;
3522 /* alloc buffer */
3523 skb = alloc_skb(size, GFP_ATOMIC);
3524 if (skb == NULL)
3525 return -ENOMEM;
3527 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3528 hdr->sadb_msg_version = PF_KEY_V2;
3529 hdr->sadb_msg_type = SADB_X_MIGRATE;
3530 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3531 hdr->sadb_msg_len = size / 8;
3532 hdr->sadb_msg_errno = 0;
3533 hdr->sadb_msg_reserved = 0;
3534 hdr->sadb_msg_seq = 0;
3535 hdr->sadb_msg_pid = 0;
3537 /* Addresses to be used by KM for negotiation, if ext is available */
3538 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
3539 goto err;
3541 /* selector src */
3542 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3544 /* selector dst */
3545 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3547 /* policy information */
3548 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3549 pol->sadb_x_policy_len = size_pol / 8;
3550 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3551 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3552 pol->sadb_x_policy_dir = dir + 1;
3553 pol->sadb_x_policy_reserved = 0;
3554 pol->sadb_x_policy_id = 0;
3555 pol->sadb_x_policy_priority = 0;
3557 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3558 /* old ipsecrequest */
3559 int mode = pfkey_mode_from_xfrm(mp->mode);
3560 if (mode < 0)
3561 goto err;
3562 if (set_ipsecrequest(skb, mp->proto, mode,
3563 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3564 mp->reqid, mp->old_family,
3565 &mp->old_saddr, &mp->old_daddr) < 0)
3566 goto err;
3568 /* new ipsecrequest */
3569 if (set_ipsecrequest(skb, mp->proto, mode,
3570 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3571 mp->reqid, mp->new_family,
3572 &mp->new_saddr, &mp->new_daddr) < 0)
3573 goto err;
3576 /* broadcast migrate message to sockets */
3577 pfkey_broadcast(skb, BROADCAST_ALL, NULL, &init_net);
3579 return 0;
3581 err:
3582 kfree_skb(skb);
3583 return -EINVAL;
3585 #else
3586 static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3587 const struct xfrm_migrate *m, int num_bundles,
3588 const struct xfrm_kmaddress *k)
3590 return -ENOPROTOOPT;
3592 #endif
3594 static int pfkey_sendmsg(struct kiocb *kiocb,
3595 struct socket *sock, struct msghdr *msg, size_t len)
3597 struct sock *sk = sock->sk;
3598 struct sk_buff *skb = NULL;
3599 struct sadb_msg *hdr = NULL;
3600 int err;
3601 struct net *net = sock_net(sk);
3603 err = -EOPNOTSUPP;
3604 if (msg->msg_flags & MSG_OOB)
3605 goto out;
3607 err = -EMSGSIZE;
3608 if ((unsigned int)len > sk->sk_sndbuf - 32)
3609 goto out;
3611 err = -ENOBUFS;
3612 skb = alloc_skb(len, GFP_KERNEL);
3613 if (skb == NULL)
3614 goto out;
3616 err = -EFAULT;
3617 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3618 goto out;
3620 hdr = pfkey_get_base_msg(skb, &err);
3621 if (!hdr)
3622 goto out;
3624 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
3625 err = pfkey_process(sk, skb, hdr);
3626 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
3628 out:
3629 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3630 err = 0;
3631 kfree_skb(skb);
3633 return err ? : len;
3636 static int pfkey_recvmsg(struct kiocb *kiocb,
3637 struct socket *sock, struct msghdr *msg, size_t len,
3638 int flags)
3640 struct sock *sk = sock->sk;
3641 struct pfkey_sock *pfk = pfkey_sk(sk);
3642 struct sk_buff *skb;
3643 int copied, err;
3645 err = -EINVAL;
3646 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3647 goto out;
3649 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3650 if (skb == NULL)
3651 goto out;
3653 copied = skb->len;
3654 if (copied > len) {
3655 msg->msg_flags |= MSG_TRUNC;
3656 copied = len;
3659 skb_reset_transport_header(skb);
3660 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3661 if (err)
3662 goto out_free;
3664 sock_recv_ts_and_drops(msg, sk, skb);
3666 err = (flags & MSG_TRUNC) ? skb->len : copied;
3668 if (pfk->dump.dump != NULL &&
3669 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3670 pfkey_do_dump(pfk);
3672 out_free:
3673 skb_free_datagram(sk, skb);
3674 out:
3675 return err;
3678 static const struct proto_ops pfkey_ops = {
3679 .family = PF_KEY,
3680 .owner = THIS_MODULE,
3681 /* Operations that make no sense on pfkey sockets. */
3682 .bind = sock_no_bind,
3683 .connect = sock_no_connect,
3684 .socketpair = sock_no_socketpair,
3685 .accept = sock_no_accept,
3686 .getname = sock_no_getname,
3687 .ioctl = sock_no_ioctl,
3688 .listen = sock_no_listen,
3689 .shutdown = sock_no_shutdown,
3690 .setsockopt = sock_no_setsockopt,
3691 .getsockopt = sock_no_getsockopt,
3692 .mmap = sock_no_mmap,
3693 .sendpage = sock_no_sendpage,
3695 /* Now the operations that really occur. */
3696 .release = pfkey_release,
3697 .poll = datagram_poll,
3698 .sendmsg = pfkey_sendmsg,
3699 .recvmsg = pfkey_recvmsg,
3702 static const struct net_proto_family pfkey_family_ops = {
3703 .family = PF_KEY,
3704 .create = pfkey_create,
3705 .owner = THIS_MODULE,
3708 #ifdef CONFIG_PROC_FS
3709 static int pfkey_seq_show(struct seq_file *f, void *v)
3711 struct sock *s = sk_entry(v);
3713 if (v == SEQ_START_TOKEN)
3714 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3715 else
3716 seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
3718 atomic_read(&s->sk_refcnt),
3719 sk_rmem_alloc_get(s),
3720 sk_wmem_alloc_get(s),
3721 from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
3722 sock_i_ino(s)
3724 return 0;
3727 static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3728 __acquires(rcu)
3730 struct net *net = seq_file_net(f);
3731 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3733 rcu_read_lock();
3734 return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
3737 static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3739 struct net *net = seq_file_net(f);
3740 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3742 return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
3745 static void pfkey_seq_stop(struct seq_file *f, void *v)
3746 __releases(rcu)
3748 rcu_read_unlock();
3751 static const struct seq_operations pfkey_seq_ops = {
3752 .start = pfkey_seq_start,
3753 .next = pfkey_seq_next,
3754 .stop = pfkey_seq_stop,
3755 .show = pfkey_seq_show,
3758 static int pfkey_seq_open(struct inode *inode, struct file *file)
3760 return seq_open_net(inode, file, &pfkey_seq_ops,
3761 sizeof(struct seq_net_private));
3764 static const struct file_operations pfkey_proc_ops = {
3765 .open = pfkey_seq_open,
3766 .read = seq_read,
3767 .llseek = seq_lseek,
3768 .release = seq_release_net,
3771 static int __net_init pfkey_init_proc(struct net *net)
3773 struct proc_dir_entry *e;
3775 e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops);
3776 if (e == NULL)
3777 return -ENOMEM;
3779 return 0;
3782 static void __net_exit pfkey_exit_proc(struct net *net)
3784 remove_proc_entry("pfkey", net->proc_net);
3786 #else
3787 static inline int pfkey_init_proc(struct net *net)
3789 return 0;
3792 static inline void pfkey_exit_proc(struct net *net)
3795 #endif
3797 static struct xfrm_mgr pfkeyv2_mgr =
3799 .id = "pfkeyv2",
3800 .notify = pfkey_send_notify,
3801 .acquire = pfkey_send_acquire,
3802 .compile_policy = pfkey_compile_policy,
3803 .new_mapping = pfkey_send_new_mapping,
3804 .notify_policy = pfkey_send_policy_notify,
3805 .migrate = pfkey_send_migrate,
3806 .is_alive = pfkey_is_alive,
3809 static int __net_init pfkey_net_init(struct net *net)
3811 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3812 int rv;
3814 INIT_HLIST_HEAD(&net_pfkey->table);
3815 atomic_set(&net_pfkey->socks_nr, 0);
3817 rv = pfkey_init_proc(net);
3819 return rv;
3822 static void __net_exit pfkey_net_exit(struct net *net)
3824 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3826 pfkey_exit_proc(net);
3827 BUG_ON(!hlist_empty(&net_pfkey->table));
3830 static struct pernet_operations pfkey_net_ops = {
3831 .init = pfkey_net_init,
3832 .exit = pfkey_net_exit,
3833 .id = &pfkey_net_id,
3834 .size = sizeof(struct netns_pfkey),
3837 static void __exit ipsec_pfkey_exit(void)
3839 xfrm_unregister_km(&pfkeyv2_mgr);
3840 sock_unregister(PF_KEY);
3841 unregister_pernet_subsys(&pfkey_net_ops);
3842 proto_unregister(&key_proto);
3845 static int __init ipsec_pfkey_init(void)
3847 int err = proto_register(&key_proto, 0);
3849 if (err != 0)
3850 goto out;
3852 err = register_pernet_subsys(&pfkey_net_ops);
3853 if (err != 0)
3854 goto out_unregister_key_proto;
3855 err = sock_register(&pfkey_family_ops);
3856 if (err != 0)
3857 goto out_unregister_pernet;
3858 err = xfrm_register_km(&pfkeyv2_mgr);
3859 if (err != 0)
3860 goto out_sock_unregister;
3861 out:
3862 return err;
3864 out_sock_unregister:
3865 sock_unregister(PF_KEY);
3866 out_unregister_pernet:
3867 unregister_pernet_subsys(&pfkey_net_ops);
3868 out_unregister_key_proto:
3869 proto_unregister(&key_proto);
3870 goto out;
3873 module_init(ipsec_pfkey_init);
3874 module_exit(ipsec_pfkey_exit);
3875 MODULE_LICENSE("GPL");
3876 MODULE_ALIAS_NETPROTO(PF_KEY);