blob: ff5c3d03005e4603b0748d003aff2b097ca675c3 [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>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020029#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/xfrm.h>
31
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
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700139static int pfkey_create(struct net *net, struct socket *sock, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 struct sock *sk;
142 int err;
143
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700144 if (net != &init_net)
145 return -EAFNOSUPPORT;
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (!capable(CAP_NET_ADMIN))
148 return -EPERM;
149 if (sock->type != SOCK_RAW)
150 return -ESOCKTNOSUPPORT;
151 if (protocol != PF_KEY_V2)
152 return -EPROTONOSUPPORT;
153
154 err = -ENOMEM;
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700155 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (sk == NULL)
157 goto out;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 sock->ops = &pfkey_ops;
160 sock_init_data(sock, sk);
161
162 sk->sk_family = PF_KEY;
163 sk->sk_destruct = pfkey_sock_destruct;
164
165 atomic_inc(&pfkey_socks_nr);
166
167 pfkey_insert(sk);
168
169 return 0;
170out:
171 return err;
172}
173
174static int pfkey_release(struct socket *sock)
175{
176 struct sock *sk = sock->sk;
177
178 if (!sk)
179 return 0;
180
181 pfkey_remove(sk);
182
183 sock_orphan(sk);
184 sock->sk = NULL;
185 skb_queue_purge(&sk->sk_write_queue);
186 sock_put(sk);
187
188 return 0;
189}
190
191static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
Al Virodd0fc662005-10-07 07:46:04 +0100192 gfp_t allocation, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 int err = -ENOBUFS;
195
196 sock_hold(sk);
197 if (*skb2 == NULL) {
198 if (atomic_read(&skb->users) != 1) {
199 *skb2 = skb_clone(skb, allocation);
200 } else {
201 *skb2 = skb;
202 atomic_inc(&skb->users);
203 }
204 }
205 if (*skb2 != NULL) {
206 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
207 skb_orphan(*skb2);
208 skb_set_owner_r(*skb2, sk);
209 skb_queue_tail(&sk->sk_receive_queue, *skb2);
210 sk->sk_data_ready(sk, (*skb2)->len);
211 *skb2 = NULL;
212 err = 0;
213 }
214 }
215 sock_put(sk);
216 return err;
217}
218
219/* Send SKB to all pfkey sockets matching selected criteria. */
220#define BROADCAST_ALL 0
221#define BROADCAST_ONE 1
222#define BROADCAST_REGISTERED 2
223#define BROADCAST_PROMISC_ONLY 4
Al Virodd0fc662005-10-07 07:46:04 +0100224static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int broadcast_flags, struct sock *one_sk)
226{
227 struct sock *sk;
228 struct hlist_node *node;
229 struct sk_buff *skb2 = NULL;
230 int err = -ESRCH;
231
232 /* XXX Do we need something like netlink_overrun? I think
233 * XXX PF_KEY socket apps will not mind current behavior.
234 */
235 if (!skb)
236 return -ENOMEM;
237
238 pfkey_lock_table();
239 sk_for_each(sk, node, &pfkey_table) {
240 struct pfkey_sock *pfk = pfkey_sk(sk);
241 int err2;
242
243 /* Yes, it means that if you are meant to receive this
244 * pfkey message you receive it twice as promiscuous
245 * socket.
246 */
247 if (pfk->promisc)
248 pfkey_broadcast_one(skb, &skb2, allocation, sk);
249
250 /* the exact target will be processed later */
251 if (sk == one_sk)
252 continue;
253 if (broadcast_flags != BROADCAST_ALL) {
254 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
255 continue;
256 if ((broadcast_flags & BROADCAST_REGISTERED) &&
257 !pfk->registered)
258 continue;
259 if (broadcast_flags & BROADCAST_ONE)
260 continue;
261 }
262
263 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
264
265 /* Error is cleare after succecful sending to at least one
266 * registered KM */
267 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
268 err = err2;
269 }
270 pfkey_unlock_table();
271
272 if (one_sk != NULL)
273 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
274
275 if (skb2)
276 kfree_skb(skb2);
277 kfree_skb(skb);
278 return err;
279}
280
281static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
282{
283 *new = *orig;
284}
285
286static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
287{
288 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
289 struct sadb_msg *hdr;
290
291 if (!skb)
292 return -ENOBUFS;
293
294 /* Woe be to the platform trying to support PFKEY yet
295 * having normal errnos outside the 1-255 range, inclusive.
296 */
297 err = -err;
298 if (err == ERESTARTSYS ||
299 err == ERESTARTNOHAND ||
300 err == ERESTARTNOINTR)
301 err = EINTR;
302 if (err >= 512)
303 err = EINVAL;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800304 BUG_ON(err <= 0 || err >= 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
307 pfkey_hdr_dup(hdr, orig);
308 hdr->sadb_msg_errno = (uint8_t) err;
309 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
310 sizeof(uint64_t));
311
312 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);
313
314 return 0;
315}
316
317static u8 sadb_ext_min_len[] = {
318 [SADB_EXT_RESERVED] = (u8) 0,
319 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
320 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
321 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
322 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
323 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
324 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
325 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
326 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
327 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
328 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
329 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
330 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
331 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
332 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
333 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
334 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
335 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
336 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
337 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
338 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
339 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
340 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
341 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
Trent Jaegerdf718372005-12-13 23:12:27 -0800342 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343};
344
345/* Verify sadb_address_{len,prefixlen} against sa_family. */
346static int verify_address_len(void *p)
347{
348 struct sadb_address *sp = p;
349 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
350 struct sockaddr_in *sin;
351#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
352 struct sockaddr_in6 *sin6;
353#endif
354 int len;
355
356 switch (addr->sa_family) {
357 case AF_INET:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700358 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (sp->sadb_address_len != len ||
360 sp->sadb_address_prefixlen > 32)
361 return -EINVAL;
362 break;
363#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
364 case AF_INET6:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700365 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (sp->sadb_address_len != len ||
367 sp->sadb_address_prefixlen > 128)
368 return -EINVAL;
369 break;
370#endif
371 default:
372 /* It is user using kernel to keep track of security
373 * associations for another protocol, such as
374 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
375 * lengths.
376 *
377 * XXX Actually, association/policy database is not yet
378 * XXX able to cope with arbitrary sockaddr families.
379 * XXX When it can, remove this -EINVAL. -DaveM
380 */
381 return -EINVAL;
382 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 return 0;
386}
387
Trent Jaegerdf718372005-12-13 23:12:27 -0800388static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx)
389{
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700390 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
391 sec_ctx->sadb_x_ctx_len,
392 sizeof(uint64_t));
Trent Jaegerdf718372005-12-13 23:12:27 -0800393}
394
395static inline int verify_sec_ctx_len(void *p)
396{
397 struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p;
398 int len;
399
400 if (sec_ctx->sadb_x_ctx_len > PAGE_SIZE)
401 return -EINVAL;
402
403 len = pfkey_sec_ctx_len(sec_ctx);
404
405 if (sec_ctx->sadb_x_sec_len != len)
406 return -EINVAL;
407
408 return 0;
409}
410
411static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx)
412{
413 struct xfrm_user_sec_ctx *uctx = NULL;
414 int ctx_size = sec_ctx->sadb_x_ctx_len;
415
416 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
417
418 if (!uctx)
419 return NULL;
420
421 uctx->len = pfkey_sec_ctx_len(sec_ctx);
422 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
423 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
424 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
425 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
426 memcpy(uctx + 1, sec_ctx + 1,
427 uctx->ctx_len);
428
429 return uctx;
430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432static int present_and_same_family(struct sadb_address *src,
433 struct sadb_address *dst)
434{
435 struct sockaddr *s_addr, *d_addr;
436
437 if (!src || !dst)
438 return 0;
439
440 s_addr = (struct sockaddr *)(src + 1);
441 d_addr = (struct sockaddr *)(dst + 1);
442 if (s_addr->sa_family != d_addr->sa_family)
443 return 0;
444 if (s_addr->sa_family != AF_INET
445#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
446 && s_addr->sa_family != AF_INET6
447#endif
448 )
449 return 0;
450
451 return 1;
452}
453
454static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
455{
456 char *p = (char *) hdr;
457 int len = skb->len;
458
459 len -= sizeof(*hdr);
460 p += sizeof(*hdr);
461 while (len > 0) {
462 struct sadb_ext *ehdr = (struct sadb_ext *) p;
463 uint16_t ext_type;
464 int ext_len;
465
466 ext_len = ehdr->sadb_ext_len;
467 ext_len *= sizeof(uint64_t);
468 ext_type = ehdr->sadb_ext_type;
469 if (ext_len < sizeof(uint64_t) ||
470 ext_len > len ||
471 ext_type == SADB_EXT_RESERVED)
472 return -EINVAL;
473
474 if (ext_type <= SADB_EXT_MAX) {
475 int min = (int) sadb_ext_min_len[ext_type];
476 if (ext_len < min)
477 return -EINVAL;
478 if (ext_hdrs[ext_type-1] != NULL)
479 return -EINVAL;
480 if (ext_type == SADB_EXT_ADDRESS_SRC ||
481 ext_type == SADB_EXT_ADDRESS_DST ||
482 ext_type == SADB_EXT_ADDRESS_PROXY ||
483 ext_type == SADB_X_EXT_NAT_T_OA) {
484 if (verify_address_len(p))
485 return -EINVAL;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900486 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800487 if (ext_type == SADB_X_EXT_SEC_CTX) {
488 if (verify_sec_ctx_len(p))
489 return -EINVAL;
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 ext_hdrs[ext_type-1] = p;
492 }
493 p += ext_len;
494 len -= ext_len;
495 }
496
497 return 0;
498}
499
500static uint16_t
501pfkey_satype2proto(uint8_t satype)
502{
503 switch (satype) {
504 case SADB_SATYPE_UNSPEC:
505 return IPSEC_PROTO_ANY;
506 case SADB_SATYPE_AH:
507 return IPPROTO_AH;
508 case SADB_SATYPE_ESP:
509 return IPPROTO_ESP;
510 case SADB_X_SATYPE_IPCOMP:
511 return IPPROTO_COMP;
512 break;
513 default:
514 return 0;
515 }
516 /* NOTREACHED */
517}
518
519static uint8_t
520pfkey_proto2satype(uint16_t proto)
521{
522 switch (proto) {
523 case IPPROTO_AH:
524 return SADB_SATYPE_AH;
525 case IPPROTO_ESP:
526 return SADB_SATYPE_ESP;
527 case IPPROTO_COMP:
528 return SADB_X_SATYPE_IPCOMP;
529 break;
530 default:
531 return 0;
532 }
533 /* NOTREACHED */
534}
535
536/* BTW, this scheme means that there is no way with PFKEY2 sockets to
537 * say specifically 'just raw sockets' as we encode them as 255.
538 */
539
540static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
541{
542 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
543}
544
545static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
546{
547 return (proto ? proto : IPSEC_PROTO_ANY);
548}
549
550static int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr,
551 xfrm_address_t *xaddr)
552{
553 switch (((struct sockaddr*)(addr + 1))->sa_family) {
554 case AF_INET:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900555 xaddr->a4 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 ((struct sockaddr_in *)(addr + 1))->sin_addr.s_addr;
557 return AF_INET;
558#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
559 case AF_INET6:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900560 memcpy(xaddr->a6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 &((struct sockaddr_in6 *)(addr + 1))->sin6_addr,
562 sizeof(struct in6_addr));
563 return AF_INET6;
564#endif
565 default:
566 return 0;
567 }
568 /* NOTREACHED */
569}
570
571static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
572{
573 struct sadb_sa *sa;
574 struct sadb_address *addr;
575 uint16_t proto;
576 unsigned short family;
577 xfrm_address_t *xaddr;
578
579 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
580 if (sa == NULL)
581 return NULL;
582
583 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
584 if (proto == 0)
585 return NULL;
586
587 /* sadb_address_len should be checked by caller */
588 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
589 if (addr == NULL)
590 return NULL;
591
592 family = ((struct sockaddr *)(addr + 1))->sa_family;
593 switch (family) {
594 case AF_INET:
595 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
596 break;
597#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
598 case AF_INET6:
599 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
600 break;
601#endif
602 default:
603 xaddr = NULL;
604 }
605
606 if (!xaddr)
607 return NULL;
608
609 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
610}
611
612#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
613static int
614pfkey_sockaddr_size(sa_family_t family)
615{
616 switch (family) {
617 case AF_INET:
618 return PFKEY_ALIGN8(sizeof(struct sockaddr_in));
619#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
620 case AF_INET6:
621 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6));
622#endif
623 default:
624 return 0;
625 }
626 /* NOTREACHED */
627}
628
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700629static inline int pfkey_mode_from_xfrm(int mode)
630{
631 switch(mode) {
632 case XFRM_MODE_TRANSPORT:
633 return IPSEC_MODE_TRANSPORT;
634 case XFRM_MODE_TUNNEL:
635 return IPSEC_MODE_TUNNEL;
636 case XFRM_MODE_BEET:
637 return IPSEC_MODE_BEET;
638 default:
639 return -1;
640 }
641}
642
643static inline int pfkey_mode_to_xfrm(int mode)
644{
645 switch(mode) {
646 case IPSEC_MODE_ANY: /*XXX*/
647 case IPSEC_MODE_TRANSPORT:
648 return XFRM_MODE_TRANSPORT;
649 case IPSEC_MODE_TUNNEL:
650 return XFRM_MODE_TUNNEL;
651 case IPSEC_MODE_BEET:
652 return XFRM_MODE_BEET;
653 default:
654 return -1;
655 }
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658static struct sk_buff * pfkey_xfrm_state2msg(struct xfrm_state *x, int add_keys, int hsc)
659{
660 struct sk_buff *skb;
661 struct sadb_msg *hdr;
662 struct sadb_sa *sa;
663 struct sadb_lifetime *lifetime;
664 struct sadb_address *addr;
665 struct sadb_key *key;
666 struct sadb_x_sa2 *sa2;
667 struct sockaddr_in *sin;
Trent Jaegerdf718372005-12-13 23:12:27 -0800668 struct sadb_x_sec_ctx *sec_ctx;
669 struct xfrm_sec_ctx *xfrm_ctx;
670 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
672 struct sockaddr_in6 *sin6;
673#endif
674 int size;
675 int auth_key_size = 0;
676 int encrypt_key_size = 0;
677 int sockaddr_size;
678 struct xfrm_encap_tmpl *natt = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700679 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 /* address family check */
682 sockaddr_size = pfkey_sockaddr_size(x->props.family);
683 if (!sockaddr_size)
684 return ERR_PTR(-EINVAL);
685
686 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
687 key(AE), (identity(SD),) (sensitivity)> */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900688 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 sizeof(struct sadb_lifetime) +
690 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
691 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900692 sizeof(struct sadb_address)*2 +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 sockaddr_size*2 +
694 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800695
696 if ((xfrm_ctx = x->security)) {
697 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
698 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
699 }
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /* identity & sensitivity */
702
703 if ((x->props.family == AF_INET &&
704 x->sel.saddr.a4 != x->props.saddr.a4)
705#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
706 || (x->props.family == AF_INET6 &&
707 memcmp (x->sel.saddr.a6, x->props.saddr.a6, sizeof (struct in6_addr)))
708#endif
709 )
710 size += sizeof(struct sadb_address) + sockaddr_size;
711
712 if (add_keys) {
713 if (x->aalg && x->aalg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900714 auth_key_size =
715 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 size += sizeof(struct sadb_key) + auth_key_size;
717 }
718 if (x->ealg && x->ealg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900719 encrypt_key_size =
720 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 size += sizeof(struct sadb_key) + encrypt_key_size;
722 }
723 }
724 if (x->encap)
725 natt = x->encap;
726
727 if (natt && natt->encap_type) {
728 size += sizeof(struct sadb_x_nat_t_type);
729 size += sizeof(struct sadb_x_nat_t_port);
730 size += sizeof(struct sadb_x_nat_t_port);
731 }
732
733 skb = alloc_skb(size + 16, GFP_ATOMIC);
734 if (skb == NULL)
735 return ERR_PTR(-ENOBUFS);
736
737 /* call should fill header later */
738 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
739 memset(hdr, 0, size); /* XXX do we need this ? */
740 hdr->sadb_msg_len = size / sizeof(uint64_t);
741
742 /* sa */
743 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
744 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
745 sa->sadb_sa_exttype = SADB_EXT_SA;
746 sa->sadb_sa_spi = x->id.spi;
747 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700748 switch (x->km.state) {
749 case XFRM_STATE_VALID:
750 sa->sadb_sa_state = x->km.dying ?
751 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
752 break;
753 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700755 break;
756 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700758 break;
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 sa->sadb_sa_auth = 0;
761 if (x->aalg) {
762 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
763 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
764 }
765 sa->sadb_sa_encrypt = 0;
766 BUG_ON(x->ealg && x->calg);
767 if (x->ealg) {
768 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
769 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
770 }
771 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
772 if (x->calg) {
773 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
774 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
775 }
776
777 sa->sadb_sa_flags = 0;
778 if (x->props.flags & XFRM_STATE_NOECN)
779 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
780 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
781 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700782 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
783 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* hard time */
786 if (hsc & 2) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900787 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 sizeof(struct sadb_lifetime));
789 lifetime->sadb_lifetime_len =
790 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
791 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
792 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
793 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
794 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
795 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
796 }
797 /* soft time */
798 if (hsc & 1) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900799 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 sizeof(struct sadb_lifetime));
801 lifetime->sadb_lifetime_len =
802 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
803 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
804 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
805 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
806 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
807 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
808 }
809 /* current time */
810 lifetime = (struct sadb_lifetime *) skb_put(skb,
811 sizeof(struct sadb_lifetime));
812 lifetime->sadb_lifetime_len =
813 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
814 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
815 lifetime->sadb_lifetime_allocations = x->curlft.packets;
816 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
817 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
818 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
819 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900820 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900822 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 (sizeof(struct sadb_address)+sockaddr_size)/
824 sizeof(uint64_t);
825 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900826 /* "if the ports are non-zero, then the sadb_address_proto field,
827 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900829 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 addr->sadb_address_reserved = 0;
831 if (x->props.family == AF_INET) {
832 addr->sadb_address_prefixlen = 32;
833
834 sin = (struct sockaddr_in *) (addr + 1);
835 sin->sin_family = AF_INET;
836 sin->sin_addr.s_addr = x->props.saddr.a4;
837 sin->sin_port = 0;
838 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
839 }
840#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
841 else if (x->props.family == AF_INET6) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900842 addr->sadb_address_prefixlen = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 sin6 = (struct sockaddr_in6 *) (addr + 1);
845 sin6->sin6_family = AF_INET6;
846 sin6->sin6_port = 0;
847 sin6->sin6_flowinfo = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900848 memcpy(&sin6->sin6_addr, x->props.saddr.a6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 sizeof(struct in6_addr));
850 sin6->sin6_scope_id = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852#endif
853 else
854 BUG();
855
856 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900857 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900859 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 (sizeof(struct sadb_address)+sockaddr_size)/
861 sizeof(uint64_t);
862 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900863 addr->sadb_address_proto = 0;
864 addr->sadb_address_prefixlen = 32; /* XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 addr->sadb_address_reserved = 0;
866 if (x->props.family == AF_INET) {
867 sin = (struct sockaddr_in *) (addr + 1);
868 sin->sin_family = AF_INET;
869 sin->sin_addr.s_addr = x->id.daddr.a4;
870 sin->sin_port = 0;
871 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
872
873 if (x->sel.saddr.a4 != x->props.saddr.a4) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900874 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900876 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 (sizeof(struct sadb_address)+sockaddr_size)/
878 sizeof(uint64_t);
879 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
880 addr->sadb_address_proto =
881 pfkey_proto_from_xfrm(x->sel.proto);
882 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
883 addr->sadb_address_reserved = 0;
884
885 sin = (struct sockaddr_in *) (addr + 1);
886 sin->sin_family = AF_INET;
887 sin->sin_addr.s_addr = x->sel.saddr.a4;
888 sin->sin_port = x->sel.sport;
889 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
890 }
891 }
892#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
893 else if (x->props.family == AF_INET6) {
894 addr->sadb_address_prefixlen = 128;
895
896 sin6 = (struct sockaddr_in6 *) (addr + 1);
897 sin6->sin6_family = AF_INET6;
898 sin6->sin6_port = 0;
899 sin6->sin6_flowinfo = 0;
900 memcpy(&sin6->sin6_addr, x->id.daddr.a6, sizeof(struct in6_addr));
901 sin6->sin6_scope_id = 0;
902
903 if (memcmp (x->sel.saddr.a6, x->props.saddr.a6,
904 sizeof(struct in6_addr))) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900905 addr = (struct sadb_address *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900907 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 (sizeof(struct sadb_address)+sockaddr_size)/
909 sizeof(uint64_t);
910 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
911 addr->sadb_address_proto =
912 pfkey_proto_from_xfrm(x->sel.proto);
913 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
914 addr->sadb_address_reserved = 0;
915
916 sin6 = (struct sockaddr_in6 *) (addr + 1);
917 sin6->sin6_family = AF_INET6;
918 sin6->sin6_port = x->sel.sport;
919 sin6->sin6_flowinfo = 0;
920 memcpy(&sin6->sin6_addr, x->sel.saddr.a6,
921 sizeof(struct in6_addr));
922 sin6->sin6_scope_id = 0;
923 }
924 }
925#endif
926 else
927 BUG();
928
929 /* auth key */
930 if (add_keys && auth_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900931 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 sizeof(struct sadb_key)+auth_key_size);
933 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
934 sizeof(uint64_t);
935 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
936 key->sadb_key_bits = x->aalg->alg_key_len;
937 key->sadb_key_reserved = 0;
938 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
939 }
940 /* encrypt key */
941 if (add_keys && encrypt_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900942 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 sizeof(struct sadb_key)+encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900944 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 encrypt_key_size) / sizeof(uint64_t);
946 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
947 key->sadb_key_bits = x->ealg->alg_key_len;
948 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900949 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 (x->ealg->alg_key_len+7)/8);
951 }
952
953 /* sa */
954 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
955 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
956 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700957 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
958 kfree_skb(skb);
959 return ERR_PTR(-EINVAL);
960 }
961 sa2->sadb_x_sa2_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 sa2->sadb_x_sa2_reserved1 = 0;
963 sa2->sadb_x_sa2_reserved2 = 0;
964 sa2->sadb_x_sa2_sequence = 0;
965 sa2->sadb_x_sa2_reqid = x->props.reqid;
966
967 if (natt && natt->encap_type) {
968 struct sadb_x_nat_t_type *n_type;
969 struct sadb_x_nat_t_port *n_port;
970
971 /* type */
972 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
973 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
974 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
975 n_type->sadb_x_nat_t_type_type = natt->encap_type;
976 n_type->sadb_x_nat_t_type_reserved[0] = 0;
977 n_type->sadb_x_nat_t_type_reserved[1] = 0;
978 n_type->sadb_x_nat_t_type_reserved[2] = 0;
979
980 /* source port */
981 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
982 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
983 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
984 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
985 n_port->sadb_x_nat_t_port_reserved = 0;
986
987 /* dest port */
988 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
989 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
990 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
991 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
992 n_port->sadb_x_nat_t_port_reserved = 0;
993 }
994
Trent Jaegerdf718372005-12-13 23:12:27 -0800995 /* security context */
996 if (xfrm_ctx) {
997 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
998 sizeof(struct sadb_x_sec_ctx) + ctx_size);
999 sec_ctx->sadb_x_sec_len =
1000 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1001 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1002 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1003 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1004 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1005 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1006 xfrm_ctx->ctx_len);
1007 }
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return skb;
1010}
1011
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001012static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 void **ext_hdrs)
1014{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001015 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 struct sadb_lifetime *lifetime;
1017 struct sadb_sa *sa;
1018 struct sadb_key *key;
Trent Jaegerdf718372005-12-13 23:12:27 -08001019 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 uint16_t proto;
1021 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
1025 if (!sa ||
1026 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1027 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1028 return ERR_PTR(-EINVAL);
1029 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1030 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1031 return ERR_PTR(-EINVAL);
1032 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1033 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1034 return ERR_PTR(-EINVAL);
1035 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1036 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1037 return ERR_PTR(-EINVAL);
1038
1039 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1040 if (proto == 0)
1041 return ERR_PTR(-EINVAL);
1042
1043 /* default error is no buffer space */
1044 err = -ENOBUFS;
1045
1046 /* RFC2367:
1047
1048 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1049 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1050 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1051 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1052 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1053 not true.
1054
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001055 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1057 */
1058 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1059 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1060 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1061 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1062 return ERR_PTR(-EINVAL);
1063 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1064 if (key != NULL &&
1065 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1066 ((key->sadb_key_bits+7) / 8 == 0 ||
1067 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1068 return ERR_PTR(-EINVAL);
1069 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1070 if (key != NULL &&
1071 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1072 ((key->sadb_key_bits+7) / 8 == 0 ||
1073 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1074 return ERR_PTR(-EINVAL);
1075
1076 x = xfrm_state_alloc();
1077 if (x == NULL)
1078 return ERR_PTR(-ENOBUFS);
1079
1080 x->id.proto = proto;
1081 x->id.spi = sa->sadb_sa_spi;
1082 x->props.replay_window = sa->sadb_sa_replay;
1083 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1084 x->props.flags |= XFRM_STATE_NOECN;
1085 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1086 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001087 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1088 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
1091 if (lifetime != NULL) {
1092 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1093 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1094 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1095 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1096 }
1097 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
1098 if (lifetime != NULL) {
1099 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1100 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1101 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1102 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1103 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001104
1105 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
1106 if (sec_ctx != NULL) {
1107 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1108
1109 if (!uctx)
1110 goto out;
1111
1112 err = security_xfrm_state_alloc(x, uctx);
1113 kfree(uctx);
1114
1115 if (err)
1116 goto out;
1117 }
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1120 if (sa->sadb_sa_auth) {
1121 int keysize = 0;
1122 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1123 if (!a) {
1124 err = -ENOSYS;
1125 goto out;
1126 }
1127 if (key)
1128 keysize = (key->sadb_key_bits + 7) / 8;
1129 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1130 if (!x->aalg)
1131 goto out;
1132 strcpy(x->aalg->alg_name, a->name);
1133 x->aalg->alg_key_len = 0;
1134 if (key) {
1135 x->aalg->alg_key_len = key->sadb_key_bits;
1136 memcpy(x->aalg->alg_key, key+1, keysize);
1137 }
1138 x->props.aalgo = sa->sadb_sa_auth;
1139 /* x->algo.flags = sa->sadb_sa_flags; */
1140 }
1141 if (sa->sadb_sa_encrypt) {
1142 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1143 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1144 if (!a) {
1145 err = -ENOSYS;
1146 goto out;
1147 }
1148 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1149 if (!x->calg)
1150 goto out;
1151 strcpy(x->calg->alg_name, a->name);
1152 x->props.calgo = sa->sadb_sa_encrypt;
1153 } else {
1154 int keysize = 0;
1155 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1156 if (!a) {
1157 err = -ENOSYS;
1158 goto out;
1159 }
1160 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1161 if (key)
1162 keysize = (key->sadb_key_bits + 7) / 8;
1163 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1164 if (!x->ealg)
1165 goto out;
1166 strcpy(x->ealg->alg_name, a->name);
1167 x->ealg->alg_key_len = 0;
1168 if (key) {
1169 x->ealg->alg_key_len = key->sadb_key_bits;
1170 memcpy(x->ealg->alg_key, key+1, keysize);
1171 }
1172 x->props.ealgo = sa->sadb_sa_encrypt;
1173 }
1174 }
1175 /* x->algo.flags = sa->sadb_sa_flags; */
1176
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001177 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 &x->props.saddr);
1179 if (!x->props.family) {
1180 err = -EAFNOSUPPORT;
1181 goto out;
1182 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001183 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 &x->id.daddr);
1185
1186 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1187 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001188 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1189 if (mode < 0) {
1190 err = -EINVAL;
1191 goto out;
1192 }
1193 x->props.mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 x->props.reqid = sa2->sadb_x_sa2_reqid;
1195 }
1196
1197 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1198 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1199
1200 /* Nobody uses this, but we try. */
1201 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1202 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1203 }
1204
Joy Latten4a4b6272007-08-02 19:25:43 -07001205 if (!x->sel.family)
1206 x->sel.family = x->props.family;
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1209 struct sadb_x_nat_t_type* n_type;
1210 struct xfrm_encap_tmpl *natt;
1211
1212 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1213 if (!x->encap)
1214 goto out;
1215
1216 natt = x->encap;
1217 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1218 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1219
1220 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1221 struct sadb_x_nat_t_port* n_port =
1222 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1223 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1224 }
1225 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1226 struct sadb_x_nat_t_port* n_port =
1227 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1228 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1229 }
1230 }
1231
Herbert Xu72cb6962005-06-20 13:18:08 -07001232 err = xfrm_init_state(x);
1233 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return x;
1238
1239out:
1240 x->km.state = XFRM_STATE_DEAD;
1241 xfrm_state_put(x);
1242 return ERR_PTR(err);
1243}
1244
1245static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1246{
1247 return -EOPNOTSUPP;
1248}
1249
1250static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1251{
1252 struct sk_buff *resp_skb;
1253 struct sadb_x_sa2 *sa2;
1254 struct sadb_address *saddr, *daddr;
1255 struct sadb_msg *out_hdr;
1256 struct xfrm_state *x = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001257 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 u32 reqid;
1259 u8 proto;
1260 unsigned short family;
1261 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1262
1263 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1264 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1265 return -EINVAL;
1266
1267 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1268 if (proto == 0)
1269 return -EINVAL;
1270
1271 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001272 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1273 if (mode < 0)
1274 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 reqid = sa2->sadb_x_sa2_reqid;
1276 } else {
1277 mode = 0;
1278 reqid = 0;
1279 }
1280
1281 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1282 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1283
1284 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1285 switch (family) {
1286 case AF_INET:
1287 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1288 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1289 break;
1290#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1291 case AF_INET6:
1292 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1293 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1294 break;
1295#endif
1296 }
1297
1298 if (hdr->sadb_msg_seq) {
1299 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1300 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1301 xfrm_state_put(x);
1302 x = NULL;
1303 }
1304 }
1305
1306 if (!x)
1307 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1308
1309 if (x == NULL)
1310 return -ENOENT;
1311
1312 resp_skb = ERR_PTR(-ENOENT);
1313
1314 spin_lock_bh(&x->lock);
1315 if (x->km.state != XFRM_STATE_DEAD) {
1316 struct sadb_spirange *range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1317 u32 min_spi, max_spi;
1318
1319 if (range != NULL) {
1320 min_spi = range->sadb_spirange_min;
1321 max_spi = range->sadb_spirange_max;
1322 } else {
1323 min_spi = 0x100;
1324 max_spi = 0x0fffffff;
1325 }
1326 xfrm_alloc_spi(x, htonl(min_spi), htonl(max_spi));
1327 if (x->id.spi)
1328 resp_skb = pfkey_xfrm_state2msg(x, 0, 3);
1329 }
1330 spin_unlock_bh(&x->lock);
1331
1332 if (IS_ERR(resp_skb)) {
1333 xfrm_state_put(x);
1334 return PTR_ERR(resp_skb);
1335 }
1336
1337 out_hdr = (struct sadb_msg *) resp_skb->data;
1338 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1339 out_hdr->sadb_msg_type = SADB_GETSPI;
1340 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1341 out_hdr->sadb_msg_errno = 0;
1342 out_hdr->sadb_msg_reserved = 0;
1343 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1344 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1345
1346 xfrm_state_put(x);
1347
1348 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1349
1350 return 0;
1351}
1352
1353static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1354{
1355 struct xfrm_state *x;
1356
1357 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1358 return -EOPNOTSUPP;
1359
1360 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1361 return 0;
1362
1363 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1364 if (x == NULL)
1365 return 0;
1366
1367 spin_lock_bh(&x->lock);
1368 if (x->km.state == XFRM_STATE_ACQ) {
1369 x->km.state = XFRM_STATE_ERROR;
1370 wake_up(&km_waitq);
1371 }
1372 spin_unlock_bh(&x->lock);
1373 xfrm_state_put(x);
1374 return 0;
1375}
1376
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001377static inline int event2poltype(int event)
1378{
1379 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001380 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001381 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001382 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001383 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001384 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001385 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001386 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001387 // return SADB_X_SPDEXPIRE;
1388 default:
1389 printk("pfkey: Unknown policy event %d\n", event);
1390 break;
1391 }
1392
1393 return 0;
1394}
1395
1396static inline int event2keytype(int event)
1397{
1398 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001399 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001400 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001401 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001402 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001403 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001404 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001405 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001406 return SADB_EXPIRE;
1407 default:
1408 printk("pfkey: Unknown SA event %d\n", event);
1409 break;
1410 }
1411
1412 return 0;
1413}
1414
1415/* ADD/UPD/DEL */
1416static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1417{
1418 struct sk_buff *skb;
1419 struct sadb_msg *hdr;
1420 int hsc = 3;
1421
Herbert Xuf60f6b82005-06-18 22:44:37 -07001422 if (c->event == XFRM_MSG_DELSA)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001423 hsc = 0;
1424
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001425 skb = pfkey_xfrm_state2msg(x, 0, hsc);
1426
1427 if (IS_ERR(skb))
1428 return PTR_ERR(skb);
1429
1430 hdr = (struct sadb_msg *) skb->data;
1431 hdr->sadb_msg_version = PF_KEY_V2;
1432 hdr->sadb_msg_type = event2keytype(c->event);
1433 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1434 hdr->sadb_msg_errno = 0;
1435 hdr->sadb_msg_reserved = 0;
1436 hdr->sadb_msg_seq = c->seq;
1437 hdr->sadb_msg_pid = c->pid;
1438
1439 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1440
1441 return 0;
1442}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1445{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 struct xfrm_state *x;
1447 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001448 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1451 if (IS_ERR(x))
1452 return PTR_ERR(x);
1453
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001454 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (hdr->sadb_msg_type == SADB_ADD)
1456 err = xfrm_state_add(x);
1457 else
1458 err = xfrm_state_update(x);
1459
Joy Lattenab5f5e82007-09-17 11:51:22 -07001460 xfrm_audit_state_add(x, err ? 0 : 1,
1461 audit_get_loginuid(current->audit_context), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (err < 0) {
1464 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001465 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001466 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001469 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001470 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001471 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001472 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001473 c.seq = hdr->sadb_msg_seq;
1474 c.pid = hdr->sadb_msg_pid;
1475 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001476out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001477 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001478 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
1481static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1482{
1483 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001484 struct km_event c;
1485 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 if (!ext_hdrs[SADB_EXT_SA-1] ||
1488 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1489 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1490 return -EINVAL;
1491
1492 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1493 if (x == NULL)
1494 return -ESRCH;
1495
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001496 if ((err = security_xfrm_state_delete(x)))
1497 goto out;
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001500 err = -EPERM;
1501 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
Joy Latten161a09e2006-11-27 13:11:54 -06001503
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001504 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001505
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001506 if (err < 0)
1507 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001508
1509 c.seq = hdr->sadb_msg_seq;
1510 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001511 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001512 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001513out:
Joy Lattenab5f5e82007-09-17 11:51:22 -07001514 xfrm_audit_state_delete(x, err ? 0 : 1,
1515 audit_get_loginuid(current->audit_context), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 xfrm_state_put(x);
1517
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001518 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
1521static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1522{
1523 __u8 proto;
1524 struct sk_buff *out_skb;
1525 struct sadb_msg *out_hdr;
1526 struct xfrm_state *x;
1527
1528 if (!ext_hdrs[SADB_EXT_SA-1] ||
1529 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1530 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1531 return -EINVAL;
1532
1533 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1534 if (x == NULL)
1535 return -ESRCH;
1536
1537 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1538 proto = x->id.proto;
1539 xfrm_state_put(x);
1540 if (IS_ERR(out_skb))
1541 return PTR_ERR(out_skb);
1542
1543 out_hdr = (struct sadb_msg *) out_skb->data;
1544 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1545 out_hdr->sadb_msg_type = SADB_DUMP;
1546 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1547 out_hdr->sadb_msg_errno = 0;
1548 out_hdr->sadb_msg_reserved = 0;
1549 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1550 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1551 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1552
1553 return 0;
1554}
1555
Randy Dunlap00fa0232005-10-04 22:43:04 -07001556static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001557 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
1559 struct sk_buff *skb;
1560 struct sadb_msg *hdr;
1561 int len, auth_len, enc_len, i;
1562
1563 auth_len = xfrm_count_auth_supported();
1564 if (auth_len) {
1565 auth_len *= sizeof(struct sadb_alg);
1566 auth_len += sizeof(struct sadb_supported);
1567 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 enc_len = xfrm_count_enc_supported();
1570 if (enc_len) {
1571 enc_len *= sizeof(struct sadb_alg);
1572 enc_len += sizeof(struct sadb_supported);
1573 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 len = enc_len + auth_len + sizeof(struct sadb_msg);
1576
1577 skb = alloc_skb(len + 16, allocation);
1578 if (!skb)
1579 goto out_put_algs;
1580
1581 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1582 pfkey_hdr_dup(hdr, orig);
1583 hdr->sadb_msg_errno = 0;
1584 hdr->sadb_msg_len = len / sizeof(uint64_t);
1585
1586 if (auth_len) {
1587 struct sadb_supported *sp;
1588 struct sadb_alg *ap;
1589
1590 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1591 ap = (struct sadb_alg *) (sp + 1);
1592
1593 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1594 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1595
1596 for (i = 0; ; i++) {
1597 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1598 if (!aalg)
1599 break;
1600 if (aalg->available)
1601 *ap++ = aalg->desc;
1602 }
1603 }
1604
1605 if (enc_len) {
1606 struct sadb_supported *sp;
1607 struct sadb_alg *ap;
1608
1609 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1610 ap = (struct sadb_alg *) (sp + 1);
1611
1612 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1613 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1614
1615 for (i = 0; ; i++) {
1616 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1617 if (!ealg)
1618 break;
1619 if (ealg->available)
1620 *ap++ = ealg->desc;
1621 }
1622 }
1623
1624out_put_algs:
1625 return skb;
1626}
1627
1628static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1629{
1630 struct pfkey_sock *pfk = pfkey_sk(sk);
1631 struct sk_buff *supp_skb;
1632
1633 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1634 return -EINVAL;
1635
1636 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1637 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1638 return -EEXIST;
1639 pfk->registered |= (1<<hdr->sadb_msg_satype);
1640 }
1641
1642 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1645 if (!supp_skb) {
1646 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1647 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1648
1649 return -ENOBUFS;
1650 }
1651
1652 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1653
1654 return 0;
1655}
1656
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001657static int key_notify_sa_flush(struct km_event *c)
1658{
1659 struct sk_buff *skb;
1660 struct sadb_msg *hdr;
1661
1662 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1663 if (!skb)
1664 return -ENOBUFS;
1665 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf088672005-06-18 22:44:00 -07001666 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001667 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001668 hdr->sadb_msg_seq = c->seq;
1669 hdr->sadb_msg_pid = c->pid;
1670 hdr->sadb_msg_version = PF_KEY_V2;
1671 hdr->sadb_msg_errno = (uint8_t) 0;
1672 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1673
1674 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1675
1676 return 0;
1677}
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1680{
1681 unsigned proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001682 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06001683 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001684 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1687 if (proto == 0)
1688 return -EINVAL;
1689
Joy Latten161a09e2006-11-27 13:11:54 -06001690 audit_info.loginuid = audit_get_loginuid(current->audit_context);
1691 audit_info.secid = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -04001692 err = xfrm_state_flush(proto, &audit_info);
1693 if (err)
1694 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001695 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001696 c.seq = hdr->sadb_msg_seq;
1697 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001698 c.event = XFRM_MSG_FLUSHSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001699 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 return 0;
1702}
1703
1704struct pfkey_dump_data
1705{
1706 struct sk_buff *skb;
1707 struct sadb_msg *hdr;
1708 struct sock *sk;
1709};
1710
1711static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1712{
1713 struct pfkey_dump_data *data = ptr;
1714 struct sk_buff *out_skb;
1715 struct sadb_msg *out_hdr;
1716
1717 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1718 if (IS_ERR(out_skb))
1719 return PTR_ERR(out_skb);
1720
1721 out_hdr = (struct sadb_msg *) out_skb->data;
1722 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
1723 out_hdr->sadb_msg_type = SADB_DUMP;
1724 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1725 out_hdr->sadb_msg_errno = 0;
1726 out_hdr->sadb_msg_reserved = 0;
1727 out_hdr->sadb_msg_seq = count;
1728 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
1729 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
1730 return 0;
1731}
1732
1733static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1734{
1735 u8 proto;
1736 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
1737
1738 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1739 if (proto == 0)
1740 return -EINVAL;
1741
1742 return xfrm_state_walk(proto, dump_sa, &data);
1743}
1744
1745static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1746{
1747 struct pfkey_sock *pfk = pfkey_sk(sk);
1748 int satype = hdr->sadb_msg_satype;
1749
1750 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1751 /* XXX we mangle packet... */
1752 hdr->sadb_msg_errno = 0;
1753 if (satype != 0 && satype != 1)
1754 return -EINVAL;
1755 pfk->promisc = satype;
1756 }
1757 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1758 return 0;
1759}
1760
1761static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1762{
1763 int i;
1764 u32 reqid = *(u32*)ptr;
1765
1766 for (i=0; i<xp->xfrm_nr; i++) {
1767 if (xp->xfrm_vec[i].reqid == reqid)
1768 return -EEXIST;
1769 }
1770 return 0;
1771}
1772
1773static u32 gen_reqid(void)
1774{
1775 u32 start;
1776 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1777
1778 start = reqid;
1779 do {
1780 ++reqid;
1781 if (reqid == 0)
1782 reqid = IPSEC_MANUAL_REQID_MAX+1;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001783 if (xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, check_reqid,
1784 (void*)&reqid) != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return reqid;
1786 } while (reqid != start);
1787 return 0;
1788}
1789
1790static int
1791parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1792{
1793 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1794 struct sockaddr_in *sin;
1795#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1796 struct sockaddr_in6 *sin6;
1797#endif
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001798 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1801 return -ELOOP;
1802
1803 if (rq->sadb_x_ipsecrequest_mode == 0)
1804 return -EINVAL;
1805
1806 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001807 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1808 return -EINVAL;
1809 t->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1811 t->optional = 1;
1812 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1813 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1814 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1815 t->reqid = 0;
1816 if (!t->reqid && !(t->reqid = gen_reqid()))
1817 return -ENOBUFS;
1818 }
1819
1820 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001821 if (t->mode == XFRM_MODE_TUNNEL) {
Miika Komu2718aa72006-11-30 16:41:50 -08001822 struct sockaddr *sa;
1823 sa = (struct sockaddr *)(rq+1);
1824 switch(sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 case AF_INET:
Miika Komu2718aa72006-11-30 16:41:50 -08001826 sin = (struct sockaddr_in*)sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 t->saddr.a4 = sin->sin_addr.s_addr;
1828 sin++;
1829 if (sin->sin_family != AF_INET)
1830 return -EINVAL;
1831 t->id.daddr.a4 = sin->sin_addr.s_addr;
1832 break;
1833#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1834 case AF_INET6:
Miika Komu2718aa72006-11-30 16:41:50 -08001835 sin6 = (struct sockaddr_in6*)sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1837 sin6++;
1838 if (sin6->sin6_family != AF_INET6)
1839 return -EINVAL;
1840 memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1841 break;
1842#endif
1843 default:
1844 return -EINVAL;
1845 }
Miika Komu2718aa72006-11-30 16:41:50 -08001846 t->encap_family = sa->sa_family;
1847 } else
1848 t->encap_family = xp->family;
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /* No way to set this via kame pfkey */
1851 t->aalgos = t->ealgos = t->calgos = ~0;
1852 xp->xfrm_nr++;
1853 return 0;
1854}
1855
1856static int
1857parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1858{
1859 int err;
1860 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1861 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1862
1863 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1864 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1865 return err;
1866 len -= rq->sadb_x_ipsecrequest_len;
1867 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1868 }
1869 return 0;
1870}
1871
Trent Jaegerdf718372005-12-13 23:12:27 -08001872static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1873{
1874 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1875
1876 if (xfrm_ctx) {
1877 int len = sizeof(struct sadb_x_sec_ctx);
1878 len += xfrm_ctx->ctx_len;
1879 return PFKEY_ALIGN8(len);
1880 }
1881 return 0;
1882}
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1885{
Miika Komu2718aa72006-11-30 16:41:50 -08001886 struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08001888 int socklen = 0;
1889 int i;
1890
1891 for (i=0; i<xp->xfrm_nr; i++) {
1892 t = xp->xfrm_vec + i;
1893 socklen += (t->encap_family == AF_INET ?
1894 sizeof(struct sockaddr_in) :
1895 sizeof(struct sockaddr_in6));
1896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 return sizeof(struct sadb_msg) +
1899 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001900 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 (sockaddr_size * 2) +
1902 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08001903 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1904 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08001905 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
1908static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1909{
1910 struct sk_buff *skb;
1911 int size;
1912
1913 size = pfkey_xfrm_policy2msg_size(xp);
1914
1915 skb = alloc_skb(size + 16, GFP_ATOMIC);
1916 if (skb == NULL)
1917 return ERR_PTR(-ENOBUFS);
1918
1919 return skb;
1920}
1921
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001922static int pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 struct sadb_msg *hdr;
1925 struct sadb_address *addr;
1926 struct sadb_lifetime *lifetime;
1927 struct sadb_x_policy *pol;
1928 struct sockaddr_in *sin;
Trent Jaegerdf718372005-12-13 23:12:27 -08001929 struct sadb_x_sec_ctx *sec_ctx;
1930 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1932 struct sockaddr_in6 *sin6;
1933#endif
1934 int i;
1935 int size;
1936 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1937 int socklen = (xp->family == AF_INET ?
1938 sizeof(struct sockaddr_in) :
1939 sizeof(struct sockaddr_in6));
1940
1941 size = pfkey_xfrm_policy2msg_size(xp);
1942
1943 /* call should fill header later */
1944 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1945 memset(hdr, 0, size); /* XXX do we need this ? */
1946
1947 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001948 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001950 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 (sizeof(struct sadb_address)+sockaddr_size)/
1952 sizeof(uint64_t);
1953 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1954 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1955 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1956 addr->sadb_address_reserved = 0;
1957 /* src address */
1958 if (xp->family == AF_INET) {
1959 sin = (struct sockaddr_in *) (addr + 1);
1960 sin->sin_family = AF_INET;
1961 sin->sin_addr.s_addr = xp->selector.saddr.a4;
1962 sin->sin_port = xp->selector.sport;
1963 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1964 }
1965#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1966 else if (xp->family == AF_INET6) {
1967 sin6 = (struct sockaddr_in6 *) (addr + 1);
1968 sin6->sin6_family = AF_INET6;
1969 sin6->sin6_port = xp->selector.sport;
1970 sin6->sin6_flowinfo = 0;
1971 memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
1972 sizeof(struct in6_addr));
1973 sin6->sin6_scope_id = 0;
1974 }
1975#endif
1976 else
1977 BUG();
1978
1979 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001980 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 sizeof(struct sadb_address)+sockaddr_size);
1982 addr->sadb_address_len =
1983 (sizeof(struct sadb_address)+sockaddr_size)/
1984 sizeof(uint64_t);
1985 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1986 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001987 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 addr->sadb_address_reserved = 0;
1989 if (xp->family == AF_INET) {
1990 sin = (struct sockaddr_in *) (addr + 1);
1991 sin->sin_family = AF_INET;
1992 sin->sin_addr.s_addr = xp->selector.daddr.a4;
1993 sin->sin_port = xp->selector.dport;
1994 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1995 }
1996#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1997 else if (xp->family == AF_INET6) {
1998 sin6 = (struct sockaddr_in6 *) (addr + 1);
1999 sin6->sin6_family = AF_INET6;
2000 sin6->sin6_port = xp->selector.dport;
2001 sin6->sin6_flowinfo = 0;
2002 memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
2003 sizeof(struct in6_addr));
2004 sin6->sin6_scope_id = 0;
2005 }
2006#endif
2007 else
2008 BUG();
2009
2010 /* hard time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002011 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 sizeof(struct sadb_lifetime));
2013 lifetime->sadb_lifetime_len =
2014 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2015 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2016 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2017 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2018 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2019 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2020 /* soft time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002021 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 sizeof(struct sadb_lifetime));
2023 lifetime->sadb_lifetime_len =
2024 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2025 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2026 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2027 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2028 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2029 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2030 /* current time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002031 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 sizeof(struct sadb_lifetime));
2033 lifetime->sadb_lifetime_len =
2034 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2035 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2036 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2037 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2038 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2039 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2040
2041 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2042 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2043 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2044 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2045 if (xp->action == XFRM_POLICY_ALLOW) {
2046 if (xp->xfrm_nr)
2047 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2048 else
2049 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2050 }
2051 pol->sadb_x_policy_dir = dir+1;
2052 pol->sadb_x_policy_id = xp->index;
2053 pol->sadb_x_policy_priority = xp->priority;
2054
2055 for (i=0; i<xp->xfrm_nr; i++) {
2056 struct sadb_x_ipsecrequest *rq;
2057 struct xfrm_tmpl *t = xp->xfrm_vec + i;
2058 int req_size;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002059 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 req_size = sizeof(struct sadb_x_ipsecrequest);
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002062 if (t->mode == XFRM_MODE_TUNNEL)
Miika Komu2718aa72006-11-30 16:41:50 -08002063 req_size += ((t->encap_family == AF_INET ?
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002064 sizeof(struct sockaddr_in) :
2065 sizeof(struct sockaddr_in6)) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 else
2067 size -= 2*socklen;
2068 rq = (void*)skb_put(skb, req_size);
2069 pol->sadb_x_policy_len += req_size/8;
2070 memset(rq, 0, sizeof(*rq));
2071 rq->sadb_x_ipsecrequest_len = req_size;
2072 rq->sadb_x_ipsecrequest_proto = t->id.proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002073 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2074 return -EINVAL;
David S. Millerfefaa752007-04-17 21:48:10 -07002075 rq->sadb_x_ipsecrequest_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2077 if (t->reqid)
2078 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2079 if (t->optional)
2080 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2081 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002082 if (t->mode == XFRM_MODE_TUNNEL) {
Miika Komu2718aa72006-11-30 16:41:50 -08002083 switch (t->encap_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 case AF_INET:
2085 sin = (void*)(rq+1);
2086 sin->sin_family = AF_INET;
2087 sin->sin_addr.s_addr = t->saddr.a4;
2088 sin->sin_port = 0;
2089 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2090 sin++;
2091 sin->sin_family = AF_INET;
2092 sin->sin_addr.s_addr = t->id.daddr.a4;
2093 sin->sin_port = 0;
2094 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2095 break;
2096#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2097 case AF_INET6:
2098 sin6 = (void*)(rq+1);
2099 sin6->sin6_family = AF_INET6;
2100 sin6->sin6_port = 0;
2101 sin6->sin6_flowinfo = 0;
2102 memcpy(&sin6->sin6_addr, t->saddr.a6,
2103 sizeof(struct in6_addr));
2104 sin6->sin6_scope_id = 0;
2105
2106 sin6++;
2107 sin6->sin6_family = AF_INET6;
2108 sin6->sin6_port = 0;
2109 sin6->sin6_flowinfo = 0;
2110 memcpy(&sin6->sin6_addr, t->id.daddr.a6,
2111 sizeof(struct in6_addr));
2112 sin6->sin6_scope_id = 0;
2113 break;
2114#endif
2115 default:
2116 break;
2117 }
2118 }
2119 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002120
2121 /* security context */
2122 if ((xfrm_ctx = xp->security)) {
2123 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2124
2125 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2126 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2127 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2128 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2129 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2130 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2131 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2132 xfrm_ctx->ctx_len);
2133 }
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 hdr->sadb_msg_len = size / sizeof(uint64_t);
2136 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002137
2138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002141static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2142{
2143 struct sk_buff *out_skb;
2144 struct sadb_msg *out_hdr;
2145 int err;
2146
2147 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2148 if (IS_ERR(out_skb)) {
2149 err = PTR_ERR(out_skb);
2150 goto out;
2151 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002152 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2153 if (err < 0)
2154 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002155
2156 out_hdr = (struct sadb_msg *) out_skb->data;
2157 out_hdr->sadb_msg_version = PF_KEY_V2;
2158
Herbert Xuf60f6b82005-06-18 22:44:37 -07002159 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002160 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2161 else
2162 out_hdr->sadb_msg_type = event2poltype(c->event);
2163 out_hdr->sadb_msg_errno = 0;
2164 out_hdr->sadb_msg_seq = c->seq;
2165 out_hdr->sadb_msg_pid = c->pid;
2166 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
2167out:
2168 return 0;
2169
2170}
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2173{
Trent Jaegerdf718372005-12-13 23:12:27 -08002174 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 struct sadb_lifetime *lifetime;
2176 struct sadb_address *sa;
2177 struct sadb_x_policy *pol;
2178 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002179 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002180 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2183 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2184 !ext_hdrs[SADB_X_EXT_POLICY-1])
2185 return -EINVAL;
2186
2187 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2188 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2189 return -EINVAL;
2190 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2191 return -EINVAL;
2192
2193 xp = xfrm_policy_alloc(GFP_KERNEL);
2194 if (xp == NULL)
2195 return -ENOBUFS;
2196
2197 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2198 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2199 xp->priority = pol->sadb_x_policy_priority;
2200
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002201 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2203 if (!xp->family) {
2204 err = -EINVAL;
2205 goto out;
2206 }
2207 xp->selector.family = xp->family;
2208 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2209 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2210 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2211 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002212 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002214 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2216 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2217
2218 /* Amusing, we set this twice. KAME apps appear to set same value
2219 * in both addresses.
2220 */
2221 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2222
2223 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2224 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002225 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Trent Jaegerdf718372005-12-13 23:12:27 -08002227 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2228 if (sec_ctx != NULL) {
2229 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2230
2231 if (!uctx) {
2232 err = -ENOBUFS;
2233 goto out;
2234 }
2235
2236 err = security_xfrm_policy_alloc(xp, uctx);
2237 kfree(uctx);
2238
2239 if (err)
2240 goto out;
2241 }
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 xp->lft.soft_byte_limit = XFRM_INF;
2244 xp->lft.hard_byte_limit = XFRM_INF;
2245 xp->lft.soft_packet_limit = XFRM_INF;
2246 xp->lft.hard_packet_limit = XFRM_INF;
2247 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2248 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2249 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2250 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2251 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2252 }
2253 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2254 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2255 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2256 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2257 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2258 }
2259 xp->xfrm_nr = 0;
2260 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2261 (err = parse_ipsecrequests(xp, pol)) < 0)
2262 goto out;
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2265 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002266
Joy Lattenab5f5e82007-09-17 11:51:22 -07002267 xfrm_audit_policy_add(xp, err ? 0 : 1,
2268 audit_get_loginuid(current->audit_context), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06002269
Trent Jaegerdf718372005-12-13 23:12:27 -08002270 if (err)
2271 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002273 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002274 c.event = XFRM_MSG_UPDPOLICY;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002275 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07002276 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002278 c.seq = hdr->sadb_msg_seq;
2279 c.pid = hdr->sadb_msg_pid;
2280
2281 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return 0;
2284
2285out:
Trent Jaegerdf718372005-12-13 23:12:27 -08002286 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 kfree(xp);
2288 return err;
2289}
2290
2291static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2292{
2293 int err;
2294 struct sadb_address *sa;
2295 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08002296 struct xfrm_policy *xp, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002298 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002299 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
2301 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2302 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2303 !ext_hdrs[SADB_X_EXT_POLICY-1])
2304 return -EINVAL;
2305
2306 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2307 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2308 return -EINVAL;
2309
2310 memset(&sel, 0, sizeof(sel));
2311
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002312 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2314 sel.prefixlen_s = sa->sadb_address_prefixlen;
2315 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2316 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2317 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002318 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002320 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2322 sel.prefixlen_d = sa->sadb_address_prefixlen;
2323 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2324 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2325 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002326 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Trent Jaegerdf718372005-12-13 23:12:27 -08002328 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2329 memset(&tmp, 0, sizeof(struct xfrm_policy));
2330
2331 if (sec_ctx != NULL) {
2332 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2333
2334 if (!uctx)
2335 return -ENOMEM;
2336
2337 err = security_xfrm_policy_alloc(&tmp, uctx);
2338 kfree(uctx);
2339
2340 if (err)
2341 return err;
2342 }
2343
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002344 xp = xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN, pol->sadb_x_policy_dir-1,
Eric Parisef41aaa2007-03-07 15:37:58 -08002345 &sel, tmp.security, 1, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08002346 security_xfrm_policy_free(&tmp);
Joy Latten161a09e2006-11-27 13:11:54 -06002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (xp == NULL)
2349 return -ENOENT;
2350
Joy Lattenab5f5e82007-09-17 11:51:22 -07002351 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2352 audit_get_loginuid(current->audit_context), 0);
David S. Miller13fcfbb2007-02-12 13:53:54 -08002353
2354 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002355 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08002356
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002357 c.seq = hdr->sadb_msg_seq;
2358 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002359 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002360 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2361
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002362out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002363 xfrm_pol_put(xp);
2364 return err;
2365}
2366
2367static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2368{
2369 int err;
2370 struct sk_buff *out_skb;
2371 struct sadb_msg *out_hdr;
2372 err = 0;
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2375 if (IS_ERR(out_skb)) {
2376 err = PTR_ERR(out_skb);
2377 goto out;
2378 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002379 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2380 if (err < 0)
2381 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 out_hdr = (struct sadb_msg *) out_skb->data;
2384 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002385 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 out_hdr->sadb_msg_satype = 0;
2387 out_hdr->sadb_msg_errno = 0;
2388 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2389 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002390 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 err = 0;
2392
2393out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return err;
2395}
2396
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002397#ifdef CONFIG_NET_KEY_MIGRATE
2398static int pfkey_sockaddr_pair_size(sa_family_t family)
2399{
2400 switch (family) {
2401 case AF_INET:
2402 return PFKEY_ALIGN8(sizeof(struct sockaddr_in) * 2);
2403#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2404 case AF_INET6:
2405 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6) * 2);
2406#endif
2407 default:
2408 return 0;
2409 }
2410 /* NOTREACHED */
2411}
2412
2413static int parse_sockaddr_pair(struct sadb_x_ipsecrequest *rq,
2414 xfrm_address_t *saddr, xfrm_address_t *daddr,
2415 u16 *family)
2416{
2417 struct sockaddr *sa = (struct sockaddr *)(rq + 1);
2418 if (rq->sadb_x_ipsecrequest_len <
2419 pfkey_sockaddr_pair_size(sa->sa_family))
2420 return -EINVAL;
2421
2422 switch (sa->sa_family) {
2423 case AF_INET:
2424 {
2425 struct sockaddr_in *sin;
2426 sin = (struct sockaddr_in *)sa;
2427 if ((sin+1)->sin_family != AF_INET)
2428 return -EINVAL;
2429 memcpy(&saddr->a4, &sin->sin_addr, sizeof(saddr->a4));
2430 sin++;
2431 memcpy(&daddr->a4, &sin->sin_addr, sizeof(daddr->a4));
2432 *family = AF_INET;
2433 break;
2434 }
2435#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2436 case AF_INET6:
2437 {
2438 struct sockaddr_in6 *sin6;
2439 sin6 = (struct sockaddr_in6 *)sa;
2440 if ((sin6+1)->sin6_family != AF_INET6)
2441 return -EINVAL;
2442 memcpy(&saddr->a6, &sin6->sin6_addr,
2443 sizeof(saddr->a6));
2444 sin6++;
2445 memcpy(&daddr->a6, &sin6->sin6_addr,
2446 sizeof(daddr->a6));
2447 *family = AF_INET6;
2448 break;
2449 }
2450#endif
2451 default:
2452 return -EINVAL;
2453 }
2454
2455 return 0;
2456}
2457
2458static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2459 struct xfrm_migrate *m)
2460{
2461 int err;
2462 struct sadb_x_ipsecrequest *rq2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002463 int mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002464
2465 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2466 len < rq1->sadb_x_ipsecrequest_len)
2467 return -EINVAL;
2468
2469 /* old endoints */
2470 err = parse_sockaddr_pair(rq1, &m->old_saddr, &m->old_daddr,
2471 &m->old_family);
2472 if (err)
2473 return err;
2474
2475 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2476 len -= rq1->sadb_x_ipsecrequest_len;
2477
2478 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2479 len < rq2->sadb_x_ipsecrequest_len)
2480 return -EINVAL;
2481
2482 /* new endpoints */
2483 err = parse_sockaddr_pair(rq2, &m->new_saddr, &m->new_daddr,
2484 &m->new_family);
2485 if (err)
2486 return err;
2487
2488 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2489 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2490 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2491 return -EINVAL;
2492
2493 m->proto = rq1->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002494 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2495 return -EINVAL;
2496 m->mode = mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002497 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2498
2499 return ((int)(rq1->sadb_x_ipsecrequest_len +
2500 rq2->sadb_x_ipsecrequest_len));
2501}
2502
2503static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2504 struct sadb_msg *hdr, void **ext_hdrs)
2505{
2506 int i, len, ret, err = -EINVAL;
2507 u8 dir;
2508 struct sadb_address *sa;
2509 struct sadb_x_policy *pol;
2510 struct sadb_x_ipsecrequest *rq;
2511 struct xfrm_selector sel;
2512 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2513
2514 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2515 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2516 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2517 err = -EINVAL;
2518 goto out;
2519 }
2520
2521 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2522 if (!pol) {
2523 err = -EINVAL;
2524 goto out;
2525 }
2526
2527 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2528 err = -EINVAL;
2529 goto out;
2530 }
2531
2532 dir = pol->sadb_x_policy_dir - 1;
2533 memset(&sel, 0, sizeof(sel));
2534
2535 /* set source address info of selector */
2536 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2537 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2538 sel.prefixlen_s = sa->sadb_address_prefixlen;
2539 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2540 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2541 if (sel.sport)
Al Viro582ee432007-07-26 17:33:39 +01002542 sel.sport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002543
2544 /* set destination address info of selector */
2545 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2546 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2547 sel.prefixlen_d = sa->sadb_address_prefixlen;
2548 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2549 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2550 if (sel.dport)
Al Viro582ee432007-07-26 17:33:39 +01002551 sel.dport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002552
2553 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2554
2555 /* extract ipsecrequests */
2556 i = 0;
2557 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2558
2559 while (len > 0 && i < XFRM_MAX_DEPTH) {
2560 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2561 if (ret < 0) {
2562 err = ret;
2563 goto out;
2564 } else {
2565 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2566 len -= ret;
2567 i++;
2568 }
2569 }
2570
2571 if (!i || len > 0) {
2572 err = -EINVAL;
2573 goto out;
2574 }
2575
2576 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i);
2577
2578 out:
2579 return err;
2580}
2581#else
2582static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2583 struct sadb_msg *hdr, void **ext_hdrs)
2584{
2585 return -ENOPROTOOPT;
2586}
2587#endif
2588
2589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2591{
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002592 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002593 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 struct sadb_x_policy *pol;
2595 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002596 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2599 return -EINVAL;
2600
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002601 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2602 if (dir >= XFRM_POLICY_MAX)
2603 return -EINVAL;
2604
Eric Paris215a2dd2007-03-07 16:01:45 -08002605 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002606 xp = xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN, dir, pol->sadb_x_policy_id,
Eric Paris215a2dd2007-03-07 16:01:45 -08002607 delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 if (xp == NULL)
2609 return -ENOENT;
2610
Eric Paris215a2dd2007-03-07 16:01:45 -08002611 if (delete) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002612 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2613 audit_get_loginuid(current->audit_context), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Eric Paris215a2dd2007-03-07 16:01:45 -08002615 if (err)
2616 goto out;
2617 c.seq = hdr->sadb_msg_seq;
2618 c.pid = hdr->sadb_msg_pid;
Herbert Xubf088672005-06-18 22:44:00 -07002619 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002620 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002621 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002622 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002623 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Eric Paris215a2dd2007-03-07 16:01:45 -08002626out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 xfrm_pol_put(xp);
2628 return err;
2629}
2630
2631static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2632{
2633 struct pfkey_dump_data *data = ptr;
2634 struct sk_buff *out_skb;
2635 struct sadb_msg *out_hdr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002636 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2639 if (IS_ERR(out_skb))
2640 return PTR_ERR(out_skb);
2641
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002642 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2643 if (err < 0)
2644 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 out_hdr = (struct sadb_msg *) out_skb->data;
2647 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
2648 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2649 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2650 out_hdr->sadb_msg_errno = 0;
2651 out_hdr->sadb_msg_seq = count;
2652 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
2653 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
2654 return 0;
2655}
2656
2657static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2658{
2659 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
2660
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002661 return xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_sp, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002664static int key_notify_policy_flush(struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
2666 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002667 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002669 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (!skb_out)
2671 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002672 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002673 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002674 hdr->sadb_msg_seq = c->seq;
2675 hdr->sadb_msg_pid = c->pid;
2676 hdr->sadb_msg_version = PF_KEY_V2;
2677 hdr->sadb_msg_errno = (uint8_t) 0;
2678 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2679 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2680 return 0;
2681
2682}
2683
2684static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2685{
2686 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06002687 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04002688 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Joy Latten161a09e2006-11-27 13:11:54 -06002690 audit_info.loginuid = audit_get_loginuid(current->audit_context);
2691 audit_info.secid = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -04002692 err = xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN, &audit_info);
2693 if (err)
2694 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002695 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002696 c.event = XFRM_MSG_FLUSHPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002697 c.pid = hdr->sadb_msg_pid;
2698 c.seq = hdr->sadb_msg_seq;
2699 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 return 0;
2702}
2703
2704typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2705 struct sadb_msg *hdr, void **ext_hdrs);
2706static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2707 [SADB_RESERVED] = pfkey_reserved,
2708 [SADB_GETSPI] = pfkey_getspi,
2709 [SADB_UPDATE] = pfkey_add,
2710 [SADB_ADD] = pfkey_add,
2711 [SADB_DELETE] = pfkey_delete,
2712 [SADB_GET] = pfkey_get,
2713 [SADB_ACQUIRE] = pfkey_acquire,
2714 [SADB_REGISTER] = pfkey_register,
2715 [SADB_EXPIRE] = NULL,
2716 [SADB_FLUSH] = pfkey_flush,
2717 [SADB_DUMP] = pfkey_dump,
2718 [SADB_X_PROMISC] = pfkey_promisc,
2719 [SADB_X_PCHANGE] = NULL,
2720 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2721 [SADB_X_SPDADD] = pfkey_spdadd,
2722 [SADB_X_SPDDELETE] = pfkey_spddelete,
2723 [SADB_X_SPDGET] = pfkey_spdget,
2724 [SADB_X_SPDACQUIRE] = NULL,
2725 [SADB_X_SPDDUMP] = pfkey_spddump,
2726 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2727 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2728 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002729 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730};
2731
2732static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2733{
2734 void *ext_hdrs[SADB_EXT_MAX];
2735 int err;
2736
2737 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2738 BROADCAST_PROMISC_ONLY, NULL);
2739
2740 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2741 err = parse_exthdrs(skb, hdr, ext_hdrs);
2742 if (!err) {
2743 err = -EOPNOTSUPP;
2744 if (pfkey_funcs[hdr->sadb_msg_type])
2745 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2746 }
2747 return err;
2748}
2749
2750static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2751{
2752 struct sadb_msg *hdr = NULL;
2753
2754 if (skb->len < sizeof(*hdr)) {
2755 *errp = -EMSGSIZE;
2756 } else {
2757 hdr = (struct sadb_msg *) skb->data;
2758 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2759 hdr->sadb_msg_reserved != 0 ||
2760 (hdr->sadb_msg_type <= SADB_RESERVED ||
2761 hdr->sadb_msg_type > SADB_MAX)) {
2762 hdr = NULL;
2763 *errp = -EINVAL;
2764 } else if (hdr->sadb_msg_len != (skb->len /
2765 sizeof(uint64_t)) ||
2766 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2767 sizeof(uint64_t))) {
2768 hdr = NULL;
2769 *errp = -EMSGSIZE;
2770 } else {
2771 *errp = 0;
2772 }
2773 }
2774 return hdr;
2775}
2776
2777static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2778{
2779 return t->aalgos & (1 << d->desc.sadb_alg_id);
2780}
2781
2782static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2783{
2784 return t->ealgos & (1 << d->desc.sadb_alg_id);
2785}
2786
2787static int count_ah_combs(struct xfrm_tmpl *t)
2788{
2789 int i, sz = 0;
2790
2791 for (i = 0; ; i++) {
2792 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2793 if (!aalg)
2794 break;
2795 if (aalg_tmpl_set(t, aalg) && aalg->available)
2796 sz += sizeof(struct sadb_comb);
2797 }
2798 return sz + sizeof(struct sadb_prop);
2799}
2800
2801static int count_esp_combs(struct xfrm_tmpl *t)
2802{
2803 int i, k, sz = 0;
2804
2805 for (i = 0; ; i++) {
2806 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2807 if (!ealg)
2808 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2811 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 for (k = 1; ; k++) {
2814 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2815 if (!aalg)
2816 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 if (aalg_tmpl_set(t, aalg) && aalg->available)
2819 sz += sizeof(struct sadb_comb);
2820 }
2821 }
2822 return sz + sizeof(struct sadb_prop);
2823}
2824
2825static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2826{
2827 struct sadb_prop *p;
2828 int i;
2829
2830 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2831 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2832 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2833 p->sadb_prop_replay = 32;
2834 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2835
2836 for (i = 0; ; i++) {
2837 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2838 if (!aalg)
2839 break;
2840
2841 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2842 struct sadb_comb *c;
2843 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2844 memset(c, 0, sizeof(*c));
2845 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2846 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2847 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2848 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2849 c->sadb_comb_hard_addtime = 24*60*60;
2850 c->sadb_comb_soft_addtime = 20*60*60;
2851 c->sadb_comb_hard_usetime = 8*60*60;
2852 c->sadb_comb_soft_usetime = 7*60*60;
2853 }
2854 }
2855}
2856
2857static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2858{
2859 struct sadb_prop *p;
2860 int i, k;
2861
2862 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2863 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2864 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2865 p->sadb_prop_replay = 32;
2866 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2867
2868 for (i=0; ; i++) {
2869 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2870 if (!ealg)
2871 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2874 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 for (k = 1; ; k++) {
2877 struct sadb_comb *c;
2878 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2879 if (!aalg)
2880 break;
2881 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2882 continue;
2883 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2884 memset(c, 0, sizeof(*c));
2885 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2886 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2887 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2888 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2889 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2890 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2891 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2892 c->sadb_comb_hard_addtime = 24*60*60;
2893 c->sadb_comb_soft_addtime = 20*60*60;
2894 c->sadb_comb_hard_usetime = 8*60*60;
2895 c->sadb_comb_soft_usetime = 7*60*60;
2896 }
2897 }
2898}
2899
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002900static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2901{
2902 return 0;
2903}
2904
2905static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 struct sk_buff *out_skb;
2908 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002909 int hard;
2910 int hsc;
2911
Herbert Xubf088672005-06-18 22:44:00 -07002912 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002913 if (hard)
2914 hsc = 2;
2915 else
2916 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 out_skb = pfkey_xfrm_state2msg(x, 0, hsc);
2919 if (IS_ERR(out_skb))
2920 return PTR_ERR(out_skb);
2921
2922 out_hdr = (struct sadb_msg *) out_skb->data;
2923 out_hdr->sadb_msg_version = PF_KEY_V2;
2924 out_hdr->sadb_msg_type = SADB_EXPIRE;
2925 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2926 out_hdr->sadb_msg_errno = 0;
2927 out_hdr->sadb_msg_reserved = 0;
2928 out_hdr->sadb_msg_seq = 0;
2929 out_hdr->sadb_msg_pid = 0;
2930
2931 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2932 return 0;
2933}
2934
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002935static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2936{
2937 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002938 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002939 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002940 case XFRM_MSG_DELSA:
2941 case XFRM_MSG_NEWSA:
2942 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002943 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002944 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002945 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002946 case XFRM_MSG_NEWAE: /* not yet supported */
2947 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002948 default:
2949 printk("pfkey: Unknown SA event %d\n", c->event);
2950 break;
2951 }
2952
2953 return 0;
2954}
2955
2956static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2957{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002958 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
2959 return 0;
2960
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002961 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002962 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002963 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002964 case XFRM_MSG_DELPOLICY:
2965 case XFRM_MSG_NEWPOLICY:
2966 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002967 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002968 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002969 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
2970 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002971 return key_notify_policy_flush(c);
2972 default:
2973 printk("pfkey: Unknown policy event %d\n", c->event);
2974 break;
2975 }
2976
2977 return 0;
2978}
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980static u32 get_acqseq(void)
2981{
2982 u32 res;
2983 static u32 acqseq;
2984 static DEFINE_SPINLOCK(acqseq_lock);
2985
2986 spin_lock_bh(&acqseq_lock);
2987 res = (++acqseq ? : ++acqseq);
2988 spin_unlock_bh(&acqseq_lock);
2989 return res;
2990}
2991
2992static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2993{
2994 struct sk_buff *skb;
2995 struct sadb_msg *hdr;
2996 struct sadb_address *addr;
2997 struct sadb_x_policy *pol;
2998 struct sockaddr_in *sin;
2999#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3000 struct sockaddr_in6 *sin6;
3001#endif
3002 int sockaddr_size;
3003 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003004 struct sadb_x_sec_ctx *sec_ctx;
3005 struct xfrm_sec_ctx *xfrm_ctx;
3006 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3009 if (!sockaddr_size)
3010 return -EINVAL;
3011
3012 size = sizeof(struct sadb_msg) +
3013 (sizeof(struct sadb_address) * 2) +
3014 (sockaddr_size * 2) +
3015 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if (x->id.proto == IPPROTO_AH)
3018 size += count_ah_combs(t);
3019 else if (x->id.proto == IPPROTO_ESP)
3020 size += count_esp_combs(t);
3021
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003022 if ((xfrm_ctx = x->security)) {
3023 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3024 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3025 }
3026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 skb = alloc_skb(size + 16, GFP_ATOMIC);
3028 if (skb == NULL)
3029 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3032 hdr->sadb_msg_version = PF_KEY_V2;
3033 hdr->sadb_msg_type = SADB_ACQUIRE;
3034 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3035 hdr->sadb_msg_len = size / sizeof(uint64_t);
3036 hdr->sadb_msg_errno = 0;
3037 hdr->sadb_msg_reserved = 0;
3038 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3039 hdr->sadb_msg_pid = 0;
3040
3041 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003042 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003044 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 (sizeof(struct sadb_address)+sockaddr_size)/
3046 sizeof(uint64_t);
3047 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3048 addr->sadb_address_proto = 0;
3049 addr->sadb_address_reserved = 0;
3050 if (x->props.family == AF_INET) {
3051 addr->sadb_address_prefixlen = 32;
3052
3053 sin = (struct sockaddr_in *) (addr + 1);
3054 sin->sin_family = AF_INET;
3055 sin->sin_addr.s_addr = x->props.saddr.a4;
3056 sin->sin_port = 0;
3057 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3058 }
3059#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3060 else if (x->props.family == AF_INET6) {
3061 addr->sadb_address_prefixlen = 128;
3062
3063 sin6 = (struct sockaddr_in6 *) (addr + 1);
3064 sin6->sin6_family = AF_INET6;
3065 sin6->sin6_port = 0;
3066 sin6->sin6_flowinfo = 0;
3067 memcpy(&sin6->sin6_addr,
3068 x->props.saddr.a6, sizeof(struct in6_addr));
3069 sin6->sin6_scope_id = 0;
3070 }
3071#endif
3072 else
3073 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003076 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 sizeof(struct sadb_address)+sockaddr_size);
3078 addr->sadb_address_len =
3079 (sizeof(struct sadb_address)+sockaddr_size)/
3080 sizeof(uint64_t);
3081 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3082 addr->sadb_address_proto = 0;
3083 addr->sadb_address_reserved = 0;
3084 if (x->props.family == AF_INET) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003085 addr->sadb_address_prefixlen = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
3087 sin = (struct sockaddr_in *) (addr + 1);
3088 sin->sin_family = AF_INET;
3089 sin->sin_addr.s_addr = x->id.daddr.a4;
3090 sin->sin_port = 0;
3091 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3092 }
3093#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3094 else if (x->props.family == AF_INET6) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003095 addr->sadb_address_prefixlen = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
3097 sin6 = (struct sockaddr_in6 *) (addr + 1);
3098 sin6->sin6_family = AF_INET6;
3099 sin6->sin6_port = 0;
3100 sin6->sin6_flowinfo = 0;
3101 memcpy(&sin6->sin6_addr,
3102 x->id.daddr.a6, sizeof(struct in6_addr));
3103 sin6->sin6_scope_id = 0;
3104 }
3105#endif
3106 else
3107 BUG();
3108
3109 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3110 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3111 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3112 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3113 pol->sadb_x_policy_dir = dir+1;
3114 pol->sadb_x_policy_id = xp->index;
3115
3116 /* Set sadb_comb's. */
3117 if (x->id.proto == IPPROTO_AH)
3118 dump_ah_combs(skb, t);
3119 else if (x->id.proto == IPPROTO_ESP)
3120 dump_esp_combs(skb, t);
3121
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003122 /* security context */
3123 if (xfrm_ctx) {
3124 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3125 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3126 sec_ctx->sadb_x_sec_len =
3127 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3128 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3129 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3130 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3131 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3132 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3133 xfrm_ctx->ctx_len);
3134 }
3135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3137}
3138
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003139static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003140 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141{
3142 struct xfrm_policy *xp;
3143 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003144 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003146 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 case AF_INET:
3148 if (opt != IP_IPSEC_POLICY) {
3149 *dir = -EOPNOTSUPP;
3150 return NULL;
3151 }
3152 break;
3153#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3154 case AF_INET6:
3155 if (opt != IPV6_IPSEC_POLICY) {
3156 *dir = -EOPNOTSUPP;
3157 return NULL;
3158 }
3159 break;
3160#endif
3161 default:
3162 *dir = -EINVAL;
3163 return NULL;
3164 }
3165
3166 *dir = -EINVAL;
3167
3168 if (len < sizeof(struct sadb_x_policy) ||
3169 pol->sadb_x_policy_len*8 > len ||
3170 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3171 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3172 return NULL;
3173
3174 xp = xfrm_policy_alloc(GFP_ATOMIC);
3175 if (xp == NULL) {
3176 *dir = -ENOBUFS;
3177 return NULL;
3178 }
3179
3180 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3181 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3182
3183 xp->lft.soft_byte_limit = XFRM_INF;
3184 xp->lft.hard_byte_limit = XFRM_INF;
3185 xp->lft.soft_packet_limit = XFRM_INF;
3186 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003187 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
3189 xp->xfrm_nr = 0;
3190 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3191 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3192 goto out;
3193
Trent Jaegerdf718372005-12-13 23:12:27 -08003194 /* security context too */
3195 if (len >= (pol->sadb_x_policy_len*8 +
3196 sizeof(struct sadb_x_sec_ctx))) {
3197 char *p = (char *)pol;
3198 struct xfrm_user_sec_ctx *uctx;
3199
3200 p += pol->sadb_x_policy_len*8;
3201 sec_ctx = (struct sadb_x_sec_ctx *)p;
3202 if (len < pol->sadb_x_policy_len*8 +
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003203 sec_ctx->sadb_x_sec_len) {
3204 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003205 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003206 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003207 if ((*dir = verify_sec_ctx_len(p)))
3208 goto out;
3209 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
3210 *dir = security_xfrm_policy_alloc(xp, uctx);
3211 kfree(uctx);
3212
3213 if (*dir)
3214 goto out;
3215 }
3216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 *dir = pol->sadb_x_policy_dir-1;
3218 return xp;
3219
3220out:
Trent Jaegerdf718372005-12-13 23:12:27 -08003221 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 kfree(xp);
3223 return NULL;
3224}
3225
Al Viro5d36b182006-11-08 00:24:06 -08003226static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
3228 struct sk_buff *skb;
3229 struct sadb_msg *hdr;
3230 struct sadb_sa *sa;
3231 struct sadb_address *addr;
3232 struct sadb_x_nat_t_port *n_port;
3233 struct sockaddr_in *sin;
3234#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3235 struct sockaddr_in6 *sin6;
3236#endif
3237 int sockaddr_size;
3238 int size;
3239 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3240 struct xfrm_encap_tmpl *natt = NULL;
3241
3242 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3243 if (!sockaddr_size)
3244 return -EINVAL;
3245
3246 if (!satype)
3247 return -EINVAL;
3248
3249 if (!x->encap)
3250 return -EINVAL;
3251
3252 natt = x->encap;
3253
3254 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3255 *
3256 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3257 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3258 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 size = sizeof(struct sadb_msg) +
3261 sizeof(struct sadb_sa) +
3262 (sizeof(struct sadb_address) * 2) +
3263 (sockaddr_size * 2) +
3264 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 skb = alloc_skb(size + 16, GFP_ATOMIC);
3267 if (skb == NULL)
3268 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003269
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3271 hdr->sadb_msg_version = PF_KEY_V2;
3272 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3273 hdr->sadb_msg_satype = satype;
3274 hdr->sadb_msg_len = size / sizeof(uint64_t);
3275 hdr->sadb_msg_errno = 0;
3276 hdr->sadb_msg_reserved = 0;
3277 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3278 hdr->sadb_msg_pid = 0;
3279
3280 /* SA */
3281 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3282 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3283 sa->sadb_sa_exttype = SADB_EXT_SA;
3284 sa->sadb_sa_spi = x->id.spi;
3285 sa->sadb_sa_replay = 0;
3286 sa->sadb_sa_state = 0;
3287 sa->sadb_sa_auth = 0;
3288 sa->sadb_sa_encrypt = 0;
3289 sa->sadb_sa_flags = 0;
3290
3291 /* ADDRESS_SRC (old addr) */
3292 addr = (struct sadb_address*)
3293 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003294 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 (sizeof(struct sadb_address)+sockaddr_size)/
3296 sizeof(uint64_t);
3297 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3298 addr->sadb_address_proto = 0;
3299 addr->sadb_address_reserved = 0;
3300 if (x->props.family == AF_INET) {
3301 addr->sadb_address_prefixlen = 32;
3302
3303 sin = (struct sockaddr_in *) (addr + 1);
3304 sin->sin_family = AF_INET;
3305 sin->sin_addr.s_addr = x->props.saddr.a4;
3306 sin->sin_port = 0;
3307 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3308 }
3309#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3310 else if (x->props.family == AF_INET6) {
3311 addr->sadb_address_prefixlen = 128;
3312
3313 sin6 = (struct sockaddr_in6 *) (addr + 1);
3314 sin6->sin6_family = AF_INET6;
3315 sin6->sin6_port = 0;
3316 sin6->sin6_flowinfo = 0;
3317 memcpy(&sin6->sin6_addr,
3318 x->props.saddr.a6, sizeof(struct in6_addr));
3319 sin6->sin6_scope_id = 0;
3320 }
3321#endif
3322 else
3323 BUG();
3324
3325 /* NAT_T_SPORT (old port) */
3326 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3327 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3328 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3329 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3330 n_port->sadb_x_nat_t_port_reserved = 0;
3331
3332 /* ADDRESS_DST (new addr) */
3333 addr = (struct sadb_address*)
3334 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003335 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 (sizeof(struct sadb_address)+sockaddr_size)/
3337 sizeof(uint64_t);
3338 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3339 addr->sadb_address_proto = 0;
3340 addr->sadb_address_reserved = 0;
3341 if (x->props.family == AF_INET) {
3342 addr->sadb_address_prefixlen = 32;
3343
3344 sin = (struct sockaddr_in *) (addr + 1);
3345 sin->sin_family = AF_INET;
3346 sin->sin_addr.s_addr = ipaddr->a4;
3347 sin->sin_port = 0;
3348 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3349 }
3350#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3351 else if (x->props.family == AF_INET6) {
3352 addr->sadb_address_prefixlen = 128;
3353
3354 sin6 = (struct sockaddr_in6 *) (addr + 1);
3355 sin6->sin6_family = AF_INET6;
3356 sin6->sin6_port = 0;
3357 sin6->sin6_flowinfo = 0;
3358 memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
3359 sin6->sin6_scope_id = 0;
3360 }
3361#endif
3362 else
3363 BUG();
3364
3365 /* NAT_T_DPORT (new port) */
3366 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3367 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3368 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3369 n_port->sadb_x_nat_t_port_port = sport;
3370 n_port->sadb_x_nat_t_port_reserved = 0;
3371
3372 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3373}
3374
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003375#ifdef CONFIG_NET_KEY_MIGRATE
3376static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3377 struct xfrm_selector *sel)
3378{
3379 struct sadb_address *addr;
3380 struct sockaddr_in *sin;
3381#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3382 struct sockaddr_in6 *sin6;
3383#endif
3384 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3385 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3386 addr->sadb_address_exttype = type;
3387 addr->sadb_address_proto = sel->proto;
3388 addr->sadb_address_reserved = 0;
3389
3390 switch (type) {
3391 case SADB_EXT_ADDRESS_SRC:
3392 if (sel->family == AF_INET) {
3393 addr->sadb_address_prefixlen = sel->prefixlen_s;
3394 sin = (struct sockaddr_in *)(addr + 1);
3395 sin->sin_family = AF_INET;
3396 memcpy(&sin->sin_addr.s_addr, &sel->saddr,
3397 sizeof(sin->sin_addr.s_addr));
3398 sin->sin_port = 0;
3399 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3400 }
3401#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3402 else if (sel->family == AF_INET6) {
3403 addr->sadb_address_prefixlen = sel->prefixlen_s;
3404 sin6 = (struct sockaddr_in6 *)(addr + 1);
3405 sin6->sin6_family = AF_INET6;
3406 sin6->sin6_port = 0;
3407 sin6->sin6_flowinfo = 0;
3408 sin6->sin6_scope_id = 0;
3409 memcpy(&sin6->sin6_addr.s6_addr, &sel->saddr,
3410 sizeof(sin6->sin6_addr.s6_addr));
3411 }
3412#endif
3413 break;
3414 case SADB_EXT_ADDRESS_DST:
3415 if (sel->family == AF_INET) {
3416 addr->sadb_address_prefixlen = sel->prefixlen_d;
3417 sin = (struct sockaddr_in *)(addr + 1);
3418 sin->sin_family = AF_INET;
3419 memcpy(&sin->sin_addr.s_addr, &sel->daddr,
3420 sizeof(sin->sin_addr.s_addr));
3421 sin->sin_port = 0;
3422 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3423 }
3424#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3425 else if (sel->family == AF_INET6) {
3426 addr->sadb_address_prefixlen = sel->prefixlen_d;
3427 sin6 = (struct sockaddr_in6 *)(addr + 1);
3428 sin6->sin6_family = AF_INET6;
3429 sin6->sin6_port = 0;
3430 sin6->sin6_flowinfo = 0;
3431 sin6->sin6_scope_id = 0;
3432 memcpy(&sin6->sin6_addr.s6_addr, &sel->daddr,
3433 sizeof(sin6->sin6_addr.s6_addr));
3434 }
3435#endif
3436 break;
3437 default:
3438 return -EINVAL;
3439 }
3440
3441 return 0;
3442}
3443
3444static int set_ipsecrequest(struct sk_buff *skb,
3445 uint8_t proto, uint8_t mode, int level,
3446 uint32_t reqid, uint8_t family,
3447 xfrm_address_t *src, xfrm_address_t *dst)
3448{
3449 struct sadb_x_ipsecrequest *rq;
3450 struct sockaddr_in *sin;
3451#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3452 struct sockaddr_in6 *sin6;
3453#endif
3454 int size_req;
3455
3456 size_req = sizeof(struct sadb_x_ipsecrequest) +
3457 pfkey_sockaddr_pair_size(family);
3458
3459 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3460 memset(rq, 0, size_req);
3461 rq->sadb_x_ipsecrequest_len = size_req;
3462 rq->sadb_x_ipsecrequest_proto = proto;
3463 rq->sadb_x_ipsecrequest_mode = mode;
3464 rq->sadb_x_ipsecrequest_level = level;
3465 rq->sadb_x_ipsecrequest_reqid = reqid;
3466
3467 switch (family) {
3468 case AF_INET:
3469 sin = (struct sockaddr_in *)(rq + 1);
3470 sin->sin_family = AF_INET;
3471 memcpy(&sin->sin_addr.s_addr, src,
3472 sizeof(sin->sin_addr.s_addr));
3473 sin++;
3474 sin->sin_family = AF_INET;
3475 memcpy(&sin->sin_addr.s_addr, dst,
3476 sizeof(sin->sin_addr.s_addr));
3477 break;
3478#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3479 case AF_INET6:
3480 sin6 = (struct sockaddr_in6 *)(rq + 1);
3481 sin6->sin6_family = AF_INET6;
3482 sin6->sin6_port = 0;
3483 sin6->sin6_flowinfo = 0;
3484 sin6->sin6_scope_id = 0;
3485 memcpy(&sin6->sin6_addr.s6_addr, src,
3486 sizeof(sin6->sin6_addr.s6_addr));
3487 sin6++;
3488 sin6->sin6_family = AF_INET6;
3489 sin6->sin6_port = 0;
3490 sin6->sin6_flowinfo = 0;
3491 sin6->sin6_scope_id = 0;
3492 memcpy(&sin6->sin6_addr.s6_addr, dst,
3493 sizeof(sin6->sin6_addr.s6_addr));
3494 break;
3495#endif
3496 default:
3497 return -EINVAL;
3498 }
3499
3500 return 0;
3501}
3502#endif
3503
3504#ifdef CONFIG_NET_KEY_MIGRATE
3505static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3506 struct xfrm_migrate *m, int num_bundles)
3507{
3508 int i;
3509 int sasize_sel;
3510 int size = 0;
3511 int size_pol = 0;
3512 struct sk_buff *skb;
3513 struct sadb_msg *hdr;
3514 struct sadb_x_policy *pol;
3515 struct xfrm_migrate *mp;
3516
3517 if (type != XFRM_POLICY_TYPE_MAIN)
3518 return 0;
3519
3520 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3521 return -EINVAL;
3522
3523 /* selector */
3524 sasize_sel = pfkey_sockaddr_size(sel->family);
3525 if (!sasize_sel)
3526 return -EINVAL;
3527 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3528
3529 /* policy info */
3530 size_pol += sizeof(struct sadb_x_policy);
3531
3532 /* ipsecrequests */
3533 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3534 /* old locator pair */
3535 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3536 pfkey_sockaddr_pair_size(mp->old_family);
3537 /* new locator pair */
3538 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3539 pfkey_sockaddr_pair_size(mp->new_family);
3540 }
3541
3542 size += sizeof(struct sadb_msg) + size_pol;
3543
3544 /* alloc buffer */
3545 skb = alloc_skb(size, GFP_ATOMIC);
3546 if (skb == NULL)
3547 return -ENOMEM;
3548
3549 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3550 hdr->sadb_msg_version = PF_KEY_V2;
3551 hdr->sadb_msg_type = SADB_X_MIGRATE;
3552 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3553 hdr->sadb_msg_len = size / 8;
3554 hdr->sadb_msg_errno = 0;
3555 hdr->sadb_msg_reserved = 0;
3556 hdr->sadb_msg_seq = 0;
3557 hdr->sadb_msg_pid = 0;
3558
3559 /* selector src */
3560 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3561
3562 /* selector dst */
3563 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3564
3565 /* policy information */
3566 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3567 pol->sadb_x_policy_len = size_pol / 8;
3568 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3569 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3570 pol->sadb_x_policy_dir = dir + 1;
3571 pol->sadb_x_policy_id = 0;
3572 pol->sadb_x_policy_priority = 0;
3573
3574 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3575 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003576 int mode = pfkey_mode_from_xfrm(mp->mode);
3577 if (mode < 0)
3578 return -EINVAL;
3579 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003580 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3581 mp->reqid, mp->old_family,
3582 &mp->old_saddr, &mp->old_daddr) < 0) {
3583 return -EINVAL;
3584 }
3585
3586 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003587 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003588 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3589 mp->reqid, mp->new_family,
3590 &mp->new_saddr, &mp->new_daddr) < 0) {
3591 return -EINVAL;
3592 }
3593 }
3594
3595 /* broadcast migrate message to sockets */
3596 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
3597
3598 return 0;
3599}
3600#else
3601static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3602 struct xfrm_migrate *m, int num_bundles)
3603{
3604 return -ENOPROTOOPT;
3605}
3606#endif
3607
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608static int pfkey_sendmsg(struct kiocb *kiocb,
3609 struct socket *sock, struct msghdr *msg, size_t len)
3610{
3611 struct sock *sk = sock->sk;
3612 struct sk_buff *skb = NULL;
3613 struct sadb_msg *hdr = NULL;
3614 int err;
3615
3616 err = -EOPNOTSUPP;
3617 if (msg->msg_flags & MSG_OOB)
3618 goto out;
3619
3620 err = -EMSGSIZE;
3621 if ((unsigned)len > sk->sk_sndbuf - 32)
3622 goto out;
3623
3624 err = -ENOBUFS;
3625 skb = alloc_skb(len, GFP_KERNEL);
3626 if (skb == NULL)
3627 goto out;
3628
3629 err = -EFAULT;
3630 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3631 goto out;
3632
3633 hdr = pfkey_get_base_msg(skb, &err);
3634 if (!hdr)
3635 goto out;
3636
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003637 mutex_lock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 err = pfkey_process(sk, skb, hdr);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003639 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
3641out:
3642 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3643 err = 0;
3644 if (skb)
3645 kfree_skb(skb);
3646
3647 return err ? : len;
3648}
3649
3650static int pfkey_recvmsg(struct kiocb *kiocb,
3651 struct socket *sock, struct msghdr *msg, size_t len,
3652 int flags)
3653{
3654 struct sock *sk = sock->sk;
3655 struct sk_buff *skb;
3656 int copied, err;
3657
3658 err = -EINVAL;
3659 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3660 goto out;
3661
3662 msg->msg_namelen = 0;
3663 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3664 if (skb == NULL)
3665 goto out;
3666
3667 copied = skb->len;
3668 if (copied > len) {
3669 msg->msg_flags |= MSG_TRUNC;
3670 copied = len;
3671 }
3672
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003673 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3675 if (err)
3676 goto out_free;
3677
3678 sock_recv_timestamp(msg, sk, skb);
3679
3680 err = (flags & MSG_TRUNC) ? skb->len : copied;
3681
3682out_free:
3683 skb_free_datagram(sk, skb);
3684out:
3685 return err;
3686}
3687
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003688static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 .family = PF_KEY,
3690 .owner = THIS_MODULE,
3691 /* Operations that make no sense on pfkey sockets. */
3692 .bind = sock_no_bind,
3693 .connect = sock_no_connect,
3694 .socketpair = sock_no_socketpair,
3695 .accept = sock_no_accept,
3696 .getname = sock_no_getname,
3697 .ioctl = sock_no_ioctl,
3698 .listen = sock_no_listen,
3699 .shutdown = sock_no_shutdown,
3700 .setsockopt = sock_no_setsockopt,
3701 .getsockopt = sock_no_getsockopt,
3702 .mmap = sock_no_mmap,
3703 .sendpage = sock_no_sendpage,
3704
3705 /* Now the operations that really occur. */
3706 .release = pfkey_release,
3707 .poll = datagram_poll,
3708 .sendmsg = pfkey_sendmsg,
3709 .recvmsg = pfkey_recvmsg,
3710};
3711
3712static struct net_proto_family pfkey_family_ops = {
3713 .family = PF_KEY,
3714 .create = pfkey_create,
3715 .owner = THIS_MODULE,
3716};
3717
3718#ifdef CONFIG_PROC_FS
3719static int pfkey_read_proc(char *buffer, char **start, off_t offset,
3720 int length, int *eof, void *data)
3721{
3722 off_t pos = 0;
3723 off_t begin = 0;
3724 int len = 0;
3725 struct sock *s;
3726 struct hlist_node *node;
3727
3728 len += sprintf(buffer,"sk RefCnt Rmem Wmem User Inode\n");
3729
3730 read_lock(&pfkey_table_lock);
3731
3732 sk_for_each(s, node, &pfkey_table) {
3733 len += sprintf(buffer+len,"%p %-6d %-6u %-6u %-6u %-6lu",
3734 s,
3735 atomic_read(&s->sk_refcnt),
3736 atomic_read(&s->sk_rmem_alloc),
3737 atomic_read(&s->sk_wmem_alloc),
3738 sock_i_uid(s),
3739 sock_i_ino(s)
3740 );
3741
3742 buffer[len++] = '\n';
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 pos = begin + len;
3745 if (pos < offset) {
3746 len = 0;
3747 begin = pos;
3748 }
3749 if(pos > offset + length)
3750 goto done;
3751 }
3752 *eof = 1;
3753
3754done:
3755 read_unlock(&pfkey_table_lock);
3756
3757 *start = buffer + (offset - begin);
3758 len -= (offset - begin);
3759
3760 if (len > length)
3761 len = length;
3762 if (len < 0)
3763 len = 0;
3764
3765 return len;
3766}
3767#endif
3768
3769static struct xfrm_mgr pfkeyv2_mgr =
3770{
3771 .id = "pfkeyv2",
3772 .notify = pfkey_send_notify,
3773 .acquire = pfkey_send_acquire,
3774 .compile_policy = pfkey_compile_policy,
3775 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003776 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003777 .migrate = pfkey_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778};
3779
3780static void __exit ipsec_pfkey_exit(void)
3781{
3782 xfrm_unregister_km(&pfkeyv2_mgr);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003783 remove_proc_entry("pfkey", init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 sock_unregister(PF_KEY);
3785 proto_unregister(&key_proto);
3786}
3787
3788static int __init ipsec_pfkey_init(void)
3789{
3790 int err = proto_register(&key_proto, 0);
3791
3792 if (err != 0)
3793 goto out;
3794
3795 err = sock_register(&pfkey_family_ops);
3796 if (err != 0)
3797 goto out_unregister_key_proto;
3798#ifdef CONFIG_PROC_FS
3799 err = -ENOMEM;
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003800 if (create_proc_read_entry("pfkey", 0, init_net.proc_net, pfkey_read_proc, NULL) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 goto out_sock_unregister;
3802#endif
3803 err = xfrm_register_km(&pfkeyv2_mgr);
3804 if (err != 0)
3805 goto out_remove_proc_entry;
3806out:
3807 return err;
3808out_remove_proc_entry:
3809#ifdef CONFIG_PROC_FS
3810 remove_proc_entry("net/pfkey", NULL);
3811out_sock_unregister:
3812#endif
3813 sock_unregister(PF_KEY);
3814out_unregister_key_proto:
3815 proto_unregister(&key_proto);
3816 goto out;
3817}
3818
3819module_init(ipsec_pfkey_init);
3820module_exit(ipsec_pfkey_exit);
3821MODULE_LICENSE("GPL");
3822MODULE_ALIAS_NETPROTO(PF_KEY);