blob: d086c117f5f07796c02cae27c3ebebead1b52f6c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Maxim Giryaev <gem@asplinux.ru>
10 * David S. Miller <davem@redhat.com>
11 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
12 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
14 * Derek Atkins <derek@ihtfp.com>
15 */
16
17#include <linux/config.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 <net/xfrm.h>
30
31#include <net/sock.h>
32
33#define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
34#define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
35
36
37/* List of all pfkey sockets. */
38static HLIST_HEAD(pfkey_table);
39static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait);
40static DEFINE_RWLOCK(pfkey_table_lock);
41static atomic_t pfkey_table_users = ATOMIC_INIT(0);
42
43static atomic_t pfkey_socks_nr = ATOMIC_INIT(0);
44
45struct pfkey_sock {
46 /* struct sock must be the first member of struct pfkey_sock */
47 struct sock sk;
48 int registered;
49 int promisc;
50};
51
52static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
53{
54 return (struct pfkey_sock *)sk;
55}
56
57static void pfkey_sock_destruct(struct sock *sk)
58{
59 skb_queue_purge(&sk->sk_receive_queue);
60
61 if (!sock_flag(sk, SOCK_DEAD)) {
62 printk("Attempt to release alive pfkey socket: %p\n", sk);
63 return;
64 }
65
66 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
67 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
68
69 atomic_dec(&pfkey_socks_nr);
70}
71
72static void pfkey_table_grab(void)
73{
74 write_lock_bh(&pfkey_table_lock);
75
76 if (atomic_read(&pfkey_table_users)) {
77 DECLARE_WAITQUEUE(wait, current);
78
79 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
80 for(;;) {
81 set_current_state(TASK_UNINTERRUPTIBLE);
82 if (atomic_read(&pfkey_table_users) == 0)
83 break;
84 write_unlock_bh(&pfkey_table_lock);
85 schedule();
86 write_lock_bh(&pfkey_table_lock);
87 }
88
89 __set_current_state(TASK_RUNNING);
90 remove_wait_queue(&pfkey_table_wait, &wait);
91 }
92}
93
94static __inline__ void pfkey_table_ungrab(void)
95{
96 write_unlock_bh(&pfkey_table_lock);
97 wake_up(&pfkey_table_wait);
98}
99
100static __inline__ void pfkey_lock_table(void)
101{
102 /* read_lock() synchronizes us to pfkey_table_grab */
103
104 read_lock(&pfkey_table_lock);
105 atomic_inc(&pfkey_table_users);
106 read_unlock(&pfkey_table_lock);
107}
108
109static __inline__ void pfkey_unlock_table(void)
110{
111 if (atomic_dec_and_test(&pfkey_table_users))
112 wake_up(&pfkey_table_wait);
113}
114
115
116static struct proto_ops pfkey_ops;
117
118static void pfkey_insert(struct sock *sk)
119{
120 pfkey_table_grab();
121 sk_add_node(sk, &pfkey_table);
122 pfkey_table_ungrab();
123}
124
125static void pfkey_remove(struct sock *sk)
126{
127 pfkey_table_grab();
128 sk_del_node_init(sk);
129 pfkey_table_ungrab();
130}
131
132static struct proto key_proto = {
133 .name = "KEY",
134 .owner = THIS_MODULE,
135 .obj_size = sizeof(struct pfkey_sock),
136};
137
138static int pfkey_create(struct socket *sock, int protocol)
139{
140 struct sock *sk;
141 int err;
142
143 if (!capable(CAP_NET_ADMIN))
144 return -EPERM;
145 if (sock->type != SOCK_RAW)
146 return -ESOCKTNOSUPPORT;
147 if (protocol != PF_KEY_V2)
148 return -EPROTONOSUPPORT;
149
150 err = -ENOMEM;
151 sk = sk_alloc(PF_KEY, GFP_KERNEL, &key_proto, 1);
152 if (sk == NULL)
153 goto out;
154
155 sock->ops = &pfkey_ops;
156 sock_init_data(sock, sk);
157
158 sk->sk_family = PF_KEY;
159 sk->sk_destruct = pfkey_sock_destruct;
160
161 atomic_inc(&pfkey_socks_nr);
162
163 pfkey_insert(sk);
164
165 return 0;
166out:
167 return err;
168}
169
170static int pfkey_release(struct socket *sock)
171{
172 struct sock *sk = sock->sk;
173
174 if (!sk)
175 return 0;
176
177 pfkey_remove(sk);
178
179 sock_orphan(sk);
180 sock->sk = NULL;
181 skb_queue_purge(&sk->sk_write_queue);
182 sock_put(sk);
183
184 return 0;
185}
186
187static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
188 int allocation, struct sock *sk)
189{
190 int err = -ENOBUFS;
191
192 sock_hold(sk);
193 if (*skb2 == NULL) {
194 if (atomic_read(&skb->users) != 1) {
195 *skb2 = skb_clone(skb, allocation);
196 } else {
197 *skb2 = skb;
198 atomic_inc(&skb->users);
199 }
200 }
201 if (*skb2 != NULL) {
202 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
203 skb_orphan(*skb2);
204 skb_set_owner_r(*skb2, sk);
205 skb_queue_tail(&sk->sk_receive_queue, *skb2);
206 sk->sk_data_ready(sk, (*skb2)->len);
207 *skb2 = NULL;
208 err = 0;
209 }
210 }
211 sock_put(sk);
212 return err;
213}
214
215/* Send SKB to all pfkey sockets matching selected criteria. */
216#define BROADCAST_ALL 0
217#define BROADCAST_ONE 1
218#define BROADCAST_REGISTERED 2
219#define BROADCAST_PROMISC_ONLY 4
220static int pfkey_broadcast(struct sk_buff *skb, int allocation,
221 int broadcast_flags, struct sock *one_sk)
222{
223 struct sock *sk;
224 struct hlist_node *node;
225 struct sk_buff *skb2 = NULL;
226 int err = -ESRCH;
227
228 /* XXX Do we need something like netlink_overrun? I think
229 * XXX PF_KEY socket apps will not mind current behavior.
230 */
231 if (!skb)
232 return -ENOMEM;
233
234 pfkey_lock_table();
235 sk_for_each(sk, node, &pfkey_table) {
236 struct pfkey_sock *pfk = pfkey_sk(sk);
237 int err2;
238
239 /* Yes, it means that if you are meant to receive this
240 * pfkey message you receive it twice as promiscuous
241 * socket.
242 */
243 if (pfk->promisc)
244 pfkey_broadcast_one(skb, &skb2, allocation, sk);
245
246 /* the exact target will be processed later */
247 if (sk == one_sk)
248 continue;
249 if (broadcast_flags != BROADCAST_ALL) {
250 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
251 continue;
252 if ((broadcast_flags & BROADCAST_REGISTERED) &&
253 !pfk->registered)
254 continue;
255 if (broadcast_flags & BROADCAST_ONE)
256 continue;
257 }
258
259 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
260
261 /* Error is cleare after succecful sending to at least one
262 * registered KM */
263 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
264 err = err2;
265 }
266 pfkey_unlock_table();
267
268 if (one_sk != NULL)
269 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
270
271 if (skb2)
272 kfree_skb(skb2);
273 kfree_skb(skb);
274 return err;
275}
276
277static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
278{
279 *new = *orig;
280}
281
282static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
283{
284 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
285 struct sadb_msg *hdr;
286
287 if (!skb)
288 return -ENOBUFS;
289
290 /* Woe be to the platform trying to support PFKEY yet
291 * having normal errnos outside the 1-255 range, inclusive.
292 */
293 err = -err;
294 if (err == ERESTARTSYS ||
295 err == ERESTARTNOHAND ||
296 err == ERESTARTNOINTR)
297 err = EINTR;
298 if (err >= 512)
299 err = EINVAL;
300 if (err <= 0 || err >= 256)
301 BUG();
302
303 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
304 pfkey_hdr_dup(hdr, orig);
305 hdr->sadb_msg_errno = (uint8_t) err;
306 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
307 sizeof(uint64_t));
308
309 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);
310
311 return 0;
312}
313
314static u8 sadb_ext_min_len[] = {
315 [SADB_EXT_RESERVED] = (u8) 0,
316 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
317 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
318 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
319 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
320 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
321 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
322 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
323 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
324 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
325 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
326 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
327 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
328 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
329 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
330 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
331 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
332 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
333 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
334 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
335 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
336 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
337 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
338 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
339};
340
341/* Verify sadb_address_{len,prefixlen} against sa_family. */
342static int verify_address_len(void *p)
343{
344 struct sadb_address *sp = p;
345 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
346 struct sockaddr_in *sin;
347#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
348 struct sockaddr_in6 *sin6;
349#endif
350 int len;
351
352 switch (addr->sa_family) {
353 case AF_INET:
354 len = sizeof(*sp) + sizeof(*sin) + (sizeof(uint64_t) - 1);
355 len /= sizeof(uint64_t);
356 if (sp->sadb_address_len != len ||
357 sp->sadb_address_prefixlen > 32)
358 return -EINVAL;
359 break;
360#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
361 case AF_INET6:
362 len = sizeof(*sp) + sizeof(*sin6) + (sizeof(uint64_t) - 1);
363 len /= sizeof(uint64_t);
364 if (sp->sadb_address_len != len ||
365 sp->sadb_address_prefixlen > 128)
366 return -EINVAL;
367 break;
368#endif
369 default:
370 /* It is user using kernel to keep track of security
371 * associations for another protocol, such as
372 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
373 * lengths.
374 *
375 * XXX Actually, association/policy database is not yet
376 * XXX able to cope with arbitrary sockaddr families.
377 * XXX When it can, remove this -EINVAL. -DaveM
378 */
379 return -EINVAL;
380 break;
381 };
382
383 return 0;
384}
385
386static int present_and_same_family(struct sadb_address *src,
387 struct sadb_address *dst)
388{
389 struct sockaddr *s_addr, *d_addr;
390
391 if (!src || !dst)
392 return 0;
393
394 s_addr = (struct sockaddr *)(src + 1);
395 d_addr = (struct sockaddr *)(dst + 1);
396 if (s_addr->sa_family != d_addr->sa_family)
397 return 0;
398 if (s_addr->sa_family != AF_INET
399#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
400 && s_addr->sa_family != AF_INET6
401#endif
402 )
403 return 0;
404
405 return 1;
406}
407
408static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
409{
410 char *p = (char *) hdr;
411 int len = skb->len;
412
413 len -= sizeof(*hdr);
414 p += sizeof(*hdr);
415 while (len > 0) {
416 struct sadb_ext *ehdr = (struct sadb_ext *) p;
417 uint16_t ext_type;
418 int ext_len;
419
420 ext_len = ehdr->sadb_ext_len;
421 ext_len *= sizeof(uint64_t);
422 ext_type = ehdr->sadb_ext_type;
423 if (ext_len < sizeof(uint64_t) ||
424 ext_len > len ||
425 ext_type == SADB_EXT_RESERVED)
426 return -EINVAL;
427
428 if (ext_type <= SADB_EXT_MAX) {
429 int min = (int) sadb_ext_min_len[ext_type];
430 if (ext_len < min)
431 return -EINVAL;
432 if (ext_hdrs[ext_type-1] != NULL)
433 return -EINVAL;
434 if (ext_type == SADB_EXT_ADDRESS_SRC ||
435 ext_type == SADB_EXT_ADDRESS_DST ||
436 ext_type == SADB_EXT_ADDRESS_PROXY ||
437 ext_type == SADB_X_EXT_NAT_T_OA) {
438 if (verify_address_len(p))
439 return -EINVAL;
440 }
441 ext_hdrs[ext_type-1] = p;
442 }
443 p += ext_len;
444 len -= ext_len;
445 }
446
447 return 0;
448}
449
450static uint16_t
451pfkey_satype2proto(uint8_t satype)
452{
453 switch (satype) {
454 case SADB_SATYPE_UNSPEC:
455 return IPSEC_PROTO_ANY;
456 case SADB_SATYPE_AH:
457 return IPPROTO_AH;
458 case SADB_SATYPE_ESP:
459 return IPPROTO_ESP;
460 case SADB_X_SATYPE_IPCOMP:
461 return IPPROTO_COMP;
462 break;
463 default:
464 return 0;
465 }
466 /* NOTREACHED */
467}
468
469static uint8_t
470pfkey_proto2satype(uint16_t proto)
471{
472 switch (proto) {
473 case IPPROTO_AH:
474 return SADB_SATYPE_AH;
475 case IPPROTO_ESP:
476 return SADB_SATYPE_ESP;
477 case IPPROTO_COMP:
478 return SADB_X_SATYPE_IPCOMP;
479 break;
480 default:
481 return 0;
482 }
483 /* NOTREACHED */
484}
485
486/* BTW, this scheme means that there is no way with PFKEY2 sockets to
487 * say specifically 'just raw sockets' as we encode them as 255.
488 */
489
490static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
491{
492 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
493}
494
495static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
496{
497 return (proto ? proto : IPSEC_PROTO_ANY);
498}
499
500static int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr,
501 xfrm_address_t *xaddr)
502{
503 switch (((struct sockaddr*)(addr + 1))->sa_family) {
504 case AF_INET:
505 xaddr->a4 =
506 ((struct sockaddr_in *)(addr + 1))->sin_addr.s_addr;
507 return AF_INET;
508#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
509 case AF_INET6:
510 memcpy(xaddr->a6,
511 &((struct sockaddr_in6 *)(addr + 1))->sin6_addr,
512 sizeof(struct in6_addr));
513 return AF_INET6;
514#endif
515 default:
516 return 0;
517 }
518 /* NOTREACHED */
519}
520
521static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
522{
523 struct sadb_sa *sa;
524 struct sadb_address *addr;
525 uint16_t proto;
526 unsigned short family;
527 xfrm_address_t *xaddr;
528
529 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
530 if (sa == NULL)
531 return NULL;
532
533 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
534 if (proto == 0)
535 return NULL;
536
537 /* sadb_address_len should be checked by caller */
538 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
539 if (addr == NULL)
540 return NULL;
541
542 family = ((struct sockaddr *)(addr + 1))->sa_family;
543 switch (family) {
544 case AF_INET:
545 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
546 break;
547#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
548 case AF_INET6:
549 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
550 break;
551#endif
552 default:
553 xaddr = NULL;
554 }
555
556 if (!xaddr)
557 return NULL;
558
559 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
560}
561
562#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
563static int
564pfkey_sockaddr_size(sa_family_t family)
565{
566 switch (family) {
567 case AF_INET:
568 return PFKEY_ALIGN8(sizeof(struct sockaddr_in));
569#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
570 case AF_INET6:
571 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6));
572#endif
573 default:
574 return 0;
575 }
576 /* NOTREACHED */
577}
578
579static struct sk_buff * pfkey_xfrm_state2msg(struct xfrm_state *x, int add_keys, int hsc)
580{
581 struct sk_buff *skb;
582 struct sadb_msg *hdr;
583 struct sadb_sa *sa;
584 struct sadb_lifetime *lifetime;
585 struct sadb_address *addr;
586 struct sadb_key *key;
587 struct sadb_x_sa2 *sa2;
588 struct sockaddr_in *sin;
589#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
590 struct sockaddr_in6 *sin6;
591#endif
592 int size;
593 int auth_key_size = 0;
594 int encrypt_key_size = 0;
595 int sockaddr_size;
596 struct xfrm_encap_tmpl *natt = NULL;
597
598 /* address family check */
599 sockaddr_size = pfkey_sockaddr_size(x->props.family);
600 if (!sockaddr_size)
601 return ERR_PTR(-EINVAL);
602
603 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
604 key(AE), (identity(SD),) (sensitivity)> */
605 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
606 sizeof(struct sadb_lifetime) +
607 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
608 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
609 sizeof(struct sadb_address)*2 +
610 sockaddr_size*2 +
611 sizeof(struct sadb_x_sa2);
612 /* identity & sensitivity */
613
614 if ((x->props.family == AF_INET &&
615 x->sel.saddr.a4 != x->props.saddr.a4)
616#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
617 || (x->props.family == AF_INET6 &&
618 memcmp (x->sel.saddr.a6, x->props.saddr.a6, sizeof (struct in6_addr)))
619#endif
620 )
621 size += sizeof(struct sadb_address) + sockaddr_size;
622
623 if (add_keys) {
624 if (x->aalg && x->aalg->alg_key_len) {
625 auth_key_size =
626 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
627 size += sizeof(struct sadb_key) + auth_key_size;
628 }
629 if (x->ealg && x->ealg->alg_key_len) {
630 encrypt_key_size =
631 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
632 size += sizeof(struct sadb_key) + encrypt_key_size;
633 }
634 }
635 if (x->encap)
636 natt = x->encap;
637
638 if (natt && natt->encap_type) {
639 size += sizeof(struct sadb_x_nat_t_type);
640 size += sizeof(struct sadb_x_nat_t_port);
641 size += sizeof(struct sadb_x_nat_t_port);
642 }
643
644 skb = alloc_skb(size + 16, GFP_ATOMIC);
645 if (skb == NULL)
646 return ERR_PTR(-ENOBUFS);
647
648 /* call should fill header later */
649 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
650 memset(hdr, 0, size); /* XXX do we need this ? */
651 hdr->sadb_msg_len = size / sizeof(uint64_t);
652
653 /* sa */
654 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
655 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
656 sa->sadb_sa_exttype = SADB_EXT_SA;
657 sa->sadb_sa_spi = x->id.spi;
658 sa->sadb_sa_replay = x->props.replay_window;
659 sa->sadb_sa_state = SADB_SASTATE_DYING;
660 if (x->km.state == XFRM_STATE_VALID && !x->km.dying)
661 sa->sadb_sa_state = SADB_SASTATE_MATURE;
662 else if (x->km.state == XFRM_STATE_ACQ)
663 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
664 else if (x->km.state == XFRM_STATE_EXPIRED)
665 sa->sadb_sa_state = SADB_SASTATE_DEAD;
666 sa->sadb_sa_auth = 0;
667 if (x->aalg) {
668 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
669 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
670 }
671 sa->sadb_sa_encrypt = 0;
672 BUG_ON(x->ealg && x->calg);
673 if (x->ealg) {
674 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
675 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
676 }
677 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
678 if (x->calg) {
679 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
680 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
681 }
682
683 sa->sadb_sa_flags = 0;
684 if (x->props.flags & XFRM_STATE_NOECN)
685 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
686 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
687 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
688
689 /* hard time */
690 if (hsc & 2) {
691 lifetime = (struct sadb_lifetime *) skb_put(skb,
692 sizeof(struct sadb_lifetime));
693 lifetime->sadb_lifetime_len =
694 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
695 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
696 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
697 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
698 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
699 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
700 }
701 /* soft time */
702 if (hsc & 1) {
703 lifetime = (struct sadb_lifetime *) skb_put(skb,
704 sizeof(struct sadb_lifetime));
705 lifetime->sadb_lifetime_len =
706 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
707 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
708 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
709 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
710 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
711 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
712 }
713 /* current time */
714 lifetime = (struct sadb_lifetime *) skb_put(skb,
715 sizeof(struct sadb_lifetime));
716 lifetime->sadb_lifetime_len =
717 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
718 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
719 lifetime->sadb_lifetime_allocations = x->curlft.packets;
720 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
721 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
722 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
723 /* src address */
724 addr = (struct sadb_address*) skb_put(skb,
725 sizeof(struct sadb_address)+sockaddr_size);
726 addr->sadb_address_len =
727 (sizeof(struct sadb_address)+sockaddr_size)/
728 sizeof(uint64_t);
729 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
730 /* "if the ports are non-zero, then the sadb_address_proto field,
731 normally zero, MUST be filled in with the transport
732 protocol's number." - RFC2367 */
733 addr->sadb_address_proto = 0;
734 addr->sadb_address_reserved = 0;
735 if (x->props.family == AF_INET) {
736 addr->sadb_address_prefixlen = 32;
737
738 sin = (struct sockaddr_in *) (addr + 1);
739 sin->sin_family = AF_INET;
740 sin->sin_addr.s_addr = x->props.saddr.a4;
741 sin->sin_port = 0;
742 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
743 }
744#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
745 else if (x->props.family == AF_INET6) {
746 addr->sadb_address_prefixlen = 128;
747
748 sin6 = (struct sockaddr_in6 *) (addr + 1);
749 sin6->sin6_family = AF_INET6;
750 sin6->sin6_port = 0;
751 sin6->sin6_flowinfo = 0;
752 memcpy(&sin6->sin6_addr, x->props.saddr.a6,
753 sizeof(struct in6_addr));
754 sin6->sin6_scope_id = 0;
755 }
756#endif
757 else
758 BUG();
759
760 /* dst address */
761 addr = (struct sadb_address*) skb_put(skb,
762 sizeof(struct sadb_address)+sockaddr_size);
763 addr->sadb_address_len =
764 (sizeof(struct sadb_address)+sockaddr_size)/
765 sizeof(uint64_t);
766 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
767 addr->sadb_address_proto = 0;
768 addr->sadb_address_prefixlen = 32; /* XXX */
769 addr->sadb_address_reserved = 0;
770 if (x->props.family == AF_INET) {
771 sin = (struct sockaddr_in *) (addr + 1);
772 sin->sin_family = AF_INET;
773 sin->sin_addr.s_addr = x->id.daddr.a4;
774 sin->sin_port = 0;
775 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
776
777 if (x->sel.saddr.a4 != x->props.saddr.a4) {
778 addr = (struct sadb_address*) skb_put(skb,
779 sizeof(struct sadb_address)+sockaddr_size);
780 addr->sadb_address_len =
781 (sizeof(struct sadb_address)+sockaddr_size)/
782 sizeof(uint64_t);
783 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
784 addr->sadb_address_proto =
785 pfkey_proto_from_xfrm(x->sel.proto);
786 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
787 addr->sadb_address_reserved = 0;
788
789 sin = (struct sockaddr_in *) (addr + 1);
790 sin->sin_family = AF_INET;
791 sin->sin_addr.s_addr = x->sel.saddr.a4;
792 sin->sin_port = x->sel.sport;
793 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
794 }
795 }
796#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
797 else if (x->props.family == AF_INET6) {
798 addr->sadb_address_prefixlen = 128;
799
800 sin6 = (struct sockaddr_in6 *) (addr + 1);
801 sin6->sin6_family = AF_INET6;
802 sin6->sin6_port = 0;
803 sin6->sin6_flowinfo = 0;
804 memcpy(&sin6->sin6_addr, x->id.daddr.a6, sizeof(struct in6_addr));
805 sin6->sin6_scope_id = 0;
806
807 if (memcmp (x->sel.saddr.a6, x->props.saddr.a6,
808 sizeof(struct in6_addr))) {
809 addr = (struct sadb_address *) skb_put(skb,
810 sizeof(struct sadb_address)+sockaddr_size);
811 addr->sadb_address_len =
812 (sizeof(struct sadb_address)+sockaddr_size)/
813 sizeof(uint64_t);
814 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
815 addr->sadb_address_proto =
816 pfkey_proto_from_xfrm(x->sel.proto);
817 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
818 addr->sadb_address_reserved = 0;
819
820 sin6 = (struct sockaddr_in6 *) (addr + 1);
821 sin6->sin6_family = AF_INET6;
822 sin6->sin6_port = x->sel.sport;
823 sin6->sin6_flowinfo = 0;
824 memcpy(&sin6->sin6_addr, x->sel.saddr.a6,
825 sizeof(struct in6_addr));
826 sin6->sin6_scope_id = 0;
827 }
828 }
829#endif
830 else
831 BUG();
832
833 /* auth key */
834 if (add_keys && auth_key_size) {
835 key = (struct sadb_key *) skb_put(skb,
836 sizeof(struct sadb_key)+auth_key_size);
837 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
838 sizeof(uint64_t);
839 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
840 key->sadb_key_bits = x->aalg->alg_key_len;
841 key->sadb_key_reserved = 0;
842 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
843 }
844 /* encrypt key */
845 if (add_keys && encrypt_key_size) {
846 key = (struct sadb_key *) skb_put(skb,
847 sizeof(struct sadb_key)+encrypt_key_size);
848 key->sadb_key_len = (sizeof(struct sadb_key) +
849 encrypt_key_size) / sizeof(uint64_t);
850 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
851 key->sadb_key_bits = x->ealg->alg_key_len;
852 key->sadb_key_reserved = 0;
853 memcpy(key + 1, x->ealg->alg_key,
854 (x->ealg->alg_key_len+7)/8);
855 }
856
857 /* sa */
858 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
859 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
860 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
861 sa2->sadb_x_sa2_mode = x->props.mode + 1;
862 sa2->sadb_x_sa2_reserved1 = 0;
863 sa2->sadb_x_sa2_reserved2 = 0;
864 sa2->sadb_x_sa2_sequence = 0;
865 sa2->sadb_x_sa2_reqid = x->props.reqid;
866
867 if (natt && natt->encap_type) {
868 struct sadb_x_nat_t_type *n_type;
869 struct sadb_x_nat_t_port *n_port;
870
871 /* type */
872 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
873 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
874 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
875 n_type->sadb_x_nat_t_type_type = natt->encap_type;
876 n_type->sadb_x_nat_t_type_reserved[0] = 0;
877 n_type->sadb_x_nat_t_type_reserved[1] = 0;
878 n_type->sadb_x_nat_t_type_reserved[2] = 0;
879
880 /* source port */
881 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
882 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
883 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
884 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
885 n_port->sadb_x_nat_t_port_reserved = 0;
886
887 /* dest port */
888 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
889 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
890 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
891 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
892 n_port->sadb_x_nat_t_port_reserved = 0;
893 }
894
895 return skb;
896}
897
898static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
899 void **ext_hdrs)
900{
901 struct xfrm_state *x;
902 struct sadb_lifetime *lifetime;
903 struct sadb_sa *sa;
904 struct sadb_key *key;
905 uint16_t proto;
906 int err;
907
908
909 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
910 if (!sa ||
911 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
912 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
913 return ERR_PTR(-EINVAL);
914 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
915 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
916 return ERR_PTR(-EINVAL);
917 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
918 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
919 return ERR_PTR(-EINVAL);
920 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
921 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
922 return ERR_PTR(-EINVAL);
923
924 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
925 if (proto == 0)
926 return ERR_PTR(-EINVAL);
927
928 /* default error is no buffer space */
929 err = -ENOBUFS;
930
931 /* RFC2367:
932
933 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
934 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
935 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
936 Therefore, the sadb_sa_state field of all submitted SAs MUST be
937 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
938 not true.
939
940 However, KAME setkey always uses SADB_SASTATE_LARVAL.
941 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
942 */
943 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
944 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
945 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
946 sa->sadb_sa_encrypt > SADB_EALG_MAX)
947 return ERR_PTR(-EINVAL);
948 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
949 if (key != NULL &&
950 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
951 ((key->sadb_key_bits+7) / 8 == 0 ||
952 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
953 return ERR_PTR(-EINVAL);
954 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
955 if (key != NULL &&
956 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
957 ((key->sadb_key_bits+7) / 8 == 0 ||
958 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
959 return ERR_PTR(-EINVAL);
960
961 x = xfrm_state_alloc();
962 if (x == NULL)
963 return ERR_PTR(-ENOBUFS);
964
965 x->id.proto = proto;
966 x->id.spi = sa->sadb_sa_spi;
967 x->props.replay_window = sa->sadb_sa_replay;
968 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
969 x->props.flags |= XFRM_STATE_NOECN;
970 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
971 x->props.flags |= XFRM_STATE_DECAP_DSCP;
972
973 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
974 if (lifetime != NULL) {
975 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
976 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
977 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
978 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
979 }
980 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
981 if (lifetime != NULL) {
982 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
983 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
984 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
985 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
986 }
987 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
988 if (sa->sadb_sa_auth) {
989 int keysize = 0;
990 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
991 if (!a) {
992 err = -ENOSYS;
993 goto out;
994 }
995 if (key)
996 keysize = (key->sadb_key_bits + 7) / 8;
997 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
998 if (!x->aalg)
999 goto out;
1000 strcpy(x->aalg->alg_name, a->name);
1001 x->aalg->alg_key_len = 0;
1002 if (key) {
1003 x->aalg->alg_key_len = key->sadb_key_bits;
1004 memcpy(x->aalg->alg_key, key+1, keysize);
1005 }
1006 x->props.aalgo = sa->sadb_sa_auth;
1007 /* x->algo.flags = sa->sadb_sa_flags; */
1008 }
1009 if (sa->sadb_sa_encrypt) {
1010 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1011 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1012 if (!a) {
1013 err = -ENOSYS;
1014 goto out;
1015 }
1016 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1017 if (!x->calg)
1018 goto out;
1019 strcpy(x->calg->alg_name, a->name);
1020 x->props.calgo = sa->sadb_sa_encrypt;
1021 } else {
1022 int keysize = 0;
1023 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1024 if (!a) {
1025 err = -ENOSYS;
1026 goto out;
1027 }
1028 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1029 if (key)
1030 keysize = (key->sadb_key_bits + 7) / 8;
1031 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1032 if (!x->ealg)
1033 goto out;
1034 strcpy(x->ealg->alg_name, a->name);
1035 x->ealg->alg_key_len = 0;
1036 if (key) {
1037 x->ealg->alg_key_len = key->sadb_key_bits;
1038 memcpy(x->ealg->alg_key, key+1, keysize);
1039 }
1040 x->props.ealgo = sa->sadb_sa_encrypt;
1041 }
1042 }
1043 /* x->algo.flags = sa->sadb_sa_flags; */
1044
1045 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1046 &x->props.saddr);
1047 if (!x->props.family) {
1048 err = -EAFNOSUPPORT;
1049 goto out;
1050 }
1051 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1052 &x->id.daddr);
1053
1054 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1055 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
1056 x->props.mode = sa2->sadb_x_sa2_mode;
1057 if (x->props.mode)
1058 x->props.mode--;
1059 x->props.reqid = sa2->sadb_x_sa2_reqid;
1060 }
1061
1062 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1063 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1064
1065 /* Nobody uses this, but we try. */
1066 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1067 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1068 }
1069
1070 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1071 struct sadb_x_nat_t_type* n_type;
1072 struct xfrm_encap_tmpl *natt;
1073
1074 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1075 if (!x->encap)
1076 goto out;
1077
1078 natt = x->encap;
1079 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1080 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1081
1082 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1083 struct sadb_x_nat_t_port* n_port =
1084 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1085 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1086 }
1087 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1088 struct sadb_x_nat_t_port* n_port =
1089 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1090 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1091 }
1092 }
1093
1094 x->type = xfrm_get_type(proto, x->props.family);
1095 if (x->type == NULL) {
1096 err = -ENOPROTOOPT;
1097 goto out;
1098 }
1099 if (x->type->init_state(x, NULL)) {
1100 err = -EINVAL;
1101 goto out;
1102 }
1103 x->km.seq = hdr->sadb_msg_seq;
1104 x->km.state = XFRM_STATE_VALID;
1105 return x;
1106
1107out:
1108 x->km.state = XFRM_STATE_DEAD;
1109 xfrm_state_put(x);
1110 return ERR_PTR(err);
1111}
1112
1113static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1114{
1115 return -EOPNOTSUPP;
1116}
1117
1118static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1119{
1120 struct sk_buff *resp_skb;
1121 struct sadb_x_sa2 *sa2;
1122 struct sadb_address *saddr, *daddr;
1123 struct sadb_msg *out_hdr;
1124 struct xfrm_state *x = NULL;
1125 u8 mode;
1126 u32 reqid;
1127 u8 proto;
1128 unsigned short family;
1129 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1130
1131 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1132 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1133 return -EINVAL;
1134
1135 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1136 if (proto == 0)
1137 return -EINVAL;
1138
1139 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1140 mode = sa2->sadb_x_sa2_mode - 1;
1141 reqid = sa2->sadb_x_sa2_reqid;
1142 } else {
1143 mode = 0;
1144 reqid = 0;
1145 }
1146
1147 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1148 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1149
1150 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1151 switch (family) {
1152 case AF_INET:
1153 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1154 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1155 break;
1156#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1157 case AF_INET6:
1158 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1159 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1160 break;
1161#endif
1162 }
1163
1164 if (hdr->sadb_msg_seq) {
1165 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1166 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1167 xfrm_state_put(x);
1168 x = NULL;
1169 }
1170 }
1171
1172 if (!x)
1173 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1174
1175 if (x == NULL)
1176 return -ENOENT;
1177
1178 resp_skb = ERR_PTR(-ENOENT);
1179
1180 spin_lock_bh(&x->lock);
1181 if (x->km.state != XFRM_STATE_DEAD) {
1182 struct sadb_spirange *range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1183 u32 min_spi, max_spi;
1184
1185 if (range != NULL) {
1186 min_spi = range->sadb_spirange_min;
1187 max_spi = range->sadb_spirange_max;
1188 } else {
1189 min_spi = 0x100;
1190 max_spi = 0x0fffffff;
1191 }
1192 xfrm_alloc_spi(x, htonl(min_spi), htonl(max_spi));
1193 if (x->id.spi)
1194 resp_skb = pfkey_xfrm_state2msg(x, 0, 3);
1195 }
1196 spin_unlock_bh(&x->lock);
1197
1198 if (IS_ERR(resp_skb)) {
1199 xfrm_state_put(x);
1200 return PTR_ERR(resp_skb);
1201 }
1202
1203 out_hdr = (struct sadb_msg *) resp_skb->data;
1204 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1205 out_hdr->sadb_msg_type = SADB_GETSPI;
1206 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1207 out_hdr->sadb_msg_errno = 0;
1208 out_hdr->sadb_msg_reserved = 0;
1209 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1210 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1211
1212 xfrm_state_put(x);
1213
1214 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1215
1216 return 0;
1217}
1218
1219static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1220{
1221 struct xfrm_state *x;
1222
1223 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1224 return -EOPNOTSUPP;
1225
1226 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1227 return 0;
1228
1229 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1230 if (x == NULL)
1231 return 0;
1232
1233 spin_lock_bh(&x->lock);
1234 if (x->km.state == XFRM_STATE_ACQ) {
1235 x->km.state = XFRM_STATE_ERROR;
1236 wake_up(&km_waitq);
1237 }
1238 spin_unlock_bh(&x->lock);
1239 xfrm_state_put(x);
1240 return 0;
1241}
1242
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001243static inline int event2poltype(int event)
1244{
1245 switch (event) {
1246 case XFRM_SAP_DELETED:
1247 return SADB_X_SPDDELETE;
1248 case XFRM_SAP_ADDED:
1249 return SADB_X_SPDADD;
1250 case XFRM_SAP_UPDATED:
1251 return SADB_X_SPDUPDATE;
1252 case XFRM_SAP_EXPIRED:
1253 // return SADB_X_SPDEXPIRE;
1254 default:
1255 printk("pfkey: Unknown policy event %d\n", event);
1256 break;
1257 }
1258
1259 return 0;
1260}
1261
1262static inline int event2keytype(int event)
1263{
1264 switch (event) {
1265 case XFRM_SAP_DELETED:
1266 return SADB_DELETE;
1267 case XFRM_SAP_ADDED:
1268 return SADB_ADD;
1269 case XFRM_SAP_UPDATED:
1270 return SADB_UPDATE;
1271 case XFRM_SAP_EXPIRED:
1272 return SADB_EXPIRE;
1273 default:
1274 printk("pfkey: Unknown SA event %d\n", event);
1275 break;
1276 }
1277
1278 return 0;
1279}
1280
1281/* ADD/UPD/DEL */
1282static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1283{
1284 struct sk_buff *skb;
1285 struct sadb_msg *hdr;
1286 int hsc = 3;
1287
1288 if (c->event == XFRM_SAP_DELETED)
1289 hsc = 0;
1290
1291 if (c->event == XFRM_SAP_EXPIRED) {
1292 if (c->data)
1293 hsc = 2;
1294 else
1295 hsc = 1;
1296 }
1297
1298 skb = pfkey_xfrm_state2msg(x, 0, hsc);
1299
1300 if (IS_ERR(skb))
1301 return PTR_ERR(skb);
1302
1303 hdr = (struct sadb_msg *) skb->data;
1304 hdr->sadb_msg_version = PF_KEY_V2;
1305 hdr->sadb_msg_type = event2keytype(c->event);
1306 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1307 hdr->sadb_msg_errno = 0;
1308 hdr->sadb_msg_reserved = 0;
1309 hdr->sadb_msg_seq = c->seq;
1310 hdr->sadb_msg_pid = c->pid;
1311
1312 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1313
1314 return 0;
1315}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1318{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 struct xfrm_state *x;
1320 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001321 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 xfrm_probe_algs();
1324
1325 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1326 if (IS_ERR(x))
1327 return PTR_ERR(x);
1328
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001329 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (hdr->sadb_msg_type == SADB_ADD)
1331 err = xfrm_state_add(x);
1332 else
1333 err = xfrm_state_update(x);
1334
1335 if (err < 0) {
1336 x->km.state = XFRM_STATE_DEAD;
1337 xfrm_state_put(x);
1338 return err;
1339 }
1340
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001341 if (hdr->sadb_msg_type == SADB_ADD)
1342 c.event = XFRM_SAP_ADDED;
1343 else
1344 c.event = XFRM_SAP_UPDATED;
1345 c.seq = hdr->sadb_msg_seq;
1346 c.pid = hdr->sadb_msg_pid;
1347 km_state_notify(x, &c);
1348 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001350 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
1353static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1354{
1355 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001356 struct km_event c;
1357 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 if (!ext_hdrs[SADB_EXT_SA-1] ||
1360 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1361 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1362 return -EINVAL;
1363
1364 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1365 if (x == NULL)
1366 return -ESRCH;
1367
1368 if (xfrm_state_kern(x)) {
1369 xfrm_state_put(x);
1370 return -EPERM;
1371 }
1372
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001373 err = xfrm_state_delete(x);
1374 if (err < 0) {
1375 xfrm_state_put(x);
1376 return err;
1377 }
1378
1379 c.seq = hdr->sadb_msg_seq;
1380 c.pid = hdr->sadb_msg_pid;
1381 c.event = XFRM_SAP_DELETED;
1382 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 xfrm_state_put(x);
1384
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1389{
1390 __u8 proto;
1391 struct sk_buff *out_skb;
1392 struct sadb_msg *out_hdr;
1393 struct xfrm_state *x;
1394
1395 if (!ext_hdrs[SADB_EXT_SA-1] ||
1396 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1397 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1398 return -EINVAL;
1399
1400 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1401 if (x == NULL)
1402 return -ESRCH;
1403
1404 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1405 proto = x->id.proto;
1406 xfrm_state_put(x);
1407 if (IS_ERR(out_skb))
1408 return PTR_ERR(out_skb);
1409
1410 out_hdr = (struct sadb_msg *) out_skb->data;
1411 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1412 out_hdr->sadb_msg_type = SADB_DUMP;
1413 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1414 out_hdr->sadb_msg_errno = 0;
1415 out_hdr->sadb_msg_reserved = 0;
1416 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1417 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1418 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1419
1420 return 0;
1421}
1422
1423static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig, int allocation)
1424{
1425 struct sk_buff *skb;
1426 struct sadb_msg *hdr;
1427 int len, auth_len, enc_len, i;
1428
1429 auth_len = xfrm_count_auth_supported();
1430 if (auth_len) {
1431 auth_len *= sizeof(struct sadb_alg);
1432 auth_len += sizeof(struct sadb_supported);
1433 }
1434
1435 enc_len = xfrm_count_enc_supported();
1436 if (enc_len) {
1437 enc_len *= sizeof(struct sadb_alg);
1438 enc_len += sizeof(struct sadb_supported);
1439 }
1440
1441 len = enc_len + auth_len + sizeof(struct sadb_msg);
1442
1443 skb = alloc_skb(len + 16, allocation);
1444 if (!skb)
1445 goto out_put_algs;
1446
1447 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1448 pfkey_hdr_dup(hdr, orig);
1449 hdr->sadb_msg_errno = 0;
1450 hdr->sadb_msg_len = len / sizeof(uint64_t);
1451
1452 if (auth_len) {
1453 struct sadb_supported *sp;
1454 struct sadb_alg *ap;
1455
1456 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1457 ap = (struct sadb_alg *) (sp + 1);
1458
1459 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1460 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1461
1462 for (i = 0; ; i++) {
1463 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1464 if (!aalg)
1465 break;
1466 if (aalg->available)
1467 *ap++ = aalg->desc;
1468 }
1469 }
1470
1471 if (enc_len) {
1472 struct sadb_supported *sp;
1473 struct sadb_alg *ap;
1474
1475 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1476 ap = (struct sadb_alg *) (sp + 1);
1477
1478 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1479 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1480
1481 for (i = 0; ; i++) {
1482 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1483 if (!ealg)
1484 break;
1485 if (ealg->available)
1486 *ap++ = ealg->desc;
1487 }
1488 }
1489
1490out_put_algs:
1491 return skb;
1492}
1493
1494static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1495{
1496 struct pfkey_sock *pfk = pfkey_sk(sk);
1497 struct sk_buff *supp_skb;
1498
1499 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1500 return -EINVAL;
1501
1502 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1503 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1504 return -EEXIST;
1505 pfk->registered |= (1<<hdr->sadb_msg_satype);
1506 }
1507
1508 xfrm_probe_algs();
1509
1510 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1511 if (!supp_skb) {
1512 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1513 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1514
1515 return -ENOBUFS;
1516 }
1517
1518 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1519
1520 return 0;
1521}
1522
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001523static int key_notify_sa_flush(struct km_event *c)
1524{
1525 struct sk_buff *skb;
1526 struct sadb_msg *hdr;
1527
1528 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1529 if (!skb)
1530 return -ENOBUFS;
1531 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1532 hdr->sadb_msg_satype = pfkey_proto2satype(c->data);
1533 hdr->sadb_msg_seq = c->seq;
1534 hdr->sadb_msg_pid = c->pid;
1535 hdr->sadb_msg_version = PF_KEY_V2;
1536 hdr->sadb_msg_errno = (uint8_t) 0;
1537 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1538
1539 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1540
1541 return 0;
1542}
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1545{
1546 unsigned proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001547 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1550 if (proto == 0)
1551 return -EINVAL;
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 xfrm_state_flush(proto);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001554 c.data = proto;
1555 c.seq = hdr->sadb_msg_seq;
1556 c.pid = hdr->sadb_msg_pid;
1557 c.event = XFRM_SAP_FLUSHED;
1558 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 return 0;
1561}
1562
1563struct pfkey_dump_data
1564{
1565 struct sk_buff *skb;
1566 struct sadb_msg *hdr;
1567 struct sock *sk;
1568};
1569
1570static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1571{
1572 struct pfkey_dump_data *data = ptr;
1573 struct sk_buff *out_skb;
1574 struct sadb_msg *out_hdr;
1575
1576 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1577 if (IS_ERR(out_skb))
1578 return PTR_ERR(out_skb);
1579
1580 out_hdr = (struct sadb_msg *) out_skb->data;
1581 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
1582 out_hdr->sadb_msg_type = SADB_DUMP;
1583 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1584 out_hdr->sadb_msg_errno = 0;
1585 out_hdr->sadb_msg_reserved = 0;
1586 out_hdr->sadb_msg_seq = count;
1587 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
1588 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
1589 return 0;
1590}
1591
1592static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1593{
1594 u8 proto;
1595 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
1596
1597 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1598 if (proto == 0)
1599 return -EINVAL;
1600
1601 return xfrm_state_walk(proto, dump_sa, &data);
1602}
1603
1604static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1605{
1606 struct pfkey_sock *pfk = pfkey_sk(sk);
1607 int satype = hdr->sadb_msg_satype;
1608
1609 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1610 /* XXX we mangle packet... */
1611 hdr->sadb_msg_errno = 0;
1612 if (satype != 0 && satype != 1)
1613 return -EINVAL;
1614 pfk->promisc = satype;
1615 }
1616 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1617 return 0;
1618}
1619
1620static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1621{
1622 int i;
1623 u32 reqid = *(u32*)ptr;
1624
1625 for (i=0; i<xp->xfrm_nr; i++) {
1626 if (xp->xfrm_vec[i].reqid == reqid)
1627 return -EEXIST;
1628 }
1629 return 0;
1630}
1631
1632static u32 gen_reqid(void)
1633{
1634 u32 start;
1635 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1636
1637 start = reqid;
1638 do {
1639 ++reqid;
1640 if (reqid == 0)
1641 reqid = IPSEC_MANUAL_REQID_MAX+1;
1642 if (xfrm_policy_walk(check_reqid, (void*)&reqid) != -EEXIST)
1643 return reqid;
1644 } while (reqid != start);
1645 return 0;
1646}
1647
1648static int
1649parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1650{
1651 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1652 struct sockaddr_in *sin;
1653#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1654 struct sockaddr_in6 *sin6;
1655#endif
1656
1657 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1658 return -ELOOP;
1659
1660 if (rq->sadb_x_ipsecrequest_mode == 0)
1661 return -EINVAL;
1662
1663 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1664 t->mode = rq->sadb_x_ipsecrequest_mode-1;
1665 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1666 t->optional = 1;
1667 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1668 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1669 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1670 t->reqid = 0;
1671 if (!t->reqid && !(t->reqid = gen_reqid()))
1672 return -ENOBUFS;
1673 }
1674
1675 /* addresses present only in tunnel mode */
1676 if (t->mode) {
1677 switch (xp->family) {
1678 case AF_INET:
1679 sin = (void*)(rq+1);
1680 if (sin->sin_family != AF_INET)
1681 return -EINVAL;
1682 t->saddr.a4 = sin->sin_addr.s_addr;
1683 sin++;
1684 if (sin->sin_family != AF_INET)
1685 return -EINVAL;
1686 t->id.daddr.a4 = sin->sin_addr.s_addr;
1687 break;
1688#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1689 case AF_INET6:
1690 sin6 = (void *)(rq+1);
1691 if (sin6->sin6_family != AF_INET6)
1692 return -EINVAL;
1693 memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1694 sin6++;
1695 if (sin6->sin6_family != AF_INET6)
1696 return -EINVAL;
1697 memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1698 break;
1699#endif
1700 default:
1701 return -EINVAL;
1702 }
1703 }
1704 /* No way to set this via kame pfkey */
1705 t->aalgos = t->ealgos = t->calgos = ~0;
1706 xp->xfrm_nr++;
1707 return 0;
1708}
1709
1710static int
1711parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1712{
1713 int err;
1714 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1715 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1716
1717 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1718 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1719 return err;
1720 len -= rq->sadb_x_ipsecrequest_len;
1721 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1722 }
1723 return 0;
1724}
1725
1726static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1727{
1728 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1729 int socklen = (xp->family == AF_INET ?
1730 sizeof(struct sockaddr_in) :
1731 sizeof(struct sockaddr_in6));
1732
1733 return sizeof(struct sadb_msg) +
1734 (sizeof(struct sadb_lifetime) * 3) +
1735 (sizeof(struct sadb_address) * 2) +
1736 (sockaddr_size * 2) +
1737 sizeof(struct sadb_x_policy) +
1738 (xp->xfrm_nr * (sizeof(struct sadb_x_ipsecrequest) +
1739 (socklen * 2)));
1740}
1741
1742static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1743{
1744 struct sk_buff *skb;
1745 int size;
1746
1747 size = pfkey_xfrm_policy2msg_size(xp);
1748
1749 skb = alloc_skb(size + 16, GFP_ATOMIC);
1750 if (skb == NULL)
1751 return ERR_PTR(-ENOBUFS);
1752
1753 return skb;
1754}
1755
1756static void pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1757{
1758 struct sadb_msg *hdr;
1759 struct sadb_address *addr;
1760 struct sadb_lifetime *lifetime;
1761 struct sadb_x_policy *pol;
1762 struct sockaddr_in *sin;
1763#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1764 struct sockaddr_in6 *sin6;
1765#endif
1766 int i;
1767 int size;
1768 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1769 int socklen = (xp->family == AF_INET ?
1770 sizeof(struct sockaddr_in) :
1771 sizeof(struct sockaddr_in6));
1772
1773 size = pfkey_xfrm_policy2msg_size(xp);
1774
1775 /* call should fill header later */
1776 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1777 memset(hdr, 0, size); /* XXX do we need this ? */
1778
1779 /* src address */
1780 addr = (struct sadb_address*) skb_put(skb,
1781 sizeof(struct sadb_address)+sockaddr_size);
1782 addr->sadb_address_len =
1783 (sizeof(struct sadb_address)+sockaddr_size)/
1784 sizeof(uint64_t);
1785 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1786 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1787 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1788 addr->sadb_address_reserved = 0;
1789 /* src address */
1790 if (xp->family == AF_INET) {
1791 sin = (struct sockaddr_in *) (addr + 1);
1792 sin->sin_family = AF_INET;
1793 sin->sin_addr.s_addr = xp->selector.saddr.a4;
1794 sin->sin_port = xp->selector.sport;
1795 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1796 }
1797#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1798 else if (xp->family == AF_INET6) {
1799 sin6 = (struct sockaddr_in6 *) (addr + 1);
1800 sin6->sin6_family = AF_INET6;
1801 sin6->sin6_port = xp->selector.sport;
1802 sin6->sin6_flowinfo = 0;
1803 memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
1804 sizeof(struct in6_addr));
1805 sin6->sin6_scope_id = 0;
1806 }
1807#endif
1808 else
1809 BUG();
1810
1811 /* dst address */
1812 addr = (struct sadb_address*) skb_put(skb,
1813 sizeof(struct sadb_address)+sockaddr_size);
1814 addr->sadb_address_len =
1815 (sizeof(struct sadb_address)+sockaddr_size)/
1816 sizeof(uint64_t);
1817 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1818 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1819 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
1820 addr->sadb_address_reserved = 0;
1821 if (xp->family == AF_INET) {
1822 sin = (struct sockaddr_in *) (addr + 1);
1823 sin->sin_family = AF_INET;
1824 sin->sin_addr.s_addr = xp->selector.daddr.a4;
1825 sin->sin_port = xp->selector.dport;
1826 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1827 }
1828#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1829 else if (xp->family == AF_INET6) {
1830 sin6 = (struct sockaddr_in6 *) (addr + 1);
1831 sin6->sin6_family = AF_INET6;
1832 sin6->sin6_port = xp->selector.dport;
1833 sin6->sin6_flowinfo = 0;
1834 memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
1835 sizeof(struct in6_addr));
1836 sin6->sin6_scope_id = 0;
1837 }
1838#endif
1839 else
1840 BUG();
1841
1842 /* hard time */
1843 lifetime = (struct sadb_lifetime *) skb_put(skb,
1844 sizeof(struct sadb_lifetime));
1845 lifetime->sadb_lifetime_len =
1846 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1847 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1848 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
1849 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
1850 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
1851 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
1852 /* soft time */
1853 lifetime = (struct sadb_lifetime *) skb_put(skb,
1854 sizeof(struct sadb_lifetime));
1855 lifetime->sadb_lifetime_len =
1856 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1857 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
1858 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
1859 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
1860 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
1861 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
1862 /* current time */
1863 lifetime = (struct sadb_lifetime *) skb_put(skb,
1864 sizeof(struct sadb_lifetime));
1865 lifetime->sadb_lifetime_len =
1866 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1867 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
1868 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
1869 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
1870 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
1871 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
1872
1873 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
1874 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
1875 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1876 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
1877 if (xp->action == XFRM_POLICY_ALLOW) {
1878 if (xp->xfrm_nr)
1879 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1880 else
1881 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
1882 }
1883 pol->sadb_x_policy_dir = dir+1;
1884 pol->sadb_x_policy_id = xp->index;
1885 pol->sadb_x_policy_priority = xp->priority;
1886
1887 for (i=0; i<xp->xfrm_nr; i++) {
1888 struct sadb_x_ipsecrequest *rq;
1889 struct xfrm_tmpl *t = xp->xfrm_vec + i;
1890 int req_size;
1891
1892 req_size = sizeof(struct sadb_x_ipsecrequest);
1893 if (t->mode)
1894 req_size += 2*socklen;
1895 else
1896 size -= 2*socklen;
1897 rq = (void*)skb_put(skb, req_size);
1898 pol->sadb_x_policy_len += req_size/8;
1899 memset(rq, 0, sizeof(*rq));
1900 rq->sadb_x_ipsecrequest_len = req_size;
1901 rq->sadb_x_ipsecrequest_proto = t->id.proto;
1902 rq->sadb_x_ipsecrequest_mode = t->mode+1;
1903 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
1904 if (t->reqid)
1905 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
1906 if (t->optional)
1907 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
1908 rq->sadb_x_ipsecrequest_reqid = t->reqid;
1909 if (t->mode) {
1910 switch (xp->family) {
1911 case AF_INET:
1912 sin = (void*)(rq+1);
1913 sin->sin_family = AF_INET;
1914 sin->sin_addr.s_addr = t->saddr.a4;
1915 sin->sin_port = 0;
1916 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1917 sin++;
1918 sin->sin_family = AF_INET;
1919 sin->sin_addr.s_addr = t->id.daddr.a4;
1920 sin->sin_port = 0;
1921 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1922 break;
1923#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1924 case AF_INET6:
1925 sin6 = (void*)(rq+1);
1926 sin6->sin6_family = AF_INET6;
1927 sin6->sin6_port = 0;
1928 sin6->sin6_flowinfo = 0;
1929 memcpy(&sin6->sin6_addr, t->saddr.a6,
1930 sizeof(struct in6_addr));
1931 sin6->sin6_scope_id = 0;
1932
1933 sin6++;
1934 sin6->sin6_family = AF_INET6;
1935 sin6->sin6_port = 0;
1936 sin6->sin6_flowinfo = 0;
1937 memcpy(&sin6->sin6_addr, t->id.daddr.a6,
1938 sizeof(struct in6_addr));
1939 sin6->sin6_scope_id = 0;
1940 break;
1941#endif
1942 default:
1943 break;
1944 }
1945 }
1946 }
1947 hdr->sadb_msg_len = size / sizeof(uint64_t);
1948 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
1949}
1950
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001951static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1952{
1953 struct sk_buff *out_skb;
1954 struct sadb_msg *out_hdr;
1955 int err;
1956
1957 out_skb = pfkey_xfrm_policy2msg_prep(xp);
1958 if (IS_ERR(out_skb)) {
1959 err = PTR_ERR(out_skb);
1960 goto out;
1961 }
1962 pfkey_xfrm_policy2msg(out_skb, xp, dir);
1963
1964 out_hdr = (struct sadb_msg *) out_skb->data;
1965 out_hdr->sadb_msg_version = PF_KEY_V2;
1966
1967 if (c->data && c->event == XFRM_SAP_DELETED)
1968 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
1969 else
1970 out_hdr->sadb_msg_type = event2poltype(c->event);
1971 out_hdr->sadb_msg_errno = 0;
1972 out_hdr->sadb_msg_seq = c->seq;
1973 out_hdr->sadb_msg_pid = c->pid;
1974 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1975out:
1976 return 0;
1977
1978}
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1981{
1982 int err;
1983 struct sadb_lifetime *lifetime;
1984 struct sadb_address *sa;
1985 struct sadb_x_policy *pol;
1986 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001987 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1990 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
1991 !ext_hdrs[SADB_X_EXT_POLICY-1])
1992 return -EINVAL;
1993
1994 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
1995 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
1996 return -EINVAL;
1997 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
1998 return -EINVAL;
1999
2000 xp = xfrm_policy_alloc(GFP_KERNEL);
2001 if (xp == NULL)
2002 return -ENOBUFS;
2003
2004 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2005 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2006 xp->priority = pol->sadb_x_policy_priority;
2007
2008 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2009 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2010 if (!xp->family) {
2011 err = -EINVAL;
2012 goto out;
2013 }
2014 xp->selector.family = xp->family;
2015 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2016 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2017 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2018 if (xp->selector.sport)
2019 xp->selector.sport_mask = ~0;
2020
2021 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2022 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2023 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2024
2025 /* Amusing, we set this twice. KAME apps appear to set same value
2026 * in both addresses.
2027 */
2028 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2029
2030 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2031 if (xp->selector.dport)
2032 xp->selector.dport_mask = ~0;
2033
2034 xp->lft.soft_byte_limit = XFRM_INF;
2035 xp->lft.hard_byte_limit = XFRM_INF;
2036 xp->lft.soft_packet_limit = XFRM_INF;
2037 xp->lft.hard_packet_limit = XFRM_INF;
2038 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2039 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2040 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2041 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2042 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2043 }
2044 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2045 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2046 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2047 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2048 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2049 }
2050 xp->xfrm_nr = 0;
2051 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2052 (err = parse_ipsecrequests(xp, pol)) < 0)
2053 goto out;
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2056 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2057 if (err) {
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002058 kfree(xp);
2059 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002062 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2063 c.event = XFRM_SAP_UPDATED;
2064 else
2065 c.event = XFRM_SAP_ADDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002067 c.seq = hdr->sadb_msg_seq;
2068 c.pid = hdr->sadb_msg_pid;
2069
2070 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return 0;
2073
2074out:
2075 kfree(xp);
2076 return err;
2077}
2078
2079static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2080{
2081 int err;
2082 struct sadb_address *sa;
2083 struct sadb_x_policy *pol;
2084 struct xfrm_policy *xp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002086 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2089 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2090 !ext_hdrs[SADB_X_EXT_POLICY-1])
2091 return -EINVAL;
2092
2093 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2094 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2095 return -EINVAL;
2096
2097 memset(&sel, 0, sizeof(sel));
2098
2099 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2100 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2101 sel.prefixlen_s = sa->sadb_address_prefixlen;
2102 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2103 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2104 if (sel.sport)
2105 sel.sport_mask = ~0;
2106
2107 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2108 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2109 sel.prefixlen_d = sa->sadb_address_prefixlen;
2110 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2111 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2112 if (sel.dport)
2113 sel.dport_mask = ~0;
2114
2115 xp = xfrm_policy_bysel(pol->sadb_x_policy_dir-1, &sel, 1);
2116 if (xp == NULL)
2117 return -ENOENT;
2118
2119 err = 0;
2120
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002121 c.seq = hdr->sadb_msg_seq;
2122 c.pid = hdr->sadb_msg_pid;
2123 c.event = XFRM_SAP_DELETED;
2124 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2125
2126 xfrm_pol_put(xp);
2127 return err;
2128}
2129
2130static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2131{
2132 int err;
2133 struct sk_buff *out_skb;
2134 struct sadb_msg *out_hdr;
2135 err = 0;
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2138 if (IS_ERR(out_skb)) {
2139 err = PTR_ERR(out_skb);
2140 goto out;
2141 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002142 pfkey_xfrm_policy2msg(out_skb, xp, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 out_hdr = (struct sadb_msg *) out_skb->data;
2145 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002146 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 out_hdr->sadb_msg_satype = 0;
2148 out_hdr->sadb_msg_errno = 0;
2149 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2150 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002151 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 err = 0;
2153
2154out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return err;
2156}
2157
2158static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2159{
2160 int err;
2161 struct sadb_x_policy *pol;
2162 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002163 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2166 return -EINVAL;
2167
2168 xp = xfrm_policy_byid(0, pol->sadb_x_policy_id,
2169 hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2170 if (xp == NULL)
2171 return -ENOENT;
2172
2173 err = 0;
2174
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002175 c.seq = hdr->sadb_msg_seq;
2176 c.pid = hdr->sadb_msg_pid;
2177 if (hdr->sadb_msg_type == SADB_X_SPDDELETE2) {
2178 c.data = 1; // to signal pfkey of SADB_X_SPDDELETE2
2179 c.event = XFRM_SAP_DELETED;
2180 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2181 } else {
2182 err = key_pol_get_resp(sk, xp, hdr, pol->sadb_x_policy_dir-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 xfrm_pol_put(xp);
2186 return err;
2187}
2188
2189static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2190{
2191 struct pfkey_dump_data *data = ptr;
2192 struct sk_buff *out_skb;
2193 struct sadb_msg *out_hdr;
2194
2195 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2196 if (IS_ERR(out_skb))
2197 return PTR_ERR(out_skb);
2198
2199 pfkey_xfrm_policy2msg(out_skb, xp, dir);
2200
2201 out_hdr = (struct sadb_msg *) out_skb->data;
2202 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
2203 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2204 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2205 out_hdr->sadb_msg_errno = 0;
2206 out_hdr->sadb_msg_seq = count;
2207 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
2208 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
2209 return 0;
2210}
2211
2212static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2213{
2214 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
2215
2216 return xfrm_policy_walk(dump_sp, &data);
2217}
2218
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002219static int key_notify_policy_flush(struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002222 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002224 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (!skb_out)
2226 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002227 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2228 hdr->sadb_msg_seq = c->seq;
2229 hdr->sadb_msg_pid = c->pid;
2230 hdr->sadb_msg_version = PF_KEY_V2;
2231 hdr->sadb_msg_errno = (uint8_t) 0;
2232 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2233 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2234 return 0;
2235
2236}
2237
2238static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2239{
2240 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 xfrm_policy_flush();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002243 c.event = XFRM_SAP_FLUSHED;
2244 c.pid = hdr->sadb_msg_pid;
2245 c.seq = hdr->sadb_msg_seq;
2246 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 return 0;
2249}
2250
2251typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2252 struct sadb_msg *hdr, void **ext_hdrs);
2253static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2254 [SADB_RESERVED] = pfkey_reserved,
2255 [SADB_GETSPI] = pfkey_getspi,
2256 [SADB_UPDATE] = pfkey_add,
2257 [SADB_ADD] = pfkey_add,
2258 [SADB_DELETE] = pfkey_delete,
2259 [SADB_GET] = pfkey_get,
2260 [SADB_ACQUIRE] = pfkey_acquire,
2261 [SADB_REGISTER] = pfkey_register,
2262 [SADB_EXPIRE] = NULL,
2263 [SADB_FLUSH] = pfkey_flush,
2264 [SADB_DUMP] = pfkey_dump,
2265 [SADB_X_PROMISC] = pfkey_promisc,
2266 [SADB_X_PCHANGE] = NULL,
2267 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2268 [SADB_X_SPDADD] = pfkey_spdadd,
2269 [SADB_X_SPDDELETE] = pfkey_spddelete,
2270 [SADB_X_SPDGET] = pfkey_spdget,
2271 [SADB_X_SPDACQUIRE] = NULL,
2272 [SADB_X_SPDDUMP] = pfkey_spddump,
2273 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2274 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2275 [SADB_X_SPDDELETE2] = pfkey_spdget,
2276};
2277
2278static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2279{
2280 void *ext_hdrs[SADB_EXT_MAX];
2281 int err;
2282
2283 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2284 BROADCAST_PROMISC_ONLY, NULL);
2285
2286 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2287 err = parse_exthdrs(skb, hdr, ext_hdrs);
2288 if (!err) {
2289 err = -EOPNOTSUPP;
2290 if (pfkey_funcs[hdr->sadb_msg_type])
2291 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2292 }
2293 return err;
2294}
2295
2296static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2297{
2298 struct sadb_msg *hdr = NULL;
2299
2300 if (skb->len < sizeof(*hdr)) {
2301 *errp = -EMSGSIZE;
2302 } else {
2303 hdr = (struct sadb_msg *) skb->data;
2304 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2305 hdr->sadb_msg_reserved != 0 ||
2306 (hdr->sadb_msg_type <= SADB_RESERVED ||
2307 hdr->sadb_msg_type > SADB_MAX)) {
2308 hdr = NULL;
2309 *errp = -EINVAL;
2310 } else if (hdr->sadb_msg_len != (skb->len /
2311 sizeof(uint64_t)) ||
2312 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2313 sizeof(uint64_t))) {
2314 hdr = NULL;
2315 *errp = -EMSGSIZE;
2316 } else {
2317 *errp = 0;
2318 }
2319 }
2320 return hdr;
2321}
2322
2323static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2324{
2325 return t->aalgos & (1 << d->desc.sadb_alg_id);
2326}
2327
2328static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2329{
2330 return t->ealgos & (1 << d->desc.sadb_alg_id);
2331}
2332
2333static int count_ah_combs(struct xfrm_tmpl *t)
2334{
2335 int i, sz = 0;
2336
2337 for (i = 0; ; i++) {
2338 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2339 if (!aalg)
2340 break;
2341 if (aalg_tmpl_set(t, aalg) && aalg->available)
2342 sz += sizeof(struct sadb_comb);
2343 }
2344 return sz + sizeof(struct sadb_prop);
2345}
2346
2347static int count_esp_combs(struct xfrm_tmpl *t)
2348{
2349 int i, k, sz = 0;
2350
2351 for (i = 0; ; i++) {
2352 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2353 if (!ealg)
2354 break;
2355
2356 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2357 continue;
2358
2359 for (k = 1; ; k++) {
2360 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2361 if (!aalg)
2362 break;
2363
2364 if (aalg_tmpl_set(t, aalg) && aalg->available)
2365 sz += sizeof(struct sadb_comb);
2366 }
2367 }
2368 return sz + sizeof(struct sadb_prop);
2369}
2370
2371static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2372{
2373 struct sadb_prop *p;
2374 int i;
2375
2376 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2377 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2378 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2379 p->sadb_prop_replay = 32;
2380 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2381
2382 for (i = 0; ; i++) {
2383 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2384 if (!aalg)
2385 break;
2386
2387 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2388 struct sadb_comb *c;
2389 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2390 memset(c, 0, sizeof(*c));
2391 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2392 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2393 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2394 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2395 c->sadb_comb_hard_addtime = 24*60*60;
2396 c->sadb_comb_soft_addtime = 20*60*60;
2397 c->sadb_comb_hard_usetime = 8*60*60;
2398 c->sadb_comb_soft_usetime = 7*60*60;
2399 }
2400 }
2401}
2402
2403static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2404{
2405 struct sadb_prop *p;
2406 int i, k;
2407
2408 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2409 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2410 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2411 p->sadb_prop_replay = 32;
2412 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2413
2414 for (i=0; ; i++) {
2415 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2416 if (!ealg)
2417 break;
2418
2419 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2420 continue;
2421
2422 for (k = 1; ; k++) {
2423 struct sadb_comb *c;
2424 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2425 if (!aalg)
2426 break;
2427 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2428 continue;
2429 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2430 memset(c, 0, sizeof(*c));
2431 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2432 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2433 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2434 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2435 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2436 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2437 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2438 c->sadb_comb_hard_addtime = 24*60*60;
2439 c->sadb_comb_soft_addtime = 20*60*60;
2440 c->sadb_comb_hard_usetime = 8*60*60;
2441 c->sadb_comb_soft_usetime = 7*60*60;
2442 }
2443 }
2444}
2445
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002446static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2447{
2448 return 0;
2449}
2450
2451static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
2453 struct sk_buff *out_skb;
2454 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002455 int hard;
2456 int hsc;
2457
2458 hard = c->data;
2459 if (hard)
2460 hsc = 2;
2461 else
2462 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 out_skb = pfkey_xfrm_state2msg(x, 0, hsc);
2465 if (IS_ERR(out_skb))
2466 return PTR_ERR(out_skb);
2467
2468 out_hdr = (struct sadb_msg *) out_skb->data;
2469 out_hdr->sadb_msg_version = PF_KEY_V2;
2470 out_hdr->sadb_msg_type = SADB_EXPIRE;
2471 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2472 out_hdr->sadb_msg_errno = 0;
2473 out_hdr->sadb_msg_reserved = 0;
2474 out_hdr->sadb_msg_seq = 0;
2475 out_hdr->sadb_msg_pid = 0;
2476
2477 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2478 return 0;
2479}
2480
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002481static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2482{
2483 switch (c->event) {
2484 case XFRM_SAP_EXPIRED:
2485 return key_notify_sa_expire(x, c);
2486 case XFRM_SAP_DELETED:
2487 case XFRM_SAP_ADDED:
2488 case XFRM_SAP_UPDATED:
2489 return key_notify_sa(x, c);
2490 case XFRM_SAP_FLUSHED:
2491 return key_notify_sa_flush(c);
2492 default:
2493 printk("pfkey: Unknown SA event %d\n", c->event);
2494 break;
2495 }
2496
2497 return 0;
2498}
2499
2500static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2501{
2502 switch (c->event) {
2503 case XFRM_SAP_EXPIRED:
2504 return key_notify_policy_expire(xp, c);
2505 case XFRM_SAP_DELETED:
2506 case XFRM_SAP_ADDED:
2507 case XFRM_SAP_UPDATED:
2508 return key_notify_policy(xp, dir, c);
2509 case XFRM_SAP_FLUSHED:
2510 return key_notify_policy_flush(c);
2511 default:
2512 printk("pfkey: Unknown policy event %d\n", c->event);
2513 break;
2514 }
2515
2516 return 0;
2517}
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519static u32 get_acqseq(void)
2520{
2521 u32 res;
2522 static u32 acqseq;
2523 static DEFINE_SPINLOCK(acqseq_lock);
2524
2525 spin_lock_bh(&acqseq_lock);
2526 res = (++acqseq ? : ++acqseq);
2527 spin_unlock_bh(&acqseq_lock);
2528 return res;
2529}
2530
2531static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2532{
2533 struct sk_buff *skb;
2534 struct sadb_msg *hdr;
2535 struct sadb_address *addr;
2536 struct sadb_x_policy *pol;
2537 struct sockaddr_in *sin;
2538#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2539 struct sockaddr_in6 *sin6;
2540#endif
2541 int sockaddr_size;
2542 int size;
2543
2544 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2545 if (!sockaddr_size)
2546 return -EINVAL;
2547
2548 size = sizeof(struct sadb_msg) +
2549 (sizeof(struct sadb_address) * 2) +
2550 (sockaddr_size * 2) +
2551 sizeof(struct sadb_x_policy);
2552
2553 if (x->id.proto == IPPROTO_AH)
2554 size += count_ah_combs(t);
2555 else if (x->id.proto == IPPROTO_ESP)
2556 size += count_esp_combs(t);
2557
2558 skb = alloc_skb(size + 16, GFP_ATOMIC);
2559 if (skb == NULL)
2560 return -ENOMEM;
2561
2562 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2563 hdr->sadb_msg_version = PF_KEY_V2;
2564 hdr->sadb_msg_type = SADB_ACQUIRE;
2565 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2566 hdr->sadb_msg_len = size / sizeof(uint64_t);
2567 hdr->sadb_msg_errno = 0;
2568 hdr->sadb_msg_reserved = 0;
2569 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2570 hdr->sadb_msg_pid = 0;
2571
2572 /* src address */
2573 addr = (struct sadb_address*) skb_put(skb,
2574 sizeof(struct sadb_address)+sockaddr_size);
2575 addr->sadb_address_len =
2576 (sizeof(struct sadb_address)+sockaddr_size)/
2577 sizeof(uint64_t);
2578 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2579 addr->sadb_address_proto = 0;
2580 addr->sadb_address_reserved = 0;
2581 if (x->props.family == AF_INET) {
2582 addr->sadb_address_prefixlen = 32;
2583
2584 sin = (struct sockaddr_in *) (addr + 1);
2585 sin->sin_family = AF_INET;
2586 sin->sin_addr.s_addr = x->props.saddr.a4;
2587 sin->sin_port = 0;
2588 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2589 }
2590#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2591 else if (x->props.family == AF_INET6) {
2592 addr->sadb_address_prefixlen = 128;
2593
2594 sin6 = (struct sockaddr_in6 *) (addr + 1);
2595 sin6->sin6_family = AF_INET6;
2596 sin6->sin6_port = 0;
2597 sin6->sin6_flowinfo = 0;
2598 memcpy(&sin6->sin6_addr,
2599 x->props.saddr.a6, sizeof(struct in6_addr));
2600 sin6->sin6_scope_id = 0;
2601 }
2602#endif
2603 else
2604 BUG();
2605
2606 /* dst address */
2607 addr = (struct sadb_address*) skb_put(skb,
2608 sizeof(struct sadb_address)+sockaddr_size);
2609 addr->sadb_address_len =
2610 (sizeof(struct sadb_address)+sockaddr_size)/
2611 sizeof(uint64_t);
2612 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2613 addr->sadb_address_proto = 0;
2614 addr->sadb_address_reserved = 0;
2615 if (x->props.family == AF_INET) {
2616 addr->sadb_address_prefixlen = 32;
2617
2618 sin = (struct sockaddr_in *) (addr + 1);
2619 sin->sin_family = AF_INET;
2620 sin->sin_addr.s_addr = x->id.daddr.a4;
2621 sin->sin_port = 0;
2622 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2623 }
2624#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2625 else if (x->props.family == AF_INET6) {
2626 addr->sadb_address_prefixlen = 128;
2627
2628 sin6 = (struct sockaddr_in6 *) (addr + 1);
2629 sin6->sin6_family = AF_INET6;
2630 sin6->sin6_port = 0;
2631 sin6->sin6_flowinfo = 0;
2632 memcpy(&sin6->sin6_addr,
2633 x->id.daddr.a6, sizeof(struct in6_addr));
2634 sin6->sin6_scope_id = 0;
2635 }
2636#endif
2637 else
2638 BUG();
2639
2640 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2641 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2642 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2643 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2644 pol->sadb_x_policy_dir = dir+1;
2645 pol->sadb_x_policy_id = xp->index;
2646
2647 /* Set sadb_comb's. */
2648 if (x->id.proto == IPPROTO_AH)
2649 dump_ah_combs(skb, t);
2650 else if (x->id.proto == IPPROTO_ESP)
2651 dump_esp_combs(skb, t);
2652
2653 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2654}
2655
2656static struct xfrm_policy *pfkey_compile_policy(u16 family, int opt,
2657 u8 *data, int len, int *dir)
2658{
2659 struct xfrm_policy *xp;
2660 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
2661
2662 switch (family) {
2663 case AF_INET:
2664 if (opt != IP_IPSEC_POLICY) {
2665 *dir = -EOPNOTSUPP;
2666 return NULL;
2667 }
2668 break;
2669#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2670 case AF_INET6:
2671 if (opt != IPV6_IPSEC_POLICY) {
2672 *dir = -EOPNOTSUPP;
2673 return NULL;
2674 }
2675 break;
2676#endif
2677 default:
2678 *dir = -EINVAL;
2679 return NULL;
2680 }
2681
2682 *dir = -EINVAL;
2683
2684 if (len < sizeof(struct sadb_x_policy) ||
2685 pol->sadb_x_policy_len*8 > len ||
2686 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
2687 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
2688 return NULL;
2689
2690 xp = xfrm_policy_alloc(GFP_ATOMIC);
2691 if (xp == NULL) {
2692 *dir = -ENOBUFS;
2693 return NULL;
2694 }
2695
2696 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2697 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2698
2699 xp->lft.soft_byte_limit = XFRM_INF;
2700 xp->lft.hard_byte_limit = XFRM_INF;
2701 xp->lft.soft_packet_limit = XFRM_INF;
2702 xp->lft.hard_packet_limit = XFRM_INF;
2703 xp->family = family;
2704
2705 xp->xfrm_nr = 0;
2706 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2707 (*dir = parse_ipsecrequests(xp, pol)) < 0)
2708 goto out;
2709
2710 *dir = pol->sadb_x_policy_dir-1;
2711 return xp;
2712
2713out:
2714 kfree(xp);
2715 return NULL;
2716}
2717
2718static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport)
2719{
2720 struct sk_buff *skb;
2721 struct sadb_msg *hdr;
2722 struct sadb_sa *sa;
2723 struct sadb_address *addr;
2724 struct sadb_x_nat_t_port *n_port;
2725 struct sockaddr_in *sin;
2726#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2727 struct sockaddr_in6 *sin6;
2728#endif
2729 int sockaddr_size;
2730 int size;
2731 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
2732 struct xfrm_encap_tmpl *natt = NULL;
2733
2734 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2735 if (!sockaddr_size)
2736 return -EINVAL;
2737
2738 if (!satype)
2739 return -EINVAL;
2740
2741 if (!x->encap)
2742 return -EINVAL;
2743
2744 natt = x->encap;
2745
2746 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
2747 *
2748 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
2749 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
2750 */
2751
2752 size = sizeof(struct sadb_msg) +
2753 sizeof(struct sadb_sa) +
2754 (sizeof(struct sadb_address) * 2) +
2755 (sockaddr_size * 2) +
2756 (sizeof(struct sadb_x_nat_t_port) * 2);
2757
2758 skb = alloc_skb(size + 16, GFP_ATOMIC);
2759 if (skb == NULL)
2760 return -ENOMEM;
2761
2762 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2763 hdr->sadb_msg_version = PF_KEY_V2;
2764 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
2765 hdr->sadb_msg_satype = satype;
2766 hdr->sadb_msg_len = size / sizeof(uint64_t);
2767 hdr->sadb_msg_errno = 0;
2768 hdr->sadb_msg_reserved = 0;
2769 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2770 hdr->sadb_msg_pid = 0;
2771
2772 /* SA */
2773 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
2774 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
2775 sa->sadb_sa_exttype = SADB_EXT_SA;
2776 sa->sadb_sa_spi = x->id.spi;
2777 sa->sadb_sa_replay = 0;
2778 sa->sadb_sa_state = 0;
2779 sa->sadb_sa_auth = 0;
2780 sa->sadb_sa_encrypt = 0;
2781 sa->sadb_sa_flags = 0;
2782
2783 /* ADDRESS_SRC (old addr) */
2784 addr = (struct sadb_address*)
2785 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
2786 addr->sadb_address_len =
2787 (sizeof(struct sadb_address)+sockaddr_size)/
2788 sizeof(uint64_t);
2789 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2790 addr->sadb_address_proto = 0;
2791 addr->sadb_address_reserved = 0;
2792 if (x->props.family == AF_INET) {
2793 addr->sadb_address_prefixlen = 32;
2794
2795 sin = (struct sockaddr_in *) (addr + 1);
2796 sin->sin_family = AF_INET;
2797 sin->sin_addr.s_addr = x->props.saddr.a4;
2798 sin->sin_port = 0;
2799 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2800 }
2801#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2802 else if (x->props.family == AF_INET6) {
2803 addr->sadb_address_prefixlen = 128;
2804
2805 sin6 = (struct sockaddr_in6 *) (addr + 1);
2806 sin6->sin6_family = AF_INET6;
2807 sin6->sin6_port = 0;
2808 sin6->sin6_flowinfo = 0;
2809 memcpy(&sin6->sin6_addr,
2810 x->props.saddr.a6, sizeof(struct in6_addr));
2811 sin6->sin6_scope_id = 0;
2812 }
2813#endif
2814 else
2815 BUG();
2816
2817 /* NAT_T_SPORT (old port) */
2818 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
2819 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
2820 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
2821 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
2822 n_port->sadb_x_nat_t_port_reserved = 0;
2823
2824 /* ADDRESS_DST (new addr) */
2825 addr = (struct sadb_address*)
2826 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
2827 addr->sadb_address_len =
2828 (sizeof(struct sadb_address)+sockaddr_size)/
2829 sizeof(uint64_t);
2830 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2831 addr->sadb_address_proto = 0;
2832 addr->sadb_address_reserved = 0;
2833 if (x->props.family == AF_INET) {
2834 addr->sadb_address_prefixlen = 32;
2835
2836 sin = (struct sockaddr_in *) (addr + 1);
2837 sin->sin_family = AF_INET;
2838 sin->sin_addr.s_addr = ipaddr->a4;
2839 sin->sin_port = 0;
2840 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2841 }
2842#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2843 else if (x->props.family == AF_INET6) {
2844 addr->sadb_address_prefixlen = 128;
2845
2846 sin6 = (struct sockaddr_in6 *) (addr + 1);
2847 sin6->sin6_family = AF_INET6;
2848 sin6->sin6_port = 0;
2849 sin6->sin6_flowinfo = 0;
2850 memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
2851 sin6->sin6_scope_id = 0;
2852 }
2853#endif
2854 else
2855 BUG();
2856
2857 /* NAT_T_DPORT (new port) */
2858 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
2859 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
2860 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
2861 n_port->sadb_x_nat_t_port_port = sport;
2862 n_port->sadb_x_nat_t_port_reserved = 0;
2863
2864 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2865}
2866
2867static int pfkey_sendmsg(struct kiocb *kiocb,
2868 struct socket *sock, struct msghdr *msg, size_t len)
2869{
2870 struct sock *sk = sock->sk;
2871 struct sk_buff *skb = NULL;
2872 struct sadb_msg *hdr = NULL;
2873 int err;
2874
2875 err = -EOPNOTSUPP;
2876 if (msg->msg_flags & MSG_OOB)
2877 goto out;
2878
2879 err = -EMSGSIZE;
2880 if ((unsigned)len > sk->sk_sndbuf - 32)
2881 goto out;
2882
2883 err = -ENOBUFS;
2884 skb = alloc_skb(len, GFP_KERNEL);
2885 if (skb == NULL)
2886 goto out;
2887
2888 err = -EFAULT;
2889 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
2890 goto out;
2891
2892 hdr = pfkey_get_base_msg(skb, &err);
2893 if (!hdr)
2894 goto out;
2895
2896 down(&xfrm_cfg_sem);
2897 err = pfkey_process(sk, skb, hdr);
2898 up(&xfrm_cfg_sem);
2899
2900out:
2901 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
2902 err = 0;
2903 if (skb)
2904 kfree_skb(skb);
2905
2906 return err ? : len;
2907}
2908
2909static int pfkey_recvmsg(struct kiocb *kiocb,
2910 struct socket *sock, struct msghdr *msg, size_t len,
2911 int flags)
2912{
2913 struct sock *sk = sock->sk;
2914 struct sk_buff *skb;
2915 int copied, err;
2916
2917 err = -EINVAL;
2918 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
2919 goto out;
2920
2921 msg->msg_namelen = 0;
2922 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
2923 if (skb == NULL)
2924 goto out;
2925
2926 copied = skb->len;
2927 if (copied > len) {
2928 msg->msg_flags |= MSG_TRUNC;
2929 copied = len;
2930 }
2931
2932 skb->h.raw = skb->data;
2933 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2934 if (err)
2935 goto out_free;
2936
2937 sock_recv_timestamp(msg, sk, skb);
2938
2939 err = (flags & MSG_TRUNC) ? skb->len : copied;
2940
2941out_free:
2942 skb_free_datagram(sk, skb);
2943out:
2944 return err;
2945}
2946
2947static struct proto_ops pfkey_ops = {
2948 .family = PF_KEY,
2949 .owner = THIS_MODULE,
2950 /* Operations that make no sense on pfkey sockets. */
2951 .bind = sock_no_bind,
2952 .connect = sock_no_connect,
2953 .socketpair = sock_no_socketpair,
2954 .accept = sock_no_accept,
2955 .getname = sock_no_getname,
2956 .ioctl = sock_no_ioctl,
2957 .listen = sock_no_listen,
2958 .shutdown = sock_no_shutdown,
2959 .setsockopt = sock_no_setsockopt,
2960 .getsockopt = sock_no_getsockopt,
2961 .mmap = sock_no_mmap,
2962 .sendpage = sock_no_sendpage,
2963
2964 /* Now the operations that really occur. */
2965 .release = pfkey_release,
2966 .poll = datagram_poll,
2967 .sendmsg = pfkey_sendmsg,
2968 .recvmsg = pfkey_recvmsg,
2969};
2970
2971static struct net_proto_family pfkey_family_ops = {
2972 .family = PF_KEY,
2973 .create = pfkey_create,
2974 .owner = THIS_MODULE,
2975};
2976
2977#ifdef CONFIG_PROC_FS
2978static int pfkey_read_proc(char *buffer, char **start, off_t offset,
2979 int length, int *eof, void *data)
2980{
2981 off_t pos = 0;
2982 off_t begin = 0;
2983 int len = 0;
2984 struct sock *s;
2985 struct hlist_node *node;
2986
2987 len += sprintf(buffer,"sk RefCnt Rmem Wmem User Inode\n");
2988
2989 read_lock(&pfkey_table_lock);
2990
2991 sk_for_each(s, node, &pfkey_table) {
2992 len += sprintf(buffer+len,"%p %-6d %-6u %-6u %-6u %-6lu",
2993 s,
2994 atomic_read(&s->sk_refcnt),
2995 atomic_read(&s->sk_rmem_alloc),
2996 atomic_read(&s->sk_wmem_alloc),
2997 sock_i_uid(s),
2998 sock_i_ino(s)
2999 );
3000
3001 buffer[len++] = '\n';
3002
3003 pos = begin + len;
3004 if (pos < offset) {
3005 len = 0;
3006 begin = pos;
3007 }
3008 if(pos > offset + length)
3009 goto done;
3010 }
3011 *eof = 1;
3012
3013done:
3014 read_unlock(&pfkey_table_lock);
3015
3016 *start = buffer + (offset - begin);
3017 len -= (offset - begin);
3018
3019 if (len > length)
3020 len = length;
3021 if (len < 0)
3022 len = 0;
3023
3024 return len;
3025}
3026#endif
3027
3028static struct xfrm_mgr pfkeyv2_mgr =
3029{
3030 .id = "pfkeyv2",
3031 .notify = pfkey_send_notify,
3032 .acquire = pfkey_send_acquire,
3033 .compile_policy = pfkey_compile_policy,
3034 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003035 .notify_policy = pfkey_send_policy_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036};
3037
3038static void __exit ipsec_pfkey_exit(void)
3039{
3040 xfrm_unregister_km(&pfkeyv2_mgr);
3041 remove_proc_entry("net/pfkey", NULL);
3042 sock_unregister(PF_KEY);
3043 proto_unregister(&key_proto);
3044}
3045
3046static int __init ipsec_pfkey_init(void)
3047{
3048 int err = proto_register(&key_proto, 0);
3049
3050 if (err != 0)
3051 goto out;
3052
3053 err = sock_register(&pfkey_family_ops);
3054 if (err != 0)
3055 goto out_unregister_key_proto;
3056#ifdef CONFIG_PROC_FS
3057 err = -ENOMEM;
3058 if (create_proc_read_entry("net/pfkey", 0, NULL, pfkey_read_proc, NULL) == NULL)
3059 goto out_sock_unregister;
3060#endif
3061 err = xfrm_register_km(&pfkeyv2_mgr);
3062 if (err != 0)
3063 goto out_remove_proc_entry;
3064out:
3065 return err;
3066out_remove_proc_entry:
3067#ifdef CONFIG_PROC_FS
3068 remove_proc_entry("net/pfkey", NULL);
3069out_sock_unregister:
3070#endif
3071 sock_unregister(PF_KEY);
3072out_unregister_key_proto:
3073 proto_unregister(&key_proto);
3074 goto out;
3075}
3076
3077module_init(ipsec_pfkey_init);
3078module_exit(ipsec_pfkey_exit);
3079MODULE_LICENSE("GPL");
3080MODULE_ALIAS_NETPROTO(PF_KEY);