net/ncsi: Use real net-device for response handler
[linux/fpc-iii.git] / net / key / af_key.c
blob76a008b1cbe5fde99ab268960f05f109dd4ef564
1 /*
2 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Maxim Giryaev <gem@asplinux.ru>
10 * David S. Miller <davem@redhat.com>
11 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
12 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
14 * Derek Atkins <derek@ihtfp.com>
17 #include <linux/capability.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/socket.h>
21 #include <linux/pfkeyv2.h>
22 #include <linux/ipsec.h>
23 #include <linux/skbuff.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/in.h>
26 #include <linux/in6.h>
27 #include <linux/proc_fs.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <net/net_namespace.h>
31 #include <net/netns/generic.h>
32 #include <net/xfrm.h>
34 #include <net/sock.h>
36 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
37 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
39 static int pfkey_net_id __read_mostly;
40 struct netns_pfkey {
41 /* List of all pfkey sockets. */
42 struct hlist_head table;
43 atomic_t socks_nr;
45 static DEFINE_MUTEX(pfkey_mutex);
47 #define DUMMY_MARK 0
48 static const struct xfrm_mark dummy_mark = {0, 0};
49 struct pfkey_sock {
50 /* struct sock must be the first member of struct pfkey_sock */
51 struct sock sk;
52 int registered;
53 int promisc;
55 struct {
56 uint8_t msg_version;
57 uint32_t msg_portid;
58 int (*dump)(struct pfkey_sock *sk);
59 void (*done)(struct pfkey_sock *sk);
60 union {
61 struct xfrm_policy_walk policy;
62 struct xfrm_state_walk state;
63 } u;
64 struct sk_buff *skb;
65 } dump;
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(atomic_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 (atomic_read(&skb->users) != 1) {
207 *skb2 = skb_clone(skb, allocation);
208 } else {
209 *skb2 = skb;
210 atomic_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 = (struct sadb_msg *) 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 if (sp->sadb_address_len <
405 DIV_ROUND_UP(sizeof(*sp) + offsetofend(typeof(*addr), sa_family),
406 sizeof(uint64_t)))
407 return -EINVAL;
409 switch (addr->sa_family) {
410 case AF_INET:
411 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
412 if (sp->sadb_address_len != len ||
413 sp->sadb_address_prefixlen > 32)
414 return -EINVAL;
415 break;
416 #if IS_ENABLED(CONFIG_IPV6)
417 case AF_INET6:
418 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
419 if (sp->sadb_address_len != len ||
420 sp->sadb_address_prefixlen > 128)
421 return -EINVAL;
422 break;
423 #endif
424 default:
425 /* It is user using kernel to keep track of security
426 * associations for another protocol, such as
427 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
428 * lengths.
430 * XXX Actually, association/policy database is not yet
431 * XXX able to cope with arbitrary sockaddr families.
432 * XXX When it can, remove this -EINVAL. -DaveM
434 return -EINVAL;
437 return 0;
440 static inline int sadb_key_len(const struct sadb_key *key)
442 int key_bytes = DIV_ROUND_UP(key->sadb_key_bits, 8);
444 return DIV_ROUND_UP(sizeof(struct sadb_key) + key_bytes,
445 sizeof(uint64_t));
448 static int verify_key_len(const void *p)
450 const struct sadb_key *key = p;
452 if (sadb_key_len(key) > key->sadb_key_len)
453 return -EINVAL;
455 return 0;
458 static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
460 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
461 sec_ctx->sadb_x_ctx_len,
462 sizeof(uint64_t));
465 static inline int verify_sec_ctx_len(const void *p)
467 const struct sadb_x_sec_ctx *sec_ctx = p;
468 int len = sec_ctx->sadb_x_ctx_len;
470 if (len > PAGE_SIZE)
471 return -EINVAL;
473 len = pfkey_sec_ctx_len(sec_ctx);
475 if (sec_ctx->sadb_x_sec_len != len)
476 return -EINVAL;
478 return 0;
481 static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx,
482 gfp_t gfp)
484 struct xfrm_user_sec_ctx *uctx = NULL;
485 int ctx_size = sec_ctx->sadb_x_ctx_len;
487 uctx = kmalloc((sizeof(*uctx)+ctx_size), gfp);
489 if (!uctx)
490 return NULL;
492 uctx->len = pfkey_sec_ctx_len(sec_ctx);
493 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
494 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
495 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
496 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
497 memcpy(uctx + 1, sec_ctx + 1,
498 uctx->ctx_len);
500 return uctx;
503 static int present_and_same_family(const struct sadb_address *src,
504 const struct sadb_address *dst)
506 const struct sockaddr *s_addr, *d_addr;
508 if (!src || !dst)
509 return 0;
511 s_addr = (const struct sockaddr *)(src + 1);
512 d_addr = (const struct sockaddr *)(dst + 1);
513 if (s_addr->sa_family != d_addr->sa_family)
514 return 0;
515 if (s_addr->sa_family != AF_INET
516 #if IS_ENABLED(CONFIG_IPV6)
517 && s_addr->sa_family != AF_INET6
518 #endif
520 return 0;
522 return 1;
525 static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
527 const char *p = (char *) hdr;
528 int len = skb->len;
530 len -= sizeof(*hdr);
531 p += sizeof(*hdr);
532 while (len > 0) {
533 const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
534 uint16_t ext_type;
535 int ext_len;
537 if (len < sizeof(*ehdr))
538 return -EINVAL;
540 ext_len = ehdr->sadb_ext_len;
541 ext_len *= sizeof(uint64_t);
542 ext_type = ehdr->sadb_ext_type;
543 if (ext_len < sizeof(uint64_t) ||
544 ext_len > len ||
545 ext_type == SADB_EXT_RESERVED)
546 return -EINVAL;
548 if (ext_type <= SADB_EXT_MAX) {
549 int min = (int) sadb_ext_min_len[ext_type];
550 if (ext_len < min)
551 return -EINVAL;
552 if (ext_hdrs[ext_type-1] != NULL)
553 return -EINVAL;
554 switch (ext_type) {
555 case SADB_EXT_ADDRESS_SRC:
556 case SADB_EXT_ADDRESS_DST:
557 case SADB_EXT_ADDRESS_PROXY:
558 case SADB_X_EXT_NAT_T_OA:
559 if (verify_address_len(p))
560 return -EINVAL;
561 break;
562 case SADB_X_EXT_SEC_CTX:
563 if (verify_sec_ctx_len(p))
564 return -EINVAL;
565 break;
566 case SADB_EXT_KEY_AUTH:
567 case SADB_EXT_KEY_ENCRYPT:
568 if (verify_key_len(p))
569 return -EINVAL;
570 break;
571 default:
572 break;
574 ext_hdrs[ext_type-1] = (void *) p;
576 p += ext_len;
577 len -= ext_len;
580 return 0;
583 static uint16_t
584 pfkey_satype2proto(uint8_t satype)
586 switch (satype) {
587 case SADB_SATYPE_UNSPEC:
588 return IPSEC_PROTO_ANY;
589 case SADB_SATYPE_AH:
590 return IPPROTO_AH;
591 case SADB_SATYPE_ESP:
592 return IPPROTO_ESP;
593 case SADB_X_SATYPE_IPCOMP:
594 return IPPROTO_COMP;
595 default:
596 return 0;
598 /* NOTREACHED */
601 static uint8_t
602 pfkey_proto2satype(uint16_t proto)
604 switch (proto) {
605 case IPPROTO_AH:
606 return SADB_SATYPE_AH;
607 case IPPROTO_ESP:
608 return SADB_SATYPE_ESP;
609 case IPPROTO_COMP:
610 return SADB_X_SATYPE_IPCOMP;
611 default:
612 return 0;
614 /* NOTREACHED */
617 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
618 * say specifically 'just raw sockets' as we encode them as 255.
621 static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
623 return proto == IPSEC_PROTO_ANY ? 0 : proto;
626 static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
628 return proto ? proto : IPSEC_PROTO_ANY;
631 static inline int pfkey_sockaddr_len(sa_family_t family)
633 switch (family) {
634 case AF_INET:
635 return sizeof(struct sockaddr_in);
636 #if IS_ENABLED(CONFIG_IPV6)
637 case AF_INET6:
638 return sizeof(struct sockaddr_in6);
639 #endif
641 return 0;
644 static
645 int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
647 switch (sa->sa_family) {
648 case AF_INET:
649 xaddr->a4 =
650 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
651 return AF_INET;
652 #if IS_ENABLED(CONFIG_IPV6)
653 case AF_INET6:
654 memcpy(xaddr->a6,
655 &((struct sockaddr_in6 *)sa)->sin6_addr,
656 sizeof(struct in6_addr));
657 return AF_INET6;
658 #endif
660 return 0;
663 static
664 int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
666 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
667 xaddr);
670 static struct xfrm_state *pfkey_xfrm_state_lookup(struct net *net, const struct sadb_msg *hdr, void * const *ext_hdrs)
672 const struct sadb_sa *sa;
673 const struct sadb_address *addr;
674 uint16_t proto;
675 unsigned short family;
676 xfrm_address_t *xaddr;
678 sa = ext_hdrs[SADB_EXT_SA - 1];
679 if (sa == NULL)
680 return NULL;
682 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
683 if (proto == 0)
684 return NULL;
686 /* sadb_address_len should be checked by caller */
687 addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
688 if (addr == NULL)
689 return NULL;
691 family = ((const struct sockaddr *)(addr + 1))->sa_family;
692 switch (family) {
693 case AF_INET:
694 xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
695 break;
696 #if IS_ENABLED(CONFIG_IPV6)
697 case AF_INET6:
698 xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
699 break;
700 #endif
701 default:
702 xaddr = NULL;
705 if (!xaddr)
706 return NULL;
708 return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
711 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
713 static int
714 pfkey_sockaddr_size(sa_family_t family)
716 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
719 static inline int pfkey_mode_from_xfrm(int mode)
721 switch(mode) {
722 case XFRM_MODE_TRANSPORT:
723 return IPSEC_MODE_TRANSPORT;
724 case XFRM_MODE_TUNNEL:
725 return IPSEC_MODE_TUNNEL;
726 case XFRM_MODE_BEET:
727 return IPSEC_MODE_BEET;
728 default:
729 return -1;
733 static inline int pfkey_mode_to_xfrm(int mode)
735 switch(mode) {
736 case IPSEC_MODE_ANY: /*XXX*/
737 case IPSEC_MODE_TRANSPORT:
738 return XFRM_MODE_TRANSPORT;
739 case IPSEC_MODE_TUNNEL:
740 return XFRM_MODE_TUNNEL;
741 case IPSEC_MODE_BEET:
742 return XFRM_MODE_BEET;
743 default:
744 return -1;
748 static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
749 struct sockaddr *sa,
750 unsigned short family)
752 switch (family) {
753 case AF_INET:
755 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
756 sin->sin_family = AF_INET;
757 sin->sin_port = port;
758 sin->sin_addr.s_addr = xaddr->a4;
759 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
760 return 32;
762 #if IS_ENABLED(CONFIG_IPV6)
763 case AF_INET6:
765 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
766 sin6->sin6_family = AF_INET6;
767 sin6->sin6_port = port;
768 sin6->sin6_flowinfo = 0;
769 sin6->sin6_addr = xaddr->in6;
770 sin6->sin6_scope_id = 0;
771 return 128;
773 #endif
775 return 0;
778 static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
779 int add_keys, int hsc)
781 struct sk_buff *skb;
782 struct sadb_msg *hdr;
783 struct sadb_sa *sa;
784 struct sadb_lifetime *lifetime;
785 struct sadb_address *addr;
786 struct sadb_key *key;
787 struct sadb_x_sa2 *sa2;
788 struct sadb_x_sec_ctx *sec_ctx;
789 struct xfrm_sec_ctx *xfrm_ctx;
790 int ctx_size = 0;
791 int size;
792 int auth_key_size = 0;
793 int encrypt_key_size = 0;
794 int sockaddr_size;
795 struct xfrm_encap_tmpl *natt = NULL;
796 int mode;
798 /* address family check */
799 sockaddr_size = pfkey_sockaddr_size(x->props.family);
800 if (!sockaddr_size)
801 return ERR_PTR(-EINVAL);
803 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
804 key(AE), (identity(SD),) (sensitivity)> */
805 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
806 sizeof(struct sadb_lifetime) +
807 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
808 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
809 sizeof(struct sadb_address)*2 +
810 sockaddr_size*2 +
811 sizeof(struct sadb_x_sa2);
813 if ((xfrm_ctx = x->security)) {
814 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
815 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
818 /* identity & sensitivity */
819 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family))
820 size += sizeof(struct sadb_address) + sockaddr_size;
822 if (add_keys) {
823 if (x->aalg && x->aalg->alg_key_len) {
824 auth_key_size =
825 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
826 size += sizeof(struct sadb_key) + auth_key_size;
828 if (x->ealg && x->ealg->alg_key_len) {
829 encrypt_key_size =
830 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
831 size += sizeof(struct sadb_key) + encrypt_key_size;
834 if (x->encap)
835 natt = x->encap;
837 if (natt && natt->encap_type) {
838 size += sizeof(struct sadb_x_nat_t_type);
839 size += sizeof(struct sadb_x_nat_t_port);
840 size += sizeof(struct sadb_x_nat_t_port);
843 skb = alloc_skb(size + 16, GFP_ATOMIC);
844 if (skb == NULL)
845 return ERR_PTR(-ENOBUFS);
847 /* call should fill header later */
848 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
849 memset(hdr, 0, size); /* XXX do we need this ? */
850 hdr->sadb_msg_len = size / sizeof(uint64_t);
852 /* sa */
853 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
854 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
855 sa->sadb_sa_exttype = SADB_EXT_SA;
856 sa->sadb_sa_spi = x->id.spi;
857 sa->sadb_sa_replay = x->props.replay_window;
858 switch (x->km.state) {
859 case XFRM_STATE_VALID:
860 sa->sadb_sa_state = x->km.dying ?
861 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
862 break;
863 case XFRM_STATE_ACQ:
864 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
865 break;
866 default:
867 sa->sadb_sa_state = SADB_SASTATE_DEAD;
868 break;
870 sa->sadb_sa_auth = 0;
871 if (x->aalg) {
872 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
873 sa->sadb_sa_auth = (a && a->pfkey_supported) ?
874 a->desc.sadb_alg_id : 0;
876 sa->sadb_sa_encrypt = 0;
877 BUG_ON(x->ealg && x->calg);
878 if (x->ealg) {
879 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
880 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
881 a->desc.sadb_alg_id : 0;
883 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
884 if (x->calg) {
885 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
886 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
887 a->desc.sadb_alg_id : 0;
890 sa->sadb_sa_flags = 0;
891 if (x->props.flags & XFRM_STATE_NOECN)
892 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
893 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
894 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
895 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
896 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
898 /* hard time */
899 if (hsc & 2) {
900 lifetime = (struct sadb_lifetime *) skb_put(skb,
901 sizeof(struct sadb_lifetime));
902 lifetime->sadb_lifetime_len =
903 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
904 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
905 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
906 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
907 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
908 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
910 /* soft time */
911 if (hsc & 1) {
912 lifetime = (struct sadb_lifetime *) skb_put(skb,
913 sizeof(struct sadb_lifetime));
914 lifetime->sadb_lifetime_len =
915 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
916 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
917 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
918 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
919 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
920 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
922 /* current time */
923 lifetime = (struct sadb_lifetime *) skb_put(skb,
924 sizeof(struct sadb_lifetime));
925 lifetime->sadb_lifetime_len =
926 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
927 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
928 lifetime->sadb_lifetime_allocations = x->curlft.packets;
929 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
930 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
931 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
932 /* src address */
933 addr = (struct sadb_address*) skb_put(skb,
934 sizeof(struct sadb_address)+sockaddr_size);
935 addr->sadb_address_len =
936 (sizeof(struct sadb_address)+sockaddr_size)/
937 sizeof(uint64_t);
938 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
939 /* "if the ports are non-zero, then the sadb_address_proto field,
940 normally zero, MUST be filled in with the transport
941 protocol's number." - RFC2367 */
942 addr->sadb_address_proto = 0;
943 addr->sadb_address_reserved = 0;
945 addr->sadb_address_prefixlen =
946 pfkey_sockaddr_fill(&x->props.saddr, 0,
947 (struct sockaddr *) (addr + 1),
948 x->props.family);
949 if (!addr->sadb_address_prefixlen)
950 BUG();
952 /* dst address */
953 addr = (struct sadb_address*) skb_put(skb,
954 sizeof(struct sadb_address)+sockaddr_size);
955 addr->sadb_address_len =
956 (sizeof(struct sadb_address)+sockaddr_size)/
957 sizeof(uint64_t);
958 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
959 addr->sadb_address_proto = 0;
960 addr->sadb_address_reserved = 0;
962 addr->sadb_address_prefixlen =
963 pfkey_sockaddr_fill(&x->id.daddr, 0,
964 (struct sockaddr *) (addr + 1),
965 x->props.family);
966 if (!addr->sadb_address_prefixlen)
967 BUG();
969 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
970 x->props.family)) {
971 addr = (struct sadb_address*) skb_put(skb,
972 sizeof(struct sadb_address)+sockaddr_size);
973 addr->sadb_address_len =
974 (sizeof(struct sadb_address)+sockaddr_size)/
975 sizeof(uint64_t);
976 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
977 addr->sadb_address_proto =
978 pfkey_proto_from_xfrm(x->sel.proto);
979 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
980 addr->sadb_address_reserved = 0;
982 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
983 (struct sockaddr *) (addr + 1),
984 x->props.family);
987 /* auth key */
988 if (add_keys && auth_key_size) {
989 key = (struct sadb_key *) skb_put(skb,
990 sizeof(struct sadb_key)+auth_key_size);
991 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
992 sizeof(uint64_t);
993 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
994 key->sadb_key_bits = x->aalg->alg_key_len;
995 key->sadb_key_reserved = 0;
996 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
998 /* encrypt key */
999 if (add_keys && encrypt_key_size) {
1000 key = (struct sadb_key *) skb_put(skb,
1001 sizeof(struct sadb_key)+encrypt_key_size);
1002 key->sadb_key_len = (sizeof(struct sadb_key) +
1003 encrypt_key_size) / sizeof(uint64_t);
1004 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
1005 key->sadb_key_bits = x->ealg->alg_key_len;
1006 key->sadb_key_reserved = 0;
1007 memcpy(key + 1, x->ealg->alg_key,
1008 (x->ealg->alg_key_len+7)/8);
1011 /* sa */
1012 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
1013 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
1014 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
1015 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
1016 kfree_skb(skb);
1017 return ERR_PTR(-EINVAL);
1019 sa2->sadb_x_sa2_mode = mode;
1020 sa2->sadb_x_sa2_reserved1 = 0;
1021 sa2->sadb_x_sa2_reserved2 = 0;
1022 sa2->sadb_x_sa2_sequence = 0;
1023 sa2->sadb_x_sa2_reqid = x->props.reqid;
1025 if (natt && natt->encap_type) {
1026 struct sadb_x_nat_t_type *n_type;
1027 struct sadb_x_nat_t_port *n_port;
1029 /* type */
1030 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
1031 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
1032 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
1033 n_type->sadb_x_nat_t_type_type = natt->encap_type;
1034 n_type->sadb_x_nat_t_type_reserved[0] = 0;
1035 n_type->sadb_x_nat_t_type_reserved[1] = 0;
1036 n_type->sadb_x_nat_t_type_reserved[2] = 0;
1038 /* source port */
1039 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
1040 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1041 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
1042 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
1043 n_port->sadb_x_nat_t_port_reserved = 0;
1045 /* dest port */
1046 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
1047 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1048 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
1049 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
1050 n_port->sadb_x_nat_t_port_reserved = 0;
1053 /* security context */
1054 if (xfrm_ctx) {
1055 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1056 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1057 sec_ctx->sadb_x_sec_len =
1058 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1059 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1060 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1061 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1062 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1063 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1064 xfrm_ctx->ctx_len);
1067 return skb;
1071 static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
1073 struct sk_buff *skb;
1075 skb = __pfkey_xfrm_state2msg(x, 1, 3);
1077 return skb;
1080 static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
1081 int hsc)
1083 return __pfkey_xfrm_state2msg(x, 0, hsc);
1086 static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
1087 const struct sadb_msg *hdr,
1088 void * const *ext_hdrs)
1090 struct xfrm_state *x;
1091 const struct sadb_lifetime *lifetime;
1092 const struct sadb_sa *sa;
1093 const struct sadb_key *key;
1094 const struct sadb_x_sec_ctx *sec_ctx;
1095 uint16_t proto;
1096 int err;
1099 sa = ext_hdrs[SADB_EXT_SA - 1];
1100 if (!sa ||
1101 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1102 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1103 return ERR_PTR(-EINVAL);
1104 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1105 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1106 return ERR_PTR(-EINVAL);
1107 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1108 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1109 return ERR_PTR(-EINVAL);
1110 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1111 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1112 return ERR_PTR(-EINVAL);
1114 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1115 if (proto == 0)
1116 return ERR_PTR(-EINVAL);
1118 /* default error is no buffer space */
1119 err = -ENOBUFS;
1121 /* RFC2367:
1123 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1124 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1125 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1126 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1127 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1128 not true.
1130 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1131 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1133 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1134 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1135 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1136 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1137 return ERR_PTR(-EINVAL);
1138 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
1139 if (key != NULL &&
1140 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1141 key->sadb_key_bits == 0)
1142 return ERR_PTR(-EINVAL);
1143 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1144 if (key != NULL &&
1145 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1146 key->sadb_key_bits == 0)
1147 return ERR_PTR(-EINVAL);
1149 x = xfrm_state_alloc(net);
1150 if (x == NULL)
1151 return ERR_PTR(-ENOBUFS);
1153 x->id.proto = proto;
1154 x->id.spi = sa->sadb_sa_spi;
1155 x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay,
1156 (sizeof(x->replay.bitmap) * 8));
1157 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1158 x->props.flags |= XFRM_STATE_NOECN;
1159 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1160 x->props.flags |= XFRM_STATE_DECAP_DSCP;
1161 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1162 x->props.flags |= XFRM_STATE_NOPMTUDISC;
1164 lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
1165 if (lifetime != NULL) {
1166 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1167 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1168 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1169 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1171 lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
1172 if (lifetime != NULL) {
1173 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1174 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1175 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1176 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1179 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
1180 if (sec_ctx != NULL) {
1181 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
1183 if (!uctx)
1184 goto out;
1186 err = security_xfrm_state_alloc(x, uctx);
1187 kfree(uctx);
1189 if (err)
1190 goto out;
1193 err = -ENOBUFS;
1194 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
1195 if (sa->sadb_sa_auth) {
1196 int keysize = 0;
1197 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1198 if (!a || !a->pfkey_supported) {
1199 err = -ENOSYS;
1200 goto out;
1202 if (key)
1203 keysize = (key->sadb_key_bits + 7) / 8;
1204 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1205 if (!x->aalg) {
1206 err = -ENOMEM;
1207 goto out;
1209 strcpy(x->aalg->alg_name, a->name);
1210 x->aalg->alg_key_len = 0;
1211 if (key) {
1212 x->aalg->alg_key_len = key->sadb_key_bits;
1213 memcpy(x->aalg->alg_key, key+1, keysize);
1215 x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
1216 x->props.aalgo = sa->sadb_sa_auth;
1217 /* x->algo.flags = sa->sadb_sa_flags; */
1219 if (sa->sadb_sa_encrypt) {
1220 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1221 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1222 if (!a || !a->pfkey_supported) {
1223 err = -ENOSYS;
1224 goto out;
1226 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1227 if (!x->calg) {
1228 err = -ENOMEM;
1229 goto out;
1231 strcpy(x->calg->alg_name, a->name);
1232 x->props.calgo = sa->sadb_sa_encrypt;
1233 } else {
1234 int keysize = 0;
1235 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1236 if (!a || !a->pfkey_supported) {
1237 err = -ENOSYS;
1238 goto out;
1240 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1241 if (key)
1242 keysize = (key->sadb_key_bits + 7) / 8;
1243 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1244 if (!x->ealg) {
1245 err = -ENOMEM;
1246 goto out;
1248 strcpy(x->ealg->alg_name, a->name);
1249 x->ealg->alg_key_len = 0;
1250 if (key) {
1251 x->ealg->alg_key_len = key->sadb_key_bits;
1252 memcpy(x->ealg->alg_key, key+1, keysize);
1254 x->props.ealgo = sa->sadb_sa_encrypt;
1255 x->geniv = a->uinfo.encr.geniv;
1258 /* x->algo.flags = sa->sadb_sa_flags; */
1260 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1261 &x->props.saddr);
1262 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1263 &x->id.daddr);
1265 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1266 const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
1267 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1268 if (mode < 0) {
1269 err = -EINVAL;
1270 goto out;
1272 x->props.mode = mode;
1273 x->props.reqid = sa2->sadb_x_sa2_reqid;
1276 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1277 const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1279 /* Nobody uses this, but we try. */
1280 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1281 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1284 if (!x->sel.family)
1285 x->sel.family = x->props.family;
1287 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1288 const struct sadb_x_nat_t_type* n_type;
1289 struct xfrm_encap_tmpl *natt;
1291 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1292 if (!x->encap) {
1293 err = -ENOMEM;
1294 goto out;
1297 natt = x->encap;
1298 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1299 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1301 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1302 const struct sadb_x_nat_t_port *n_port =
1303 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1304 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1306 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1307 const struct sadb_x_nat_t_port *n_port =
1308 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1309 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1311 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
1314 err = xfrm_init_state(x);
1315 if (err)
1316 goto out;
1318 x->km.seq = hdr->sadb_msg_seq;
1319 return x;
1321 out:
1322 x->km.state = XFRM_STATE_DEAD;
1323 xfrm_state_put(x);
1324 return ERR_PTR(err);
1327 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1329 return -EOPNOTSUPP;
1332 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1334 struct net *net = sock_net(sk);
1335 struct sk_buff *resp_skb;
1336 struct sadb_x_sa2 *sa2;
1337 struct sadb_address *saddr, *daddr;
1338 struct sadb_msg *out_hdr;
1339 struct sadb_spirange *range;
1340 struct xfrm_state *x = NULL;
1341 int mode;
1342 int err;
1343 u32 min_spi, max_spi;
1344 u32 reqid;
1345 u8 proto;
1346 unsigned short family;
1347 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1349 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1350 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1351 return -EINVAL;
1353 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1354 if (proto == 0)
1355 return -EINVAL;
1357 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1358 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1359 if (mode < 0)
1360 return -EINVAL;
1361 reqid = sa2->sadb_x_sa2_reqid;
1362 } else {
1363 mode = 0;
1364 reqid = 0;
1367 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1368 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1370 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1371 switch (family) {
1372 case AF_INET:
1373 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1374 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1375 break;
1376 #if IS_ENABLED(CONFIG_IPV6)
1377 case AF_INET6:
1378 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1379 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1380 break;
1381 #endif
1384 if (hdr->sadb_msg_seq) {
1385 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
1386 if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
1387 xfrm_state_put(x);
1388 x = NULL;
1392 if (!x)
1393 x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
1395 if (x == NULL)
1396 return -ENOENT;
1398 min_spi = 0x100;
1399 max_spi = 0x0fffffff;
1401 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1402 if (range) {
1403 min_spi = range->sadb_spirange_min;
1404 max_spi = range->sadb_spirange_max;
1407 err = verify_spi_info(x->id.proto, min_spi, max_spi);
1408 if (err) {
1409 xfrm_state_put(x);
1410 return err;
1413 err = xfrm_alloc_spi(x, min_spi, max_spi);
1414 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
1416 if (IS_ERR(resp_skb)) {
1417 xfrm_state_put(x);
1418 return PTR_ERR(resp_skb);
1421 out_hdr = (struct sadb_msg *) resp_skb->data;
1422 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1423 out_hdr->sadb_msg_type = SADB_GETSPI;
1424 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1425 out_hdr->sadb_msg_errno = 0;
1426 out_hdr->sadb_msg_reserved = 0;
1427 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1428 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1430 xfrm_state_put(x);
1432 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net);
1434 return 0;
1437 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1439 struct net *net = sock_net(sk);
1440 struct xfrm_state *x;
1442 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1443 return -EOPNOTSUPP;
1445 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1446 return 0;
1448 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
1449 if (x == NULL)
1450 return 0;
1452 spin_lock_bh(&x->lock);
1453 if (x->km.state == XFRM_STATE_ACQ)
1454 x->km.state = XFRM_STATE_ERROR;
1456 spin_unlock_bh(&x->lock);
1457 xfrm_state_put(x);
1458 return 0;
1461 static inline int event2poltype(int event)
1463 switch (event) {
1464 case XFRM_MSG_DELPOLICY:
1465 return SADB_X_SPDDELETE;
1466 case XFRM_MSG_NEWPOLICY:
1467 return SADB_X_SPDADD;
1468 case XFRM_MSG_UPDPOLICY:
1469 return SADB_X_SPDUPDATE;
1470 case XFRM_MSG_POLEXPIRE:
1471 // return SADB_X_SPDEXPIRE;
1472 default:
1473 pr_err("pfkey: Unknown policy event %d\n", event);
1474 break;
1477 return 0;
1480 static inline int event2keytype(int event)
1482 switch (event) {
1483 case XFRM_MSG_DELSA:
1484 return SADB_DELETE;
1485 case XFRM_MSG_NEWSA:
1486 return SADB_ADD;
1487 case XFRM_MSG_UPDSA:
1488 return SADB_UPDATE;
1489 case XFRM_MSG_EXPIRE:
1490 return SADB_EXPIRE;
1491 default:
1492 pr_err("pfkey: Unknown SA event %d\n", event);
1493 break;
1496 return 0;
1499 /* ADD/UPD/DEL */
1500 static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
1502 struct sk_buff *skb;
1503 struct sadb_msg *hdr;
1505 skb = pfkey_xfrm_state2msg(x);
1507 if (IS_ERR(skb))
1508 return PTR_ERR(skb);
1510 hdr = (struct sadb_msg *) skb->data;
1511 hdr->sadb_msg_version = PF_KEY_V2;
1512 hdr->sadb_msg_type = event2keytype(c->event);
1513 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1514 hdr->sadb_msg_errno = 0;
1515 hdr->sadb_msg_reserved = 0;
1516 hdr->sadb_msg_seq = c->seq;
1517 hdr->sadb_msg_pid = c->portid;
1519 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x));
1521 return 0;
1524 static int pfkey_add(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1526 struct net *net = sock_net(sk);
1527 struct xfrm_state *x;
1528 int err;
1529 struct km_event c;
1531 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
1532 if (IS_ERR(x))
1533 return PTR_ERR(x);
1535 xfrm_state_hold(x);
1536 if (hdr->sadb_msg_type == SADB_ADD)
1537 err = xfrm_state_add(x);
1538 else
1539 err = xfrm_state_update(x);
1541 xfrm_audit_state_add(x, err ? 0 : 1, true);
1543 if (err < 0) {
1544 x->km.state = XFRM_STATE_DEAD;
1545 __xfrm_state_put(x);
1546 goto out;
1549 if (hdr->sadb_msg_type == SADB_ADD)
1550 c.event = XFRM_MSG_NEWSA;
1551 else
1552 c.event = XFRM_MSG_UPDSA;
1553 c.seq = hdr->sadb_msg_seq;
1554 c.portid = hdr->sadb_msg_pid;
1555 km_state_notify(x, &c);
1556 out:
1557 xfrm_state_put(x);
1558 return err;
1561 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1563 struct net *net = sock_net(sk);
1564 struct xfrm_state *x;
1565 struct km_event c;
1566 int err;
1568 if (!ext_hdrs[SADB_EXT_SA-1] ||
1569 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1570 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1571 return -EINVAL;
1573 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
1574 if (x == NULL)
1575 return -ESRCH;
1577 if ((err = security_xfrm_state_delete(x)))
1578 goto out;
1580 if (xfrm_state_kern(x)) {
1581 err = -EPERM;
1582 goto out;
1585 err = xfrm_state_delete(x);
1587 if (err < 0)
1588 goto out;
1590 c.seq = hdr->sadb_msg_seq;
1591 c.portid = hdr->sadb_msg_pid;
1592 c.event = XFRM_MSG_DELSA;
1593 km_state_notify(x, &c);
1594 out:
1595 xfrm_audit_state_delete(x, err ? 0 : 1, true);
1596 xfrm_state_put(x);
1598 return err;
1601 static int pfkey_get(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1603 struct net *net = sock_net(sk);
1604 __u8 proto;
1605 struct sk_buff *out_skb;
1606 struct sadb_msg *out_hdr;
1607 struct xfrm_state *x;
1609 if (!ext_hdrs[SADB_EXT_SA-1] ||
1610 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1611 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1612 return -EINVAL;
1614 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
1615 if (x == NULL)
1616 return -ESRCH;
1618 out_skb = pfkey_xfrm_state2msg(x);
1619 proto = x->id.proto;
1620 xfrm_state_put(x);
1621 if (IS_ERR(out_skb))
1622 return PTR_ERR(out_skb);
1624 out_hdr = (struct sadb_msg *) out_skb->data;
1625 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1626 out_hdr->sadb_msg_type = SADB_GET;
1627 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1628 out_hdr->sadb_msg_errno = 0;
1629 out_hdr->sadb_msg_reserved = 0;
1630 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1631 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1632 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
1634 return 0;
1637 static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
1638 gfp_t allocation)
1640 struct sk_buff *skb;
1641 struct sadb_msg *hdr;
1642 int len, auth_len, enc_len, i;
1644 auth_len = xfrm_count_pfkey_auth_supported();
1645 if (auth_len) {
1646 auth_len *= sizeof(struct sadb_alg);
1647 auth_len += sizeof(struct sadb_supported);
1650 enc_len = xfrm_count_pfkey_enc_supported();
1651 if (enc_len) {
1652 enc_len *= sizeof(struct sadb_alg);
1653 enc_len += sizeof(struct sadb_supported);
1656 len = enc_len + auth_len + sizeof(struct sadb_msg);
1658 skb = alloc_skb(len + 16, allocation);
1659 if (!skb)
1660 goto out_put_algs;
1662 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1663 pfkey_hdr_dup(hdr, orig);
1664 hdr->sadb_msg_errno = 0;
1665 hdr->sadb_msg_len = len / sizeof(uint64_t);
1667 if (auth_len) {
1668 struct sadb_supported *sp;
1669 struct sadb_alg *ap;
1671 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1672 ap = (struct sadb_alg *) (sp + 1);
1674 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1675 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1677 for (i = 0; ; i++) {
1678 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1679 if (!aalg)
1680 break;
1681 if (!aalg->pfkey_supported)
1682 continue;
1683 if (aalg->available)
1684 *ap++ = aalg->desc;
1688 if (enc_len) {
1689 struct sadb_supported *sp;
1690 struct sadb_alg *ap;
1692 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1693 ap = (struct sadb_alg *) (sp + 1);
1695 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1696 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1698 for (i = 0; ; i++) {
1699 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1700 if (!ealg)
1701 break;
1702 if (!ealg->pfkey_supported)
1703 continue;
1704 if (ealg->available)
1705 *ap++ = ealg->desc;
1709 out_put_algs:
1710 return skb;
1713 static int pfkey_register(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1715 struct pfkey_sock *pfk = pfkey_sk(sk);
1716 struct sk_buff *supp_skb;
1718 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1719 return -EINVAL;
1721 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1722 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1723 return -EEXIST;
1724 pfk->registered |= (1<<hdr->sadb_msg_satype);
1727 xfrm_probe_algs();
1729 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1730 if (!supp_skb) {
1731 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1732 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1734 return -ENOBUFS;
1737 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk,
1738 sock_net(sk));
1739 return 0;
1742 static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
1744 struct sk_buff *skb;
1745 struct sadb_msg *hdr;
1747 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1748 if (!skb)
1749 return -ENOBUFS;
1751 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1752 memcpy(hdr, ihdr, sizeof(struct sadb_msg));
1753 hdr->sadb_msg_errno = (uint8_t) 0;
1754 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1756 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ONE, sk,
1757 sock_net(sk));
1760 static int key_notify_sa_flush(const struct km_event *c)
1762 struct sk_buff *skb;
1763 struct sadb_msg *hdr;
1765 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1766 if (!skb)
1767 return -ENOBUFS;
1768 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1769 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
1770 hdr->sadb_msg_type = SADB_FLUSH;
1771 hdr->sadb_msg_seq = c->seq;
1772 hdr->sadb_msg_pid = c->portid;
1773 hdr->sadb_msg_version = PF_KEY_V2;
1774 hdr->sadb_msg_errno = (uint8_t) 0;
1775 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1776 hdr->sadb_msg_reserved = 0;
1778 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
1780 return 0;
1783 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1785 struct net *net = sock_net(sk);
1786 unsigned int proto;
1787 struct km_event c;
1788 int err, err2;
1790 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1791 if (proto == 0)
1792 return -EINVAL;
1794 err = xfrm_state_flush(net, proto, true);
1795 err2 = unicast_flush_resp(sk, hdr);
1796 if (err || err2) {
1797 if (err == -ESRCH) /* empty table - go quietly */
1798 err = 0;
1799 return err ? err : err2;
1802 c.data.proto = proto;
1803 c.seq = hdr->sadb_msg_seq;
1804 c.portid = hdr->sadb_msg_pid;
1805 c.event = XFRM_MSG_FLUSHSA;
1806 c.net = net;
1807 km_state_notify(NULL, &c);
1809 return 0;
1812 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1814 struct pfkey_sock *pfk = ptr;
1815 struct sk_buff *out_skb;
1816 struct sadb_msg *out_hdr;
1818 if (!pfkey_can_dump(&pfk->sk))
1819 return -ENOBUFS;
1821 out_skb = pfkey_xfrm_state2msg(x);
1822 if (IS_ERR(out_skb))
1823 return PTR_ERR(out_skb);
1825 out_hdr = (struct sadb_msg *) out_skb->data;
1826 out_hdr->sadb_msg_version = pfk->dump.msg_version;
1827 out_hdr->sadb_msg_type = SADB_DUMP;
1828 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1829 out_hdr->sadb_msg_errno = 0;
1830 out_hdr->sadb_msg_reserved = 0;
1831 out_hdr->sadb_msg_seq = count + 1;
1832 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
1834 if (pfk->dump.skb)
1835 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
1836 &pfk->sk, sock_net(&pfk->sk));
1837 pfk->dump.skb = out_skb;
1839 return 0;
1842 static int pfkey_dump_sa(struct pfkey_sock *pfk)
1844 struct net *net = sock_net(&pfk->sk);
1845 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
1848 static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1850 struct net *net = sock_net(&pfk->sk);
1852 xfrm_state_walk_done(&pfk->dump.u.state, net);
1855 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1857 u8 proto;
1858 struct xfrm_address_filter *filter = NULL;
1859 struct pfkey_sock *pfk = pfkey_sk(sk);
1861 mutex_lock(&pfk->dump_lock);
1862 if (pfk->dump.dump != NULL) {
1863 mutex_unlock(&pfk->dump_lock);
1864 return -EBUSY;
1867 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1868 if (proto == 0) {
1869 mutex_unlock(&pfk->dump_lock);
1870 return -EINVAL;
1873 if (ext_hdrs[SADB_X_EXT_FILTER - 1]) {
1874 struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1];
1876 if ((xfilter->sadb_x_filter_splen >=
1877 (sizeof(xfrm_address_t) << 3)) ||
1878 (xfilter->sadb_x_filter_dplen >=
1879 (sizeof(xfrm_address_t) << 3))) {
1880 mutex_unlock(&pfk->dump_lock);
1881 return -EINVAL;
1883 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
1884 if (filter == NULL) {
1885 mutex_unlock(&pfk->dump_lock);
1886 return -ENOMEM;
1889 memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr,
1890 sizeof(xfrm_address_t));
1891 memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr,
1892 sizeof(xfrm_address_t));
1893 filter->family = xfilter->sadb_x_filter_family;
1894 filter->splen = xfilter->sadb_x_filter_splen;
1895 filter->dplen = xfilter->sadb_x_filter_dplen;
1898 pfk->dump.msg_version = hdr->sadb_msg_version;
1899 pfk->dump.msg_portid = hdr->sadb_msg_pid;
1900 pfk->dump.dump = pfkey_dump_sa;
1901 pfk->dump.done = pfkey_dump_sa_done;
1902 xfrm_state_walk_init(&pfk->dump.u.state, proto, filter);
1903 mutex_unlock(&pfk->dump_lock);
1905 return pfkey_do_dump(pfk);
1908 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1910 struct pfkey_sock *pfk = pfkey_sk(sk);
1911 int satype = hdr->sadb_msg_satype;
1912 bool reset_errno = false;
1914 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1915 reset_errno = true;
1916 if (satype != 0 && satype != 1)
1917 return -EINVAL;
1918 pfk->promisc = satype;
1920 if (reset_errno && skb_cloned(skb))
1921 skb = skb_copy(skb, GFP_KERNEL);
1922 else
1923 skb = skb_clone(skb, GFP_KERNEL);
1925 if (reset_errno && skb) {
1926 struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
1927 new_hdr->sadb_msg_errno = 0;
1930 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk));
1931 return 0;
1934 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1936 int i;
1937 u32 reqid = *(u32*)ptr;
1939 for (i=0; i<xp->xfrm_nr; i++) {
1940 if (xp->xfrm_vec[i].reqid == reqid)
1941 return -EEXIST;
1943 return 0;
1946 static u32 gen_reqid(struct net *net)
1948 struct xfrm_policy_walk walk;
1949 u32 start;
1950 int rc;
1951 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1953 start = reqid;
1954 do {
1955 ++reqid;
1956 if (reqid == 0)
1957 reqid = IPSEC_MANUAL_REQID_MAX+1;
1958 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
1959 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
1960 xfrm_policy_walk_done(&walk, net);
1961 if (rc != -EEXIST)
1962 return reqid;
1963 } while (reqid != start);
1964 return 0;
1967 static int
1968 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1970 struct net *net = xp_net(xp);
1971 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1972 int mode;
1974 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1975 return -ELOOP;
1977 if (rq->sadb_x_ipsecrequest_mode == 0)
1978 return -EINVAL;
1979 if (!xfrm_id_proto_valid(rq->sadb_x_ipsecrequest_proto))
1980 return -EINVAL;
1982 t->id.proto = rq->sadb_x_ipsecrequest_proto;
1983 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1984 return -EINVAL;
1985 t->mode = mode;
1986 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1987 t->optional = 1;
1988 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1989 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1990 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1991 t->reqid = 0;
1992 if (!t->reqid && !(t->reqid = gen_reqid(net)))
1993 return -ENOBUFS;
1996 /* addresses present only in tunnel mode */
1997 if (t->mode == XFRM_MODE_TUNNEL) {
1998 int err;
2000 err = parse_sockaddr_pair(
2001 (struct sockaddr *)(rq + 1),
2002 rq->sadb_x_ipsecrequest_len - sizeof(*rq),
2003 &t->saddr, &t->id.daddr, &t->encap_family);
2004 if (err)
2005 return err;
2006 } else
2007 t->encap_family = xp->family;
2009 /* No way to set this via kame pfkey */
2010 t->allalgs = 1;
2011 xp->xfrm_nr++;
2012 return 0;
2015 static int
2016 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
2018 int err;
2019 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
2020 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
2022 if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
2023 return -EINVAL;
2025 while (len >= sizeof(*rq)) {
2026 if (len < rq->sadb_x_ipsecrequest_len ||
2027 rq->sadb_x_ipsecrequest_len < sizeof(*rq))
2028 return -EINVAL;
2030 if ((err = parse_ipsecrequest(xp, rq)) < 0)
2031 return err;
2032 len -= rq->sadb_x_ipsecrequest_len;
2033 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
2035 return 0;
2038 static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
2040 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
2042 if (xfrm_ctx) {
2043 int len = sizeof(struct sadb_x_sec_ctx);
2044 len += xfrm_ctx->ctx_len;
2045 return PFKEY_ALIGN8(len);
2047 return 0;
2050 static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
2052 const struct xfrm_tmpl *t;
2053 int sockaddr_size = pfkey_sockaddr_size(xp->family);
2054 int socklen = 0;
2055 int i;
2057 for (i=0; i<xp->xfrm_nr; i++) {
2058 t = xp->xfrm_vec + i;
2059 socklen += pfkey_sockaddr_len(t->encap_family);
2062 return sizeof(struct sadb_msg) +
2063 (sizeof(struct sadb_lifetime) * 3) +
2064 (sizeof(struct sadb_address) * 2) +
2065 (sockaddr_size * 2) +
2066 sizeof(struct sadb_x_policy) +
2067 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
2068 (socklen * 2) +
2069 pfkey_xfrm_policy2sec_ctx_size(xp);
2072 static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
2074 struct sk_buff *skb;
2075 int size;
2077 size = pfkey_xfrm_policy2msg_size(xp);
2079 skb = alloc_skb(size + 16, GFP_ATOMIC);
2080 if (skb == NULL)
2081 return ERR_PTR(-ENOBUFS);
2083 return skb;
2086 static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
2088 struct sadb_msg *hdr;
2089 struct sadb_address *addr;
2090 struct sadb_lifetime *lifetime;
2091 struct sadb_x_policy *pol;
2092 struct sadb_x_sec_ctx *sec_ctx;
2093 struct xfrm_sec_ctx *xfrm_ctx;
2094 int i;
2095 int size;
2096 int sockaddr_size = pfkey_sockaddr_size(xp->family);
2097 int socklen = pfkey_sockaddr_len(xp->family);
2099 size = pfkey_xfrm_policy2msg_size(xp);
2101 /* call should fill header later */
2102 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2103 memset(hdr, 0, size); /* XXX do we need this ? */
2105 /* src address */
2106 addr = (struct sadb_address*) skb_put(skb,
2107 sizeof(struct sadb_address)+sockaddr_size);
2108 addr->sadb_address_len =
2109 (sizeof(struct sadb_address)+sockaddr_size)/
2110 sizeof(uint64_t);
2111 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2112 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2113 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2114 addr->sadb_address_reserved = 0;
2115 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2116 xp->selector.sport,
2117 (struct sockaddr *) (addr + 1),
2118 xp->family))
2119 BUG();
2121 /* dst address */
2122 addr = (struct sadb_address*) skb_put(skb,
2123 sizeof(struct sadb_address)+sockaddr_size);
2124 addr->sadb_address_len =
2125 (sizeof(struct sadb_address)+sockaddr_size)/
2126 sizeof(uint64_t);
2127 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2128 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2129 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
2130 addr->sadb_address_reserved = 0;
2132 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2133 (struct sockaddr *) (addr + 1),
2134 xp->family);
2136 /* hard time */
2137 lifetime = (struct sadb_lifetime *) skb_put(skb,
2138 sizeof(struct sadb_lifetime));
2139 lifetime->sadb_lifetime_len =
2140 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2141 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2142 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2143 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2144 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2145 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2146 /* soft time */
2147 lifetime = (struct sadb_lifetime *) skb_put(skb,
2148 sizeof(struct sadb_lifetime));
2149 lifetime->sadb_lifetime_len =
2150 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2151 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2152 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2153 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2154 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2155 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2156 /* current time */
2157 lifetime = (struct sadb_lifetime *) skb_put(skb,
2158 sizeof(struct sadb_lifetime));
2159 lifetime->sadb_lifetime_len =
2160 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2161 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2162 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2163 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2164 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2165 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2167 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2168 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2169 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2170 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2171 if (xp->action == XFRM_POLICY_ALLOW) {
2172 if (xp->xfrm_nr)
2173 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2174 else
2175 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2177 pol->sadb_x_policy_dir = dir+1;
2178 pol->sadb_x_policy_reserved = 0;
2179 pol->sadb_x_policy_id = xp->index;
2180 pol->sadb_x_policy_priority = xp->priority;
2182 for (i=0; i<xp->xfrm_nr; i++) {
2183 const struct xfrm_tmpl *t = xp->xfrm_vec + i;
2184 struct sadb_x_ipsecrequest *rq;
2185 int req_size;
2186 int mode;
2188 req_size = sizeof(struct sadb_x_ipsecrequest);
2189 if (t->mode == XFRM_MODE_TUNNEL) {
2190 socklen = pfkey_sockaddr_len(t->encap_family);
2191 req_size += socklen * 2;
2192 } else {
2193 size -= 2*socklen;
2195 rq = (void*)skb_put(skb, req_size);
2196 pol->sadb_x_policy_len += req_size/8;
2197 memset(rq, 0, sizeof(*rq));
2198 rq->sadb_x_ipsecrequest_len = req_size;
2199 rq->sadb_x_ipsecrequest_proto = t->id.proto;
2200 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2201 return -EINVAL;
2202 rq->sadb_x_ipsecrequest_mode = mode;
2203 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2204 if (t->reqid)
2205 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2206 if (t->optional)
2207 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2208 rq->sadb_x_ipsecrequest_reqid = t->reqid;
2210 if (t->mode == XFRM_MODE_TUNNEL) {
2211 u8 *sa = (void *)(rq + 1);
2212 pfkey_sockaddr_fill(&t->saddr, 0,
2213 (struct sockaddr *)sa,
2214 t->encap_family);
2215 pfkey_sockaddr_fill(&t->id.daddr, 0,
2216 (struct sockaddr *) (sa + socklen),
2217 t->encap_family);
2221 /* security context */
2222 if ((xfrm_ctx = xp->security)) {
2223 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2225 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2226 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2227 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2228 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2229 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2230 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2231 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2232 xfrm_ctx->ctx_len);
2235 hdr->sadb_msg_len = size / sizeof(uint64_t);
2236 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
2238 return 0;
2241 static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2243 struct sk_buff *out_skb;
2244 struct sadb_msg *out_hdr;
2245 int err;
2247 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2248 if (IS_ERR(out_skb))
2249 return PTR_ERR(out_skb);
2251 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2252 if (err < 0)
2253 return err;
2255 out_hdr = (struct sadb_msg *) out_skb->data;
2256 out_hdr->sadb_msg_version = PF_KEY_V2;
2258 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
2259 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2260 else
2261 out_hdr->sadb_msg_type = event2poltype(c->event);
2262 out_hdr->sadb_msg_errno = 0;
2263 out_hdr->sadb_msg_seq = c->seq;
2264 out_hdr->sadb_msg_pid = c->portid;
2265 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp));
2266 return 0;
2270 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2272 struct net *net = sock_net(sk);
2273 int err = 0;
2274 struct sadb_lifetime *lifetime;
2275 struct sadb_address *sa;
2276 struct sadb_x_policy *pol;
2277 struct xfrm_policy *xp;
2278 struct km_event c;
2279 struct sadb_x_sec_ctx *sec_ctx;
2281 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2282 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2283 !ext_hdrs[SADB_X_EXT_POLICY-1])
2284 return -EINVAL;
2286 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2287 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2288 return -EINVAL;
2289 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2290 return -EINVAL;
2292 xp = xfrm_policy_alloc(net, GFP_KERNEL);
2293 if (xp == NULL)
2294 return -ENOBUFS;
2296 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2297 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2298 xp->priority = pol->sadb_x_policy_priority;
2300 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
2301 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2302 xp->selector.family = xp->family;
2303 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2304 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2305 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2306 if (xp->selector.sport)
2307 xp->selector.sport_mask = htons(0xffff);
2309 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
2310 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2311 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2313 /* Amusing, we set this twice. KAME apps appear to set same value
2314 * in both addresses.
2316 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2318 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2319 if (xp->selector.dport)
2320 xp->selector.dport_mask = htons(0xffff);
2322 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
2323 if (sec_ctx != NULL) {
2324 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
2326 if (!uctx) {
2327 err = -ENOBUFS;
2328 goto out;
2331 err = security_xfrm_policy_alloc(&xp->security, uctx, GFP_KERNEL);
2332 kfree(uctx);
2334 if (err)
2335 goto out;
2338 xp->lft.soft_byte_limit = XFRM_INF;
2339 xp->lft.hard_byte_limit = XFRM_INF;
2340 xp->lft.soft_packet_limit = XFRM_INF;
2341 xp->lft.hard_packet_limit = XFRM_INF;
2342 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2343 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2344 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2345 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2346 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2348 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2349 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2350 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2351 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2352 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2354 xp->xfrm_nr = 0;
2355 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2356 (err = parse_ipsecrequests(xp, pol)) < 0)
2357 goto out;
2359 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2360 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2362 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
2364 if (err)
2365 goto out;
2367 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2368 c.event = XFRM_MSG_UPDPOLICY;
2369 else
2370 c.event = XFRM_MSG_NEWPOLICY;
2372 c.seq = hdr->sadb_msg_seq;
2373 c.portid = hdr->sadb_msg_pid;
2375 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2376 xfrm_pol_put(xp);
2377 return 0;
2379 out:
2380 xp->walk.dead = 1;
2381 xfrm_policy_destroy(xp);
2382 return err;
2385 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2387 struct net *net = sock_net(sk);
2388 int err;
2389 struct sadb_address *sa;
2390 struct sadb_x_policy *pol;
2391 struct xfrm_policy *xp;
2392 struct xfrm_selector sel;
2393 struct km_event c;
2394 struct sadb_x_sec_ctx *sec_ctx;
2395 struct xfrm_sec_ctx *pol_ctx = NULL;
2397 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2398 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2399 !ext_hdrs[SADB_X_EXT_POLICY-1])
2400 return -EINVAL;
2402 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2403 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2404 return -EINVAL;
2406 memset(&sel, 0, sizeof(sel));
2408 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
2409 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2410 sel.prefixlen_s = sa->sadb_address_prefixlen;
2411 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2412 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2413 if (sel.sport)
2414 sel.sport_mask = htons(0xffff);
2416 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
2417 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2418 sel.prefixlen_d = sa->sadb_address_prefixlen;
2419 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2420 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2421 if (sel.dport)
2422 sel.dport_mask = htons(0xffff);
2424 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
2425 if (sec_ctx != NULL) {
2426 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
2428 if (!uctx)
2429 return -ENOMEM;
2431 err = security_xfrm_policy_alloc(&pol_ctx, uctx, GFP_KERNEL);
2432 kfree(uctx);
2433 if (err)
2434 return err;
2437 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
2438 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2439 1, &err);
2440 security_xfrm_policy_free(pol_ctx);
2441 if (xp == NULL)
2442 return -ENOENT;
2444 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2446 if (err)
2447 goto out;
2449 c.seq = hdr->sadb_msg_seq;
2450 c.portid = hdr->sadb_msg_pid;
2451 c.data.byid = 0;
2452 c.event = XFRM_MSG_DELPOLICY;
2453 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2455 out:
2456 xfrm_pol_put(xp);
2457 if (err == 0)
2458 xfrm_garbage_collect(net);
2459 return err;
2462 static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, const struct sadb_msg *hdr, int dir)
2464 int err;
2465 struct sk_buff *out_skb;
2466 struct sadb_msg *out_hdr;
2467 err = 0;
2469 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2470 if (IS_ERR(out_skb)) {
2471 err = PTR_ERR(out_skb);
2472 goto out;
2474 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2475 if (err < 0) {
2476 kfree_skb(out_skb);
2477 goto out;
2480 out_hdr = (struct sadb_msg *) out_skb->data;
2481 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2482 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2483 out_hdr->sadb_msg_satype = 0;
2484 out_hdr->sadb_msg_errno = 0;
2485 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2486 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2487 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp));
2488 err = 0;
2490 out:
2491 return err;
2494 static int pfkey_sockaddr_pair_size(sa_family_t family)
2496 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
2499 static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
2500 xfrm_address_t *saddr, xfrm_address_t *daddr,
2501 u16 *family)
2503 int af, socklen;
2505 if (ext_len < 2 || ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
2506 return -EINVAL;
2508 af = pfkey_sockaddr_extract(sa, saddr);
2509 if (!af)
2510 return -EINVAL;
2512 socklen = pfkey_sockaddr_len(af);
2513 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
2514 daddr) != af)
2515 return -EINVAL;
2517 *family = af;
2518 return 0;
2521 #ifdef CONFIG_NET_KEY_MIGRATE
2522 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2523 struct xfrm_migrate *m)
2525 int err;
2526 struct sadb_x_ipsecrequest *rq2;
2527 int mode;
2529 if (len < sizeof(*rq1) ||
2530 len < rq1->sadb_x_ipsecrequest_len ||
2531 rq1->sadb_x_ipsecrequest_len < sizeof(*rq1))
2532 return -EINVAL;
2534 /* old endoints */
2535 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
2536 rq1->sadb_x_ipsecrequest_len - sizeof(*rq1),
2537 &m->old_saddr, &m->old_daddr,
2538 &m->old_family);
2539 if (err)
2540 return err;
2542 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2543 len -= rq1->sadb_x_ipsecrequest_len;
2545 if (len <= sizeof(*rq2) ||
2546 len < rq2->sadb_x_ipsecrequest_len ||
2547 rq2->sadb_x_ipsecrequest_len < sizeof(*rq2))
2548 return -EINVAL;
2550 /* new endpoints */
2551 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
2552 rq2->sadb_x_ipsecrequest_len - sizeof(*rq2),
2553 &m->new_saddr, &m->new_daddr,
2554 &m->new_family);
2555 if (err)
2556 return err;
2558 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2559 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2560 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2561 return -EINVAL;
2563 m->proto = rq1->sadb_x_ipsecrequest_proto;
2564 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2565 return -EINVAL;
2566 m->mode = mode;
2567 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2569 return ((int)(rq1->sadb_x_ipsecrequest_len +
2570 rq2->sadb_x_ipsecrequest_len));
2573 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2574 const struct sadb_msg *hdr, void * const *ext_hdrs)
2576 int i, len, ret, err = -EINVAL;
2577 u8 dir;
2578 struct sadb_address *sa;
2579 struct sadb_x_kmaddress *kma;
2580 struct sadb_x_policy *pol;
2581 struct sadb_x_ipsecrequest *rq;
2582 struct xfrm_selector sel;
2583 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2584 struct xfrm_kmaddress k;
2585 struct net *net = sock_net(sk);
2587 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2588 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2589 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2590 err = -EINVAL;
2591 goto out;
2594 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
2595 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2597 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2598 err = -EINVAL;
2599 goto out;
2602 if (kma) {
2603 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2604 k.reserved = kma->sadb_x_kmaddress_reserved;
2605 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2606 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2607 &k.local, &k.remote, &k.family);
2608 if (ret < 0) {
2609 err = ret;
2610 goto out;
2614 dir = pol->sadb_x_policy_dir - 1;
2615 memset(&sel, 0, sizeof(sel));
2617 /* set source address info of selector */
2618 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2619 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2620 sel.prefixlen_s = sa->sadb_address_prefixlen;
2621 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2622 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2623 if (sel.sport)
2624 sel.sport_mask = htons(0xffff);
2626 /* set destination address info of selector */
2627 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
2628 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2629 sel.prefixlen_d = sa->sadb_address_prefixlen;
2630 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2631 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2632 if (sel.dport)
2633 sel.dport_mask = htons(0xffff);
2635 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2637 /* extract ipsecrequests */
2638 i = 0;
2639 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2641 while (len > 0 && i < XFRM_MAX_DEPTH) {
2642 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2643 if (ret < 0) {
2644 err = ret;
2645 goto out;
2646 } else {
2647 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2648 len -= ret;
2649 i++;
2653 if (!i || len > 0) {
2654 err = -EINVAL;
2655 goto out;
2658 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
2659 kma ? &k : NULL, net);
2661 out:
2662 return err;
2664 #else
2665 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2666 const struct sadb_msg *hdr, void * const *ext_hdrs)
2668 return -ENOPROTOOPT;
2670 #endif
2673 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2675 struct net *net = sock_net(sk);
2676 unsigned int dir;
2677 int err = 0, delete;
2678 struct sadb_x_policy *pol;
2679 struct xfrm_policy *xp;
2680 struct km_event c;
2682 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2683 return -EINVAL;
2685 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2686 if (dir >= XFRM_POLICY_MAX)
2687 return -EINVAL;
2689 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2690 xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
2691 dir, pol->sadb_x_policy_id, delete, &err);
2692 if (xp == NULL)
2693 return -ENOENT;
2695 if (delete) {
2696 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2698 if (err)
2699 goto out;
2700 c.seq = hdr->sadb_msg_seq;
2701 c.portid = hdr->sadb_msg_pid;
2702 c.data.byid = 1;
2703 c.event = XFRM_MSG_DELPOLICY;
2704 km_policy_notify(xp, dir, &c);
2705 } else {
2706 err = key_pol_get_resp(sk, xp, hdr, dir);
2709 out:
2710 xfrm_pol_put(xp);
2711 if (delete && err == 0)
2712 xfrm_garbage_collect(net);
2713 return err;
2716 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2718 struct pfkey_sock *pfk = ptr;
2719 struct sk_buff *out_skb;
2720 struct sadb_msg *out_hdr;
2721 int err;
2723 if (!pfkey_can_dump(&pfk->sk))
2724 return -ENOBUFS;
2726 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2727 if (IS_ERR(out_skb))
2728 return PTR_ERR(out_skb);
2730 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2731 if (err < 0) {
2732 kfree_skb(out_skb);
2733 return err;
2736 out_hdr = (struct sadb_msg *) out_skb->data;
2737 out_hdr->sadb_msg_version = pfk->dump.msg_version;
2738 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2739 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2740 out_hdr->sadb_msg_errno = 0;
2741 out_hdr->sadb_msg_seq = count + 1;
2742 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
2744 if (pfk->dump.skb)
2745 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
2746 &pfk->sk, sock_net(&pfk->sk));
2747 pfk->dump.skb = out_skb;
2749 return 0;
2752 static int pfkey_dump_sp(struct pfkey_sock *pfk)
2754 struct net *net = sock_net(&pfk->sk);
2755 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
2758 static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2760 struct net *net = sock_net((struct sock *)pfk);
2762 xfrm_policy_walk_done(&pfk->dump.u.policy, net);
2765 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2767 struct pfkey_sock *pfk = pfkey_sk(sk);
2769 mutex_lock(&pfk->dump_lock);
2770 if (pfk->dump.dump != NULL) {
2771 mutex_unlock(&pfk->dump_lock);
2772 return -EBUSY;
2775 pfk->dump.msg_version = hdr->sadb_msg_version;
2776 pfk->dump.msg_portid = hdr->sadb_msg_pid;
2777 pfk->dump.dump = pfkey_dump_sp;
2778 pfk->dump.done = pfkey_dump_sp_done;
2779 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2780 mutex_unlock(&pfk->dump_lock);
2782 return pfkey_do_dump(pfk);
2785 static int key_notify_policy_flush(const struct km_event *c)
2787 struct sk_buff *skb_out;
2788 struct sadb_msg *hdr;
2790 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
2791 if (!skb_out)
2792 return -ENOBUFS;
2793 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2794 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
2795 hdr->sadb_msg_seq = c->seq;
2796 hdr->sadb_msg_pid = c->portid;
2797 hdr->sadb_msg_version = PF_KEY_V2;
2798 hdr->sadb_msg_errno = (uint8_t) 0;
2799 hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2800 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2801 hdr->sadb_msg_reserved = 0;
2802 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
2803 return 0;
2807 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2809 struct net *net = sock_net(sk);
2810 struct km_event c;
2811 int err, err2;
2813 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true);
2814 err2 = unicast_flush_resp(sk, hdr);
2815 if (err || err2) {
2816 if (err == -ESRCH) /* empty table - old silent behavior */
2817 return 0;
2818 return err;
2821 c.data.type = XFRM_POLICY_TYPE_MAIN;
2822 c.event = XFRM_MSG_FLUSHPOLICY;
2823 c.portid = hdr->sadb_msg_pid;
2824 c.seq = hdr->sadb_msg_seq;
2825 c.net = net;
2826 km_policy_notify(NULL, 0, &c);
2828 return 0;
2831 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2832 const struct sadb_msg *hdr, void * const *ext_hdrs);
2833 static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2834 [SADB_RESERVED] = pfkey_reserved,
2835 [SADB_GETSPI] = pfkey_getspi,
2836 [SADB_UPDATE] = pfkey_add,
2837 [SADB_ADD] = pfkey_add,
2838 [SADB_DELETE] = pfkey_delete,
2839 [SADB_GET] = pfkey_get,
2840 [SADB_ACQUIRE] = pfkey_acquire,
2841 [SADB_REGISTER] = pfkey_register,
2842 [SADB_EXPIRE] = NULL,
2843 [SADB_FLUSH] = pfkey_flush,
2844 [SADB_DUMP] = pfkey_dump,
2845 [SADB_X_PROMISC] = pfkey_promisc,
2846 [SADB_X_PCHANGE] = NULL,
2847 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2848 [SADB_X_SPDADD] = pfkey_spdadd,
2849 [SADB_X_SPDDELETE] = pfkey_spddelete,
2850 [SADB_X_SPDGET] = pfkey_spdget,
2851 [SADB_X_SPDACQUIRE] = NULL,
2852 [SADB_X_SPDDUMP] = pfkey_spddump,
2853 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2854 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2855 [SADB_X_SPDDELETE2] = pfkey_spdget,
2856 [SADB_X_MIGRATE] = pfkey_migrate,
2859 static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
2861 void *ext_hdrs[SADB_EXT_MAX];
2862 int err;
2864 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2865 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
2867 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2868 err = parse_exthdrs(skb, hdr, ext_hdrs);
2869 if (!err) {
2870 err = -EOPNOTSUPP;
2871 if (pfkey_funcs[hdr->sadb_msg_type])
2872 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2874 return err;
2877 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2879 struct sadb_msg *hdr = NULL;
2881 if (skb->len < sizeof(*hdr)) {
2882 *errp = -EMSGSIZE;
2883 } else {
2884 hdr = (struct sadb_msg *) skb->data;
2885 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2886 hdr->sadb_msg_reserved != 0 ||
2887 (hdr->sadb_msg_type <= SADB_RESERVED ||
2888 hdr->sadb_msg_type > SADB_MAX)) {
2889 hdr = NULL;
2890 *errp = -EINVAL;
2891 } else if (hdr->sadb_msg_len != (skb->len /
2892 sizeof(uint64_t)) ||
2893 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2894 sizeof(uint64_t))) {
2895 hdr = NULL;
2896 *errp = -EMSGSIZE;
2897 } else {
2898 *errp = 0;
2901 return hdr;
2904 static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
2905 const struct xfrm_algo_desc *d)
2907 unsigned int id = d->desc.sadb_alg_id;
2909 if (id >= sizeof(t->aalgos) * 8)
2910 return 0;
2912 return (t->aalgos >> id) & 1;
2915 static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
2916 const struct xfrm_algo_desc *d)
2918 unsigned int id = d->desc.sadb_alg_id;
2920 if (id >= sizeof(t->ealgos) * 8)
2921 return 0;
2923 return (t->ealgos >> id) & 1;
2926 static int count_ah_combs(const struct xfrm_tmpl *t)
2928 int i, sz = 0;
2930 for (i = 0; ; i++) {
2931 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2932 if (!aalg)
2933 break;
2934 if (!aalg->pfkey_supported)
2935 continue;
2936 if (aalg_tmpl_set(t, aalg) && aalg->available)
2937 sz += sizeof(struct sadb_comb);
2939 return sz + sizeof(struct sadb_prop);
2942 static int count_esp_combs(const struct xfrm_tmpl *t)
2944 int i, k, sz = 0;
2946 for (i = 0; ; i++) {
2947 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2948 if (!ealg)
2949 break;
2951 if (!ealg->pfkey_supported)
2952 continue;
2954 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2955 continue;
2957 for (k = 1; ; k++) {
2958 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2959 if (!aalg)
2960 break;
2962 if (!aalg->pfkey_supported)
2963 continue;
2965 if (aalg_tmpl_set(t, aalg) && aalg->available)
2966 sz += sizeof(struct sadb_comb);
2969 return sz + sizeof(struct sadb_prop);
2972 static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
2974 struct sadb_prop *p;
2975 int i;
2977 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2978 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2979 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2980 p->sadb_prop_replay = 32;
2981 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2983 for (i = 0; ; i++) {
2984 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2985 if (!aalg)
2986 break;
2988 if (!aalg->pfkey_supported)
2989 continue;
2991 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2992 struct sadb_comb *c;
2993 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2994 memset(c, 0, sizeof(*c));
2995 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2996 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2997 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2998 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2999 c->sadb_comb_hard_addtime = 24*60*60;
3000 c->sadb_comb_soft_addtime = 20*60*60;
3001 c->sadb_comb_hard_usetime = 8*60*60;
3002 c->sadb_comb_soft_usetime = 7*60*60;
3007 static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
3009 struct sadb_prop *p;
3010 int i, k;
3012 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
3013 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
3014 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
3015 p->sadb_prop_replay = 32;
3016 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
3018 for (i=0; ; i++) {
3019 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
3020 if (!ealg)
3021 break;
3023 if (!ealg->pfkey_supported)
3024 continue;
3026 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
3027 continue;
3029 for (k = 1; ; k++) {
3030 struct sadb_comb *c;
3031 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
3032 if (!aalg)
3033 break;
3034 if (!aalg->pfkey_supported)
3035 continue;
3036 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
3037 continue;
3038 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
3039 memset(c, 0, sizeof(*c));
3040 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
3041 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
3042 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
3043 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
3044 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
3045 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
3046 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
3047 c->sadb_comb_hard_addtime = 24*60*60;
3048 c->sadb_comb_soft_addtime = 20*60*60;
3049 c->sadb_comb_hard_usetime = 8*60*60;
3050 c->sadb_comb_soft_usetime = 7*60*60;
3055 static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
3057 return 0;
3060 static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
3062 struct sk_buff *out_skb;
3063 struct sadb_msg *out_hdr;
3064 int hard;
3065 int hsc;
3067 hard = c->data.hard;
3068 if (hard)
3069 hsc = 2;
3070 else
3071 hsc = 1;
3073 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
3074 if (IS_ERR(out_skb))
3075 return PTR_ERR(out_skb);
3077 out_hdr = (struct sadb_msg *) out_skb->data;
3078 out_hdr->sadb_msg_version = PF_KEY_V2;
3079 out_hdr->sadb_msg_type = SADB_EXPIRE;
3080 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3081 out_hdr->sadb_msg_errno = 0;
3082 out_hdr->sadb_msg_reserved = 0;
3083 out_hdr->sadb_msg_seq = 0;
3084 out_hdr->sadb_msg_pid = 0;
3086 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3087 xs_net(x));
3088 return 0;
3091 static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
3093 struct net *net = x ? xs_net(x) : c->net;
3094 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3096 if (atomic_read(&net_pfkey->socks_nr) == 0)
3097 return 0;
3099 switch (c->event) {
3100 case XFRM_MSG_EXPIRE:
3101 return key_notify_sa_expire(x, c);
3102 case XFRM_MSG_DELSA:
3103 case XFRM_MSG_NEWSA:
3104 case XFRM_MSG_UPDSA:
3105 return key_notify_sa(x, c);
3106 case XFRM_MSG_FLUSHSA:
3107 return key_notify_sa_flush(c);
3108 case XFRM_MSG_NEWAE: /* not yet supported */
3109 break;
3110 default:
3111 pr_err("pfkey: Unknown SA event %d\n", c->event);
3112 break;
3115 return 0;
3118 static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3120 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3121 return 0;
3123 switch (c->event) {
3124 case XFRM_MSG_POLEXPIRE:
3125 return key_notify_policy_expire(xp, c);
3126 case XFRM_MSG_DELPOLICY:
3127 case XFRM_MSG_NEWPOLICY:
3128 case XFRM_MSG_UPDPOLICY:
3129 return key_notify_policy(xp, dir, c);
3130 case XFRM_MSG_FLUSHPOLICY:
3131 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3132 break;
3133 return key_notify_policy_flush(c);
3134 default:
3135 pr_err("pfkey: Unknown policy event %d\n", c->event);
3136 break;
3139 return 0;
3142 static u32 get_acqseq(void)
3144 u32 res;
3145 static atomic_t acqseq;
3147 do {
3148 res = atomic_inc_return(&acqseq);
3149 } while (!res);
3150 return res;
3153 static bool pfkey_is_alive(const struct km_event *c)
3155 struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id);
3156 struct sock *sk;
3157 bool is_alive = false;
3159 rcu_read_lock();
3160 sk_for_each_rcu(sk, &net_pfkey->table) {
3161 if (pfkey_sk(sk)->registered) {
3162 is_alive = true;
3163 break;
3166 rcu_read_unlock();
3168 return is_alive;
3171 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
3173 struct sk_buff *skb;
3174 struct sadb_msg *hdr;
3175 struct sadb_address *addr;
3176 struct sadb_x_policy *pol;
3177 int sockaddr_size;
3178 int size;
3179 struct sadb_x_sec_ctx *sec_ctx;
3180 struct xfrm_sec_ctx *xfrm_ctx;
3181 int ctx_size = 0;
3183 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3184 if (!sockaddr_size)
3185 return -EINVAL;
3187 size = sizeof(struct sadb_msg) +
3188 (sizeof(struct sadb_address) * 2) +
3189 (sockaddr_size * 2) +
3190 sizeof(struct sadb_x_policy);
3192 if (x->id.proto == IPPROTO_AH)
3193 size += count_ah_combs(t);
3194 else if (x->id.proto == IPPROTO_ESP)
3195 size += count_esp_combs(t);
3197 if ((xfrm_ctx = x->security)) {
3198 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3199 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3202 skb = alloc_skb(size + 16, GFP_ATOMIC);
3203 if (skb == NULL)
3204 return -ENOMEM;
3206 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3207 hdr->sadb_msg_version = PF_KEY_V2;
3208 hdr->sadb_msg_type = SADB_ACQUIRE;
3209 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3210 hdr->sadb_msg_len = size / sizeof(uint64_t);
3211 hdr->sadb_msg_errno = 0;
3212 hdr->sadb_msg_reserved = 0;
3213 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3214 hdr->sadb_msg_pid = 0;
3216 /* src address */
3217 addr = (struct sadb_address*) skb_put(skb,
3218 sizeof(struct sadb_address)+sockaddr_size);
3219 addr->sadb_address_len =
3220 (sizeof(struct sadb_address)+sockaddr_size)/
3221 sizeof(uint64_t);
3222 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3223 addr->sadb_address_proto = 0;
3224 addr->sadb_address_reserved = 0;
3225 addr->sadb_address_prefixlen =
3226 pfkey_sockaddr_fill(&x->props.saddr, 0,
3227 (struct sockaddr *) (addr + 1),
3228 x->props.family);
3229 if (!addr->sadb_address_prefixlen)
3230 BUG();
3232 /* dst address */
3233 addr = (struct sadb_address*) skb_put(skb,
3234 sizeof(struct sadb_address)+sockaddr_size);
3235 addr->sadb_address_len =
3236 (sizeof(struct sadb_address)+sockaddr_size)/
3237 sizeof(uint64_t);
3238 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3239 addr->sadb_address_proto = 0;
3240 addr->sadb_address_reserved = 0;
3241 addr->sadb_address_prefixlen =
3242 pfkey_sockaddr_fill(&x->id.daddr, 0,
3243 (struct sockaddr *) (addr + 1),
3244 x->props.family);
3245 if (!addr->sadb_address_prefixlen)
3246 BUG();
3248 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3249 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3250 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3251 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3252 pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1;
3253 pol->sadb_x_policy_reserved = 0;
3254 pol->sadb_x_policy_id = xp->index;
3255 pol->sadb_x_policy_priority = xp->priority;
3257 /* Set sadb_comb's. */
3258 if (x->id.proto == IPPROTO_AH)
3259 dump_ah_combs(skb, t);
3260 else if (x->id.proto == IPPROTO_ESP)
3261 dump_esp_combs(skb, t);
3263 /* security context */
3264 if (xfrm_ctx) {
3265 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3266 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3267 sec_ctx->sadb_x_sec_len =
3268 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3269 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3270 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3271 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3272 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3273 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3274 xfrm_ctx->ctx_len);
3277 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3278 xs_net(x));
3281 static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
3282 u8 *data, int len, int *dir)
3284 struct net *net = sock_net(sk);
3285 struct xfrm_policy *xp;
3286 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
3287 struct sadb_x_sec_ctx *sec_ctx;
3289 switch (sk->sk_family) {
3290 case AF_INET:
3291 if (opt != IP_IPSEC_POLICY) {
3292 *dir = -EOPNOTSUPP;
3293 return NULL;
3295 break;
3296 #if IS_ENABLED(CONFIG_IPV6)
3297 case AF_INET6:
3298 if (opt != IPV6_IPSEC_POLICY) {
3299 *dir = -EOPNOTSUPP;
3300 return NULL;
3302 break;
3303 #endif
3304 default:
3305 *dir = -EINVAL;
3306 return NULL;
3309 *dir = -EINVAL;
3311 if (len < sizeof(struct sadb_x_policy) ||
3312 pol->sadb_x_policy_len*8 > len ||
3313 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3314 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3315 return NULL;
3317 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3318 if (xp == NULL) {
3319 *dir = -ENOBUFS;
3320 return NULL;
3323 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3324 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3326 xp->lft.soft_byte_limit = XFRM_INF;
3327 xp->lft.hard_byte_limit = XFRM_INF;
3328 xp->lft.soft_packet_limit = XFRM_INF;
3329 xp->lft.hard_packet_limit = XFRM_INF;
3330 xp->family = sk->sk_family;
3332 xp->xfrm_nr = 0;
3333 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3334 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3335 goto out;
3337 /* security context too */
3338 if (len >= (pol->sadb_x_policy_len*8 +
3339 sizeof(struct sadb_x_sec_ctx))) {
3340 char *p = (char *)pol;
3341 struct xfrm_user_sec_ctx *uctx;
3343 p += pol->sadb_x_policy_len*8;
3344 sec_ctx = (struct sadb_x_sec_ctx *)p;
3345 if (len < pol->sadb_x_policy_len*8 +
3346 sec_ctx->sadb_x_sec_len*8) {
3347 *dir = -EINVAL;
3348 goto out;
3350 if ((*dir = verify_sec_ctx_len(p)))
3351 goto out;
3352 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_ATOMIC);
3353 *dir = security_xfrm_policy_alloc(&xp->security, uctx, GFP_ATOMIC);
3354 kfree(uctx);
3356 if (*dir)
3357 goto out;
3360 *dir = pol->sadb_x_policy_dir-1;
3361 return xp;
3363 out:
3364 xp->walk.dead = 1;
3365 xfrm_policy_destroy(xp);
3366 return NULL;
3369 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
3371 struct sk_buff *skb;
3372 struct sadb_msg *hdr;
3373 struct sadb_sa *sa;
3374 struct sadb_address *addr;
3375 struct sadb_x_nat_t_port *n_port;
3376 int sockaddr_size;
3377 int size;
3378 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3379 struct xfrm_encap_tmpl *natt = NULL;
3381 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3382 if (!sockaddr_size)
3383 return -EINVAL;
3385 if (!satype)
3386 return -EINVAL;
3388 if (!x->encap)
3389 return -EINVAL;
3391 natt = x->encap;
3393 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3395 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3396 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3399 size = sizeof(struct sadb_msg) +
3400 sizeof(struct sadb_sa) +
3401 (sizeof(struct sadb_address) * 2) +
3402 (sockaddr_size * 2) +
3403 (sizeof(struct sadb_x_nat_t_port) * 2);
3405 skb = alloc_skb(size + 16, GFP_ATOMIC);
3406 if (skb == NULL)
3407 return -ENOMEM;
3409 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3410 hdr->sadb_msg_version = PF_KEY_V2;
3411 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3412 hdr->sadb_msg_satype = satype;
3413 hdr->sadb_msg_len = size / sizeof(uint64_t);
3414 hdr->sadb_msg_errno = 0;
3415 hdr->sadb_msg_reserved = 0;
3416 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3417 hdr->sadb_msg_pid = 0;
3419 /* SA */
3420 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3421 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3422 sa->sadb_sa_exttype = SADB_EXT_SA;
3423 sa->sadb_sa_spi = x->id.spi;
3424 sa->sadb_sa_replay = 0;
3425 sa->sadb_sa_state = 0;
3426 sa->sadb_sa_auth = 0;
3427 sa->sadb_sa_encrypt = 0;
3428 sa->sadb_sa_flags = 0;
3430 /* ADDRESS_SRC (old addr) */
3431 addr = (struct sadb_address*)
3432 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3433 addr->sadb_address_len =
3434 (sizeof(struct sadb_address)+sockaddr_size)/
3435 sizeof(uint64_t);
3436 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3437 addr->sadb_address_proto = 0;
3438 addr->sadb_address_reserved = 0;
3439 addr->sadb_address_prefixlen =
3440 pfkey_sockaddr_fill(&x->props.saddr, 0,
3441 (struct sockaddr *) (addr + 1),
3442 x->props.family);
3443 if (!addr->sadb_address_prefixlen)
3444 BUG();
3446 /* NAT_T_SPORT (old port) */
3447 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3448 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3449 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3450 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3451 n_port->sadb_x_nat_t_port_reserved = 0;
3453 /* ADDRESS_DST (new addr) */
3454 addr = (struct sadb_address*)
3455 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3456 addr->sadb_address_len =
3457 (sizeof(struct sadb_address)+sockaddr_size)/
3458 sizeof(uint64_t);
3459 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3460 addr->sadb_address_proto = 0;
3461 addr->sadb_address_reserved = 0;
3462 addr->sadb_address_prefixlen =
3463 pfkey_sockaddr_fill(ipaddr, 0,
3464 (struct sockaddr *) (addr + 1),
3465 x->props.family);
3466 if (!addr->sadb_address_prefixlen)
3467 BUG();
3469 /* NAT_T_DPORT (new port) */
3470 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3471 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3472 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3473 n_port->sadb_x_nat_t_port_port = sport;
3474 n_port->sadb_x_nat_t_port_reserved = 0;
3476 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3477 xs_net(x));
3480 #ifdef CONFIG_NET_KEY_MIGRATE
3481 static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3482 const struct xfrm_selector *sel)
3484 struct sadb_address *addr;
3485 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3486 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3487 addr->sadb_address_exttype = type;
3488 addr->sadb_address_proto = sel->proto;
3489 addr->sadb_address_reserved = 0;
3491 switch (type) {
3492 case SADB_EXT_ADDRESS_SRC:
3493 addr->sadb_address_prefixlen = sel->prefixlen_s;
3494 pfkey_sockaddr_fill(&sel->saddr, 0,
3495 (struct sockaddr *)(addr + 1),
3496 sel->family);
3497 break;
3498 case SADB_EXT_ADDRESS_DST:
3499 addr->sadb_address_prefixlen = sel->prefixlen_d;
3500 pfkey_sockaddr_fill(&sel->daddr, 0,
3501 (struct sockaddr *)(addr + 1),
3502 sel->family);
3503 break;
3504 default:
3505 return -EINVAL;
3508 return 0;
3512 static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
3514 struct sadb_x_kmaddress *kma;
3515 u8 *sa;
3516 int family = k->family;
3517 int socklen = pfkey_sockaddr_len(family);
3518 int size_req;
3520 size_req = (sizeof(struct sadb_x_kmaddress) +
3521 pfkey_sockaddr_pair_size(family));
3523 kma = (struct sadb_x_kmaddress *)skb_put(skb, size_req);
3524 memset(kma, 0, size_req);
3525 kma->sadb_x_kmaddress_len = size_req / 8;
3526 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3527 kma->sadb_x_kmaddress_reserved = k->reserved;
3529 sa = (u8 *)(kma + 1);
3530 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3531 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3532 return -EINVAL;
3534 return 0;
3537 static int set_ipsecrequest(struct sk_buff *skb,
3538 uint8_t proto, uint8_t mode, int level,
3539 uint32_t reqid, uint8_t family,
3540 const xfrm_address_t *src, const xfrm_address_t *dst)
3542 struct sadb_x_ipsecrequest *rq;
3543 u8 *sa;
3544 int socklen = pfkey_sockaddr_len(family);
3545 int size_req;
3547 size_req = sizeof(struct sadb_x_ipsecrequest) +
3548 pfkey_sockaddr_pair_size(family);
3550 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3551 memset(rq, 0, size_req);
3552 rq->sadb_x_ipsecrequest_len = size_req;
3553 rq->sadb_x_ipsecrequest_proto = proto;
3554 rq->sadb_x_ipsecrequest_mode = mode;
3555 rq->sadb_x_ipsecrequest_level = level;
3556 rq->sadb_x_ipsecrequest_reqid = reqid;
3558 sa = (u8 *) (rq + 1);
3559 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3560 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
3561 return -EINVAL;
3563 return 0;
3565 #endif
3567 #ifdef CONFIG_NET_KEY_MIGRATE
3568 static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3569 const struct xfrm_migrate *m, int num_bundles,
3570 const struct xfrm_kmaddress *k)
3572 int i;
3573 int sasize_sel;
3574 int size = 0;
3575 int size_pol = 0;
3576 struct sk_buff *skb;
3577 struct sadb_msg *hdr;
3578 struct sadb_x_policy *pol;
3579 const struct xfrm_migrate *mp;
3581 if (type != XFRM_POLICY_TYPE_MAIN)
3582 return 0;
3584 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3585 return -EINVAL;
3587 if (k != NULL) {
3588 /* addresses for KM */
3589 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3590 pfkey_sockaddr_pair_size(k->family));
3593 /* selector */
3594 sasize_sel = pfkey_sockaddr_size(sel->family);
3595 if (!sasize_sel)
3596 return -EINVAL;
3597 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3599 /* policy info */
3600 size_pol += sizeof(struct sadb_x_policy);
3602 /* ipsecrequests */
3603 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3604 /* old locator pair */
3605 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3606 pfkey_sockaddr_pair_size(mp->old_family);
3607 /* new locator pair */
3608 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3609 pfkey_sockaddr_pair_size(mp->new_family);
3612 size += sizeof(struct sadb_msg) + size_pol;
3614 /* alloc buffer */
3615 skb = alloc_skb(size, GFP_ATOMIC);
3616 if (skb == NULL)
3617 return -ENOMEM;
3619 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3620 hdr->sadb_msg_version = PF_KEY_V2;
3621 hdr->sadb_msg_type = SADB_X_MIGRATE;
3622 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3623 hdr->sadb_msg_len = size / 8;
3624 hdr->sadb_msg_errno = 0;
3625 hdr->sadb_msg_reserved = 0;
3626 hdr->sadb_msg_seq = 0;
3627 hdr->sadb_msg_pid = 0;
3629 /* Addresses to be used by KM for negotiation, if ext is available */
3630 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
3631 goto err;
3633 /* selector src */
3634 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3636 /* selector dst */
3637 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3639 /* policy information */
3640 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3641 pol->sadb_x_policy_len = size_pol / 8;
3642 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3643 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3644 pol->sadb_x_policy_dir = dir + 1;
3645 pol->sadb_x_policy_reserved = 0;
3646 pol->sadb_x_policy_id = 0;
3647 pol->sadb_x_policy_priority = 0;
3649 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3650 /* old ipsecrequest */
3651 int mode = pfkey_mode_from_xfrm(mp->mode);
3652 if (mode < 0)
3653 goto err;
3654 if (set_ipsecrequest(skb, mp->proto, mode,
3655 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3656 mp->reqid, mp->old_family,
3657 &mp->old_saddr, &mp->old_daddr) < 0)
3658 goto err;
3660 /* new ipsecrequest */
3661 if (set_ipsecrequest(skb, mp->proto, mode,
3662 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3663 mp->reqid, mp->new_family,
3664 &mp->new_saddr, &mp->new_daddr) < 0)
3665 goto err;
3668 /* broadcast migrate message to sockets */
3669 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net);
3671 return 0;
3673 err:
3674 kfree_skb(skb);
3675 return -EINVAL;
3677 #else
3678 static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3679 const struct xfrm_migrate *m, int num_bundles,
3680 const struct xfrm_kmaddress *k)
3682 return -ENOPROTOOPT;
3684 #endif
3686 static int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
3688 struct sock *sk = sock->sk;
3689 struct sk_buff *skb = NULL;
3690 struct sadb_msg *hdr = NULL;
3691 int err;
3692 struct net *net = sock_net(sk);
3694 err = -EOPNOTSUPP;
3695 if (msg->msg_flags & MSG_OOB)
3696 goto out;
3698 err = -EMSGSIZE;
3699 if ((unsigned int)len > sk->sk_sndbuf - 32)
3700 goto out;
3702 err = -ENOBUFS;
3703 skb = alloc_skb(len, GFP_KERNEL);
3704 if (skb == NULL)
3705 goto out;
3707 err = -EFAULT;
3708 if (memcpy_from_msg(skb_put(skb,len), msg, len))
3709 goto out;
3711 hdr = pfkey_get_base_msg(skb, &err);
3712 if (!hdr)
3713 goto out;
3715 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
3716 err = pfkey_process(sk, skb, hdr);
3717 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
3719 out:
3720 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3721 err = 0;
3722 kfree_skb(skb);
3724 return err ? : len;
3727 static int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3728 int flags)
3730 struct sock *sk = sock->sk;
3731 struct pfkey_sock *pfk = pfkey_sk(sk);
3732 struct sk_buff *skb;
3733 int copied, err;
3735 err = -EINVAL;
3736 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3737 goto out;
3739 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3740 if (skb == NULL)
3741 goto out;
3743 copied = skb->len;
3744 if (copied > len) {
3745 msg->msg_flags |= MSG_TRUNC;
3746 copied = len;
3749 skb_reset_transport_header(skb);
3750 err = skb_copy_datagram_msg(skb, 0, msg, copied);
3751 if (err)
3752 goto out_free;
3754 sock_recv_ts_and_drops(msg, sk, skb);
3756 err = (flags & MSG_TRUNC) ? skb->len : copied;
3758 if (pfk->dump.dump != NULL &&
3759 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3760 pfkey_do_dump(pfk);
3762 out_free:
3763 skb_free_datagram(sk, skb);
3764 out:
3765 return err;
3768 static const struct proto_ops pfkey_ops = {
3769 .family = PF_KEY,
3770 .owner = THIS_MODULE,
3771 /* Operations that make no sense on pfkey sockets. */
3772 .bind = sock_no_bind,
3773 .connect = sock_no_connect,
3774 .socketpair = sock_no_socketpair,
3775 .accept = sock_no_accept,
3776 .getname = sock_no_getname,
3777 .ioctl = sock_no_ioctl,
3778 .listen = sock_no_listen,
3779 .shutdown = sock_no_shutdown,
3780 .setsockopt = sock_no_setsockopt,
3781 .getsockopt = sock_no_getsockopt,
3782 .mmap = sock_no_mmap,
3783 .sendpage = sock_no_sendpage,
3785 /* Now the operations that really occur. */
3786 .release = pfkey_release,
3787 .poll = datagram_poll,
3788 .sendmsg = pfkey_sendmsg,
3789 .recvmsg = pfkey_recvmsg,
3792 static const struct net_proto_family pfkey_family_ops = {
3793 .family = PF_KEY,
3794 .create = pfkey_create,
3795 .owner = THIS_MODULE,
3798 #ifdef CONFIG_PROC_FS
3799 static int pfkey_seq_show(struct seq_file *f, void *v)
3801 struct sock *s = sk_entry(v);
3803 if (v == SEQ_START_TOKEN)
3804 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3805 else
3806 seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
3808 atomic_read(&s->sk_refcnt),
3809 sk_rmem_alloc_get(s),
3810 sk_wmem_alloc_get(s),
3811 from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
3812 sock_i_ino(s)
3814 return 0;
3817 static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3818 __acquires(rcu)
3820 struct net *net = seq_file_net(f);
3821 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3823 rcu_read_lock();
3824 return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
3827 static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3829 struct net *net = seq_file_net(f);
3830 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3832 return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
3835 static void pfkey_seq_stop(struct seq_file *f, void *v)
3836 __releases(rcu)
3838 rcu_read_unlock();
3841 static const struct seq_operations pfkey_seq_ops = {
3842 .start = pfkey_seq_start,
3843 .next = pfkey_seq_next,
3844 .stop = pfkey_seq_stop,
3845 .show = pfkey_seq_show,
3848 static int pfkey_seq_open(struct inode *inode, struct file *file)
3850 return seq_open_net(inode, file, &pfkey_seq_ops,
3851 sizeof(struct seq_net_private));
3854 static const struct file_operations pfkey_proc_ops = {
3855 .open = pfkey_seq_open,
3856 .read = seq_read,
3857 .llseek = seq_lseek,
3858 .release = seq_release_net,
3861 static int __net_init pfkey_init_proc(struct net *net)
3863 struct proc_dir_entry *e;
3865 e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops);
3866 if (e == NULL)
3867 return -ENOMEM;
3869 return 0;
3872 static void __net_exit pfkey_exit_proc(struct net *net)
3874 remove_proc_entry("pfkey", net->proc_net);
3876 #else
3877 static inline int pfkey_init_proc(struct net *net)
3879 return 0;
3882 static inline void pfkey_exit_proc(struct net *net)
3885 #endif
3887 static struct xfrm_mgr pfkeyv2_mgr =
3889 .id = "pfkeyv2",
3890 .notify = pfkey_send_notify,
3891 .acquire = pfkey_send_acquire,
3892 .compile_policy = pfkey_compile_policy,
3893 .new_mapping = pfkey_send_new_mapping,
3894 .notify_policy = pfkey_send_policy_notify,
3895 .migrate = pfkey_send_migrate,
3896 .is_alive = pfkey_is_alive,
3899 static int __net_init pfkey_net_init(struct net *net)
3901 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3902 int rv;
3904 INIT_HLIST_HEAD(&net_pfkey->table);
3905 atomic_set(&net_pfkey->socks_nr, 0);
3907 rv = pfkey_init_proc(net);
3909 return rv;
3912 static void __net_exit pfkey_net_exit(struct net *net)
3914 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3916 pfkey_exit_proc(net);
3917 BUG_ON(!hlist_empty(&net_pfkey->table));
3920 static struct pernet_operations pfkey_net_ops = {
3921 .init = pfkey_net_init,
3922 .exit = pfkey_net_exit,
3923 .id = &pfkey_net_id,
3924 .size = sizeof(struct netns_pfkey),
3927 static void __exit ipsec_pfkey_exit(void)
3929 xfrm_unregister_km(&pfkeyv2_mgr);
3930 sock_unregister(PF_KEY);
3931 unregister_pernet_subsys(&pfkey_net_ops);
3932 proto_unregister(&key_proto);
3935 static int __init ipsec_pfkey_init(void)
3937 int err = proto_register(&key_proto, 0);
3939 if (err != 0)
3940 goto out;
3942 err = register_pernet_subsys(&pfkey_net_ops);
3943 if (err != 0)
3944 goto out_unregister_key_proto;
3945 err = sock_register(&pfkey_family_ops);
3946 if (err != 0)
3947 goto out_unregister_pernet;
3948 err = xfrm_register_km(&pfkeyv2_mgr);
3949 if (err != 0)
3950 goto out_sock_unregister;
3951 out:
3952 return err;
3954 out_sock_unregister:
3955 sock_unregister(PF_KEY);
3956 out_unregister_pernet:
3957 unregister_pernet_subsys(&pfkey_net_ops);
3958 out_unregister_key_proto:
3959 proto_unregister(&key_proto);
3960 goto out;
3963 module_init(ipsec_pfkey_init);
3964 module_exit(ipsec_pfkey_exit);
3965 MODULE_LICENSE("GPL");
3966 MODULE_ALIAS_NETPROTO(PF_KEY);