blob: 83b443ddc72f57c044e7f00f961f7e41d83fc148 [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
Randy Dunlap4fc268d2006-01-11 12:17:47 -080017#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#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
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800116static const struct proto_ops pfkey_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
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,
Al Virodd0fc662005-10-07 07:46:04 +0100188 gfp_t allocation, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
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
Al Virodd0fc662005-10-07 07:46:04 +0100220static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 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;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800300 BUG_ON(err <= 0 || err >= 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
303 pfkey_hdr_dup(hdr, orig);
304 hdr->sadb_msg_errno = (uint8_t) err;
305 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
306 sizeof(uint64_t));
307
308 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);
309
310 return 0;
311}
312
313static u8 sadb_ext_min_len[] = {
314 [SADB_EXT_RESERVED] = (u8) 0,
315 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
316 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
317 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
318 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
319 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
320 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
321 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
322 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
323 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
324 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
325 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
326 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
327 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
328 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
329 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
330 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
331 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
332 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
333 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
334 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
335 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
336 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
337 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
Trent Jaegerdf718372005-12-13 23:12:27 -0800338 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339};
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
Trent Jaegerdf718372005-12-13 23:12:27 -0800386static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx)
387{
388 int len = 0;
389
390 len += sizeof(struct sadb_x_sec_ctx);
391 len += sec_ctx->sadb_x_ctx_len;
392 len += sizeof(uint64_t) - 1;
393 len /= sizeof(uint64_t);
394
395 return len;
396}
397
398static inline int verify_sec_ctx_len(void *p)
399{
400 struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p;
401 int len;
402
403 if (sec_ctx->sadb_x_ctx_len > PAGE_SIZE)
404 return -EINVAL;
405
406 len = pfkey_sec_ctx_len(sec_ctx);
407
408 if (sec_ctx->sadb_x_sec_len != len)
409 return -EINVAL;
410
411 return 0;
412}
413
414static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx)
415{
416 struct xfrm_user_sec_ctx *uctx = NULL;
417 int ctx_size = sec_ctx->sadb_x_ctx_len;
418
419 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
420
421 if (!uctx)
422 return NULL;
423
424 uctx->len = pfkey_sec_ctx_len(sec_ctx);
425 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
426 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
427 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
428 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
429 memcpy(uctx + 1, sec_ctx + 1,
430 uctx->ctx_len);
431
432 return uctx;
433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435static int present_and_same_family(struct sadb_address *src,
436 struct sadb_address *dst)
437{
438 struct sockaddr *s_addr, *d_addr;
439
440 if (!src || !dst)
441 return 0;
442
443 s_addr = (struct sockaddr *)(src + 1);
444 d_addr = (struct sockaddr *)(dst + 1);
445 if (s_addr->sa_family != d_addr->sa_family)
446 return 0;
447 if (s_addr->sa_family != AF_INET
448#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
449 && s_addr->sa_family != AF_INET6
450#endif
451 )
452 return 0;
453
454 return 1;
455}
456
457static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
458{
459 char *p = (char *) hdr;
460 int len = skb->len;
461
462 len -= sizeof(*hdr);
463 p += sizeof(*hdr);
464 while (len > 0) {
465 struct sadb_ext *ehdr = (struct sadb_ext *) p;
466 uint16_t ext_type;
467 int ext_len;
468
469 ext_len = ehdr->sadb_ext_len;
470 ext_len *= sizeof(uint64_t);
471 ext_type = ehdr->sadb_ext_type;
472 if (ext_len < sizeof(uint64_t) ||
473 ext_len > len ||
474 ext_type == SADB_EXT_RESERVED)
475 return -EINVAL;
476
477 if (ext_type <= SADB_EXT_MAX) {
478 int min = (int) sadb_ext_min_len[ext_type];
479 if (ext_len < min)
480 return -EINVAL;
481 if (ext_hdrs[ext_type-1] != NULL)
482 return -EINVAL;
483 if (ext_type == SADB_EXT_ADDRESS_SRC ||
484 ext_type == SADB_EXT_ADDRESS_DST ||
485 ext_type == SADB_EXT_ADDRESS_PROXY ||
486 ext_type == SADB_X_EXT_NAT_T_OA) {
487 if (verify_address_len(p))
488 return -EINVAL;
489 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800490 if (ext_type == SADB_X_EXT_SEC_CTX) {
491 if (verify_sec_ctx_len(p))
492 return -EINVAL;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 ext_hdrs[ext_type-1] = p;
495 }
496 p += ext_len;
497 len -= ext_len;
498 }
499
500 return 0;
501}
502
503static uint16_t
504pfkey_satype2proto(uint8_t satype)
505{
506 switch (satype) {
507 case SADB_SATYPE_UNSPEC:
508 return IPSEC_PROTO_ANY;
509 case SADB_SATYPE_AH:
510 return IPPROTO_AH;
511 case SADB_SATYPE_ESP:
512 return IPPROTO_ESP;
513 case SADB_X_SATYPE_IPCOMP:
514 return IPPROTO_COMP;
515 break;
516 default:
517 return 0;
518 }
519 /* NOTREACHED */
520}
521
522static uint8_t
523pfkey_proto2satype(uint16_t proto)
524{
525 switch (proto) {
526 case IPPROTO_AH:
527 return SADB_SATYPE_AH;
528 case IPPROTO_ESP:
529 return SADB_SATYPE_ESP;
530 case IPPROTO_COMP:
531 return SADB_X_SATYPE_IPCOMP;
532 break;
533 default:
534 return 0;
535 }
536 /* NOTREACHED */
537}
538
539/* BTW, this scheme means that there is no way with PFKEY2 sockets to
540 * say specifically 'just raw sockets' as we encode them as 255.
541 */
542
543static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
544{
545 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
546}
547
548static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
549{
550 return (proto ? proto : IPSEC_PROTO_ANY);
551}
552
553static int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr,
554 xfrm_address_t *xaddr)
555{
556 switch (((struct sockaddr*)(addr + 1))->sa_family) {
557 case AF_INET:
558 xaddr->a4 =
559 ((struct sockaddr_in *)(addr + 1))->sin_addr.s_addr;
560 return AF_INET;
561#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
562 case AF_INET6:
563 memcpy(xaddr->a6,
564 &((struct sockaddr_in6 *)(addr + 1))->sin6_addr,
565 sizeof(struct in6_addr));
566 return AF_INET6;
567#endif
568 default:
569 return 0;
570 }
571 /* NOTREACHED */
572}
573
574static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
575{
576 struct sadb_sa *sa;
577 struct sadb_address *addr;
578 uint16_t proto;
579 unsigned short family;
580 xfrm_address_t *xaddr;
581
582 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
583 if (sa == NULL)
584 return NULL;
585
586 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
587 if (proto == 0)
588 return NULL;
589
590 /* sadb_address_len should be checked by caller */
591 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
592 if (addr == NULL)
593 return NULL;
594
595 family = ((struct sockaddr *)(addr + 1))->sa_family;
596 switch (family) {
597 case AF_INET:
598 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
599 break;
600#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
601 case AF_INET6:
602 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
603 break;
604#endif
605 default:
606 xaddr = NULL;
607 }
608
609 if (!xaddr)
610 return NULL;
611
612 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
613}
614
615#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
616static int
617pfkey_sockaddr_size(sa_family_t family)
618{
619 switch (family) {
620 case AF_INET:
621 return PFKEY_ALIGN8(sizeof(struct sockaddr_in));
622#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
623 case AF_INET6:
624 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6));
625#endif
626 default:
627 return 0;
628 }
629 /* NOTREACHED */
630}
631
632static struct sk_buff * pfkey_xfrm_state2msg(struct xfrm_state *x, int add_keys, int hsc)
633{
634 struct sk_buff *skb;
635 struct sadb_msg *hdr;
636 struct sadb_sa *sa;
637 struct sadb_lifetime *lifetime;
638 struct sadb_address *addr;
639 struct sadb_key *key;
640 struct sadb_x_sa2 *sa2;
641 struct sockaddr_in *sin;
Trent Jaegerdf718372005-12-13 23:12:27 -0800642 struct sadb_x_sec_ctx *sec_ctx;
643 struct xfrm_sec_ctx *xfrm_ctx;
644 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
646 struct sockaddr_in6 *sin6;
647#endif
648 int size;
649 int auth_key_size = 0;
650 int encrypt_key_size = 0;
651 int sockaddr_size;
652 struct xfrm_encap_tmpl *natt = NULL;
653
654 /* address family check */
655 sockaddr_size = pfkey_sockaddr_size(x->props.family);
656 if (!sockaddr_size)
657 return ERR_PTR(-EINVAL);
658
659 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
660 key(AE), (identity(SD),) (sensitivity)> */
661 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
662 sizeof(struct sadb_lifetime) +
663 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
664 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
665 sizeof(struct sadb_address)*2 +
666 sockaddr_size*2 +
667 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800668
669 if ((xfrm_ctx = x->security)) {
670 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
671 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
672 }
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /* identity & sensitivity */
675
676 if ((x->props.family == AF_INET &&
677 x->sel.saddr.a4 != x->props.saddr.a4)
678#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
679 || (x->props.family == AF_INET6 &&
680 memcmp (x->sel.saddr.a6, x->props.saddr.a6, sizeof (struct in6_addr)))
681#endif
682 )
683 size += sizeof(struct sadb_address) + sockaddr_size;
684
685 if (add_keys) {
686 if (x->aalg && x->aalg->alg_key_len) {
687 auth_key_size =
688 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
689 size += sizeof(struct sadb_key) + auth_key_size;
690 }
691 if (x->ealg && x->ealg->alg_key_len) {
692 encrypt_key_size =
693 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
694 size += sizeof(struct sadb_key) + encrypt_key_size;
695 }
696 }
697 if (x->encap)
698 natt = x->encap;
699
700 if (natt && natt->encap_type) {
701 size += sizeof(struct sadb_x_nat_t_type);
702 size += sizeof(struct sadb_x_nat_t_port);
703 size += sizeof(struct sadb_x_nat_t_port);
704 }
705
706 skb = alloc_skb(size + 16, GFP_ATOMIC);
707 if (skb == NULL)
708 return ERR_PTR(-ENOBUFS);
709
710 /* call should fill header later */
711 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
712 memset(hdr, 0, size); /* XXX do we need this ? */
713 hdr->sadb_msg_len = size / sizeof(uint64_t);
714
715 /* sa */
716 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
717 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
718 sa->sadb_sa_exttype = SADB_EXT_SA;
719 sa->sadb_sa_spi = x->id.spi;
720 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700721 switch (x->km.state) {
722 case XFRM_STATE_VALID:
723 sa->sadb_sa_state = x->km.dying ?
724 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
725 break;
726 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700728 break;
729 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700731 break;
732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 sa->sadb_sa_auth = 0;
734 if (x->aalg) {
735 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
736 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
737 }
738 sa->sadb_sa_encrypt = 0;
739 BUG_ON(x->ealg && x->calg);
740 if (x->ealg) {
741 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
742 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
743 }
744 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
745 if (x->calg) {
746 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
747 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
748 }
749
750 sa->sadb_sa_flags = 0;
751 if (x->props.flags & XFRM_STATE_NOECN)
752 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
753 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
754 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700755 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
756 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 /* hard time */
759 if (hsc & 2) {
760 lifetime = (struct sadb_lifetime *) skb_put(skb,
761 sizeof(struct sadb_lifetime));
762 lifetime->sadb_lifetime_len =
763 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
764 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
765 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
766 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
767 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
768 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
769 }
770 /* soft time */
771 if (hsc & 1) {
772 lifetime = (struct sadb_lifetime *) skb_put(skb,
773 sizeof(struct sadb_lifetime));
774 lifetime->sadb_lifetime_len =
775 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
776 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
777 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
778 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
779 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
780 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
781 }
782 /* current time */
783 lifetime = (struct sadb_lifetime *) skb_put(skb,
784 sizeof(struct sadb_lifetime));
785 lifetime->sadb_lifetime_len =
786 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
787 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
788 lifetime->sadb_lifetime_allocations = x->curlft.packets;
789 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
790 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
791 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
792 /* src address */
793 addr = (struct sadb_address*) skb_put(skb,
794 sizeof(struct sadb_address)+sockaddr_size);
795 addr->sadb_address_len =
796 (sizeof(struct sadb_address)+sockaddr_size)/
797 sizeof(uint64_t);
798 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
799 /* "if the ports are non-zero, then the sadb_address_proto field,
800 normally zero, MUST be filled in with the transport
801 protocol's number." - RFC2367 */
802 addr->sadb_address_proto = 0;
803 addr->sadb_address_reserved = 0;
804 if (x->props.family == AF_INET) {
805 addr->sadb_address_prefixlen = 32;
806
807 sin = (struct sockaddr_in *) (addr + 1);
808 sin->sin_family = AF_INET;
809 sin->sin_addr.s_addr = x->props.saddr.a4;
810 sin->sin_port = 0;
811 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
812 }
813#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
814 else if (x->props.family == AF_INET6) {
815 addr->sadb_address_prefixlen = 128;
816
817 sin6 = (struct sockaddr_in6 *) (addr + 1);
818 sin6->sin6_family = AF_INET6;
819 sin6->sin6_port = 0;
820 sin6->sin6_flowinfo = 0;
821 memcpy(&sin6->sin6_addr, x->props.saddr.a6,
822 sizeof(struct in6_addr));
823 sin6->sin6_scope_id = 0;
824 }
825#endif
826 else
827 BUG();
828
829 /* dst address */
830 addr = (struct sadb_address*) skb_put(skb,
831 sizeof(struct sadb_address)+sockaddr_size);
832 addr->sadb_address_len =
833 (sizeof(struct sadb_address)+sockaddr_size)/
834 sizeof(uint64_t);
835 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
836 addr->sadb_address_proto = 0;
837 addr->sadb_address_prefixlen = 32; /* XXX */
838 addr->sadb_address_reserved = 0;
839 if (x->props.family == AF_INET) {
840 sin = (struct sockaddr_in *) (addr + 1);
841 sin->sin_family = AF_INET;
842 sin->sin_addr.s_addr = x->id.daddr.a4;
843 sin->sin_port = 0;
844 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
845
846 if (x->sel.saddr.a4 != x->props.saddr.a4) {
847 addr = (struct sadb_address*) skb_put(skb,
848 sizeof(struct sadb_address)+sockaddr_size);
849 addr->sadb_address_len =
850 (sizeof(struct sadb_address)+sockaddr_size)/
851 sizeof(uint64_t);
852 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
853 addr->sadb_address_proto =
854 pfkey_proto_from_xfrm(x->sel.proto);
855 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
856 addr->sadb_address_reserved = 0;
857
858 sin = (struct sockaddr_in *) (addr + 1);
859 sin->sin_family = AF_INET;
860 sin->sin_addr.s_addr = x->sel.saddr.a4;
861 sin->sin_port = x->sel.sport;
862 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
863 }
864 }
865#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
866 else if (x->props.family == AF_INET6) {
867 addr->sadb_address_prefixlen = 128;
868
869 sin6 = (struct sockaddr_in6 *) (addr + 1);
870 sin6->sin6_family = AF_INET6;
871 sin6->sin6_port = 0;
872 sin6->sin6_flowinfo = 0;
873 memcpy(&sin6->sin6_addr, x->id.daddr.a6, sizeof(struct in6_addr));
874 sin6->sin6_scope_id = 0;
875
876 if (memcmp (x->sel.saddr.a6, x->props.saddr.a6,
877 sizeof(struct in6_addr))) {
878 addr = (struct sadb_address *) skb_put(skb,
879 sizeof(struct sadb_address)+sockaddr_size);
880 addr->sadb_address_len =
881 (sizeof(struct sadb_address)+sockaddr_size)/
882 sizeof(uint64_t);
883 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
884 addr->sadb_address_proto =
885 pfkey_proto_from_xfrm(x->sel.proto);
886 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
887 addr->sadb_address_reserved = 0;
888
889 sin6 = (struct sockaddr_in6 *) (addr + 1);
890 sin6->sin6_family = AF_INET6;
891 sin6->sin6_port = x->sel.sport;
892 sin6->sin6_flowinfo = 0;
893 memcpy(&sin6->sin6_addr, x->sel.saddr.a6,
894 sizeof(struct in6_addr));
895 sin6->sin6_scope_id = 0;
896 }
897 }
898#endif
899 else
900 BUG();
901
902 /* auth key */
903 if (add_keys && auth_key_size) {
904 key = (struct sadb_key *) skb_put(skb,
905 sizeof(struct sadb_key)+auth_key_size);
906 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
907 sizeof(uint64_t);
908 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
909 key->sadb_key_bits = x->aalg->alg_key_len;
910 key->sadb_key_reserved = 0;
911 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
912 }
913 /* encrypt key */
914 if (add_keys && encrypt_key_size) {
915 key = (struct sadb_key *) skb_put(skb,
916 sizeof(struct sadb_key)+encrypt_key_size);
917 key->sadb_key_len = (sizeof(struct sadb_key) +
918 encrypt_key_size) / sizeof(uint64_t);
919 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
920 key->sadb_key_bits = x->ealg->alg_key_len;
921 key->sadb_key_reserved = 0;
922 memcpy(key + 1, x->ealg->alg_key,
923 (x->ealg->alg_key_len+7)/8);
924 }
925
926 /* sa */
927 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
928 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
929 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
930 sa2->sadb_x_sa2_mode = x->props.mode + 1;
931 sa2->sadb_x_sa2_reserved1 = 0;
932 sa2->sadb_x_sa2_reserved2 = 0;
933 sa2->sadb_x_sa2_sequence = 0;
934 sa2->sadb_x_sa2_reqid = x->props.reqid;
935
936 if (natt && natt->encap_type) {
937 struct sadb_x_nat_t_type *n_type;
938 struct sadb_x_nat_t_port *n_port;
939
940 /* type */
941 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
942 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
943 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
944 n_type->sadb_x_nat_t_type_type = natt->encap_type;
945 n_type->sadb_x_nat_t_type_reserved[0] = 0;
946 n_type->sadb_x_nat_t_type_reserved[1] = 0;
947 n_type->sadb_x_nat_t_type_reserved[2] = 0;
948
949 /* source port */
950 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
951 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
952 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
953 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
954 n_port->sadb_x_nat_t_port_reserved = 0;
955
956 /* dest port */
957 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
958 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
959 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
960 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
961 n_port->sadb_x_nat_t_port_reserved = 0;
962 }
963
Trent Jaegerdf718372005-12-13 23:12:27 -0800964 /* security context */
965 if (xfrm_ctx) {
966 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
967 sizeof(struct sadb_x_sec_ctx) + ctx_size);
968 sec_ctx->sadb_x_sec_len =
969 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
970 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
971 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
972 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
973 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
974 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
975 xfrm_ctx->ctx_len);
976 }
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return skb;
979}
980
981static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
982 void **ext_hdrs)
983{
984 struct xfrm_state *x;
985 struct sadb_lifetime *lifetime;
986 struct sadb_sa *sa;
987 struct sadb_key *key;
Trent Jaegerdf718372005-12-13 23:12:27 -0800988 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 uint16_t proto;
990 int err;
991
992
993 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
994 if (!sa ||
995 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
996 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
997 return ERR_PTR(-EINVAL);
998 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
999 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1000 return ERR_PTR(-EINVAL);
1001 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1002 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1003 return ERR_PTR(-EINVAL);
1004 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1005 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1006 return ERR_PTR(-EINVAL);
1007
1008 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1009 if (proto == 0)
1010 return ERR_PTR(-EINVAL);
1011
1012 /* default error is no buffer space */
1013 err = -ENOBUFS;
1014
1015 /* RFC2367:
1016
1017 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1018 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1019 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1020 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1021 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1022 not true.
1023
1024 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1025 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1026 */
1027 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1028 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1029 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1030 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1031 return ERR_PTR(-EINVAL);
1032 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1033 if (key != NULL &&
1034 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1035 ((key->sadb_key_bits+7) / 8 == 0 ||
1036 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1037 return ERR_PTR(-EINVAL);
1038 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1039 if (key != NULL &&
1040 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1041 ((key->sadb_key_bits+7) / 8 == 0 ||
1042 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1043 return ERR_PTR(-EINVAL);
1044
1045 x = xfrm_state_alloc();
1046 if (x == NULL)
1047 return ERR_PTR(-ENOBUFS);
1048
1049 x->id.proto = proto;
1050 x->id.spi = sa->sadb_sa_spi;
1051 x->props.replay_window = sa->sadb_sa_replay;
1052 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1053 x->props.flags |= XFRM_STATE_NOECN;
1054 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1055 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001056 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1057 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
1060 if (lifetime != NULL) {
1061 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1062 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1063 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1064 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1065 }
1066 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
1067 if (lifetime != NULL) {
1068 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1069 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1070 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1071 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1072 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001073
1074 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
1075 if (sec_ctx != NULL) {
1076 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1077
1078 if (!uctx)
1079 goto out;
1080
1081 err = security_xfrm_state_alloc(x, uctx);
1082 kfree(uctx);
1083
1084 if (err)
1085 goto out;
1086 }
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1089 if (sa->sadb_sa_auth) {
1090 int keysize = 0;
1091 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1092 if (!a) {
1093 err = -ENOSYS;
1094 goto out;
1095 }
1096 if (key)
1097 keysize = (key->sadb_key_bits + 7) / 8;
1098 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1099 if (!x->aalg)
1100 goto out;
1101 strcpy(x->aalg->alg_name, a->name);
1102 x->aalg->alg_key_len = 0;
1103 if (key) {
1104 x->aalg->alg_key_len = key->sadb_key_bits;
1105 memcpy(x->aalg->alg_key, key+1, keysize);
1106 }
1107 x->props.aalgo = sa->sadb_sa_auth;
1108 /* x->algo.flags = sa->sadb_sa_flags; */
1109 }
1110 if (sa->sadb_sa_encrypt) {
1111 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1112 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1113 if (!a) {
1114 err = -ENOSYS;
1115 goto out;
1116 }
1117 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1118 if (!x->calg)
1119 goto out;
1120 strcpy(x->calg->alg_name, a->name);
1121 x->props.calgo = sa->sadb_sa_encrypt;
1122 } else {
1123 int keysize = 0;
1124 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1125 if (!a) {
1126 err = -ENOSYS;
1127 goto out;
1128 }
1129 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1130 if (key)
1131 keysize = (key->sadb_key_bits + 7) / 8;
1132 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1133 if (!x->ealg)
1134 goto out;
1135 strcpy(x->ealg->alg_name, a->name);
1136 x->ealg->alg_key_len = 0;
1137 if (key) {
1138 x->ealg->alg_key_len = key->sadb_key_bits;
1139 memcpy(x->ealg->alg_key, key+1, keysize);
1140 }
1141 x->props.ealgo = sa->sadb_sa_encrypt;
1142 }
1143 }
1144 /* x->algo.flags = sa->sadb_sa_flags; */
1145
1146 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1147 &x->props.saddr);
1148 if (!x->props.family) {
1149 err = -EAFNOSUPPORT;
1150 goto out;
1151 }
1152 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1153 &x->id.daddr);
1154
1155 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1156 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
1157 x->props.mode = sa2->sadb_x_sa2_mode;
1158 if (x->props.mode)
1159 x->props.mode--;
1160 x->props.reqid = sa2->sadb_x_sa2_reqid;
1161 }
1162
1163 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1164 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1165
1166 /* Nobody uses this, but we try. */
1167 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1168 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1169 }
1170
1171 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1172 struct sadb_x_nat_t_type* n_type;
1173 struct xfrm_encap_tmpl *natt;
1174
1175 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1176 if (!x->encap)
1177 goto out;
1178
1179 natt = x->encap;
1180 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1181 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1182
1183 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1184 struct sadb_x_nat_t_port* n_port =
1185 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1186 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1187 }
1188 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1189 struct sadb_x_nat_t_port* n_port =
1190 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1191 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1192 }
1193 }
1194
Herbert Xu72cb6962005-06-20 13:18:08 -07001195 err = xfrm_init_state(x);
1196 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return x;
1201
1202out:
1203 x->km.state = XFRM_STATE_DEAD;
1204 xfrm_state_put(x);
1205 return ERR_PTR(err);
1206}
1207
1208static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1209{
1210 return -EOPNOTSUPP;
1211}
1212
1213static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1214{
1215 struct sk_buff *resp_skb;
1216 struct sadb_x_sa2 *sa2;
1217 struct sadb_address *saddr, *daddr;
1218 struct sadb_msg *out_hdr;
1219 struct xfrm_state *x = NULL;
1220 u8 mode;
1221 u32 reqid;
1222 u8 proto;
1223 unsigned short family;
1224 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1225
1226 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1227 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1228 return -EINVAL;
1229
1230 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1231 if (proto == 0)
1232 return -EINVAL;
1233
1234 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1235 mode = sa2->sadb_x_sa2_mode - 1;
1236 reqid = sa2->sadb_x_sa2_reqid;
1237 } else {
1238 mode = 0;
1239 reqid = 0;
1240 }
1241
1242 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1243 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1244
1245 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1246 switch (family) {
1247 case AF_INET:
1248 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1249 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1250 break;
1251#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1252 case AF_INET6:
1253 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1254 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1255 break;
1256#endif
1257 }
1258
1259 if (hdr->sadb_msg_seq) {
1260 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1261 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1262 xfrm_state_put(x);
1263 x = NULL;
1264 }
1265 }
1266
1267 if (!x)
1268 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1269
1270 if (x == NULL)
1271 return -ENOENT;
1272
1273 resp_skb = ERR_PTR(-ENOENT);
1274
1275 spin_lock_bh(&x->lock);
1276 if (x->km.state != XFRM_STATE_DEAD) {
1277 struct sadb_spirange *range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1278 u32 min_spi, max_spi;
1279
1280 if (range != NULL) {
1281 min_spi = range->sadb_spirange_min;
1282 max_spi = range->sadb_spirange_max;
1283 } else {
1284 min_spi = 0x100;
1285 max_spi = 0x0fffffff;
1286 }
1287 xfrm_alloc_spi(x, htonl(min_spi), htonl(max_spi));
1288 if (x->id.spi)
1289 resp_skb = pfkey_xfrm_state2msg(x, 0, 3);
1290 }
1291 spin_unlock_bh(&x->lock);
1292
1293 if (IS_ERR(resp_skb)) {
1294 xfrm_state_put(x);
1295 return PTR_ERR(resp_skb);
1296 }
1297
1298 out_hdr = (struct sadb_msg *) resp_skb->data;
1299 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1300 out_hdr->sadb_msg_type = SADB_GETSPI;
1301 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1302 out_hdr->sadb_msg_errno = 0;
1303 out_hdr->sadb_msg_reserved = 0;
1304 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1305 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1306
1307 xfrm_state_put(x);
1308
1309 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1310
1311 return 0;
1312}
1313
1314static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1315{
1316 struct xfrm_state *x;
1317
1318 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1319 return -EOPNOTSUPP;
1320
1321 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1322 return 0;
1323
1324 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1325 if (x == NULL)
1326 return 0;
1327
1328 spin_lock_bh(&x->lock);
1329 if (x->km.state == XFRM_STATE_ACQ) {
1330 x->km.state = XFRM_STATE_ERROR;
1331 wake_up(&km_waitq);
1332 }
1333 spin_unlock_bh(&x->lock);
1334 xfrm_state_put(x);
1335 return 0;
1336}
1337
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001338static inline int event2poltype(int event)
1339{
1340 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001341 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001342 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001343 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001344 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001345 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001346 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001347 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001348 // return SADB_X_SPDEXPIRE;
1349 default:
1350 printk("pfkey: Unknown policy event %d\n", event);
1351 break;
1352 }
1353
1354 return 0;
1355}
1356
1357static inline int event2keytype(int event)
1358{
1359 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001360 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001361 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001362 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001363 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001364 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001365 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001366 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001367 return SADB_EXPIRE;
1368 default:
1369 printk("pfkey: Unknown SA event %d\n", event);
1370 break;
1371 }
1372
1373 return 0;
1374}
1375
1376/* ADD/UPD/DEL */
1377static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1378{
1379 struct sk_buff *skb;
1380 struct sadb_msg *hdr;
1381 int hsc = 3;
1382
Herbert Xuf60f6b82005-06-18 22:44:37 -07001383 if (c->event == XFRM_MSG_DELSA)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001384 hsc = 0;
1385
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001386 skb = pfkey_xfrm_state2msg(x, 0, hsc);
1387
1388 if (IS_ERR(skb))
1389 return PTR_ERR(skb);
1390
1391 hdr = (struct sadb_msg *) skb->data;
1392 hdr->sadb_msg_version = PF_KEY_V2;
1393 hdr->sadb_msg_type = event2keytype(c->event);
1394 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1395 hdr->sadb_msg_errno = 0;
1396 hdr->sadb_msg_reserved = 0;
1397 hdr->sadb_msg_seq = c->seq;
1398 hdr->sadb_msg_pid = c->pid;
1399
1400 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1401
1402 return 0;
1403}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1406{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 struct xfrm_state *x;
1408 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001409 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 xfrm_probe_algs();
1412
1413 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1414 if (IS_ERR(x))
1415 return PTR_ERR(x);
1416
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001417 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (hdr->sadb_msg_type == SADB_ADD)
1419 err = xfrm_state_add(x);
1420 else
1421 err = xfrm_state_update(x);
1422
1423 if (err < 0) {
1424 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001425 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001426 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001429 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001430 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001431 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001432 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001433 c.seq = hdr->sadb_msg_seq;
1434 c.pid = hdr->sadb_msg_pid;
1435 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001436out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001437 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001438 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
1441static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1442{
1443 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001444 struct km_event c;
1445 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 if (!ext_hdrs[SADB_EXT_SA-1] ||
1448 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1449 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1450 return -EINVAL;
1451
1452 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1453 if (x == NULL)
1454 return -ESRCH;
1455
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001456 if ((err = security_xfrm_state_delete(x)))
1457 goto out;
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001460 err = -EPERM;
1461 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001464 err = xfrm_state_delete(x);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001465 if (err < 0)
1466 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001467
1468 c.seq = hdr->sadb_msg_seq;
1469 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001470 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001471 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001472out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 xfrm_state_put(x);
1474
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001475 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
1478static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1479{
1480 __u8 proto;
1481 struct sk_buff *out_skb;
1482 struct sadb_msg *out_hdr;
1483 struct xfrm_state *x;
1484
1485 if (!ext_hdrs[SADB_EXT_SA-1] ||
1486 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1487 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1488 return -EINVAL;
1489
1490 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1491 if (x == NULL)
1492 return -ESRCH;
1493
1494 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1495 proto = x->id.proto;
1496 xfrm_state_put(x);
1497 if (IS_ERR(out_skb))
1498 return PTR_ERR(out_skb);
1499
1500 out_hdr = (struct sadb_msg *) out_skb->data;
1501 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1502 out_hdr->sadb_msg_type = SADB_DUMP;
1503 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1504 out_hdr->sadb_msg_errno = 0;
1505 out_hdr->sadb_msg_reserved = 0;
1506 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1507 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1508 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1509
1510 return 0;
1511}
1512
Randy Dunlap00fa0232005-10-04 22:43:04 -07001513static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001514 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 struct sk_buff *skb;
1517 struct sadb_msg *hdr;
1518 int len, auth_len, enc_len, i;
1519
1520 auth_len = xfrm_count_auth_supported();
1521 if (auth_len) {
1522 auth_len *= sizeof(struct sadb_alg);
1523 auth_len += sizeof(struct sadb_supported);
1524 }
1525
1526 enc_len = xfrm_count_enc_supported();
1527 if (enc_len) {
1528 enc_len *= sizeof(struct sadb_alg);
1529 enc_len += sizeof(struct sadb_supported);
1530 }
1531
1532 len = enc_len + auth_len + sizeof(struct sadb_msg);
1533
1534 skb = alloc_skb(len + 16, allocation);
1535 if (!skb)
1536 goto out_put_algs;
1537
1538 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1539 pfkey_hdr_dup(hdr, orig);
1540 hdr->sadb_msg_errno = 0;
1541 hdr->sadb_msg_len = len / sizeof(uint64_t);
1542
1543 if (auth_len) {
1544 struct sadb_supported *sp;
1545 struct sadb_alg *ap;
1546
1547 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1548 ap = (struct sadb_alg *) (sp + 1);
1549
1550 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1551 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1552
1553 for (i = 0; ; i++) {
1554 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1555 if (!aalg)
1556 break;
1557 if (aalg->available)
1558 *ap++ = aalg->desc;
1559 }
1560 }
1561
1562 if (enc_len) {
1563 struct sadb_supported *sp;
1564 struct sadb_alg *ap;
1565
1566 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1567 ap = (struct sadb_alg *) (sp + 1);
1568
1569 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1570 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1571
1572 for (i = 0; ; i++) {
1573 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1574 if (!ealg)
1575 break;
1576 if (ealg->available)
1577 *ap++ = ealg->desc;
1578 }
1579 }
1580
1581out_put_algs:
1582 return skb;
1583}
1584
1585static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1586{
1587 struct pfkey_sock *pfk = pfkey_sk(sk);
1588 struct sk_buff *supp_skb;
1589
1590 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1591 return -EINVAL;
1592
1593 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1594 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1595 return -EEXIST;
1596 pfk->registered |= (1<<hdr->sadb_msg_satype);
1597 }
1598
1599 xfrm_probe_algs();
1600
1601 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1602 if (!supp_skb) {
1603 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1604 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1605
1606 return -ENOBUFS;
1607 }
1608
1609 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1610
1611 return 0;
1612}
1613
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001614static int key_notify_sa_flush(struct km_event *c)
1615{
1616 struct sk_buff *skb;
1617 struct sadb_msg *hdr;
1618
1619 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1620 if (!skb)
1621 return -ENOBUFS;
1622 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf088672005-06-18 22:44:00 -07001623 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001624 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001625 hdr->sadb_msg_seq = c->seq;
1626 hdr->sadb_msg_pid = c->pid;
1627 hdr->sadb_msg_version = PF_KEY_V2;
1628 hdr->sadb_msg_errno = (uint8_t) 0;
1629 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1630
1631 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1632
1633 return 0;
1634}
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1637{
1638 unsigned proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001639 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1642 if (proto == 0)
1643 return -EINVAL;
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 xfrm_state_flush(proto);
Herbert Xubf088672005-06-18 22:44:00 -07001646 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001647 c.seq = hdr->sadb_msg_seq;
1648 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001649 c.event = XFRM_MSG_FLUSHSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001650 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 return 0;
1653}
1654
1655struct pfkey_dump_data
1656{
1657 struct sk_buff *skb;
1658 struct sadb_msg *hdr;
1659 struct sock *sk;
1660};
1661
1662static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1663{
1664 struct pfkey_dump_data *data = ptr;
1665 struct sk_buff *out_skb;
1666 struct sadb_msg *out_hdr;
1667
1668 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1669 if (IS_ERR(out_skb))
1670 return PTR_ERR(out_skb);
1671
1672 out_hdr = (struct sadb_msg *) out_skb->data;
1673 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
1674 out_hdr->sadb_msg_type = SADB_DUMP;
1675 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1676 out_hdr->sadb_msg_errno = 0;
1677 out_hdr->sadb_msg_reserved = 0;
1678 out_hdr->sadb_msg_seq = count;
1679 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
1680 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
1681 return 0;
1682}
1683
1684static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1685{
1686 u8 proto;
1687 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
1688
1689 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1690 if (proto == 0)
1691 return -EINVAL;
1692
1693 return xfrm_state_walk(proto, dump_sa, &data);
1694}
1695
1696static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1697{
1698 struct pfkey_sock *pfk = pfkey_sk(sk);
1699 int satype = hdr->sadb_msg_satype;
1700
1701 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1702 /* XXX we mangle packet... */
1703 hdr->sadb_msg_errno = 0;
1704 if (satype != 0 && satype != 1)
1705 return -EINVAL;
1706 pfk->promisc = satype;
1707 }
1708 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1709 return 0;
1710}
1711
1712static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1713{
1714 int i;
1715 u32 reqid = *(u32*)ptr;
1716
1717 for (i=0; i<xp->xfrm_nr; i++) {
1718 if (xp->xfrm_vec[i].reqid == reqid)
1719 return -EEXIST;
1720 }
1721 return 0;
1722}
1723
1724static u32 gen_reqid(void)
1725{
1726 u32 start;
1727 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1728
1729 start = reqid;
1730 do {
1731 ++reqid;
1732 if (reqid == 0)
1733 reqid = IPSEC_MANUAL_REQID_MAX+1;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001734 if (xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, check_reqid,
1735 (void*)&reqid) != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return reqid;
1737 } while (reqid != start);
1738 return 0;
1739}
1740
1741static int
1742parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1743{
1744 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1745 struct sockaddr_in *sin;
1746#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1747 struct sockaddr_in6 *sin6;
1748#endif
1749
1750 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1751 return -ELOOP;
1752
1753 if (rq->sadb_x_ipsecrequest_mode == 0)
1754 return -EINVAL;
1755
1756 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1757 t->mode = rq->sadb_x_ipsecrequest_mode-1;
1758 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1759 t->optional = 1;
1760 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1761 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1762 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1763 t->reqid = 0;
1764 if (!t->reqid && !(t->reqid = gen_reqid()))
1765 return -ENOBUFS;
1766 }
1767
1768 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001769 if (t->mode == XFRM_MODE_TUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 switch (xp->family) {
1771 case AF_INET:
1772 sin = (void*)(rq+1);
1773 if (sin->sin_family != AF_INET)
1774 return -EINVAL;
1775 t->saddr.a4 = sin->sin_addr.s_addr;
1776 sin++;
1777 if (sin->sin_family != AF_INET)
1778 return -EINVAL;
1779 t->id.daddr.a4 = sin->sin_addr.s_addr;
1780 break;
1781#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1782 case AF_INET6:
1783 sin6 = (void *)(rq+1);
1784 if (sin6->sin6_family != AF_INET6)
1785 return -EINVAL;
1786 memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1787 sin6++;
1788 if (sin6->sin6_family != AF_INET6)
1789 return -EINVAL;
1790 memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1791 break;
1792#endif
1793 default:
1794 return -EINVAL;
1795 }
1796 }
1797 /* No way to set this via kame pfkey */
1798 t->aalgos = t->ealgos = t->calgos = ~0;
1799 xp->xfrm_nr++;
1800 return 0;
1801}
1802
1803static int
1804parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1805{
1806 int err;
1807 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1808 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1809
1810 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1811 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1812 return err;
1813 len -= rq->sadb_x_ipsecrequest_len;
1814 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1815 }
1816 return 0;
1817}
1818
Trent Jaegerdf718372005-12-13 23:12:27 -08001819static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1820{
1821 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1822
1823 if (xfrm_ctx) {
1824 int len = sizeof(struct sadb_x_sec_ctx);
1825 len += xfrm_ctx->ctx_len;
1826 return PFKEY_ALIGN8(len);
1827 }
1828 return 0;
1829}
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1832{
1833 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1834 int socklen = (xp->family == AF_INET ?
1835 sizeof(struct sockaddr_in) :
1836 sizeof(struct sockaddr_in6));
1837
1838 return sizeof(struct sadb_msg) +
1839 (sizeof(struct sadb_lifetime) * 3) +
1840 (sizeof(struct sadb_address) * 2) +
1841 (sockaddr_size * 2) +
1842 sizeof(struct sadb_x_policy) +
1843 (xp->xfrm_nr * (sizeof(struct sadb_x_ipsecrequest) +
Trent Jaegerdf718372005-12-13 23:12:27 -08001844 (socklen * 2))) +
1845 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
1848static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1849{
1850 struct sk_buff *skb;
1851 int size;
1852
1853 size = pfkey_xfrm_policy2msg_size(xp);
1854
1855 skb = alloc_skb(size + 16, GFP_ATOMIC);
1856 if (skb == NULL)
1857 return ERR_PTR(-ENOBUFS);
1858
1859 return skb;
1860}
1861
1862static void pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1863{
1864 struct sadb_msg *hdr;
1865 struct sadb_address *addr;
1866 struct sadb_lifetime *lifetime;
1867 struct sadb_x_policy *pol;
1868 struct sockaddr_in *sin;
Trent Jaegerdf718372005-12-13 23:12:27 -08001869 struct sadb_x_sec_ctx *sec_ctx;
1870 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1872 struct sockaddr_in6 *sin6;
1873#endif
1874 int i;
1875 int size;
1876 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1877 int socklen = (xp->family == AF_INET ?
1878 sizeof(struct sockaddr_in) :
1879 sizeof(struct sockaddr_in6));
1880
1881 size = pfkey_xfrm_policy2msg_size(xp);
1882
1883 /* call should fill header later */
1884 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1885 memset(hdr, 0, size); /* XXX do we need this ? */
1886
1887 /* src address */
1888 addr = (struct sadb_address*) skb_put(skb,
1889 sizeof(struct sadb_address)+sockaddr_size);
1890 addr->sadb_address_len =
1891 (sizeof(struct sadb_address)+sockaddr_size)/
1892 sizeof(uint64_t);
1893 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1894 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1895 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1896 addr->sadb_address_reserved = 0;
1897 /* src address */
1898 if (xp->family == AF_INET) {
1899 sin = (struct sockaddr_in *) (addr + 1);
1900 sin->sin_family = AF_INET;
1901 sin->sin_addr.s_addr = xp->selector.saddr.a4;
1902 sin->sin_port = xp->selector.sport;
1903 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1904 }
1905#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1906 else if (xp->family == AF_INET6) {
1907 sin6 = (struct sockaddr_in6 *) (addr + 1);
1908 sin6->sin6_family = AF_INET6;
1909 sin6->sin6_port = xp->selector.sport;
1910 sin6->sin6_flowinfo = 0;
1911 memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
1912 sizeof(struct in6_addr));
1913 sin6->sin6_scope_id = 0;
1914 }
1915#endif
1916 else
1917 BUG();
1918
1919 /* dst address */
1920 addr = (struct sadb_address*) skb_put(skb,
1921 sizeof(struct sadb_address)+sockaddr_size);
1922 addr->sadb_address_len =
1923 (sizeof(struct sadb_address)+sockaddr_size)/
1924 sizeof(uint64_t);
1925 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1926 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1927 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
1928 addr->sadb_address_reserved = 0;
1929 if (xp->family == AF_INET) {
1930 sin = (struct sockaddr_in *) (addr + 1);
1931 sin->sin_family = AF_INET;
1932 sin->sin_addr.s_addr = xp->selector.daddr.a4;
1933 sin->sin_port = xp->selector.dport;
1934 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1935 }
1936#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1937 else if (xp->family == AF_INET6) {
1938 sin6 = (struct sockaddr_in6 *) (addr + 1);
1939 sin6->sin6_family = AF_INET6;
1940 sin6->sin6_port = xp->selector.dport;
1941 sin6->sin6_flowinfo = 0;
1942 memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
1943 sizeof(struct in6_addr));
1944 sin6->sin6_scope_id = 0;
1945 }
1946#endif
1947 else
1948 BUG();
1949
1950 /* hard time */
1951 lifetime = (struct sadb_lifetime *) skb_put(skb,
1952 sizeof(struct sadb_lifetime));
1953 lifetime->sadb_lifetime_len =
1954 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1955 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1956 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
1957 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
1958 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
1959 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
1960 /* soft time */
1961 lifetime = (struct sadb_lifetime *) skb_put(skb,
1962 sizeof(struct sadb_lifetime));
1963 lifetime->sadb_lifetime_len =
1964 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1965 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
1966 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
1967 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
1968 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
1969 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
1970 /* current time */
1971 lifetime = (struct sadb_lifetime *) skb_put(skb,
1972 sizeof(struct sadb_lifetime));
1973 lifetime->sadb_lifetime_len =
1974 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1975 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
1976 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
1977 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
1978 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
1979 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
1980
1981 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
1982 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
1983 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1984 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
1985 if (xp->action == XFRM_POLICY_ALLOW) {
1986 if (xp->xfrm_nr)
1987 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1988 else
1989 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
1990 }
1991 pol->sadb_x_policy_dir = dir+1;
1992 pol->sadb_x_policy_id = xp->index;
1993 pol->sadb_x_policy_priority = xp->priority;
1994
1995 for (i=0; i<xp->xfrm_nr; i++) {
1996 struct sadb_x_ipsecrequest *rq;
1997 struct xfrm_tmpl *t = xp->xfrm_vec + i;
1998 int req_size;
1999
2000 req_size = sizeof(struct sadb_x_ipsecrequest);
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002001 if (t->mode == XFRM_MODE_TUNNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 req_size += 2*socklen;
2003 else
2004 size -= 2*socklen;
2005 rq = (void*)skb_put(skb, req_size);
2006 pol->sadb_x_policy_len += req_size/8;
2007 memset(rq, 0, sizeof(*rq));
2008 rq->sadb_x_ipsecrequest_len = req_size;
2009 rq->sadb_x_ipsecrequest_proto = t->id.proto;
2010 rq->sadb_x_ipsecrequest_mode = t->mode+1;
2011 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2012 if (t->reqid)
2013 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2014 if (t->optional)
2015 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2016 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002017 if (t->mode == XFRM_MODE_TUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 switch (xp->family) {
2019 case AF_INET:
2020 sin = (void*)(rq+1);
2021 sin->sin_family = AF_INET;
2022 sin->sin_addr.s_addr = t->saddr.a4;
2023 sin->sin_port = 0;
2024 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2025 sin++;
2026 sin->sin_family = AF_INET;
2027 sin->sin_addr.s_addr = t->id.daddr.a4;
2028 sin->sin_port = 0;
2029 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2030 break;
2031#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2032 case AF_INET6:
2033 sin6 = (void*)(rq+1);
2034 sin6->sin6_family = AF_INET6;
2035 sin6->sin6_port = 0;
2036 sin6->sin6_flowinfo = 0;
2037 memcpy(&sin6->sin6_addr, t->saddr.a6,
2038 sizeof(struct in6_addr));
2039 sin6->sin6_scope_id = 0;
2040
2041 sin6++;
2042 sin6->sin6_family = AF_INET6;
2043 sin6->sin6_port = 0;
2044 sin6->sin6_flowinfo = 0;
2045 memcpy(&sin6->sin6_addr, t->id.daddr.a6,
2046 sizeof(struct in6_addr));
2047 sin6->sin6_scope_id = 0;
2048 break;
2049#endif
2050 default:
2051 break;
2052 }
2053 }
2054 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002055
2056 /* security context */
2057 if ((xfrm_ctx = xp->security)) {
2058 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2059
2060 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2061 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2062 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2063 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2064 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2065 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2066 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2067 xfrm_ctx->ctx_len);
2068 }
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 hdr->sadb_msg_len = size / sizeof(uint64_t);
2071 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
2072}
2073
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002074static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2075{
2076 struct sk_buff *out_skb;
2077 struct sadb_msg *out_hdr;
2078 int err;
2079
2080 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2081 if (IS_ERR(out_skb)) {
2082 err = PTR_ERR(out_skb);
2083 goto out;
2084 }
2085 pfkey_xfrm_policy2msg(out_skb, xp, dir);
2086
2087 out_hdr = (struct sadb_msg *) out_skb->data;
2088 out_hdr->sadb_msg_version = PF_KEY_V2;
2089
Herbert Xuf60f6b82005-06-18 22:44:37 -07002090 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002091 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2092 else
2093 out_hdr->sadb_msg_type = event2poltype(c->event);
2094 out_hdr->sadb_msg_errno = 0;
2095 out_hdr->sadb_msg_seq = c->seq;
2096 out_hdr->sadb_msg_pid = c->pid;
2097 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
2098out:
2099 return 0;
2100
2101}
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2104{
Trent Jaegerdf718372005-12-13 23:12:27 -08002105 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 struct sadb_lifetime *lifetime;
2107 struct sadb_address *sa;
2108 struct sadb_x_policy *pol;
2109 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002110 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002111 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2114 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2115 !ext_hdrs[SADB_X_EXT_POLICY-1])
2116 return -EINVAL;
2117
2118 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2119 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2120 return -EINVAL;
2121 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2122 return -EINVAL;
2123
2124 xp = xfrm_policy_alloc(GFP_KERNEL);
2125 if (xp == NULL)
2126 return -ENOBUFS;
2127
2128 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2129 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2130 xp->priority = pol->sadb_x_policy_priority;
2131
2132 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2133 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2134 if (!xp->family) {
2135 err = -EINVAL;
2136 goto out;
2137 }
2138 xp->selector.family = xp->family;
2139 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2140 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2141 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2142 if (xp->selector.sport)
2143 xp->selector.sport_mask = ~0;
2144
2145 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2146 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2147 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2148
2149 /* Amusing, we set this twice. KAME apps appear to set same value
2150 * in both addresses.
2151 */
2152 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2153
2154 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2155 if (xp->selector.dport)
2156 xp->selector.dport_mask = ~0;
2157
Trent Jaegerdf718372005-12-13 23:12:27 -08002158 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2159 if (sec_ctx != NULL) {
2160 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2161
2162 if (!uctx) {
2163 err = -ENOBUFS;
2164 goto out;
2165 }
2166
2167 err = security_xfrm_policy_alloc(xp, uctx);
2168 kfree(uctx);
2169
2170 if (err)
2171 goto out;
2172 }
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 xp->lft.soft_byte_limit = XFRM_INF;
2175 xp->lft.hard_byte_limit = XFRM_INF;
2176 xp->lft.soft_packet_limit = XFRM_INF;
2177 xp->lft.hard_packet_limit = XFRM_INF;
2178 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2179 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2180 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2181 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2182 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2183 }
2184 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2185 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2186 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2187 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2188 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2189 }
2190 xp->xfrm_nr = 0;
2191 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2192 (err = parse_ipsecrequests(xp, pol)) < 0)
2193 goto out;
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2196 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002197
2198 if (err)
2199 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002201 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002202 c.event = XFRM_MSG_UPDPOLICY;
2203 else
2204 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002206 c.seq = hdr->sadb_msg_seq;
2207 c.pid = hdr->sadb_msg_pid;
2208
2209 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return 0;
2212
2213out:
Trent Jaegerdf718372005-12-13 23:12:27 -08002214 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 kfree(xp);
2216 return err;
2217}
2218
2219static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2220{
2221 int err;
2222 struct sadb_address *sa;
2223 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08002224 struct xfrm_policy *xp, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002226 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002227 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2230 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2231 !ext_hdrs[SADB_X_EXT_POLICY-1])
2232 return -EINVAL;
2233
2234 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2235 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2236 return -EINVAL;
2237
2238 memset(&sel, 0, sizeof(sel));
2239
2240 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2241 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2242 sel.prefixlen_s = sa->sadb_address_prefixlen;
2243 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2244 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2245 if (sel.sport)
2246 sel.sport_mask = ~0;
2247
2248 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2249 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2250 sel.prefixlen_d = sa->sadb_address_prefixlen;
2251 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2252 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2253 if (sel.dport)
2254 sel.dport_mask = ~0;
2255
Trent Jaegerdf718372005-12-13 23:12:27 -08002256 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2257 memset(&tmp, 0, sizeof(struct xfrm_policy));
2258
2259 if (sec_ctx != NULL) {
2260 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2261
2262 if (!uctx)
2263 return -ENOMEM;
2264
2265 err = security_xfrm_policy_alloc(&tmp, uctx);
2266 kfree(uctx);
2267
2268 if (err)
2269 return err;
2270 }
2271
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002272 xp = xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN, pol->sadb_x_policy_dir-1,
2273 &sel, tmp.security, 1);
Trent Jaegerdf718372005-12-13 23:12:27 -08002274 security_xfrm_policy_free(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (xp == NULL)
2276 return -ENOENT;
2277
2278 err = 0;
2279
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002280 if ((err = security_xfrm_policy_delete(xp)))
2281 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002282 c.seq = hdr->sadb_msg_seq;
2283 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002284 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002285 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2286
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002287out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002288 xfrm_pol_put(xp);
2289 return err;
2290}
2291
2292static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2293{
2294 int err;
2295 struct sk_buff *out_skb;
2296 struct sadb_msg *out_hdr;
2297 err = 0;
2298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2300 if (IS_ERR(out_skb)) {
2301 err = PTR_ERR(out_skb);
2302 goto out;
2303 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002304 pfkey_xfrm_policy2msg(out_skb, xp, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 out_hdr = (struct sadb_msg *) out_skb->data;
2307 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002308 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 out_hdr->sadb_msg_satype = 0;
2310 out_hdr->sadb_msg_errno = 0;
2311 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2312 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002313 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 err = 0;
2315
2316out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return err;
2318}
2319
2320static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2321{
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002322 unsigned int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 int err;
2324 struct sadb_x_policy *pol;
2325 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002326 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2329 return -EINVAL;
2330
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002331 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2332 if (dir >= XFRM_POLICY_MAX)
2333 return -EINVAL;
2334
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002335 xp = xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN, dir, pol->sadb_x_policy_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2337 if (xp == NULL)
2338 return -ENOENT;
2339
2340 err = 0;
2341
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002342 c.seq = hdr->sadb_msg_seq;
2343 c.pid = hdr->sadb_msg_pid;
2344 if (hdr->sadb_msg_type == SADB_X_SPDDELETE2) {
Herbert Xubf088672005-06-18 22:44:00 -07002345 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002346 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002347 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002348 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002349 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 xfrm_pol_put(xp);
2353 return err;
2354}
2355
2356static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2357{
2358 struct pfkey_dump_data *data = ptr;
2359 struct sk_buff *out_skb;
2360 struct sadb_msg *out_hdr;
2361
2362 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2363 if (IS_ERR(out_skb))
2364 return PTR_ERR(out_skb);
2365
2366 pfkey_xfrm_policy2msg(out_skb, xp, dir);
2367
2368 out_hdr = (struct sadb_msg *) out_skb->data;
2369 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
2370 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2371 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2372 out_hdr->sadb_msg_errno = 0;
2373 out_hdr->sadb_msg_seq = count;
2374 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
2375 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
2376 return 0;
2377}
2378
2379static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2380{
2381 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
2382
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002383 return xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_sp, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002386static int key_notify_policy_flush(struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
2388 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002389 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002391 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (!skb_out)
2393 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002394 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002395 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002396 hdr->sadb_msg_seq = c->seq;
2397 hdr->sadb_msg_pid = c->pid;
2398 hdr->sadb_msg_version = PF_KEY_V2;
2399 hdr->sadb_msg_errno = (uint8_t) 0;
2400 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2401 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2402 return 0;
2403
2404}
2405
2406static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2407{
2408 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002410 xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN);
2411 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002412 c.event = XFRM_MSG_FLUSHPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002413 c.pid = hdr->sadb_msg_pid;
2414 c.seq = hdr->sadb_msg_seq;
2415 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 return 0;
2418}
2419
2420typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2421 struct sadb_msg *hdr, void **ext_hdrs);
2422static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2423 [SADB_RESERVED] = pfkey_reserved,
2424 [SADB_GETSPI] = pfkey_getspi,
2425 [SADB_UPDATE] = pfkey_add,
2426 [SADB_ADD] = pfkey_add,
2427 [SADB_DELETE] = pfkey_delete,
2428 [SADB_GET] = pfkey_get,
2429 [SADB_ACQUIRE] = pfkey_acquire,
2430 [SADB_REGISTER] = pfkey_register,
2431 [SADB_EXPIRE] = NULL,
2432 [SADB_FLUSH] = pfkey_flush,
2433 [SADB_DUMP] = pfkey_dump,
2434 [SADB_X_PROMISC] = pfkey_promisc,
2435 [SADB_X_PCHANGE] = NULL,
2436 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2437 [SADB_X_SPDADD] = pfkey_spdadd,
2438 [SADB_X_SPDDELETE] = pfkey_spddelete,
2439 [SADB_X_SPDGET] = pfkey_spdget,
2440 [SADB_X_SPDACQUIRE] = NULL,
2441 [SADB_X_SPDDUMP] = pfkey_spddump,
2442 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2443 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2444 [SADB_X_SPDDELETE2] = pfkey_spdget,
2445};
2446
2447static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2448{
2449 void *ext_hdrs[SADB_EXT_MAX];
2450 int err;
2451
2452 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2453 BROADCAST_PROMISC_ONLY, NULL);
2454
2455 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2456 err = parse_exthdrs(skb, hdr, ext_hdrs);
2457 if (!err) {
2458 err = -EOPNOTSUPP;
2459 if (pfkey_funcs[hdr->sadb_msg_type])
2460 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2461 }
2462 return err;
2463}
2464
2465static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2466{
2467 struct sadb_msg *hdr = NULL;
2468
2469 if (skb->len < sizeof(*hdr)) {
2470 *errp = -EMSGSIZE;
2471 } else {
2472 hdr = (struct sadb_msg *) skb->data;
2473 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2474 hdr->sadb_msg_reserved != 0 ||
2475 (hdr->sadb_msg_type <= SADB_RESERVED ||
2476 hdr->sadb_msg_type > SADB_MAX)) {
2477 hdr = NULL;
2478 *errp = -EINVAL;
2479 } else if (hdr->sadb_msg_len != (skb->len /
2480 sizeof(uint64_t)) ||
2481 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2482 sizeof(uint64_t))) {
2483 hdr = NULL;
2484 *errp = -EMSGSIZE;
2485 } else {
2486 *errp = 0;
2487 }
2488 }
2489 return hdr;
2490}
2491
2492static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2493{
2494 return t->aalgos & (1 << d->desc.sadb_alg_id);
2495}
2496
2497static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2498{
2499 return t->ealgos & (1 << d->desc.sadb_alg_id);
2500}
2501
2502static int count_ah_combs(struct xfrm_tmpl *t)
2503{
2504 int i, sz = 0;
2505
2506 for (i = 0; ; i++) {
2507 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2508 if (!aalg)
2509 break;
2510 if (aalg_tmpl_set(t, aalg) && aalg->available)
2511 sz += sizeof(struct sadb_comb);
2512 }
2513 return sz + sizeof(struct sadb_prop);
2514}
2515
2516static int count_esp_combs(struct xfrm_tmpl *t)
2517{
2518 int i, k, sz = 0;
2519
2520 for (i = 0; ; i++) {
2521 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2522 if (!ealg)
2523 break;
2524
2525 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2526 continue;
2527
2528 for (k = 1; ; k++) {
2529 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2530 if (!aalg)
2531 break;
2532
2533 if (aalg_tmpl_set(t, aalg) && aalg->available)
2534 sz += sizeof(struct sadb_comb);
2535 }
2536 }
2537 return sz + sizeof(struct sadb_prop);
2538}
2539
2540static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2541{
2542 struct sadb_prop *p;
2543 int i;
2544
2545 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2546 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2547 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2548 p->sadb_prop_replay = 32;
2549 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2550
2551 for (i = 0; ; i++) {
2552 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2553 if (!aalg)
2554 break;
2555
2556 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2557 struct sadb_comb *c;
2558 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2559 memset(c, 0, sizeof(*c));
2560 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2561 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2562 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2563 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2564 c->sadb_comb_hard_addtime = 24*60*60;
2565 c->sadb_comb_soft_addtime = 20*60*60;
2566 c->sadb_comb_hard_usetime = 8*60*60;
2567 c->sadb_comb_soft_usetime = 7*60*60;
2568 }
2569 }
2570}
2571
2572static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2573{
2574 struct sadb_prop *p;
2575 int i, k;
2576
2577 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2578 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2579 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2580 p->sadb_prop_replay = 32;
2581 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2582
2583 for (i=0; ; i++) {
2584 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2585 if (!ealg)
2586 break;
2587
2588 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2589 continue;
2590
2591 for (k = 1; ; k++) {
2592 struct sadb_comb *c;
2593 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2594 if (!aalg)
2595 break;
2596 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2597 continue;
2598 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2599 memset(c, 0, sizeof(*c));
2600 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2601 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2602 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2603 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2604 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2605 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2606 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2607 c->sadb_comb_hard_addtime = 24*60*60;
2608 c->sadb_comb_soft_addtime = 20*60*60;
2609 c->sadb_comb_hard_usetime = 8*60*60;
2610 c->sadb_comb_soft_usetime = 7*60*60;
2611 }
2612 }
2613}
2614
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002615static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2616{
2617 return 0;
2618}
2619
2620static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
2622 struct sk_buff *out_skb;
2623 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002624 int hard;
2625 int hsc;
2626
Herbert Xubf088672005-06-18 22:44:00 -07002627 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002628 if (hard)
2629 hsc = 2;
2630 else
2631 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633 out_skb = pfkey_xfrm_state2msg(x, 0, hsc);
2634 if (IS_ERR(out_skb))
2635 return PTR_ERR(out_skb);
2636
2637 out_hdr = (struct sadb_msg *) out_skb->data;
2638 out_hdr->sadb_msg_version = PF_KEY_V2;
2639 out_hdr->sadb_msg_type = SADB_EXPIRE;
2640 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2641 out_hdr->sadb_msg_errno = 0;
2642 out_hdr->sadb_msg_reserved = 0;
2643 out_hdr->sadb_msg_seq = 0;
2644 out_hdr->sadb_msg_pid = 0;
2645
2646 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2647 return 0;
2648}
2649
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002650static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2651{
2652 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002653 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002654 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002655 case XFRM_MSG_DELSA:
2656 case XFRM_MSG_NEWSA:
2657 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002658 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002659 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002660 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002661 case XFRM_MSG_NEWAE: /* not yet supported */
2662 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002663 default:
2664 printk("pfkey: Unknown SA event %d\n", c->event);
2665 break;
2666 }
2667
2668 return 0;
2669}
2670
2671static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2672{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002673 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
2674 return 0;
2675
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002676 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002677 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002678 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002679 case XFRM_MSG_DELPOLICY:
2680 case XFRM_MSG_NEWPOLICY:
2681 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002682 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002683 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002684 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
2685 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002686 return key_notify_policy_flush(c);
2687 default:
2688 printk("pfkey: Unknown policy event %d\n", c->event);
2689 break;
2690 }
2691
2692 return 0;
2693}
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695static u32 get_acqseq(void)
2696{
2697 u32 res;
2698 static u32 acqseq;
2699 static DEFINE_SPINLOCK(acqseq_lock);
2700
2701 spin_lock_bh(&acqseq_lock);
2702 res = (++acqseq ? : ++acqseq);
2703 spin_unlock_bh(&acqseq_lock);
2704 return res;
2705}
2706
2707static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2708{
2709 struct sk_buff *skb;
2710 struct sadb_msg *hdr;
2711 struct sadb_address *addr;
2712 struct sadb_x_policy *pol;
2713 struct sockaddr_in *sin;
2714#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2715 struct sockaddr_in6 *sin6;
2716#endif
2717 int sockaddr_size;
2718 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07002719 struct sadb_x_sec_ctx *sec_ctx;
2720 struct xfrm_sec_ctx *xfrm_ctx;
2721 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2724 if (!sockaddr_size)
2725 return -EINVAL;
2726
2727 size = sizeof(struct sadb_msg) +
2728 (sizeof(struct sadb_address) * 2) +
2729 (sockaddr_size * 2) +
2730 sizeof(struct sadb_x_policy);
2731
2732 if (x->id.proto == IPPROTO_AH)
2733 size += count_ah_combs(t);
2734 else if (x->id.proto == IPPROTO_ESP)
2735 size += count_esp_combs(t);
2736
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07002737 if ((xfrm_ctx = x->security)) {
2738 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
2739 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
2740 }
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 skb = alloc_skb(size + 16, GFP_ATOMIC);
2743 if (skb == NULL)
2744 return -ENOMEM;
2745
2746 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2747 hdr->sadb_msg_version = PF_KEY_V2;
2748 hdr->sadb_msg_type = SADB_ACQUIRE;
2749 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2750 hdr->sadb_msg_len = size / sizeof(uint64_t);
2751 hdr->sadb_msg_errno = 0;
2752 hdr->sadb_msg_reserved = 0;
2753 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2754 hdr->sadb_msg_pid = 0;
2755
2756 /* src address */
2757 addr = (struct sadb_address*) skb_put(skb,
2758 sizeof(struct sadb_address)+sockaddr_size);
2759 addr->sadb_address_len =
2760 (sizeof(struct sadb_address)+sockaddr_size)/
2761 sizeof(uint64_t);
2762 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2763 addr->sadb_address_proto = 0;
2764 addr->sadb_address_reserved = 0;
2765 if (x->props.family == AF_INET) {
2766 addr->sadb_address_prefixlen = 32;
2767
2768 sin = (struct sockaddr_in *) (addr + 1);
2769 sin->sin_family = AF_INET;
2770 sin->sin_addr.s_addr = x->props.saddr.a4;
2771 sin->sin_port = 0;
2772 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2773 }
2774#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2775 else if (x->props.family == AF_INET6) {
2776 addr->sadb_address_prefixlen = 128;
2777
2778 sin6 = (struct sockaddr_in6 *) (addr + 1);
2779 sin6->sin6_family = AF_INET6;
2780 sin6->sin6_port = 0;
2781 sin6->sin6_flowinfo = 0;
2782 memcpy(&sin6->sin6_addr,
2783 x->props.saddr.a6, sizeof(struct in6_addr));
2784 sin6->sin6_scope_id = 0;
2785 }
2786#endif
2787 else
2788 BUG();
2789
2790 /* dst address */
2791 addr = (struct sadb_address*) skb_put(skb,
2792 sizeof(struct sadb_address)+sockaddr_size);
2793 addr->sadb_address_len =
2794 (sizeof(struct sadb_address)+sockaddr_size)/
2795 sizeof(uint64_t);
2796 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2797 addr->sadb_address_proto = 0;
2798 addr->sadb_address_reserved = 0;
2799 if (x->props.family == AF_INET) {
2800 addr->sadb_address_prefixlen = 32;
2801
2802 sin = (struct sockaddr_in *) (addr + 1);
2803 sin->sin_family = AF_INET;
2804 sin->sin_addr.s_addr = x->id.daddr.a4;
2805 sin->sin_port = 0;
2806 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2807 }
2808#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2809 else if (x->props.family == AF_INET6) {
2810 addr->sadb_address_prefixlen = 128;
2811
2812 sin6 = (struct sockaddr_in6 *) (addr + 1);
2813 sin6->sin6_family = AF_INET6;
2814 sin6->sin6_port = 0;
2815 sin6->sin6_flowinfo = 0;
2816 memcpy(&sin6->sin6_addr,
2817 x->id.daddr.a6, sizeof(struct in6_addr));
2818 sin6->sin6_scope_id = 0;
2819 }
2820#endif
2821 else
2822 BUG();
2823
2824 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2825 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2826 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2827 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2828 pol->sadb_x_policy_dir = dir+1;
2829 pol->sadb_x_policy_id = xp->index;
2830
2831 /* Set sadb_comb's. */
2832 if (x->id.proto == IPPROTO_AH)
2833 dump_ah_combs(skb, t);
2834 else if (x->id.proto == IPPROTO_ESP)
2835 dump_esp_combs(skb, t);
2836
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07002837 /* security context */
2838 if (xfrm_ctx) {
2839 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
2840 sizeof(struct sadb_x_sec_ctx) + ctx_size);
2841 sec_ctx->sadb_x_sec_len =
2842 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
2843 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2844 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2845 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2846 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2847 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2848 xfrm_ctx->ctx_len);
2849 }
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2852}
2853
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002854static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 u8 *data, int len, int *dir)
2856{
2857 struct xfrm_policy *xp;
2858 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08002859 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002861 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 case AF_INET:
2863 if (opt != IP_IPSEC_POLICY) {
2864 *dir = -EOPNOTSUPP;
2865 return NULL;
2866 }
2867 break;
2868#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2869 case AF_INET6:
2870 if (opt != IPV6_IPSEC_POLICY) {
2871 *dir = -EOPNOTSUPP;
2872 return NULL;
2873 }
2874 break;
2875#endif
2876 default:
2877 *dir = -EINVAL;
2878 return NULL;
2879 }
2880
2881 *dir = -EINVAL;
2882
2883 if (len < sizeof(struct sadb_x_policy) ||
2884 pol->sadb_x_policy_len*8 > len ||
2885 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
2886 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
2887 return NULL;
2888
2889 xp = xfrm_policy_alloc(GFP_ATOMIC);
2890 if (xp == NULL) {
2891 *dir = -ENOBUFS;
2892 return NULL;
2893 }
2894
2895 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2896 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2897
2898 xp->lft.soft_byte_limit = XFRM_INF;
2899 xp->lft.hard_byte_limit = XFRM_INF;
2900 xp->lft.soft_packet_limit = XFRM_INF;
2901 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002902 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 xp->xfrm_nr = 0;
2905 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2906 (*dir = parse_ipsecrequests(xp, pol)) < 0)
2907 goto out;
2908
Trent Jaegerdf718372005-12-13 23:12:27 -08002909 /* security context too */
2910 if (len >= (pol->sadb_x_policy_len*8 +
2911 sizeof(struct sadb_x_sec_ctx))) {
2912 char *p = (char *)pol;
2913 struct xfrm_user_sec_ctx *uctx;
2914
2915 p += pol->sadb_x_policy_len*8;
2916 sec_ctx = (struct sadb_x_sec_ctx *)p;
2917 if (len < pol->sadb_x_policy_len*8 +
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002918 sec_ctx->sadb_x_sec_len) {
2919 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08002920 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002921 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002922 if ((*dir = verify_sec_ctx_len(p)))
2923 goto out;
2924 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2925 *dir = security_xfrm_policy_alloc(xp, uctx);
2926 kfree(uctx);
2927
2928 if (*dir)
2929 goto out;
2930 }
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002931 else {
2932 *dir = security_xfrm_sock_policy_alloc(xp, sk);
2933 if (*dir)
2934 goto out;
2935 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 *dir = pol->sadb_x_policy_dir-1;
2938 return xp;
2939
2940out:
Trent Jaegerdf718372005-12-13 23:12:27 -08002941 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 kfree(xp);
2943 return NULL;
2944}
2945
2946static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport)
2947{
2948 struct sk_buff *skb;
2949 struct sadb_msg *hdr;
2950 struct sadb_sa *sa;
2951 struct sadb_address *addr;
2952 struct sadb_x_nat_t_port *n_port;
2953 struct sockaddr_in *sin;
2954#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2955 struct sockaddr_in6 *sin6;
2956#endif
2957 int sockaddr_size;
2958 int size;
2959 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
2960 struct xfrm_encap_tmpl *natt = NULL;
2961
2962 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2963 if (!sockaddr_size)
2964 return -EINVAL;
2965
2966 if (!satype)
2967 return -EINVAL;
2968
2969 if (!x->encap)
2970 return -EINVAL;
2971
2972 natt = x->encap;
2973
2974 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
2975 *
2976 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
2977 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
2978 */
2979
2980 size = sizeof(struct sadb_msg) +
2981 sizeof(struct sadb_sa) +
2982 (sizeof(struct sadb_address) * 2) +
2983 (sockaddr_size * 2) +
2984 (sizeof(struct sadb_x_nat_t_port) * 2);
2985
2986 skb = alloc_skb(size + 16, GFP_ATOMIC);
2987 if (skb == NULL)
2988 return -ENOMEM;
2989
2990 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2991 hdr->sadb_msg_version = PF_KEY_V2;
2992 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
2993 hdr->sadb_msg_satype = satype;
2994 hdr->sadb_msg_len = size / sizeof(uint64_t);
2995 hdr->sadb_msg_errno = 0;
2996 hdr->sadb_msg_reserved = 0;
2997 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2998 hdr->sadb_msg_pid = 0;
2999
3000 /* SA */
3001 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3002 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3003 sa->sadb_sa_exttype = SADB_EXT_SA;
3004 sa->sadb_sa_spi = x->id.spi;
3005 sa->sadb_sa_replay = 0;
3006 sa->sadb_sa_state = 0;
3007 sa->sadb_sa_auth = 0;
3008 sa->sadb_sa_encrypt = 0;
3009 sa->sadb_sa_flags = 0;
3010
3011 /* ADDRESS_SRC (old addr) */
3012 addr = (struct sadb_address*)
3013 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3014 addr->sadb_address_len =
3015 (sizeof(struct sadb_address)+sockaddr_size)/
3016 sizeof(uint64_t);
3017 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3018 addr->sadb_address_proto = 0;
3019 addr->sadb_address_reserved = 0;
3020 if (x->props.family == AF_INET) {
3021 addr->sadb_address_prefixlen = 32;
3022
3023 sin = (struct sockaddr_in *) (addr + 1);
3024 sin->sin_family = AF_INET;
3025 sin->sin_addr.s_addr = x->props.saddr.a4;
3026 sin->sin_port = 0;
3027 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3028 }
3029#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3030 else if (x->props.family == AF_INET6) {
3031 addr->sadb_address_prefixlen = 128;
3032
3033 sin6 = (struct sockaddr_in6 *) (addr + 1);
3034 sin6->sin6_family = AF_INET6;
3035 sin6->sin6_port = 0;
3036 sin6->sin6_flowinfo = 0;
3037 memcpy(&sin6->sin6_addr,
3038 x->props.saddr.a6, sizeof(struct in6_addr));
3039 sin6->sin6_scope_id = 0;
3040 }
3041#endif
3042 else
3043 BUG();
3044
3045 /* NAT_T_SPORT (old port) */
3046 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3047 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3048 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3049 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3050 n_port->sadb_x_nat_t_port_reserved = 0;
3051
3052 /* ADDRESS_DST (new addr) */
3053 addr = (struct sadb_address*)
3054 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3055 addr->sadb_address_len =
3056 (sizeof(struct sadb_address)+sockaddr_size)/
3057 sizeof(uint64_t);
3058 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3059 addr->sadb_address_proto = 0;
3060 addr->sadb_address_reserved = 0;
3061 if (x->props.family == AF_INET) {
3062 addr->sadb_address_prefixlen = 32;
3063
3064 sin = (struct sockaddr_in *) (addr + 1);
3065 sin->sin_family = AF_INET;
3066 sin->sin_addr.s_addr = ipaddr->a4;
3067 sin->sin_port = 0;
3068 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3069 }
3070#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3071 else if (x->props.family == AF_INET6) {
3072 addr->sadb_address_prefixlen = 128;
3073
3074 sin6 = (struct sockaddr_in6 *) (addr + 1);
3075 sin6->sin6_family = AF_INET6;
3076 sin6->sin6_port = 0;
3077 sin6->sin6_flowinfo = 0;
3078 memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
3079 sin6->sin6_scope_id = 0;
3080 }
3081#endif
3082 else
3083 BUG();
3084
3085 /* NAT_T_DPORT (new port) */
3086 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3087 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3088 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3089 n_port->sadb_x_nat_t_port_port = sport;
3090 n_port->sadb_x_nat_t_port_reserved = 0;
3091
3092 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3093}
3094
3095static int pfkey_sendmsg(struct kiocb *kiocb,
3096 struct socket *sock, struct msghdr *msg, size_t len)
3097{
3098 struct sock *sk = sock->sk;
3099 struct sk_buff *skb = NULL;
3100 struct sadb_msg *hdr = NULL;
3101 int err;
3102
3103 err = -EOPNOTSUPP;
3104 if (msg->msg_flags & MSG_OOB)
3105 goto out;
3106
3107 err = -EMSGSIZE;
3108 if ((unsigned)len > sk->sk_sndbuf - 32)
3109 goto out;
3110
3111 err = -ENOBUFS;
3112 skb = alloc_skb(len, GFP_KERNEL);
3113 if (skb == NULL)
3114 goto out;
3115
3116 err = -EFAULT;
3117 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3118 goto out;
3119
3120 hdr = pfkey_get_base_msg(skb, &err);
3121 if (!hdr)
3122 goto out;
3123
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003124 mutex_lock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 err = pfkey_process(sk, skb, hdr);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003126 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
3128out:
3129 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3130 err = 0;
3131 if (skb)
3132 kfree_skb(skb);
3133
3134 return err ? : len;
3135}
3136
3137static int pfkey_recvmsg(struct kiocb *kiocb,
3138 struct socket *sock, struct msghdr *msg, size_t len,
3139 int flags)
3140{
3141 struct sock *sk = sock->sk;
3142 struct sk_buff *skb;
3143 int copied, err;
3144
3145 err = -EINVAL;
3146 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3147 goto out;
3148
3149 msg->msg_namelen = 0;
3150 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3151 if (skb == NULL)
3152 goto out;
3153
3154 copied = skb->len;
3155 if (copied > len) {
3156 msg->msg_flags |= MSG_TRUNC;
3157 copied = len;
3158 }
3159
3160 skb->h.raw = skb->data;
3161 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3162 if (err)
3163 goto out_free;
3164
3165 sock_recv_timestamp(msg, sk, skb);
3166
3167 err = (flags & MSG_TRUNC) ? skb->len : copied;
3168
3169out_free:
3170 skb_free_datagram(sk, skb);
3171out:
3172 return err;
3173}
3174
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003175static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 .family = PF_KEY,
3177 .owner = THIS_MODULE,
3178 /* Operations that make no sense on pfkey sockets. */
3179 .bind = sock_no_bind,
3180 .connect = sock_no_connect,
3181 .socketpair = sock_no_socketpair,
3182 .accept = sock_no_accept,
3183 .getname = sock_no_getname,
3184 .ioctl = sock_no_ioctl,
3185 .listen = sock_no_listen,
3186 .shutdown = sock_no_shutdown,
3187 .setsockopt = sock_no_setsockopt,
3188 .getsockopt = sock_no_getsockopt,
3189 .mmap = sock_no_mmap,
3190 .sendpage = sock_no_sendpage,
3191
3192 /* Now the operations that really occur. */
3193 .release = pfkey_release,
3194 .poll = datagram_poll,
3195 .sendmsg = pfkey_sendmsg,
3196 .recvmsg = pfkey_recvmsg,
3197};
3198
3199static struct net_proto_family pfkey_family_ops = {
3200 .family = PF_KEY,
3201 .create = pfkey_create,
3202 .owner = THIS_MODULE,
3203};
3204
3205#ifdef CONFIG_PROC_FS
3206static int pfkey_read_proc(char *buffer, char **start, off_t offset,
3207 int length, int *eof, void *data)
3208{
3209 off_t pos = 0;
3210 off_t begin = 0;
3211 int len = 0;
3212 struct sock *s;
3213 struct hlist_node *node;
3214
3215 len += sprintf(buffer,"sk RefCnt Rmem Wmem User Inode\n");
3216
3217 read_lock(&pfkey_table_lock);
3218
3219 sk_for_each(s, node, &pfkey_table) {
3220 len += sprintf(buffer+len,"%p %-6d %-6u %-6u %-6u %-6lu",
3221 s,
3222 atomic_read(&s->sk_refcnt),
3223 atomic_read(&s->sk_rmem_alloc),
3224 atomic_read(&s->sk_wmem_alloc),
3225 sock_i_uid(s),
3226 sock_i_ino(s)
3227 );
3228
3229 buffer[len++] = '\n';
3230
3231 pos = begin + len;
3232 if (pos < offset) {
3233 len = 0;
3234 begin = pos;
3235 }
3236 if(pos > offset + length)
3237 goto done;
3238 }
3239 *eof = 1;
3240
3241done:
3242 read_unlock(&pfkey_table_lock);
3243
3244 *start = buffer + (offset - begin);
3245 len -= (offset - begin);
3246
3247 if (len > length)
3248 len = length;
3249 if (len < 0)
3250 len = 0;
3251
3252 return len;
3253}
3254#endif
3255
3256static struct xfrm_mgr pfkeyv2_mgr =
3257{
3258 .id = "pfkeyv2",
3259 .notify = pfkey_send_notify,
3260 .acquire = pfkey_send_acquire,
3261 .compile_policy = pfkey_compile_policy,
3262 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003263 .notify_policy = pfkey_send_policy_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264};
3265
3266static void __exit ipsec_pfkey_exit(void)
3267{
3268 xfrm_unregister_km(&pfkeyv2_mgr);
3269 remove_proc_entry("net/pfkey", NULL);
3270 sock_unregister(PF_KEY);
3271 proto_unregister(&key_proto);
3272}
3273
3274static int __init ipsec_pfkey_init(void)
3275{
3276 int err = proto_register(&key_proto, 0);
3277
3278 if (err != 0)
3279 goto out;
3280
3281 err = sock_register(&pfkey_family_ops);
3282 if (err != 0)
3283 goto out_unregister_key_proto;
3284#ifdef CONFIG_PROC_FS
3285 err = -ENOMEM;
3286 if (create_proc_read_entry("net/pfkey", 0, NULL, pfkey_read_proc, NULL) == NULL)
3287 goto out_sock_unregister;
3288#endif
3289 err = xfrm_register_km(&pfkeyv2_mgr);
3290 if (err != 0)
3291 goto out_remove_proc_entry;
3292out:
3293 return err;
3294out_remove_proc_entry:
3295#ifdef CONFIG_PROC_FS
3296 remove_proc_entry("net/pfkey", NULL);
3297out_sock_unregister:
3298#endif
3299 sock_unregister(PF_KEY);
3300out_unregister_key_proto:
3301 proto_unregister(&key_proto);
3302 goto out;
3303}
3304
3305module_init(ipsec_pfkey_init);
3306module_exit(ipsec_pfkey_exit);
3307MODULE_LICENSE("GPL");
3308MODULE_ALIAS_NETPROTO(PF_KEY);