blob: ad495a3cf275d61290a40b821c95758e79443e10 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
3 *
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.
8 *
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>
15 */
16
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>
33
34#include <net/sock.h>
35
36#define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
37#define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
38
39static int pfkey_net_id __read_mostly;
40struct netns_pfkey {
41 /* List of all pfkey sockets. */
42 struct hlist_head table;
43 atomic_t socks_nr;
44};
45static DEFINE_MUTEX(pfkey_mutex);
46
47#define DUMMY_MARK 0
48static struct xfrm_mark dummy_mark = {0, 0};
49struct pfkey_sock {
50 /* struct sock must be the first member of struct pfkey_sock */
51 struct sock sk;
52 int registered;
53 int promisc;
54
55 struct {
56 uint8_t msg_version;
57 uint32_t msg_pid;
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};
67
68static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
69{
70 return (struct pfkey_sock *)sk;
71}
72
73static int pfkey_can_dump(const struct sock *sk)
74{
75 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
76 return 1;
77 return 0;
78}
79
80static void pfkey_terminate_dump(struct pfkey_sock *pfk)
81{
82 if (pfk->dump.dump) {
83 if (pfk->dump.skb) {
84 kfree_skb(pfk->dump.skb);
85 pfk->dump.skb = NULL;
86 }
87 pfk->dump.done(pfk);
88 pfk->dump.dump = NULL;
89 pfk->dump.done = NULL;
90 }
91}
92
93static void pfkey_sock_destruct(struct sock *sk)
94{
95 struct net *net = sock_net(sk);
96 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
97
98 pfkey_terminate_dump(pfkey_sk(sk));
99 skb_queue_purge(&sk->sk_receive_queue);
100
101 if (!sock_flag(sk, SOCK_DEAD)) {
102 pr_err("Attempt to release alive pfkey socket: %p\n", sk);
103 return;
104 }
105
106 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
107 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
108
109 atomic_dec(&net_pfkey->socks_nr);
110}
111
112static const struct proto_ops pfkey_ops;
113
114static void pfkey_insert(struct sock *sk)
115{
116 struct net *net = sock_net(sk);
117 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
118
119 mutex_lock(&pfkey_mutex);
120 sk_add_node_rcu(sk, &net_pfkey->table);
121 mutex_unlock(&pfkey_mutex);
122}
123
124static void pfkey_remove(struct sock *sk)
125{
126 mutex_lock(&pfkey_mutex);
127 sk_del_node_init_rcu(sk);
128 mutex_unlock(&pfkey_mutex);
129}
130
131static struct proto key_proto = {
132 .name = "KEY",
133 .owner = THIS_MODULE,
134 .obj_size = sizeof(struct pfkey_sock),
135};
136
137static int pfkey_create(struct net *net, struct socket *sock, int protocol,
138 int kern)
139{
140 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
141 struct sock *sk;
142 int err;
143
144 if (!capable(CAP_NET_ADMIN))
145 return -EPERM;
146 if (sock->type != SOCK_RAW)
147 return -ESOCKTNOSUPPORT;
148 if (protocol != PF_KEY_V2)
149 return -EPROTONOSUPPORT;
150
151 err = -ENOMEM;
152 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto);
153 if (sk == NULL)
154 goto out;
155
156 sock->ops = &pfkey_ops;
157 sock_init_data(sock, sk);
158
159 sk->sk_family = PF_KEY;
160 sk->sk_destruct = pfkey_sock_destruct;
161
162 atomic_inc(&net_pfkey->socks_nr);
163
164 pfkey_insert(sk);
165
166 return 0;
167out:
168 return err;
169}
170
171static int pfkey_release(struct socket *sock)
172{
173 struct sock *sk = sock->sk;
174
175 if (!sk)
176 return 0;
177
178 pfkey_remove(sk);
179
180 sock_orphan(sk);
181 sock->sk = NULL;
182 skb_queue_purge(&sk->sk_write_queue);
183
184 synchronize_rcu();
185 sock_put(sk);
186
187 return 0;
188}
189
190static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
191 gfp_t allocation, struct sock *sk)
192{
193 int err = -ENOBUFS;
194
195 sock_hold(sk);
196 if (*skb2 == NULL) {
197 if (atomic_read(&skb->users) != 1) {
198 *skb2 = skb_clone(skb, allocation);
199 } else {
200 *skb2 = skb;
201 atomic_inc(&skb->users);
202 track_add(skb, 0, USER_INFO, 0);
203 }
204 }
205 if (*skb2 != NULL) {
206 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
207 skb_orphan(*skb2);
208 skb_set_owner_r(*skb2, sk);
209 skb_queue_tail(&sk->sk_receive_queue, *skb2);
210 sk->sk_data_ready(sk, (*skb2)->len);
211 *skb2 = NULL;
212 err = 0;
213 }
214 }
215 sock_put(sk);
216 return err;
217}
218
219/* Send SKB to all pfkey sockets matching selected criteria. */
220#define BROADCAST_ALL 0
221#define BROADCAST_ONE 1
222#define BROADCAST_REGISTERED 2
223#define BROADCAST_PROMISC_ONLY 4
224static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
225 int broadcast_flags, struct sock *one_sk,
226 struct net *net)
227{
228 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
229 struct sock *sk;
230 struct hlist_node *node;
231 struct sk_buff *skb2 = NULL;
232 int err = -ESRCH;
233
234 /* XXX Do we need something like netlink_overrun? I think
235 * XXX PF_KEY socket apps will not mind current behavior.
236 */
237 if (!skb)
238 return -ENOMEM;
239
240 rcu_read_lock();
241 sk_for_each_rcu(sk, node, &net_pfkey->table) {
242 struct pfkey_sock *pfk = pfkey_sk(sk);
243 int err2;
244
245 /* Yes, it means that if you are meant to receive this
246 * pfkey message you receive it twice as promiscuous
247 * socket.
248 */
249 if (pfk->promisc)
250 pfkey_broadcast_one(skb, &skb2, allocation, sk);
251
252 /* the exact target will be processed later */
253 if (sk == one_sk)
254 continue;
255 if (broadcast_flags != BROADCAST_ALL) {
256 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
257 continue;
258 if ((broadcast_flags & BROADCAST_REGISTERED) &&
259 !pfk->registered)
260 continue;
261 if (broadcast_flags & BROADCAST_ONE)
262 continue;
263 }
264
265 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
266
267 /* Error is cleare after succecful sending to at least one
268 * registered KM */
269 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
270 err = err2;
271 }
272 rcu_read_unlock();
273
274 if (one_sk != NULL)
275 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
276
277 kfree_skb(skb2);
278 kfree_skb(skb);
279 return err;
280}
281
282static int pfkey_do_dump(struct pfkey_sock *pfk)
283{
284 struct sadb_msg *hdr;
285 int rc;
286
287 rc = pfk->dump.dump(pfk);
288 if (rc == -ENOBUFS)
289 return 0;
290
291 if (pfk->dump.skb) {
292 if (!pfkey_can_dump(&pfk->sk))
293 return 0;
294
295 hdr = (struct sadb_msg *) pfk->dump.skb->data;
296 hdr->sadb_msg_seq = 0;
297 hdr->sadb_msg_errno = rc;
298 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
299 &pfk->sk, sock_net(&pfk->sk));
300 pfk->dump.skb = NULL;
301 }
302
303 pfkey_terminate_dump(pfk);
304 return rc;
305}
306
307static inline void pfkey_hdr_dup(struct sadb_msg *new,
308 const struct sadb_msg *orig)
309{
310 *new = *orig;
311}
312
313static int pfkey_error(const struct sadb_msg *orig, int err, struct sock *sk)
314{
315 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
316 struct sadb_msg *hdr;
317
318 if (!skb)
319 return -ENOBUFS;
320
321 /* Woe be to the platform trying to support PFKEY yet
322 * having normal errnos outside the 1-255 range, inclusive.
323 */
324 err = -err;
325 if (err == ERESTARTSYS ||
326 err == ERESTARTNOHAND ||
327 err == ERESTARTNOINTR)
328 err = EINTR;
329 if (err >= 512)
330 err = EINVAL;
331 BUG_ON(err <= 0 || err >= 256);
332
333 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
334 pfkey_hdr_dup(hdr, orig);
335 hdr->sadb_msg_errno = (uint8_t) err;
336 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
337 sizeof(uint64_t));
338
339 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk, sock_net(sk));
340
341 return 0;
342}
343
344static u8 sadb_ext_min_len[] = {
345 [SADB_EXT_RESERVED] = (u8) 0,
346 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
347 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
348 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
349 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
350 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
351 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
352 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
353 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
354 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
355 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
356 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
357 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
358 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
359 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
360 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
361 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
362 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
363 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
364 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
365 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
366 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
367 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
368 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
369 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
370 [SADB_X_EXT_KMADDRESS] = (u8) sizeof(struct sadb_x_kmaddress),
371};
372
373/* Verify sadb_address_{len,prefixlen} against sa_family. */
374static int verify_address_len(const void *p)
375{
376 const struct sadb_address *sp = p;
377 const struct sockaddr *addr = (const struct sockaddr *)(sp + 1);
378 const struct sockaddr_in *sin;
379#if IS_ENABLED(CONFIG_IPV6)
380 const struct sockaddr_in6 *sin6;
381#endif
382 int len;
383
384 switch (addr->sa_family) {
385 case AF_INET:
386 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
387 if (sp->sadb_address_len != len ||
388 sp->sadb_address_prefixlen > 32)
389 return -EINVAL;
390 break;
391#if IS_ENABLED(CONFIG_IPV6)
392 case AF_INET6:
393 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
394 if (sp->sadb_address_len != len ||
395 sp->sadb_address_prefixlen > 128)
396 return -EINVAL;
397 break;
398#endif
399 default:
400 /* It is user using kernel to keep track of security
401 * associations for another protocol, such as
402 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
403 * lengths.
404 *
405 * XXX Actually, association/policy database is not yet
406 * XXX able to cope with arbitrary sockaddr families.
407 * XXX When it can, remove this -EINVAL. -DaveM
408 */
409 return -EINVAL;
410 break;
411 }
412
413 return 0;
414}
415
416static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
417{
418 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
419 sec_ctx->sadb_x_ctx_len,
420 sizeof(uint64_t));
421}
422
423static inline int verify_sec_ctx_len(const void *p)
424{
425 const struct sadb_x_sec_ctx *sec_ctx = p;
426 int len = sec_ctx->sadb_x_ctx_len;
427
428 if (len > PAGE_SIZE)
429 return -EINVAL;
430
431 len = pfkey_sec_ctx_len(sec_ctx);
432
433 if (sec_ctx->sadb_x_sec_len != len)
434 return -EINVAL;
435
436 return 0;
437}
438
439static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx)
440{
441 struct xfrm_user_sec_ctx *uctx = NULL;
442 int ctx_size = sec_ctx->sadb_x_ctx_len;
443
444 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
445
446 if (!uctx)
447 return NULL;
448
449 uctx->len = pfkey_sec_ctx_len(sec_ctx);
450 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
451 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
452 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
453 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
454 memcpy(uctx + 1, sec_ctx + 1,
455 uctx->ctx_len);
456
457 return uctx;
458}
459
460static int present_and_same_family(const struct sadb_address *src,
461 const struct sadb_address *dst)
462{
463 const struct sockaddr *s_addr, *d_addr;
464
465 if (!src || !dst)
466 return 0;
467
468 s_addr = (const struct sockaddr *)(src + 1);
469 d_addr = (const struct sockaddr *)(dst + 1);
470 if (s_addr->sa_family != d_addr->sa_family)
471 return 0;
472 if (s_addr->sa_family != AF_INET
473#if IS_ENABLED(CONFIG_IPV6)
474 && s_addr->sa_family != AF_INET6
475#endif
476 )
477 return 0;
478
479 return 1;
480}
481
482static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
483{
484 const char *p = (char *) hdr;
485 int len = skb->len;
486
487 len -= sizeof(*hdr);
488 p += sizeof(*hdr);
489 while (len > 0) {
490 const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
491 uint16_t ext_type;
492 int ext_len;
493
494 ext_len = ehdr->sadb_ext_len;
495 ext_len *= sizeof(uint64_t);
496 ext_type = ehdr->sadb_ext_type;
497 if (ext_len < sizeof(uint64_t) ||
498 ext_len > len ||
499 ext_type == SADB_EXT_RESERVED)
500 return -EINVAL;
501
502 if (ext_type <= SADB_EXT_MAX) {
503 int min = (int) sadb_ext_min_len[ext_type];
504 if (ext_len < min)
505 return -EINVAL;
506 if (ext_hdrs[ext_type-1] != NULL)
507 return -EINVAL;
508 if (ext_type == SADB_EXT_ADDRESS_SRC ||
509 ext_type == SADB_EXT_ADDRESS_DST ||
510 ext_type == SADB_EXT_ADDRESS_PROXY ||
511 ext_type == SADB_X_EXT_NAT_T_OA) {
512 if (verify_address_len(p))
513 return -EINVAL;
514 }
515 if (ext_type == SADB_X_EXT_SEC_CTX) {
516 if (verify_sec_ctx_len(p))
517 return -EINVAL;
518 }
519 ext_hdrs[ext_type-1] = (void *) p;
520 }
521 p += ext_len;
522 len -= ext_len;
523 }
524
525 return 0;
526}
527
528static uint16_t
529pfkey_satype2proto(uint8_t satype)
530{
531 switch (satype) {
532 case SADB_SATYPE_UNSPEC:
533 return IPSEC_PROTO_ANY;
534 case SADB_SATYPE_AH:
535 return IPPROTO_AH;
536 case SADB_SATYPE_ESP:
537 return IPPROTO_ESP;
538 case SADB_X_SATYPE_IPCOMP:
539 return IPPROTO_COMP;
540 break;
541 default:
542 return 0;
543 }
544 /* NOTREACHED */
545}
546
547static uint8_t
548pfkey_proto2satype(uint16_t proto)
549{
550 switch (proto) {
551 case IPPROTO_AH:
552 return SADB_SATYPE_AH;
553 case IPPROTO_ESP:
554 return SADB_SATYPE_ESP;
555 case IPPROTO_COMP:
556 return SADB_X_SATYPE_IPCOMP;
557 break;
558 default:
559 return 0;
560 }
561 /* NOTREACHED */
562}
563
564/* BTW, this scheme means that there is no way with PFKEY2 sockets to
565 * say specifically 'just raw sockets' as we encode them as 255.
566 */
567
568static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
569{
570 return proto == IPSEC_PROTO_ANY ? 0 : proto;
571}
572
573static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
574{
575 return proto ? proto : IPSEC_PROTO_ANY;
576}
577
578static inline int pfkey_sockaddr_len(sa_family_t family)
579{
580 switch (family) {
581 case AF_INET:
582 return sizeof(struct sockaddr_in);
583#if IS_ENABLED(CONFIG_IPV6)
584 case AF_INET6:
585 return sizeof(struct sockaddr_in6);
586#endif
587 }
588 return 0;
589}
590
591static
592int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
593{
594 switch (sa->sa_family) {
595 case AF_INET:
596 xaddr->a4 =
597 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
598 return AF_INET;
599#if IS_ENABLED(CONFIG_IPV6)
600 case AF_INET6:
601 memcpy(xaddr->a6,
602 &((struct sockaddr_in6 *)sa)->sin6_addr,
603 sizeof(struct in6_addr));
604 return AF_INET6;
605#endif
606 }
607 return 0;
608}
609
610static
611int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
612{
613 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
614 xaddr);
615}
616
617static struct xfrm_state *pfkey_xfrm_state_lookup(struct net *net, const struct sadb_msg *hdr, void * const *ext_hdrs)
618{
619 const struct sadb_sa *sa;
620 const struct sadb_address *addr;
621 uint16_t proto;
622 unsigned short family;
623 xfrm_address_t *xaddr;
624
625 sa = ext_hdrs[SADB_EXT_SA - 1];
626 if (sa == NULL)
627 return NULL;
628
629 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
630 if (proto == 0)
631 return NULL;
632
633 /* sadb_address_len should be checked by caller */
634 addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
635 if (addr == NULL)
636 return NULL;
637
638 family = ((const struct sockaddr *)(addr + 1))->sa_family;
639 switch (family) {
640 case AF_INET:
641 xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
642 break;
643#if IS_ENABLED(CONFIG_IPV6)
644 case AF_INET6:
645 xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
646 break;
647#endif
648 default:
649 xaddr = NULL;
650 }
651
652 if (!xaddr)
653 return NULL;
654
655 return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
656}
657
658#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
659
660static int
661pfkey_sockaddr_size(sa_family_t family)
662{
663 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
664}
665
666static inline int pfkey_mode_from_xfrm(int mode)
667{
668 switch(mode) {
669 case XFRM_MODE_TRANSPORT:
670 return IPSEC_MODE_TRANSPORT;
671 case XFRM_MODE_TUNNEL:
672 return IPSEC_MODE_TUNNEL;
673 case XFRM_MODE_BEET:
674 return IPSEC_MODE_BEET;
675 default:
676 return -1;
677 }
678}
679
680static inline int pfkey_mode_to_xfrm(int mode)
681{
682 switch(mode) {
683 case IPSEC_MODE_ANY: /*XXX*/
684 case IPSEC_MODE_TRANSPORT:
685 return XFRM_MODE_TRANSPORT;
686 case IPSEC_MODE_TUNNEL:
687 return XFRM_MODE_TUNNEL;
688 case IPSEC_MODE_BEET:
689 return XFRM_MODE_BEET;
690 default:
691 return -1;
692 }
693}
694
695static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
696 struct sockaddr *sa,
697 unsigned short family)
698{
699 switch (family) {
700 case AF_INET:
701 {
702 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
703 sin->sin_family = AF_INET;
704 sin->sin_port = port;
705 sin->sin_addr.s_addr = xaddr->a4;
706 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
707 return 32;
708 }
709#if IS_ENABLED(CONFIG_IPV6)
710 case AF_INET6:
711 {
712 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
713 sin6->sin6_family = AF_INET6;
714 sin6->sin6_port = port;
715 sin6->sin6_flowinfo = 0;
716 sin6->sin6_addr = *(struct in6_addr *)xaddr->a6;
717 sin6->sin6_scope_id = 0;
718 return 128;
719 }
720#endif
721 }
722 return 0;
723}
724
725static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
726 int add_keys, int hsc)
727{
728 struct sk_buff *skb;
729 struct sadb_msg *hdr;
730 struct sadb_sa *sa;
731 struct sadb_lifetime *lifetime;
732 struct sadb_address *addr;
733 struct sadb_key *key;
734 struct sadb_x_sa2 *sa2;
735 struct sadb_x_sec_ctx *sec_ctx;
736 struct xfrm_sec_ctx *xfrm_ctx;
737 int ctx_size = 0;
738 int size;
739 int auth_key_size = 0;
740 int encrypt_key_size = 0;
741 int sockaddr_size;
742 struct xfrm_encap_tmpl *natt = NULL;
743 int mode;
744
745 /* address family check */
746 sockaddr_size = pfkey_sockaddr_size(x->props.family);
747 if (!sockaddr_size)
748 return ERR_PTR(-EINVAL);
749
750 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
751 key(AE), (identity(SD),) (sensitivity)> */
752 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
753 sizeof(struct sadb_lifetime) +
754 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
755 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
756 sizeof(struct sadb_address)*2 +
757 sockaddr_size*2 +
758 sizeof(struct sadb_x_sa2);
759
760 if ((xfrm_ctx = x->security)) {
761 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
762 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
763 }
764
765 /* identity & sensitivity */
766 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr, x->props.family))
767 size += sizeof(struct sadb_address) + sockaddr_size;
768
769 if (add_keys) {
770 if (x->aalg && x->aalg->alg_key_len) {
771 auth_key_size =
772 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
773 size += sizeof(struct sadb_key) + auth_key_size;
774 }
775 if (x->ealg && x->ealg->alg_key_len) {
776 encrypt_key_size =
777 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
778 size += sizeof(struct sadb_key) + encrypt_key_size;
779 }
780 }
781 if (x->encap)
782 natt = x->encap;
783
784 if (natt && natt->encap_type) {
785 size += sizeof(struct sadb_x_nat_t_type);
786 size += sizeof(struct sadb_x_nat_t_port);
787 size += sizeof(struct sadb_x_nat_t_port);
788 }
789
790 skb = alloc_skb(size + 16, GFP_ATOMIC);
791 if (skb == NULL)
792 return ERR_PTR(-ENOBUFS);
793
794 /* call should fill header later */
795 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
796 memset(hdr, 0, size); /* XXX do we need this ? */
797 hdr->sadb_msg_len = size / sizeof(uint64_t);
798
799 /* sa */
800 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
801 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
802 sa->sadb_sa_exttype = SADB_EXT_SA;
803 sa->sadb_sa_spi = x->id.spi;
804 sa->sadb_sa_replay = x->props.replay_window;
805 switch (x->km.state) {
806 case XFRM_STATE_VALID:
807 sa->sadb_sa_state = x->km.dying ?
808 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
809 break;
810 case XFRM_STATE_ACQ:
811 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
812 break;
813 default:
814 sa->sadb_sa_state = SADB_SASTATE_DEAD;
815 break;
816 }
817 sa->sadb_sa_auth = 0;
818 if (x->aalg) {
819 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
820 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
821 }
822 sa->sadb_sa_encrypt = 0;
823 BUG_ON(x->ealg && x->calg);
824 if (x->ealg) {
825 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
826 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
827 }
828 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
829 if (x->calg) {
830 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
831 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
832 }
833
834 sa->sadb_sa_flags = 0;
835 if (x->props.flags & XFRM_STATE_NOECN)
836 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
837 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
838 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
839 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
840 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
841
842 /* hard time */
843 if (hsc & 2) {
844 lifetime = (struct sadb_lifetime *) skb_put(skb,
845 sizeof(struct sadb_lifetime));
846 lifetime->sadb_lifetime_len =
847 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
848 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
849 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
850 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
851 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
852 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
853 }
854 /* soft time */
855 if (hsc & 1) {
856 lifetime = (struct sadb_lifetime *) skb_put(skb,
857 sizeof(struct sadb_lifetime));
858 lifetime->sadb_lifetime_len =
859 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
860 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
861 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
862 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
863 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
864 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
865 }
866 /* current time */
867 lifetime = (struct sadb_lifetime *) skb_put(skb,
868 sizeof(struct sadb_lifetime));
869 lifetime->sadb_lifetime_len =
870 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
871 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
872 lifetime->sadb_lifetime_allocations = x->curlft.packets;
873 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
874 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
875 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
876 /* src address */
877 addr = (struct sadb_address*) skb_put(skb,
878 sizeof(struct sadb_address)+sockaddr_size);
879 addr->sadb_address_len =
880 (sizeof(struct sadb_address)+sockaddr_size)/
881 sizeof(uint64_t);
882 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
883 /* "if the ports are non-zero, then the sadb_address_proto field,
884 normally zero, MUST be filled in with the transport
885 protocol's number." - RFC2367 */
886 addr->sadb_address_proto = 0;
887 addr->sadb_address_reserved = 0;
888
889 addr->sadb_address_prefixlen =
890 pfkey_sockaddr_fill(&x->props.saddr, 0,
891 (struct sockaddr *) (addr + 1),
892 x->props.family);
893 if (!addr->sadb_address_prefixlen)
894 BUG();
895
896 /* dst address */
897 addr = (struct sadb_address*) skb_put(skb,
898 sizeof(struct sadb_address)+sockaddr_size);
899 addr->sadb_address_len =
900 (sizeof(struct sadb_address)+sockaddr_size)/
901 sizeof(uint64_t);
902 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
903 addr->sadb_address_proto = 0;
904 addr->sadb_address_reserved = 0;
905
906 addr->sadb_address_prefixlen =
907 pfkey_sockaddr_fill(&x->id.daddr, 0,
908 (struct sockaddr *) (addr + 1),
909 x->props.family);
910 if (!addr->sadb_address_prefixlen)
911 BUG();
912
913 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr,
914 x->props.family)) {
915 addr = (struct sadb_address*) skb_put(skb,
916 sizeof(struct sadb_address)+sockaddr_size);
917 addr->sadb_address_len =
918 (sizeof(struct sadb_address)+sockaddr_size)/
919 sizeof(uint64_t);
920 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
921 addr->sadb_address_proto =
922 pfkey_proto_from_xfrm(x->sel.proto);
923 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
924 addr->sadb_address_reserved = 0;
925
926 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
927 (struct sockaddr *) (addr + 1),
928 x->props.family);
929 }
930
931 /* auth key */
932 if (add_keys && auth_key_size) {
933 key = (struct sadb_key *) skb_put(skb,
934 sizeof(struct sadb_key)+auth_key_size);
935 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
936 sizeof(uint64_t);
937 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
938 key->sadb_key_bits = x->aalg->alg_key_len;
939 key->sadb_key_reserved = 0;
940 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
941 }
942 /* encrypt key */
943 if (add_keys && encrypt_key_size) {
944 key = (struct sadb_key *) skb_put(skb,
945 sizeof(struct sadb_key)+encrypt_key_size);
946 key->sadb_key_len = (sizeof(struct sadb_key) +
947 encrypt_key_size) / sizeof(uint64_t);
948 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
949 key->sadb_key_bits = x->ealg->alg_key_len;
950 key->sadb_key_reserved = 0;
951 memcpy(key + 1, x->ealg->alg_key,
952 (x->ealg->alg_key_len+7)/8);
953 }
954
955 /* sa */
956 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
957 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
958 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
959 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
960 kfree_skb(skb);
961 return ERR_PTR(-EINVAL);
962 }
963 sa2->sadb_x_sa2_mode = mode;
964 sa2->sadb_x_sa2_reserved1 = 0;
965 sa2->sadb_x_sa2_reserved2 = 0;
966 sa2->sadb_x_sa2_sequence = 0;
967 sa2->sadb_x_sa2_reqid = x->props.reqid;
968
969 if (natt && natt->encap_type) {
970 struct sadb_x_nat_t_type *n_type;
971 struct sadb_x_nat_t_port *n_port;
972
973 /* type */
974 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
975 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
976 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
977 n_type->sadb_x_nat_t_type_type = natt->encap_type;
978 n_type->sadb_x_nat_t_type_reserved[0] = 0;
979 n_type->sadb_x_nat_t_type_reserved[1] = 0;
980 n_type->sadb_x_nat_t_type_reserved[2] = 0;
981
982 /* source port */
983 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
984 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
985 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
986 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
987 n_port->sadb_x_nat_t_port_reserved = 0;
988
989 /* dest port */
990 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
991 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
992 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
993 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
994 n_port->sadb_x_nat_t_port_reserved = 0;
995 }
996
997 /* security context */
998 if (xfrm_ctx) {
999 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1000 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1001 sec_ctx->sadb_x_sec_len =
1002 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1003 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1004 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1005 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1006 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1007 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1008 xfrm_ctx->ctx_len);
1009 }
1010
1011 return skb;
1012}
1013
1014
1015static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
1016{
1017 struct sk_buff *skb;
1018
1019 skb = __pfkey_xfrm_state2msg(x, 1, 3);
1020
1021 return skb;
1022}
1023
1024static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
1025 int hsc)
1026{
1027 return __pfkey_xfrm_state2msg(x, 0, hsc);
1028}
1029
1030static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
1031 const struct sadb_msg *hdr,
1032 void * const *ext_hdrs)
1033{
1034 struct xfrm_state *x;
1035 const struct sadb_lifetime *lifetime;
1036 const struct sadb_sa *sa;
1037 const struct sadb_key *key;
1038 const struct sadb_x_sec_ctx *sec_ctx;
1039 uint16_t proto;
1040 int err;
1041
1042
1043 sa = ext_hdrs[SADB_EXT_SA - 1];
1044 if (!sa ||
1045 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1046 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1047 return ERR_PTR(-EINVAL);
1048 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1049 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1050 return ERR_PTR(-EINVAL);
1051 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1052 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1053 return ERR_PTR(-EINVAL);
1054 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1055 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1056 return ERR_PTR(-EINVAL);
1057
1058 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1059 if (proto == 0)
1060 return ERR_PTR(-EINVAL);
1061
1062 /* default error is no buffer space */
1063 err = -ENOBUFS;
1064
1065 /* RFC2367:
1066
1067 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1068 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1069 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1070 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1071 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1072 not true.
1073
1074 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1075 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1076 */
1077 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1078 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1079 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1080 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1081 return ERR_PTR(-EINVAL);
1082 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
1083 if (key != NULL &&
1084 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1085 ((key->sadb_key_bits+7) / 8 == 0 ||
1086 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1087 return ERR_PTR(-EINVAL);
1088 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1089 if (key != NULL &&
1090 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1091 ((key->sadb_key_bits+7) / 8 == 0 ||
1092 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1093 return ERR_PTR(-EINVAL);
1094
1095 x = xfrm_state_alloc(net);
1096 if (x == NULL)
1097 return ERR_PTR(-ENOBUFS);
1098
1099 x->id.proto = proto;
1100 x->id.spi = sa->sadb_sa_spi;
1101 x->props.replay_window = sa->sadb_sa_replay;
1102 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1103 x->props.flags |= XFRM_STATE_NOECN;
1104 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1105 x->props.flags |= XFRM_STATE_DECAP_DSCP;
1106 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1107 x->props.flags |= XFRM_STATE_NOPMTUDISC;
1108
1109 lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
1110 if (lifetime != NULL) {
1111 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1112 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1113 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1114 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1115 }
1116 lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
1117 if (lifetime != NULL) {
1118 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1119 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1120 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1121 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1122 }
1123
1124 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
1125 if (sec_ctx != NULL) {
1126 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1127
1128 if (!uctx)
1129 goto out;
1130
1131 err = security_xfrm_state_alloc(x, uctx);
1132 kfree(uctx);
1133
1134 if (err)
1135 goto out;
1136 }
1137
1138 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
1139 if (sa->sadb_sa_auth) {
1140 int keysize = 0;
1141 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1142 if (!a) {
1143 err = -ENOSYS;
1144 goto out;
1145 }
1146 if (key)
1147 keysize = (key->sadb_key_bits + 7) / 8;
1148 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1149 if (!x->aalg)
1150 goto out;
1151 strcpy(x->aalg->alg_name, a->name);
1152 x->aalg->alg_key_len = 0;
1153 if (key) {
1154 x->aalg->alg_key_len = key->sadb_key_bits;
1155 memcpy(x->aalg->alg_key, key+1, keysize);
1156 }
1157 x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
1158 x->props.aalgo = sa->sadb_sa_auth;
1159 /* x->algo.flags = sa->sadb_sa_flags; */
1160 }
1161 if (sa->sadb_sa_encrypt) {
1162 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1163 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1164 if (!a) {
1165 err = -ENOSYS;
1166 goto out;
1167 }
1168 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1169 if (!x->calg)
1170 goto out;
1171 strcpy(x->calg->alg_name, a->name);
1172 x->props.calgo = sa->sadb_sa_encrypt;
1173 } else {
1174 int keysize = 0;
1175 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1176 if (!a) {
1177 err = -ENOSYS;
1178 goto out;
1179 }
1180 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1181 if (key)
1182 keysize = (key->sadb_key_bits + 7) / 8;
1183 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1184 if (!x->ealg)
1185 goto out;
1186 strcpy(x->ealg->alg_name, a->name);
1187 x->ealg->alg_key_len = 0;
1188 if (key) {
1189 x->ealg->alg_key_len = key->sadb_key_bits;
1190 memcpy(x->ealg->alg_key, key+1, keysize);
1191 }
1192 x->props.ealgo = sa->sadb_sa_encrypt;
1193 }
1194 }
1195 /* x->algo.flags = sa->sadb_sa_flags; */
1196
1197 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1198 &x->props.saddr);
1199 if (!x->props.family) {
1200 err = -EAFNOSUPPORT;
1201 goto out;
1202 }
1203 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1204 &x->id.daddr);
1205
1206 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1207 const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
1208 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1209 if (mode < 0) {
1210 err = -EINVAL;
1211 goto out;
1212 }
1213 x->props.mode = mode;
1214 x->props.reqid = sa2->sadb_x_sa2_reqid;
1215 }
1216
1217 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1218 const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1219
1220 /* Nobody uses this, but we try. */
1221 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1222 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1223 }
1224
1225 if (!x->sel.family)
1226 x->sel.family = x->props.family;
1227
1228 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1229 const struct sadb_x_nat_t_type* n_type;
1230 struct xfrm_encap_tmpl *natt;
1231
1232 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1233 if (!x->encap)
1234 goto out;
1235
1236 natt = x->encap;
1237 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1238 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1239
1240 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1241 const struct sadb_x_nat_t_port *n_port =
1242 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1243 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1244 }
1245 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1246 const struct sadb_x_nat_t_port *n_port =
1247 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1248 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1249 }
1250 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
1251 }
1252
1253 err = xfrm_init_state(x);
1254 if (err)
1255 goto out;
1256
1257 x->km.seq = hdr->sadb_msg_seq;
1258 return x;
1259
1260out:
1261 x->km.state = XFRM_STATE_DEAD;
1262 xfrm_state_put(x);
1263 return ERR_PTR(err);
1264}
1265
1266static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1267{
1268 return -EOPNOTSUPP;
1269}
1270
1271static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1272{
1273 struct net *net = sock_net(sk);
1274 struct sk_buff *resp_skb;
1275 struct sadb_x_sa2 *sa2;
1276 struct sadb_address *saddr, *daddr;
1277 struct sadb_msg *out_hdr;
1278 struct sadb_spirange *range;
1279 struct xfrm_state *x = NULL;
1280 int mode;
1281 int err;
1282 u32 min_spi, max_spi;
1283 u32 reqid;
1284 u8 proto;
1285 unsigned short family;
1286 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1287
1288 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1289 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1290 return -EINVAL;
1291
1292 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1293 if (proto == 0)
1294 return -EINVAL;
1295
1296 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1297 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1298 if (mode < 0)
1299 return -EINVAL;
1300 reqid = sa2->sadb_x_sa2_reqid;
1301 } else {
1302 mode = 0;
1303 reqid = 0;
1304 }
1305
1306 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1307 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1308
1309 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1310 switch (family) {
1311 case AF_INET:
1312 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1313 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1314 break;
1315#if IS_ENABLED(CONFIG_IPV6)
1316 case AF_INET6:
1317 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1318 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1319 break;
1320#endif
1321 }
1322
1323 if (hdr->sadb_msg_seq) {
1324 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
1325 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1326 xfrm_state_put(x);
1327 x = NULL;
1328 }
1329 }
1330
1331 if (!x)
1332 x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
1333
1334 if (x == NULL)
1335 return -ENOENT;
1336
1337 min_spi = 0x100;
1338 max_spi = 0x0fffffff;
1339
1340 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1341 if (range) {
1342 min_spi = range->sadb_spirange_min;
1343 max_spi = range->sadb_spirange_max;
1344 }
1345
1346 err = xfrm_alloc_spi(x, min_spi, max_spi);
1347 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
1348
1349 if (IS_ERR(resp_skb)) {
1350 xfrm_state_put(x);
1351 return PTR_ERR(resp_skb);
1352 }
1353
1354 out_hdr = (struct sadb_msg *) resp_skb->data;
1355 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1356 out_hdr->sadb_msg_type = SADB_GETSPI;
1357 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1358 out_hdr->sadb_msg_errno = 0;
1359 out_hdr->sadb_msg_reserved = 0;
1360 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1361 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1362
1363 xfrm_state_put(x);
1364
1365 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net);
1366
1367 return 0;
1368}
1369
1370static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1371{
1372 struct net *net = sock_net(sk);
1373 struct xfrm_state *x;
1374
1375 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1376 return -EOPNOTSUPP;
1377
1378 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1379 return 0;
1380
1381 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
1382 if (x == NULL)
1383 return 0;
1384
1385 spin_lock_bh(&x->lock);
1386 if (x->km.state == XFRM_STATE_ACQ) {
1387 x->km.state = XFRM_STATE_ERROR;
1388 wake_up(&net->xfrm.km_waitq);
1389 }
1390 spin_unlock_bh(&x->lock);
1391 xfrm_state_put(x);
1392 return 0;
1393}
1394
1395static inline int event2poltype(int event)
1396{
1397 switch (event) {
1398 case XFRM_MSG_DELPOLICY:
1399 return SADB_X_SPDDELETE;
1400 case XFRM_MSG_NEWPOLICY:
1401 return SADB_X_SPDADD;
1402 case XFRM_MSG_UPDPOLICY:
1403 return SADB_X_SPDUPDATE;
1404 case XFRM_MSG_POLEXPIRE:
1405 // return SADB_X_SPDEXPIRE;
1406 default:
1407 pr_err("pfkey: Unknown policy event %d\n", event);
1408 break;
1409 }
1410
1411 return 0;
1412}
1413
1414static inline int event2keytype(int event)
1415{
1416 switch (event) {
1417 case XFRM_MSG_DELSA:
1418 return SADB_DELETE;
1419 case XFRM_MSG_NEWSA:
1420 return SADB_ADD;
1421 case XFRM_MSG_UPDSA:
1422 return SADB_UPDATE;
1423 case XFRM_MSG_EXPIRE:
1424 return SADB_EXPIRE;
1425 default:
1426 pr_err("pfkey: Unknown SA event %d\n", event);
1427 break;
1428 }
1429
1430 return 0;
1431}
1432
1433/* ADD/UPD/DEL */
1434static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
1435{
1436 struct sk_buff *skb;
1437 struct sadb_msg *hdr;
1438
1439 skb = pfkey_xfrm_state2msg(x);
1440
1441 if (IS_ERR(skb))
1442 return PTR_ERR(skb);
1443
1444 hdr = (struct sadb_msg *) skb->data;
1445 hdr->sadb_msg_version = PF_KEY_V2;
1446 hdr->sadb_msg_type = event2keytype(c->event);
1447 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1448 hdr->sadb_msg_errno = 0;
1449 hdr->sadb_msg_reserved = 0;
1450 hdr->sadb_msg_seq = c->seq;
1451 hdr->sadb_msg_pid = c->pid;
1452
1453 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x));
1454
1455 return 0;
1456}
1457
1458static int pfkey_add(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1459{
1460 struct net *net = sock_net(sk);
1461 struct xfrm_state *x;
1462 int err;
1463 struct km_event c;
1464
1465 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
1466 if (IS_ERR(x))
1467 return PTR_ERR(x);
1468
1469 xfrm_state_hold(x);
1470 if (hdr->sadb_msg_type == SADB_ADD)
1471 err = xfrm_state_add(x);
1472 else
1473 err = xfrm_state_update(x);
1474
1475 xfrm_audit_state_add(x, err ? 0 : 1,
1476 audit_get_loginuid(current),
1477 audit_get_sessionid(current), 0);
1478
1479 if (err < 0) {
1480 x->km.state = XFRM_STATE_DEAD;
1481 __xfrm_state_put(x);
1482 goto out;
1483 }
1484
1485 if (hdr->sadb_msg_type == SADB_ADD)
1486 c.event = XFRM_MSG_NEWSA;
1487 else
1488 c.event = XFRM_MSG_UPDSA;
1489 c.seq = hdr->sadb_msg_seq;
1490 c.pid = hdr->sadb_msg_pid;
1491 km_state_notify(x, &c);
1492out:
1493 xfrm_state_put(x);
1494 return err;
1495}
1496
1497static int pfkey_delete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1498{
1499 struct net *net = sock_net(sk);
1500 struct xfrm_state *x;
1501 struct km_event c;
1502 int err;
1503
1504 if (!ext_hdrs[SADB_EXT_SA-1] ||
1505 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1506 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1507 return -EINVAL;
1508
1509 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
1510 if (x == NULL)
1511 return -ESRCH;
1512
1513 if ((err = security_xfrm_state_delete(x)))
1514 goto out;
1515
1516 if (xfrm_state_kern(x)) {
1517 err = -EPERM;
1518 goto out;
1519 }
1520
1521 err = xfrm_state_delete(x);
1522
1523 if (err < 0)
1524 goto out;
1525
1526 c.seq = hdr->sadb_msg_seq;
1527 c.pid = hdr->sadb_msg_pid;
1528 c.event = XFRM_MSG_DELSA;
1529 km_state_notify(x, &c);
1530out:
1531 xfrm_audit_state_delete(x, err ? 0 : 1,
1532 audit_get_loginuid(current),
1533 audit_get_sessionid(current), 0);
1534 xfrm_state_put(x);
1535
1536 return err;
1537}
1538
1539static int pfkey_get(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1540{
1541 struct net *net = sock_net(sk);
1542 __u8 proto;
1543 struct sk_buff *out_skb;
1544 struct sadb_msg *out_hdr;
1545 struct xfrm_state *x;
1546
1547 if (!ext_hdrs[SADB_EXT_SA-1] ||
1548 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1549 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1550 return -EINVAL;
1551
1552 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
1553 if (x == NULL)
1554 return -ESRCH;
1555
1556 out_skb = pfkey_xfrm_state2msg(x);
1557 proto = x->id.proto;
1558 xfrm_state_put(x);
1559 if (IS_ERR(out_skb))
1560 return PTR_ERR(out_skb);
1561
1562 out_hdr = (struct sadb_msg *) out_skb->data;
1563 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1564 out_hdr->sadb_msg_type = SADB_GET;
1565 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1566 out_hdr->sadb_msg_errno = 0;
1567 out_hdr->sadb_msg_reserved = 0;
1568 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1569 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1570 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
1571
1572 return 0;
1573}
1574
1575static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
1576 gfp_t allocation)
1577{
1578 struct sk_buff *skb;
1579 struct sadb_msg *hdr;
1580 int len, auth_len, enc_len, i;
1581
1582 auth_len = xfrm_count_auth_supported();
1583 if (auth_len) {
1584 auth_len *= sizeof(struct sadb_alg);
1585 auth_len += sizeof(struct sadb_supported);
1586 }
1587
1588 enc_len = xfrm_count_enc_supported();
1589 if (enc_len) {
1590 enc_len *= sizeof(struct sadb_alg);
1591 enc_len += sizeof(struct sadb_supported);
1592 }
1593
1594 len = enc_len + auth_len + sizeof(struct sadb_msg);
1595
1596 skb = alloc_skb(len + 16, allocation);
1597 if (!skb)
1598 goto out_put_algs;
1599
1600 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1601 pfkey_hdr_dup(hdr, orig);
1602 hdr->sadb_msg_errno = 0;
1603 hdr->sadb_msg_len = len / sizeof(uint64_t);
1604
1605 if (auth_len) {
1606 struct sadb_supported *sp;
1607 struct sadb_alg *ap;
1608
1609 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1610 ap = (struct sadb_alg *) (sp + 1);
1611
1612 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1613 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1614
1615 for (i = 0; ; i++) {
1616 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1617 if (!aalg)
1618 break;
1619 if (aalg->available)
1620 *ap++ = aalg->desc;
1621 }
1622 }
1623
1624 if (enc_len) {
1625 struct sadb_supported *sp;
1626 struct sadb_alg *ap;
1627
1628 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1629 ap = (struct sadb_alg *) (sp + 1);
1630
1631 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1632 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1633
1634 for (i = 0; ; i++) {
1635 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1636 if (!ealg)
1637 break;
1638 if (ealg->available)
1639 *ap++ = ealg->desc;
1640 }
1641 }
1642
1643out_put_algs:
1644 return skb;
1645}
1646
1647static int pfkey_register(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1648{
1649 struct pfkey_sock *pfk = pfkey_sk(sk);
1650 struct sk_buff *supp_skb;
1651
1652 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1653 return -EINVAL;
1654
1655 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1656 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1657 return -EEXIST;
1658 pfk->registered |= (1<<hdr->sadb_msg_satype);
1659 }
1660#ifdef CVE_SECURITY
1661 mutex_lock(&pfkey_mutex);//CVE-2022-3028
1662#endif
1663 xfrm_probe_algs();
1664
1665 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL | __GFP_ZERO); //* CVE-2022-1353 *
1666#ifdef CVE_SECURITY
1667 mutex_lock(&pfkey_mutex);//CVE-2022-3028
1668#endif
1669 if (!supp_skb) {
1670 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1671 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1672
1673 return -ENOBUFS;
1674 }
1675
1676 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk, sock_net(sk));
1677
1678 return 0;
1679}
1680
1681static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
1682{
1683 struct sk_buff *skb;
1684 struct sadb_msg *hdr;
1685
1686 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1687 if (!skb)
1688 return -ENOBUFS;
1689
1690 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1691 memcpy(hdr, ihdr, sizeof(struct sadb_msg));
1692 hdr->sadb_msg_errno = (uint8_t) 0;
1693 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1694
1695 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
1696}
1697
1698static int key_notify_sa_flush(const struct km_event *c)
1699{
1700 struct sk_buff *skb;
1701 struct sadb_msg *hdr;
1702
1703 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1704 if (!skb)
1705 return -ENOBUFS;
1706 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1707 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
1708 hdr->sadb_msg_type = SADB_FLUSH;
1709 hdr->sadb_msg_seq = c->seq;
1710 hdr->sadb_msg_pid = c->pid;
1711 hdr->sadb_msg_version = PF_KEY_V2;
1712 hdr->sadb_msg_errno = (uint8_t) 0;
1713 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1714 hdr->sadb_msg_reserved = 0;
1715
1716 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
1717
1718 return 0;
1719}
1720
1721static int pfkey_flush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1722{
1723 struct net *net = sock_net(sk);
1724 unsigned proto;
1725 struct km_event c;
1726 struct xfrm_audit audit_info;
1727 int err, err2;
1728
1729 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1730 if (proto == 0)
1731 return -EINVAL;
1732
1733 audit_info.loginuid = audit_get_loginuid(current);
1734 audit_info.sessionid = audit_get_sessionid(current);
1735 audit_info.secid = 0;
1736 err = xfrm_state_flush(net, proto, &audit_info);
1737 err2 = unicast_flush_resp(sk, hdr);
1738 if (err || err2) {
1739 if (err == -ESRCH) /* empty table - go quietly */
1740 err = 0;
1741 return err ? err : err2;
1742 }
1743
1744 c.data.proto = proto;
1745 c.seq = hdr->sadb_msg_seq;
1746 c.pid = hdr->sadb_msg_pid;
1747 c.event = XFRM_MSG_FLUSHSA;
1748 c.net = net;
1749 km_state_notify(NULL, &c);
1750
1751 return 0;
1752}
1753
1754static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1755{
1756 struct pfkey_sock *pfk = ptr;
1757 struct sk_buff *out_skb;
1758 struct sadb_msg *out_hdr;
1759
1760 if (!pfkey_can_dump(&pfk->sk))
1761 return -ENOBUFS;
1762
1763 out_skb = pfkey_xfrm_state2msg(x);
1764 if (IS_ERR(out_skb))
1765 return PTR_ERR(out_skb);
1766
1767 out_hdr = (struct sadb_msg *) out_skb->data;
1768 out_hdr->sadb_msg_version = pfk->dump.msg_version;
1769 out_hdr->sadb_msg_type = SADB_DUMP;
1770 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1771 out_hdr->sadb_msg_errno = 0;
1772 out_hdr->sadb_msg_reserved = 0;
1773 out_hdr->sadb_msg_seq = count + 1;
1774 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
1775
1776 if (pfk->dump.skb)
1777 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
1778 &pfk->sk, sock_net(&pfk->sk));
1779 pfk->dump.skb = out_skb;
1780
1781 return 0;
1782}
1783
1784static int pfkey_dump_sa(struct pfkey_sock *pfk)
1785{
1786 struct net *net = sock_net(&pfk->sk);
1787 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
1788}
1789
1790static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1791{
1792 xfrm_state_walk_done(&pfk->dump.u.state);
1793}
1794
1795static int pfkey_dump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1796{
1797 u8 proto;
1798 struct pfkey_sock *pfk = pfkey_sk(sk);
1799
1800 if (pfk->dump.dump != NULL)
1801 return -EBUSY;
1802
1803 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1804 if (proto == 0)
1805 return -EINVAL;
1806
1807 pfk->dump.msg_version = hdr->sadb_msg_version;
1808 pfk->dump.msg_pid = hdr->sadb_msg_pid;
1809 pfk->dump.dump = pfkey_dump_sa;
1810 pfk->dump.done = pfkey_dump_sa_done;
1811 xfrm_state_walk_init(&pfk->dump.u.state, proto);
1812
1813 return pfkey_do_dump(pfk);
1814}
1815
1816static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1817{
1818 struct pfkey_sock *pfk = pfkey_sk(sk);
1819 int satype = hdr->sadb_msg_satype;
1820 bool reset_errno = false;
1821
1822 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1823 reset_errno = true;
1824 if (satype != 0 && satype != 1)
1825 return -EINVAL;
1826 pfk->promisc = satype;
1827 }
1828 if (reset_errno && skb_cloned(skb))
1829 skb = skb_copy(skb, GFP_KERNEL);
1830 else
1831 skb = skb_clone(skb, GFP_KERNEL);
1832
1833 if (reset_errno && skb) {
1834 struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
1835 new_hdr->sadb_msg_errno = 0;
1836 }
1837
1838 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk));
1839 return 0;
1840}
1841
1842static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1843{
1844 int i;
1845 u32 reqid = *(u32*)ptr;
1846
1847 for (i=0; i<xp->xfrm_nr; i++) {
1848 if (xp->xfrm_vec[i].reqid == reqid)
1849 return -EEXIST;
1850 }
1851 return 0;
1852}
1853
1854static u32 gen_reqid(struct net *net)
1855{
1856 struct xfrm_policy_walk walk;
1857 u32 start;
1858 int rc;
1859 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1860
1861 start = reqid;
1862 do {
1863 ++reqid;
1864 if (reqid == 0)
1865 reqid = IPSEC_MANUAL_REQID_MAX+1;
1866 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
1867 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
1868 xfrm_policy_walk_done(&walk);
1869 if (rc != -EEXIST)
1870 return reqid;
1871 } while (reqid != start);
1872 return 0;
1873}
1874
1875static int
1876parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1877{
1878 struct net *net = xp_net(xp);
1879 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1880 int mode;
1881
1882 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1883 return -ELOOP;
1884
1885 if (rq->sadb_x_ipsecrequest_mode == 0)
1886 return -EINVAL;
1887//CVE-2019-25045
1888 switch (rq->sadb_x_ipsecrequest_proto) {
1889 case IPPROTO_AH:
1890 case IPPROTO_ESP:
1891 case IPPROTO_COMP:
1892#if IS_ENABLED(CONFIG_IPV6)
1893 case IPPROTO_ROUTING:
1894 case IPPROTO_DSTOPTS:
1895#endif
1896 break;
1897 default:
1898 return -EINVAL;
1899 }
1900//CVE-2019-25045
1901
1902 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1903 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1904 return -EINVAL;
1905 t->mode = mode;
1906 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1907 t->optional = 1;
1908 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1909 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1910 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1911 t->reqid = 0;
1912 if (!t->reqid && !(t->reqid = gen_reqid(net)))
1913 return -ENOBUFS;
1914 }
1915
1916 /* addresses present only in tunnel mode */
1917 if (t->mode == XFRM_MODE_TUNNEL) {
1918 u8 *sa = (u8 *) (rq + 1);
1919 int family, socklen;
1920
1921 family = pfkey_sockaddr_extract((struct sockaddr *)sa,
1922 &t->saddr);
1923 if (!family)
1924 return -EINVAL;
1925
1926 socklen = pfkey_sockaddr_len(family);
1927 if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen),
1928 &t->id.daddr) != family)
1929 return -EINVAL;
1930 t->encap_family = family;
1931 } else
1932 t->encap_family = xp->family;
1933
1934 /* No way to set this via kame pfkey */
1935 t->allalgs = 1;
1936 xp->xfrm_nr++;
1937 return 0;
1938}
1939
1940static int
1941parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1942{
1943 int err;
1944 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1945 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1946
1947 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1948 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1949 return err;
1950 len -= rq->sadb_x_ipsecrequest_len;
1951 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1952 }
1953 return 0;
1954}
1955
1956static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
1957{
1958 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1959
1960 if (xfrm_ctx) {
1961 int len = sizeof(struct sadb_x_sec_ctx);
1962 len += xfrm_ctx->ctx_len;
1963 return PFKEY_ALIGN8(len);
1964 }
1965 return 0;
1966}
1967
1968static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
1969{
1970 const struct xfrm_tmpl *t;
1971 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1972 int socklen = 0;
1973 int i;
1974
1975 for (i=0; i<xp->xfrm_nr; i++) {
1976 t = xp->xfrm_vec + i;
1977 socklen += pfkey_sockaddr_len(t->encap_family);
1978 }
1979
1980 return sizeof(struct sadb_msg) +
1981 (sizeof(struct sadb_lifetime) * 3) +
1982 (sizeof(struct sadb_address) * 2) +
1983 (sockaddr_size * 2) +
1984 sizeof(struct sadb_x_policy) +
1985 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1986 (socklen * 2) +
1987 pfkey_xfrm_policy2sec_ctx_size(xp);
1988}
1989
1990static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
1991{
1992 struct sk_buff *skb;
1993 int size;
1994
1995 size = pfkey_xfrm_policy2msg_size(xp);
1996
1997 skb = alloc_skb(size + 16, GFP_ATOMIC);
1998 if (skb == NULL)
1999 return ERR_PTR(-ENOBUFS);
2000
2001 return skb;
2002}
2003
2004static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
2005{
2006 struct sadb_msg *hdr;
2007 struct sadb_address *addr;
2008 struct sadb_lifetime *lifetime;
2009 struct sadb_x_policy *pol;
2010 struct sadb_x_sec_ctx *sec_ctx;
2011 struct xfrm_sec_ctx *xfrm_ctx;
2012 int i;
2013 int size;
2014 int sockaddr_size = pfkey_sockaddr_size(xp->family);
2015 int socklen = pfkey_sockaddr_len(xp->family);
2016
2017 size = pfkey_xfrm_policy2msg_size(xp);
2018
2019 /* call should fill header later */
2020 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2021 memset(hdr, 0, size); /* XXX do we need this ? */
2022
2023 /* src address */
2024 addr = (struct sadb_address*) skb_put(skb,
2025 sizeof(struct sadb_address)+sockaddr_size);
2026 addr->sadb_address_len =
2027 (sizeof(struct sadb_address)+sockaddr_size)/
2028 sizeof(uint64_t);
2029 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2030 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2031 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2032 addr->sadb_address_reserved = 0;
2033 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2034 xp->selector.sport,
2035 (struct sockaddr *) (addr + 1),
2036 xp->family))
2037 BUG();
2038
2039 /* dst address */
2040 addr = (struct sadb_address*) skb_put(skb,
2041 sizeof(struct sadb_address)+sockaddr_size);
2042 addr->sadb_address_len =
2043 (sizeof(struct sadb_address)+sockaddr_size)/
2044 sizeof(uint64_t);
2045 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2046 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2047 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
2048 addr->sadb_address_reserved = 0;
2049
2050 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2051 (struct sockaddr *) (addr + 1),
2052 xp->family);
2053
2054 /* hard time */
2055 lifetime = (struct sadb_lifetime *) skb_put(skb,
2056 sizeof(struct sadb_lifetime));
2057 lifetime->sadb_lifetime_len =
2058 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2059 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2060 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2061 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2062 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2063 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2064 /* soft time */
2065 lifetime = (struct sadb_lifetime *) skb_put(skb,
2066 sizeof(struct sadb_lifetime));
2067 lifetime->sadb_lifetime_len =
2068 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2069 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2070 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2071 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2072 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2073 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2074 /* current time */
2075 lifetime = (struct sadb_lifetime *) skb_put(skb,
2076 sizeof(struct sadb_lifetime));
2077 lifetime->sadb_lifetime_len =
2078 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2079 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2080 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2081 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2082 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2083 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2084
2085 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2086 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2087 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2088 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2089 if (xp->action == XFRM_POLICY_ALLOW) {
2090 if (xp->xfrm_nr)
2091 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2092 else
2093 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2094 }
2095 pol->sadb_x_policy_dir = dir+1;
2096 pol->sadb_x_policy_reserved = 0;
2097 pol->sadb_x_policy_id = xp->index;
2098 pol->sadb_x_policy_priority = xp->priority;
2099
2100 for (i=0; i<xp->xfrm_nr; i++) {
2101 const struct xfrm_tmpl *t = xp->xfrm_vec + i;
2102 struct sadb_x_ipsecrequest *rq;
2103 int req_size;
2104 int mode;
2105
2106 req_size = sizeof(struct sadb_x_ipsecrequest);
2107 if (t->mode == XFRM_MODE_TUNNEL) {
2108 socklen = pfkey_sockaddr_len(t->encap_family);
2109 req_size += socklen * 2;
2110 } else {
2111 size -= 2*socklen;
2112 }
2113 rq = (void*)skb_put(skb, req_size);
2114 pol->sadb_x_policy_len += req_size/8;
2115 memset(rq, 0, sizeof(*rq));
2116 rq->sadb_x_ipsecrequest_len = req_size;
2117 rq->sadb_x_ipsecrequest_proto = t->id.proto;
2118 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2119 return -EINVAL;
2120 rq->sadb_x_ipsecrequest_mode = mode;
2121 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2122 if (t->reqid)
2123 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2124 if (t->optional)
2125 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2126 rq->sadb_x_ipsecrequest_reqid = t->reqid;
2127
2128 if (t->mode == XFRM_MODE_TUNNEL) {
2129 u8 *sa = (void *)(rq + 1);
2130 pfkey_sockaddr_fill(&t->saddr, 0,
2131 (struct sockaddr *)sa,
2132 t->encap_family);
2133 pfkey_sockaddr_fill(&t->id.daddr, 0,
2134 (struct sockaddr *) (sa + socklen),
2135 t->encap_family);
2136 }
2137 }
2138
2139 /* security context */
2140 if ((xfrm_ctx = xp->security)) {
2141 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2142
2143 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2144 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2145 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2146 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2147 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2148 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2149 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2150 xfrm_ctx->ctx_len);
2151 }
2152
2153 hdr->sadb_msg_len = size / sizeof(uint64_t);
2154 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
2155
2156 return 0;
2157}
2158
2159static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2160{
2161 struct sk_buff *out_skb;
2162 struct sadb_msg *out_hdr;
2163 int err;
2164
2165 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2166 if (IS_ERR(out_skb))
2167 return PTR_ERR(out_skb);
2168
2169 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2170 if (err < 0)
2171 return err;
2172
2173 out_hdr = (struct sadb_msg *) out_skb->data;
2174 out_hdr->sadb_msg_version = PF_KEY_V2;
2175
2176 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
2177 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2178 else
2179 out_hdr->sadb_msg_type = event2poltype(c->event);
2180 out_hdr->sadb_msg_errno = 0;
2181 out_hdr->sadb_msg_seq = c->seq;
2182 out_hdr->sadb_msg_pid = c->pid;
2183 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp));
2184 return 0;
2185
2186}
2187
2188static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2189{
2190 struct net *net = sock_net(sk);
2191 int err = 0;
2192 struct sadb_lifetime *lifetime;
2193 struct sadb_address *sa;
2194 struct sadb_x_policy *pol;
2195 struct xfrm_policy *xp;
2196 struct km_event c;
2197 struct sadb_x_sec_ctx *sec_ctx;
2198
2199 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2200 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2201 !ext_hdrs[SADB_X_EXT_POLICY-1])
2202 return -EINVAL;
2203
2204 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2205 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2206 return -EINVAL;
2207 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2208 return -EINVAL;
2209
2210 xp = xfrm_policy_alloc(net, GFP_KERNEL);
2211 if (xp == NULL)
2212 return -ENOBUFS;
2213
2214 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2215 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2216 xp->priority = pol->sadb_x_policy_priority;
2217
2218 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2219 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2220 if (!xp->family) {
2221 err = -EINVAL;
2222 goto out;
2223 }
2224 xp->selector.family = xp->family;
2225 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2226 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2227 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2228 if (xp->selector.sport)
2229 xp->selector.sport_mask = htons(0xffff);
2230
2231 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2232 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2233 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2234
2235 /* Amusing, we set this twice. KAME apps appear to set same value
2236 * in both addresses.
2237 */
2238 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2239
2240 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2241 if (xp->selector.dport)
2242 xp->selector.dport_mask = htons(0xffff);
2243
2244 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
2245 if (sec_ctx != NULL) {
2246 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2247
2248 if (!uctx) {
2249 err = -ENOBUFS;
2250 goto out;
2251 }
2252
2253 err = security_xfrm_policy_alloc(&xp->security, uctx);
2254 kfree(uctx);
2255
2256 if (err)
2257 goto out;
2258 }
2259
2260 xp->lft.soft_byte_limit = XFRM_INF;
2261 xp->lft.hard_byte_limit = XFRM_INF;
2262 xp->lft.soft_packet_limit = XFRM_INF;
2263 xp->lft.hard_packet_limit = XFRM_INF;
2264 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2265 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2266 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2267 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2268 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2269 }
2270 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2271 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2272 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2273 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2274 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2275 }
2276 xp->xfrm_nr = 0;
2277 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2278 (err = parse_ipsecrequests(xp, pol)) < 0)
2279 goto out;
2280
2281 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2282 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2283
2284 xfrm_audit_policy_add(xp, err ? 0 : 1,
2285 audit_get_loginuid(current),
2286 audit_get_sessionid(current), 0);
2287
2288 if (err)
2289 goto out;
2290
2291 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2292 c.event = XFRM_MSG_UPDPOLICY;
2293 else
2294 c.event = XFRM_MSG_NEWPOLICY;
2295
2296 c.seq = hdr->sadb_msg_seq;
2297 c.pid = hdr->sadb_msg_pid;
2298
2299 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2300 xfrm_pol_put(xp);
2301 return 0;
2302
2303out:
2304 xp->walk.dead = 1;
2305 xfrm_policy_destroy(xp);
2306 return err;
2307}
2308
2309static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2310{
2311 struct net *net = sock_net(sk);
2312 int err;
2313 struct sadb_address *sa;
2314 struct sadb_x_policy *pol;
2315 struct xfrm_policy *xp;
2316 struct xfrm_selector sel;
2317 struct km_event c;
2318 struct sadb_x_sec_ctx *sec_ctx;
2319 struct xfrm_sec_ctx *pol_ctx = NULL;
2320
2321 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2322 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2323 !ext_hdrs[SADB_X_EXT_POLICY-1])
2324 return -EINVAL;
2325
2326 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2327 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2328 return -EINVAL;
2329
2330 memset(&sel, 0, sizeof(sel));
2331
2332 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2333 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2334 sel.prefixlen_s = sa->sadb_address_prefixlen;
2335 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2336 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2337 if (sel.sport)
2338 sel.sport_mask = htons(0xffff);
2339
2340 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2341 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2342 sel.prefixlen_d = sa->sadb_address_prefixlen;
2343 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2344 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2345 if (sel.dport)
2346 sel.dport_mask = htons(0xffff);
2347
2348 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
2349 if (sec_ctx != NULL) {
2350 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2351
2352 if (!uctx)
2353 return -ENOMEM;
2354
2355 err = security_xfrm_policy_alloc(&pol_ctx, uctx);
2356 kfree(uctx);
2357 if (err)
2358 return err;
2359 }
2360
2361 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
2362 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2363 1, &err);
2364 security_xfrm_policy_free(pol_ctx);
2365 if (xp == NULL)
2366 return -ENOENT;
2367
2368 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2369 audit_get_loginuid(current),
2370 audit_get_sessionid(current), 0);
2371
2372 if (err)
2373 goto out;
2374
2375 c.seq = hdr->sadb_msg_seq;
2376 c.pid = hdr->sadb_msg_pid;
2377 c.data.byid = 0;
2378 c.event = XFRM_MSG_DELPOLICY;
2379 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2380
2381out:
2382 xfrm_pol_put(xp);
2383 return err;
2384}
2385
2386static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, const struct sadb_msg *hdr, int dir)
2387{
2388 int err;
2389 struct sk_buff *out_skb;
2390 struct sadb_msg *out_hdr;
2391 err = 0;
2392
2393 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2394 if (IS_ERR(out_skb)) {
2395 err = PTR_ERR(out_skb);
2396 goto out;
2397 }
2398 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2399 if (err < 0)
2400 goto out;
2401
2402 out_hdr = (struct sadb_msg *) out_skb->data;
2403 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2404 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2405 out_hdr->sadb_msg_satype = 0;
2406 out_hdr->sadb_msg_errno = 0;
2407 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2408 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2409 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp));
2410 err = 0;
2411
2412out:
2413 return err;
2414}
2415
2416#ifdef CONFIG_NET_KEY_MIGRATE
2417static int pfkey_sockaddr_pair_size(sa_family_t family)
2418{
2419 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
2420}
2421
2422static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
2423 xfrm_address_t *saddr, xfrm_address_t *daddr,
2424 u16 *family)
2425{
2426 int af, socklen;
2427
2428 if (ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
2429 return -EINVAL;
2430
2431 af = pfkey_sockaddr_extract(sa, saddr);
2432 if (!af)
2433 return -EINVAL;
2434
2435 socklen = pfkey_sockaddr_len(af);
2436 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
2437 daddr) != af)
2438 return -EINVAL;
2439
2440 *family = af;
2441 return 0;
2442}
2443
2444static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2445 struct xfrm_migrate *m)
2446{
2447 int err;
2448 struct sadb_x_ipsecrequest *rq2;
2449 int mode;
2450
2451 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2452 len < rq1->sadb_x_ipsecrequest_len)
2453 return -EINVAL;
2454
2455 /* old endoints */
2456 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
2457 rq1->sadb_x_ipsecrequest_len,
2458 &m->old_saddr, &m->old_daddr,
2459 &m->old_family);
2460 if (err)
2461 return err;
2462
2463 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2464 len -= rq1->sadb_x_ipsecrequest_len;
2465
2466 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2467 len < rq2->sadb_x_ipsecrequest_len)
2468 return -EINVAL;
2469
2470 /* new endpoints */
2471 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
2472 rq2->sadb_x_ipsecrequest_len,
2473 &m->new_saddr, &m->new_daddr,
2474 &m->new_family);
2475 if (err)
2476 return err;
2477
2478 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2479 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2480 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2481 return -EINVAL;
2482
2483 m->proto = rq1->sadb_x_ipsecrequest_proto;
2484 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2485 return -EINVAL;
2486 m->mode = mode;
2487 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2488
2489 return ((int)(rq1->sadb_x_ipsecrequest_len +
2490 rq2->sadb_x_ipsecrequest_len));
2491}
2492
2493static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2494 const struct sadb_msg *hdr, void * const *ext_hdrs)
2495{
2496 int i, len, ret, err = -EINVAL;
2497 u8 dir;
2498 struct sadb_address *sa;
2499 struct sadb_x_kmaddress *kma;
2500 struct sadb_x_policy *pol;
2501 struct sadb_x_ipsecrequest *rq;
2502 struct xfrm_selector sel;
2503 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2504 struct xfrm_kmaddress k;
2505
2506 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2507 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2508 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2509 err = -EINVAL;
2510 goto out;
2511 }
2512
2513 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
2514 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2515
2516 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2517 err = -EINVAL;
2518 goto out;
2519 }
2520
2521 if (kma) {
2522 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2523 k.reserved = kma->sadb_x_kmaddress_reserved;
2524 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2525 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2526 &k.local, &k.remote, &k.family);
2527 if (ret < 0) {
2528 err = ret;
2529 goto out;
2530 }
2531 }
2532
2533 dir = pol->sadb_x_policy_dir - 1;
2534 memset(&sel, 0, sizeof(sel));
2535
2536 /* set source address info of selector */
2537 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2538 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2539 sel.prefixlen_s = sa->sadb_address_prefixlen;
2540 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2541 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2542 if (sel.sport)
2543 sel.sport_mask = htons(0xffff);
2544
2545 /* set destination address info of selector */
2546 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2547 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2548 sel.prefixlen_d = sa->sadb_address_prefixlen;
2549 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2550 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2551 if (sel.dport)
2552 sel.dport_mask = htons(0xffff);
2553
2554 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2555
2556 /* extract ipsecrequests */
2557 i = 0;
2558 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2559
2560 while (len > 0 && i < XFRM_MAX_DEPTH) {
2561 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2562 if (ret < 0) {
2563 err = ret;
2564 goto out;
2565 } else {
2566 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2567 len -= ret;
2568 i++;
2569 }
2570 }
2571
2572 if (!i || len > 0) {
2573 err = -EINVAL;
2574 goto out;
2575 }
2576
2577 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
2578 kma ? &k : NULL);
2579
2580 out:
2581 return err;
2582}
2583#else
2584static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2585 const struct sadb_msg *hdr, void * const *ext_hdrs)
2586{
2587 return -ENOPROTOOPT;
2588}
2589#endif
2590
2591
2592static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2593{
2594 struct net *net = sock_net(sk);
2595 unsigned int dir;
2596 int err = 0, delete;
2597 struct sadb_x_policy *pol;
2598 struct xfrm_policy *xp;
2599 struct km_event c;
2600
2601 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2602 return -EINVAL;
2603
2604 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2605 if (dir >= XFRM_POLICY_MAX)
2606 return -EINVAL;
2607
2608 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2609 xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
2610 dir, pol->sadb_x_policy_id, delete, &err);
2611 if (xp == NULL)
2612 return -ENOENT;
2613
2614 if (delete) {
2615 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2616 audit_get_loginuid(current),
2617 audit_get_sessionid(current), 0);
2618
2619 if (err)
2620 goto out;
2621 c.seq = hdr->sadb_msg_seq;
2622 c.pid = hdr->sadb_msg_pid;
2623 c.data.byid = 1;
2624 c.event = XFRM_MSG_DELPOLICY;
2625 km_policy_notify(xp, dir, &c);
2626 } else {
2627 err = key_pol_get_resp(sk, xp, hdr, dir);
2628 }
2629
2630out:
2631 xfrm_pol_put(xp);
2632 return err;
2633}
2634
2635static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2636{
2637 struct pfkey_sock *pfk = ptr;
2638 struct sk_buff *out_skb;
2639 struct sadb_msg *out_hdr;
2640 int err;
2641
2642 if (!pfkey_can_dump(&pfk->sk))
2643 return -ENOBUFS;
2644
2645 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2646 if (IS_ERR(out_skb))
2647 return PTR_ERR(out_skb);
2648
2649 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2650 if (err < 0)
2651 return err;
2652
2653 out_hdr = (struct sadb_msg *) out_skb->data;
2654 out_hdr->sadb_msg_version = pfk->dump.msg_version;
2655 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2656 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2657 out_hdr->sadb_msg_errno = 0;
2658 out_hdr->sadb_msg_seq = count + 1;
2659 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
2660
2661 if (pfk->dump.skb)
2662 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
2663 &pfk->sk, sock_net(&pfk->sk));
2664 pfk->dump.skb = out_skb;
2665
2666 return 0;
2667}
2668
2669static int pfkey_dump_sp(struct pfkey_sock *pfk)
2670{
2671 struct net *net = sock_net(&pfk->sk);
2672 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
2673}
2674
2675static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2676{
2677 xfrm_policy_walk_done(&pfk->dump.u.policy);
2678}
2679
2680static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2681{
2682 struct pfkey_sock *pfk = pfkey_sk(sk);
2683
2684 if (pfk->dump.dump != NULL)
2685 return -EBUSY;
2686
2687 pfk->dump.msg_version = hdr->sadb_msg_version;
2688 pfk->dump.msg_pid = hdr->sadb_msg_pid;
2689 pfk->dump.dump = pfkey_dump_sp;
2690 pfk->dump.done = pfkey_dump_sp_done;
2691 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2692
2693 return pfkey_do_dump(pfk);
2694}
2695
2696static int key_notify_policy_flush(const struct km_event *c)
2697{
2698 struct sk_buff *skb_out;
2699 struct sadb_msg *hdr;
2700
2701 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
2702 if (!skb_out)
2703 return -ENOBUFS;
2704 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2705 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
2706 hdr->sadb_msg_seq = c->seq;
2707 hdr->sadb_msg_pid = c->pid;
2708 hdr->sadb_msg_version = PF_KEY_V2;
2709 hdr->sadb_msg_errno = (uint8_t) 0;
2710 hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2711 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2712 hdr->sadb_msg_reserved = 0;
2713 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
2714 return 0;
2715
2716}
2717
2718static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2719{
2720 struct net *net = sock_net(sk);
2721 struct km_event c;
2722 struct xfrm_audit audit_info;
2723 int err, err2;
2724
2725 audit_info.loginuid = audit_get_loginuid(current);
2726 audit_info.sessionid = audit_get_sessionid(current);
2727 audit_info.secid = 0;
2728 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
2729 err2 = unicast_flush_resp(sk, hdr);
2730 if (err || err2) {
2731 if (err == -ESRCH) /* empty table - old silent behavior */
2732 return 0;
2733 return err;
2734 }
2735
2736 c.data.type = XFRM_POLICY_TYPE_MAIN;
2737 c.event = XFRM_MSG_FLUSHPOLICY;
2738 c.pid = hdr->sadb_msg_pid;
2739 c.seq = hdr->sadb_msg_seq;
2740 c.net = net;
2741 km_policy_notify(NULL, 0, &c);
2742
2743 return 0;
2744}
2745
2746typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2747 const struct sadb_msg *hdr, void * const *ext_hdrs);
2748static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2749 [SADB_RESERVED] = pfkey_reserved,
2750 [SADB_GETSPI] = pfkey_getspi,
2751 [SADB_UPDATE] = pfkey_add,
2752 [SADB_ADD] = pfkey_add,
2753 [SADB_DELETE] = pfkey_delete,
2754 [SADB_GET] = pfkey_get,
2755 [SADB_ACQUIRE] = pfkey_acquire,
2756 [SADB_REGISTER] = pfkey_register,
2757 [SADB_EXPIRE] = NULL,
2758 [SADB_FLUSH] = pfkey_flush,
2759 [SADB_DUMP] = pfkey_dump,
2760 [SADB_X_PROMISC] = pfkey_promisc,
2761 [SADB_X_PCHANGE] = NULL,
2762 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2763 [SADB_X_SPDADD] = pfkey_spdadd,
2764 [SADB_X_SPDDELETE] = pfkey_spddelete,
2765 [SADB_X_SPDGET] = pfkey_spdget,
2766 [SADB_X_SPDACQUIRE] = NULL,
2767 [SADB_X_SPDDUMP] = pfkey_spddump,
2768 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2769 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2770 [SADB_X_SPDDELETE2] = pfkey_spdget,
2771 [SADB_X_MIGRATE] = pfkey_migrate,
2772};
2773
2774static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
2775{
2776 void *ext_hdrs[SADB_EXT_MAX];
2777 int err;
2778
2779 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2780 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
2781
2782 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2783 err = parse_exthdrs(skb, hdr, ext_hdrs);
2784 if (!err) {
2785 err = -EOPNOTSUPP;
2786 if (pfkey_funcs[hdr->sadb_msg_type])
2787 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2788 }
2789 return err;
2790}
2791
2792static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2793{
2794 struct sadb_msg *hdr = NULL;
2795
2796 if (skb->len < sizeof(*hdr)) {
2797 *errp = -EMSGSIZE;
2798 } else {
2799 hdr = (struct sadb_msg *) skb->data;
2800 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2801 hdr->sadb_msg_reserved != 0 ||
2802 (hdr->sadb_msg_type <= SADB_RESERVED ||
2803 hdr->sadb_msg_type > SADB_MAX)) {
2804 hdr = NULL;
2805 *errp = -EINVAL;
2806 } else if (hdr->sadb_msg_len != (skb->len /
2807 sizeof(uint64_t)) ||
2808 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2809 sizeof(uint64_t))) {
2810 hdr = NULL;
2811 *errp = -EMSGSIZE;
2812 } else {
2813 *errp = 0;
2814 }
2815 }
2816 return hdr;
2817}
2818
2819static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
2820 const struct xfrm_algo_desc *d)
2821{
2822 unsigned int id = d->desc.sadb_alg_id;
2823
2824 if (id >= sizeof(t->aalgos) * 8)
2825 return 0;
2826
2827 return (t->aalgos >> id) & 1;
2828}
2829
2830static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
2831 const struct xfrm_algo_desc *d)
2832{
2833 unsigned int id = d->desc.sadb_alg_id;
2834
2835 if (id >= sizeof(t->ealgos) * 8)
2836 return 0;
2837
2838 return (t->ealgos >> id) & 1;
2839}
2840
2841static int count_ah_combs(const struct xfrm_tmpl *t)
2842{
2843 int i, sz = 0;
2844
2845 for (i = 0; ; i++) {
2846 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2847 if (!aalg)
2848 break;
2849 if (aalg_tmpl_set(t, aalg) && aalg->available)
2850 sz += sizeof(struct sadb_comb);
2851 }
2852 return sz + sizeof(struct sadb_prop);
2853}
2854
2855static int count_esp_combs(const struct xfrm_tmpl *t)
2856{
2857 int i, k, sz = 0;
2858
2859 for (i = 0; ; i++) {
2860 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2861 if (!ealg)
2862 break;
2863
2864 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2865 continue;
2866
2867 for (k = 1; ; k++) {
2868 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2869 if (!aalg)
2870 break;
2871
2872 if (aalg_tmpl_set(t, aalg) && aalg->available)
2873 sz += sizeof(struct sadb_comb);
2874 }
2875 }
2876 return sz + sizeof(struct sadb_prop);
2877}
2878
2879static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
2880{
2881 struct sadb_prop *p;
2882 int i;
2883
2884 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2885 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2886 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2887 p->sadb_prop_replay = 32;
2888 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2889
2890 for (i = 0; ; i++) {
2891 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2892 if (!aalg)
2893 break;
2894
2895 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2896 struct sadb_comb *c;
2897 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2898 memset(c, 0, sizeof(*c));
2899 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2900 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2901 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2902 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2903 c->sadb_comb_hard_addtime = 24*60*60;
2904 c->sadb_comb_soft_addtime = 20*60*60;
2905 c->sadb_comb_hard_usetime = 8*60*60;
2906 c->sadb_comb_soft_usetime = 7*60*60;
2907 }
2908 }
2909}
2910
2911static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
2912{
2913 struct sadb_prop *p;
2914 int i, k;
2915
2916 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2917 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2918 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2919 p->sadb_prop_replay = 32;
2920 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2921
2922 for (i=0; ; i++) {
2923 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2924 if (!ealg)
2925 break;
2926
2927 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2928 continue;
2929
2930 for (k = 1; ; k++) {
2931 struct sadb_comb *c;
2932 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2933 if (!aalg)
2934 break;
2935 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2936 continue;
2937 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2938 memset(c, 0, sizeof(*c));
2939 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2940 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2941 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2942 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2943 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2944 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2945 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2946 c->sadb_comb_hard_addtime = 24*60*60;
2947 c->sadb_comb_soft_addtime = 20*60*60;
2948 c->sadb_comb_hard_usetime = 8*60*60;
2949 c->sadb_comb_soft_usetime = 7*60*60;
2950 }
2951 }
2952}
2953
2954static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
2955{
2956 return 0;
2957}
2958
2959static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
2960{
2961 struct sk_buff *out_skb;
2962 struct sadb_msg *out_hdr;
2963 int hard;
2964 int hsc;
2965
2966 hard = c->data.hard;
2967 if (hard)
2968 hsc = 2;
2969 else
2970 hsc = 1;
2971
2972 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
2973 if (IS_ERR(out_skb))
2974 return PTR_ERR(out_skb);
2975
2976 out_hdr = (struct sadb_msg *) out_skb->data;
2977 out_hdr->sadb_msg_version = PF_KEY_V2;
2978 out_hdr->sadb_msg_type = SADB_EXPIRE;
2979 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2980 out_hdr->sadb_msg_errno = 0;
2981 out_hdr->sadb_msg_reserved = 0;
2982 out_hdr->sadb_msg_seq = 0;
2983 out_hdr->sadb_msg_pid = 0;
2984
2985 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
2986 return 0;
2987}
2988
2989static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
2990{
2991 struct net *net = x ? xs_net(x) : c->net;
2992 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
2993
2994 if (atomic_read(&net_pfkey->socks_nr) == 0)
2995 return 0;
2996
2997 switch (c->event) {
2998 case XFRM_MSG_EXPIRE:
2999 return key_notify_sa_expire(x, c);
3000 case XFRM_MSG_DELSA:
3001 case XFRM_MSG_NEWSA:
3002 case XFRM_MSG_UPDSA:
3003 return key_notify_sa(x, c);
3004 case XFRM_MSG_FLUSHSA:
3005 return key_notify_sa_flush(c);
3006 case XFRM_MSG_NEWAE: /* not yet supported */
3007 break;
3008 default:
3009 pr_err("pfkey: Unknown SA event %d\n", c->event);
3010 break;
3011 }
3012
3013 return 0;
3014}
3015
3016static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3017{
3018 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3019 return 0;
3020
3021 switch (c->event) {
3022 case XFRM_MSG_POLEXPIRE:
3023 return key_notify_policy_expire(xp, c);
3024 case XFRM_MSG_DELPOLICY:
3025 case XFRM_MSG_NEWPOLICY:
3026 case XFRM_MSG_UPDPOLICY:
3027 return key_notify_policy(xp, dir, c);
3028 case XFRM_MSG_FLUSHPOLICY:
3029 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3030 break;
3031 return key_notify_policy_flush(c);
3032 default:
3033 pr_err("pfkey: Unknown policy event %d\n", c->event);
3034 break;
3035 }
3036
3037 return 0;
3038}
3039
3040static u32 get_acqseq(void)
3041{
3042 u32 res;
3043 static atomic_t acqseq;
3044
3045 do {
3046 res = atomic_inc_return(&acqseq);
3047 } while (!res);
3048 return res;
3049}
3050
3051static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
3052{
3053 struct sk_buff *skb;
3054 struct sadb_msg *hdr;
3055 struct sadb_address *addr;
3056 struct sadb_x_policy *pol;
3057 int sockaddr_size;
3058 int size;
3059 struct sadb_x_sec_ctx *sec_ctx;
3060 struct xfrm_sec_ctx *xfrm_ctx;
3061 int ctx_size = 0;
3062
3063 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3064 if (!sockaddr_size)
3065 return -EINVAL;
3066
3067 size = sizeof(struct sadb_msg) +
3068 (sizeof(struct sadb_address) * 2) +
3069 (sockaddr_size * 2) +
3070 sizeof(struct sadb_x_policy);
3071
3072 if (x->id.proto == IPPROTO_AH)
3073 size += count_ah_combs(t);
3074 else if (x->id.proto == IPPROTO_ESP)
3075 size += count_esp_combs(t);
3076
3077 if ((xfrm_ctx = x->security)) {
3078 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3079 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3080 }
3081
3082 skb = alloc_skb(size + 16, GFP_ATOMIC);
3083 if (skb == NULL)
3084 return -ENOMEM;
3085
3086 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3087 hdr->sadb_msg_version = PF_KEY_V2;
3088 hdr->sadb_msg_type = SADB_ACQUIRE;
3089 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3090 hdr->sadb_msg_len = size / sizeof(uint64_t);
3091 hdr->sadb_msg_errno = 0;
3092 hdr->sadb_msg_reserved = 0;
3093 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3094 hdr->sadb_msg_pid = 0;
3095
3096 /* src address */
3097 addr = (struct sadb_address*) skb_put(skb,
3098 sizeof(struct sadb_address)+sockaddr_size);
3099 addr->sadb_address_len =
3100 (sizeof(struct sadb_address)+sockaddr_size)/
3101 sizeof(uint64_t);
3102 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3103 addr->sadb_address_proto = 0;
3104 addr->sadb_address_reserved = 0;
3105 addr->sadb_address_prefixlen =
3106 pfkey_sockaddr_fill(&x->props.saddr, 0,
3107 (struct sockaddr *) (addr + 1),
3108 x->props.family);
3109 if (!addr->sadb_address_prefixlen)
3110 BUG();
3111
3112 /* dst address */
3113 addr = (struct sadb_address*) skb_put(skb,
3114 sizeof(struct sadb_address)+sockaddr_size);
3115 addr->sadb_address_len =
3116 (sizeof(struct sadb_address)+sockaddr_size)/
3117 sizeof(uint64_t);
3118 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3119 addr->sadb_address_proto = 0;
3120 addr->sadb_address_reserved = 0;
3121 addr->sadb_address_prefixlen =
3122 pfkey_sockaddr_fill(&x->id.daddr, 0,
3123 (struct sockaddr *) (addr + 1),
3124 x->props.family);
3125 if (!addr->sadb_address_prefixlen)
3126 BUG();
3127
3128 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3129 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3130 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3131 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3132 pol->sadb_x_policy_dir = dir+1;
3133 pol->sadb_x_policy_reserved = 0;
3134 pol->sadb_x_policy_id = xp->index;
3135 pol->sadb_x_policy_priority = xp->priority;
3136
3137 /* Set sadb_comb's. */
3138 if (x->id.proto == IPPROTO_AH)
3139 dump_ah_combs(skb, t);
3140 else if (x->id.proto == IPPROTO_ESP)
3141 dump_esp_combs(skb, t);
3142
3143 /* security context */
3144 if (xfrm_ctx) {
3145 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3146 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3147 sec_ctx->sadb_x_sec_len =
3148 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3149 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3150 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3151 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3152 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3153 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3154 xfrm_ctx->ctx_len);
3155 }
3156
3157 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
3158}
3159
3160static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
3161 u8 *data, int len, int *dir)
3162{
3163 struct net *net = sock_net(sk);
3164 struct xfrm_policy *xp;
3165 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
3166 struct sadb_x_sec_ctx *sec_ctx;
3167
3168 switch (sk->sk_family) {
3169 case AF_INET:
3170 if (opt != IP_IPSEC_POLICY) {
3171 *dir = -EOPNOTSUPP;
3172 return NULL;
3173 }
3174 break;
3175#if IS_ENABLED(CONFIG_IPV6)
3176 case AF_INET6:
3177 if (opt != IPV6_IPSEC_POLICY) {
3178 *dir = -EOPNOTSUPP;
3179 return NULL;
3180 }
3181 break;
3182#endif
3183 default:
3184 *dir = -EINVAL;
3185 return NULL;
3186 }
3187
3188 *dir = -EINVAL;
3189
3190 if (len < sizeof(struct sadb_x_policy) ||
3191 pol->sadb_x_policy_len*8 > len ||
3192 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3193 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3194 return NULL;
3195
3196 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3197 if (xp == NULL) {
3198 *dir = -ENOBUFS;
3199 return NULL;
3200 }
3201
3202 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3203 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3204
3205 xp->lft.soft_byte_limit = XFRM_INF;
3206 xp->lft.hard_byte_limit = XFRM_INF;
3207 xp->lft.soft_packet_limit = XFRM_INF;
3208 xp->lft.hard_packet_limit = XFRM_INF;
3209 xp->family = sk->sk_family;
3210
3211 xp->xfrm_nr = 0;
3212 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3213 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3214 goto out;
3215
3216 /* security context too */
3217 if (len >= (pol->sadb_x_policy_len*8 +
3218 sizeof(struct sadb_x_sec_ctx))) {
3219 char *p = (char *)pol;
3220 struct xfrm_user_sec_ctx *uctx;
3221
3222 p += pol->sadb_x_policy_len*8;
3223 sec_ctx = (struct sadb_x_sec_ctx *)p;
3224 if (len < pol->sadb_x_policy_len*8 +
3225 sec_ctx->sadb_x_sec_len) {
3226 *dir = -EINVAL;
3227 goto out;
3228 }
3229 if ((*dir = verify_sec_ctx_len(p)))
3230 goto out;
3231 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
3232 *dir = security_xfrm_policy_alloc(&xp->security, uctx);
3233 kfree(uctx);
3234
3235 if (*dir)
3236 goto out;
3237 }
3238
3239 *dir = pol->sadb_x_policy_dir-1;
3240 return xp;
3241
3242out:
3243 xp->walk.dead = 1;
3244 xfrm_policy_destroy(xp);
3245 return NULL;
3246}
3247
3248static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
3249{
3250 struct sk_buff *skb;
3251 struct sadb_msg *hdr;
3252 struct sadb_sa *sa;
3253 struct sadb_address *addr;
3254 struct sadb_x_nat_t_port *n_port;
3255 int sockaddr_size;
3256 int size;
3257 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3258 struct xfrm_encap_tmpl *natt = NULL;
3259
3260 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3261 if (!sockaddr_size)
3262 return -EINVAL;
3263
3264 if (!satype)
3265 return -EINVAL;
3266
3267 if (!x->encap)
3268 return -EINVAL;
3269
3270 natt = x->encap;
3271
3272 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3273 *
3274 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3275 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3276 */
3277
3278 size = sizeof(struct sadb_msg) +
3279 sizeof(struct sadb_sa) +
3280 (sizeof(struct sadb_address) * 2) +
3281 (sockaddr_size * 2) +
3282 (sizeof(struct sadb_x_nat_t_port) * 2);
3283
3284 skb = alloc_skb(size + 16, GFP_ATOMIC);
3285 if (skb == NULL)
3286 return -ENOMEM;
3287
3288 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3289 hdr->sadb_msg_version = PF_KEY_V2;
3290 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3291 hdr->sadb_msg_satype = satype;
3292 hdr->sadb_msg_len = size / sizeof(uint64_t);
3293 hdr->sadb_msg_errno = 0;
3294 hdr->sadb_msg_reserved = 0;
3295 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3296 hdr->sadb_msg_pid = 0;
3297
3298 /* SA */
3299 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3300 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3301 sa->sadb_sa_exttype = SADB_EXT_SA;
3302 sa->sadb_sa_spi = x->id.spi;
3303 sa->sadb_sa_replay = 0;
3304 sa->sadb_sa_state = 0;
3305 sa->sadb_sa_auth = 0;
3306 sa->sadb_sa_encrypt = 0;
3307 sa->sadb_sa_flags = 0;
3308
3309 /* ADDRESS_SRC (old addr) */
3310 addr = (struct sadb_address*)
3311 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3312 addr->sadb_address_len =
3313 (sizeof(struct sadb_address)+sockaddr_size)/
3314 sizeof(uint64_t);
3315 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3316 addr->sadb_address_proto = 0;
3317 addr->sadb_address_reserved = 0;
3318 addr->sadb_address_prefixlen =
3319 pfkey_sockaddr_fill(&x->props.saddr, 0,
3320 (struct sockaddr *) (addr + 1),
3321 x->props.family);
3322 if (!addr->sadb_address_prefixlen)
3323 BUG();
3324
3325 /* NAT_T_SPORT (old port) */
3326 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3327 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3328 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3329 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3330 n_port->sadb_x_nat_t_port_reserved = 0;
3331
3332 /* ADDRESS_DST (new addr) */
3333 addr = (struct sadb_address*)
3334 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3335 addr->sadb_address_len =
3336 (sizeof(struct sadb_address)+sockaddr_size)/
3337 sizeof(uint64_t);
3338 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3339 addr->sadb_address_proto = 0;
3340 addr->sadb_address_reserved = 0;
3341 addr->sadb_address_prefixlen =
3342 pfkey_sockaddr_fill(ipaddr, 0,
3343 (struct sockaddr *) (addr + 1),
3344 x->props.family);
3345 if (!addr->sadb_address_prefixlen)
3346 BUG();
3347
3348 /* NAT_T_DPORT (new port) */
3349 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3350 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3351 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3352 n_port->sadb_x_nat_t_port_port = sport;
3353 n_port->sadb_x_nat_t_port_reserved = 0;
3354
3355 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
3356}
3357
3358#ifdef CONFIG_NET_KEY_MIGRATE
3359static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3360 const struct xfrm_selector *sel)
3361{
3362 struct sadb_address *addr;
3363 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3364 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3365 addr->sadb_address_exttype = type;
3366 addr->sadb_address_proto = sel->proto;
3367 addr->sadb_address_reserved = 0;
3368
3369 switch (type) {
3370 case SADB_EXT_ADDRESS_SRC:
3371 addr->sadb_address_prefixlen = sel->prefixlen_s;
3372 pfkey_sockaddr_fill(&sel->saddr, 0,
3373 (struct sockaddr *)(addr + 1),
3374 sel->family);
3375 break;
3376 case SADB_EXT_ADDRESS_DST:
3377 addr->sadb_address_prefixlen = sel->prefixlen_d;
3378 pfkey_sockaddr_fill(&sel->daddr, 0,
3379 (struct sockaddr *)(addr + 1),
3380 sel->family);
3381 break;
3382 default:
3383 return -EINVAL;
3384 }
3385
3386 return 0;
3387}
3388
3389
3390static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
3391{
3392 struct sadb_x_kmaddress *kma;
3393 u8 *sa;
3394 int family = k->family;
3395 int socklen = pfkey_sockaddr_len(family);
3396 int size_req;
3397
3398 size_req = (sizeof(struct sadb_x_kmaddress) +
3399 pfkey_sockaddr_pair_size(family));
3400
3401 kma = (struct sadb_x_kmaddress *)skb_put(skb, size_req);
3402 memset(kma, 0, size_req);
3403 kma->sadb_x_kmaddress_len = size_req / 8;
3404 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3405 kma->sadb_x_kmaddress_reserved = k->reserved;
3406
3407 sa = (u8 *)(kma + 1);
3408 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3409 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3410 return -EINVAL;
3411
3412 return 0;
3413}
3414
3415static int set_ipsecrequest(struct sk_buff *skb,
3416 uint8_t proto, uint8_t mode, int level,
3417 uint32_t reqid, uint8_t family,
3418 const xfrm_address_t *src, const xfrm_address_t *dst)
3419{
3420 struct sadb_x_ipsecrequest *rq;
3421 u8 *sa;
3422 int socklen = pfkey_sockaddr_len(family);
3423 int size_req;
3424
3425 size_req = sizeof(struct sadb_x_ipsecrequest) +
3426 pfkey_sockaddr_pair_size(family);
3427
3428 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3429 memset(rq, 0, size_req);
3430 rq->sadb_x_ipsecrequest_len = size_req;
3431 rq->sadb_x_ipsecrequest_proto = proto;
3432 rq->sadb_x_ipsecrequest_mode = mode;
3433 rq->sadb_x_ipsecrequest_level = level;
3434 rq->sadb_x_ipsecrequest_reqid = reqid;
3435
3436 sa = (u8 *) (rq + 1);
3437 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3438 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
3439 return -EINVAL;
3440
3441 return 0;
3442}
3443#endif
3444
3445#ifdef CONFIG_NET_KEY_MIGRATE
3446static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3447 const struct xfrm_migrate *m, int num_bundles,
3448 const struct xfrm_kmaddress *k)
3449{
3450 int i;
3451 int sasize_sel;
3452 int size = 0;
3453 int size_pol = 0;
3454 struct sk_buff *skb;
3455 struct sadb_msg *hdr;
3456 struct sadb_x_policy *pol;
3457 const struct xfrm_migrate *mp;
3458
3459 if (type != XFRM_POLICY_TYPE_MAIN)
3460 return 0;
3461
3462 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3463 return -EINVAL;
3464
3465 if (k != NULL) {
3466 /* addresses for KM */
3467 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3468 pfkey_sockaddr_pair_size(k->family));
3469 }
3470
3471 /* selector */
3472 sasize_sel = pfkey_sockaddr_size(sel->family);
3473 if (!sasize_sel)
3474 return -EINVAL;
3475 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3476
3477 /* policy info */
3478 size_pol += sizeof(struct sadb_x_policy);
3479
3480 /* ipsecrequests */
3481 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3482 /* old locator pair */
3483 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3484 pfkey_sockaddr_pair_size(mp->old_family);
3485 /* new locator pair */
3486 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3487 pfkey_sockaddr_pair_size(mp->new_family);
3488 }
3489
3490 size += sizeof(struct sadb_msg) + size_pol;
3491
3492 /* alloc buffer */
3493 skb = alloc_skb(size, GFP_ATOMIC);
3494 if (skb == NULL)
3495 return -ENOMEM;
3496
3497 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3498 hdr->sadb_msg_version = PF_KEY_V2;
3499 hdr->sadb_msg_type = SADB_X_MIGRATE;
3500 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3501 hdr->sadb_msg_len = size / 8;
3502 hdr->sadb_msg_errno = 0;
3503 hdr->sadb_msg_reserved = 0;
3504 hdr->sadb_msg_seq = 0;
3505 hdr->sadb_msg_pid = 0;
3506
3507 /* Addresses to be used by KM for negotiation, if ext is available */
3508 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
3509 goto err;
3510
3511 /* selector src */
3512 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3513
3514 /* selector dst */
3515 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3516
3517 /* policy information */
3518 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3519 pol->sadb_x_policy_len = size_pol / 8;
3520 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3521 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3522 pol->sadb_x_policy_dir = dir + 1;
3523 pol->sadb_x_policy_reserved = 0;
3524 pol->sadb_x_policy_id = 0;
3525 pol->sadb_x_policy_priority = 0;
3526
3527 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3528 /* old ipsecrequest */
3529 int mode = pfkey_mode_from_xfrm(mp->mode);
3530 if (mode < 0)
3531 goto err;
3532 if (set_ipsecrequest(skb, mp->proto, mode,
3533 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3534 mp->reqid, mp->old_family,
3535 &mp->old_saddr, &mp->old_daddr) < 0)
3536 goto err;
3537
3538 /* new ipsecrequest */
3539 if (set_ipsecrequest(skb, mp->proto, mode,
3540 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3541 mp->reqid, mp->new_family,
3542 &mp->new_saddr, &mp->new_daddr) < 0)
3543 goto err;
3544 }
3545
3546 /* broadcast migrate message to sockets */
3547 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net);
3548
3549 return 0;
3550
3551err:
3552 kfree_skb(skb);
3553 return -EINVAL;
3554}
3555#else
3556static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3557 const struct xfrm_migrate *m, int num_bundles,
3558 const struct xfrm_kmaddress *k)
3559{
3560 return -ENOPROTOOPT;
3561}
3562#endif
3563
3564static int pfkey_sendmsg(struct kiocb *kiocb,
3565 struct socket *sock, struct msghdr *msg, size_t len)
3566{
3567 struct sock *sk = sock->sk;
3568 struct sk_buff *skb = NULL;
3569 struct sadb_msg *hdr = NULL;
3570 int err;
3571
3572 err = -EOPNOTSUPP;
3573 if (msg->msg_flags & MSG_OOB)
3574 goto out;
3575
3576 err = -EMSGSIZE;
3577 if ((unsigned)len > sk->sk_sndbuf - 32)
3578 goto out;
3579
3580 err = -ENOBUFS;
3581 skb = alloc_skb(len, GFP_KERNEL);
3582 if (skb == NULL)
3583 goto out;
3584
3585 err = -EFAULT;
3586 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3587 goto out;
3588
3589 hdr = pfkey_get_base_msg(skb, &err);
3590 if (!hdr)
3591 goto out;
3592
3593 mutex_lock(&xfrm_cfg_mutex);
3594 err = pfkey_process(sk, skb, hdr);
3595 mutex_unlock(&xfrm_cfg_mutex);
3596
3597out:
3598 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3599 err = 0;
3600 kfree_skb(skb);
3601
3602 return err ? : len;
3603}
3604
3605static int pfkey_recvmsg(struct kiocb *kiocb,
3606 struct socket *sock, struct msghdr *msg, size_t len,
3607 int flags)
3608{
3609 struct sock *sk = sock->sk;
3610 struct pfkey_sock *pfk = pfkey_sk(sk);
3611 struct sk_buff *skb;
3612 int copied, err;
3613
3614 err = -EINVAL;
3615 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3616 goto out;
3617
3618 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3619 if (skb == NULL)
3620 goto out;
3621
3622 copied = skb->len;
3623 if (copied > len) {
3624 msg->msg_flags |= MSG_TRUNC;
3625 copied = len;
3626 }
3627
3628 skb_reset_transport_header(skb);
3629 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3630 if (err)
3631 goto out_free;
3632
3633 sock_recv_ts_and_drops(msg, sk, skb);
3634
3635 err = (flags & MSG_TRUNC) ? skb->len : copied;
3636
3637 if (pfk->dump.dump != NULL &&
3638 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3639 pfkey_do_dump(pfk);
3640
3641out_free:
3642 skb_free_datagram(sk, skb);
3643out:
3644 return err;
3645}
3646
3647static const struct proto_ops pfkey_ops = {
3648 .family = PF_KEY,
3649 .owner = THIS_MODULE,
3650 /* Operations that make no sense on pfkey sockets. */
3651 .bind = sock_no_bind,
3652 .connect = sock_no_connect,
3653 .socketpair = sock_no_socketpair,
3654 .accept = sock_no_accept,
3655 .getname = sock_no_getname,
3656 .ioctl = sock_no_ioctl,
3657 .listen = sock_no_listen,
3658 .shutdown = sock_no_shutdown,
3659 .setsockopt = sock_no_setsockopt,
3660 .getsockopt = sock_no_getsockopt,
3661 .mmap = sock_no_mmap,
3662 .sendpage = sock_no_sendpage,
3663
3664 /* Now the operations that really occur. */
3665 .release = pfkey_release,
3666 .poll = datagram_poll,
3667 .sendmsg = pfkey_sendmsg,
3668 .recvmsg = pfkey_recvmsg,
3669};
3670
3671static const struct net_proto_family pfkey_family_ops = {
3672 .family = PF_KEY,
3673 .create = pfkey_create,
3674 .owner = THIS_MODULE,
3675};
3676
3677#ifdef CONFIG_PROC_FS
3678static int pfkey_seq_show(struct seq_file *f, void *v)
3679{
3680 struct sock *s = sk_entry(v);
3681
3682 if (v == SEQ_START_TOKEN)
3683 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3684 else
3685 seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
3686 s,
3687 atomic_read(&s->sk_refcnt),
3688 sk_rmem_alloc_get(s),
3689 sk_wmem_alloc_get(s),
3690 sock_i_uid(s),
3691 sock_i_ino(s)
3692 );
3693 return 0;
3694}
3695
3696static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3697 __acquires(rcu)
3698{
3699 struct net *net = seq_file_net(f);
3700 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3701
3702 rcu_read_lock();
3703 return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
3704}
3705
3706static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3707{
3708 struct net *net = seq_file_net(f);
3709 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3710
3711 return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
3712}
3713
3714static void pfkey_seq_stop(struct seq_file *f, void *v)
3715 __releases(rcu)
3716{
3717 rcu_read_unlock();
3718}
3719
3720static const struct seq_operations pfkey_seq_ops = {
3721 .start = pfkey_seq_start,
3722 .next = pfkey_seq_next,
3723 .stop = pfkey_seq_stop,
3724 .show = pfkey_seq_show,
3725};
3726
3727static int pfkey_seq_open(struct inode *inode, struct file *file)
3728{
3729 return seq_open_net(inode, file, &pfkey_seq_ops,
3730 sizeof(struct seq_net_private));
3731}
3732
3733static const struct file_operations pfkey_proc_ops = {
3734 .open = pfkey_seq_open,
3735 .read = seq_read,
3736 .llseek = seq_lseek,
3737 .release = seq_release_net,
3738};
3739
3740static int __net_init pfkey_init_proc(struct net *net)
3741{
3742 struct proc_dir_entry *e;
3743
3744 e = proc_net_fops_create(net, "pfkey", 0, &pfkey_proc_ops);
3745 if (e == NULL)
3746 return -ENOMEM;
3747
3748 return 0;
3749}
3750
3751static void __net_exit pfkey_exit_proc(struct net *net)
3752{
3753 proc_net_remove(net, "pfkey");
3754}
3755#else
3756static inline int pfkey_init_proc(struct net *net)
3757{
3758 return 0;
3759}
3760
3761static inline void pfkey_exit_proc(struct net *net)
3762{
3763}
3764#endif
3765
3766static struct xfrm_mgr pfkeyv2_mgr =
3767{
3768 .id = "pfkeyv2",
3769 .notify = pfkey_send_notify,
3770 .acquire = pfkey_send_acquire,
3771 .compile_policy = pfkey_compile_policy,
3772 .new_mapping = pfkey_send_new_mapping,
3773 .notify_policy = pfkey_send_policy_notify,
3774 .migrate = pfkey_send_migrate,
3775};
3776
3777static int __net_init pfkey_net_init(struct net *net)
3778{
3779 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3780 int rv;
3781
3782 INIT_HLIST_HEAD(&net_pfkey->table);
3783 atomic_set(&net_pfkey->socks_nr, 0);
3784
3785 rv = pfkey_init_proc(net);
3786
3787 return rv;
3788}
3789
3790static void __net_exit pfkey_net_exit(struct net *net)
3791{
3792 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3793
3794 pfkey_exit_proc(net);
3795 BUG_ON(!hlist_empty(&net_pfkey->table));
3796}
3797
3798static struct pernet_operations pfkey_net_ops = {
3799 .init = pfkey_net_init,
3800 .exit = pfkey_net_exit,
3801 .id = &pfkey_net_id,
3802 .size = sizeof(struct netns_pfkey),
3803};
3804
3805static void __exit ipsec_pfkey_exit(void)
3806{
3807 xfrm_unregister_km(&pfkeyv2_mgr);
3808 sock_unregister(PF_KEY);
3809 unregister_pernet_subsys(&pfkey_net_ops);
3810 proto_unregister(&key_proto);
3811}
3812
3813static int __init ipsec_pfkey_init(void)
3814{
3815 int err = proto_register(&key_proto, 0);
3816
3817 if (err != 0)
3818 goto out;
3819
3820 err = register_pernet_subsys(&pfkey_net_ops);
3821 if (err != 0)
3822 goto out_unregister_key_proto;
3823 err = sock_register(&pfkey_family_ops);
3824 if (err != 0)
3825 goto out_unregister_pernet;
3826 err = xfrm_register_km(&pfkeyv2_mgr);
3827 if (err != 0)
3828 goto out_sock_unregister;
3829out:
3830 return err;
3831
3832out_sock_unregister:
3833 sock_unregister(PF_KEY);
3834out_unregister_pernet:
3835 unregister_pernet_subsys(&pfkey_net_ops);
3836out_unregister_key_proto:
3837 proto_unregister(&key_proto);
3838 goto out;
3839}
3840
3841module_init(ipsec_pfkey_init);
3842module_exit(ipsec_pfkey_exit);
3843MODULE_LICENSE("GPL");
3844MODULE_ALIAS_NETPROTO(PF_KEY);