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