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