blob: 1a2bd5f88b7d98fdbc10fe806ccf5b80233c4e1f [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>
Joy Latten161a09e2006-11-27 13:11:54 -060030#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <net/sock.h>
33
34#define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
35#define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
36
37
38/* List of all pfkey sockets. */
39static HLIST_HEAD(pfkey_table);
40static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait);
41static DEFINE_RWLOCK(pfkey_table_lock);
42static atomic_t pfkey_table_users = ATOMIC_INIT(0);
43
44static atomic_t pfkey_socks_nr = ATOMIC_INIT(0);
45
46struct pfkey_sock {
47 /* struct sock must be the first member of struct pfkey_sock */
48 struct sock sk;
49 int registered;
50 int promisc;
51};
52
53static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
54{
55 return (struct pfkey_sock *)sk;
56}
57
58static void pfkey_sock_destruct(struct sock *sk)
59{
60 skb_queue_purge(&sk->sk_receive_queue);
61
62 if (!sock_flag(sk, SOCK_DEAD)) {
63 printk("Attempt to release alive pfkey socket: %p\n", sk);
64 return;
65 }
66
67 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
68 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
69
70 atomic_dec(&pfkey_socks_nr);
71}
72
73static void pfkey_table_grab(void)
74{
75 write_lock_bh(&pfkey_table_lock);
76
77 if (atomic_read(&pfkey_table_users)) {
78 DECLARE_WAITQUEUE(wait, current);
79
80 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
81 for(;;) {
82 set_current_state(TASK_UNINTERRUPTIBLE);
83 if (atomic_read(&pfkey_table_users) == 0)
84 break;
85 write_unlock_bh(&pfkey_table_lock);
86 schedule();
87 write_lock_bh(&pfkey_table_lock);
88 }
89
90 __set_current_state(TASK_RUNNING);
91 remove_wait_queue(&pfkey_table_wait, &wait);
92 }
93}
94
95static __inline__ void pfkey_table_ungrab(void)
96{
97 write_unlock_bh(&pfkey_table_lock);
98 wake_up(&pfkey_table_wait);
99}
100
101static __inline__ void pfkey_lock_table(void)
102{
103 /* read_lock() synchronizes us to pfkey_table_grab */
104
105 read_lock(&pfkey_table_lock);
106 atomic_inc(&pfkey_table_users);
107 read_unlock(&pfkey_table_lock);
108}
109
110static __inline__ void pfkey_unlock_table(void)
111{
112 if (atomic_dec_and_test(&pfkey_table_users))
113 wake_up(&pfkey_table_wait);
114}
115
116
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800117static const struct proto_ops pfkey_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119static void pfkey_insert(struct sock *sk)
120{
121 pfkey_table_grab();
122 sk_add_node(sk, &pfkey_table);
123 pfkey_table_ungrab();
124}
125
126static void pfkey_remove(struct sock *sk)
127{
128 pfkey_table_grab();
129 sk_del_node_init(sk);
130 pfkey_table_ungrab();
131}
132
133static struct proto key_proto = {
134 .name = "KEY",
135 .owner = THIS_MODULE,
136 .obj_size = sizeof(struct pfkey_sock),
137};
138
139static int pfkey_create(struct socket *sock, int protocol)
140{
141 struct sock *sk;
142 int err;
143
144 if (!capable(CAP_NET_ADMIN))
145 return -EPERM;
146 if (sock->type != SOCK_RAW)
147 return -ESOCKTNOSUPPORT;
148 if (protocol != PF_KEY_V2)
149 return -EPROTONOSUPPORT;
150
151 err = -ENOMEM;
152 sk = sk_alloc(PF_KEY, GFP_KERNEL, &key_proto, 1);
153 if (sk == NULL)
154 goto out;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 sock->ops = &pfkey_ops;
157 sock_init_data(sock, sk);
158
159 sk->sk_family = PF_KEY;
160 sk->sk_destruct = pfkey_sock_destruct;
161
162 atomic_inc(&pfkey_socks_nr);
163
164 pfkey_insert(sk);
165
166 return 0;
167out:
168 return err;
169}
170
171static int pfkey_release(struct socket *sock)
172{
173 struct sock *sk = sock->sk;
174
175 if (!sk)
176 return 0;
177
178 pfkey_remove(sk);
179
180 sock_orphan(sk);
181 sock->sk = NULL;
182 skb_queue_purge(&sk->sk_write_queue);
183 sock_put(sk);
184
185 return 0;
186}
187
188static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
Al Virodd0fc662005-10-07 07:46:04 +0100189 gfp_t allocation, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 int err = -ENOBUFS;
192
193 sock_hold(sk);
194 if (*skb2 == NULL) {
195 if (atomic_read(&skb->users) != 1) {
196 *skb2 = skb_clone(skb, allocation);
197 } else {
198 *skb2 = skb;
199 atomic_inc(&skb->users);
200 }
201 }
202 if (*skb2 != NULL) {
203 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
204 skb_orphan(*skb2);
205 skb_set_owner_r(*skb2, sk);
206 skb_queue_tail(&sk->sk_receive_queue, *skb2);
207 sk->sk_data_ready(sk, (*skb2)->len);
208 *skb2 = NULL;
209 err = 0;
210 }
211 }
212 sock_put(sk);
213 return err;
214}
215
216/* Send SKB to all pfkey sockets matching selected criteria. */
217#define BROADCAST_ALL 0
218#define BROADCAST_ONE 1
219#define BROADCAST_REGISTERED 2
220#define BROADCAST_PROMISC_ONLY 4
Al Virodd0fc662005-10-07 07:46:04 +0100221static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int broadcast_flags, struct sock *one_sk)
223{
224 struct sock *sk;
225 struct hlist_node *node;
226 struct sk_buff *skb2 = NULL;
227 int err = -ESRCH;
228
229 /* XXX Do we need something like netlink_overrun? I think
230 * XXX PF_KEY socket apps will not mind current behavior.
231 */
232 if (!skb)
233 return -ENOMEM;
234
235 pfkey_lock_table();
236 sk_for_each(sk, node, &pfkey_table) {
237 struct pfkey_sock *pfk = pfkey_sk(sk);
238 int err2;
239
240 /* Yes, it means that if you are meant to receive this
241 * pfkey message you receive it twice as promiscuous
242 * socket.
243 */
244 if (pfk->promisc)
245 pfkey_broadcast_one(skb, &skb2, allocation, sk);
246
247 /* the exact target will be processed later */
248 if (sk == one_sk)
249 continue;
250 if (broadcast_flags != BROADCAST_ALL) {
251 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
252 continue;
253 if ((broadcast_flags & BROADCAST_REGISTERED) &&
254 !pfk->registered)
255 continue;
256 if (broadcast_flags & BROADCAST_ONE)
257 continue;
258 }
259
260 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
261
262 /* Error is cleare after succecful sending to at least one
263 * registered KM */
264 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
265 err = err2;
266 }
267 pfkey_unlock_table();
268
269 if (one_sk != NULL)
270 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
271
272 if (skb2)
273 kfree_skb(skb2);
274 kfree_skb(skb);
275 return err;
276}
277
278static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
279{
280 *new = *orig;
281}
282
283static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
284{
285 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
286 struct sadb_msg *hdr;
287
288 if (!skb)
289 return -ENOBUFS;
290
291 /* Woe be to the platform trying to support PFKEY yet
292 * having normal errnos outside the 1-255 range, inclusive.
293 */
294 err = -err;
295 if (err == ERESTARTSYS ||
296 err == ERESTARTNOHAND ||
297 err == ERESTARTNOINTR)
298 err = EINTR;
299 if (err >= 512)
300 err = EINVAL;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800301 BUG_ON(err <= 0 || err >= 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
304 pfkey_hdr_dup(hdr, orig);
305 hdr->sadb_msg_errno = (uint8_t) err;
306 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
307 sizeof(uint64_t));
308
309 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);
310
311 return 0;
312}
313
314static u8 sadb_ext_min_len[] = {
315 [SADB_EXT_RESERVED] = (u8) 0,
316 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
317 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
318 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
319 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
320 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
321 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
322 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
323 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
324 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
325 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
326 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
327 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
328 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
329 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
330 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
331 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
332 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
333 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
334 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
335 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
336 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
337 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
338 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
Trent Jaegerdf718372005-12-13 23:12:27 -0800339 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340};
341
342/* Verify sadb_address_{len,prefixlen} against sa_family. */
343static int verify_address_len(void *p)
344{
345 struct sadb_address *sp = p;
346 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
347 struct sockaddr_in *sin;
348#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
349 struct sockaddr_in6 *sin6;
350#endif
351 int len;
352
353 switch (addr->sa_family) {
354 case AF_INET:
355 len = sizeof(*sp) + sizeof(*sin) + (sizeof(uint64_t) - 1);
356 len /= sizeof(uint64_t);
357 if (sp->sadb_address_len != len ||
358 sp->sadb_address_prefixlen > 32)
359 return -EINVAL;
360 break;
361#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
362 case AF_INET6:
363 len = sizeof(*sp) + sizeof(*sin6) + (sizeof(uint64_t) - 1);
364 len /= sizeof(uint64_t);
365 if (sp->sadb_address_len != len ||
366 sp->sadb_address_prefixlen > 128)
367 return -EINVAL;
368 break;
369#endif
370 default:
371 /* It is user using kernel to keep track of security
372 * associations for another protocol, such as
373 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
374 * lengths.
375 *
376 * XXX Actually, association/policy database is not yet
377 * XXX able to cope with arbitrary sockaddr families.
378 * XXX When it can, remove this -EINVAL. -DaveM
379 */
380 return -EINVAL;
381 break;
382 };
383
384 return 0;
385}
386
Trent Jaegerdf718372005-12-13 23:12:27 -0800387static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx)
388{
389 int len = 0;
390
391 len += sizeof(struct sadb_x_sec_ctx);
392 len += sec_ctx->sadb_x_ctx_len;
393 len += sizeof(uint64_t) - 1;
394 len /= sizeof(uint64_t);
395
396 return len;
397}
398
399static inline int verify_sec_ctx_len(void *p)
400{
401 struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p;
402 int len;
403
404 if (sec_ctx->sadb_x_ctx_len > PAGE_SIZE)
405 return -EINVAL;
406
407 len = pfkey_sec_ctx_len(sec_ctx);
408
409 if (sec_ctx->sadb_x_sec_len != len)
410 return -EINVAL;
411
412 return 0;
413}
414
415static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx)
416{
417 struct xfrm_user_sec_ctx *uctx = NULL;
418 int ctx_size = sec_ctx->sadb_x_ctx_len;
419
420 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
421
422 if (!uctx)
423 return NULL;
424
425 uctx->len = pfkey_sec_ctx_len(sec_ctx);
426 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
427 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
428 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
429 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
430 memcpy(uctx + 1, sec_ctx + 1,
431 uctx->ctx_len);
432
433 return uctx;
434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static int present_and_same_family(struct sadb_address *src,
437 struct sadb_address *dst)
438{
439 struct sockaddr *s_addr, *d_addr;
440
441 if (!src || !dst)
442 return 0;
443
444 s_addr = (struct sockaddr *)(src + 1);
445 d_addr = (struct sockaddr *)(dst + 1);
446 if (s_addr->sa_family != d_addr->sa_family)
447 return 0;
448 if (s_addr->sa_family != AF_INET
449#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
450 && s_addr->sa_family != AF_INET6
451#endif
452 )
453 return 0;
454
455 return 1;
456}
457
458static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
459{
460 char *p = (char *) hdr;
461 int len = skb->len;
462
463 len -= sizeof(*hdr);
464 p += sizeof(*hdr);
465 while (len > 0) {
466 struct sadb_ext *ehdr = (struct sadb_ext *) p;
467 uint16_t ext_type;
468 int ext_len;
469
470 ext_len = ehdr->sadb_ext_len;
471 ext_len *= sizeof(uint64_t);
472 ext_type = ehdr->sadb_ext_type;
473 if (ext_len < sizeof(uint64_t) ||
474 ext_len > len ||
475 ext_type == SADB_EXT_RESERVED)
476 return -EINVAL;
477
478 if (ext_type <= SADB_EXT_MAX) {
479 int min = (int) sadb_ext_min_len[ext_type];
480 if (ext_len < min)
481 return -EINVAL;
482 if (ext_hdrs[ext_type-1] != NULL)
483 return -EINVAL;
484 if (ext_type == SADB_EXT_ADDRESS_SRC ||
485 ext_type == SADB_EXT_ADDRESS_DST ||
486 ext_type == SADB_EXT_ADDRESS_PROXY ||
487 ext_type == SADB_X_EXT_NAT_T_OA) {
488 if (verify_address_len(p))
489 return -EINVAL;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900490 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800491 if (ext_type == SADB_X_EXT_SEC_CTX) {
492 if (verify_sec_ctx_len(p))
493 return -EINVAL;
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 ext_hdrs[ext_type-1] = p;
496 }
497 p += ext_len;
498 len -= ext_len;
499 }
500
501 return 0;
502}
503
504static uint16_t
505pfkey_satype2proto(uint8_t satype)
506{
507 switch (satype) {
508 case SADB_SATYPE_UNSPEC:
509 return IPSEC_PROTO_ANY;
510 case SADB_SATYPE_AH:
511 return IPPROTO_AH;
512 case SADB_SATYPE_ESP:
513 return IPPROTO_ESP;
514 case SADB_X_SATYPE_IPCOMP:
515 return IPPROTO_COMP;
516 break;
517 default:
518 return 0;
519 }
520 /* NOTREACHED */
521}
522
523static uint8_t
524pfkey_proto2satype(uint16_t proto)
525{
526 switch (proto) {
527 case IPPROTO_AH:
528 return SADB_SATYPE_AH;
529 case IPPROTO_ESP:
530 return SADB_SATYPE_ESP;
531 case IPPROTO_COMP:
532 return SADB_X_SATYPE_IPCOMP;
533 break;
534 default:
535 return 0;
536 }
537 /* NOTREACHED */
538}
539
540/* BTW, this scheme means that there is no way with PFKEY2 sockets to
541 * say specifically 'just raw sockets' as we encode them as 255.
542 */
543
544static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
545{
546 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
547}
548
549static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
550{
551 return (proto ? proto : IPSEC_PROTO_ANY);
552}
553
554static int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr,
555 xfrm_address_t *xaddr)
556{
557 switch (((struct sockaddr*)(addr + 1))->sa_family) {
558 case AF_INET:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900559 xaddr->a4 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 ((struct sockaddr_in *)(addr + 1))->sin_addr.s_addr;
561 return AF_INET;
562#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
563 case AF_INET6:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900564 memcpy(xaddr->a6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 &((struct sockaddr_in6 *)(addr + 1))->sin6_addr,
566 sizeof(struct in6_addr));
567 return AF_INET6;
568#endif
569 default:
570 return 0;
571 }
572 /* NOTREACHED */
573}
574
575static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
576{
577 struct sadb_sa *sa;
578 struct sadb_address *addr;
579 uint16_t proto;
580 unsigned short family;
581 xfrm_address_t *xaddr;
582
583 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
584 if (sa == NULL)
585 return NULL;
586
587 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
588 if (proto == 0)
589 return NULL;
590
591 /* sadb_address_len should be checked by caller */
592 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
593 if (addr == NULL)
594 return NULL;
595
596 family = ((struct sockaddr *)(addr + 1))->sa_family;
597 switch (family) {
598 case AF_INET:
599 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
600 break;
601#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
602 case AF_INET6:
603 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
604 break;
605#endif
606 default:
607 xaddr = NULL;
608 }
609
610 if (!xaddr)
611 return NULL;
612
613 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
614}
615
616#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
617static int
618pfkey_sockaddr_size(sa_family_t family)
619{
620 switch (family) {
621 case AF_INET:
622 return PFKEY_ALIGN8(sizeof(struct sockaddr_in));
623#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
624 case AF_INET6:
625 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6));
626#endif
627 default:
628 return 0;
629 }
630 /* NOTREACHED */
631}
632
633static struct sk_buff * pfkey_xfrm_state2msg(struct xfrm_state *x, int add_keys, int hsc)
634{
635 struct sk_buff *skb;
636 struct sadb_msg *hdr;
637 struct sadb_sa *sa;
638 struct sadb_lifetime *lifetime;
639 struct sadb_address *addr;
640 struct sadb_key *key;
641 struct sadb_x_sa2 *sa2;
642 struct sockaddr_in *sin;
Trent Jaegerdf718372005-12-13 23:12:27 -0800643 struct sadb_x_sec_ctx *sec_ctx;
644 struct xfrm_sec_ctx *xfrm_ctx;
645 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
647 struct sockaddr_in6 *sin6;
648#endif
649 int size;
650 int auth_key_size = 0;
651 int encrypt_key_size = 0;
652 int sockaddr_size;
653 struct xfrm_encap_tmpl *natt = NULL;
654
655 /* address family check */
656 sockaddr_size = pfkey_sockaddr_size(x->props.family);
657 if (!sockaddr_size)
658 return ERR_PTR(-EINVAL);
659
660 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
661 key(AE), (identity(SD),) (sensitivity)> */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900662 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 sizeof(struct sadb_lifetime) +
664 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
665 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900666 sizeof(struct sadb_address)*2 +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 sockaddr_size*2 +
668 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800669
670 if ((xfrm_ctx = x->security)) {
671 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
672 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
673 }
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /* identity & sensitivity */
676
677 if ((x->props.family == AF_INET &&
678 x->sel.saddr.a4 != x->props.saddr.a4)
679#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
680 || (x->props.family == AF_INET6 &&
681 memcmp (x->sel.saddr.a6, x->props.saddr.a6, sizeof (struct in6_addr)))
682#endif
683 )
684 size += sizeof(struct sadb_address) + sockaddr_size;
685
686 if (add_keys) {
687 if (x->aalg && x->aalg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900688 auth_key_size =
689 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 size += sizeof(struct sadb_key) + auth_key_size;
691 }
692 if (x->ealg && x->ealg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900693 encrypt_key_size =
694 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 size += sizeof(struct sadb_key) + encrypt_key_size;
696 }
697 }
698 if (x->encap)
699 natt = x->encap;
700
701 if (natt && natt->encap_type) {
702 size += sizeof(struct sadb_x_nat_t_type);
703 size += sizeof(struct sadb_x_nat_t_port);
704 size += sizeof(struct sadb_x_nat_t_port);
705 }
706
707 skb = alloc_skb(size + 16, GFP_ATOMIC);
708 if (skb == NULL)
709 return ERR_PTR(-ENOBUFS);
710
711 /* call should fill header later */
712 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
713 memset(hdr, 0, size); /* XXX do we need this ? */
714 hdr->sadb_msg_len = size / sizeof(uint64_t);
715
716 /* sa */
717 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
718 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
719 sa->sadb_sa_exttype = SADB_EXT_SA;
720 sa->sadb_sa_spi = x->id.spi;
721 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700722 switch (x->km.state) {
723 case XFRM_STATE_VALID:
724 sa->sadb_sa_state = x->km.dying ?
725 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
726 break;
727 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700729 break;
730 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700732 break;
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 sa->sadb_sa_auth = 0;
735 if (x->aalg) {
736 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
737 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
738 }
739 sa->sadb_sa_encrypt = 0;
740 BUG_ON(x->ealg && x->calg);
741 if (x->ealg) {
742 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
743 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
744 }
745 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
746 if (x->calg) {
747 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
748 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
749 }
750
751 sa->sadb_sa_flags = 0;
752 if (x->props.flags & XFRM_STATE_NOECN)
753 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
754 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
755 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700756 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
757 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /* hard time */
760 if (hsc & 2) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900761 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 sizeof(struct sadb_lifetime));
763 lifetime->sadb_lifetime_len =
764 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
765 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
766 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
767 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
768 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
769 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
770 }
771 /* soft time */
772 if (hsc & 1) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900773 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 sizeof(struct sadb_lifetime));
775 lifetime->sadb_lifetime_len =
776 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
777 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
778 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
779 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
780 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
781 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
782 }
783 /* current time */
784 lifetime = (struct sadb_lifetime *) skb_put(skb,
785 sizeof(struct sadb_lifetime));
786 lifetime->sadb_lifetime_len =
787 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
788 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
789 lifetime->sadb_lifetime_allocations = x->curlft.packets;
790 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
791 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
792 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
793 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900794 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900796 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 (sizeof(struct sadb_address)+sockaddr_size)/
798 sizeof(uint64_t);
799 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900800 /* "if the ports are non-zero, then the sadb_address_proto field,
801 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900803 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 addr->sadb_address_reserved = 0;
805 if (x->props.family == AF_INET) {
806 addr->sadb_address_prefixlen = 32;
807
808 sin = (struct sockaddr_in *) (addr + 1);
809 sin->sin_family = AF_INET;
810 sin->sin_addr.s_addr = x->props.saddr.a4;
811 sin->sin_port = 0;
812 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
813 }
814#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
815 else if (x->props.family == AF_INET6) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900816 addr->sadb_address_prefixlen = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 sin6 = (struct sockaddr_in6 *) (addr + 1);
819 sin6->sin6_family = AF_INET6;
820 sin6->sin6_port = 0;
821 sin6->sin6_flowinfo = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900822 memcpy(&sin6->sin6_addr, x->props.saddr.a6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 sizeof(struct in6_addr));
824 sin6->sin6_scope_id = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826#endif
827 else
828 BUG();
829
830 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900831 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900833 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 (sizeof(struct sadb_address)+sockaddr_size)/
835 sizeof(uint64_t);
836 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900837 addr->sadb_address_proto = 0;
838 addr->sadb_address_prefixlen = 32; /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 addr->sadb_address_reserved = 0;
840 if (x->props.family == AF_INET) {
841 sin = (struct sockaddr_in *) (addr + 1);
842 sin->sin_family = AF_INET;
843 sin->sin_addr.s_addr = x->id.daddr.a4;
844 sin->sin_port = 0;
845 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
846
847 if (x->sel.saddr.a4 != x->props.saddr.a4) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900848 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900850 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 (sizeof(struct sadb_address)+sockaddr_size)/
852 sizeof(uint64_t);
853 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
854 addr->sadb_address_proto =
855 pfkey_proto_from_xfrm(x->sel.proto);
856 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
857 addr->sadb_address_reserved = 0;
858
859 sin = (struct sockaddr_in *) (addr + 1);
860 sin->sin_family = AF_INET;
861 sin->sin_addr.s_addr = x->sel.saddr.a4;
862 sin->sin_port = x->sel.sport;
863 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
864 }
865 }
866#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
867 else if (x->props.family == AF_INET6) {
868 addr->sadb_address_prefixlen = 128;
869
870 sin6 = (struct sockaddr_in6 *) (addr + 1);
871 sin6->sin6_family = AF_INET6;
872 sin6->sin6_port = 0;
873 sin6->sin6_flowinfo = 0;
874 memcpy(&sin6->sin6_addr, x->id.daddr.a6, sizeof(struct in6_addr));
875 sin6->sin6_scope_id = 0;
876
877 if (memcmp (x->sel.saddr.a6, x->props.saddr.a6,
878 sizeof(struct in6_addr))) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900879 addr = (struct sadb_address *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900881 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 (sizeof(struct sadb_address)+sockaddr_size)/
883 sizeof(uint64_t);
884 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
885 addr->sadb_address_proto =
886 pfkey_proto_from_xfrm(x->sel.proto);
887 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
888 addr->sadb_address_reserved = 0;
889
890 sin6 = (struct sockaddr_in6 *) (addr + 1);
891 sin6->sin6_family = AF_INET6;
892 sin6->sin6_port = x->sel.sport;
893 sin6->sin6_flowinfo = 0;
894 memcpy(&sin6->sin6_addr, x->sel.saddr.a6,
895 sizeof(struct in6_addr));
896 sin6->sin6_scope_id = 0;
897 }
898 }
899#endif
900 else
901 BUG();
902
903 /* auth key */
904 if (add_keys && auth_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900905 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 sizeof(struct sadb_key)+auth_key_size);
907 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
908 sizeof(uint64_t);
909 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
910 key->sadb_key_bits = x->aalg->alg_key_len;
911 key->sadb_key_reserved = 0;
912 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
913 }
914 /* encrypt key */
915 if (add_keys && encrypt_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900916 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 sizeof(struct sadb_key)+encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900918 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 encrypt_key_size) / sizeof(uint64_t);
920 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
921 key->sadb_key_bits = x->ealg->alg_key_len;
922 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900923 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 (x->ealg->alg_key_len+7)/8);
925 }
926
927 /* sa */
928 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
929 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
930 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
931 sa2->sadb_x_sa2_mode = x->props.mode + 1;
932 sa2->sadb_x_sa2_reserved1 = 0;
933 sa2->sadb_x_sa2_reserved2 = 0;
934 sa2->sadb_x_sa2_sequence = 0;
935 sa2->sadb_x_sa2_reqid = x->props.reqid;
936
937 if (natt && natt->encap_type) {
938 struct sadb_x_nat_t_type *n_type;
939 struct sadb_x_nat_t_port *n_port;
940
941 /* type */
942 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
943 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
944 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
945 n_type->sadb_x_nat_t_type_type = natt->encap_type;
946 n_type->sadb_x_nat_t_type_reserved[0] = 0;
947 n_type->sadb_x_nat_t_type_reserved[1] = 0;
948 n_type->sadb_x_nat_t_type_reserved[2] = 0;
949
950 /* source port */
951 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
952 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
953 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
954 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
955 n_port->sadb_x_nat_t_port_reserved = 0;
956
957 /* dest port */
958 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
959 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
960 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
961 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
962 n_port->sadb_x_nat_t_port_reserved = 0;
963 }
964
Trent Jaegerdf718372005-12-13 23:12:27 -0800965 /* security context */
966 if (xfrm_ctx) {
967 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
968 sizeof(struct sadb_x_sec_ctx) + ctx_size);
969 sec_ctx->sadb_x_sec_len =
970 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
971 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
972 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
973 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
974 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
975 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
976 xfrm_ctx->ctx_len);
977 }
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return skb;
980}
981
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900982static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 void **ext_hdrs)
984{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900985 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 struct sadb_lifetime *lifetime;
987 struct sadb_sa *sa;
988 struct sadb_key *key;
Trent Jaegerdf718372005-12-13 23:12:27 -0800989 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 uint16_t proto;
991 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
995 if (!sa ||
996 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
997 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
998 return ERR_PTR(-EINVAL);
999 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1000 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1001 return ERR_PTR(-EINVAL);
1002 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1003 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1004 return ERR_PTR(-EINVAL);
1005 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1006 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1007 return ERR_PTR(-EINVAL);
1008
1009 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1010 if (proto == 0)
1011 return ERR_PTR(-EINVAL);
1012
1013 /* default error is no buffer space */
1014 err = -ENOBUFS;
1015
1016 /* RFC2367:
1017
1018 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1019 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1020 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1021 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1022 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1023 not true.
1024
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001025 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1027 */
1028 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1029 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1030 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1031 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1032 return ERR_PTR(-EINVAL);
1033 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1034 if (key != NULL &&
1035 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1036 ((key->sadb_key_bits+7) / 8 == 0 ||
1037 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1038 return ERR_PTR(-EINVAL);
1039 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1040 if (key != NULL &&
1041 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1042 ((key->sadb_key_bits+7) / 8 == 0 ||
1043 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1044 return ERR_PTR(-EINVAL);
1045
1046 x = xfrm_state_alloc();
1047 if (x == NULL)
1048 return ERR_PTR(-ENOBUFS);
1049
1050 x->id.proto = proto;
1051 x->id.spi = sa->sadb_sa_spi;
1052 x->props.replay_window = sa->sadb_sa_replay;
1053 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1054 x->props.flags |= XFRM_STATE_NOECN;
1055 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1056 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001057 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1058 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
1061 if (lifetime != NULL) {
1062 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1063 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1064 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1065 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1066 }
1067 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
1068 if (lifetime != NULL) {
1069 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1070 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1071 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1072 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1073 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001074
1075 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
1076 if (sec_ctx != NULL) {
1077 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1078
1079 if (!uctx)
1080 goto out;
1081
1082 err = security_xfrm_state_alloc(x, uctx);
1083 kfree(uctx);
1084
1085 if (err)
1086 goto out;
1087 }
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1090 if (sa->sadb_sa_auth) {
1091 int keysize = 0;
1092 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1093 if (!a) {
1094 err = -ENOSYS;
1095 goto out;
1096 }
1097 if (key)
1098 keysize = (key->sadb_key_bits + 7) / 8;
1099 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1100 if (!x->aalg)
1101 goto out;
1102 strcpy(x->aalg->alg_name, a->name);
1103 x->aalg->alg_key_len = 0;
1104 if (key) {
1105 x->aalg->alg_key_len = key->sadb_key_bits;
1106 memcpy(x->aalg->alg_key, key+1, keysize);
1107 }
1108 x->props.aalgo = sa->sadb_sa_auth;
1109 /* x->algo.flags = sa->sadb_sa_flags; */
1110 }
1111 if (sa->sadb_sa_encrypt) {
1112 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1113 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1114 if (!a) {
1115 err = -ENOSYS;
1116 goto out;
1117 }
1118 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1119 if (!x->calg)
1120 goto out;
1121 strcpy(x->calg->alg_name, a->name);
1122 x->props.calgo = sa->sadb_sa_encrypt;
1123 } else {
1124 int keysize = 0;
1125 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1126 if (!a) {
1127 err = -ENOSYS;
1128 goto out;
1129 }
1130 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1131 if (key)
1132 keysize = (key->sadb_key_bits + 7) / 8;
1133 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1134 if (!x->ealg)
1135 goto out;
1136 strcpy(x->ealg->alg_name, a->name);
1137 x->ealg->alg_key_len = 0;
1138 if (key) {
1139 x->ealg->alg_key_len = key->sadb_key_bits;
1140 memcpy(x->ealg->alg_key, key+1, keysize);
1141 }
1142 x->props.ealgo = sa->sadb_sa_encrypt;
1143 }
1144 }
1145 /* x->algo.flags = sa->sadb_sa_flags; */
1146
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001147 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 &x->props.saddr);
1149 if (!x->props.family) {
1150 err = -EAFNOSUPPORT;
1151 goto out;
1152 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001153 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 &x->id.daddr);
1155
1156 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1157 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
1158 x->props.mode = sa2->sadb_x_sa2_mode;
1159 if (x->props.mode)
1160 x->props.mode--;
1161 x->props.reqid = sa2->sadb_x_sa2_reqid;
1162 }
1163
1164 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1165 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1166
1167 /* Nobody uses this, but we try. */
1168 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1169 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1170 }
1171
1172 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1173 struct sadb_x_nat_t_type* n_type;
1174 struct xfrm_encap_tmpl *natt;
1175
1176 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1177 if (!x->encap)
1178 goto out;
1179
1180 natt = x->encap;
1181 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1182 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1183
1184 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1185 struct sadb_x_nat_t_port* n_port =
1186 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1187 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1188 }
1189 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1190 struct sadb_x_nat_t_port* n_port =
1191 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1192 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1193 }
1194 }
1195
Herbert Xu72cb6962005-06-20 13:18:08 -07001196 err = xfrm_init_state(x);
1197 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return x;
1202
1203out:
1204 x->km.state = XFRM_STATE_DEAD;
1205 xfrm_state_put(x);
1206 return ERR_PTR(err);
1207}
1208
1209static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1210{
1211 return -EOPNOTSUPP;
1212}
1213
1214static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1215{
1216 struct sk_buff *resp_skb;
1217 struct sadb_x_sa2 *sa2;
1218 struct sadb_address *saddr, *daddr;
1219 struct sadb_msg *out_hdr;
1220 struct xfrm_state *x = NULL;
1221 u8 mode;
1222 u32 reqid;
1223 u8 proto;
1224 unsigned short family;
1225 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1226
1227 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1228 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1229 return -EINVAL;
1230
1231 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1232 if (proto == 0)
1233 return -EINVAL;
1234
1235 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1236 mode = sa2->sadb_x_sa2_mode - 1;
1237 reqid = sa2->sadb_x_sa2_reqid;
1238 } else {
1239 mode = 0;
1240 reqid = 0;
1241 }
1242
1243 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1244 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1245
1246 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1247 switch (family) {
1248 case AF_INET:
1249 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1250 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1251 break;
1252#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1253 case AF_INET6:
1254 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1255 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1256 break;
1257#endif
1258 }
1259
1260 if (hdr->sadb_msg_seq) {
1261 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1262 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1263 xfrm_state_put(x);
1264 x = NULL;
1265 }
1266 }
1267
1268 if (!x)
1269 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1270
1271 if (x == NULL)
1272 return -ENOENT;
1273
1274 resp_skb = ERR_PTR(-ENOENT);
1275
1276 spin_lock_bh(&x->lock);
1277 if (x->km.state != XFRM_STATE_DEAD) {
1278 struct sadb_spirange *range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1279 u32 min_spi, max_spi;
1280
1281 if (range != NULL) {
1282 min_spi = range->sadb_spirange_min;
1283 max_spi = range->sadb_spirange_max;
1284 } else {
1285 min_spi = 0x100;
1286 max_spi = 0x0fffffff;
1287 }
1288 xfrm_alloc_spi(x, htonl(min_spi), htonl(max_spi));
1289 if (x->id.spi)
1290 resp_skb = pfkey_xfrm_state2msg(x, 0, 3);
1291 }
1292 spin_unlock_bh(&x->lock);
1293
1294 if (IS_ERR(resp_skb)) {
1295 xfrm_state_put(x);
1296 return PTR_ERR(resp_skb);
1297 }
1298
1299 out_hdr = (struct sadb_msg *) resp_skb->data;
1300 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1301 out_hdr->sadb_msg_type = SADB_GETSPI;
1302 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1303 out_hdr->sadb_msg_errno = 0;
1304 out_hdr->sadb_msg_reserved = 0;
1305 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1306 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1307
1308 xfrm_state_put(x);
1309
1310 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1311
1312 return 0;
1313}
1314
1315static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1316{
1317 struct xfrm_state *x;
1318
1319 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1320 return -EOPNOTSUPP;
1321
1322 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1323 return 0;
1324
1325 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1326 if (x == NULL)
1327 return 0;
1328
1329 spin_lock_bh(&x->lock);
1330 if (x->km.state == XFRM_STATE_ACQ) {
1331 x->km.state = XFRM_STATE_ERROR;
1332 wake_up(&km_waitq);
1333 }
1334 spin_unlock_bh(&x->lock);
1335 xfrm_state_put(x);
1336 return 0;
1337}
1338
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001339static inline int event2poltype(int event)
1340{
1341 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001342 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001343 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001344 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001345 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001346 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001347 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001348 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001349 // return SADB_X_SPDEXPIRE;
1350 default:
1351 printk("pfkey: Unknown policy event %d\n", event);
1352 break;
1353 }
1354
1355 return 0;
1356}
1357
1358static inline int event2keytype(int event)
1359{
1360 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001361 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001362 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001363 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001364 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001365 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001366 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001367 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001368 return SADB_EXPIRE;
1369 default:
1370 printk("pfkey: Unknown SA event %d\n", event);
1371 break;
1372 }
1373
1374 return 0;
1375}
1376
1377/* ADD/UPD/DEL */
1378static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1379{
1380 struct sk_buff *skb;
1381 struct sadb_msg *hdr;
1382 int hsc = 3;
1383
Herbert Xuf60f6b82005-06-18 22:44:37 -07001384 if (c->event == XFRM_MSG_DELSA)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001385 hsc = 0;
1386
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001387 skb = pfkey_xfrm_state2msg(x, 0, hsc);
1388
1389 if (IS_ERR(skb))
1390 return PTR_ERR(skb);
1391
1392 hdr = (struct sadb_msg *) skb->data;
1393 hdr->sadb_msg_version = PF_KEY_V2;
1394 hdr->sadb_msg_type = event2keytype(c->event);
1395 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1396 hdr->sadb_msg_errno = 0;
1397 hdr->sadb_msg_reserved = 0;
1398 hdr->sadb_msg_seq = c->seq;
1399 hdr->sadb_msg_pid = c->pid;
1400
1401 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1402
1403 return 0;
1404}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 struct xfrm_state *x;
1409 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001410 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1415 if (IS_ERR(x))
1416 return PTR_ERR(x);
1417
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001418 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (hdr->sadb_msg_type == SADB_ADD)
1420 err = xfrm_state_add(x);
1421 else
1422 err = xfrm_state_update(x);
1423
Joy Latten161a09e2006-11-27 13:11:54 -06001424 xfrm_audit_log(audit_get_loginuid(current->audit_context), 0,
1425 AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (err < 0) {
1428 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001429 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001430 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001433 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001434 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001435 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001436 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001437 c.seq = hdr->sadb_msg_seq;
1438 c.pid = hdr->sadb_msg_pid;
1439 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001440out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001441 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001442 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
1445static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1446{
1447 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001448 struct km_event c;
1449 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 if (!ext_hdrs[SADB_EXT_SA-1] ||
1452 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1453 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1454 return -EINVAL;
1455
1456 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1457 if (x == NULL)
1458 return -ESRCH;
1459
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001460 if ((err = security_xfrm_state_delete(x)))
1461 goto out;
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001464 err = -EPERM;
1465 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
Joy Latten161a09e2006-11-27 13:11:54 -06001467
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001468 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001469
1470 xfrm_audit_log(audit_get_loginuid(current->audit_context), 0,
1471 AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
1472
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001473 if (err < 0)
1474 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001475
1476 c.seq = hdr->sadb_msg_seq;
1477 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001478 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001479 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001480out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 xfrm_state_put(x);
1482
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001483 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
1486static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1487{
1488 __u8 proto;
1489 struct sk_buff *out_skb;
1490 struct sadb_msg *out_hdr;
1491 struct xfrm_state *x;
1492
1493 if (!ext_hdrs[SADB_EXT_SA-1] ||
1494 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1495 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1496 return -EINVAL;
1497
1498 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1499 if (x == NULL)
1500 return -ESRCH;
1501
1502 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1503 proto = x->id.proto;
1504 xfrm_state_put(x);
1505 if (IS_ERR(out_skb))
1506 return PTR_ERR(out_skb);
1507
1508 out_hdr = (struct sadb_msg *) out_skb->data;
1509 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1510 out_hdr->sadb_msg_type = SADB_DUMP;
1511 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1512 out_hdr->sadb_msg_errno = 0;
1513 out_hdr->sadb_msg_reserved = 0;
1514 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1515 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1516 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1517
1518 return 0;
1519}
1520
Randy Dunlap00fa0232005-10-04 22:43:04 -07001521static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001522 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 struct sk_buff *skb;
1525 struct sadb_msg *hdr;
1526 int len, auth_len, enc_len, i;
1527
1528 auth_len = xfrm_count_auth_supported();
1529 if (auth_len) {
1530 auth_len *= sizeof(struct sadb_alg);
1531 auth_len += sizeof(struct sadb_supported);
1532 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 enc_len = xfrm_count_enc_supported();
1535 if (enc_len) {
1536 enc_len *= sizeof(struct sadb_alg);
1537 enc_len += sizeof(struct sadb_supported);
1538 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 len = enc_len + auth_len + sizeof(struct sadb_msg);
1541
1542 skb = alloc_skb(len + 16, allocation);
1543 if (!skb)
1544 goto out_put_algs;
1545
1546 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1547 pfkey_hdr_dup(hdr, orig);
1548 hdr->sadb_msg_errno = 0;
1549 hdr->sadb_msg_len = len / sizeof(uint64_t);
1550
1551 if (auth_len) {
1552 struct sadb_supported *sp;
1553 struct sadb_alg *ap;
1554
1555 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1556 ap = (struct sadb_alg *) (sp + 1);
1557
1558 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1559 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1560
1561 for (i = 0; ; i++) {
1562 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1563 if (!aalg)
1564 break;
1565 if (aalg->available)
1566 *ap++ = aalg->desc;
1567 }
1568 }
1569
1570 if (enc_len) {
1571 struct sadb_supported *sp;
1572 struct sadb_alg *ap;
1573
1574 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1575 ap = (struct sadb_alg *) (sp + 1);
1576
1577 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1578 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1579
1580 for (i = 0; ; i++) {
1581 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1582 if (!ealg)
1583 break;
1584 if (ealg->available)
1585 *ap++ = ealg->desc;
1586 }
1587 }
1588
1589out_put_algs:
1590 return skb;
1591}
1592
1593static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1594{
1595 struct pfkey_sock *pfk = pfkey_sk(sk);
1596 struct sk_buff *supp_skb;
1597
1598 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1599 return -EINVAL;
1600
1601 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1602 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1603 return -EEXIST;
1604 pfk->registered |= (1<<hdr->sadb_msg_satype);
1605 }
1606
1607 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1610 if (!supp_skb) {
1611 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1612 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1613
1614 return -ENOBUFS;
1615 }
1616
1617 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1618
1619 return 0;
1620}
1621
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001622static int key_notify_sa_flush(struct km_event *c)
1623{
1624 struct sk_buff *skb;
1625 struct sadb_msg *hdr;
1626
1627 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1628 if (!skb)
1629 return -ENOBUFS;
1630 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf088672005-06-18 22:44:00 -07001631 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001632 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001633 hdr->sadb_msg_seq = c->seq;
1634 hdr->sadb_msg_pid = c->pid;
1635 hdr->sadb_msg_version = PF_KEY_V2;
1636 hdr->sadb_msg_errno = (uint8_t) 0;
1637 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1638
1639 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1640
1641 return 0;
1642}
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1645{
1646 unsigned proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001647 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06001648 struct xfrm_audit audit_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1651 if (proto == 0)
1652 return -EINVAL;
1653
Joy Latten161a09e2006-11-27 13:11:54 -06001654 audit_info.loginuid = audit_get_loginuid(current->audit_context);
1655 audit_info.secid = 0;
1656 xfrm_state_flush(proto, &audit_info);
Herbert Xubf088672005-06-18 22:44:00 -07001657 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001658 c.seq = hdr->sadb_msg_seq;
1659 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001660 c.event = XFRM_MSG_FLUSHSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001661 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 return 0;
1664}
1665
1666struct pfkey_dump_data
1667{
1668 struct sk_buff *skb;
1669 struct sadb_msg *hdr;
1670 struct sock *sk;
1671};
1672
1673static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1674{
1675 struct pfkey_dump_data *data = ptr;
1676 struct sk_buff *out_skb;
1677 struct sadb_msg *out_hdr;
1678
1679 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1680 if (IS_ERR(out_skb))
1681 return PTR_ERR(out_skb);
1682
1683 out_hdr = (struct sadb_msg *) out_skb->data;
1684 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
1685 out_hdr->sadb_msg_type = SADB_DUMP;
1686 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1687 out_hdr->sadb_msg_errno = 0;
1688 out_hdr->sadb_msg_reserved = 0;
1689 out_hdr->sadb_msg_seq = count;
1690 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
1691 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
1692 return 0;
1693}
1694
1695static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1696{
1697 u8 proto;
1698 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
1699
1700 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1701 if (proto == 0)
1702 return -EINVAL;
1703
1704 return xfrm_state_walk(proto, dump_sa, &data);
1705}
1706
1707static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1708{
1709 struct pfkey_sock *pfk = pfkey_sk(sk);
1710 int satype = hdr->sadb_msg_satype;
1711
1712 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1713 /* XXX we mangle packet... */
1714 hdr->sadb_msg_errno = 0;
1715 if (satype != 0 && satype != 1)
1716 return -EINVAL;
1717 pfk->promisc = satype;
1718 }
1719 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1720 return 0;
1721}
1722
1723static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1724{
1725 int i;
1726 u32 reqid = *(u32*)ptr;
1727
1728 for (i=0; i<xp->xfrm_nr; i++) {
1729 if (xp->xfrm_vec[i].reqid == reqid)
1730 return -EEXIST;
1731 }
1732 return 0;
1733}
1734
1735static u32 gen_reqid(void)
1736{
1737 u32 start;
1738 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1739
1740 start = reqid;
1741 do {
1742 ++reqid;
1743 if (reqid == 0)
1744 reqid = IPSEC_MANUAL_REQID_MAX+1;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001745 if (xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, check_reqid,
1746 (void*)&reqid) != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return reqid;
1748 } while (reqid != start);
1749 return 0;
1750}
1751
1752static int
1753parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1754{
1755 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1756 struct sockaddr_in *sin;
1757#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1758 struct sockaddr_in6 *sin6;
1759#endif
1760
1761 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1762 return -ELOOP;
1763
1764 if (rq->sadb_x_ipsecrequest_mode == 0)
1765 return -EINVAL;
1766
1767 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1768 t->mode = rq->sadb_x_ipsecrequest_mode-1;
1769 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1770 t->optional = 1;
1771 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1772 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1773 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1774 t->reqid = 0;
1775 if (!t->reqid && !(t->reqid = gen_reqid()))
1776 return -ENOBUFS;
1777 }
1778
1779 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001780 if (t->mode == XFRM_MODE_TUNNEL) {
Miika Komu2718aa72006-11-30 16:41:50 -08001781 struct sockaddr *sa;
1782 sa = (struct sockaddr *)(rq+1);
1783 switch(sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 case AF_INET:
Miika Komu2718aa72006-11-30 16:41:50 -08001785 sin = (struct sockaddr_in*)sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 t->saddr.a4 = sin->sin_addr.s_addr;
1787 sin++;
1788 if (sin->sin_family != AF_INET)
1789 return -EINVAL;
1790 t->id.daddr.a4 = sin->sin_addr.s_addr;
1791 break;
1792#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1793 case AF_INET6:
Miika Komu2718aa72006-11-30 16:41:50 -08001794 sin6 = (struct sockaddr_in6*)sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1796 sin6++;
1797 if (sin6->sin6_family != AF_INET6)
1798 return -EINVAL;
1799 memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1800 break;
1801#endif
1802 default:
1803 return -EINVAL;
1804 }
Miika Komu2718aa72006-11-30 16:41:50 -08001805 t->encap_family = sa->sa_family;
1806 } else
1807 t->encap_family = xp->family;
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /* No way to set this via kame pfkey */
1810 t->aalgos = t->ealgos = t->calgos = ~0;
1811 xp->xfrm_nr++;
1812 return 0;
1813}
1814
1815static int
1816parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1817{
1818 int err;
1819 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1820 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1821
1822 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1823 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1824 return err;
1825 len -= rq->sadb_x_ipsecrequest_len;
1826 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1827 }
1828 return 0;
1829}
1830
Trent Jaegerdf718372005-12-13 23:12:27 -08001831static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1832{
1833 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1834
1835 if (xfrm_ctx) {
1836 int len = sizeof(struct sadb_x_sec_ctx);
1837 len += xfrm_ctx->ctx_len;
1838 return PFKEY_ALIGN8(len);
1839 }
1840 return 0;
1841}
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1844{
Miika Komu2718aa72006-11-30 16:41:50 -08001845 struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08001847 int socklen = 0;
1848 int i;
1849
1850 for (i=0; i<xp->xfrm_nr; i++) {
1851 t = xp->xfrm_vec + i;
1852 socklen += (t->encap_family == AF_INET ?
1853 sizeof(struct sockaddr_in) :
1854 sizeof(struct sockaddr_in6));
1855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 return sizeof(struct sadb_msg) +
1858 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001859 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 (sockaddr_size * 2) +
1861 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08001862 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1863 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08001864 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
1867static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1868{
1869 struct sk_buff *skb;
1870 int size;
1871
1872 size = pfkey_xfrm_policy2msg_size(xp);
1873
1874 skb = alloc_skb(size + 16, GFP_ATOMIC);
1875 if (skb == NULL)
1876 return ERR_PTR(-ENOBUFS);
1877
1878 return skb;
1879}
1880
1881static void pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1882{
1883 struct sadb_msg *hdr;
1884 struct sadb_address *addr;
1885 struct sadb_lifetime *lifetime;
1886 struct sadb_x_policy *pol;
1887 struct sockaddr_in *sin;
Trent Jaegerdf718372005-12-13 23:12:27 -08001888 struct sadb_x_sec_ctx *sec_ctx;
1889 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1891 struct sockaddr_in6 *sin6;
1892#endif
1893 int i;
1894 int size;
1895 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1896 int socklen = (xp->family == AF_INET ?
1897 sizeof(struct sockaddr_in) :
1898 sizeof(struct sockaddr_in6));
1899
1900 size = pfkey_xfrm_policy2msg_size(xp);
1901
1902 /* call should fill header later */
1903 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1904 memset(hdr, 0, size); /* XXX do we need this ? */
1905
1906 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001907 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001909 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 (sizeof(struct sadb_address)+sockaddr_size)/
1911 sizeof(uint64_t);
1912 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1913 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1914 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1915 addr->sadb_address_reserved = 0;
1916 /* src address */
1917 if (xp->family == AF_INET) {
1918 sin = (struct sockaddr_in *) (addr + 1);
1919 sin->sin_family = AF_INET;
1920 sin->sin_addr.s_addr = xp->selector.saddr.a4;
1921 sin->sin_port = xp->selector.sport;
1922 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1923 }
1924#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1925 else if (xp->family == AF_INET6) {
1926 sin6 = (struct sockaddr_in6 *) (addr + 1);
1927 sin6->sin6_family = AF_INET6;
1928 sin6->sin6_port = xp->selector.sport;
1929 sin6->sin6_flowinfo = 0;
1930 memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
1931 sizeof(struct in6_addr));
1932 sin6->sin6_scope_id = 0;
1933 }
1934#endif
1935 else
1936 BUG();
1937
1938 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001939 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 sizeof(struct sadb_address)+sockaddr_size);
1941 addr->sadb_address_len =
1942 (sizeof(struct sadb_address)+sockaddr_size)/
1943 sizeof(uint64_t);
1944 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1945 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001946 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 addr->sadb_address_reserved = 0;
1948 if (xp->family == AF_INET) {
1949 sin = (struct sockaddr_in *) (addr + 1);
1950 sin->sin_family = AF_INET;
1951 sin->sin_addr.s_addr = xp->selector.daddr.a4;
1952 sin->sin_port = xp->selector.dport;
1953 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1954 }
1955#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1956 else if (xp->family == AF_INET6) {
1957 sin6 = (struct sockaddr_in6 *) (addr + 1);
1958 sin6->sin6_family = AF_INET6;
1959 sin6->sin6_port = xp->selector.dport;
1960 sin6->sin6_flowinfo = 0;
1961 memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
1962 sizeof(struct in6_addr));
1963 sin6->sin6_scope_id = 0;
1964 }
1965#endif
1966 else
1967 BUG();
1968
1969 /* hard time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001970 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 sizeof(struct sadb_lifetime));
1972 lifetime->sadb_lifetime_len =
1973 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1974 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1975 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
1976 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
1977 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
1978 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
1979 /* soft time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001980 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 sizeof(struct sadb_lifetime));
1982 lifetime->sadb_lifetime_len =
1983 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1984 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
1985 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
1986 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
1987 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
1988 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
1989 /* current time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001990 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 sizeof(struct sadb_lifetime));
1992 lifetime->sadb_lifetime_len =
1993 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1994 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
1995 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
1996 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
1997 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
1998 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
1999
2000 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2001 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2002 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2003 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2004 if (xp->action == XFRM_POLICY_ALLOW) {
2005 if (xp->xfrm_nr)
2006 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2007 else
2008 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2009 }
2010 pol->sadb_x_policy_dir = dir+1;
2011 pol->sadb_x_policy_id = xp->index;
2012 pol->sadb_x_policy_priority = xp->priority;
2013
2014 for (i=0; i<xp->xfrm_nr; i++) {
2015 struct sadb_x_ipsecrequest *rq;
2016 struct xfrm_tmpl *t = xp->xfrm_vec + i;
2017 int req_size;
2018
2019 req_size = sizeof(struct sadb_x_ipsecrequest);
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002020 if (t->mode == XFRM_MODE_TUNNEL)
Miika Komu2718aa72006-11-30 16:41:50 -08002021 req_size += ((t->encap_family == AF_INET ?
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002022 sizeof(struct sockaddr_in) :
2023 sizeof(struct sockaddr_in6)) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 else
2025 size -= 2*socklen;
2026 rq = (void*)skb_put(skb, req_size);
2027 pol->sadb_x_policy_len += req_size/8;
2028 memset(rq, 0, sizeof(*rq));
2029 rq->sadb_x_ipsecrequest_len = req_size;
2030 rq->sadb_x_ipsecrequest_proto = t->id.proto;
2031 rq->sadb_x_ipsecrequest_mode = t->mode+1;
2032 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2033 if (t->reqid)
2034 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2035 if (t->optional)
2036 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2037 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002038 if (t->mode == XFRM_MODE_TUNNEL) {
Miika Komu2718aa72006-11-30 16:41:50 -08002039 switch (t->encap_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 case AF_INET:
2041 sin = (void*)(rq+1);
2042 sin->sin_family = AF_INET;
2043 sin->sin_addr.s_addr = t->saddr.a4;
2044 sin->sin_port = 0;
2045 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2046 sin++;
2047 sin->sin_family = AF_INET;
2048 sin->sin_addr.s_addr = t->id.daddr.a4;
2049 sin->sin_port = 0;
2050 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2051 break;
2052#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2053 case AF_INET6:
2054 sin6 = (void*)(rq+1);
2055 sin6->sin6_family = AF_INET6;
2056 sin6->sin6_port = 0;
2057 sin6->sin6_flowinfo = 0;
2058 memcpy(&sin6->sin6_addr, t->saddr.a6,
2059 sizeof(struct in6_addr));
2060 sin6->sin6_scope_id = 0;
2061
2062 sin6++;
2063 sin6->sin6_family = AF_INET6;
2064 sin6->sin6_port = 0;
2065 sin6->sin6_flowinfo = 0;
2066 memcpy(&sin6->sin6_addr, t->id.daddr.a6,
2067 sizeof(struct in6_addr));
2068 sin6->sin6_scope_id = 0;
2069 break;
2070#endif
2071 default:
2072 break;
2073 }
2074 }
2075 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002076
2077 /* security context */
2078 if ((xfrm_ctx = xp->security)) {
2079 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2080
2081 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2082 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2083 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2084 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2085 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2086 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2087 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2088 xfrm_ctx->ctx_len);
2089 }
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 hdr->sadb_msg_len = size / sizeof(uint64_t);
2092 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
2093}
2094
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002095static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2096{
2097 struct sk_buff *out_skb;
2098 struct sadb_msg *out_hdr;
2099 int err;
2100
2101 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2102 if (IS_ERR(out_skb)) {
2103 err = PTR_ERR(out_skb);
2104 goto out;
2105 }
2106 pfkey_xfrm_policy2msg(out_skb, xp, dir);
2107
2108 out_hdr = (struct sadb_msg *) out_skb->data;
2109 out_hdr->sadb_msg_version = PF_KEY_V2;
2110
Herbert Xuf60f6b82005-06-18 22:44:37 -07002111 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002112 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2113 else
2114 out_hdr->sadb_msg_type = event2poltype(c->event);
2115 out_hdr->sadb_msg_errno = 0;
2116 out_hdr->sadb_msg_seq = c->seq;
2117 out_hdr->sadb_msg_pid = c->pid;
2118 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
2119out:
2120 return 0;
2121
2122}
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2125{
Trent Jaegerdf718372005-12-13 23:12:27 -08002126 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 struct sadb_lifetime *lifetime;
2128 struct sadb_address *sa;
2129 struct sadb_x_policy *pol;
2130 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002131 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002132 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2135 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2136 !ext_hdrs[SADB_X_EXT_POLICY-1])
2137 return -EINVAL;
2138
2139 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2140 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2141 return -EINVAL;
2142 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2143 return -EINVAL;
2144
2145 xp = xfrm_policy_alloc(GFP_KERNEL);
2146 if (xp == NULL)
2147 return -ENOBUFS;
2148
2149 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2150 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2151 xp->priority = pol->sadb_x_policy_priority;
2152
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002153 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2155 if (!xp->family) {
2156 err = -EINVAL;
2157 goto out;
2158 }
2159 xp->selector.family = xp->family;
2160 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2161 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2162 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2163 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002164 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002166 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2168 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2169
2170 /* Amusing, we set this twice. KAME apps appear to set same value
2171 * in both addresses.
2172 */
2173 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2174
2175 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2176 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002177 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Trent Jaegerdf718372005-12-13 23:12:27 -08002179 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2180 if (sec_ctx != NULL) {
2181 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2182
2183 if (!uctx) {
2184 err = -ENOBUFS;
2185 goto out;
2186 }
2187
2188 err = security_xfrm_policy_alloc(xp, uctx);
2189 kfree(uctx);
2190
2191 if (err)
2192 goto out;
2193 }
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 xp->lft.soft_byte_limit = XFRM_INF;
2196 xp->lft.hard_byte_limit = XFRM_INF;
2197 xp->lft.soft_packet_limit = XFRM_INF;
2198 xp->lft.hard_packet_limit = XFRM_INF;
2199 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2200 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2201 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2202 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2203 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2204 }
2205 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2206 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2207 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2208 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2209 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2210 }
2211 xp->xfrm_nr = 0;
2212 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2213 (err = parse_ipsecrequests(xp, pol)) < 0)
2214 goto out;
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2217 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002218
Joy Latten161a09e2006-11-27 13:11:54 -06002219 xfrm_audit_log(audit_get_loginuid(current->audit_context), 0,
2220 AUDIT_MAC_IPSEC_ADDSPD, err ? 0 : 1, xp, NULL);
2221
Trent Jaegerdf718372005-12-13 23:12:27 -08002222 if (err)
2223 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002225 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002226 c.event = XFRM_MSG_UPDPOLICY;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002227 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07002228 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002230 c.seq = hdr->sadb_msg_seq;
2231 c.pid = hdr->sadb_msg_pid;
2232
2233 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return 0;
2236
2237out:
Trent Jaegerdf718372005-12-13 23:12:27 -08002238 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 kfree(xp);
2240 return err;
2241}
2242
2243static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2244{
2245 int err;
2246 struct sadb_address *sa;
2247 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08002248 struct xfrm_policy *xp, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002250 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002251 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2254 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2255 !ext_hdrs[SADB_X_EXT_POLICY-1])
2256 return -EINVAL;
2257
2258 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2259 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2260 return -EINVAL;
2261
2262 memset(&sel, 0, sizeof(sel));
2263
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002264 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2266 sel.prefixlen_s = sa->sadb_address_prefixlen;
2267 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2268 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2269 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002270 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002272 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2274 sel.prefixlen_d = sa->sadb_address_prefixlen;
2275 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2276 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2277 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002278 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Trent Jaegerdf718372005-12-13 23:12:27 -08002280 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2281 memset(&tmp, 0, sizeof(struct xfrm_policy));
2282
2283 if (sec_ctx != NULL) {
2284 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2285
2286 if (!uctx)
2287 return -ENOMEM;
2288
2289 err = security_xfrm_policy_alloc(&tmp, uctx);
2290 kfree(uctx);
2291
2292 if (err)
2293 return err;
2294 }
2295
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002296 xp = xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN, pol->sadb_x_policy_dir-1,
Eric Parisef41aaa2007-03-07 15:37:58 -08002297 &sel, tmp.security, 1, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08002298 security_xfrm_policy_free(&tmp);
Joy Latten161a09e2006-11-27 13:11:54 -06002299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (xp == NULL)
2301 return -ENOENT;
2302
David S. Miller13fcfbb2007-02-12 13:53:54 -08002303 xfrm_audit_log(audit_get_loginuid(current->audit_context), 0,
2304 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
2305
2306 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002307 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08002308
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002309 c.seq = hdr->sadb_msg_seq;
2310 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002311 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002312 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2313
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002314out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002315 xfrm_pol_put(xp);
2316 return err;
2317}
2318
2319static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2320{
2321 int err;
2322 struct sk_buff *out_skb;
2323 struct sadb_msg *out_hdr;
2324 err = 0;
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2327 if (IS_ERR(out_skb)) {
2328 err = PTR_ERR(out_skb);
2329 goto out;
2330 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002331 pfkey_xfrm_policy2msg(out_skb, xp, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 out_hdr = (struct sadb_msg *) out_skb->data;
2334 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002335 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 out_hdr->sadb_msg_satype = 0;
2337 out_hdr->sadb_msg_errno = 0;
2338 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2339 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002340 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 err = 0;
2342
2343out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return err;
2345}
2346
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002347#ifdef CONFIG_NET_KEY_MIGRATE
2348static int pfkey_sockaddr_pair_size(sa_family_t family)
2349{
2350 switch (family) {
2351 case AF_INET:
2352 return PFKEY_ALIGN8(sizeof(struct sockaddr_in) * 2);
2353#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2354 case AF_INET6:
2355 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6) * 2);
2356#endif
2357 default:
2358 return 0;
2359 }
2360 /* NOTREACHED */
2361}
2362
2363static int parse_sockaddr_pair(struct sadb_x_ipsecrequest *rq,
2364 xfrm_address_t *saddr, xfrm_address_t *daddr,
2365 u16 *family)
2366{
2367 struct sockaddr *sa = (struct sockaddr *)(rq + 1);
2368 if (rq->sadb_x_ipsecrequest_len <
2369 pfkey_sockaddr_pair_size(sa->sa_family))
2370 return -EINVAL;
2371
2372 switch (sa->sa_family) {
2373 case AF_INET:
2374 {
2375 struct sockaddr_in *sin;
2376 sin = (struct sockaddr_in *)sa;
2377 if ((sin+1)->sin_family != AF_INET)
2378 return -EINVAL;
2379 memcpy(&saddr->a4, &sin->sin_addr, sizeof(saddr->a4));
2380 sin++;
2381 memcpy(&daddr->a4, &sin->sin_addr, sizeof(daddr->a4));
2382 *family = AF_INET;
2383 break;
2384 }
2385#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2386 case AF_INET6:
2387 {
2388 struct sockaddr_in6 *sin6;
2389 sin6 = (struct sockaddr_in6 *)sa;
2390 if ((sin6+1)->sin6_family != AF_INET6)
2391 return -EINVAL;
2392 memcpy(&saddr->a6, &sin6->sin6_addr,
2393 sizeof(saddr->a6));
2394 sin6++;
2395 memcpy(&daddr->a6, &sin6->sin6_addr,
2396 sizeof(daddr->a6));
2397 *family = AF_INET6;
2398 break;
2399 }
2400#endif
2401 default:
2402 return -EINVAL;
2403 }
2404
2405 return 0;
2406}
2407
2408static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2409 struct xfrm_migrate *m)
2410{
2411 int err;
2412 struct sadb_x_ipsecrequest *rq2;
2413
2414 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2415 len < rq1->sadb_x_ipsecrequest_len)
2416 return -EINVAL;
2417
2418 /* old endoints */
2419 err = parse_sockaddr_pair(rq1, &m->old_saddr, &m->old_daddr,
2420 &m->old_family);
2421 if (err)
2422 return err;
2423
2424 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2425 len -= rq1->sadb_x_ipsecrequest_len;
2426
2427 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2428 len < rq2->sadb_x_ipsecrequest_len)
2429 return -EINVAL;
2430
2431 /* new endpoints */
2432 err = parse_sockaddr_pair(rq2, &m->new_saddr, &m->new_daddr,
2433 &m->new_family);
2434 if (err)
2435 return err;
2436
2437 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2438 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2439 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2440 return -EINVAL;
2441
2442 m->proto = rq1->sadb_x_ipsecrequest_proto;
2443 m->mode = rq1->sadb_x_ipsecrequest_mode - 1;
2444 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2445
2446 return ((int)(rq1->sadb_x_ipsecrequest_len +
2447 rq2->sadb_x_ipsecrequest_len));
2448}
2449
2450static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2451 struct sadb_msg *hdr, void **ext_hdrs)
2452{
2453 int i, len, ret, err = -EINVAL;
2454 u8 dir;
2455 struct sadb_address *sa;
2456 struct sadb_x_policy *pol;
2457 struct sadb_x_ipsecrequest *rq;
2458 struct xfrm_selector sel;
2459 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2460
2461 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2462 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2463 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2464 err = -EINVAL;
2465 goto out;
2466 }
2467
2468 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2469 if (!pol) {
2470 err = -EINVAL;
2471 goto out;
2472 }
2473
2474 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2475 err = -EINVAL;
2476 goto out;
2477 }
2478
2479 dir = pol->sadb_x_policy_dir - 1;
2480 memset(&sel, 0, sizeof(sel));
2481
2482 /* set source address info of selector */
2483 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2484 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2485 sel.prefixlen_s = sa->sadb_address_prefixlen;
2486 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2487 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2488 if (sel.sport)
2489 sel.sport_mask = ~0;
2490
2491 /* set destination address info of selector */
2492 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2493 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2494 sel.prefixlen_d = sa->sadb_address_prefixlen;
2495 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2496 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2497 if (sel.dport)
2498 sel.dport_mask = ~0;
2499
2500 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2501
2502 /* extract ipsecrequests */
2503 i = 0;
2504 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2505
2506 while (len > 0 && i < XFRM_MAX_DEPTH) {
2507 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2508 if (ret < 0) {
2509 err = ret;
2510 goto out;
2511 } else {
2512 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2513 len -= ret;
2514 i++;
2515 }
2516 }
2517
2518 if (!i || len > 0) {
2519 err = -EINVAL;
2520 goto out;
2521 }
2522
2523 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i);
2524
2525 out:
2526 return err;
2527}
2528#else
2529static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2530 struct sadb_msg *hdr, void **ext_hdrs)
2531{
2532 return -ENOPROTOOPT;
2533}
2534#endif
2535
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2538{
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002539 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002540 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 struct sadb_x_policy *pol;
2542 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002543 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
2545 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2546 return -EINVAL;
2547
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002548 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2549 if (dir >= XFRM_POLICY_MAX)
2550 return -EINVAL;
2551
Eric Paris215a2dd2007-03-07 16:01:45 -08002552 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002553 xp = xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN, dir, pol->sadb_x_policy_id,
Eric Paris215a2dd2007-03-07 16:01:45 -08002554 delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if (xp == NULL)
2556 return -ENOENT;
2557
Eric Paris215a2dd2007-03-07 16:01:45 -08002558 if (delete) {
2559 xfrm_audit_log(audit_get_loginuid(current->audit_context), 0,
2560 AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Eric Paris215a2dd2007-03-07 16:01:45 -08002562 if (err)
2563 goto out;
2564 c.seq = hdr->sadb_msg_seq;
2565 c.pid = hdr->sadb_msg_pid;
Herbert Xubf088672005-06-18 22:44:00 -07002566 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002567 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002568 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002569 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002570 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Eric Paris215a2dd2007-03-07 16:01:45 -08002573out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 xfrm_pol_put(xp);
2575 return err;
2576}
2577
2578static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2579{
2580 struct pfkey_dump_data *data = ptr;
2581 struct sk_buff *out_skb;
2582 struct sadb_msg *out_hdr;
2583
2584 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2585 if (IS_ERR(out_skb))
2586 return PTR_ERR(out_skb);
2587
2588 pfkey_xfrm_policy2msg(out_skb, xp, dir);
2589
2590 out_hdr = (struct sadb_msg *) out_skb->data;
2591 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
2592 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2593 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2594 out_hdr->sadb_msg_errno = 0;
2595 out_hdr->sadb_msg_seq = count;
2596 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
2597 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
2598 return 0;
2599}
2600
2601static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2602{
2603 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
2604
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002605 return xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_sp, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002608static int key_notify_policy_flush(struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
2610 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002611 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002613 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 if (!skb_out)
2615 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002616 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002617 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002618 hdr->sadb_msg_seq = c->seq;
2619 hdr->sadb_msg_pid = c->pid;
2620 hdr->sadb_msg_version = PF_KEY_V2;
2621 hdr->sadb_msg_errno = (uint8_t) 0;
2622 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2623 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2624 return 0;
2625
2626}
2627
2628static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2629{
2630 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06002631 struct xfrm_audit audit_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Joy Latten161a09e2006-11-27 13:11:54 -06002633 audit_info.loginuid = audit_get_loginuid(current->audit_context);
2634 audit_info.secid = 0;
2635 xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN, &audit_info);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002636 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002637 c.event = XFRM_MSG_FLUSHPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002638 c.pid = hdr->sadb_msg_pid;
2639 c.seq = hdr->sadb_msg_seq;
2640 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 return 0;
2643}
2644
2645typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2646 struct sadb_msg *hdr, void **ext_hdrs);
2647static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2648 [SADB_RESERVED] = pfkey_reserved,
2649 [SADB_GETSPI] = pfkey_getspi,
2650 [SADB_UPDATE] = pfkey_add,
2651 [SADB_ADD] = pfkey_add,
2652 [SADB_DELETE] = pfkey_delete,
2653 [SADB_GET] = pfkey_get,
2654 [SADB_ACQUIRE] = pfkey_acquire,
2655 [SADB_REGISTER] = pfkey_register,
2656 [SADB_EXPIRE] = NULL,
2657 [SADB_FLUSH] = pfkey_flush,
2658 [SADB_DUMP] = pfkey_dump,
2659 [SADB_X_PROMISC] = pfkey_promisc,
2660 [SADB_X_PCHANGE] = NULL,
2661 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2662 [SADB_X_SPDADD] = pfkey_spdadd,
2663 [SADB_X_SPDDELETE] = pfkey_spddelete,
2664 [SADB_X_SPDGET] = pfkey_spdget,
2665 [SADB_X_SPDACQUIRE] = NULL,
2666 [SADB_X_SPDDUMP] = pfkey_spddump,
2667 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2668 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2669 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002670 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671};
2672
2673static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2674{
2675 void *ext_hdrs[SADB_EXT_MAX];
2676 int err;
2677
2678 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2679 BROADCAST_PROMISC_ONLY, NULL);
2680
2681 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2682 err = parse_exthdrs(skb, hdr, ext_hdrs);
2683 if (!err) {
2684 err = -EOPNOTSUPP;
2685 if (pfkey_funcs[hdr->sadb_msg_type])
2686 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2687 }
2688 return err;
2689}
2690
2691static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2692{
2693 struct sadb_msg *hdr = NULL;
2694
2695 if (skb->len < sizeof(*hdr)) {
2696 *errp = -EMSGSIZE;
2697 } else {
2698 hdr = (struct sadb_msg *) skb->data;
2699 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2700 hdr->sadb_msg_reserved != 0 ||
2701 (hdr->sadb_msg_type <= SADB_RESERVED ||
2702 hdr->sadb_msg_type > SADB_MAX)) {
2703 hdr = NULL;
2704 *errp = -EINVAL;
2705 } else if (hdr->sadb_msg_len != (skb->len /
2706 sizeof(uint64_t)) ||
2707 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2708 sizeof(uint64_t))) {
2709 hdr = NULL;
2710 *errp = -EMSGSIZE;
2711 } else {
2712 *errp = 0;
2713 }
2714 }
2715 return hdr;
2716}
2717
2718static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2719{
2720 return t->aalgos & (1 << d->desc.sadb_alg_id);
2721}
2722
2723static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2724{
2725 return t->ealgos & (1 << d->desc.sadb_alg_id);
2726}
2727
2728static int count_ah_combs(struct xfrm_tmpl *t)
2729{
2730 int i, sz = 0;
2731
2732 for (i = 0; ; i++) {
2733 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2734 if (!aalg)
2735 break;
2736 if (aalg_tmpl_set(t, aalg) && aalg->available)
2737 sz += sizeof(struct sadb_comb);
2738 }
2739 return sz + sizeof(struct sadb_prop);
2740}
2741
2742static int count_esp_combs(struct xfrm_tmpl *t)
2743{
2744 int i, k, sz = 0;
2745
2746 for (i = 0; ; i++) {
2747 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2748 if (!ealg)
2749 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2752 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 for (k = 1; ; k++) {
2755 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2756 if (!aalg)
2757 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 if (aalg_tmpl_set(t, aalg) && aalg->available)
2760 sz += sizeof(struct sadb_comb);
2761 }
2762 }
2763 return sz + sizeof(struct sadb_prop);
2764}
2765
2766static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2767{
2768 struct sadb_prop *p;
2769 int i;
2770
2771 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2772 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2773 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2774 p->sadb_prop_replay = 32;
2775 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2776
2777 for (i = 0; ; i++) {
2778 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2779 if (!aalg)
2780 break;
2781
2782 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2783 struct sadb_comb *c;
2784 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2785 memset(c, 0, sizeof(*c));
2786 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2787 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2788 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2789 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2790 c->sadb_comb_hard_addtime = 24*60*60;
2791 c->sadb_comb_soft_addtime = 20*60*60;
2792 c->sadb_comb_hard_usetime = 8*60*60;
2793 c->sadb_comb_soft_usetime = 7*60*60;
2794 }
2795 }
2796}
2797
2798static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2799{
2800 struct sadb_prop *p;
2801 int i, k;
2802
2803 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2804 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2805 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2806 p->sadb_prop_replay = 32;
2807 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2808
2809 for (i=0; ; i++) {
2810 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2811 if (!ealg)
2812 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2815 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 for (k = 1; ; k++) {
2818 struct sadb_comb *c;
2819 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2820 if (!aalg)
2821 break;
2822 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2823 continue;
2824 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2825 memset(c, 0, sizeof(*c));
2826 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2827 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2828 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2829 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2830 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2831 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2832 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2833 c->sadb_comb_hard_addtime = 24*60*60;
2834 c->sadb_comb_soft_addtime = 20*60*60;
2835 c->sadb_comb_hard_usetime = 8*60*60;
2836 c->sadb_comb_soft_usetime = 7*60*60;
2837 }
2838 }
2839}
2840
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002841static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2842{
2843 return 0;
2844}
2845
2846static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 struct sk_buff *out_skb;
2849 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002850 int hard;
2851 int hsc;
2852
Herbert Xubf088672005-06-18 22:44:00 -07002853 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002854 if (hard)
2855 hsc = 2;
2856 else
2857 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 out_skb = pfkey_xfrm_state2msg(x, 0, hsc);
2860 if (IS_ERR(out_skb))
2861 return PTR_ERR(out_skb);
2862
2863 out_hdr = (struct sadb_msg *) out_skb->data;
2864 out_hdr->sadb_msg_version = PF_KEY_V2;
2865 out_hdr->sadb_msg_type = SADB_EXPIRE;
2866 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2867 out_hdr->sadb_msg_errno = 0;
2868 out_hdr->sadb_msg_reserved = 0;
2869 out_hdr->sadb_msg_seq = 0;
2870 out_hdr->sadb_msg_pid = 0;
2871
2872 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2873 return 0;
2874}
2875
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002876static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2877{
2878 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002879 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002880 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002881 case XFRM_MSG_DELSA:
2882 case XFRM_MSG_NEWSA:
2883 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002884 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002885 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002886 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002887 case XFRM_MSG_NEWAE: /* not yet supported */
2888 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002889 default:
2890 printk("pfkey: Unknown SA event %d\n", c->event);
2891 break;
2892 }
2893
2894 return 0;
2895}
2896
2897static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2898{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002899 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
2900 return 0;
2901
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002902 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002903 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002904 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002905 case XFRM_MSG_DELPOLICY:
2906 case XFRM_MSG_NEWPOLICY:
2907 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002908 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002909 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002910 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
2911 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002912 return key_notify_policy_flush(c);
2913 default:
2914 printk("pfkey: Unknown policy event %d\n", c->event);
2915 break;
2916 }
2917
2918 return 0;
2919}
2920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921static u32 get_acqseq(void)
2922{
2923 u32 res;
2924 static u32 acqseq;
2925 static DEFINE_SPINLOCK(acqseq_lock);
2926
2927 spin_lock_bh(&acqseq_lock);
2928 res = (++acqseq ? : ++acqseq);
2929 spin_unlock_bh(&acqseq_lock);
2930 return res;
2931}
2932
2933static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2934{
2935 struct sk_buff *skb;
2936 struct sadb_msg *hdr;
2937 struct sadb_address *addr;
2938 struct sadb_x_policy *pol;
2939 struct sockaddr_in *sin;
2940#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2941 struct sockaddr_in6 *sin6;
2942#endif
2943 int sockaddr_size;
2944 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07002945 struct sadb_x_sec_ctx *sec_ctx;
2946 struct xfrm_sec_ctx *xfrm_ctx;
2947 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2950 if (!sockaddr_size)
2951 return -EINVAL;
2952
2953 size = sizeof(struct sadb_msg) +
2954 (sizeof(struct sadb_address) * 2) +
2955 (sockaddr_size * 2) +
2956 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 if (x->id.proto == IPPROTO_AH)
2959 size += count_ah_combs(t);
2960 else if (x->id.proto == IPPROTO_ESP)
2961 size += count_esp_combs(t);
2962
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07002963 if ((xfrm_ctx = x->security)) {
2964 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
2965 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
2966 }
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 skb = alloc_skb(size + 16, GFP_ATOMIC);
2969 if (skb == NULL)
2970 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2973 hdr->sadb_msg_version = PF_KEY_V2;
2974 hdr->sadb_msg_type = SADB_ACQUIRE;
2975 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2976 hdr->sadb_msg_len = size / sizeof(uint64_t);
2977 hdr->sadb_msg_errno = 0;
2978 hdr->sadb_msg_reserved = 0;
2979 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2980 hdr->sadb_msg_pid = 0;
2981
2982 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002983 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002985 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 (sizeof(struct sadb_address)+sockaddr_size)/
2987 sizeof(uint64_t);
2988 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2989 addr->sadb_address_proto = 0;
2990 addr->sadb_address_reserved = 0;
2991 if (x->props.family == AF_INET) {
2992 addr->sadb_address_prefixlen = 32;
2993
2994 sin = (struct sockaddr_in *) (addr + 1);
2995 sin->sin_family = AF_INET;
2996 sin->sin_addr.s_addr = x->props.saddr.a4;
2997 sin->sin_port = 0;
2998 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2999 }
3000#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3001 else if (x->props.family == AF_INET6) {
3002 addr->sadb_address_prefixlen = 128;
3003
3004 sin6 = (struct sockaddr_in6 *) (addr + 1);
3005 sin6->sin6_family = AF_INET6;
3006 sin6->sin6_port = 0;
3007 sin6->sin6_flowinfo = 0;
3008 memcpy(&sin6->sin6_addr,
3009 x->props.saddr.a6, sizeof(struct in6_addr));
3010 sin6->sin6_scope_id = 0;
3011 }
3012#endif
3013 else
3014 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003017 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 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_DST;
3023 addr->sadb_address_proto = 0;
3024 addr->sadb_address_reserved = 0;
3025 if (x->props.family == AF_INET) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003026 addr->sadb_address_prefixlen = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028 sin = (struct sockaddr_in *) (addr + 1);
3029 sin->sin_family = AF_INET;
3030 sin->sin_addr.s_addr = x->id.daddr.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) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003036 addr->sadb_address_prefixlen = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
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->id.daddr.a6, sizeof(struct in6_addr));
3044 sin6->sin6_scope_id = 0;
3045 }
3046#endif
3047 else
3048 BUG();
3049
3050 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3051 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3052 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3053 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3054 pol->sadb_x_policy_dir = dir+1;
3055 pol->sadb_x_policy_id = xp->index;
3056
3057 /* Set sadb_comb's. */
3058 if (x->id.proto == IPPROTO_AH)
3059 dump_ah_combs(skb, t);
3060 else if (x->id.proto == IPPROTO_ESP)
3061 dump_esp_combs(skb, t);
3062
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003063 /* security context */
3064 if (xfrm_ctx) {
3065 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3066 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3067 sec_ctx->sadb_x_sec_len =
3068 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3069 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3070 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3071 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3072 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3073 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3074 xfrm_ctx->ctx_len);
3075 }
3076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3078}
3079
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003080static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003081 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
3083 struct xfrm_policy *xp;
3084 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003085 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003087 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 case AF_INET:
3089 if (opt != IP_IPSEC_POLICY) {
3090 *dir = -EOPNOTSUPP;
3091 return NULL;
3092 }
3093 break;
3094#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3095 case AF_INET6:
3096 if (opt != IPV6_IPSEC_POLICY) {
3097 *dir = -EOPNOTSUPP;
3098 return NULL;
3099 }
3100 break;
3101#endif
3102 default:
3103 *dir = -EINVAL;
3104 return NULL;
3105 }
3106
3107 *dir = -EINVAL;
3108
3109 if (len < sizeof(struct sadb_x_policy) ||
3110 pol->sadb_x_policy_len*8 > len ||
3111 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3112 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3113 return NULL;
3114
3115 xp = xfrm_policy_alloc(GFP_ATOMIC);
3116 if (xp == NULL) {
3117 *dir = -ENOBUFS;
3118 return NULL;
3119 }
3120
3121 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3122 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3123
3124 xp->lft.soft_byte_limit = XFRM_INF;
3125 xp->lft.hard_byte_limit = XFRM_INF;
3126 xp->lft.soft_packet_limit = XFRM_INF;
3127 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003128 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130 xp->xfrm_nr = 0;
3131 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3132 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3133 goto out;
3134
Trent Jaegerdf718372005-12-13 23:12:27 -08003135 /* security context too */
3136 if (len >= (pol->sadb_x_policy_len*8 +
3137 sizeof(struct sadb_x_sec_ctx))) {
3138 char *p = (char *)pol;
3139 struct xfrm_user_sec_ctx *uctx;
3140
3141 p += pol->sadb_x_policy_len*8;
3142 sec_ctx = (struct sadb_x_sec_ctx *)p;
3143 if (len < pol->sadb_x_policy_len*8 +
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003144 sec_ctx->sadb_x_sec_len) {
3145 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003146 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003147 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003148 if ((*dir = verify_sec_ctx_len(p)))
3149 goto out;
3150 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
3151 *dir = security_xfrm_policy_alloc(xp, uctx);
3152 kfree(uctx);
3153
3154 if (*dir)
3155 goto out;
3156 }
3157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 *dir = pol->sadb_x_policy_dir-1;
3159 return xp;
3160
3161out:
Trent Jaegerdf718372005-12-13 23:12:27 -08003162 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 kfree(xp);
3164 return NULL;
3165}
3166
Al Viro5d36b182006-11-08 00:24:06 -08003167static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
3169 struct sk_buff *skb;
3170 struct sadb_msg *hdr;
3171 struct sadb_sa *sa;
3172 struct sadb_address *addr;
3173 struct sadb_x_nat_t_port *n_port;
3174 struct sockaddr_in *sin;
3175#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3176 struct sockaddr_in6 *sin6;
3177#endif
3178 int sockaddr_size;
3179 int size;
3180 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3181 struct xfrm_encap_tmpl *natt = NULL;
3182
3183 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3184 if (!sockaddr_size)
3185 return -EINVAL;
3186
3187 if (!satype)
3188 return -EINVAL;
3189
3190 if (!x->encap)
3191 return -EINVAL;
3192
3193 natt = x->encap;
3194
3195 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3196 *
3197 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3198 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3199 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 size = sizeof(struct sadb_msg) +
3202 sizeof(struct sadb_sa) +
3203 (sizeof(struct sadb_address) * 2) +
3204 (sockaddr_size * 2) +
3205 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 skb = alloc_skb(size + 16, GFP_ATOMIC);
3208 if (skb == NULL)
3209 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3212 hdr->sadb_msg_version = PF_KEY_V2;
3213 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3214 hdr->sadb_msg_satype = satype;
3215 hdr->sadb_msg_len = size / sizeof(uint64_t);
3216 hdr->sadb_msg_errno = 0;
3217 hdr->sadb_msg_reserved = 0;
3218 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3219 hdr->sadb_msg_pid = 0;
3220
3221 /* SA */
3222 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3223 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3224 sa->sadb_sa_exttype = SADB_EXT_SA;
3225 sa->sadb_sa_spi = x->id.spi;
3226 sa->sadb_sa_replay = 0;
3227 sa->sadb_sa_state = 0;
3228 sa->sadb_sa_auth = 0;
3229 sa->sadb_sa_encrypt = 0;
3230 sa->sadb_sa_flags = 0;
3231
3232 /* ADDRESS_SRC (old addr) */
3233 addr = (struct sadb_address*)
3234 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003235 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 (sizeof(struct sadb_address)+sockaddr_size)/
3237 sizeof(uint64_t);
3238 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3239 addr->sadb_address_proto = 0;
3240 addr->sadb_address_reserved = 0;
3241 if (x->props.family == AF_INET) {
3242 addr->sadb_address_prefixlen = 32;
3243
3244 sin = (struct sockaddr_in *) (addr + 1);
3245 sin->sin_family = AF_INET;
3246 sin->sin_addr.s_addr = x->props.saddr.a4;
3247 sin->sin_port = 0;
3248 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3249 }
3250#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3251 else if (x->props.family == AF_INET6) {
3252 addr->sadb_address_prefixlen = 128;
3253
3254 sin6 = (struct sockaddr_in6 *) (addr + 1);
3255 sin6->sin6_family = AF_INET6;
3256 sin6->sin6_port = 0;
3257 sin6->sin6_flowinfo = 0;
3258 memcpy(&sin6->sin6_addr,
3259 x->props.saddr.a6, sizeof(struct in6_addr));
3260 sin6->sin6_scope_id = 0;
3261 }
3262#endif
3263 else
3264 BUG();
3265
3266 /* NAT_T_SPORT (old port) */
3267 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3268 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3269 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3270 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3271 n_port->sadb_x_nat_t_port_reserved = 0;
3272
3273 /* ADDRESS_DST (new addr) */
3274 addr = (struct sadb_address*)
3275 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003276 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 (sizeof(struct sadb_address)+sockaddr_size)/
3278 sizeof(uint64_t);
3279 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3280 addr->sadb_address_proto = 0;
3281 addr->sadb_address_reserved = 0;
3282 if (x->props.family == AF_INET) {
3283 addr->sadb_address_prefixlen = 32;
3284
3285 sin = (struct sockaddr_in *) (addr + 1);
3286 sin->sin_family = AF_INET;
3287 sin->sin_addr.s_addr = ipaddr->a4;
3288 sin->sin_port = 0;
3289 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3290 }
3291#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3292 else if (x->props.family == AF_INET6) {
3293 addr->sadb_address_prefixlen = 128;
3294
3295 sin6 = (struct sockaddr_in6 *) (addr + 1);
3296 sin6->sin6_family = AF_INET6;
3297 sin6->sin6_port = 0;
3298 sin6->sin6_flowinfo = 0;
3299 memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
3300 sin6->sin6_scope_id = 0;
3301 }
3302#endif
3303 else
3304 BUG();
3305
3306 /* NAT_T_DPORT (new port) */
3307 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3308 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3309 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3310 n_port->sadb_x_nat_t_port_port = sport;
3311 n_port->sadb_x_nat_t_port_reserved = 0;
3312
3313 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3314}
3315
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003316#ifdef CONFIG_NET_KEY_MIGRATE
3317static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3318 struct xfrm_selector *sel)
3319{
3320 struct sadb_address *addr;
3321 struct sockaddr_in *sin;
3322#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3323 struct sockaddr_in6 *sin6;
3324#endif
3325 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3326 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3327 addr->sadb_address_exttype = type;
3328 addr->sadb_address_proto = sel->proto;
3329 addr->sadb_address_reserved = 0;
3330
3331 switch (type) {
3332 case SADB_EXT_ADDRESS_SRC:
3333 if (sel->family == AF_INET) {
3334 addr->sadb_address_prefixlen = sel->prefixlen_s;
3335 sin = (struct sockaddr_in *)(addr + 1);
3336 sin->sin_family = AF_INET;
3337 memcpy(&sin->sin_addr.s_addr, &sel->saddr,
3338 sizeof(sin->sin_addr.s_addr));
3339 sin->sin_port = 0;
3340 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3341 }
3342#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3343 else if (sel->family == AF_INET6) {
3344 addr->sadb_address_prefixlen = sel->prefixlen_s;
3345 sin6 = (struct sockaddr_in6 *)(addr + 1);
3346 sin6->sin6_family = AF_INET6;
3347 sin6->sin6_port = 0;
3348 sin6->sin6_flowinfo = 0;
3349 sin6->sin6_scope_id = 0;
3350 memcpy(&sin6->sin6_addr.s6_addr, &sel->saddr,
3351 sizeof(sin6->sin6_addr.s6_addr));
3352 }
3353#endif
3354 break;
3355 case SADB_EXT_ADDRESS_DST:
3356 if (sel->family == AF_INET) {
3357 addr->sadb_address_prefixlen = sel->prefixlen_d;
3358 sin = (struct sockaddr_in *)(addr + 1);
3359 sin->sin_family = AF_INET;
3360 memcpy(&sin->sin_addr.s_addr, &sel->daddr,
3361 sizeof(sin->sin_addr.s_addr));
3362 sin->sin_port = 0;
3363 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3364 }
3365#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3366 else if (sel->family == AF_INET6) {
3367 addr->sadb_address_prefixlen = sel->prefixlen_d;
3368 sin6 = (struct sockaddr_in6 *)(addr + 1);
3369 sin6->sin6_family = AF_INET6;
3370 sin6->sin6_port = 0;
3371 sin6->sin6_flowinfo = 0;
3372 sin6->sin6_scope_id = 0;
3373 memcpy(&sin6->sin6_addr.s6_addr, &sel->daddr,
3374 sizeof(sin6->sin6_addr.s6_addr));
3375 }
3376#endif
3377 break;
3378 default:
3379 return -EINVAL;
3380 }
3381
3382 return 0;
3383}
3384
3385static int set_ipsecrequest(struct sk_buff *skb,
3386 uint8_t proto, uint8_t mode, int level,
3387 uint32_t reqid, uint8_t family,
3388 xfrm_address_t *src, xfrm_address_t *dst)
3389{
3390 struct sadb_x_ipsecrequest *rq;
3391 struct sockaddr_in *sin;
3392#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3393 struct sockaddr_in6 *sin6;
3394#endif
3395 int size_req;
3396
3397 size_req = sizeof(struct sadb_x_ipsecrequest) +
3398 pfkey_sockaddr_pair_size(family);
3399
3400 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3401 memset(rq, 0, size_req);
3402 rq->sadb_x_ipsecrequest_len = size_req;
3403 rq->sadb_x_ipsecrequest_proto = proto;
3404 rq->sadb_x_ipsecrequest_mode = mode;
3405 rq->sadb_x_ipsecrequest_level = level;
3406 rq->sadb_x_ipsecrequest_reqid = reqid;
3407
3408 switch (family) {
3409 case AF_INET:
3410 sin = (struct sockaddr_in *)(rq + 1);
3411 sin->sin_family = AF_INET;
3412 memcpy(&sin->sin_addr.s_addr, src,
3413 sizeof(sin->sin_addr.s_addr));
3414 sin++;
3415 sin->sin_family = AF_INET;
3416 memcpy(&sin->sin_addr.s_addr, dst,
3417 sizeof(sin->sin_addr.s_addr));
3418 break;
3419#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3420 case AF_INET6:
3421 sin6 = (struct sockaddr_in6 *)(rq + 1);
3422 sin6->sin6_family = AF_INET6;
3423 sin6->sin6_port = 0;
3424 sin6->sin6_flowinfo = 0;
3425 sin6->sin6_scope_id = 0;
3426 memcpy(&sin6->sin6_addr.s6_addr, src,
3427 sizeof(sin6->sin6_addr.s6_addr));
3428 sin6++;
3429 sin6->sin6_family = AF_INET6;
3430 sin6->sin6_port = 0;
3431 sin6->sin6_flowinfo = 0;
3432 sin6->sin6_scope_id = 0;
3433 memcpy(&sin6->sin6_addr.s6_addr, dst,
3434 sizeof(sin6->sin6_addr.s6_addr));
3435 break;
3436#endif
3437 default:
3438 return -EINVAL;
3439 }
3440
3441 return 0;
3442}
3443#endif
3444
3445#ifdef CONFIG_NET_KEY_MIGRATE
3446static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3447 struct xfrm_migrate *m, int num_bundles)
3448{
3449 int i;
3450 int sasize_sel;
3451 int size = 0;
3452 int size_pol = 0;
3453 struct sk_buff *skb;
3454 struct sadb_msg *hdr;
3455 struct sadb_x_policy *pol;
3456 struct xfrm_migrate *mp;
3457
3458 if (type != XFRM_POLICY_TYPE_MAIN)
3459 return 0;
3460
3461 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3462 return -EINVAL;
3463
3464 /* selector */
3465 sasize_sel = pfkey_sockaddr_size(sel->family);
3466 if (!sasize_sel)
3467 return -EINVAL;
3468 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3469
3470 /* policy info */
3471 size_pol += sizeof(struct sadb_x_policy);
3472
3473 /* ipsecrequests */
3474 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3475 /* old locator pair */
3476 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3477 pfkey_sockaddr_pair_size(mp->old_family);
3478 /* new locator pair */
3479 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3480 pfkey_sockaddr_pair_size(mp->new_family);
3481 }
3482
3483 size += sizeof(struct sadb_msg) + size_pol;
3484
3485 /* alloc buffer */
3486 skb = alloc_skb(size, GFP_ATOMIC);
3487 if (skb == NULL)
3488 return -ENOMEM;
3489
3490 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3491 hdr->sadb_msg_version = PF_KEY_V2;
3492 hdr->sadb_msg_type = SADB_X_MIGRATE;
3493 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3494 hdr->sadb_msg_len = size / 8;
3495 hdr->sadb_msg_errno = 0;
3496 hdr->sadb_msg_reserved = 0;
3497 hdr->sadb_msg_seq = 0;
3498 hdr->sadb_msg_pid = 0;
3499
3500 /* selector src */
3501 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3502
3503 /* selector dst */
3504 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3505
3506 /* policy information */
3507 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3508 pol->sadb_x_policy_len = size_pol / 8;
3509 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3510 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3511 pol->sadb_x_policy_dir = dir + 1;
3512 pol->sadb_x_policy_id = 0;
3513 pol->sadb_x_policy_priority = 0;
3514
3515 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3516 /* old ipsecrequest */
3517 if (set_ipsecrequest(skb, mp->proto, mp->mode + 1,
3518 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3519 mp->reqid, mp->old_family,
3520 &mp->old_saddr, &mp->old_daddr) < 0) {
3521 return -EINVAL;
3522 }
3523
3524 /* new ipsecrequest */
3525 if (set_ipsecrequest(skb, mp->proto, mp->mode + 1,
3526 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3527 mp->reqid, mp->new_family,
3528 &mp->new_saddr, &mp->new_daddr) < 0) {
3529 return -EINVAL;
3530 }
3531 }
3532
3533 /* broadcast migrate message to sockets */
3534 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
3535
3536 return 0;
3537}
3538#else
3539static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3540 struct xfrm_migrate *m, int num_bundles)
3541{
3542 return -ENOPROTOOPT;
3543}
3544#endif
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546static int pfkey_sendmsg(struct kiocb *kiocb,
3547 struct socket *sock, struct msghdr *msg, size_t len)
3548{
3549 struct sock *sk = sock->sk;
3550 struct sk_buff *skb = NULL;
3551 struct sadb_msg *hdr = NULL;
3552 int err;
3553
3554 err = -EOPNOTSUPP;
3555 if (msg->msg_flags & MSG_OOB)
3556 goto out;
3557
3558 err = -EMSGSIZE;
3559 if ((unsigned)len > sk->sk_sndbuf - 32)
3560 goto out;
3561
3562 err = -ENOBUFS;
3563 skb = alloc_skb(len, GFP_KERNEL);
3564 if (skb == NULL)
3565 goto out;
3566
3567 err = -EFAULT;
3568 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3569 goto out;
3570
3571 hdr = pfkey_get_base_msg(skb, &err);
3572 if (!hdr)
3573 goto out;
3574
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003575 mutex_lock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 err = pfkey_process(sk, skb, hdr);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003577 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
3579out:
3580 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3581 err = 0;
3582 if (skb)
3583 kfree_skb(skb);
3584
3585 return err ? : len;
3586}
3587
3588static int pfkey_recvmsg(struct kiocb *kiocb,
3589 struct socket *sock, struct msghdr *msg, size_t len,
3590 int flags)
3591{
3592 struct sock *sk = sock->sk;
3593 struct sk_buff *skb;
3594 int copied, err;
3595
3596 err = -EINVAL;
3597 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3598 goto out;
3599
3600 msg->msg_namelen = 0;
3601 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3602 if (skb == NULL)
3603 goto out;
3604
3605 copied = skb->len;
3606 if (copied > len) {
3607 msg->msg_flags |= MSG_TRUNC;
3608 copied = len;
3609 }
3610
3611 skb->h.raw = skb->data;
3612 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3613 if (err)
3614 goto out_free;
3615
3616 sock_recv_timestamp(msg, sk, skb);
3617
3618 err = (flags & MSG_TRUNC) ? skb->len : copied;
3619
3620out_free:
3621 skb_free_datagram(sk, skb);
3622out:
3623 return err;
3624}
3625
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003626static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 .family = PF_KEY,
3628 .owner = THIS_MODULE,
3629 /* Operations that make no sense on pfkey sockets. */
3630 .bind = sock_no_bind,
3631 .connect = sock_no_connect,
3632 .socketpair = sock_no_socketpair,
3633 .accept = sock_no_accept,
3634 .getname = sock_no_getname,
3635 .ioctl = sock_no_ioctl,
3636 .listen = sock_no_listen,
3637 .shutdown = sock_no_shutdown,
3638 .setsockopt = sock_no_setsockopt,
3639 .getsockopt = sock_no_getsockopt,
3640 .mmap = sock_no_mmap,
3641 .sendpage = sock_no_sendpage,
3642
3643 /* Now the operations that really occur. */
3644 .release = pfkey_release,
3645 .poll = datagram_poll,
3646 .sendmsg = pfkey_sendmsg,
3647 .recvmsg = pfkey_recvmsg,
3648};
3649
3650static struct net_proto_family pfkey_family_ops = {
3651 .family = PF_KEY,
3652 .create = pfkey_create,
3653 .owner = THIS_MODULE,
3654};
3655
3656#ifdef CONFIG_PROC_FS
3657static int pfkey_read_proc(char *buffer, char **start, off_t offset,
3658 int length, int *eof, void *data)
3659{
3660 off_t pos = 0;
3661 off_t begin = 0;
3662 int len = 0;
3663 struct sock *s;
3664 struct hlist_node *node;
3665
3666 len += sprintf(buffer,"sk RefCnt Rmem Wmem User Inode\n");
3667
3668 read_lock(&pfkey_table_lock);
3669
3670 sk_for_each(s, node, &pfkey_table) {
3671 len += sprintf(buffer+len,"%p %-6d %-6u %-6u %-6u %-6lu",
3672 s,
3673 atomic_read(&s->sk_refcnt),
3674 atomic_read(&s->sk_rmem_alloc),
3675 atomic_read(&s->sk_wmem_alloc),
3676 sock_i_uid(s),
3677 sock_i_ino(s)
3678 );
3679
3680 buffer[len++] = '\n';
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003681
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 pos = begin + len;
3683 if (pos < offset) {
3684 len = 0;
3685 begin = pos;
3686 }
3687 if(pos > offset + length)
3688 goto done;
3689 }
3690 *eof = 1;
3691
3692done:
3693 read_unlock(&pfkey_table_lock);
3694
3695 *start = buffer + (offset - begin);
3696 len -= (offset - begin);
3697
3698 if (len > length)
3699 len = length;
3700 if (len < 0)
3701 len = 0;
3702
3703 return len;
3704}
3705#endif
3706
3707static struct xfrm_mgr pfkeyv2_mgr =
3708{
3709 .id = "pfkeyv2",
3710 .notify = pfkey_send_notify,
3711 .acquire = pfkey_send_acquire,
3712 .compile_policy = pfkey_compile_policy,
3713 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003714 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003715 .migrate = pfkey_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716};
3717
3718static void __exit ipsec_pfkey_exit(void)
3719{
3720 xfrm_unregister_km(&pfkeyv2_mgr);
3721 remove_proc_entry("net/pfkey", NULL);
3722 sock_unregister(PF_KEY);
3723 proto_unregister(&key_proto);
3724}
3725
3726static int __init ipsec_pfkey_init(void)
3727{
3728 int err = proto_register(&key_proto, 0);
3729
3730 if (err != 0)
3731 goto out;
3732
3733 err = sock_register(&pfkey_family_ops);
3734 if (err != 0)
3735 goto out_unregister_key_proto;
3736#ifdef CONFIG_PROC_FS
3737 err = -ENOMEM;
3738 if (create_proc_read_entry("net/pfkey", 0, NULL, pfkey_read_proc, NULL) == NULL)
3739 goto out_sock_unregister;
3740#endif
3741 err = xfrm_register_km(&pfkeyv2_mgr);
3742 if (err != 0)
3743 goto out_remove_proc_entry;
3744out:
3745 return err;
3746out_remove_proc_entry:
3747#ifdef CONFIG_PROC_FS
3748 remove_proc_entry("net/pfkey", NULL);
3749out_sock_unregister:
3750#endif
3751 sock_unregister(PF_KEY);
3752out_unregister_key_proto:
3753 proto_unregister(&key_proto);
3754 goto out;
3755}
3756
3757module_init(ipsec_pfkey_init);
3758module_exit(ipsec_pfkey_exit);
3759MODULE_LICENSE("GPL");
3760MODULE_ALIAS_NETPROTO(PF_KEY);