Merge tag 'staging-4.14-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-stable.git] / net / key / af_key.c
bloba00d607e7224d422e6f3ae3b139bee9706852d7b
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 unsigned 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;
66 struct mutex dump_lock;
69 static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
70 xfrm_address_t *saddr, xfrm_address_t *daddr,
71 u16 *family);
73 static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
75 return (struct pfkey_sock *)sk;
78 static int pfkey_can_dump(const struct sock *sk)
80 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
81 return 1;
82 return 0;
85 static void pfkey_terminate_dump(struct pfkey_sock *pfk)
87 if (pfk->dump.dump) {
88 if (pfk->dump.skb) {
89 kfree_skb(pfk->dump.skb);
90 pfk->dump.skb = NULL;
92 pfk->dump.done(pfk);
93 pfk->dump.dump = NULL;
94 pfk->dump.done = NULL;
98 static void pfkey_sock_destruct(struct sock *sk)
100 struct net *net = sock_net(sk);
101 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
103 pfkey_terminate_dump(pfkey_sk(sk));
104 skb_queue_purge(&sk->sk_receive_queue);
106 if (!sock_flag(sk, SOCK_DEAD)) {
107 pr_err("Attempt to release alive pfkey socket: %p\n", sk);
108 return;
111 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
112 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
114 atomic_dec(&net_pfkey->socks_nr);
117 static const struct proto_ops pfkey_ops;
119 static void pfkey_insert(struct sock *sk)
121 struct net *net = sock_net(sk);
122 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
124 mutex_lock(&pfkey_mutex);
125 sk_add_node_rcu(sk, &net_pfkey->table);
126 mutex_unlock(&pfkey_mutex);
129 static void pfkey_remove(struct sock *sk)
131 mutex_lock(&pfkey_mutex);
132 sk_del_node_init_rcu(sk);
133 mutex_unlock(&pfkey_mutex);
136 static struct proto key_proto = {
137 .name = "KEY",
138 .owner = THIS_MODULE,
139 .obj_size = sizeof(struct pfkey_sock),
142 static int pfkey_create(struct net *net, struct socket *sock, int protocol,
143 int kern)
145 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
146 struct sock *sk;
147 struct pfkey_sock *pfk;
148 int err;
150 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
151 return -EPERM;
152 if (sock->type != SOCK_RAW)
153 return -ESOCKTNOSUPPORT;
154 if (protocol != PF_KEY_V2)
155 return -EPROTONOSUPPORT;
157 err = -ENOMEM;
158 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto, kern);
159 if (sk == NULL)
160 goto out;
162 pfk = pfkey_sk(sk);
163 mutex_init(&pfk->dump_lock);
165 sock->ops = &pfkey_ops;
166 sock_init_data(sock, sk);
168 sk->sk_family = PF_KEY;
169 sk->sk_destruct = pfkey_sock_destruct;
171 atomic_inc(&net_pfkey->socks_nr);
173 pfkey_insert(sk);
175 return 0;
176 out:
177 return err;
180 static int pfkey_release(struct socket *sock)
182 struct sock *sk = sock->sk;
184 if (!sk)
185 return 0;
187 pfkey_remove(sk);
189 sock_orphan(sk);
190 sock->sk = NULL;
191 skb_queue_purge(&sk->sk_write_queue);
193 synchronize_rcu();
194 sock_put(sk);
196 return 0;
199 static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
200 gfp_t allocation, struct sock *sk)
202 int err = -ENOBUFS;
204 sock_hold(sk);
205 if (*skb2 == NULL) {
206 if (refcount_read(&skb->users) != 1) {
207 *skb2 = skb_clone(skb, allocation);
208 } else {
209 *skb2 = skb;
210 refcount_inc(&skb->users);
213 if (*skb2 != NULL) {
214 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
215 skb_set_owner_r(*skb2, sk);
216 skb_queue_tail(&sk->sk_receive_queue, *skb2);
217 sk->sk_data_ready(sk);
218 *skb2 = NULL;
219 err = 0;
222 sock_put(sk);
223 return err;
226 /* Send SKB to all pfkey sockets matching selected criteria. */
227 #define BROADCAST_ALL 0
228 #define BROADCAST_ONE 1
229 #define BROADCAST_REGISTERED 2
230 #define BROADCAST_PROMISC_ONLY 4
231 static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
232 int broadcast_flags, struct sock *one_sk,
233 struct net *net)
235 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
236 struct sock *sk;
237 struct sk_buff *skb2 = NULL;
238 int err = -ESRCH;
240 /* XXX Do we need something like netlink_overrun? I think
241 * XXX PF_KEY socket apps will not mind current behavior.
243 if (!skb)
244 return -ENOMEM;
246 rcu_read_lock();
247 sk_for_each_rcu(sk, &net_pfkey->table) {
248 struct pfkey_sock *pfk = pfkey_sk(sk);
249 int err2;
251 /* Yes, it means that if you are meant to receive this
252 * pfkey message you receive it twice as promiscuous
253 * socket.
255 if (pfk->promisc)
256 pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk);
258 /* the exact target will be processed later */
259 if (sk == one_sk)
260 continue;
261 if (broadcast_flags != BROADCAST_ALL) {
262 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
263 continue;
264 if ((broadcast_flags & BROADCAST_REGISTERED) &&
265 !pfk->registered)
266 continue;
267 if (broadcast_flags & BROADCAST_ONE)
268 continue;
271 err2 = pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk);
273 /* Error is cleared after successful sending to at least one
274 * registered KM */
275 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
276 err = err2;
278 rcu_read_unlock();
280 if (one_sk != NULL)
281 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
283 kfree_skb(skb2);
284 kfree_skb(skb);
285 return err;
288 static int pfkey_do_dump(struct pfkey_sock *pfk)
290 struct sadb_msg *hdr;
291 int rc;
293 mutex_lock(&pfk->dump_lock);
294 if (!pfk->dump.dump) {
295 rc = 0;
296 goto out;
299 rc = pfk->dump.dump(pfk);
300 if (rc == -ENOBUFS) {
301 rc = 0;
302 goto out;
305 if (pfk->dump.skb) {
306 if (!pfkey_can_dump(&pfk->sk)) {
307 rc = 0;
308 goto out;
311 hdr = (struct sadb_msg *) pfk->dump.skb->data;
312 hdr->sadb_msg_seq = 0;
313 hdr->sadb_msg_errno = rc;
314 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
315 &pfk->sk, sock_net(&pfk->sk));
316 pfk->dump.skb = NULL;
319 pfkey_terminate_dump(pfk);
321 out:
322 mutex_unlock(&pfk->dump_lock);
323 return rc;
326 static inline void pfkey_hdr_dup(struct sadb_msg *new,
327 const struct sadb_msg *orig)
329 *new = *orig;
332 static int pfkey_error(const struct sadb_msg *orig, int err, struct sock *sk)
334 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
335 struct sadb_msg *hdr;
337 if (!skb)
338 return -ENOBUFS;
340 /* Woe be to the platform trying to support PFKEY yet
341 * having normal errnos outside the 1-255 range, inclusive.
343 err = -err;
344 if (err == ERESTARTSYS ||
345 err == ERESTARTNOHAND ||
346 err == ERESTARTNOINTR)
347 err = EINTR;
348 if (err >= 512)
349 err = EINVAL;
350 BUG_ON(err <= 0 || err >= 256);
352 hdr = skb_put(skb, sizeof(struct sadb_msg));
353 pfkey_hdr_dup(hdr, orig);
354 hdr->sadb_msg_errno = (uint8_t) err;
355 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
356 sizeof(uint64_t));
358 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk, sock_net(sk));
360 return 0;
363 static const u8 sadb_ext_min_len[] = {
364 [SADB_EXT_RESERVED] = (u8) 0,
365 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
366 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
367 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
368 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
369 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
370 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
371 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
372 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
373 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
374 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
375 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
376 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
377 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
378 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
379 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
380 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
381 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
382 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
383 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
384 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
385 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
386 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
387 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
388 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
389 [SADB_X_EXT_KMADDRESS] = (u8) sizeof(struct sadb_x_kmaddress),
390 [SADB_X_EXT_FILTER] = (u8) sizeof(struct sadb_x_filter),
393 /* Verify sadb_address_{len,prefixlen} against sa_family. */
394 static int verify_address_len(const void *p)
396 const struct sadb_address *sp = p;
397 const struct sockaddr *addr = (const struct sockaddr *)(sp + 1);
398 const struct sockaddr_in *sin;
399 #if IS_ENABLED(CONFIG_IPV6)
400 const struct sockaddr_in6 *sin6;
401 #endif
402 int len;
404 switch (addr->sa_family) {
405 case AF_INET:
406 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
407 if (sp->sadb_address_len != len ||
408 sp->sadb_address_prefixlen > 32)
409 return -EINVAL;
410 break;
411 #if IS_ENABLED(CONFIG_IPV6)
412 case AF_INET6:
413 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
414 if (sp->sadb_address_len != len ||
415 sp->sadb_address_prefixlen > 128)
416 return -EINVAL;
417 break;
418 #endif
419 default:
420 /* It is user using kernel to keep track of security
421 * associations for another protocol, such as
422 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
423 * lengths.
425 * XXX Actually, association/policy database is not yet
426 * XXX able to cope with arbitrary sockaddr families.
427 * XXX When it can, remove this -EINVAL. -DaveM
429 return -EINVAL;
432 return 0;
435 static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
437 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
438 sec_ctx->sadb_x_ctx_len,
439 sizeof(uint64_t));
442 static inline int verify_sec_ctx_len(const void *p)
444 const struct sadb_x_sec_ctx *sec_ctx = p;
445 int len = sec_ctx->sadb_x_ctx_len;
447 if (len > PAGE_SIZE)
448 return -EINVAL;
450 len = pfkey_sec_ctx_len(sec_ctx);
452 if (sec_ctx->sadb_x_sec_len != len)
453 return -EINVAL;
455 return 0;
458 static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx,
459 gfp_t gfp)
461 struct xfrm_user_sec_ctx *uctx = NULL;
462 int ctx_size = sec_ctx->sadb_x_ctx_len;
464 uctx = kmalloc((sizeof(*uctx)+ctx_size), gfp);
466 if (!uctx)
467 return NULL;
469 uctx->len = pfkey_sec_ctx_len(sec_ctx);
470 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
471 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
472 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
473 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
474 memcpy(uctx + 1, sec_ctx + 1,
475 uctx->ctx_len);
477 return uctx;
480 static int present_and_same_family(const struct sadb_address *src,
481 const struct sadb_address *dst)
483 const struct sockaddr *s_addr, *d_addr;
485 if (!src || !dst)
486 return 0;
488 s_addr = (const struct sockaddr *)(src + 1);
489 d_addr = (const struct sockaddr *)(dst + 1);
490 if (s_addr->sa_family != d_addr->sa_family)
491 return 0;
492 if (s_addr->sa_family != AF_INET
493 #if IS_ENABLED(CONFIG_IPV6)
494 && s_addr->sa_family != AF_INET6
495 #endif
497 return 0;
499 return 1;
502 static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
504 const char *p = (char *) hdr;
505 int len = skb->len;
507 len -= sizeof(*hdr);
508 p += sizeof(*hdr);
509 while (len > 0) {
510 const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
511 uint16_t ext_type;
512 int ext_len;
514 ext_len = ehdr->sadb_ext_len;
515 ext_len *= sizeof(uint64_t);
516 ext_type = ehdr->sadb_ext_type;
517 if (ext_len < sizeof(uint64_t) ||
518 ext_len > len ||
519 ext_type == SADB_EXT_RESERVED)
520 return -EINVAL;
522 if (ext_type <= SADB_EXT_MAX) {
523 int min = (int) sadb_ext_min_len[ext_type];
524 if (ext_len < min)
525 return -EINVAL;
526 if (ext_hdrs[ext_type-1] != NULL)
527 return -EINVAL;
528 if (ext_type == SADB_EXT_ADDRESS_SRC ||
529 ext_type == SADB_EXT_ADDRESS_DST ||
530 ext_type == SADB_EXT_ADDRESS_PROXY ||
531 ext_type == SADB_X_EXT_NAT_T_OA) {
532 if (verify_address_len(p))
533 return -EINVAL;
535 if (ext_type == SADB_X_EXT_SEC_CTX) {
536 if (verify_sec_ctx_len(p))
537 return -EINVAL;
539 ext_hdrs[ext_type-1] = (void *) p;
541 p += ext_len;
542 len -= ext_len;
545 return 0;
548 static uint16_t
549 pfkey_satype2proto(uint8_t satype)
551 switch (satype) {
552 case SADB_SATYPE_UNSPEC:
553 return IPSEC_PROTO_ANY;
554 case SADB_SATYPE_AH:
555 return IPPROTO_AH;
556 case SADB_SATYPE_ESP:
557 return IPPROTO_ESP;
558 case SADB_X_SATYPE_IPCOMP:
559 return IPPROTO_COMP;
560 default:
561 return 0;
563 /* NOTREACHED */
566 static uint8_t
567 pfkey_proto2satype(uint16_t proto)
569 switch (proto) {
570 case IPPROTO_AH:
571 return SADB_SATYPE_AH;
572 case IPPROTO_ESP:
573 return SADB_SATYPE_ESP;
574 case IPPROTO_COMP:
575 return SADB_X_SATYPE_IPCOMP;
576 default:
577 return 0;
579 /* NOTREACHED */
582 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
583 * say specifically 'just raw sockets' as we encode them as 255.
586 static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
588 return proto == IPSEC_PROTO_ANY ? 0 : proto;
591 static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
593 return proto ? proto : IPSEC_PROTO_ANY;
596 static inline int pfkey_sockaddr_len(sa_family_t family)
598 switch (family) {
599 case AF_INET:
600 return sizeof(struct sockaddr_in);
601 #if IS_ENABLED(CONFIG_IPV6)
602 case AF_INET6:
603 return sizeof(struct sockaddr_in6);
604 #endif
606 return 0;
609 static
610 int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
612 switch (sa->sa_family) {
613 case AF_INET:
614 xaddr->a4 =
615 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
616 return AF_INET;
617 #if IS_ENABLED(CONFIG_IPV6)
618 case AF_INET6:
619 memcpy(xaddr->a6,
620 &((struct sockaddr_in6 *)sa)->sin6_addr,
621 sizeof(struct in6_addr));
622 return AF_INET6;
623 #endif
625 return 0;
628 static
629 int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
631 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
632 xaddr);
635 static struct xfrm_state *pfkey_xfrm_state_lookup(struct net *net, const struct sadb_msg *hdr, void * const *ext_hdrs)
637 const struct sadb_sa *sa;
638 const struct sadb_address *addr;
639 uint16_t proto;
640 unsigned short family;
641 xfrm_address_t *xaddr;
643 sa = ext_hdrs[SADB_EXT_SA - 1];
644 if (sa == NULL)
645 return NULL;
647 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
648 if (proto == 0)
649 return NULL;
651 /* sadb_address_len should be checked by caller */
652 addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
653 if (addr == NULL)
654 return NULL;
656 family = ((const struct sockaddr *)(addr + 1))->sa_family;
657 switch (family) {
658 case AF_INET:
659 xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
660 break;
661 #if IS_ENABLED(CONFIG_IPV6)
662 case AF_INET6:
663 xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
664 break;
665 #endif
666 default:
667 xaddr = NULL;
670 if (!xaddr)
671 return NULL;
673 return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
676 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
678 static int
679 pfkey_sockaddr_size(sa_family_t family)
681 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
684 static inline int pfkey_mode_from_xfrm(int mode)
686 switch(mode) {
687 case XFRM_MODE_TRANSPORT:
688 return IPSEC_MODE_TRANSPORT;
689 case XFRM_MODE_TUNNEL:
690 return IPSEC_MODE_TUNNEL;
691 case XFRM_MODE_BEET:
692 return IPSEC_MODE_BEET;
693 default:
694 return -1;
698 static inline int pfkey_mode_to_xfrm(int mode)
700 switch(mode) {
701 case IPSEC_MODE_ANY: /*XXX*/
702 case IPSEC_MODE_TRANSPORT:
703 return XFRM_MODE_TRANSPORT;
704 case IPSEC_MODE_TUNNEL:
705 return XFRM_MODE_TUNNEL;
706 case IPSEC_MODE_BEET:
707 return XFRM_MODE_BEET;
708 default:
709 return -1;
713 static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
714 struct sockaddr *sa,
715 unsigned short family)
717 switch (family) {
718 case AF_INET:
720 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
721 sin->sin_family = AF_INET;
722 sin->sin_port = port;
723 sin->sin_addr.s_addr = xaddr->a4;
724 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
725 return 32;
727 #if IS_ENABLED(CONFIG_IPV6)
728 case AF_INET6:
730 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
731 sin6->sin6_family = AF_INET6;
732 sin6->sin6_port = port;
733 sin6->sin6_flowinfo = 0;
734 sin6->sin6_addr = xaddr->in6;
735 sin6->sin6_scope_id = 0;
736 return 128;
738 #endif
740 return 0;
743 static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
744 int add_keys, int hsc)
746 struct sk_buff *skb;
747 struct sadb_msg *hdr;
748 struct sadb_sa *sa;
749 struct sadb_lifetime *lifetime;
750 struct sadb_address *addr;
751 struct sadb_key *key;
752 struct sadb_x_sa2 *sa2;
753 struct sadb_x_sec_ctx *sec_ctx;
754 struct xfrm_sec_ctx *xfrm_ctx;
755 int ctx_size = 0;
756 int size;
757 int auth_key_size = 0;
758 int encrypt_key_size = 0;
759 int sockaddr_size;
760 struct xfrm_encap_tmpl *natt = NULL;
761 int mode;
763 /* address family check */
764 sockaddr_size = pfkey_sockaddr_size(x->props.family);
765 if (!sockaddr_size)
766 return ERR_PTR(-EINVAL);
768 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
769 key(AE), (identity(SD),) (sensitivity)> */
770 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
771 sizeof(struct sadb_lifetime) +
772 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
773 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
774 sizeof(struct sadb_address)*2 +
775 sockaddr_size*2 +
776 sizeof(struct sadb_x_sa2);
778 if ((xfrm_ctx = x->security)) {
779 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
780 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
783 /* identity & sensitivity */
784 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family))
785 size += sizeof(struct sadb_address) + sockaddr_size;
787 if (add_keys) {
788 if (x->aalg && x->aalg->alg_key_len) {
789 auth_key_size =
790 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
791 size += sizeof(struct sadb_key) + auth_key_size;
793 if (x->ealg && x->ealg->alg_key_len) {
794 encrypt_key_size =
795 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
796 size += sizeof(struct sadb_key) + encrypt_key_size;
799 if (x->encap)
800 natt = x->encap;
802 if (natt && natt->encap_type) {
803 size += sizeof(struct sadb_x_nat_t_type);
804 size += sizeof(struct sadb_x_nat_t_port);
805 size += sizeof(struct sadb_x_nat_t_port);
808 skb = alloc_skb(size + 16, GFP_ATOMIC);
809 if (skb == NULL)
810 return ERR_PTR(-ENOBUFS);
812 /* call should fill header later */
813 hdr = skb_put(skb, sizeof(struct sadb_msg));
814 memset(hdr, 0, size); /* XXX do we need this ? */
815 hdr->sadb_msg_len = size / sizeof(uint64_t);
817 /* sa */
818 sa = skb_put(skb, sizeof(struct sadb_sa));
819 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
820 sa->sadb_sa_exttype = SADB_EXT_SA;
821 sa->sadb_sa_spi = x->id.spi;
822 sa->sadb_sa_replay = x->props.replay_window;
823 switch (x->km.state) {
824 case XFRM_STATE_VALID:
825 sa->sadb_sa_state = x->km.dying ?
826 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
827 break;
828 case XFRM_STATE_ACQ:
829 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
830 break;
831 default:
832 sa->sadb_sa_state = SADB_SASTATE_DEAD;
833 break;
835 sa->sadb_sa_auth = 0;
836 if (x->aalg) {
837 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
838 sa->sadb_sa_auth = (a && a->pfkey_supported) ?
839 a->desc.sadb_alg_id : 0;
841 sa->sadb_sa_encrypt = 0;
842 BUG_ON(x->ealg && x->calg);
843 if (x->ealg) {
844 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
845 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
846 a->desc.sadb_alg_id : 0;
848 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
849 if (x->calg) {
850 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
851 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
852 a->desc.sadb_alg_id : 0;
855 sa->sadb_sa_flags = 0;
856 if (x->props.flags & XFRM_STATE_NOECN)
857 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
858 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
859 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
860 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
861 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
863 /* hard time */
864 if (hsc & 2) {
865 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
866 lifetime->sadb_lifetime_len =
867 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
868 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
869 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
870 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
871 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
872 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
874 /* soft time */
875 if (hsc & 1) {
876 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
877 lifetime->sadb_lifetime_len =
878 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
879 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
880 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
881 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
882 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
883 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
885 /* current time */
886 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
887 lifetime->sadb_lifetime_len =
888 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
889 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
890 lifetime->sadb_lifetime_allocations = x->curlft.packets;
891 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
892 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
893 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
894 /* src address */
895 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
896 addr->sadb_address_len =
897 (sizeof(struct sadb_address)+sockaddr_size)/
898 sizeof(uint64_t);
899 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
900 /* "if the ports are non-zero, then the sadb_address_proto field,
901 normally zero, MUST be filled in with the transport
902 protocol's number." - RFC2367 */
903 addr->sadb_address_proto = 0;
904 addr->sadb_address_reserved = 0;
906 addr->sadb_address_prefixlen =
907 pfkey_sockaddr_fill(&x->props.saddr, 0,
908 (struct sockaddr *) (addr + 1),
909 x->props.family);
910 if (!addr->sadb_address_prefixlen)
911 BUG();
913 /* dst address */
914 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
915 addr->sadb_address_len =
916 (sizeof(struct sadb_address)+sockaddr_size)/
917 sizeof(uint64_t);
918 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
919 addr->sadb_address_proto = 0;
920 addr->sadb_address_reserved = 0;
922 addr->sadb_address_prefixlen =
923 pfkey_sockaddr_fill(&x->id.daddr, 0,
924 (struct sockaddr *) (addr + 1),
925 x->props.family);
926 if (!addr->sadb_address_prefixlen)
927 BUG();
929 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
930 x->props.family)) {
931 addr = skb_put(skb,
932 sizeof(struct sadb_address) + sockaddr_size);
933 addr->sadb_address_len =
934 (sizeof(struct sadb_address)+sockaddr_size)/
935 sizeof(uint64_t);
936 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
937 addr->sadb_address_proto =
938 pfkey_proto_from_xfrm(x->sel.proto);
939 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
940 addr->sadb_address_reserved = 0;
942 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
943 (struct sockaddr *) (addr + 1),
944 x->props.family);
947 /* auth key */
948 if (add_keys && auth_key_size) {
949 key = skb_put(skb, sizeof(struct sadb_key) + auth_key_size);
950 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
951 sizeof(uint64_t);
952 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
953 key->sadb_key_bits = x->aalg->alg_key_len;
954 key->sadb_key_reserved = 0;
955 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
957 /* encrypt key */
958 if (add_keys && encrypt_key_size) {
959 key = skb_put(skb, sizeof(struct sadb_key) + encrypt_key_size);
960 key->sadb_key_len = (sizeof(struct sadb_key) +
961 encrypt_key_size) / sizeof(uint64_t);
962 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
963 key->sadb_key_bits = x->ealg->alg_key_len;
964 key->sadb_key_reserved = 0;
965 memcpy(key + 1, x->ealg->alg_key,
966 (x->ealg->alg_key_len+7)/8);
969 /* sa */
970 sa2 = skb_put(skb, sizeof(struct sadb_x_sa2));
971 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
972 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
973 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
974 kfree_skb(skb);
975 return ERR_PTR(-EINVAL);
977 sa2->sadb_x_sa2_mode = mode;
978 sa2->sadb_x_sa2_reserved1 = 0;
979 sa2->sadb_x_sa2_reserved2 = 0;
980 sa2->sadb_x_sa2_sequence = 0;
981 sa2->sadb_x_sa2_reqid = x->props.reqid;
983 if (natt && natt->encap_type) {
984 struct sadb_x_nat_t_type *n_type;
985 struct sadb_x_nat_t_port *n_port;
987 /* type */
988 n_type = skb_put(skb, sizeof(*n_type));
989 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
990 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
991 n_type->sadb_x_nat_t_type_type = natt->encap_type;
992 n_type->sadb_x_nat_t_type_reserved[0] = 0;
993 n_type->sadb_x_nat_t_type_reserved[1] = 0;
994 n_type->sadb_x_nat_t_type_reserved[2] = 0;
996 /* source port */
997 n_port = skb_put(skb, sizeof(*n_port));
998 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
999 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
1000 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
1001 n_port->sadb_x_nat_t_port_reserved = 0;
1003 /* dest port */
1004 n_port = skb_put(skb, sizeof(*n_port));
1005 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1006 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
1007 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
1008 n_port->sadb_x_nat_t_port_reserved = 0;
1011 /* security context */
1012 if (xfrm_ctx) {
1013 sec_ctx = skb_put(skb,
1014 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1015 sec_ctx->sadb_x_sec_len =
1016 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1017 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1018 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1019 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1020 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1021 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1022 xfrm_ctx->ctx_len);
1025 return skb;
1029 static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
1031 struct sk_buff *skb;
1033 skb = __pfkey_xfrm_state2msg(x, 1, 3);
1035 return skb;
1038 static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
1039 int hsc)
1041 return __pfkey_xfrm_state2msg(x, 0, hsc);
1044 static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
1045 const struct sadb_msg *hdr,
1046 void * const *ext_hdrs)
1048 struct xfrm_state *x;
1049 const struct sadb_lifetime *lifetime;
1050 const struct sadb_sa *sa;
1051 const struct sadb_key *key;
1052 const struct sadb_x_sec_ctx *sec_ctx;
1053 uint16_t proto;
1054 int err;
1057 sa = ext_hdrs[SADB_EXT_SA - 1];
1058 if (!sa ||
1059 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1060 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1061 return ERR_PTR(-EINVAL);
1062 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1063 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1064 return ERR_PTR(-EINVAL);
1065 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1066 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1067 return ERR_PTR(-EINVAL);
1068 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1069 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1070 return ERR_PTR(-EINVAL);
1072 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1073 if (proto == 0)
1074 return ERR_PTR(-EINVAL);
1076 /* default error is no buffer space */
1077 err = -ENOBUFS;
1079 /* RFC2367:
1081 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1082 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1083 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1084 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1085 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1086 not true.
1088 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1089 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1091 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1092 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1093 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1094 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1095 return ERR_PTR(-EINVAL);
1096 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
1097 if (key != NULL &&
1098 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1099 ((key->sadb_key_bits+7) / 8 == 0 ||
1100 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1101 return ERR_PTR(-EINVAL);
1102 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1103 if (key != NULL &&
1104 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1105 ((key->sadb_key_bits+7) / 8 == 0 ||
1106 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1107 return ERR_PTR(-EINVAL);
1109 x = xfrm_state_alloc(net);
1110 if (x == NULL)
1111 return ERR_PTR(-ENOBUFS);
1113 x->id.proto = proto;
1114 x->id.spi = sa->sadb_sa_spi;
1115 x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay,
1116 (sizeof(x->replay.bitmap) * 8));
1117 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1118 x->props.flags |= XFRM_STATE_NOECN;
1119 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1120 x->props.flags |= XFRM_STATE_DECAP_DSCP;
1121 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1122 x->props.flags |= XFRM_STATE_NOPMTUDISC;
1124 lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
1125 if (lifetime != NULL) {
1126 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1127 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1128 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1129 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1131 lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
1132 if (lifetime != NULL) {
1133 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1134 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1135 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1136 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1139 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
1140 if (sec_ctx != NULL) {
1141 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
1143 if (!uctx)
1144 goto out;
1146 err = security_xfrm_state_alloc(x, uctx);
1147 kfree(uctx);
1149 if (err)
1150 goto out;
1153 err = -ENOBUFS;
1154 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
1155 if (sa->sadb_sa_auth) {
1156 int keysize = 0;
1157 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1158 if (!a || !a->pfkey_supported) {
1159 err = -ENOSYS;
1160 goto out;
1162 if (key)
1163 keysize = (key->sadb_key_bits + 7) / 8;
1164 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1165 if (!x->aalg) {
1166 err = -ENOMEM;
1167 goto out;
1169 strcpy(x->aalg->alg_name, a->name);
1170 x->aalg->alg_key_len = 0;
1171 if (key) {
1172 x->aalg->alg_key_len = key->sadb_key_bits;
1173 memcpy(x->aalg->alg_key, key+1, keysize);
1175 x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
1176 x->props.aalgo = sa->sadb_sa_auth;
1177 /* x->algo.flags = sa->sadb_sa_flags; */
1179 if (sa->sadb_sa_encrypt) {
1180 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1181 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1182 if (!a || !a->pfkey_supported) {
1183 err = -ENOSYS;
1184 goto out;
1186 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1187 if (!x->calg) {
1188 err = -ENOMEM;
1189 goto out;
1191 strcpy(x->calg->alg_name, a->name);
1192 x->props.calgo = sa->sadb_sa_encrypt;
1193 } else {
1194 int keysize = 0;
1195 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1196 if (!a || !a->pfkey_supported) {
1197 err = -ENOSYS;
1198 goto out;
1200 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1201 if (key)
1202 keysize = (key->sadb_key_bits + 7) / 8;
1203 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1204 if (!x->ealg) {
1205 err = -ENOMEM;
1206 goto out;
1208 strcpy(x->ealg->alg_name, a->name);
1209 x->ealg->alg_key_len = 0;
1210 if (key) {
1211 x->ealg->alg_key_len = key->sadb_key_bits;
1212 memcpy(x->ealg->alg_key, key+1, keysize);
1214 x->props.ealgo = sa->sadb_sa_encrypt;
1215 x->geniv = a->uinfo.encr.geniv;
1218 /* x->algo.flags = sa->sadb_sa_flags; */
1220 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1221 &x->props.saddr);
1222 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1223 &x->id.daddr);
1225 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1226 const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
1227 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1228 if (mode < 0) {
1229 err = -EINVAL;
1230 goto out;
1232 x->props.mode = mode;
1233 x->props.reqid = sa2->sadb_x_sa2_reqid;
1236 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1237 const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1239 /* Nobody uses this, but we try. */
1240 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1241 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1244 if (!x->sel.family)
1245 x->sel.family = x->props.family;
1247 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1248 const struct sadb_x_nat_t_type* n_type;
1249 struct xfrm_encap_tmpl *natt;
1251 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1252 if (!x->encap) {
1253 err = -ENOMEM;
1254 goto out;
1257 natt = x->encap;
1258 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1259 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1261 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1262 const struct sadb_x_nat_t_port *n_port =
1263 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1264 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1266 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1267 const struct sadb_x_nat_t_port *n_port =
1268 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1269 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1271 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
1274 err = xfrm_init_state(x);
1275 if (err)
1276 goto out;
1278 x->km.seq = hdr->sadb_msg_seq;
1279 return x;
1281 out:
1282 x->km.state = XFRM_STATE_DEAD;
1283 xfrm_state_put(x);
1284 return ERR_PTR(err);
1287 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1289 return -EOPNOTSUPP;
1292 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1294 struct net *net = sock_net(sk);
1295 struct sk_buff *resp_skb;
1296 struct sadb_x_sa2 *sa2;
1297 struct sadb_address *saddr, *daddr;
1298 struct sadb_msg *out_hdr;
1299 struct sadb_spirange *range;
1300 struct xfrm_state *x = NULL;
1301 int mode;
1302 int err;
1303 u32 min_spi, max_spi;
1304 u32 reqid;
1305 u8 proto;
1306 unsigned short family;
1307 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1309 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1310 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1311 return -EINVAL;
1313 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1314 if (proto == 0)
1315 return -EINVAL;
1317 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1318 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1319 if (mode < 0)
1320 return -EINVAL;
1321 reqid = sa2->sadb_x_sa2_reqid;
1322 } else {
1323 mode = 0;
1324 reqid = 0;
1327 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1328 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1330 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1331 switch (family) {
1332 case AF_INET:
1333 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1334 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1335 break;
1336 #if IS_ENABLED(CONFIG_IPV6)
1337 case AF_INET6:
1338 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1339 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1340 break;
1341 #endif
1344 if (hdr->sadb_msg_seq) {
1345 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
1346 if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
1347 xfrm_state_put(x);
1348 x = NULL;
1352 if (!x)
1353 x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
1355 if (x == NULL)
1356 return -ENOENT;
1358 min_spi = 0x100;
1359 max_spi = 0x0fffffff;
1361 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1362 if (range) {
1363 min_spi = range->sadb_spirange_min;
1364 max_spi = range->sadb_spirange_max;
1367 err = verify_spi_info(x->id.proto, min_spi, max_spi);
1368 if (err) {
1369 xfrm_state_put(x);
1370 return err;
1373 err = xfrm_alloc_spi(x, min_spi, max_spi);
1374 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
1376 if (IS_ERR(resp_skb)) {
1377 xfrm_state_put(x);
1378 return PTR_ERR(resp_skb);
1381 out_hdr = (struct sadb_msg *) resp_skb->data;
1382 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1383 out_hdr->sadb_msg_type = SADB_GETSPI;
1384 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1385 out_hdr->sadb_msg_errno = 0;
1386 out_hdr->sadb_msg_reserved = 0;
1387 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1388 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1390 xfrm_state_put(x);
1392 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net);
1394 return 0;
1397 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1399 struct net *net = sock_net(sk);
1400 struct xfrm_state *x;
1402 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1403 return -EOPNOTSUPP;
1405 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1406 return 0;
1408 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
1409 if (x == NULL)
1410 return 0;
1412 spin_lock_bh(&x->lock);
1413 if (x->km.state == XFRM_STATE_ACQ)
1414 x->km.state = XFRM_STATE_ERROR;
1416 spin_unlock_bh(&x->lock);
1417 xfrm_state_put(x);
1418 return 0;
1421 static inline int event2poltype(int event)
1423 switch (event) {
1424 case XFRM_MSG_DELPOLICY:
1425 return SADB_X_SPDDELETE;
1426 case XFRM_MSG_NEWPOLICY:
1427 return SADB_X_SPDADD;
1428 case XFRM_MSG_UPDPOLICY:
1429 return SADB_X_SPDUPDATE;
1430 case XFRM_MSG_POLEXPIRE:
1431 // return SADB_X_SPDEXPIRE;
1432 default:
1433 pr_err("pfkey: Unknown policy event %d\n", event);
1434 break;
1437 return 0;
1440 static inline int event2keytype(int event)
1442 switch (event) {
1443 case XFRM_MSG_DELSA:
1444 return SADB_DELETE;
1445 case XFRM_MSG_NEWSA:
1446 return SADB_ADD;
1447 case XFRM_MSG_UPDSA:
1448 return SADB_UPDATE;
1449 case XFRM_MSG_EXPIRE:
1450 return SADB_EXPIRE;
1451 default:
1452 pr_err("pfkey: Unknown SA event %d\n", event);
1453 break;
1456 return 0;
1459 /* ADD/UPD/DEL */
1460 static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
1462 struct sk_buff *skb;
1463 struct sadb_msg *hdr;
1465 skb = pfkey_xfrm_state2msg(x);
1467 if (IS_ERR(skb))
1468 return PTR_ERR(skb);
1470 hdr = (struct sadb_msg *) skb->data;
1471 hdr->sadb_msg_version = PF_KEY_V2;
1472 hdr->sadb_msg_type = event2keytype(c->event);
1473 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1474 hdr->sadb_msg_errno = 0;
1475 hdr->sadb_msg_reserved = 0;
1476 hdr->sadb_msg_seq = c->seq;
1477 hdr->sadb_msg_pid = c->portid;
1479 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x));
1481 return 0;
1484 static int pfkey_add(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1486 struct net *net = sock_net(sk);
1487 struct xfrm_state *x;
1488 int err;
1489 struct km_event c;
1491 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
1492 if (IS_ERR(x))
1493 return PTR_ERR(x);
1495 xfrm_state_hold(x);
1496 if (hdr->sadb_msg_type == SADB_ADD)
1497 err = xfrm_state_add(x);
1498 else
1499 err = xfrm_state_update(x);
1501 xfrm_audit_state_add(x, err ? 0 : 1, true);
1503 if (err < 0) {
1504 x->km.state = XFRM_STATE_DEAD;
1505 __xfrm_state_put(x);
1506 goto out;
1509 if (hdr->sadb_msg_type == SADB_ADD)
1510 c.event = XFRM_MSG_NEWSA;
1511 else
1512 c.event = XFRM_MSG_UPDSA;
1513 c.seq = hdr->sadb_msg_seq;
1514 c.portid = hdr->sadb_msg_pid;
1515 km_state_notify(x, &c);
1516 out:
1517 xfrm_state_put(x);
1518 return err;
1521 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1523 struct net *net = sock_net(sk);
1524 struct xfrm_state *x;
1525 struct km_event c;
1526 int err;
1528 if (!ext_hdrs[SADB_EXT_SA-1] ||
1529 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1530 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1531 return -EINVAL;
1533 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
1534 if (x == NULL)
1535 return -ESRCH;
1537 if ((err = security_xfrm_state_delete(x)))
1538 goto out;
1540 if (xfrm_state_kern(x)) {
1541 err = -EPERM;
1542 goto out;
1545 err = xfrm_state_delete(x);
1547 if (err < 0)
1548 goto out;
1550 c.seq = hdr->sadb_msg_seq;
1551 c.portid = hdr->sadb_msg_pid;
1552 c.event = XFRM_MSG_DELSA;
1553 km_state_notify(x, &c);
1554 out:
1555 xfrm_audit_state_delete(x, err ? 0 : 1, true);
1556 xfrm_state_put(x);
1558 return err;
1561 static int pfkey_get(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1563 struct net *net = sock_net(sk);
1564 __u8 proto;
1565 struct sk_buff *out_skb;
1566 struct sadb_msg *out_hdr;
1567 struct xfrm_state *x;
1569 if (!ext_hdrs[SADB_EXT_SA-1] ||
1570 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1571 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1572 return -EINVAL;
1574 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
1575 if (x == NULL)
1576 return -ESRCH;
1578 out_skb = pfkey_xfrm_state2msg(x);
1579 proto = x->id.proto;
1580 xfrm_state_put(x);
1581 if (IS_ERR(out_skb))
1582 return PTR_ERR(out_skb);
1584 out_hdr = (struct sadb_msg *) out_skb->data;
1585 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1586 out_hdr->sadb_msg_type = SADB_GET;
1587 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1588 out_hdr->sadb_msg_errno = 0;
1589 out_hdr->sadb_msg_reserved = 0;
1590 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1591 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1592 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
1594 return 0;
1597 static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
1598 gfp_t allocation)
1600 struct sk_buff *skb;
1601 struct sadb_msg *hdr;
1602 int len, auth_len, enc_len, i;
1604 auth_len = xfrm_count_pfkey_auth_supported();
1605 if (auth_len) {
1606 auth_len *= sizeof(struct sadb_alg);
1607 auth_len += sizeof(struct sadb_supported);
1610 enc_len = xfrm_count_pfkey_enc_supported();
1611 if (enc_len) {
1612 enc_len *= sizeof(struct sadb_alg);
1613 enc_len += sizeof(struct sadb_supported);
1616 len = enc_len + auth_len + sizeof(struct sadb_msg);
1618 skb = alloc_skb(len + 16, allocation);
1619 if (!skb)
1620 goto out_put_algs;
1622 hdr = skb_put(skb, sizeof(*hdr));
1623 pfkey_hdr_dup(hdr, orig);
1624 hdr->sadb_msg_errno = 0;
1625 hdr->sadb_msg_len = len / sizeof(uint64_t);
1627 if (auth_len) {
1628 struct sadb_supported *sp;
1629 struct sadb_alg *ap;
1631 sp = skb_put(skb, auth_len);
1632 ap = (struct sadb_alg *) (sp + 1);
1634 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1635 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1637 for (i = 0; ; i++) {
1638 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1639 if (!aalg)
1640 break;
1641 if (!aalg->pfkey_supported)
1642 continue;
1643 if (aalg->available)
1644 *ap++ = aalg->desc;
1648 if (enc_len) {
1649 struct sadb_supported *sp;
1650 struct sadb_alg *ap;
1652 sp = skb_put(skb, enc_len);
1653 ap = (struct sadb_alg *) (sp + 1);
1655 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1656 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1658 for (i = 0; ; i++) {
1659 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1660 if (!ealg)
1661 break;
1662 if (!ealg->pfkey_supported)
1663 continue;
1664 if (ealg->available)
1665 *ap++ = ealg->desc;
1669 out_put_algs:
1670 return skb;
1673 static int pfkey_register(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1675 struct pfkey_sock *pfk = pfkey_sk(sk);
1676 struct sk_buff *supp_skb;
1678 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1679 return -EINVAL;
1681 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1682 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1683 return -EEXIST;
1684 pfk->registered |= (1<<hdr->sadb_msg_satype);
1687 xfrm_probe_algs();
1689 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1690 if (!supp_skb) {
1691 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1692 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1694 return -ENOBUFS;
1697 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk,
1698 sock_net(sk));
1699 return 0;
1702 static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
1704 struct sk_buff *skb;
1705 struct sadb_msg *hdr;
1707 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1708 if (!skb)
1709 return -ENOBUFS;
1711 hdr = skb_put_data(skb, ihdr, sizeof(struct sadb_msg));
1712 hdr->sadb_msg_errno = (uint8_t) 0;
1713 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1715 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ONE, sk,
1716 sock_net(sk));
1719 static int key_notify_sa_flush(const struct km_event *c)
1721 struct sk_buff *skb;
1722 struct sadb_msg *hdr;
1724 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1725 if (!skb)
1726 return -ENOBUFS;
1727 hdr = skb_put(skb, sizeof(struct sadb_msg));
1728 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
1729 hdr->sadb_msg_type = SADB_FLUSH;
1730 hdr->sadb_msg_seq = c->seq;
1731 hdr->sadb_msg_pid = c->portid;
1732 hdr->sadb_msg_version = PF_KEY_V2;
1733 hdr->sadb_msg_errno = (uint8_t) 0;
1734 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1735 hdr->sadb_msg_reserved = 0;
1737 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
1739 return 0;
1742 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1744 struct net *net = sock_net(sk);
1745 unsigned int proto;
1746 struct km_event c;
1747 int err, err2;
1749 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1750 if (proto == 0)
1751 return -EINVAL;
1753 err = xfrm_state_flush(net, proto, true);
1754 err2 = unicast_flush_resp(sk, hdr);
1755 if (err || err2) {
1756 if (err == -ESRCH) /* empty table - go quietly */
1757 err = 0;
1758 return err ? err : err2;
1761 c.data.proto = proto;
1762 c.seq = hdr->sadb_msg_seq;
1763 c.portid = hdr->sadb_msg_pid;
1764 c.event = XFRM_MSG_FLUSHSA;
1765 c.net = net;
1766 km_state_notify(NULL, &c);
1768 return 0;
1771 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1773 struct pfkey_sock *pfk = ptr;
1774 struct sk_buff *out_skb;
1775 struct sadb_msg *out_hdr;
1777 if (!pfkey_can_dump(&pfk->sk))
1778 return -ENOBUFS;
1780 out_skb = pfkey_xfrm_state2msg(x);
1781 if (IS_ERR(out_skb))
1782 return PTR_ERR(out_skb);
1784 out_hdr = (struct sadb_msg *) out_skb->data;
1785 out_hdr->sadb_msg_version = pfk->dump.msg_version;
1786 out_hdr->sadb_msg_type = SADB_DUMP;
1787 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1788 out_hdr->sadb_msg_errno = 0;
1789 out_hdr->sadb_msg_reserved = 0;
1790 out_hdr->sadb_msg_seq = count + 1;
1791 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
1793 if (pfk->dump.skb)
1794 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
1795 &pfk->sk, sock_net(&pfk->sk));
1796 pfk->dump.skb = out_skb;
1798 return 0;
1801 static int pfkey_dump_sa(struct pfkey_sock *pfk)
1803 struct net *net = sock_net(&pfk->sk);
1804 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
1807 static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1809 struct net *net = sock_net(&pfk->sk);
1811 xfrm_state_walk_done(&pfk->dump.u.state, net);
1814 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1816 u8 proto;
1817 struct xfrm_address_filter *filter = NULL;
1818 struct pfkey_sock *pfk = pfkey_sk(sk);
1820 mutex_lock(&pfk->dump_lock);
1821 if (pfk->dump.dump != NULL) {
1822 mutex_unlock(&pfk->dump_lock);
1823 return -EBUSY;
1826 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1827 if (proto == 0) {
1828 mutex_unlock(&pfk->dump_lock);
1829 return -EINVAL;
1832 if (ext_hdrs[SADB_X_EXT_FILTER - 1]) {
1833 struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1];
1835 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
1836 if (filter == NULL) {
1837 mutex_unlock(&pfk->dump_lock);
1838 return -ENOMEM;
1841 memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr,
1842 sizeof(xfrm_address_t));
1843 memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr,
1844 sizeof(xfrm_address_t));
1845 filter->family = xfilter->sadb_x_filter_family;
1846 filter->splen = xfilter->sadb_x_filter_splen;
1847 filter->dplen = xfilter->sadb_x_filter_dplen;
1850 pfk->dump.msg_version = hdr->sadb_msg_version;
1851 pfk->dump.msg_portid = hdr->sadb_msg_pid;
1852 pfk->dump.dump = pfkey_dump_sa;
1853 pfk->dump.done = pfkey_dump_sa_done;
1854 xfrm_state_walk_init(&pfk->dump.u.state, proto, filter);
1855 mutex_unlock(&pfk->dump_lock);
1857 return pfkey_do_dump(pfk);
1860 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1862 struct pfkey_sock *pfk = pfkey_sk(sk);
1863 int satype = hdr->sadb_msg_satype;
1864 bool reset_errno = false;
1866 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1867 reset_errno = true;
1868 if (satype != 0 && satype != 1)
1869 return -EINVAL;
1870 pfk->promisc = satype;
1872 if (reset_errno && skb_cloned(skb))
1873 skb = skb_copy(skb, GFP_KERNEL);
1874 else
1875 skb = skb_clone(skb, GFP_KERNEL);
1877 if (reset_errno && skb) {
1878 struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
1879 new_hdr->sadb_msg_errno = 0;
1882 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk));
1883 return 0;
1886 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1888 int i;
1889 u32 reqid = *(u32*)ptr;
1891 for (i=0; i<xp->xfrm_nr; i++) {
1892 if (xp->xfrm_vec[i].reqid == reqid)
1893 return -EEXIST;
1895 return 0;
1898 static u32 gen_reqid(struct net *net)
1900 struct xfrm_policy_walk walk;
1901 u32 start;
1902 int rc;
1903 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1905 start = reqid;
1906 do {
1907 ++reqid;
1908 if (reqid == 0)
1909 reqid = IPSEC_MANUAL_REQID_MAX+1;
1910 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
1911 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
1912 xfrm_policy_walk_done(&walk, net);
1913 if (rc != -EEXIST)
1914 return reqid;
1915 } while (reqid != start);
1916 return 0;
1919 static int
1920 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1922 struct net *net = xp_net(xp);
1923 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1924 int mode;
1926 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1927 return -ELOOP;
1929 if (rq->sadb_x_ipsecrequest_mode == 0)
1930 return -EINVAL;
1932 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1933 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1934 return -EINVAL;
1935 t->mode = mode;
1936 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1937 t->optional = 1;
1938 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1939 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1940 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1941 t->reqid = 0;
1942 if (!t->reqid && !(t->reqid = gen_reqid(net)))
1943 return -ENOBUFS;
1946 /* addresses present only in tunnel mode */
1947 if (t->mode == XFRM_MODE_TUNNEL) {
1948 int err;
1950 err = parse_sockaddr_pair(
1951 (struct sockaddr *)(rq + 1),
1952 rq->sadb_x_ipsecrequest_len - sizeof(*rq),
1953 &t->saddr, &t->id.daddr, &t->encap_family);
1954 if (err)
1955 return err;
1956 } else
1957 t->encap_family = xp->family;
1959 /* No way to set this via kame pfkey */
1960 t->allalgs = 1;
1961 xp->xfrm_nr++;
1962 return 0;
1965 static int
1966 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1968 int err;
1969 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1970 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1972 if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
1973 return -EINVAL;
1975 while (len >= sizeof(*rq)) {
1976 if (len < rq->sadb_x_ipsecrequest_len ||
1977 rq->sadb_x_ipsecrequest_len < sizeof(*rq))
1978 return -EINVAL;
1980 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1981 return err;
1982 len -= rq->sadb_x_ipsecrequest_len;
1983 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1985 return 0;
1988 static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
1990 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1992 if (xfrm_ctx) {
1993 int len = sizeof(struct sadb_x_sec_ctx);
1994 len += xfrm_ctx->ctx_len;
1995 return PFKEY_ALIGN8(len);
1997 return 0;
2000 static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
2002 const struct xfrm_tmpl *t;
2003 int sockaddr_size = pfkey_sockaddr_size(xp->family);
2004 int socklen = 0;
2005 int i;
2007 for (i=0; i<xp->xfrm_nr; i++) {
2008 t = xp->xfrm_vec + i;
2009 socklen += pfkey_sockaddr_len(t->encap_family);
2012 return sizeof(struct sadb_msg) +
2013 (sizeof(struct sadb_lifetime) * 3) +
2014 (sizeof(struct sadb_address) * 2) +
2015 (sockaddr_size * 2) +
2016 sizeof(struct sadb_x_policy) +
2017 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
2018 (socklen * 2) +
2019 pfkey_xfrm_policy2sec_ctx_size(xp);
2022 static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
2024 struct sk_buff *skb;
2025 int size;
2027 size = pfkey_xfrm_policy2msg_size(xp);
2029 skb = alloc_skb(size + 16, GFP_ATOMIC);
2030 if (skb == NULL)
2031 return ERR_PTR(-ENOBUFS);
2033 return skb;
2036 static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
2038 struct sadb_msg *hdr;
2039 struct sadb_address *addr;
2040 struct sadb_lifetime *lifetime;
2041 struct sadb_x_policy *pol;
2042 struct sadb_x_sec_ctx *sec_ctx;
2043 struct xfrm_sec_ctx *xfrm_ctx;
2044 int i;
2045 int size;
2046 int sockaddr_size = pfkey_sockaddr_size(xp->family);
2047 int socklen = pfkey_sockaddr_len(xp->family);
2049 size = pfkey_xfrm_policy2msg_size(xp);
2051 /* call should fill header later */
2052 hdr = skb_put(skb, sizeof(struct sadb_msg));
2053 memset(hdr, 0, size); /* XXX do we need this ? */
2055 /* src address */
2056 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
2057 addr->sadb_address_len =
2058 (sizeof(struct sadb_address)+sockaddr_size)/
2059 sizeof(uint64_t);
2060 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2061 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2062 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2063 addr->sadb_address_reserved = 0;
2064 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2065 xp->selector.sport,
2066 (struct sockaddr *) (addr + 1),
2067 xp->family))
2068 BUG();
2070 /* dst address */
2071 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
2072 addr->sadb_address_len =
2073 (sizeof(struct sadb_address)+sockaddr_size)/
2074 sizeof(uint64_t);
2075 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2076 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2077 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
2078 addr->sadb_address_reserved = 0;
2080 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2081 (struct sockaddr *) (addr + 1),
2082 xp->family);
2084 /* hard time */
2085 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
2086 lifetime->sadb_lifetime_len =
2087 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2088 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2089 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2090 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2091 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2092 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2093 /* soft time */
2094 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
2095 lifetime->sadb_lifetime_len =
2096 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2097 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2098 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2099 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2100 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2101 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2102 /* current time */
2103 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
2104 lifetime->sadb_lifetime_len =
2105 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2106 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2107 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2108 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2109 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2110 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2112 pol = skb_put(skb, sizeof(struct sadb_x_policy));
2113 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2114 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2115 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2116 if (xp->action == XFRM_POLICY_ALLOW) {
2117 if (xp->xfrm_nr)
2118 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2119 else
2120 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2122 pol->sadb_x_policy_dir = dir+1;
2123 pol->sadb_x_policy_reserved = 0;
2124 pol->sadb_x_policy_id = xp->index;
2125 pol->sadb_x_policy_priority = xp->priority;
2127 for (i=0; i<xp->xfrm_nr; i++) {
2128 const struct xfrm_tmpl *t = xp->xfrm_vec + i;
2129 struct sadb_x_ipsecrequest *rq;
2130 int req_size;
2131 int mode;
2133 req_size = sizeof(struct sadb_x_ipsecrequest);
2134 if (t->mode == XFRM_MODE_TUNNEL) {
2135 socklen = pfkey_sockaddr_len(t->encap_family);
2136 req_size += socklen * 2;
2137 } else {
2138 size -= 2*socklen;
2140 rq = skb_put(skb, req_size);
2141 pol->sadb_x_policy_len += req_size/8;
2142 memset(rq, 0, sizeof(*rq));
2143 rq->sadb_x_ipsecrequest_len = req_size;
2144 rq->sadb_x_ipsecrequest_proto = t->id.proto;
2145 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2146 return -EINVAL;
2147 rq->sadb_x_ipsecrequest_mode = mode;
2148 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2149 if (t->reqid)
2150 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2151 if (t->optional)
2152 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2153 rq->sadb_x_ipsecrequest_reqid = t->reqid;
2155 if (t->mode == XFRM_MODE_TUNNEL) {
2156 u8 *sa = (void *)(rq + 1);
2157 pfkey_sockaddr_fill(&t->saddr, 0,
2158 (struct sockaddr *)sa,
2159 t->encap_family);
2160 pfkey_sockaddr_fill(&t->id.daddr, 0,
2161 (struct sockaddr *) (sa + socklen),
2162 t->encap_family);
2166 /* security context */
2167 if ((xfrm_ctx = xp->security)) {
2168 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2170 sec_ctx = skb_put(skb, ctx_size);
2171 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2172 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2173 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2174 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2175 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2176 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2177 xfrm_ctx->ctx_len);
2180 hdr->sadb_msg_len = size / sizeof(uint64_t);
2181 hdr->sadb_msg_reserved = refcount_read(&xp->refcnt);
2183 return 0;
2186 static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2188 struct sk_buff *out_skb;
2189 struct sadb_msg *out_hdr;
2190 int err;
2192 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2193 if (IS_ERR(out_skb))
2194 return PTR_ERR(out_skb);
2196 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2197 if (err < 0)
2198 return err;
2200 out_hdr = (struct sadb_msg *) out_skb->data;
2201 out_hdr->sadb_msg_version = PF_KEY_V2;
2203 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
2204 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2205 else
2206 out_hdr->sadb_msg_type = event2poltype(c->event);
2207 out_hdr->sadb_msg_errno = 0;
2208 out_hdr->sadb_msg_seq = c->seq;
2209 out_hdr->sadb_msg_pid = c->portid;
2210 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp));
2211 return 0;
2215 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2217 struct net *net = sock_net(sk);
2218 int err = 0;
2219 struct sadb_lifetime *lifetime;
2220 struct sadb_address *sa;
2221 struct sadb_x_policy *pol;
2222 struct xfrm_policy *xp;
2223 struct km_event c;
2224 struct sadb_x_sec_ctx *sec_ctx;
2226 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2227 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2228 !ext_hdrs[SADB_X_EXT_POLICY-1])
2229 return -EINVAL;
2231 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2232 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2233 return -EINVAL;
2234 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2235 return -EINVAL;
2237 xp = xfrm_policy_alloc(net, GFP_KERNEL);
2238 if (xp == NULL)
2239 return -ENOBUFS;
2241 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2242 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2243 xp->priority = pol->sadb_x_policy_priority;
2245 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
2246 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2247 xp->selector.family = xp->family;
2248 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2249 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2250 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2251 if (xp->selector.sport)
2252 xp->selector.sport_mask = htons(0xffff);
2254 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
2255 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2256 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2258 /* Amusing, we set this twice. KAME apps appear to set same value
2259 * in both addresses.
2261 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2263 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2264 if (xp->selector.dport)
2265 xp->selector.dport_mask = htons(0xffff);
2267 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
2268 if (sec_ctx != NULL) {
2269 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
2271 if (!uctx) {
2272 err = -ENOBUFS;
2273 goto out;
2276 err = security_xfrm_policy_alloc(&xp->security, uctx, GFP_KERNEL);
2277 kfree(uctx);
2279 if (err)
2280 goto out;
2283 xp->lft.soft_byte_limit = XFRM_INF;
2284 xp->lft.hard_byte_limit = XFRM_INF;
2285 xp->lft.soft_packet_limit = XFRM_INF;
2286 xp->lft.hard_packet_limit = XFRM_INF;
2287 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2288 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2289 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2290 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2291 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2293 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2294 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2295 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2296 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2297 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2299 xp->xfrm_nr = 0;
2300 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2301 (err = parse_ipsecrequests(xp, pol)) < 0)
2302 goto out;
2304 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2305 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2307 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
2309 if (err)
2310 goto out;
2312 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2313 c.event = XFRM_MSG_UPDPOLICY;
2314 else
2315 c.event = XFRM_MSG_NEWPOLICY;
2317 c.seq = hdr->sadb_msg_seq;
2318 c.portid = hdr->sadb_msg_pid;
2320 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2321 xfrm_pol_put(xp);
2322 return 0;
2324 out:
2325 xp->walk.dead = 1;
2326 xfrm_policy_destroy(xp);
2327 return err;
2330 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2332 struct net *net = sock_net(sk);
2333 int err;
2334 struct sadb_address *sa;
2335 struct sadb_x_policy *pol;
2336 struct xfrm_policy *xp;
2337 struct xfrm_selector sel;
2338 struct km_event c;
2339 struct sadb_x_sec_ctx *sec_ctx;
2340 struct xfrm_sec_ctx *pol_ctx = NULL;
2342 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2343 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2344 !ext_hdrs[SADB_X_EXT_POLICY-1])
2345 return -EINVAL;
2347 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2348 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2349 return -EINVAL;
2351 memset(&sel, 0, sizeof(sel));
2353 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
2354 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2355 sel.prefixlen_s = sa->sadb_address_prefixlen;
2356 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2357 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2358 if (sel.sport)
2359 sel.sport_mask = htons(0xffff);
2361 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
2362 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2363 sel.prefixlen_d = sa->sadb_address_prefixlen;
2364 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2365 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2366 if (sel.dport)
2367 sel.dport_mask = htons(0xffff);
2369 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
2370 if (sec_ctx != NULL) {
2371 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
2373 if (!uctx)
2374 return -ENOMEM;
2376 err = security_xfrm_policy_alloc(&pol_ctx, uctx, GFP_KERNEL);
2377 kfree(uctx);
2378 if (err)
2379 return err;
2382 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
2383 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2384 1, &err);
2385 security_xfrm_policy_free(pol_ctx);
2386 if (xp == NULL)
2387 return -ENOENT;
2389 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2391 if (err)
2392 goto out;
2394 c.seq = hdr->sadb_msg_seq;
2395 c.portid = hdr->sadb_msg_pid;
2396 c.data.byid = 0;
2397 c.event = XFRM_MSG_DELPOLICY;
2398 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2400 out:
2401 xfrm_pol_put(xp);
2402 return err;
2405 static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, const struct sadb_msg *hdr, int dir)
2407 int err;
2408 struct sk_buff *out_skb;
2409 struct sadb_msg *out_hdr;
2410 err = 0;
2412 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2413 if (IS_ERR(out_skb)) {
2414 err = PTR_ERR(out_skb);
2415 goto out;
2417 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2418 if (err < 0)
2419 goto out;
2421 out_hdr = (struct sadb_msg *) out_skb->data;
2422 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2423 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2424 out_hdr->sadb_msg_satype = 0;
2425 out_hdr->sadb_msg_errno = 0;
2426 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2427 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2428 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp));
2429 err = 0;
2431 out:
2432 return err;
2435 static int pfkey_sockaddr_pair_size(sa_family_t family)
2437 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
2440 static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
2441 xfrm_address_t *saddr, xfrm_address_t *daddr,
2442 u16 *family)
2444 int af, socklen;
2446 if (ext_len < 2 || ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
2447 return -EINVAL;
2449 af = pfkey_sockaddr_extract(sa, saddr);
2450 if (!af)
2451 return -EINVAL;
2453 socklen = pfkey_sockaddr_len(af);
2454 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
2455 daddr) != af)
2456 return -EINVAL;
2458 *family = af;
2459 return 0;
2462 #ifdef CONFIG_NET_KEY_MIGRATE
2463 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2464 struct xfrm_migrate *m)
2466 int err;
2467 struct sadb_x_ipsecrequest *rq2;
2468 int mode;
2470 if (len < sizeof(*rq1) ||
2471 len < rq1->sadb_x_ipsecrequest_len ||
2472 rq1->sadb_x_ipsecrequest_len < sizeof(*rq1))
2473 return -EINVAL;
2475 /* old endoints */
2476 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
2477 rq1->sadb_x_ipsecrequest_len - sizeof(*rq1),
2478 &m->old_saddr, &m->old_daddr,
2479 &m->old_family);
2480 if (err)
2481 return err;
2483 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2484 len -= rq1->sadb_x_ipsecrequest_len;
2486 if (len <= sizeof(*rq2) ||
2487 len < rq2->sadb_x_ipsecrequest_len ||
2488 rq2->sadb_x_ipsecrequest_len < sizeof(*rq2))
2489 return -EINVAL;
2491 /* new endpoints */
2492 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
2493 rq2->sadb_x_ipsecrequest_len - sizeof(*rq2),
2494 &m->new_saddr, &m->new_daddr,
2495 &m->new_family);
2496 if (err)
2497 return err;
2499 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2500 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2501 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2502 return -EINVAL;
2504 m->proto = rq1->sadb_x_ipsecrequest_proto;
2505 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2506 return -EINVAL;
2507 m->mode = mode;
2508 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2510 return ((int)(rq1->sadb_x_ipsecrequest_len +
2511 rq2->sadb_x_ipsecrequest_len));
2514 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2515 const struct sadb_msg *hdr, void * const *ext_hdrs)
2517 int i, len, ret, err = -EINVAL;
2518 u8 dir;
2519 struct sadb_address *sa;
2520 struct sadb_x_kmaddress *kma;
2521 struct sadb_x_policy *pol;
2522 struct sadb_x_ipsecrequest *rq;
2523 struct xfrm_selector sel;
2524 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2525 struct xfrm_kmaddress k;
2526 struct net *net = sock_net(sk);
2528 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2529 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2530 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2531 err = -EINVAL;
2532 goto out;
2535 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
2536 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2538 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2539 err = -EINVAL;
2540 goto out;
2543 if (kma) {
2544 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2545 k.reserved = kma->sadb_x_kmaddress_reserved;
2546 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2547 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2548 &k.local, &k.remote, &k.family);
2549 if (ret < 0) {
2550 err = ret;
2551 goto out;
2555 dir = pol->sadb_x_policy_dir - 1;
2556 memset(&sel, 0, sizeof(sel));
2558 /* set source address info of selector */
2559 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2560 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2561 sel.prefixlen_s = sa->sadb_address_prefixlen;
2562 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2563 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2564 if (sel.sport)
2565 sel.sport_mask = htons(0xffff);
2567 /* set destination address info of selector */
2568 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
2569 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2570 sel.prefixlen_d = sa->sadb_address_prefixlen;
2571 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2572 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2573 if (sel.dport)
2574 sel.dport_mask = htons(0xffff);
2576 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2578 /* extract ipsecrequests */
2579 i = 0;
2580 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2582 while (len > 0 && i < XFRM_MAX_DEPTH) {
2583 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2584 if (ret < 0) {
2585 err = ret;
2586 goto out;
2587 } else {
2588 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2589 len -= ret;
2590 i++;
2594 if (!i || len > 0) {
2595 err = -EINVAL;
2596 goto out;
2599 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
2600 kma ? &k : NULL, net, NULL);
2602 out:
2603 return err;
2605 #else
2606 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2607 const struct sadb_msg *hdr, void * const *ext_hdrs)
2609 return -ENOPROTOOPT;
2611 #endif
2614 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2616 struct net *net = sock_net(sk);
2617 unsigned int dir;
2618 int err = 0, delete;
2619 struct sadb_x_policy *pol;
2620 struct xfrm_policy *xp;
2621 struct km_event c;
2623 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2624 return -EINVAL;
2626 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2627 if (dir >= XFRM_POLICY_MAX)
2628 return -EINVAL;
2630 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2631 xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
2632 dir, pol->sadb_x_policy_id, delete, &err);
2633 if (xp == NULL)
2634 return -ENOENT;
2636 if (delete) {
2637 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2639 if (err)
2640 goto out;
2641 c.seq = hdr->sadb_msg_seq;
2642 c.portid = hdr->sadb_msg_pid;
2643 c.data.byid = 1;
2644 c.event = XFRM_MSG_DELPOLICY;
2645 km_policy_notify(xp, dir, &c);
2646 } else {
2647 err = key_pol_get_resp(sk, xp, hdr, dir);
2650 out:
2651 xfrm_pol_put(xp);
2652 return err;
2655 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2657 struct pfkey_sock *pfk = ptr;
2658 struct sk_buff *out_skb;
2659 struct sadb_msg *out_hdr;
2660 int err;
2662 if (!pfkey_can_dump(&pfk->sk))
2663 return -ENOBUFS;
2665 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2666 if (IS_ERR(out_skb))
2667 return PTR_ERR(out_skb);
2669 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2670 if (err < 0)
2671 return err;
2673 out_hdr = (struct sadb_msg *) out_skb->data;
2674 out_hdr->sadb_msg_version = pfk->dump.msg_version;
2675 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2676 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2677 out_hdr->sadb_msg_errno = 0;
2678 out_hdr->sadb_msg_seq = count + 1;
2679 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
2681 if (pfk->dump.skb)
2682 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
2683 &pfk->sk, sock_net(&pfk->sk));
2684 pfk->dump.skb = out_skb;
2686 return 0;
2689 static int pfkey_dump_sp(struct pfkey_sock *pfk)
2691 struct net *net = sock_net(&pfk->sk);
2692 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
2695 static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2697 struct net *net = sock_net((struct sock *)pfk);
2699 xfrm_policy_walk_done(&pfk->dump.u.policy, net);
2702 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2704 struct pfkey_sock *pfk = pfkey_sk(sk);
2706 mutex_lock(&pfk->dump_lock);
2707 if (pfk->dump.dump != NULL) {
2708 mutex_unlock(&pfk->dump_lock);
2709 return -EBUSY;
2712 pfk->dump.msg_version = hdr->sadb_msg_version;
2713 pfk->dump.msg_portid = hdr->sadb_msg_pid;
2714 pfk->dump.dump = pfkey_dump_sp;
2715 pfk->dump.done = pfkey_dump_sp_done;
2716 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2717 mutex_unlock(&pfk->dump_lock);
2719 return pfkey_do_dump(pfk);
2722 static int key_notify_policy_flush(const struct km_event *c)
2724 struct sk_buff *skb_out;
2725 struct sadb_msg *hdr;
2727 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
2728 if (!skb_out)
2729 return -ENOBUFS;
2730 hdr = skb_put(skb_out, sizeof(struct sadb_msg));
2731 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
2732 hdr->sadb_msg_seq = c->seq;
2733 hdr->sadb_msg_pid = c->portid;
2734 hdr->sadb_msg_version = PF_KEY_V2;
2735 hdr->sadb_msg_errno = (uint8_t) 0;
2736 hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2737 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2738 hdr->sadb_msg_reserved = 0;
2739 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
2740 return 0;
2744 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2746 struct net *net = sock_net(sk);
2747 struct km_event c;
2748 int err, err2;
2750 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true);
2751 err2 = unicast_flush_resp(sk, hdr);
2752 if (err || err2) {
2753 if (err == -ESRCH) /* empty table - old silent behavior */
2754 return 0;
2755 return err;
2758 c.data.type = XFRM_POLICY_TYPE_MAIN;
2759 c.event = XFRM_MSG_FLUSHPOLICY;
2760 c.portid = hdr->sadb_msg_pid;
2761 c.seq = hdr->sadb_msg_seq;
2762 c.net = net;
2763 km_policy_notify(NULL, 0, &c);
2765 return 0;
2768 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2769 const struct sadb_msg *hdr, void * const *ext_hdrs);
2770 static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2771 [SADB_RESERVED] = pfkey_reserved,
2772 [SADB_GETSPI] = pfkey_getspi,
2773 [SADB_UPDATE] = pfkey_add,
2774 [SADB_ADD] = pfkey_add,
2775 [SADB_DELETE] = pfkey_delete,
2776 [SADB_GET] = pfkey_get,
2777 [SADB_ACQUIRE] = pfkey_acquire,
2778 [SADB_REGISTER] = pfkey_register,
2779 [SADB_EXPIRE] = NULL,
2780 [SADB_FLUSH] = pfkey_flush,
2781 [SADB_DUMP] = pfkey_dump,
2782 [SADB_X_PROMISC] = pfkey_promisc,
2783 [SADB_X_PCHANGE] = NULL,
2784 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2785 [SADB_X_SPDADD] = pfkey_spdadd,
2786 [SADB_X_SPDDELETE] = pfkey_spddelete,
2787 [SADB_X_SPDGET] = pfkey_spdget,
2788 [SADB_X_SPDACQUIRE] = NULL,
2789 [SADB_X_SPDDUMP] = pfkey_spddump,
2790 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2791 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2792 [SADB_X_SPDDELETE2] = pfkey_spdget,
2793 [SADB_X_MIGRATE] = pfkey_migrate,
2796 static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
2798 void *ext_hdrs[SADB_EXT_MAX];
2799 int err;
2801 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2802 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
2804 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2805 err = parse_exthdrs(skb, hdr, ext_hdrs);
2806 if (!err) {
2807 err = -EOPNOTSUPP;
2808 if (pfkey_funcs[hdr->sadb_msg_type])
2809 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2811 return err;
2814 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2816 struct sadb_msg *hdr = NULL;
2818 if (skb->len < sizeof(*hdr)) {
2819 *errp = -EMSGSIZE;
2820 } else {
2821 hdr = (struct sadb_msg *) skb->data;
2822 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2823 hdr->sadb_msg_reserved != 0 ||
2824 (hdr->sadb_msg_type <= SADB_RESERVED ||
2825 hdr->sadb_msg_type > SADB_MAX)) {
2826 hdr = NULL;
2827 *errp = -EINVAL;
2828 } else if (hdr->sadb_msg_len != (skb->len /
2829 sizeof(uint64_t)) ||
2830 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2831 sizeof(uint64_t))) {
2832 hdr = NULL;
2833 *errp = -EMSGSIZE;
2834 } else {
2835 *errp = 0;
2838 return hdr;
2841 static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
2842 const struct xfrm_algo_desc *d)
2844 unsigned int id = d->desc.sadb_alg_id;
2846 if (id >= sizeof(t->aalgos) * 8)
2847 return 0;
2849 return (t->aalgos >> id) & 1;
2852 static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
2853 const struct xfrm_algo_desc *d)
2855 unsigned int id = d->desc.sadb_alg_id;
2857 if (id >= sizeof(t->ealgos) * 8)
2858 return 0;
2860 return (t->ealgos >> id) & 1;
2863 static int count_ah_combs(const struct xfrm_tmpl *t)
2865 int i, sz = 0;
2867 for (i = 0; ; i++) {
2868 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2869 if (!aalg)
2870 break;
2871 if (!aalg->pfkey_supported)
2872 continue;
2873 if (aalg_tmpl_set(t, aalg) && aalg->available)
2874 sz += sizeof(struct sadb_comb);
2876 return sz + sizeof(struct sadb_prop);
2879 static int count_esp_combs(const struct xfrm_tmpl *t)
2881 int i, k, sz = 0;
2883 for (i = 0; ; i++) {
2884 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2885 if (!ealg)
2886 break;
2888 if (!ealg->pfkey_supported)
2889 continue;
2891 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2892 continue;
2894 for (k = 1; ; k++) {
2895 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2896 if (!aalg)
2897 break;
2899 if (!aalg->pfkey_supported)
2900 continue;
2902 if (aalg_tmpl_set(t, aalg) && aalg->available)
2903 sz += sizeof(struct sadb_comb);
2906 return sz + sizeof(struct sadb_prop);
2909 static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
2911 struct sadb_prop *p;
2912 int i;
2914 p = skb_put(skb, sizeof(struct sadb_prop));
2915 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2916 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2917 p->sadb_prop_replay = 32;
2918 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2920 for (i = 0; ; i++) {
2921 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2922 if (!aalg)
2923 break;
2925 if (!aalg->pfkey_supported)
2926 continue;
2928 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2929 struct sadb_comb *c;
2930 c = skb_put_zero(skb, sizeof(struct sadb_comb));
2931 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2932 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2933 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2934 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2935 c->sadb_comb_hard_addtime = 24*60*60;
2936 c->sadb_comb_soft_addtime = 20*60*60;
2937 c->sadb_comb_hard_usetime = 8*60*60;
2938 c->sadb_comb_soft_usetime = 7*60*60;
2943 static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
2945 struct sadb_prop *p;
2946 int i, k;
2948 p = skb_put(skb, sizeof(struct sadb_prop));
2949 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2950 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2951 p->sadb_prop_replay = 32;
2952 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2954 for (i=0; ; i++) {
2955 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2956 if (!ealg)
2957 break;
2959 if (!ealg->pfkey_supported)
2960 continue;
2962 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2963 continue;
2965 for (k = 1; ; k++) {
2966 struct sadb_comb *c;
2967 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2968 if (!aalg)
2969 break;
2970 if (!aalg->pfkey_supported)
2971 continue;
2972 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2973 continue;
2974 c = skb_put(skb, sizeof(struct sadb_comb));
2975 memset(c, 0, sizeof(*c));
2976 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2977 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2978 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2979 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2980 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2981 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2982 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2983 c->sadb_comb_hard_addtime = 24*60*60;
2984 c->sadb_comb_soft_addtime = 20*60*60;
2985 c->sadb_comb_hard_usetime = 8*60*60;
2986 c->sadb_comb_soft_usetime = 7*60*60;
2991 static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
2993 return 0;
2996 static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
2998 struct sk_buff *out_skb;
2999 struct sadb_msg *out_hdr;
3000 int hard;
3001 int hsc;
3003 hard = c->data.hard;
3004 if (hard)
3005 hsc = 2;
3006 else
3007 hsc = 1;
3009 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
3010 if (IS_ERR(out_skb))
3011 return PTR_ERR(out_skb);
3013 out_hdr = (struct sadb_msg *) out_skb->data;
3014 out_hdr->sadb_msg_version = PF_KEY_V2;
3015 out_hdr->sadb_msg_type = SADB_EXPIRE;
3016 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3017 out_hdr->sadb_msg_errno = 0;
3018 out_hdr->sadb_msg_reserved = 0;
3019 out_hdr->sadb_msg_seq = 0;
3020 out_hdr->sadb_msg_pid = 0;
3022 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3023 xs_net(x));
3024 return 0;
3027 static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
3029 struct net *net = x ? xs_net(x) : c->net;
3030 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3032 if (atomic_read(&net_pfkey->socks_nr) == 0)
3033 return 0;
3035 switch (c->event) {
3036 case XFRM_MSG_EXPIRE:
3037 return key_notify_sa_expire(x, c);
3038 case XFRM_MSG_DELSA:
3039 case XFRM_MSG_NEWSA:
3040 case XFRM_MSG_UPDSA:
3041 return key_notify_sa(x, c);
3042 case XFRM_MSG_FLUSHSA:
3043 return key_notify_sa_flush(c);
3044 case XFRM_MSG_NEWAE: /* not yet supported */
3045 break;
3046 default:
3047 pr_err("pfkey: Unknown SA event %d\n", c->event);
3048 break;
3051 return 0;
3054 static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3056 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3057 return 0;
3059 switch (c->event) {
3060 case XFRM_MSG_POLEXPIRE:
3061 return key_notify_policy_expire(xp, c);
3062 case XFRM_MSG_DELPOLICY:
3063 case XFRM_MSG_NEWPOLICY:
3064 case XFRM_MSG_UPDPOLICY:
3065 return key_notify_policy(xp, dir, c);
3066 case XFRM_MSG_FLUSHPOLICY:
3067 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3068 break;
3069 return key_notify_policy_flush(c);
3070 default:
3071 pr_err("pfkey: Unknown policy event %d\n", c->event);
3072 break;
3075 return 0;
3078 static u32 get_acqseq(void)
3080 u32 res;
3081 static atomic_t acqseq;
3083 do {
3084 res = atomic_inc_return(&acqseq);
3085 } while (!res);
3086 return res;
3089 static bool pfkey_is_alive(const struct km_event *c)
3091 struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id);
3092 struct sock *sk;
3093 bool is_alive = false;
3095 rcu_read_lock();
3096 sk_for_each_rcu(sk, &net_pfkey->table) {
3097 if (pfkey_sk(sk)->registered) {
3098 is_alive = true;
3099 break;
3102 rcu_read_unlock();
3104 return is_alive;
3107 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
3109 struct sk_buff *skb;
3110 struct sadb_msg *hdr;
3111 struct sadb_address *addr;
3112 struct sadb_x_policy *pol;
3113 int sockaddr_size;
3114 int size;
3115 struct sadb_x_sec_ctx *sec_ctx;
3116 struct xfrm_sec_ctx *xfrm_ctx;
3117 int ctx_size = 0;
3119 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3120 if (!sockaddr_size)
3121 return -EINVAL;
3123 size = sizeof(struct sadb_msg) +
3124 (sizeof(struct sadb_address) * 2) +
3125 (sockaddr_size * 2) +
3126 sizeof(struct sadb_x_policy);
3128 if (x->id.proto == IPPROTO_AH)
3129 size += count_ah_combs(t);
3130 else if (x->id.proto == IPPROTO_ESP)
3131 size += count_esp_combs(t);
3133 if ((xfrm_ctx = x->security)) {
3134 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3135 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3138 skb = alloc_skb(size + 16, GFP_ATOMIC);
3139 if (skb == NULL)
3140 return -ENOMEM;
3142 hdr = skb_put(skb, sizeof(struct sadb_msg));
3143 hdr->sadb_msg_version = PF_KEY_V2;
3144 hdr->sadb_msg_type = SADB_ACQUIRE;
3145 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3146 hdr->sadb_msg_len = size / sizeof(uint64_t);
3147 hdr->sadb_msg_errno = 0;
3148 hdr->sadb_msg_reserved = 0;
3149 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3150 hdr->sadb_msg_pid = 0;
3152 /* src address */
3153 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
3154 addr->sadb_address_len =
3155 (sizeof(struct sadb_address)+sockaddr_size)/
3156 sizeof(uint64_t);
3157 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3158 addr->sadb_address_proto = 0;
3159 addr->sadb_address_reserved = 0;
3160 addr->sadb_address_prefixlen =
3161 pfkey_sockaddr_fill(&x->props.saddr, 0,
3162 (struct sockaddr *) (addr + 1),
3163 x->props.family);
3164 if (!addr->sadb_address_prefixlen)
3165 BUG();
3167 /* dst address */
3168 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
3169 addr->sadb_address_len =
3170 (sizeof(struct sadb_address)+sockaddr_size)/
3171 sizeof(uint64_t);
3172 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3173 addr->sadb_address_proto = 0;
3174 addr->sadb_address_reserved = 0;
3175 addr->sadb_address_prefixlen =
3176 pfkey_sockaddr_fill(&x->id.daddr, 0,
3177 (struct sockaddr *) (addr + 1),
3178 x->props.family);
3179 if (!addr->sadb_address_prefixlen)
3180 BUG();
3182 pol = skb_put(skb, sizeof(struct sadb_x_policy));
3183 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3184 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3185 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3186 pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1;
3187 pol->sadb_x_policy_reserved = 0;
3188 pol->sadb_x_policy_id = xp->index;
3189 pol->sadb_x_policy_priority = xp->priority;
3191 /* Set sadb_comb's. */
3192 if (x->id.proto == IPPROTO_AH)
3193 dump_ah_combs(skb, t);
3194 else if (x->id.proto == IPPROTO_ESP)
3195 dump_esp_combs(skb, t);
3197 /* security context */
3198 if (xfrm_ctx) {
3199 sec_ctx = skb_put(skb,
3200 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3201 sec_ctx->sadb_x_sec_len =
3202 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3203 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3204 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3205 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3206 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3207 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3208 xfrm_ctx->ctx_len);
3211 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3212 xs_net(x));
3215 static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
3216 u8 *data, int len, int *dir)
3218 struct net *net = sock_net(sk);
3219 struct xfrm_policy *xp;
3220 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
3221 struct sadb_x_sec_ctx *sec_ctx;
3223 switch (sk->sk_family) {
3224 case AF_INET:
3225 if (opt != IP_IPSEC_POLICY) {
3226 *dir = -EOPNOTSUPP;
3227 return NULL;
3229 break;
3230 #if IS_ENABLED(CONFIG_IPV6)
3231 case AF_INET6:
3232 if (opt != IPV6_IPSEC_POLICY) {
3233 *dir = -EOPNOTSUPP;
3234 return NULL;
3236 break;
3237 #endif
3238 default:
3239 *dir = -EINVAL;
3240 return NULL;
3243 *dir = -EINVAL;
3245 if (len < sizeof(struct sadb_x_policy) ||
3246 pol->sadb_x_policy_len*8 > len ||
3247 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3248 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3249 return NULL;
3251 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3252 if (xp == NULL) {
3253 *dir = -ENOBUFS;
3254 return NULL;
3257 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3258 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3260 xp->lft.soft_byte_limit = XFRM_INF;
3261 xp->lft.hard_byte_limit = XFRM_INF;
3262 xp->lft.soft_packet_limit = XFRM_INF;
3263 xp->lft.hard_packet_limit = XFRM_INF;
3264 xp->family = sk->sk_family;
3266 xp->xfrm_nr = 0;
3267 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3268 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3269 goto out;
3271 /* security context too */
3272 if (len >= (pol->sadb_x_policy_len*8 +
3273 sizeof(struct sadb_x_sec_ctx))) {
3274 char *p = (char *)pol;
3275 struct xfrm_user_sec_ctx *uctx;
3277 p += pol->sadb_x_policy_len*8;
3278 sec_ctx = (struct sadb_x_sec_ctx *)p;
3279 if (len < pol->sadb_x_policy_len*8 +
3280 sec_ctx->sadb_x_sec_len*8) {
3281 *dir = -EINVAL;
3282 goto out;
3284 if ((*dir = verify_sec_ctx_len(p)))
3285 goto out;
3286 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_ATOMIC);
3287 *dir = security_xfrm_policy_alloc(&xp->security, uctx, GFP_ATOMIC);
3288 kfree(uctx);
3290 if (*dir)
3291 goto out;
3294 *dir = pol->sadb_x_policy_dir-1;
3295 return xp;
3297 out:
3298 xp->walk.dead = 1;
3299 xfrm_policy_destroy(xp);
3300 return NULL;
3303 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
3305 struct sk_buff *skb;
3306 struct sadb_msg *hdr;
3307 struct sadb_sa *sa;
3308 struct sadb_address *addr;
3309 struct sadb_x_nat_t_port *n_port;
3310 int sockaddr_size;
3311 int size;
3312 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3313 struct xfrm_encap_tmpl *natt = NULL;
3315 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3316 if (!sockaddr_size)
3317 return -EINVAL;
3319 if (!satype)
3320 return -EINVAL;
3322 if (!x->encap)
3323 return -EINVAL;
3325 natt = x->encap;
3327 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3329 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3330 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3333 size = sizeof(struct sadb_msg) +
3334 sizeof(struct sadb_sa) +
3335 (sizeof(struct sadb_address) * 2) +
3336 (sockaddr_size * 2) +
3337 (sizeof(struct sadb_x_nat_t_port) * 2);
3339 skb = alloc_skb(size + 16, GFP_ATOMIC);
3340 if (skb == NULL)
3341 return -ENOMEM;
3343 hdr = skb_put(skb, sizeof(struct sadb_msg));
3344 hdr->sadb_msg_version = PF_KEY_V2;
3345 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3346 hdr->sadb_msg_satype = satype;
3347 hdr->sadb_msg_len = size / sizeof(uint64_t);
3348 hdr->sadb_msg_errno = 0;
3349 hdr->sadb_msg_reserved = 0;
3350 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3351 hdr->sadb_msg_pid = 0;
3353 /* SA */
3354 sa = skb_put(skb, sizeof(struct sadb_sa));
3355 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3356 sa->sadb_sa_exttype = SADB_EXT_SA;
3357 sa->sadb_sa_spi = x->id.spi;
3358 sa->sadb_sa_replay = 0;
3359 sa->sadb_sa_state = 0;
3360 sa->sadb_sa_auth = 0;
3361 sa->sadb_sa_encrypt = 0;
3362 sa->sadb_sa_flags = 0;
3364 /* ADDRESS_SRC (old addr) */
3365 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
3366 addr->sadb_address_len =
3367 (sizeof(struct sadb_address)+sockaddr_size)/
3368 sizeof(uint64_t);
3369 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3370 addr->sadb_address_proto = 0;
3371 addr->sadb_address_reserved = 0;
3372 addr->sadb_address_prefixlen =
3373 pfkey_sockaddr_fill(&x->props.saddr, 0,
3374 (struct sockaddr *) (addr + 1),
3375 x->props.family);
3376 if (!addr->sadb_address_prefixlen)
3377 BUG();
3379 /* NAT_T_SPORT (old port) */
3380 n_port = skb_put(skb, sizeof(*n_port));
3381 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3382 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3383 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3384 n_port->sadb_x_nat_t_port_reserved = 0;
3386 /* ADDRESS_DST (new addr) */
3387 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
3388 addr->sadb_address_len =
3389 (sizeof(struct sadb_address)+sockaddr_size)/
3390 sizeof(uint64_t);
3391 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3392 addr->sadb_address_proto = 0;
3393 addr->sadb_address_reserved = 0;
3394 addr->sadb_address_prefixlen =
3395 pfkey_sockaddr_fill(ipaddr, 0,
3396 (struct sockaddr *) (addr + 1),
3397 x->props.family);
3398 if (!addr->sadb_address_prefixlen)
3399 BUG();
3401 /* NAT_T_DPORT (new port) */
3402 n_port = skb_put(skb, sizeof(*n_port));
3403 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3404 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3405 n_port->sadb_x_nat_t_port_port = sport;
3406 n_port->sadb_x_nat_t_port_reserved = 0;
3408 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3409 xs_net(x));
3412 #ifdef CONFIG_NET_KEY_MIGRATE
3413 static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3414 const struct xfrm_selector *sel)
3416 struct sadb_address *addr;
3417 addr = skb_put(skb, sizeof(struct sadb_address) + sasize);
3418 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3419 addr->sadb_address_exttype = type;
3420 addr->sadb_address_proto = sel->proto;
3421 addr->sadb_address_reserved = 0;
3423 switch (type) {
3424 case SADB_EXT_ADDRESS_SRC:
3425 addr->sadb_address_prefixlen = sel->prefixlen_s;
3426 pfkey_sockaddr_fill(&sel->saddr, 0,
3427 (struct sockaddr *)(addr + 1),
3428 sel->family);
3429 break;
3430 case SADB_EXT_ADDRESS_DST:
3431 addr->sadb_address_prefixlen = sel->prefixlen_d;
3432 pfkey_sockaddr_fill(&sel->daddr, 0,
3433 (struct sockaddr *)(addr + 1),
3434 sel->family);
3435 break;
3436 default:
3437 return -EINVAL;
3440 return 0;
3444 static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
3446 struct sadb_x_kmaddress *kma;
3447 u8 *sa;
3448 int family = k->family;
3449 int socklen = pfkey_sockaddr_len(family);
3450 int size_req;
3452 size_req = (sizeof(struct sadb_x_kmaddress) +
3453 pfkey_sockaddr_pair_size(family));
3455 kma = skb_put_zero(skb, size_req);
3456 kma->sadb_x_kmaddress_len = size_req / 8;
3457 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3458 kma->sadb_x_kmaddress_reserved = k->reserved;
3460 sa = (u8 *)(kma + 1);
3461 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3462 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3463 return -EINVAL;
3465 return 0;
3468 static int set_ipsecrequest(struct sk_buff *skb,
3469 uint8_t proto, uint8_t mode, int level,
3470 uint32_t reqid, uint8_t family,
3471 const xfrm_address_t *src, const xfrm_address_t *dst)
3473 struct sadb_x_ipsecrequest *rq;
3474 u8 *sa;
3475 int socklen = pfkey_sockaddr_len(family);
3476 int size_req;
3478 size_req = sizeof(struct sadb_x_ipsecrequest) +
3479 pfkey_sockaddr_pair_size(family);
3481 rq = skb_put_zero(skb, size_req);
3482 rq->sadb_x_ipsecrequest_len = size_req;
3483 rq->sadb_x_ipsecrequest_proto = proto;
3484 rq->sadb_x_ipsecrequest_mode = mode;
3485 rq->sadb_x_ipsecrequest_level = level;
3486 rq->sadb_x_ipsecrequest_reqid = reqid;
3488 sa = (u8 *) (rq + 1);
3489 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3490 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
3491 return -EINVAL;
3493 return 0;
3495 #endif
3497 #ifdef CONFIG_NET_KEY_MIGRATE
3498 static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3499 const struct xfrm_migrate *m, int num_bundles,
3500 const struct xfrm_kmaddress *k,
3501 const struct xfrm_encap_tmpl *encap)
3503 int i;
3504 int sasize_sel;
3505 int size = 0;
3506 int size_pol = 0;
3507 struct sk_buff *skb;
3508 struct sadb_msg *hdr;
3509 struct sadb_x_policy *pol;
3510 const struct xfrm_migrate *mp;
3512 if (type != XFRM_POLICY_TYPE_MAIN)
3513 return 0;
3515 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3516 return -EINVAL;
3518 if (k != NULL) {
3519 /* addresses for KM */
3520 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3521 pfkey_sockaddr_pair_size(k->family));
3524 /* selector */
3525 sasize_sel = pfkey_sockaddr_size(sel->family);
3526 if (!sasize_sel)
3527 return -EINVAL;
3528 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3530 /* policy info */
3531 size_pol += sizeof(struct sadb_x_policy);
3533 /* ipsecrequests */
3534 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3535 /* old locator pair */
3536 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3537 pfkey_sockaddr_pair_size(mp->old_family);
3538 /* new locator pair */
3539 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3540 pfkey_sockaddr_pair_size(mp->new_family);
3543 size += sizeof(struct sadb_msg) + size_pol;
3545 /* alloc buffer */
3546 skb = alloc_skb(size, GFP_ATOMIC);
3547 if (skb == NULL)
3548 return -ENOMEM;
3550 hdr = skb_put(skb, sizeof(struct sadb_msg));
3551 hdr->sadb_msg_version = PF_KEY_V2;
3552 hdr->sadb_msg_type = SADB_X_MIGRATE;
3553 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3554 hdr->sadb_msg_len = size / 8;
3555 hdr->sadb_msg_errno = 0;
3556 hdr->sadb_msg_reserved = 0;
3557 hdr->sadb_msg_seq = 0;
3558 hdr->sadb_msg_pid = 0;
3560 /* Addresses to be used by KM for negotiation, if ext is available */
3561 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
3562 goto err;
3564 /* selector src */
3565 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3567 /* selector dst */
3568 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3570 /* policy information */
3571 pol = skb_put(skb, sizeof(struct sadb_x_policy));
3572 pol->sadb_x_policy_len = size_pol / 8;
3573 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3574 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3575 pol->sadb_x_policy_dir = dir + 1;
3576 pol->sadb_x_policy_reserved = 0;
3577 pol->sadb_x_policy_id = 0;
3578 pol->sadb_x_policy_priority = 0;
3580 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3581 /* old ipsecrequest */
3582 int mode = pfkey_mode_from_xfrm(mp->mode);
3583 if (mode < 0)
3584 goto err;
3585 if (set_ipsecrequest(skb, mp->proto, mode,
3586 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3587 mp->reqid, mp->old_family,
3588 &mp->old_saddr, &mp->old_daddr) < 0)
3589 goto err;
3591 /* new ipsecrequest */
3592 if (set_ipsecrequest(skb, mp->proto, mode,
3593 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3594 mp->reqid, mp->new_family,
3595 &mp->new_saddr, &mp->new_daddr) < 0)
3596 goto err;
3599 /* broadcast migrate message to sockets */
3600 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net);
3602 return 0;
3604 err:
3605 kfree_skb(skb);
3606 return -EINVAL;
3608 #else
3609 static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3610 const struct xfrm_migrate *m, int num_bundles,
3611 const struct xfrm_kmaddress *k,
3612 const struct xfrm_encap_tmpl *encap)
3614 return -ENOPROTOOPT;
3616 #endif
3618 static int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
3620 struct sock *sk = sock->sk;
3621 struct sk_buff *skb = NULL;
3622 struct sadb_msg *hdr = NULL;
3623 int err;
3624 struct net *net = sock_net(sk);
3626 err = -EOPNOTSUPP;
3627 if (msg->msg_flags & MSG_OOB)
3628 goto out;
3630 err = -EMSGSIZE;
3631 if ((unsigned int)len > sk->sk_sndbuf - 32)
3632 goto out;
3634 err = -ENOBUFS;
3635 skb = alloc_skb(len, GFP_KERNEL);
3636 if (skb == NULL)
3637 goto out;
3639 err = -EFAULT;
3640 if (memcpy_from_msg(skb_put(skb,len), msg, len))
3641 goto out;
3643 hdr = pfkey_get_base_msg(skb, &err);
3644 if (!hdr)
3645 goto out;
3647 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
3648 err = pfkey_process(sk, skb, hdr);
3649 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
3651 out:
3652 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3653 err = 0;
3654 kfree_skb(skb);
3656 return err ? : len;
3659 static int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3660 int flags)
3662 struct sock *sk = sock->sk;
3663 struct pfkey_sock *pfk = pfkey_sk(sk);
3664 struct sk_buff *skb;
3665 int copied, err;
3667 err = -EINVAL;
3668 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3669 goto out;
3671 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3672 if (skb == NULL)
3673 goto out;
3675 copied = skb->len;
3676 if (copied > len) {
3677 msg->msg_flags |= MSG_TRUNC;
3678 copied = len;
3681 skb_reset_transport_header(skb);
3682 err = skb_copy_datagram_msg(skb, 0, msg, copied);
3683 if (err)
3684 goto out_free;
3686 sock_recv_ts_and_drops(msg, sk, skb);
3688 err = (flags & MSG_TRUNC) ? skb->len : copied;
3690 if (pfk->dump.dump != NULL &&
3691 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3692 pfkey_do_dump(pfk);
3694 out_free:
3695 skb_free_datagram(sk, skb);
3696 out:
3697 return err;
3700 static const struct proto_ops pfkey_ops = {
3701 .family = PF_KEY,
3702 .owner = THIS_MODULE,
3703 /* Operations that make no sense on pfkey sockets. */
3704 .bind = sock_no_bind,
3705 .connect = sock_no_connect,
3706 .socketpair = sock_no_socketpair,
3707 .accept = sock_no_accept,
3708 .getname = sock_no_getname,
3709 .ioctl = sock_no_ioctl,
3710 .listen = sock_no_listen,
3711 .shutdown = sock_no_shutdown,
3712 .setsockopt = sock_no_setsockopt,
3713 .getsockopt = sock_no_getsockopt,
3714 .mmap = sock_no_mmap,
3715 .sendpage = sock_no_sendpage,
3717 /* Now the operations that really occur. */
3718 .release = pfkey_release,
3719 .poll = datagram_poll,
3720 .sendmsg = pfkey_sendmsg,
3721 .recvmsg = pfkey_recvmsg,
3724 static const struct net_proto_family pfkey_family_ops = {
3725 .family = PF_KEY,
3726 .create = pfkey_create,
3727 .owner = THIS_MODULE,
3730 #ifdef CONFIG_PROC_FS
3731 static int pfkey_seq_show(struct seq_file *f, void *v)
3733 struct sock *s = sk_entry(v);
3735 if (v == SEQ_START_TOKEN)
3736 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3737 else
3738 seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
3740 refcount_read(&s->sk_refcnt),
3741 sk_rmem_alloc_get(s),
3742 sk_wmem_alloc_get(s),
3743 from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
3744 sock_i_ino(s)
3746 return 0;
3749 static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3750 __acquires(rcu)
3752 struct net *net = seq_file_net(f);
3753 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3755 rcu_read_lock();
3756 return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
3759 static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3761 struct net *net = seq_file_net(f);
3762 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3764 return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
3767 static void pfkey_seq_stop(struct seq_file *f, void *v)
3768 __releases(rcu)
3770 rcu_read_unlock();
3773 static const struct seq_operations pfkey_seq_ops = {
3774 .start = pfkey_seq_start,
3775 .next = pfkey_seq_next,
3776 .stop = pfkey_seq_stop,
3777 .show = pfkey_seq_show,
3780 static int pfkey_seq_open(struct inode *inode, struct file *file)
3782 return seq_open_net(inode, file, &pfkey_seq_ops,
3783 sizeof(struct seq_net_private));
3786 static const struct file_operations pfkey_proc_ops = {
3787 .open = pfkey_seq_open,
3788 .read = seq_read,
3789 .llseek = seq_lseek,
3790 .release = seq_release_net,
3793 static int __net_init pfkey_init_proc(struct net *net)
3795 struct proc_dir_entry *e;
3797 e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops);
3798 if (e == NULL)
3799 return -ENOMEM;
3801 return 0;
3804 static void __net_exit pfkey_exit_proc(struct net *net)
3806 remove_proc_entry("pfkey", net->proc_net);
3808 #else
3809 static inline int pfkey_init_proc(struct net *net)
3811 return 0;
3814 static inline void pfkey_exit_proc(struct net *net)
3817 #endif
3819 static struct xfrm_mgr pfkeyv2_mgr =
3821 .notify = pfkey_send_notify,
3822 .acquire = pfkey_send_acquire,
3823 .compile_policy = pfkey_compile_policy,
3824 .new_mapping = pfkey_send_new_mapping,
3825 .notify_policy = pfkey_send_policy_notify,
3826 .migrate = pfkey_send_migrate,
3827 .is_alive = pfkey_is_alive,
3830 static int __net_init pfkey_net_init(struct net *net)
3832 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3833 int rv;
3835 INIT_HLIST_HEAD(&net_pfkey->table);
3836 atomic_set(&net_pfkey->socks_nr, 0);
3838 rv = pfkey_init_proc(net);
3840 return rv;
3843 static void __net_exit pfkey_net_exit(struct net *net)
3845 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3847 pfkey_exit_proc(net);
3848 BUG_ON(!hlist_empty(&net_pfkey->table));
3851 static struct pernet_operations pfkey_net_ops = {
3852 .init = pfkey_net_init,
3853 .exit = pfkey_net_exit,
3854 .id = &pfkey_net_id,
3855 .size = sizeof(struct netns_pfkey),
3858 static void __exit ipsec_pfkey_exit(void)
3860 xfrm_unregister_km(&pfkeyv2_mgr);
3861 sock_unregister(PF_KEY);
3862 unregister_pernet_subsys(&pfkey_net_ops);
3863 proto_unregister(&key_proto);
3866 static int __init ipsec_pfkey_init(void)
3868 int err = proto_register(&key_proto, 0);
3870 if (err != 0)
3871 goto out;
3873 err = register_pernet_subsys(&pfkey_net_ops);
3874 if (err != 0)
3875 goto out_unregister_key_proto;
3876 err = sock_register(&pfkey_family_ops);
3877 if (err != 0)
3878 goto out_unregister_pernet;
3879 err = xfrm_register_km(&pfkeyv2_mgr);
3880 if (err != 0)
3881 goto out_sock_unregister;
3882 out:
3883 return err;
3885 out_sock_unregister:
3886 sock_unregister(PF_KEY);
3887 out_unregister_pernet:
3888 unregister_pernet_subsys(&pfkey_net_ops);
3889 out_unregister_key_proto:
3890 proto_unregister(&key_proto);
3891 goto out;
3894 module_init(ipsec_pfkey_init);
3895 module_exit(ipsec_pfkey_exit);
3896 MODULE_LICENSE("GPL");
3897 MODULE_ALIAS_NETPROTO(PF_KEY);