blob: 0e1dbfbb9b109ae6e2be92bbac0a592ec4a2ecb0 [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) {
Miika Komu2718aa72006-11-30 16:41:50 -08001770 struct sockaddr *sa;
1771 sa = (struct sockaddr *)(rq+1);
1772 switch(sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 case AF_INET:
Miika Komu2718aa72006-11-30 16:41:50 -08001774 sin = (struct sockaddr_in*)sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 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:
Miika Komu2718aa72006-11-30 16:41:50 -08001783 sin6 = (struct sockaddr_in6*)sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1785 sin6++;
1786 if (sin6->sin6_family != AF_INET6)
1787 return -EINVAL;
1788 memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1789 break;
1790#endif
1791 default:
1792 return -EINVAL;
1793 }
Miika Komu2718aa72006-11-30 16:41:50 -08001794 t->encap_family = sa->sa_family;
1795 } else
1796 t->encap_family = xp->family;
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /* No way to set this via kame pfkey */
1799 t->aalgos = t->ealgos = t->calgos = ~0;
1800 xp->xfrm_nr++;
1801 return 0;
1802}
1803
1804static int
1805parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1806{
1807 int err;
1808 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1809 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1810
1811 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1812 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1813 return err;
1814 len -= rq->sadb_x_ipsecrequest_len;
1815 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1816 }
1817 return 0;
1818}
1819
Trent Jaegerdf718372005-12-13 23:12:27 -08001820static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1821{
1822 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1823
1824 if (xfrm_ctx) {
1825 int len = sizeof(struct sadb_x_sec_ctx);
1826 len += xfrm_ctx->ctx_len;
1827 return PFKEY_ALIGN8(len);
1828 }
1829 return 0;
1830}
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1833{
Miika Komu2718aa72006-11-30 16:41:50 -08001834 struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08001836 int socklen = 0;
1837 int i;
1838
1839 for (i=0; i<xp->xfrm_nr; i++) {
1840 t = xp->xfrm_vec + i;
1841 socklen += (t->encap_family == AF_INET ?
1842 sizeof(struct sockaddr_in) :
1843 sizeof(struct sockaddr_in6));
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 return sizeof(struct sadb_msg) +
1847 (sizeof(struct sadb_lifetime) * 3) +
1848 (sizeof(struct sadb_address) * 2) +
1849 (sockaddr_size * 2) +
1850 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08001851 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1852 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08001853 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
1856static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1857{
1858 struct sk_buff *skb;
1859 int size;
1860
1861 size = pfkey_xfrm_policy2msg_size(xp);
1862
1863 skb = alloc_skb(size + 16, GFP_ATOMIC);
1864 if (skb == NULL)
1865 return ERR_PTR(-ENOBUFS);
1866
1867 return skb;
1868}
1869
1870static void pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1871{
1872 struct sadb_msg *hdr;
1873 struct sadb_address *addr;
1874 struct sadb_lifetime *lifetime;
1875 struct sadb_x_policy *pol;
1876 struct sockaddr_in *sin;
Trent Jaegerdf718372005-12-13 23:12:27 -08001877 struct sadb_x_sec_ctx *sec_ctx;
1878 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1880 struct sockaddr_in6 *sin6;
1881#endif
1882 int i;
1883 int size;
1884 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1885 int socklen = (xp->family == AF_INET ?
1886 sizeof(struct sockaddr_in) :
1887 sizeof(struct sockaddr_in6));
1888
1889 size = pfkey_xfrm_policy2msg_size(xp);
1890
1891 /* call should fill header later */
1892 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1893 memset(hdr, 0, size); /* XXX do we need this ? */
1894
1895 /* src address */
1896 addr = (struct sadb_address*) skb_put(skb,
1897 sizeof(struct sadb_address)+sockaddr_size);
1898 addr->sadb_address_len =
1899 (sizeof(struct sadb_address)+sockaddr_size)/
1900 sizeof(uint64_t);
1901 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1902 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1903 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1904 addr->sadb_address_reserved = 0;
1905 /* src address */
1906 if (xp->family == AF_INET) {
1907 sin = (struct sockaddr_in *) (addr + 1);
1908 sin->sin_family = AF_INET;
1909 sin->sin_addr.s_addr = xp->selector.saddr.a4;
1910 sin->sin_port = xp->selector.sport;
1911 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1912 }
1913#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1914 else if (xp->family == AF_INET6) {
1915 sin6 = (struct sockaddr_in6 *) (addr + 1);
1916 sin6->sin6_family = AF_INET6;
1917 sin6->sin6_port = xp->selector.sport;
1918 sin6->sin6_flowinfo = 0;
1919 memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
1920 sizeof(struct in6_addr));
1921 sin6->sin6_scope_id = 0;
1922 }
1923#endif
1924 else
1925 BUG();
1926
1927 /* dst address */
1928 addr = (struct sadb_address*) skb_put(skb,
1929 sizeof(struct sadb_address)+sockaddr_size);
1930 addr->sadb_address_len =
1931 (sizeof(struct sadb_address)+sockaddr_size)/
1932 sizeof(uint64_t);
1933 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1934 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1935 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
1936 addr->sadb_address_reserved = 0;
1937 if (xp->family == AF_INET) {
1938 sin = (struct sockaddr_in *) (addr + 1);
1939 sin->sin_family = AF_INET;
1940 sin->sin_addr.s_addr = xp->selector.daddr.a4;
1941 sin->sin_port = xp->selector.dport;
1942 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1943 }
1944#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1945 else if (xp->family == AF_INET6) {
1946 sin6 = (struct sockaddr_in6 *) (addr + 1);
1947 sin6->sin6_family = AF_INET6;
1948 sin6->sin6_port = xp->selector.dport;
1949 sin6->sin6_flowinfo = 0;
1950 memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
1951 sizeof(struct in6_addr));
1952 sin6->sin6_scope_id = 0;
1953 }
1954#endif
1955 else
1956 BUG();
1957
1958 /* hard time */
1959 lifetime = (struct sadb_lifetime *) skb_put(skb,
1960 sizeof(struct sadb_lifetime));
1961 lifetime->sadb_lifetime_len =
1962 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1963 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1964 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
1965 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
1966 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
1967 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
1968 /* soft time */
1969 lifetime = (struct sadb_lifetime *) skb_put(skb,
1970 sizeof(struct sadb_lifetime));
1971 lifetime->sadb_lifetime_len =
1972 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1973 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
1974 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
1975 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
1976 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
1977 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
1978 /* current time */
1979 lifetime = (struct sadb_lifetime *) skb_put(skb,
1980 sizeof(struct sadb_lifetime));
1981 lifetime->sadb_lifetime_len =
1982 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1983 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
1984 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
1985 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
1986 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
1987 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
1988
1989 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
1990 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
1991 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1992 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
1993 if (xp->action == XFRM_POLICY_ALLOW) {
1994 if (xp->xfrm_nr)
1995 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1996 else
1997 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
1998 }
1999 pol->sadb_x_policy_dir = dir+1;
2000 pol->sadb_x_policy_id = xp->index;
2001 pol->sadb_x_policy_priority = xp->priority;
2002
2003 for (i=0; i<xp->xfrm_nr; i++) {
2004 struct sadb_x_ipsecrequest *rq;
2005 struct xfrm_tmpl *t = xp->xfrm_vec + i;
2006 int req_size;
2007
2008 req_size = sizeof(struct sadb_x_ipsecrequest);
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002009 if (t->mode == XFRM_MODE_TUNNEL)
Miika Komu2718aa72006-11-30 16:41:50 -08002010 req_size += ((t->encap_family == AF_INET ?
2011 sizeof(struct sockaddr_in) :
2012 sizeof(struct sockaddr_in6)) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 else
2014 size -= 2*socklen;
2015 rq = (void*)skb_put(skb, req_size);
2016 pol->sadb_x_policy_len += req_size/8;
2017 memset(rq, 0, sizeof(*rq));
2018 rq->sadb_x_ipsecrequest_len = req_size;
2019 rq->sadb_x_ipsecrequest_proto = t->id.proto;
2020 rq->sadb_x_ipsecrequest_mode = t->mode+1;
2021 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2022 if (t->reqid)
2023 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2024 if (t->optional)
2025 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2026 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002027 if (t->mode == XFRM_MODE_TUNNEL) {
Miika Komu2718aa72006-11-30 16:41:50 -08002028 switch (t->encap_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 case AF_INET:
2030 sin = (void*)(rq+1);
2031 sin->sin_family = AF_INET;
2032 sin->sin_addr.s_addr = t->saddr.a4;
2033 sin->sin_port = 0;
2034 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2035 sin++;
2036 sin->sin_family = AF_INET;
2037 sin->sin_addr.s_addr = t->id.daddr.a4;
2038 sin->sin_port = 0;
2039 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2040 break;
2041#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2042 case AF_INET6:
2043 sin6 = (void*)(rq+1);
2044 sin6->sin6_family = AF_INET6;
2045 sin6->sin6_port = 0;
2046 sin6->sin6_flowinfo = 0;
2047 memcpy(&sin6->sin6_addr, t->saddr.a6,
2048 sizeof(struct in6_addr));
2049 sin6->sin6_scope_id = 0;
2050
2051 sin6++;
2052 sin6->sin6_family = AF_INET6;
2053 sin6->sin6_port = 0;
2054 sin6->sin6_flowinfo = 0;
2055 memcpy(&sin6->sin6_addr, t->id.daddr.a6,
2056 sizeof(struct in6_addr));
2057 sin6->sin6_scope_id = 0;
2058 break;
2059#endif
2060 default:
2061 break;
2062 }
2063 }
2064 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002065
2066 /* security context */
2067 if ((xfrm_ctx = xp->security)) {
2068 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2069
2070 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2071 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2072 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2073 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2074 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2075 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2076 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2077 xfrm_ctx->ctx_len);
2078 }
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 hdr->sadb_msg_len = size / sizeof(uint64_t);
2081 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
2082}
2083
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002084static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2085{
2086 struct sk_buff *out_skb;
2087 struct sadb_msg *out_hdr;
2088 int err;
2089
2090 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2091 if (IS_ERR(out_skb)) {
2092 err = PTR_ERR(out_skb);
2093 goto out;
2094 }
2095 pfkey_xfrm_policy2msg(out_skb, xp, dir);
2096
2097 out_hdr = (struct sadb_msg *) out_skb->data;
2098 out_hdr->sadb_msg_version = PF_KEY_V2;
2099
Herbert Xuf60f6b82005-06-18 22:44:37 -07002100 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002101 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2102 else
2103 out_hdr->sadb_msg_type = event2poltype(c->event);
2104 out_hdr->sadb_msg_errno = 0;
2105 out_hdr->sadb_msg_seq = c->seq;
2106 out_hdr->sadb_msg_pid = c->pid;
2107 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
2108out:
2109 return 0;
2110
2111}
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2114{
Trent Jaegerdf718372005-12-13 23:12:27 -08002115 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 struct sadb_lifetime *lifetime;
2117 struct sadb_address *sa;
2118 struct sadb_x_policy *pol;
2119 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002120 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002121 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2124 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2125 !ext_hdrs[SADB_X_EXT_POLICY-1])
2126 return -EINVAL;
2127
2128 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2129 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2130 return -EINVAL;
2131 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2132 return -EINVAL;
2133
2134 xp = xfrm_policy_alloc(GFP_KERNEL);
2135 if (xp == NULL)
2136 return -ENOBUFS;
2137
2138 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2139 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2140 xp->priority = pol->sadb_x_policy_priority;
2141
2142 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2143 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2144 if (!xp->family) {
2145 err = -EINVAL;
2146 goto out;
2147 }
2148 xp->selector.family = xp->family;
2149 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2150 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2151 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2152 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002153 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2156 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2157 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2158
2159 /* Amusing, we set this twice. KAME apps appear to set same value
2160 * in both addresses.
2161 */
2162 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2163
2164 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2165 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002166 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Trent Jaegerdf718372005-12-13 23:12:27 -08002168 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2169 if (sec_ctx != NULL) {
2170 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2171
2172 if (!uctx) {
2173 err = -ENOBUFS;
2174 goto out;
2175 }
2176
2177 err = security_xfrm_policy_alloc(xp, uctx);
2178 kfree(uctx);
2179
2180 if (err)
2181 goto out;
2182 }
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 xp->lft.soft_byte_limit = XFRM_INF;
2185 xp->lft.hard_byte_limit = XFRM_INF;
2186 xp->lft.soft_packet_limit = XFRM_INF;
2187 xp->lft.hard_packet_limit = XFRM_INF;
2188 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2189 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2190 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2191 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2192 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2193 }
2194 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2195 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2196 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2197 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2198 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2199 }
2200 xp->xfrm_nr = 0;
2201 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2202 (err = parse_ipsecrequests(xp, pol)) < 0)
2203 goto out;
2204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2206 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002207
2208 if (err)
2209 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002211 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002212 c.event = XFRM_MSG_UPDPOLICY;
2213 else
2214 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002216 c.seq = hdr->sadb_msg_seq;
2217 c.pid = hdr->sadb_msg_pid;
2218
2219 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return 0;
2222
2223out:
Trent Jaegerdf718372005-12-13 23:12:27 -08002224 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 kfree(xp);
2226 return err;
2227}
2228
2229static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2230{
2231 int err;
2232 struct sadb_address *sa;
2233 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08002234 struct xfrm_policy *xp, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002236 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002237 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2240 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2241 !ext_hdrs[SADB_X_EXT_POLICY-1])
2242 return -EINVAL;
2243
2244 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2245 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2246 return -EINVAL;
2247
2248 memset(&sel, 0, sizeof(sel));
2249
2250 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2251 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2252 sel.prefixlen_s = sa->sadb_address_prefixlen;
2253 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2254 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2255 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002256 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2259 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2260 sel.prefixlen_d = sa->sadb_address_prefixlen;
2261 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2262 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2263 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002264 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Trent Jaegerdf718372005-12-13 23:12:27 -08002266 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2267 memset(&tmp, 0, sizeof(struct xfrm_policy));
2268
2269 if (sec_ctx != NULL) {
2270 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2271
2272 if (!uctx)
2273 return -ENOMEM;
2274
2275 err = security_xfrm_policy_alloc(&tmp, uctx);
2276 kfree(uctx);
2277
2278 if (err)
2279 return err;
2280 }
2281
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002282 xp = xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN, pol->sadb_x_policy_dir-1,
2283 &sel, tmp.security, 1);
Trent Jaegerdf718372005-12-13 23:12:27 -08002284 security_xfrm_policy_free(&tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (xp == NULL)
2286 return -ENOENT;
2287
2288 err = 0;
2289
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002290 if ((err = security_xfrm_policy_delete(xp)))
2291 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002292 c.seq = hdr->sadb_msg_seq;
2293 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002294 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002295 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2296
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002297out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002298 xfrm_pol_put(xp);
2299 return err;
2300}
2301
2302static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2303{
2304 int err;
2305 struct sk_buff *out_skb;
2306 struct sadb_msg *out_hdr;
2307 err = 0;
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2310 if (IS_ERR(out_skb)) {
2311 err = PTR_ERR(out_skb);
2312 goto out;
2313 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002314 pfkey_xfrm_policy2msg(out_skb, xp, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 out_hdr = (struct sadb_msg *) out_skb->data;
2317 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002318 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 out_hdr->sadb_msg_satype = 0;
2320 out_hdr->sadb_msg_errno = 0;
2321 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2322 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002323 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 err = 0;
2325
2326out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return err;
2328}
2329
2330static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2331{
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002332 unsigned int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 int err;
2334 struct sadb_x_policy *pol;
2335 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002336 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2339 return -EINVAL;
2340
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002341 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2342 if (dir >= XFRM_POLICY_MAX)
2343 return -EINVAL;
2344
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002345 xp = xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN, dir, pol->sadb_x_policy_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2347 if (xp == NULL)
2348 return -ENOENT;
2349
2350 err = 0;
2351
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002352 c.seq = hdr->sadb_msg_seq;
2353 c.pid = hdr->sadb_msg_pid;
2354 if (hdr->sadb_msg_type == SADB_X_SPDDELETE2) {
Herbert Xubf088672005-06-18 22:44:00 -07002355 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002356 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002357 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002358 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002359 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 xfrm_pol_put(xp);
2363 return err;
2364}
2365
2366static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2367{
2368 struct pfkey_dump_data *data = ptr;
2369 struct sk_buff *out_skb;
2370 struct sadb_msg *out_hdr;
2371
2372 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2373 if (IS_ERR(out_skb))
2374 return PTR_ERR(out_skb);
2375
2376 pfkey_xfrm_policy2msg(out_skb, xp, dir);
2377
2378 out_hdr = (struct sadb_msg *) out_skb->data;
2379 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
2380 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2381 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2382 out_hdr->sadb_msg_errno = 0;
2383 out_hdr->sadb_msg_seq = count;
2384 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
2385 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
2386 return 0;
2387}
2388
2389static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2390{
2391 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
2392
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002393 return xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_sp, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002396static int key_notify_policy_flush(struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
2398 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002399 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002401 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (!skb_out)
2403 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002404 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002405 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002406 hdr->sadb_msg_seq = c->seq;
2407 hdr->sadb_msg_pid = c->pid;
2408 hdr->sadb_msg_version = PF_KEY_V2;
2409 hdr->sadb_msg_errno = (uint8_t) 0;
2410 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2411 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2412 return 0;
2413
2414}
2415
2416static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2417{
2418 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002420 xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN);
2421 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002422 c.event = XFRM_MSG_FLUSHPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002423 c.pid = hdr->sadb_msg_pid;
2424 c.seq = hdr->sadb_msg_seq;
2425 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 return 0;
2428}
2429
2430typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2431 struct sadb_msg *hdr, void **ext_hdrs);
2432static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2433 [SADB_RESERVED] = pfkey_reserved,
2434 [SADB_GETSPI] = pfkey_getspi,
2435 [SADB_UPDATE] = pfkey_add,
2436 [SADB_ADD] = pfkey_add,
2437 [SADB_DELETE] = pfkey_delete,
2438 [SADB_GET] = pfkey_get,
2439 [SADB_ACQUIRE] = pfkey_acquire,
2440 [SADB_REGISTER] = pfkey_register,
2441 [SADB_EXPIRE] = NULL,
2442 [SADB_FLUSH] = pfkey_flush,
2443 [SADB_DUMP] = pfkey_dump,
2444 [SADB_X_PROMISC] = pfkey_promisc,
2445 [SADB_X_PCHANGE] = NULL,
2446 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2447 [SADB_X_SPDADD] = pfkey_spdadd,
2448 [SADB_X_SPDDELETE] = pfkey_spddelete,
2449 [SADB_X_SPDGET] = pfkey_spdget,
2450 [SADB_X_SPDACQUIRE] = NULL,
2451 [SADB_X_SPDDUMP] = pfkey_spddump,
2452 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2453 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2454 [SADB_X_SPDDELETE2] = pfkey_spdget,
2455};
2456
2457static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2458{
2459 void *ext_hdrs[SADB_EXT_MAX];
2460 int err;
2461
2462 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2463 BROADCAST_PROMISC_ONLY, NULL);
2464
2465 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2466 err = parse_exthdrs(skb, hdr, ext_hdrs);
2467 if (!err) {
2468 err = -EOPNOTSUPP;
2469 if (pfkey_funcs[hdr->sadb_msg_type])
2470 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2471 }
2472 return err;
2473}
2474
2475static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2476{
2477 struct sadb_msg *hdr = NULL;
2478
2479 if (skb->len < sizeof(*hdr)) {
2480 *errp = -EMSGSIZE;
2481 } else {
2482 hdr = (struct sadb_msg *) skb->data;
2483 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2484 hdr->sadb_msg_reserved != 0 ||
2485 (hdr->sadb_msg_type <= SADB_RESERVED ||
2486 hdr->sadb_msg_type > SADB_MAX)) {
2487 hdr = NULL;
2488 *errp = -EINVAL;
2489 } else if (hdr->sadb_msg_len != (skb->len /
2490 sizeof(uint64_t)) ||
2491 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2492 sizeof(uint64_t))) {
2493 hdr = NULL;
2494 *errp = -EMSGSIZE;
2495 } else {
2496 *errp = 0;
2497 }
2498 }
2499 return hdr;
2500}
2501
2502static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2503{
2504 return t->aalgos & (1 << d->desc.sadb_alg_id);
2505}
2506
2507static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2508{
2509 return t->ealgos & (1 << d->desc.sadb_alg_id);
2510}
2511
2512static int count_ah_combs(struct xfrm_tmpl *t)
2513{
2514 int i, sz = 0;
2515
2516 for (i = 0; ; i++) {
2517 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2518 if (!aalg)
2519 break;
2520 if (aalg_tmpl_set(t, aalg) && aalg->available)
2521 sz += sizeof(struct sadb_comb);
2522 }
2523 return sz + sizeof(struct sadb_prop);
2524}
2525
2526static int count_esp_combs(struct xfrm_tmpl *t)
2527{
2528 int i, k, sz = 0;
2529
2530 for (i = 0; ; i++) {
2531 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2532 if (!ealg)
2533 break;
2534
2535 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2536 continue;
2537
2538 for (k = 1; ; k++) {
2539 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2540 if (!aalg)
2541 break;
2542
2543 if (aalg_tmpl_set(t, aalg) && aalg->available)
2544 sz += sizeof(struct sadb_comb);
2545 }
2546 }
2547 return sz + sizeof(struct sadb_prop);
2548}
2549
2550static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2551{
2552 struct sadb_prop *p;
2553 int i;
2554
2555 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2556 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2557 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2558 p->sadb_prop_replay = 32;
2559 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2560
2561 for (i = 0; ; i++) {
2562 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2563 if (!aalg)
2564 break;
2565
2566 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2567 struct sadb_comb *c;
2568 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2569 memset(c, 0, sizeof(*c));
2570 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2571 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2572 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2573 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2574 c->sadb_comb_hard_addtime = 24*60*60;
2575 c->sadb_comb_soft_addtime = 20*60*60;
2576 c->sadb_comb_hard_usetime = 8*60*60;
2577 c->sadb_comb_soft_usetime = 7*60*60;
2578 }
2579 }
2580}
2581
2582static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2583{
2584 struct sadb_prop *p;
2585 int i, k;
2586
2587 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2588 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2589 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2590 p->sadb_prop_replay = 32;
2591 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2592
2593 for (i=0; ; i++) {
2594 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2595 if (!ealg)
2596 break;
2597
2598 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2599 continue;
2600
2601 for (k = 1; ; k++) {
2602 struct sadb_comb *c;
2603 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2604 if (!aalg)
2605 break;
2606 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2607 continue;
2608 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2609 memset(c, 0, sizeof(*c));
2610 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2611 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2612 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2613 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2614 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2615 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2616 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2617 c->sadb_comb_hard_addtime = 24*60*60;
2618 c->sadb_comb_soft_addtime = 20*60*60;
2619 c->sadb_comb_hard_usetime = 8*60*60;
2620 c->sadb_comb_soft_usetime = 7*60*60;
2621 }
2622 }
2623}
2624
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002625static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2626{
2627 return 0;
2628}
2629
2630static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
2632 struct sk_buff *out_skb;
2633 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002634 int hard;
2635 int hsc;
2636
Herbert Xubf088672005-06-18 22:44:00 -07002637 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002638 if (hard)
2639 hsc = 2;
2640 else
2641 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 out_skb = pfkey_xfrm_state2msg(x, 0, hsc);
2644 if (IS_ERR(out_skb))
2645 return PTR_ERR(out_skb);
2646
2647 out_hdr = (struct sadb_msg *) out_skb->data;
2648 out_hdr->sadb_msg_version = PF_KEY_V2;
2649 out_hdr->sadb_msg_type = SADB_EXPIRE;
2650 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2651 out_hdr->sadb_msg_errno = 0;
2652 out_hdr->sadb_msg_reserved = 0;
2653 out_hdr->sadb_msg_seq = 0;
2654 out_hdr->sadb_msg_pid = 0;
2655
2656 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2657 return 0;
2658}
2659
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002660static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2661{
2662 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002663 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002664 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002665 case XFRM_MSG_DELSA:
2666 case XFRM_MSG_NEWSA:
2667 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002668 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002669 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002670 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002671 case XFRM_MSG_NEWAE: /* not yet supported */
2672 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002673 default:
2674 printk("pfkey: Unknown SA event %d\n", c->event);
2675 break;
2676 }
2677
2678 return 0;
2679}
2680
2681static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2682{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002683 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
2684 return 0;
2685
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002686 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002687 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002688 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002689 case XFRM_MSG_DELPOLICY:
2690 case XFRM_MSG_NEWPOLICY:
2691 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002692 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002693 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002694 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
2695 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002696 return key_notify_policy_flush(c);
2697 default:
2698 printk("pfkey: Unknown policy event %d\n", c->event);
2699 break;
2700 }
2701
2702 return 0;
2703}
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705static u32 get_acqseq(void)
2706{
2707 u32 res;
2708 static u32 acqseq;
2709 static DEFINE_SPINLOCK(acqseq_lock);
2710
2711 spin_lock_bh(&acqseq_lock);
2712 res = (++acqseq ? : ++acqseq);
2713 spin_unlock_bh(&acqseq_lock);
2714 return res;
2715}
2716
2717static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2718{
2719 struct sk_buff *skb;
2720 struct sadb_msg *hdr;
2721 struct sadb_address *addr;
2722 struct sadb_x_policy *pol;
2723 struct sockaddr_in *sin;
2724#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2725 struct sockaddr_in6 *sin6;
2726#endif
2727 int sockaddr_size;
2728 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07002729 struct sadb_x_sec_ctx *sec_ctx;
2730 struct xfrm_sec_ctx *xfrm_ctx;
2731 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2734 if (!sockaddr_size)
2735 return -EINVAL;
2736
2737 size = sizeof(struct sadb_msg) +
2738 (sizeof(struct sadb_address) * 2) +
2739 (sockaddr_size * 2) +
2740 sizeof(struct sadb_x_policy);
2741
2742 if (x->id.proto == IPPROTO_AH)
2743 size += count_ah_combs(t);
2744 else if (x->id.proto == IPPROTO_ESP)
2745 size += count_esp_combs(t);
2746
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07002747 if ((xfrm_ctx = x->security)) {
2748 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
2749 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
2750 }
2751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 skb = alloc_skb(size + 16, GFP_ATOMIC);
2753 if (skb == NULL)
2754 return -ENOMEM;
2755
2756 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2757 hdr->sadb_msg_version = PF_KEY_V2;
2758 hdr->sadb_msg_type = SADB_ACQUIRE;
2759 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2760 hdr->sadb_msg_len = size / sizeof(uint64_t);
2761 hdr->sadb_msg_errno = 0;
2762 hdr->sadb_msg_reserved = 0;
2763 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2764 hdr->sadb_msg_pid = 0;
2765
2766 /* src address */
2767 addr = (struct sadb_address*) skb_put(skb,
2768 sizeof(struct sadb_address)+sockaddr_size);
2769 addr->sadb_address_len =
2770 (sizeof(struct sadb_address)+sockaddr_size)/
2771 sizeof(uint64_t);
2772 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2773 addr->sadb_address_proto = 0;
2774 addr->sadb_address_reserved = 0;
2775 if (x->props.family == AF_INET) {
2776 addr->sadb_address_prefixlen = 32;
2777
2778 sin = (struct sockaddr_in *) (addr + 1);
2779 sin->sin_family = AF_INET;
2780 sin->sin_addr.s_addr = x->props.saddr.a4;
2781 sin->sin_port = 0;
2782 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2783 }
2784#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2785 else if (x->props.family == AF_INET6) {
2786 addr->sadb_address_prefixlen = 128;
2787
2788 sin6 = (struct sockaddr_in6 *) (addr + 1);
2789 sin6->sin6_family = AF_INET6;
2790 sin6->sin6_port = 0;
2791 sin6->sin6_flowinfo = 0;
2792 memcpy(&sin6->sin6_addr,
2793 x->props.saddr.a6, sizeof(struct in6_addr));
2794 sin6->sin6_scope_id = 0;
2795 }
2796#endif
2797 else
2798 BUG();
2799
2800 /* dst address */
2801 addr = (struct sadb_address*) skb_put(skb,
2802 sizeof(struct sadb_address)+sockaddr_size);
2803 addr->sadb_address_len =
2804 (sizeof(struct sadb_address)+sockaddr_size)/
2805 sizeof(uint64_t);
2806 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2807 addr->sadb_address_proto = 0;
2808 addr->sadb_address_reserved = 0;
2809 if (x->props.family == AF_INET) {
2810 addr->sadb_address_prefixlen = 32;
2811
2812 sin = (struct sockaddr_in *) (addr + 1);
2813 sin->sin_family = AF_INET;
2814 sin->sin_addr.s_addr = x->id.daddr.a4;
2815 sin->sin_port = 0;
2816 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2817 }
2818#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2819 else if (x->props.family == AF_INET6) {
2820 addr->sadb_address_prefixlen = 128;
2821
2822 sin6 = (struct sockaddr_in6 *) (addr + 1);
2823 sin6->sin6_family = AF_INET6;
2824 sin6->sin6_port = 0;
2825 sin6->sin6_flowinfo = 0;
2826 memcpy(&sin6->sin6_addr,
2827 x->id.daddr.a6, sizeof(struct in6_addr));
2828 sin6->sin6_scope_id = 0;
2829 }
2830#endif
2831 else
2832 BUG();
2833
2834 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2835 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2836 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2837 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2838 pol->sadb_x_policy_dir = dir+1;
2839 pol->sadb_x_policy_id = xp->index;
2840
2841 /* Set sadb_comb's. */
2842 if (x->id.proto == IPPROTO_AH)
2843 dump_ah_combs(skb, t);
2844 else if (x->id.proto == IPPROTO_ESP)
2845 dump_esp_combs(skb, t);
2846
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07002847 /* security context */
2848 if (xfrm_ctx) {
2849 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
2850 sizeof(struct sadb_x_sec_ctx) + ctx_size);
2851 sec_ctx->sadb_x_sec_len =
2852 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
2853 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2854 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2855 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2856 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2857 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2858 xfrm_ctx->ctx_len);
2859 }
2860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2862}
2863
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002864static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 u8 *data, int len, int *dir)
2866{
2867 struct xfrm_policy *xp;
2868 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08002869 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002871 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 case AF_INET:
2873 if (opt != IP_IPSEC_POLICY) {
2874 *dir = -EOPNOTSUPP;
2875 return NULL;
2876 }
2877 break;
2878#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2879 case AF_INET6:
2880 if (opt != IPV6_IPSEC_POLICY) {
2881 *dir = -EOPNOTSUPP;
2882 return NULL;
2883 }
2884 break;
2885#endif
2886 default:
2887 *dir = -EINVAL;
2888 return NULL;
2889 }
2890
2891 *dir = -EINVAL;
2892
2893 if (len < sizeof(struct sadb_x_policy) ||
2894 pol->sadb_x_policy_len*8 > len ||
2895 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
2896 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
2897 return NULL;
2898
2899 xp = xfrm_policy_alloc(GFP_ATOMIC);
2900 if (xp == NULL) {
2901 *dir = -ENOBUFS;
2902 return NULL;
2903 }
2904
2905 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2906 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2907
2908 xp->lft.soft_byte_limit = XFRM_INF;
2909 xp->lft.hard_byte_limit = XFRM_INF;
2910 xp->lft.soft_packet_limit = XFRM_INF;
2911 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002912 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
2914 xp->xfrm_nr = 0;
2915 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2916 (*dir = parse_ipsecrequests(xp, pol)) < 0)
2917 goto out;
2918
Trent Jaegerdf718372005-12-13 23:12:27 -08002919 /* security context too */
2920 if (len >= (pol->sadb_x_policy_len*8 +
2921 sizeof(struct sadb_x_sec_ctx))) {
2922 char *p = (char *)pol;
2923 struct xfrm_user_sec_ctx *uctx;
2924
2925 p += pol->sadb_x_policy_len*8;
2926 sec_ctx = (struct sadb_x_sec_ctx *)p;
2927 if (len < pol->sadb_x_policy_len*8 +
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002928 sec_ctx->sadb_x_sec_len) {
2929 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08002930 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002931 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002932 if ((*dir = verify_sec_ctx_len(p)))
2933 goto out;
2934 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2935 *dir = security_xfrm_policy_alloc(xp, uctx);
2936 kfree(uctx);
2937
2938 if (*dir)
2939 goto out;
2940 }
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 *dir = pol->sadb_x_policy_dir-1;
2943 return xp;
2944
2945out:
Trent Jaegerdf718372005-12-13 23:12:27 -08002946 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 kfree(xp);
2948 return NULL;
2949}
2950
Al Viro5d36b182006-11-08 00:24:06 -08002951static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
2953 struct sk_buff *skb;
2954 struct sadb_msg *hdr;
2955 struct sadb_sa *sa;
2956 struct sadb_address *addr;
2957 struct sadb_x_nat_t_port *n_port;
2958 struct sockaddr_in *sin;
2959#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2960 struct sockaddr_in6 *sin6;
2961#endif
2962 int sockaddr_size;
2963 int size;
2964 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
2965 struct xfrm_encap_tmpl *natt = NULL;
2966
2967 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2968 if (!sockaddr_size)
2969 return -EINVAL;
2970
2971 if (!satype)
2972 return -EINVAL;
2973
2974 if (!x->encap)
2975 return -EINVAL;
2976
2977 natt = x->encap;
2978
2979 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
2980 *
2981 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
2982 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
2983 */
2984
2985 size = sizeof(struct sadb_msg) +
2986 sizeof(struct sadb_sa) +
2987 (sizeof(struct sadb_address) * 2) +
2988 (sockaddr_size * 2) +
2989 (sizeof(struct sadb_x_nat_t_port) * 2);
2990
2991 skb = alloc_skb(size + 16, GFP_ATOMIC);
2992 if (skb == NULL)
2993 return -ENOMEM;
2994
2995 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2996 hdr->sadb_msg_version = PF_KEY_V2;
2997 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
2998 hdr->sadb_msg_satype = satype;
2999 hdr->sadb_msg_len = size / sizeof(uint64_t);
3000 hdr->sadb_msg_errno = 0;
3001 hdr->sadb_msg_reserved = 0;
3002 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3003 hdr->sadb_msg_pid = 0;
3004
3005 /* SA */
3006 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3007 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3008 sa->sadb_sa_exttype = SADB_EXT_SA;
3009 sa->sadb_sa_spi = x->id.spi;
3010 sa->sadb_sa_replay = 0;
3011 sa->sadb_sa_state = 0;
3012 sa->sadb_sa_auth = 0;
3013 sa->sadb_sa_encrypt = 0;
3014 sa->sadb_sa_flags = 0;
3015
3016 /* ADDRESS_SRC (old addr) */
3017 addr = (struct sadb_address*)
3018 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3019 addr->sadb_address_len =
3020 (sizeof(struct sadb_address)+sockaddr_size)/
3021 sizeof(uint64_t);
3022 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3023 addr->sadb_address_proto = 0;
3024 addr->sadb_address_reserved = 0;
3025 if (x->props.family == AF_INET) {
3026 addr->sadb_address_prefixlen = 32;
3027
3028 sin = (struct sockaddr_in *) (addr + 1);
3029 sin->sin_family = AF_INET;
3030 sin->sin_addr.s_addr = x->props.saddr.a4;
3031 sin->sin_port = 0;
3032 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3033 }
3034#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3035 else if (x->props.family == AF_INET6) {
3036 addr->sadb_address_prefixlen = 128;
3037
3038 sin6 = (struct sockaddr_in6 *) (addr + 1);
3039 sin6->sin6_family = AF_INET6;
3040 sin6->sin6_port = 0;
3041 sin6->sin6_flowinfo = 0;
3042 memcpy(&sin6->sin6_addr,
3043 x->props.saddr.a6, sizeof(struct in6_addr));
3044 sin6->sin6_scope_id = 0;
3045 }
3046#endif
3047 else
3048 BUG();
3049
3050 /* NAT_T_SPORT (old port) */
3051 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3052 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3053 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3054 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3055 n_port->sadb_x_nat_t_port_reserved = 0;
3056
3057 /* ADDRESS_DST (new addr) */
3058 addr = (struct sadb_address*)
3059 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3060 addr->sadb_address_len =
3061 (sizeof(struct sadb_address)+sockaddr_size)/
3062 sizeof(uint64_t);
3063 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3064 addr->sadb_address_proto = 0;
3065 addr->sadb_address_reserved = 0;
3066 if (x->props.family == AF_INET) {
3067 addr->sadb_address_prefixlen = 32;
3068
3069 sin = (struct sockaddr_in *) (addr + 1);
3070 sin->sin_family = AF_INET;
3071 sin->sin_addr.s_addr = ipaddr->a4;
3072 sin->sin_port = 0;
3073 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3074 }
3075#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3076 else if (x->props.family == AF_INET6) {
3077 addr->sadb_address_prefixlen = 128;
3078
3079 sin6 = (struct sockaddr_in6 *) (addr + 1);
3080 sin6->sin6_family = AF_INET6;
3081 sin6->sin6_port = 0;
3082 sin6->sin6_flowinfo = 0;
3083 memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
3084 sin6->sin6_scope_id = 0;
3085 }
3086#endif
3087 else
3088 BUG();
3089
3090 /* NAT_T_DPORT (new port) */
3091 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3092 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3093 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3094 n_port->sadb_x_nat_t_port_port = sport;
3095 n_port->sadb_x_nat_t_port_reserved = 0;
3096
3097 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3098}
3099
3100static int pfkey_sendmsg(struct kiocb *kiocb,
3101 struct socket *sock, struct msghdr *msg, size_t len)
3102{
3103 struct sock *sk = sock->sk;
3104 struct sk_buff *skb = NULL;
3105 struct sadb_msg *hdr = NULL;
3106 int err;
3107
3108 err = -EOPNOTSUPP;
3109 if (msg->msg_flags & MSG_OOB)
3110 goto out;
3111
3112 err = -EMSGSIZE;
3113 if ((unsigned)len > sk->sk_sndbuf - 32)
3114 goto out;
3115
3116 err = -ENOBUFS;
3117 skb = alloc_skb(len, GFP_KERNEL);
3118 if (skb == NULL)
3119 goto out;
3120
3121 err = -EFAULT;
3122 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3123 goto out;
3124
3125 hdr = pfkey_get_base_msg(skb, &err);
3126 if (!hdr)
3127 goto out;
3128
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003129 mutex_lock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 err = pfkey_process(sk, skb, hdr);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003131 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
3133out:
3134 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3135 err = 0;
3136 if (skb)
3137 kfree_skb(skb);
3138
3139 return err ? : len;
3140}
3141
3142static int pfkey_recvmsg(struct kiocb *kiocb,
3143 struct socket *sock, struct msghdr *msg, size_t len,
3144 int flags)
3145{
3146 struct sock *sk = sock->sk;
3147 struct sk_buff *skb;
3148 int copied, err;
3149
3150 err = -EINVAL;
3151 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3152 goto out;
3153
3154 msg->msg_namelen = 0;
3155 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3156 if (skb == NULL)
3157 goto out;
3158
3159 copied = skb->len;
3160 if (copied > len) {
3161 msg->msg_flags |= MSG_TRUNC;
3162 copied = len;
3163 }
3164
3165 skb->h.raw = skb->data;
3166 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3167 if (err)
3168 goto out_free;
3169
3170 sock_recv_timestamp(msg, sk, skb);
3171
3172 err = (flags & MSG_TRUNC) ? skb->len : copied;
3173
3174out_free:
3175 skb_free_datagram(sk, skb);
3176out:
3177 return err;
3178}
3179
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003180static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 .family = PF_KEY,
3182 .owner = THIS_MODULE,
3183 /* Operations that make no sense on pfkey sockets. */
3184 .bind = sock_no_bind,
3185 .connect = sock_no_connect,
3186 .socketpair = sock_no_socketpair,
3187 .accept = sock_no_accept,
3188 .getname = sock_no_getname,
3189 .ioctl = sock_no_ioctl,
3190 .listen = sock_no_listen,
3191 .shutdown = sock_no_shutdown,
3192 .setsockopt = sock_no_setsockopt,
3193 .getsockopt = sock_no_getsockopt,
3194 .mmap = sock_no_mmap,
3195 .sendpage = sock_no_sendpage,
3196
3197 /* Now the operations that really occur. */
3198 .release = pfkey_release,
3199 .poll = datagram_poll,
3200 .sendmsg = pfkey_sendmsg,
3201 .recvmsg = pfkey_recvmsg,
3202};
3203
3204static struct net_proto_family pfkey_family_ops = {
3205 .family = PF_KEY,
3206 .create = pfkey_create,
3207 .owner = THIS_MODULE,
3208};
3209
3210#ifdef CONFIG_PROC_FS
3211static int pfkey_read_proc(char *buffer, char **start, off_t offset,
3212 int length, int *eof, void *data)
3213{
3214 off_t pos = 0;
3215 off_t begin = 0;
3216 int len = 0;
3217 struct sock *s;
3218 struct hlist_node *node;
3219
3220 len += sprintf(buffer,"sk RefCnt Rmem Wmem User Inode\n");
3221
3222 read_lock(&pfkey_table_lock);
3223
3224 sk_for_each(s, node, &pfkey_table) {
3225 len += sprintf(buffer+len,"%p %-6d %-6u %-6u %-6u %-6lu",
3226 s,
3227 atomic_read(&s->sk_refcnt),
3228 atomic_read(&s->sk_rmem_alloc),
3229 atomic_read(&s->sk_wmem_alloc),
3230 sock_i_uid(s),
3231 sock_i_ino(s)
3232 );
3233
3234 buffer[len++] = '\n';
3235
3236 pos = begin + len;
3237 if (pos < offset) {
3238 len = 0;
3239 begin = pos;
3240 }
3241 if(pos > offset + length)
3242 goto done;
3243 }
3244 *eof = 1;
3245
3246done:
3247 read_unlock(&pfkey_table_lock);
3248
3249 *start = buffer + (offset - begin);
3250 len -= (offset - begin);
3251
3252 if (len > length)
3253 len = length;
3254 if (len < 0)
3255 len = 0;
3256
3257 return len;
3258}
3259#endif
3260
3261static struct xfrm_mgr pfkeyv2_mgr =
3262{
3263 .id = "pfkeyv2",
3264 .notify = pfkey_send_notify,
3265 .acquire = pfkey_send_acquire,
3266 .compile_policy = pfkey_compile_policy,
3267 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003268 .notify_policy = pfkey_send_policy_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269};
3270
3271static void __exit ipsec_pfkey_exit(void)
3272{
3273 xfrm_unregister_km(&pfkeyv2_mgr);
3274 remove_proc_entry("net/pfkey", NULL);
3275 sock_unregister(PF_KEY);
3276 proto_unregister(&key_proto);
3277}
3278
3279static int __init ipsec_pfkey_init(void)
3280{
3281 int err = proto_register(&key_proto, 0);
3282
3283 if (err != 0)
3284 goto out;
3285
3286 err = sock_register(&pfkey_family_ops);
3287 if (err != 0)
3288 goto out_unregister_key_proto;
3289#ifdef CONFIG_PROC_FS
3290 err = -ENOMEM;
3291 if (create_proc_read_entry("net/pfkey", 0, NULL, pfkey_read_proc, NULL) == NULL)
3292 goto out_sock_unregister;
3293#endif
3294 err = xfrm_register_km(&pfkeyv2_mgr);
3295 if (err != 0)
3296 goto out_remove_proc_entry;
3297out:
3298 return err;
3299out_remove_proc_entry:
3300#ifdef CONFIG_PROC_FS
3301 remove_proc_entry("net/pfkey", NULL);
3302out_sock_unregister:
3303#endif
3304 sock_unregister(PF_KEY);
3305out_unregister_key_proto:
3306 proto_unregister(&key_proto);
3307 goto out;
3308}
3309
3310module_init(ipsec_pfkey_init);
3311module_exit(ipsec_pfkey_exit);
3312MODULE_LICENSE("GPL");
3313MODULE_ALIAS_NETPROTO(PF_KEY);