blob: f0fc46c8038d0632bca9fb3b031af5b908f1ff2d [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;
Timo Teras83321d62008-03-03 23:40:12 -080051
52 struct {
53 uint8_t msg_version;
54 uint32_t msg_pid;
55 int (*dump)(struct pfkey_sock *sk);
56 void (*done)(struct pfkey_sock *sk);
57 union {
58 struct xfrm_policy_walk policy;
59 struct xfrm_state_walk state;
60 } u;
61 } dump;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
65{
66 return (struct pfkey_sock *)sk;
67}
68
Timo Teras83321d62008-03-03 23:40:12 -080069static int pfkey_can_dump(struct sock *sk)
70{
71 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
72 return 1;
73 return 0;
74}
75
76static int pfkey_do_dump(struct pfkey_sock *pfk)
77{
78 int rc;
79
80 rc = pfk->dump.dump(pfk);
81 if (rc == -ENOBUFS)
82 return 0;
83
84 pfk->dump.done(pfk);
85 pfk->dump.dump = NULL;
86 pfk->dump.done = NULL;
87 return rc;
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void pfkey_sock_destruct(struct sock *sk)
91{
92 skb_queue_purge(&sk->sk_receive_queue);
93
94 if (!sock_flag(sk, SOCK_DEAD)) {
95 printk("Attempt to release alive pfkey socket: %p\n", sk);
96 return;
97 }
98
99 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
100 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
101
102 atomic_dec(&pfkey_socks_nr);
103}
104
105static void pfkey_table_grab(void)
106{
107 write_lock_bh(&pfkey_table_lock);
108
109 if (atomic_read(&pfkey_table_users)) {
110 DECLARE_WAITQUEUE(wait, current);
111
112 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
113 for(;;) {
114 set_current_state(TASK_UNINTERRUPTIBLE);
115 if (atomic_read(&pfkey_table_users) == 0)
116 break;
117 write_unlock_bh(&pfkey_table_lock);
118 schedule();
119 write_lock_bh(&pfkey_table_lock);
120 }
121
122 __set_current_state(TASK_RUNNING);
123 remove_wait_queue(&pfkey_table_wait, &wait);
124 }
125}
126
127static __inline__ void pfkey_table_ungrab(void)
128{
129 write_unlock_bh(&pfkey_table_lock);
130 wake_up(&pfkey_table_wait);
131}
132
133static __inline__ void pfkey_lock_table(void)
134{
135 /* read_lock() synchronizes us to pfkey_table_grab */
136
137 read_lock(&pfkey_table_lock);
138 atomic_inc(&pfkey_table_users);
139 read_unlock(&pfkey_table_lock);
140}
141
142static __inline__ void pfkey_unlock_table(void)
143{
144 if (atomic_dec_and_test(&pfkey_table_users))
145 wake_up(&pfkey_table_wait);
146}
147
148
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800149static const struct proto_ops pfkey_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151static void pfkey_insert(struct sock *sk)
152{
153 pfkey_table_grab();
154 sk_add_node(sk, &pfkey_table);
155 pfkey_table_ungrab();
156}
157
158static void pfkey_remove(struct sock *sk)
159{
160 pfkey_table_grab();
161 sk_del_node_init(sk);
162 pfkey_table_ungrab();
163}
164
165static struct proto key_proto = {
166 .name = "KEY",
167 .owner = THIS_MODULE,
168 .obj_size = sizeof(struct pfkey_sock),
169};
170
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700171static int pfkey_create(struct net *net, struct socket *sock, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct sock *sk;
174 int err;
175
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700176 if (net != &init_net)
177 return -EAFNOSUPPORT;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!capable(CAP_NET_ADMIN))
180 return -EPERM;
181 if (sock->type != SOCK_RAW)
182 return -ESOCKTNOSUPPORT;
183 if (protocol != PF_KEY_V2)
184 return -EPROTONOSUPPORT;
185
186 err = -ENOMEM;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700187 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (sk == NULL)
189 goto out;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 sock->ops = &pfkey_ops;
192 sock_init_data(sock, sk);
193
194 sk->sk_family = PF_KEY;
195 sk->sk_destruct = pfkey_sock_destruct;
196
197 atomic_inc(&pfkey_socks_nr);
198
199 pfkey_insert(sk);
200
201 return 0;
202out:
203 return err;
204}
205
206static int pfkey_release(struct socket *sock)
207{
208 struct sock *sk = sock->sk;
209
210 if (!sk)
211 return 0;
212
213 pfkey_remove(sk);
214
215 sock_orphan(sk);
216 sock->sk = NULL;
217 skb_queue_purge(&sk->sk_write_queue);
218 sock_put(sk);
219
220 return 0;
221}
222
223static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
Al Virodd0fc662005-10-07 07:46:04 +0100224 gfp_t allocation, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 int err = -ENOBUFS;
227
228 sock_hold(sk);
229 if (*skb2 == NULL) {
230 if (atomic_read(&skb->users) != 1) {
231 *skb2 = skb_clone(skb, allocation);
232 } else {
233 *skb2 = skb;
234 atomic_inc(&skb->users);
235 }
236 }
237 if (*skb2 != NULL) {
238 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
239 skb_orphan(*skb2);
240 skb_set_owner_r(*skb2, sk);
241 skb_queue_tail(&sk->sk_receive_queue, *skb2);
242 sk->sk_data_ready(sk, (*skb2)->len);
243 *skb2 = NULL;
244 err = 0;
245 }
246 }
247 sock_put(sk);
248 return err;
249}
250
251/* Send SKB to all pfkey sockets matching selected criteria. */
252#define BROADCAST_ALL 0
253#define BROADCAST_ONE 1
254#define BROADCAST_REGISTERED 2
255#define BROADCAST_PROMISC_ONLY 4
Al Virodd0fc662005-10-07 07:46:04 +0100256static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 int broadcast_flags, struct sock *one_sk)
258{
259 struct sock *sk;
260 struct hlist_node *node;
261 struct sk_buff *skb2 = NULL;
262 int err = -ESRCH;
263
264 /* XXX Do we need something like netlink_overrun? I think
265 * XXX PF_KEY socket apps will not mind current behavior.
266 */
267 if (!skb)
268 return -ENOMEM;
269
270 pfkey_lock_table();
271 sk_for_each(sk, node, &pfkey_table) {
272 struct pfkey_sock *pfk = pfkey_sk(sk);
273 int err2;
274
275 /* Yes, it means that if you are meant to receive this
276 * pfkey message you receive it twice as promiscuous
277 * socket.
278 */
279 if (pfk->promisc)
280 pfkey_broadcast_one(skb, &skb2, allocation, sk);
281
282 /* the exact target will be processed later */
283 if (sk == one_sk)
284 continue;
285 if (broadcast_flags != BROADCAST_ALL) {
286 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
287 continue;
288 if ((broadcast_flags & BROADCAST_REGISTERED) &&
289 !pfk->registered)
290 continue;
291 if (broadcast_flags & BROADCAST_ONE)
292 continue;
293 }
294
295 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
296
297 /* Error is cleare after succecful sending to at least one
298 * registered KM */
299 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
300 err = err2;
301 }
302 pfkey_unlock_table();
303
304 if (one_sk != NULL)
305 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
306
307 if (skb2)
308 kfree_skb(skb2);
309 kfree_skb(skb);
310 return err;
311}
312
313static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
314{
315 *new = *orig;
316}
317
318static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
319{
320 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
321 struct sadb_msg *hdr;
322
323 if (!skb)
324 return -ENOBUFS;
325
326 /* Woe be to the platform trying to support PFKEY yet
327 * having normal errnos outside the 1-255 range, inclusive.
328 */
329 err = -err;
330 if (err == ERESTARTSYS ||
331 err == ERESTARTNOHAND ||
332 err == ERESTARTNOINTR)
333 err = EINTR;
334 if (err >= 512)
335 err = EINVAL;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800336 BUG_ON(err <= 0 || err >= 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
339 pfkey_hdr_dup(hdr, orig);
340 hdr->sadb_msg_errno = (uint8_t) err;
341 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
342 sizeof(uint64_t));
343
344 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);
345
346 return 0;
347}
348
349static u8 sadb_ext_min_len[] = {
350 [SADB_EXT_RESERVED] = (u8) 0,
351 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
352 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
353 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
354 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
355 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
356 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
357 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
358 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
359 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
360 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
361 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
362 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
363 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
364 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
365 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
366 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
367 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
368 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
369 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
370 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
371 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
372 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
373 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
Trent Jaegerdf718372005-12-13 23:12:27 -0800374 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375};
376
377/* Verify sadb_address_{len,prefixlen} against sa_family. */
378static int verify_address_len(void *p)
379{
380 struct sadb_address *sp = p;
381 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
382 struct sockaddr_in *sin;
383#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
384 struct sockaddr_in6 *sin6;
385#endif
386 int len;
387
388 switch (addr->sa_family) {
389 case AF_INET:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700390 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (sp->sadb_address_len != len ||
392 sp->sadb_address_prefixlen > 32)
393 return -EINVAL;
394 break;
395#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
396 case AF_INET6:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700397 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (sp->sadb_address_len != len ||
399 sp->sadb_address_prefixlen > 128)
400 return -EINVAL;
401 break;
402#endif
403 default:
404 /* It is user using kernel to keep track of security
405 * associations for another protocol, such as
406 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
407 * lengths.
408 *
409 * XXX Actually, association/policy database is not yet
410 * XXX able to cope with arbitrary sockaddr families.
411 * XXX When it can, remove this -EINVAL. -DaveM
412 */
413 return -EINVAL;
414 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 return 0;
418}
419
Trent Jaegerdf718372005-12-13 23:12:27 -0800420static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx)
421{
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700422 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
423 sec_ctx->sadb_x_ctx_len,
424 sizeof(uint64_t));
Trent Jaegerdf718372005-12-13 23:12:27 -0800425}
426
427static inline int verify_sec_ctx_len(void *p)
428{
429 struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p;
Stephen Rothwell298bb622007-10-30 23:57:05 -0700430 int len = sec_ctx->sadb_x_ctx_len;
Trent Jaegerdf718372005-12-13 23:12:27 -0800431
Stephen Rothwell298bb622007-10-30 23:57:05 -0700432 if (len > PAGE_SIZE)
Trent Jaegerdf718372005-12-13 23:12:27 -0800433 return -EINVAL;
434
435 len = pfkey_sec_ctx_len(sec_ctx);
436
437 if (sec_ctx->sadb_x_sec_len != len)
438 return -EINVAL;
439
440 return 0;
441}
442
443static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx)
444{
445 struct xfrm_user_sec_ctx *uctx = NULL;
446 int ctx_size = sec_ctx->sadb_x_ctx_len;
447
448 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
449
450 if (!uctx)
451 return NULL;
452
453 uctx->len = pfkey_sec_ctx_len(sec_ctx);
454 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
455 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
456 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
457 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
458 memcpy(uctx + 1, sec_ctx + 1,
459 uctx->ctx_len);
460
461 return uctx;
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static int present_and_same_family(struct sadb_address *src,
465 struct sadb_address *dst)
466{
467 struct sockaddr *s_addr, *d_addr;
468
469 if (!src || !dst)
470 return 0;
471
472 s_addr = (struct sockaddr *)(src + 1);
473 d_addr = (struct sockaddr *)(dst + 1);
474 if (s_addr->sa_family != d_addr->sa_family)
475 return 0;
476 if (s_addr->sa_family != AF_INET
477#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
478 && s_addr->sa_family != AF_INET6
479#endif
480 )
481 return 0;
482
483 return 1;
484}
485
486static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
487{
488 char *p = (char *) hdr;
489 int len = skb->len;
490
491 len -= sizeof(*hdr);
492 p += sizeof(*hdr);
493 while (len > 0) {
494 struct sadb_ext *ehdr = (struct sadb_ext *) p;
495 uint16_t ext_type;
496 int ext_len;
497
498 ext_len = ehdr->sadb_ext_len;
499 ext_len *= sizeof(uint64_t);
500 ext_type = ehdr->sadb_ext_type;
501 if (ext_len < sizeof(uint64_t) ||
502 ext_len > len ||
503 ext_type == SADB_EXT_RESERVED)
504 return -EINVAL;
505
506 if (ext_type <= SADB_EXT_MAX) {
507 int min = (int) sadb_ext_min_len[ext_type];
508 if (ext_len < min)
509 return -EINVAL;
510 if (ext_hdrs[ext_type-1] != NULL)
511 return -EINVAL;
512 if (ext_type == SADB_EXT_ADDRESS_SRC ||
513 ext_type == SADB_EXT_ADDRESS_DST ||
514 ext_type == SADB_EXT_ADDRESS_PROXY ||
515 ext_type == SADB_X_EXT_NAT_T_OA) {
516 if (verify_address_len(p))
517 return -EINVAL;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900518 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800519 if (ext_type == SADB_X_EXT_SEC_CTX) {
520 if (verify_sec_ctx_len(p))
521 return -EINVAL;
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 ext_hdrs[ext_type-1] = p;
524 }
525 p += ext_len;
526 len -= ext_len;
527 }
528
529 return 0;
530}
531
532static uint16_t
533pfkey_satype2proto(uint8_t satype)
534{
535 switch (satype) {
536 case SADB_SATYPE_UNSPEC:
537 return IPSEC_PROTO_ANY;
538 case SADB_SATYPE_AH:
539 return IPPROTO_AH;
540 case SADB_SATYPE_ESP:
541 return IPPROTO_ESP;
542 case SADB_X_SATYPE_IPCOMP:
543 return IPPROTO_COMP;
544 break;
545 default:
546 return 0;
547 }
548 /* NOTREACHED */
549}
550
551static uint8_t
552pfkey_proto2satype(uint16_t proto)
553{
554 switch (proto) {
555 case IPPROTO_AH:
556 return SADB_SATYPE_AH;
557 case IPPROTO_ESP:
558 return SADB_SATYPE_ESP;
559 case IPPROTO_COMP:
560 return SADB_X_SATYPE_IPCOMP;
561 break;
562 default:
563 return 0;
564 }
565 /* NOTREACHED */
566}
567
568/* BTW, this scheme means that there is no way with PFKEY2 sockets to
569 * say specifically 'just raw sockets' as we encode them as 255.
570 */
571
572static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
573{
574 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
575}
576
577static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
578{
579 return (proto ? proto : IPSEC_PROTO_ANY);
580}
581
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900582static inline int pfkey_sockaddr_len(sa_family_t family)
583{
584 switch (family) {
585 case AF_INET:
586 return sizeof(struct sockaddr_in);
587#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
588 case AF_INET6:
589 return sizeof(struct sockaddr_in6);
590#endif
591 }
592 return 0;
593}
594
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900595static
596int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900598 switch (sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 case AF_INET:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900600 xaddr->a4 =
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900601 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return AF_INET;
603#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
604 case AF_INET6:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900605 memcpy(xaddr->a6,
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900606 &((struct sockaddr_in6 *)sa)->sin6_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 sizeof(struct in6_addr));
608 return AF_INET6;
609#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900611 return 0;
612}
613
614static
615int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr, xfrm_address_t *xaddr)
616{
617 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
618 xaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
622{
623 struct sadb_sa *sa;
624 struct sadb_address *addr;
625 uint16_t proto;
626 unsigned short family;
627 xfrm_address_t *xaddr;
628
629 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
630 if (sa == NULL)
631 return NULL;
632
633 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
634 if (proto == 0)
635 return NULL;
636
637 /* sadb_address_len should be checked by caller */
638 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
639 if (addr == NULL)
640 return NULL;
641
642 family = ((struct sockaddr *)(addr + 1))->sa_family;
643 switch (family) {
644 case AF_INET:
645 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
646 break;
647#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
648 case AF_INET6:
649 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
650 break;
651#endif
652 default:
653 xaddr = NULL;
654 }
655
656 if (!xaddr)
657 return NULL;
658
659 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
660}
661
662#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static int
665pfkey_sockaddr_size(sa_family_t family)
666{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900667 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700670static inline int pfkey_mode_from_xfrm(int mode)
671{
672 switch(mode) {
673 case XFRM_MODE_TRANSPORT:
674 return IPSEC_MODE_TRANSPORT;
675 case XFRM_MODE_TUNNEL:
676 return IPSEC_MODE_TUNNEL;
677 case XFRM_MODE_BEET:
678 return IPSEC_MODE_BEET;
679 default:
680 return -1;
681 }
682}
683
684static inline int pfkey_mode_to_xfrm(int mode)
685{
686 switch(mode) {
687 case IPSEC_MODE_ANY: /*XXX*/
688 case IPSEC_MODE_TRANSPORT:
689 return XFRM_MODE_TRANSPORT;
690 case IPSEC_MODE_TUNNEL:
691 return XFRM_MODE_TUNNEL;
692 case IPSEC_MODE_BEET:
693 return XFRM_MODE_BEET;
694 default:
695 return -1;
696 }
697}
698
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900699static unsigned int pfkey_sockaddr_fill(xfrm_address_t *xaddr, __be16 port,
700 struct sockaddr *sa,
701 unsigned short family)
702{
703 switch (family) {
704 case AF_INET:
705 {
706 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
707 sin->sin_family = AF_INET;
708 sin->sin_port = port;
709 sin->sin_addr.s_addr = xaddr->a4;
710 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
711 return 32;
712 }
713#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
714 case AF_INET6:
715 {
716 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
717 sin6->sin6_family = AF_INET6;
718 sin6->sin6_port = port;
719 sin6->sin6_flowinfo = 0;
720 ipv6_addr_copy(&sin6->sin6_addr, (struct in6_addr *)xaddr->a6);
721 sin6->sin6_scope_id = 0;
722 return 128;
723 }
724#endif
725 }
726 return 0;
727}
728
Herbert Xu050f0092007-10-09 13:31:47 -0700729static struct sk_buff *__pfkey_xfrm_state2msg(struct xfrm_state *x,
730 int add_keys, int hsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct sk_buff *skb;
733 struct sadb_msg *hdr;
734 struct sadb_sa *sa;
735 struct sadb_lifetime *lifetime;
736 struct sadb_address *addr;
737 struct sadb_key *key;
738 struct sadb_x_sa2 *sa2;
Trent Jaegerdf718372005-12-13 23:12:27 -0800739 struct sadb_x_sec_ctx *sec_ctx;
740 struct xfrm_sec_ctx *xfrm_ctx;
741 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 int size;
743 int auth_key_size = 0;
744 int encrypt_key_size = 0;
745 int sockaddr_size;
746 struct xfrm_encap_tmpl *natt = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700747 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 /* address family check */
750 sockaddr_size = pfkey_sockaddr_size(x->props.family);
751 if (!sockaddr_size)
752 return ERR_PTR(-EINVAL);
753
754 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
755 key(AE), (identity(SD),) (sensitivity)> */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900756 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 sizeof(struct sadb_lifetime) +
758 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
759 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900760 sizeof(struct sadb_address)*2 +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 sockaddr_size*2 +
762 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800763
764 if ((xfrm_ctx = x->security)) {
765 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
766 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
767 }
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /* identity & sensitivity */
YOSHIFUJI Hideaki81b302a2008-04-28 03:17:38 +0900770 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr, x->props.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 size += sizeof(struct sadb_address) + sockaddr_size;
772
773 if (add_keys) {
774 if (x->aalg && x->aalg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900775 auth_key_size =
776 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 size += sizeof(struct sadb_key) + auth_key_size;
778 }
779 if (x->ealg && x->ealg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900780 encrypt_key_size =
781 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 size += sizeof(struct sadb_key) + encrypt_key_size;
783 }
784 }
785 if (x->encap)
786 natt = x->encap;
787
788 if (natt && natt->encap_type) {
789 size += sizeof(struct sadb_x_nat_t_type);
790 size += sizeof(struct sadb_x_nat_t_port);
791 size += sizeof(struct sadb_x_nat_t_port);
792 }
793
794 skb = alloc_skb(size + 16, GFP_ATOMIC);
795 if (skb == NULL)
796 return ERR_PTR(-ENOBUFS);
797
798 /* call should fill header later */
799 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
800 memset(hdr, 0, size); /* XXX do we need this ? */
801 hdr->sadb_msg_len = size / sizeof(uint64_t);
802
803 /* sa */
804 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
805 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
806 sa->sadb_sa_exttype = SADB_EXT_SA;
807 sa->sadb_sa_spi = x->id.spi;
808 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700809 switch (x->km.state) {
810 case XFRM_STATE_VALID:
811 sa->sadb_sa_state = x->km.dying ?
812 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
813 break;
814 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700816 break;
817 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700819 break;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 sa->sadb_sa_auth = 0;
822 if (x->aalg) {
823 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
824 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
825 }
826 sa->sadb_sa_encrypt = 0;
827 BUG_ON(x->ealg && x->calg);
828 if (x->ealg) {
829 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
830 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
831 }
832 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
833 if (x->calg) {
834 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
835 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
836 }
837
838 sa->sadb_sa_flags = 0;
839 if (x->props.flags & XFRM_STATE_NOECN)
840 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
841 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
842 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700843 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
844 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /* hard time */
847 if (hsc & 2) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900848 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 sizeof(struct sadb_lifetime));
850 lifetime->sadb_lifetime_len =
851 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
852 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
853 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
854 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
855 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
856 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
857 }
858 /* soft time */
859 if (hsc & 1) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900860 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 sizeof(struct sadb_lifetime));
862 lifetime->sadb_lifetime_len =
863 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
864 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
865 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
866 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
867 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
868 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
869 }
870 /* current time */
871 lifetime = (struct sadb_lifetime *) skb_put(skb,
872 sizeof(struct sadb_lifetime));
873 lifetime->sadb_lifetime_len =
874 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
875 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
876 lifetime->sadb_lifetime_allocations = x->curlft.packets;
877 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
878 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
879 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
880 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900881 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900883 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 (sizeof(struct sadb_address)+sockaddr_size)/
885 sizeof(uint64_t);
886 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900887 /* "if the ports are non-zero, then the sadb_address_proto field,
888 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900890 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900893 addr->sadb_address_prefixlen =
894 pfkey_sockaddr_fill(&x->props.saddr, 0,
895 (struct sockaddr *) (addr + 1),
896 x->props.family);
897 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 BUG();
899
900 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900901 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900903 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 (sizeof(struct sadb_address)+sockaddr_size)/
905 sizeof(uint64_t);
906 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900907 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900910 addr->sadb_address_prefixlen =
911 pfkey_sockaddr_fill(&x->id.daddr, 0,
912 (struct sockaddr *) (addr + 1),
913 x->props.family);
914 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 BUG();
916
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900917 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr,
918 x->props.family)) {
919 addr = (struct sadb_address*) skb_put(skb,
920 sizeof(struct sadb_address)+sockaddr_size);
921 addr->sadb_address_len =
922 (sizeof(struct sadb_address)+sockaddr_size)/
923 sizeof(uint64_t);
924 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
925 addr->sadb_address_proto =
926 pfkey_proto_from_xfrm(x->sel.proto);
927 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
928 addr->sadb_address_reserved = 0;
929
930 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
931 (struct sockaddr *) (addr + 1),
932 x->props.family);
933 }
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /* auth key */
936 if (add_keys && auth_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900937 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 sizeof(struct sadb_key)+auth_key_size);
939 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
940 sizeof(uint64_t);
941 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
942 key->sadb_key_bits = x->aalg->alg_key_len;
943 key->sadb_key_reserved = 0;
944 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
945 }
946 /* encrypt key */
947 if (add_keys && encrypt_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900948 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 sizeof(struct sadb_key)+encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900950 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 encrypt_key_size) / sizeof(uint64_t);
952 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
953 key->sadb_key_bits = x->ealg->alg_key_len;
954 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900955 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 (x->ealg->alg_key_len+7)/8);
957 }
958
959 /* sa */
960 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
961 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
962 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700963 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
964 kfree_skb(skb);
965 return ERR_PTR(-EINVAL);
966 }
967 sa2->sadb_x_sa2_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 sa2->sadb_x_sa2_reserved1 = 0;
969 sa2->sadb_x_sa2_reserved2 = 0;
970 sa2->sadb_x_sa2_sequence = 0;
971 sa2->sadb_x_sa2_reqid = x->props.reqid;
972
973 if (natt && natt->encap_type) {
974 struct sadb_x_nat_t_type *n_type;
975 struct sadb_x_nat_t_port *n_port;
976
977 /* type */
978 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
979 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
980 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
981 n_type->sadb_x_nat_t_type_type = natt->encap_type;
982 n_type->sadb_x_nat_t_type_reserved[0] = 0;
983 n_type->sadb_x_nat_t_type_reserved[1] = 0;
984 n_type->sadb_x_nat_t_type_reserved[2] = 0;
985
986 /* source port */
987 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
988 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
989 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
990 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
991 n_port->sadb_x_nat_t_port_reserved = 0;
992
993 /* dest port */
994 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
995 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
996 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
997 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
998 n_port->sadb_x_nat_t_port_reserved = 0;
999 }
1000
Trent Jaegerdf718372005-12-13 23:12:27 -08001001 /* security context */
1002 if (xfrm_ctx) {
1003 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1004 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1005 sec_ctx->sadb_x_sec_len =
1006 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1007 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1008 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1009 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1010 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1011 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1012 xfrm_ctx->ctx_len);
1013 }
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return skb;
1016}
1017
Herbert Xu050f0092007-10-09 13:31:47 -07001018
1019static inline struct sk_buff *pfkey_xfrm_state2msg(struct xfrm_state *x)
1020{
1021 struct sk_buff *skb;
1022
Herbert Xu050f0092007-10-09 13:31:47 -07001023 skb = __pfkey_xfrm_state2msg(x, 1, 3);
Herbert Xu050f0092007-10-09 13:31:47 -07001024
1025 return skb;
1026}
1027
1028static inline struct sk_buff *pfkey_xfrm_state2msg_expire(struct xfrm_state *x,
1029 int hsc)
1030{
1031 return __pfkey_xfrm_state2msg(x, 0, hsc);
1032}
1033
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001034static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 void **ext_hdrs)
1036{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001037 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct sadb_lifetime *lifetime;
1039 struct sadb_sa *sa;
1040 struct sadb_key *key;
Trent Jaegerdf718372005-12-13 23:12:27 -08001041 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 uint16_t proto;
1043 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
1047 if (!sa ||
1048 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1049 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1050 return ERR_PTR(-EINVAL);
1051 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1052 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1053 return ERR_PTR(-EINVAL);
1054 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1055 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1056 return ERR_PTR(-EINVAL);
1057 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1058 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1059 return ERR_PTR(-EINVAL);
1060
1061 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1062 if (proto == 0)
1063 return ERR_PTR(-EINVAL);
1064
1065 /* default error is no buffer space */
1066 err = -ENOBUFS;
1067
1068 /* RFC2367:
1069
1070 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1071 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1072 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1073 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1074 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1075 not true.
1076
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001077 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1079 */
1080 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1081 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1082 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1083 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1084 return ERR_PTR(-EINVAL);
1085 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1086 if (key != NULL &&
1087 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1088 ((key->sadb_key_bits+7) / 8 == 0 ||
1089 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1090 return ERR_PTR(-EINVAL);
1091 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1092 if (key != NULL &&
1093 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1094 ((key->sadb_key_bits+7) / 8 == 0 ||
1095 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1096 return ERR_PTR(-EINVAL);
1097
1098 x = xfrm_state_alloc();
1099 if (x == NULL)
1100 return ERR_PTR(-ENOBUFS);
1101
1102 x->id.proto = proto;
1103 x->id.spi = sa->sadb_sa_spi;
1104 x->props.replay_window = sa->sadb_sa_replay;
1105 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1106 x->props.flags |= XFRM_STATE_NOECN;
1107 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1108 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001109 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1110 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
1113 if (lifetime != NULL) {
1114 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1115 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1116 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1117 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1118 }
1119 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
1120 if (lifetime != NULL) {
1121 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1122 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1123 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1124 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1125 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001126
1127 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
1128 if (sec_ctx != NULL) {
1129 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1130
1131 if (!uctx)
1132 goto out;
1133
1134 err = security_xfrm_state_alloc(x, uctx);
1135 kfree(uctx);
1136
1137 if (err)
1138 goto out;
1139 }
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1142 if (sa->sadb_sa_auth) {
1143 int keysize = 0;
1144 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1145 if (!a) {
1146 err = -ENOSYS;
1147 goto out;
1148 }
1149 if (key)
1150 keysize = (key->sadb_key_bits + 7) / 8;
1151 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1152 if (!x->aalg)
1153 goto out;
1154 strcpy(x->aalg->alg_name, a->name);
1155 x->aalg->alg_key_len = 0;
1156 if (key) {
1157 x->aalg->alg_key_len = key->sadb_key_bits;
1158 memcpy(x->aalg->alg_key, key+1, keysize);
1159 }
1160 x->props.aalgo = sa->sadb_sa_auth;
1161 /* x->algo.flags = sa->sadb_sa_flags; */
1162 }
1163 if (sa->sadb_sa_encrypt) {
1164 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1165 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1166 if (!a) {
1167 err = -ENOSYS;
1168 goto out;
1169 }
1170 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1171 if (!x->calg)
1172 goto out;
1173 strcpy(x->calg->alg_name, a->name);
1174 x->props.calgo = sa->sadb_sa_encrypt;
1175 } else {
1176 int keysize = 0;
1177 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1178 if (!a) {
1179 err = -ENOSYS;
1180 goto out;
1181 }
1182 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1183 if (key)
1184 keysize = (key->sadb_key_bits + 7) / 8;
1185 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1186 if (!x->ealg)
1187 goto out;
1188 strcpy(x->ealg->alg_name, a->name);
1189 x->ealg->alg_key_len = 0;
1190 if (key) {
1191 x->ealg->alg_key_len = key->sadb_key_bits;
1192 memcpy(x->ealg->alg_key, key+1, keysize);
1193 }
1194 x->props.ealgo = sa->sadb_sa_encrypt;
1195 }
1196 }
1197 /* x->algo.flags = sa->sadb_sa_flags; */
1198
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001199 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 &x->props.saddr);
1201 if (!x->props.family) {
1202 err = -EAFNOSUPPORT;
1203 goto out;
1204 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001205 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 &x->id.daddr);
1207
1208 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1209 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001210 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1211 if (mode < 0) {
1212 err = -EINVAL;
1213 goto out;
1214 }
1215 x->props.mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 x->props.reqid = sa2->sadb_x_sa2_reqid;
1217 }
1218
1219 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1220 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1221
1222 /* Nobody uses this, but we try. */
1223 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1224 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1225 }
1226
Kazunori MIYAZAWA4da51052008-05-21 13:26:11 -07001227 if (!x->sel.family)
Joy Latten4a4b6272007-08-02 19:25:43 -07001228 x->sel.family = x->props.family;
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1231 struct sadb_x_nat_t_type* n_type;
1232 struct xfrm_encap_tmpl *natt;
1233
1234 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1235 if (!x->encap)
1236 goto out;
1237
1238 natt = x->encap;
1239 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1240 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1241
1242 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1243 struct sadb_x_nat_t_port* n_port =
1244 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1245 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1246 }
1247 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1248 struct sadb_x_nat_t_port* n_port =
1249 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1250 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1251 }
1252 }
1253
Herbert Xu72cb6962005-06-20 13:18:08 -07001254 err = xfrm_init_state(x);
1255 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return x;
1260
1261out:
1262 x->km.state = XFRM_STATE_DEAD;
1263 xfrm_state_put(x);
1264 return ERR_PTR(err);
1265}
1266
1267static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1268{
1269 return -EOPNOTSUPP;
1270}
1271
1272static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1273{
1274 struct sk_buff *resp_skb;
1275 struct sadb_x_sa2 *sa2;
1276 struct sadb_address *saddr, *daddr;
1277 struct sadb_msg *out_hdr;
Herbert Xu658b2192007-10-09 13:29:52 -07001278 struct sadb_spirange *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 struct xfrm_state *x = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001280 int mode;
Herbert Xu658b2192007-10-09 13:29:52 -07001281 int err;
1282 u32 min_spi, max_spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 u32 reqid;
1284 u8 proto;
1285 unsigned short family;
1286 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1287
1288 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1289 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1290 return -EINVAL;
1291
1292 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1293 if (proto == 0)
1294 return -EINVAL;
1295
1296 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001297 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1298 if (mode < 0)
1299 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 reqid = sa2->sadb_x_sa2_reqid;
1301 } else {
1302 mode = 0;
1303 reqid = 0;
1304 }
1305
1306 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1307 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1308
1309 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1310 switch (family) {
1311 case AF_INET:
1312 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1313 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1314 break;
1315#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1316 case AF_INET6:
1317 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1318 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1319 break;
1320#endif
1321 }
1322
1323 if (hdr->sadb_msg_seq) {
1324 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1325 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1326 xfrm_state_put(x);
1327 x = NULL;
1328 }
1329 }
1330
1331 if (!x)
1332 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1333
1334 if (x == NULL)
1335 return -ENOENT;
1336
Herbert Xu658b2192007-10-09 13:29:52 -07001337 min_spi = 0x100;
1338 max_spi = 0x0fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Herbert Xu658b2192007-10-09 13:29:52 -07001340 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1341 if (range) {
1342 min_spi = range->sadb_spirange_min;
1343 max_spi = range->sadb_spirange_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
Herbert Xu658b2192007-10-09 13:29:52 -07001345
1346 err = xfrm_alloc_spi(x, min_spi, max_spi);
Herbert Xu050f0092007-10-09 13:31:47 -07001347 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 if (IS_ERR(resp_skb)) {
1350 xfrm_state_put(x);
1351 return PTR_ERR(resp_skb);
1352 }
1353
1354 out_hdr = (struct sadb_msg *) resp_skb->data;
1355 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1356 out_hdr->sadb_msg_type = SADB_GETSPI;
1357 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1358 out_hdr->sadb_msg_errno = 0;
1359 out_hdr->sadb_msg_reserved = 0;
1360 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1361 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1362
1363 xfrm_state_put(x);
1364
1365 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1366
1367 return 0;
1368}
1369
1370static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1371{
1372 struct xfrm_state *x;
1373
1374 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1375 return -EOPNOTSUPP;
1376
1377 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1378 return 0;
1379
1380 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1381 if (x == NULL)
1382 return 0;
1383
1384 spin_lock_bh(&x->lock);
1385 if (x->km.state == XFRM_STATE_ACQ) {
1386 x->km.state = XFRM_STATE_ERROR;
1387 wake_up(&km_waitq);
1388 }
1389 spin_unlock_bh(&x->lock);
1390 xfrm_state_put(x);
1391 return 0;
1392}
1393
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001394static inline int event2poltype(int event)
1395{
1396 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001397 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001398 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001399 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001400 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001401 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001402 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001403 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001404 // return SADB_X_SPDEXPIRE;
1405 default:
1406 printk("pfkey: Unknown policy event %d\n", event);
1407 break;
1408 }
1409
1410 return 0;
1411}
1412
1413static inline int event2keytype(int event)
1414{
1415 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001416 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001417 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001418 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001419 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001420 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001421 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001422 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001423 return SADB_EXPIRE;
1424 default:
1425 printk("pfkey: Unknown SA event %d\n", event);
1426 break;
1427 }
1428
1429 return 0;
1430}
1431
1432/* ADD/UPD/DEL */
1433static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1434{
1435 struct sk_buff *skb;
1436 struct sadb_msg *hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001437
Herbert Xu050f0092007-10-09 13:31:47 -07001438 skb = pfkey_xfrm_state2msg(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001439
1440 if (IS_ERR(skb))
1441 return PTR_ERR(skb);
1442
1443 hdr = (struct sadb_msg *) skb->data;
1444 hdr->sadb_msg_version = PF_KEY_V2;
1445 hdr->sadb_msg_type = event2keytype(c->event);
1446 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1447 hdr->sadb_msg_errno = 0;
1448 hdr->sadb_msg_reserved = 0;
1449 hdr->sadb_msg_seq = c->seq;
1450 hdr->sadb_msg_pid = c->pid;
1451
1452 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1453
1454 return 0;
1455}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1458{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 struct xfrm_state *x;
1460 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001461 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1464 if (IS_ERR(x))
1465 return PTR_ERR(x);
1466
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001467 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (hdr->sadb_msg_type == SADB_ADD)
1469 err = xfrm_state_add(x);
1470 else
1471 err = xfrm_state_update(x);
1472
Joy Lattenab5f5e82007-09-17 11:51:22 -07001473 xfrm_audit_state_add(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001474 audit_get_loginuid(current),
1475 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (err < 0) {
1478 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001479 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001480 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001483 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001484 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001485 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001486 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001487 c.seq = hdr->sadb_msg_seq;
1488 c.pid = hdr->sadb_msg_pid;
1489 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001490out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001491 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001492 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
1495static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1496{
1497 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001498 struct km_event c;
1499 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 if (!ext_hdrs[SADB_EXT_SA-1] ||
1502 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1503 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1504 return -EINVAL;
1505
1506 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1507 if (x == NULL)
1508 return -ESRCH;
1509
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001510 if ((err = security_xfrm_state_delete(x)))
1511 goto out;
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001514 err = -EPERM;
1515 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
Joy Latten161a09e2006-11-27 13:11:54 -06001517
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001518 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001519
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001520 if (err < 0)
1521 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001522
1523 c.seq = hdr->sadb_msg_seq;
1524 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001525 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001526 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001527out:
Joy Lattenab5f5e82007-09-17 11:51:22 -07001528 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001529 audit_get_loginuid(current),
1530 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 xfrm_state_put(x);
1532
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001533 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1537{
1538 __u8 proto;
1539 struct sk_buff *out_skb;
1540 struct sadb_msg *out_hdr;
1541 struct xfrm_state *x;
1542
1543 if (!ext_hdrs[SADB_EXT_SA-1] ||
1544 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1545 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1546 return -EINVAL;
1547
1548 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1549 if (x == NULL)
1550 return -ESRCH;
1551
Herbert Xu050f0092007-10-09 13:31:47 -07001552 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 proto = x->id.proto;
1554 xfrm_state_put(x);
1555 if (IS_ERR(out_skb))
1556 return PTR_ERR(out_skb);
1557
1558 out_hdr = (struct sadb_msg *) out_skb->data;
1559 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Charles Hardin435000b2007-11-22 19:35:15 +08001560 out_hdr->sadb_msg_type = SADB_GET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1562 out_hdr->sadb_msg_errno = 0;
1563 out_hdr->sadb_msg_reserved = 0;
1564 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1565 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1566 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1567
1568 return 0;
1569}
1570
Randy Dunlap00fa0232005-10-04 22:43:04 -07001571static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001572 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 struct sk_buff *skb;
1575 struct sadb_msg *hdr;
1576 int len, auth_len, enc_len, i;
1577
1578 auth_len = xfrm_count_auth_supported();
1579 if (auth_len) {
1580 auth_len *= sizeof(struct sadb_alg);
1581 auth_len += sizeof(struct sadb_supported);
1582 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 enc_len = xfrm_count_enc_supported();
1585 if (enc_len) {
1586 enc_len *= sizeof(struct sadb_alg);
1587 enc_len += sizeof(struct sadb_supported);
1588 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 len = enc_len + auth_len + sizeof(struct sadb_msg);
1591
1592 skb = alloc_skb(len + 16, allocation);
1593 if (!skb)
1594 goto out_put_algs;
1595
1596 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1597 pfkey_hdr_dup(hdr, orig);
1598 hdr->sadb_msg_errno = 0;
1599 hdr->sadb_msg_len = len / sizeof(uint64_t);
1600
1601 if (auth_len) {
1602 struct sadb_supported *sp;
1603 struct sadb_alg *ap;
1604
1605 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1606 ap = (struct sadb_alg *) (sp + 1);
1607
1608 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1609 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1610
1611 for (i = 0; ; i++) {
1612 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1613 if (!aalg)
1614 break;
1615 if (aalg->available)
1616 *ap++ = aalg->desc;
1617 }
1618 }
1619
1620 if (enc_len) {
1621 struct sadb_supported *sp;
1622 struct sadb_alg *ap;
1623
1624 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1625 ap = (struct sadb_alg *) (sp + 1);
1626
1627 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1628 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1629
1630 for (i = 0; ; i++) {
1631 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1632 if (!ealg)
1633 break;
1634 if (ealg->available)
1635 *ap++ = ealg->desc;
1636 }
1637 }
1638
1639out_put_algs:
1640 return skb;
1641}
1642
1643static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1644{
1645 struct pfkey_sock *pfk = pfkey_sk(sk);
1646 struct sk_buff *supp_skb;
1647
1648 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1649 return -EINVAL;
1650
1651 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1652 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1653 return -EEXIST;
1654 pfk->registered |= (1<<hdr->sadb_msg_satype);
1655 }
1656
1657 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1660 if (!supp_skb) {
1661 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1662 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1663
1664 return -ENOBUFS;
1665 }
1666
1667 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1668
1669 return 0;
1670}
1671
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001672static int key_notify_sa_flush(struct km_event *c)
1673{
1674 struct sk_buff *skb;
1675 struct sadb_msg *hdr;
1676
1677 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1678 if (!skb)
1679 return -ENOBUFS;
1680 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf088672005-06-18 22:44:00 -07001681 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001682 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001683 hdr->sadb_msg_seq = c->seq;
1684 hdr->sadb_msg_pid = c->pid;
1685 hdr->sadb_msg_version = PF_KEY_V2;
1686 hdr->sadb_msg_errno = (uint8_t) 0;
1687 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1688
1689 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1690
1691 return 0;
1692}
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1695{
1696 unsigned proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001697 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06001698 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001699 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1702 if (proto == 0)
1703 return -EINVAL;
1704
Al Viro0c11b942008-01-10 04:20:52 -05001705 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04001706 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06001707 audit_info.secid = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -04001708 err = xfrm_state_flush(proto, &audit_info);
1709 if (err)
1710 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001711 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001712 c.seq = hdr->sadb_msg_seq;
1713 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001714 c.event = XFRM_MSG_FLUSHSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001715 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 return 0;
1718}
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1721{
Timo Teras83321d62008-03-03 23:40:12 -08001722 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 struct sk_buff *out_skb;
1724 struct sadb_msg *out_hdr;
1725
Timo Teras83321d62008-03-03 23:40:12 -08001726 if (!pfkey_can_dump(&pfk->sk))
1727 return -ENOBUFS;
1728
Herbert Xu050f0092007-10-09 13:31:47 -07001729 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (IS_ERR(out_skb))
1731 return PTR_ERR(out_skb);
1732
1733 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08001734 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 out_hdr->sadb_msg_type = SADB_DUMP;
1736 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1737 out_hdr->sadb_msg_errno = 0;
1738 out_hdr->sadb_msg_reserved = 0;
1739 out_hdr->sadb_msg_seq = count;
Timo Teras83321d62008-03-03 23:40:12 -08001740 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
1741 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, &pfk->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return 0;
1743}
1744
Timo Teras83321d62008-03-03 23:40:12 -08001745static int pfkey_dump_sa(struct pfkey_sock *pfk)
1746{
1747 return xfrm_state_walk(&pfk->dump.u.state, dump_sa, (void *) pfk);
1748}
1749
1750static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1751{
1752 xfrm_state_walk_done(&pfk->dump.u.state);
1753}
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1756{
1757 u8 proto;
Timo Teras83321d62008-03-03 23:40:12 -08001758 struct pfkey_sock *pfk = pfkey_sk(sk);
1759
1760 if (pfk->dump.dump != NULL)
1761 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1764 if (proto == 0)
1765 return -EINVAL;
1766
Timo Teras83321d62008-03-03 23:40:12 -08001767 pfk->dump.msg_version = hdr->sadb_msg_version;
1768 pfk->dump.msg_pid = hdr->sadb_msg_pid;
1769 pfk->dump.dump = pfkey_dump_sa;
1770 pfk->dump.done = pfkey_dump_sa_done;
1771 xfrm_state_walk_init(&pfk->dump.u.state, proto);
Timo Teras4c563f72008-02-28 21:31:08 -08001772
Timo Teras83321d62008-03-03 23:40:12 -08001773 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
1776static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1777{
1778 struct pfkey_sock *pfk = pfkey_sk(sk);
1779 int satype = hdr->sadb_msg_satype;
1780
1781 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1782 /* XXX we mangle packet... */
1783 hdr->sadb_msg_errno = 0;
1784 if (satype != 0 && satype != 1)
1785 return -EINVAL;
1786 pfk->promisc = satype;
1787 }
1788 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1789 return 0;
1790}
1791
1792static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1793{
1794 int i;
1795 u32 reqid = *(u32*)ptr;
1796
1797 for (i=0; i<xp->xfrm_nr; i++) {
1798 if (xp->xfrm_vec[i].reqid == reqid)
1799 return -EEXIST;
1800 }
1801 return 0;
1802}
1803
1804static u32 gen_reqid(void)
1805{
Timo Teras4c563f72008-02-28 21:31:08 -08001806 struct xfrm_policy_walk walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 u32 start;
Timo Teras4c563f72008-02-28 21:31:08 -08001808 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1810
1811 start = reqid;
1812 do {
1813 ++reqid;
1814 if (reqid == 0)
1815 reqid = IPSEC_MANUAL_REQID_MAX+1;
Timo Teras4c563f72008-02-28 21:31:08 -08001816 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
1817 rc = xfrm_policy_walk(&walk, check_reqid, (void*)&reqid);
1818 xfrm_policy_walk_done(&walk);
1819 if (rc != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return reqid;
1821 } while (reqid != start);
1822 return 0;
1823}
1824
1825static int
1826parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1827{
1828 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001829 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1832 return -ELOOP;
1833
1834 if (rq->sadb_x_ipsecrequest_mode == 0)
1835 return -EINVAL;
1836
1837 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001838 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1839 return -EINVAL;
1840 t->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1842 t->optional = 1;
1843 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1844 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1845 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1846 t->reqid = 0;
1847 if (!t->reqid && !(t->reqid = gen_reqid()))
1848 return -ENOBUFS;
1849 }
1850
1851 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001852 if (t->mode == XFRM_MODE_TUNNEL) {
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001853 u8 *sa = (u8 *) (rq + 1);
1854 int family, socklen;
1855
1856 family = pfkey_sockaddr_extract((struct sockaddr *)sa,
1857 &t->saddr);
1858 if (!family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return -EINVAL;
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001860
1861 socklen = pfkey_sockaddr_len(family);
1862 if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen),
1863 &t->id.daddr) != family)
1864 return -EINVAL;
1865 t->encap_family = family;
Miika Komu2718aa72006-11-30 16:41:50 -08001866 } else
1867 t->encap_family = xp->family;
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /* No way to set this via kame pfkey */
Herbert Xuc5d18e92008-04-22 00:46:42 -07001870 t->allalgs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 xp->xfrm_nr++;
1872 return 0;
1873}
1874
1875static int
1876parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1877{
1878 int err;
1879 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1880 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1881
1882 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1883 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1884 return err;
1885 len -= rq->sadb_x_ipsecrequest_len;
1886 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1887 }
1888 return 0;
1889}
1890
Trent Jaegerdf718372005-12-13 23:12:27 -08001891static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1892{
1893 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1894
1895 if (xfrm_ctx) {
1896 int len = sizeof(struct sadb_x_sec_ctx);
1897 len += xfrm_ctx->ctx_len;
1898 return PFKEY_ALIGN8(len);
1899 }
1900 return 0;
1901}
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1904{
Miika Komu2718aa72006-11-30 16:41:50 -08001905 struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08001907 int socklen = 0;
1908 int i;
1909
1910 for (i=0; i<xp->xfrm_nr; i++) {
1911 t = xp->xfrm_vec + i;
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09001912 socklen += pfkey_sockaddr_len(t->encap_family);
Miika Komu2718aa72006-11-30 16:41:50 -08001913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 return sizeof(struct sadb_msg) +
1916 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001917 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 (sockaddr_size * 2) +
1919 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08001920 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1921 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08001922 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
1925static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1926{
1927 struct sk_buff *skb;
1928 int size;
1929
1930 size = pfkey_xfrm_policy2msg_size(xp);
1931
1932 skb = alloc_skb(size + 16, GFP_ATOMIC);
1933 if (skb == NULL)
1934 return ERR_PTR(-ENOBUFS);
1935
1936 return skb;
1937}
1938
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001939static int pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
1941 struct sadb_msg *hdr;
1942 struct sadb_address *addr;
1943 struct sadb_lifetime *lifetime;
1944 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08001945 struct sadb_x_sec_ctx *sec_ctx;
1946 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 int i;
1948 int size;
1949 int sockaddr_size = pfkey_sockaddr_size(xp->family);
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09001950 int socklen = pfkey_sockaddr_len(xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 size = pfkey_xfrm_policy2msg_size(xp);
1953
1954 /* call should fill header later */
1955 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1956 memset(hdr, 0, size); /* XXX do we need this ? */
1957
1958 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001959 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001961 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 (sizeof(struct sadb_address)+sockaddr_size)/
1963 sizeof(uint64_t);
1964 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1965 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1966 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1967 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09001968 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
1969 xp->selector.sport,
1970 (struct sockaddr *) (addr + 1),
1971 xp->family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 BUG();
1973
1974 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001975 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 sizeof(struct sadb_address)+sockaddr_size);
1977 addr->sadb_address_len =
1978 (sizeof(struct sadb_address)+sockaddr_size)/
1979 sizeof(uint64_t);
1980 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1981 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001982 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09001984
1985 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
1986 (struct sockaddr *) (addr + 1),
1987 xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 /* hard time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001990 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 sizeof(struct sadb_lifetime));
1992 lifetime->sadb_lifetime_len =
1993 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1994 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1995 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
1996 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
1997 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
1998 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
1999 /* soft time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002000 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 sizeof(struct sadb_lifetime));
2002 lifetime->sadb_lifetime_len =
2003 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2004 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2005 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2006 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2007 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2008 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2009 /* current time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002010 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 sizeof(struct sadb_lifetime));
2012 lifetime->sadb_lifetime_len =
2013 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2014 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2015 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2016 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2017 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2018 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2019
2020 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2021 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2022 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2023 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2024 if (xp->action == XFRM_POLICY_ALLOW) {
2025 if (xp->xfrm_nr)
2026 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2027 else
2028 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2029 }
2030 pol->sadb_x_policy_dir = dir+1;
2031 pol->sadb_x_policy_id = xp->index;
2032 pol->sadb_x_policy_priority = xp->priority;
2033
2034 for (i=0; i<xp->xfrm_nr; i++) {
2035 struct sadb_x_ipsecrequest *rq;
2036 struct xfrm_tmpl *t = xp->xfrm_vec + i;
2037 int req_size;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002038 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 req_size = sizeof(struct sadb_x_ipsecrequest);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002041 if (t->mode == XFRM_MODE_TUNNEL) {
2042 socklen = pfkey_sockaddr_len(t->encap_family);
2043 req_size += socklen * 2;
2044 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 size -= 2*socklen;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002046 socklen = 0;
2047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 rq = (void*)skb_put(skb, req_size);
2049 pol->sadb_x_policy_len += req_size/8;
2050 memset(rq, 0, sizeof(*rq));
2051 rq->sadb_x_ipsecrequest_len = req_size;
2052 rq->sadb_x_ipsecrequest_proto = t->id.proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002053 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2054 return -EINVAL;
David S. Millerfefaa752007-04-17 21:48:10 -07002055 rq->sadb_x_ipsecrequest_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2057 if (t->reqid)
2058 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2059 if (t->optional)
2060 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2061 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002063 if (t->mode == XFRM_MODE_TUNNEL) {
2064 u8 *sa = (void *)(rq + 1);
2065 pfkey_sockaddr_fill(&t->saddr, 0,
2066 (struct sockaddr *)sa,
2067 t->encap_family);
2068 pfkey_sockaddr_fill(&t->id.daddr, 0,
2069 (struct sockaddr *) (sa + socklen),
2070 t->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002073
2074 /* security context */
2075 if ((xfrm_ctx = xp->security)) {
2076 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2077
2078 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2079 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2080 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2081 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2082 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2083 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2084 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2085 xfrm_ctx->ctx_len);
2086 }
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 hdr->sadb_msg_len = size / sizeof(uint64_t);
2089 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002090
2091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002094static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2095{
2096 struct sk_buff *out_skb;
2097 struct sadb_msg *out_hdr;
2098 int err;
2099
2100 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2101 if (IS_ERR(out_skb)) {
2102 err = PTR_ERR(out_skb);
2103 goto out;
2104 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002105 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2106 if (err < 0)
2107 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002108
2109 out_hdr = (struct sadb_msg *) out_skb->data;
2110 out_hdr->sadb_msg_version = PF_KEY_V2;
2111
Herbert Xuf60f6b82005-06-18 22:44:37 -07002112 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002113 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2114 else
2115 out_hdr->sadb_msg_type = event2poltype(c->event);
2116 out_hdr->sadb_msg_errno = 0;
2117 out_hdr->sadb_msg_seq = c->seq;
2118 out_hdr->sadb_msg_pid = c->pid;
2119 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
2120out:
2121 return 0;
2122
2123}
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2126{
Trent Jaegerdf718372005-12-13 23:12:27 -08002127 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 struct sadb_lifetime *lifetime;
2129 struct sadb_address *sa;
2130 struct sadb_x_policy *pol;
2131 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002132 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002133 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2136 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2137 !ext_hdrs[SADB_X_EXT_POLICY-1])
2138 return -EINVAL;
2139
2140 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2141 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2142 return -EINVAL;
2143 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2144 return -EINVAL;
2145
2146 xp = xfrm_policy_alloc(GFP_KERNEL);
2147 if (xp == NULL)
2148 return -ENOBUFS;
2149
2150 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2151 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2152 xp->priority = pol->sadb_x_policy_priority;
2153
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002154 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2156 if (!xp->family) {
2157 err = -EINVAL;
2158 goto out;
2159 }
2160 xp->selector.family = xp->family;
2161 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2162 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2163 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2164 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002165 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002167 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2169 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2170
2171 /* Amusing, we set this twice. KAME apps appear to set same value
2172 * in both addresses.
2173 */
2174 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2175
2176 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2177 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002178 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Trent Jaegerdf718372005-12-13 23:12:27 -08002180 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2181 if (sec_ctx != NULL) {
2182 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2183
2184 if (!uctx) {
2185 err = -ENOBUFS;
2186 goto out;
2187 }
2188
Paul Moore03e1ad72008-04-12 19:07:52 -07002189 err = security_xfrm_policy_alloc(&xp->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002190 kfree(uctx);
2191
2192 if (err)
2193 goto out;
2194 }
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 xp->lft.soft_byte_limit = XFRM_INF;
2197 xp->lft.hard_byte_limit = XFRM_INF;
2198 xp->lft.soft_packet_limit = XFRM_INF;
2199 xp->lft.hard_packet_limit = XFRM_INF;
2200 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2201 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2202 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2203 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2204 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2205 }
2206 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2207 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2208 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2209 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2210 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2211 }
2212 xp->xfrm_nr = 0;
2213 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2214 (err = parse_ipsecrequests(xp, pol)) < 0)
2215 goto out;
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2218 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002219
Joy Lattenab5f5e82007-09-17 11:51:22 -07002220 xfrm_audit_policy_add(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002221 audit_get_loginuid(current),
2222 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06002223
Trent Jaegerdf718372005-12-13 23:12:27 -08002224 if (err)
2225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002227 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002228 c.event = XFRM_MSG_UPDPOLICY;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002229 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07002230 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002232 c.seq = hdr->sadb_msg_seq;
2233 c.pid = hdr->sadb_msg_pid;
2234
2235 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return 0;
2238
2239out:
Kazunori MIYAZAWAa4d6b8a2008-02-14 14:51:38 -08002240 xp->dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08002241 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return err;
2243}
2244
2245static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2246{
2247 int err;
2248 struct sadb_address *sa;
2249 struct sadb_x_policy *pol;
Paul Moore03e1ad72008-04-12 19:07:52 -07002250 struct xfrm_policy *xp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002252 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002253 struct sadb_x_sec_ctx *sec_ctx;
Brian Haley2db3e472008-04-24 20:38:31 -07002254 struct xfrm_sec_ctx *pol_ctx = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2257 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2258 !ext_hdrs[SADB_X_EXT_POLICY-1])
2259 return -EINVAL;
2260
2261 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2262 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2263 return -EINVAL;
2264
2265 memset(&sel, 0, sizeof(sel));
2266
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002267 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2269 sel.prefixlen_s = sa->sadb_address_prefixlen;
2270 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2271 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2272 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002273 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002275 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2277 sel.prefixlen_d = sa->sadb_address_prefixlen;
2278 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2279 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2280 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002281 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Trent Jaegerdf718372005-12-13 23:12:27 -08002283 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002284 if (sec_ctx != NULL) {
2285 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2286
2287 if (!uctx)
2288 return -ENOMEM;
2289
Paul Moore03e1ad72008-04-12 19:07:52 -07002290 err = security_xfrm_policy_alloc(&pol_ctx, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002291 kfree(uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002292 if (err)
2293 return err;
Brian Haley2db3e472008-04-24 20:38:31 -07002294 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002295
Paul Moore03e1ad72008-04-12 19:07:52 -07002296 xp = xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN,
2297 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2298 1, &err);
2299 security_xfrm_policy_free(pol_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (xp == NULL)
2301 return -ENOENT;
2302
Joy Lattenab5f5e82007-09-17 11:51:22 -07002303 xfrm_audit_policy_delete(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002304 audit_get_loginuid(current),
2305 audit_get_sessionid(current), 0);
David S. Miller13fcfbb2007-02-12 13:53:54 -08002306
2307 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002308 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08002309
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002310 c.seq = hdr->sadb_msg_seq;
2311 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002312 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002313 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2314
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002315out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002316 xfrm_pol_put(xp);
2317 return err;
2318}
2319
2320static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2321{
2322 int err;
2323 struct sk_buff *out_skb;
2324 struct sadb_msg *out_hdr;
2325 err = 0;
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2328 if (IS_ERR(out_skb)) {
2329 err = PTR_ERR(out_skb);
2330 goto out;
2331 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002332 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2333 if (err < 0)
2334 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 out_hdr = (struct sadb_msg *) out_skb->data;
2337 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002338 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 out_hdr->sadb_msg_satype = 0;
2340 out_hdr->sadb_msg_errno = 0;
2341 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2342 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002343 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 err = 0;
2345
2346out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return err;
2348}
2349
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002350#ifdef CONFIG_NET_KEY_MIGRATE
2351static int pfkey_sockaddr_pair_size(sa_family_t family)
2352{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002353 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002354}
2355
2356static int parse_sockaddr_pair(struct sadb_x_ipsecrequest *rq,
2357 xfrm_address_t *saddr, xfrm_address_t *daddr,
2358 u16 *family)
2359{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002360 u8 *sa = (u8 *) (rq + 1);
2361 int af, socklen;
2362
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002363 if (rq->sadb_x_ipsecrequest_len <
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002364 pfkey_sockaddr_pair_size(((struct sockaddr *)sa)->sa_family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002365 return -EINVAL;
2366
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002367 af = pfkey_sockaddr_extract((struct sockaddr *) sa,
2368 saddr);
2369 if (!af)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002370 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002371
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002372 socklen = pfkey_sockaddr_len(af);
2373 if (pfkey_sockaddr_extract((struct sockaddr *) (sa + socklen),
2374 daddr) != af)
2375 return -EINVAL;
2376
2377 *family = af;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002378 return 0;
2379}
2380
2381static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2382 struct xfrm_migrate *m)
2383{
2384 int err;
2385 struct sadb_x_ipsecrequest *rq2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002386 int mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002387
2388 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2389 len < rq1->sadb_x_ipsecrequest_len)
2390 return -EINVAL;
2391
2392 /* old endoints */
2393 err = parse_sockaddr_pair(rq1, &m->old_saddr, &m->old_daddr,
2394 &m->old_family);
2395 if (err)
2396 return err;
2397
2398 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2399 len -= rq1->sadb_x_ipsecrequest_len;
2400
2401 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2402 len < rq2->sadb_x_ipsecrequest_len)
2403 return -EINVAL;
2404
2405 /* new endpoints */
2406 err = parse_sockaddr_pair(rq2, &m->new_saddr, &m->new_daddr,
2407 &m->new_family);
2408 if (err)
2409 return err;
2410
2411 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2412 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2413 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2414 return -EINVAL;
2415
2416 m->proto = rq1->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002417 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2418 return -EINVAL;
2419 m->mode = mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002420 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2421
2422 return ((int)(rq1->sadb_x_ipsecrequest_len +
2423 rq2->sadb_x_ipsecrequest_len));
2424}
2425
2426static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2427 struct sadb_msg *hdr, void **ext_hdrs)
2428{
2429 int i, len, ret, err = -EINVAL;
2430 u8 dir;
2431 struct sadb_address *sa;
2432 struct sadb_x_policy *pol;
2433 struct sadb_x_ipsecrequest *rq;
2434 struct xfrm_selector sel;
2435 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2436
2437 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2438 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2439 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2440 err = -EINVAL;
2441 goto out;
2442 }
2443
2444 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2445 if (!pol) {
2446 err = -EINVAL;
2447 goto out;
2448 }
2449
2450 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2451 err = -EINVAL;
2452 goto out;
2453 }
2454
2455 dir = pol->sadb_x_policy_dir - 1;
2456 memset(&sel, 0, sizeof(sel));
2457
2458 /* set source address info of selector */
2459 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2460 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2461 sel.prefixlen_s = sa->sadb_address_prefixlen;
2462 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2463 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2464 if (sel.sport)
Al Viro582ee432007-07-26 17:33:39 +01002465 sel.sport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002466
2467 /* set destination address info of selector */
2468 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2469 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2470 sel.prefixlen_d = sa->sadb_address_prefixlen;
2471 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2472 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2473 if (sel.dport)
Al Viro582ee432007-07-26 17:33:39 +01002474 sel.dport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002475
2476 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2477
2478 /* extract ipsecrequests */
2479 i = 0;
2480 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2481
2482 while (len > 0 && i < XFRM_MAX_DEPTH) {
2483 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2484 if (ret < 0) {
2485 err = ret;
2486 goto out;
2487 } else {
2488 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2489 len -= ret;
2490 i++;
2491 }
2492 }
2493
2494 if (!i || len > 0) {
2495 err = -EINVAL;
2496 goto out;
2497 }
2498
2499 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i);
2500
2501 out:
2502 return err;
2503}
2504#else
2505static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2506 struct sadb_msg *hdr, void **ext_hdrs)
2507{
2508 return -ENOPROTOOPT;
2509}
2510#endif
2511
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2514{
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002515 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002516 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 struct sadb_x_policy *pol;
2518 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002519 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2522 return -EINVAL;
2523
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002524 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2525 if (dir >= XFRM_POLICY_MAX)
2526 return -EINVAL;
2527
Eric Paris215a2dd2007-03-07 16:01:45 -08002528 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002529 xp = xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN, dir, pol->sadb_x_policy_id,
Eric Paris215a2dd2007-03-07 16:01:45 -08002530 delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (xp == NULL)
2532 return -ENOENT;
2533
Eric Paris215a2dd2007-03-07 16:01:45 -08002534 if (delete) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002535 xfrm_audit_policy_delete(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002536 audit_get_loginuid(current),
2537 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Eric Paris215a2dd2007-03-07 16:01:45 -08002539 if (err)
2540 goto out;
2541 c.seq = hdr->sadb_msg_seq;
2542 c.pid = hdr->sadb_msg_pid;
Herbert Xubf088672005-06-18 22:44:00 -07002543 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002544 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002545 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002546 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002547 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Eric Paris215a2dd2007-03-07 16:01:45 -08002550out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 xfrm_pol_put(xp);
2552 return err;
2553}
2554
2555static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2556{
Timo Teras83321d62008-03-03 23:40:12 -08002557 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 struct sk_buff *out_skb;
2559 struct sadb_msg *out_hdr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002560 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Timo Teras83321d62008-03-03 23:40:12 -08002562 if (!pfkey_can_dump(&pfk->sk))
2563 return -ENOBUFS;
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2566 if (IS_ERR(out_skb))
2567 return PTR_ERR(out_skb);
2568
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002569 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2570 if (err < 0)
2571 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08002574 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2576 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2577 out_hdr->sadb_msg_errno = 0;
2578 out_hdr->sadb_msg_seq = count;
Timo Teras83321d62008-03-03 23:40:12 -08002579 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
2580 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, &pfk->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return 0;
2582}
2583
Timo Teras83321d62008-03-03 23:40:12 -08002584static int pfkey_dump_sp(struct pfkey_sock *pfk)
2585{
2586 return xfrm_policy_walk(&pfk->dump.u.policy, dump_sp, (void *) pfk);
2587}
2588
2589static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2590{
2591 xfrm_policy_walk_done(&pfk->dump.u.policy);
2592}
2593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2595{
Timo Teras83321d62008-03-03 23:40:12 -08002596 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Timo Teras83321d62008-03-03 23:40:12 -08002598 if (pfk->dump.dump != NULL)
2599 return -EBUSY;
Timo Teras4c563f72008-02-28 21:31:08 -08002600
Timo Teras83321d62008-03-03 23:40:12 -08002601 pfk->dump.msg_version = hdr->sadb_msg_version;
2602 pfk->dump.msg_pid = hdr->sadb_msg_pid;
2603 pfk->dump.dump = pfkey_dump_sp;
2604 pfk->dump.done = pfkey_dump_sp_done;
2605 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2606
2607 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002610static int key_notify_policy_flush(struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
2612 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002613 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002615 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 if (!skb_out)
2617 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002618 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002619 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002620 hdr->sadb_msg_seq = c->seq;
2621 hdr->sadb_msg_pid = c->pid;
2622 hdr->sadb_msg_version = PF_KEY_V2;
2623 hdr->sadb_msg_errno = (uint8_t) 0;
2624 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2625 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2626 return 0;
2627
2628}
2629
2630static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2631{
2632 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06002633 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04002634 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Al Viro0c11b942008-01-10 04:20:52 -05002636 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04002637 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06002638 audit_info.secid = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -04002639 err = xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN, &audit_info);
2640 if (err)
2641 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002642 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002643 c.event = XFRM_MSG_FLUSHPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002644 c.pid = hdr->sadb_msg_pid;
2645 c.seq = hdr->sadb_msg_seq;
2646 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 return 0;
2649}
2650
2651typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2652 struct sadb_msg *hdr, void **ext_hdrs);
2653static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2654 [SADB_RESERVED] = pfkey_reserved,
2655 [SADB_GETSPI] = pfkey_getspi,
2656 [SADB_UPDATE] = pfkey_add,
2657 [SADB_ADD] = pfkey_add,
2658 [SADB_DELETE] = pfkey_delete,
2659 [SADB_GET] = pfkey_get,
2660 [SADB_ACQUIRE] = pfkey_acquire,
2661 [SADB_REGISTER] = pfkey_register,
2662 [SADB_EXPIRE] = NULL,
2663 [SADB_FLUSH] = pfkey_flush,
2664 [SADB_DUMP] = pfkey_dump,
2665 [SADB_X_PROMISC] = pfkey_promisc,
2666 [SADB_X_PCHANGE] = NULL,
2667 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2668 [SADB_X_SPDADD] = pfkey_spdadd,
2669 [SADB_X_SPDDELETE] = pfkey_spddelete,
2670 [SADB_X_SPDGET] = pfkey_spdget,
2671 [SADB_X_SPDACQUIRE] = NULL,
2672 [SADB_X_SPDDUMP] = pfkey_spddump,
2673 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2674 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2675 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002676 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677};
2678
2679static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2680{
2681 void *ext_hdrs[SADB_EXT_MAX];
2682 int err;
2683
2684 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2685 BROADCAST_PROMISC_ONLY, NULL);
2686
2687 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2688 err = parse_exthdrs(skb, hdr, ext_hdrs);
2689 if (!err) {
2690 err = -EOPNOTSUPP;
2691 if (pfkey_funcs[hdr->sadb_msg_type])
2692 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2693 }
2694 return err;
2695}
2696
2697static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2698{
2699 struct sadb_msg *hdr = NULL;
2700
2701 if (skb->len < sizeof(*hdr)) {
2702 *errp = -EMSGSIZE;
2703 } else {
2704 hdr = (struct sadb_msg *) skb->data;
2705 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2706 hdr->sadb_msg_reserved != 0 ||
2707 (hdr->sadb_msg_type <= SADB_RESERVED ||
2708 hdr->sadb_msg_type > SADB_MAX)) {
2709 hdr = NULL;
2710 *errp = -EINVAL;
2711 } else if (hdr->sadb_msg_len != (skb->len /
2712 sizeof(uint64_t)) ||
2713 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2714 sizeof(uint64_t))) {
2715 hdr = NULL;
2716 *errp = -EMSGSIZE;
2717 } else {
2718 *errp = 0;
2719 }
2720 }
2721 return hdr;
2722}
2723
2724static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2725{
Herbert Xuf3980352007-12-19 23:44:29 -08002726 unsigned int id = d->desc.sadb_alg_id;
2727
2728 if (id >= sizeof(t->aalgos) * 8)
2729 return 0;
2730
2731 return (t->aalgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
2734static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2735{
Herbert Xuf3980352007-12-19 23:44:29 -08002736 unsigned int id = d->desc.sadb_alg_id;
2737
2738 if (id >= sizeof(t->ealgos) * 8)
2739 return 0;
2740
2741 return (t->ealgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743
2744static int count_ah_combs(struct xfrm_tmpl *t)
2745{
2746 int i, sz = 0;
2747
2748 for (i = 0; ; i++) {
2749 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2750 if (!aalg)
2751 break;
2752 if (aalg_tmpl_set(t, aalg) && aalg->available)
2753 sz += sizeof(struct sadb_comb);
2754 }
2755 return sz + sizeof(struct sadb_prop);
2756}
2757
2758static int count_esp_combs(struct xfrm_tmpl *t)
2759{
2760 int i, k, sz = 0;
2761
2762 for (i = 0; ; i++) {
2763 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2764 if (!ealg)
2765 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2768 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 for (k = 1; ; k++) {
2771 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2772 if (!aalg)
2773 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 if (aalg_tmpl_set(t, aalg) && aalg->available)
2776 sz += sizeof(struct sadb_comb);
2777 }
2778 }
2779 return sz + sizeof(struct sadb_prop);
2780}
2781
2782static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2783{
2784 struct sadb_prop *p;
2785 int i;
2786
2787 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2788 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2789 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2790 p->sadb_prop_replay = 32;
2791 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2792
2793 for (i = 0; ; i++) {
2794 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2795 if (!aalg)
2796 break;
2797
2798 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2799 struct sadb_comb *c;
2800 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2801 memset(c, 0, sizeof(*c));
2802 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2803 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2804 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2805 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2806 c->sadb_comb_hard_addtime = 24*60*60;
2807 c->sadb_comb_soft_addtime = 20*60*60;
2808 c->sadb_comb_hard_usetime = 8*60*60;
2809 c->sadb_comb_soft_usetime = 7*60*60;
2810 }
2811 }
2812}
2813
2814static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2815{
2816 struct sadb_prop *p;
2817 int i, k;
2818
2819 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2820 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2821 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2822 p->sadb_prop_replay = 32;
2823 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2824
2825 for (i=0; ; i++) {
2826 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2827 if (!ealg)
2828 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002829
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2831 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 for (k = 1; ; k++) {
2834 struct sadb_comb *c;
2835 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2836 if (!aalg)
2837 break;
2838 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2839 continue;
2840 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2841 memset(c, 0, sizeof(*c));
2842 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2843 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2844 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2845 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2846 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2847 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2848 c->sadb_comb_encrypt_maxbits = ealg->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
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002857static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2858{
2859 return 0;
2860}
2861
2862static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 struct sk_buff *out_skb;
2865 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002866 int hard;
2867 int hsc;
2868
Herbert Xubf088672005-06-18 22:44:00 -07002869 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002870 if (hard)
2871 hsc = 2;
2872 else
2873 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Herbert Xu050f0092007-10-09 13:31:47 -07002875 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 if (IS_ERR(out_skb))
2877 return PTR_ERR(out_skb);
2878
2879 out_hdr = (struct sadb_msg *) out_skb->data;
2880 out_hdr->sadb_msg_version = PF_KEY_V2;
2881 out_hdr->sadb_msg_type = SADB_EXPIRE;
2882 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2883 out_hdr->sadb_msg_errno = 0;
2884 out_hdr->sadb_msg_reserved = 0;
2885 out_hdr->sadb_msg_seq = 0;
2886 out_hdr->sadb_msg_pid = 0;
2887
2888 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2889 return 0;
2890}
2891
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002892static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2893{
Jamal Hadi Salim99c6f602008-06-10 14:25:34 -07002894 if (atomic_read(&pfkey_socks_nr) == 0)
2895 return 0;
2896
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002897 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002898 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002899 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002900 case XFRM_MSG_DELSA:
2901 case XFRM_MSG_NEWSA:
2902 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002903 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002904 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002905 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002906 case XFRM_MSG_NEWAE: /* not yet supported */
2907 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002908 default:
2909 printk("pfkey: Unknown SA event %d\n", c->event);
2910 break;
2911 }
2912
2913 return 0;
2914}
2915
2916static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2917{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002918 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
2919 return 0;
2920
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002921 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002922 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002923 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002924 case XFRM_MSG_DELPOLICY:
2925 case XFRM_MSG_NEWPOLICY:
2926 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002927 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002928 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002929 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
2930 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002931 return key_notify_policy_flush(c);
2932 default:
2933 printk("pfkey: Unknown policy event %d\n", c->event);
2934 break;
2935 }
2936
2937 return 0;
2938}
2939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940static u32 get_acqseq(void)
2941{
2942 u32 res;
2943 static u32 acqseq;
2944 static DEFINE_SPINLOCK(acqseq_lock);
2945
2946 spin_lock_bh(&acqseq_lock);
2947 res = (++acqseq ? : ++acqseq);
2948 spin_unlock_bh(&acqseq_lock);
2949 return res;
2950}
2951
2952static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2953{
2954 struct sk_buff *skb;
2955 struct sadb_msg *hdr;
2956 struct sadb_address *addr;
2957 struct sadb_x_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 int sockaddr_size;
2959 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07002960 struct sadb_x_sec_ctx *sec_ctx;
2961 struct xfrm_sec_ctx *xfrm_ctx;
2962 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2965 if (!sockaddr_size)
2966 return -EINVAL;
2967
2968 size = sizeof(struct sadb_msg) +
2969 (sizeof(struct sadb_address) * 2) +
2970 (sockaddr_size * 2) +
2971 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (x->id.proto == IPPROTO_AH)
2974 size += count_ah_combs(t);
2975 else if (x->id.proto == IPPROTO_ESP)
2976 size += count_esp_combs(t);
2977
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07002978 if ((xfrm_ctx = x->security)) {
2979 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
2980 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
2981 }
2982
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 skb = alloc_skb(size + 16, GFP_ATOMIC);
2984 if (skb == NULL)
2985 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2988 hdr->sadb_msg_version = PF_KEY_V2;
2989 hdr->sadb_msg_type = SADB_ACQUIRE;
2990 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2991 hdr->sadb_msg_len = size / sizeof(uint64_t);
2992 hdr->sadb_msg_errno = 0;
2993 hdr->sadb_msg_reserved = 0;
2994 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2995 hdr->sadb_msg_pid = 0;
2996
2997 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002998 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003000 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 (sizeof(struct sadb_address)+sockaddr_size)/
3002 sizeof(uint64_t);
3003 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3004 addr->sadb_address_proto = 0;
3005 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003006 addr->sadb_address_prefixlen =
3007 pfkey_sockaddr_fill(&x->props.saddr, 0,
3008 (struct sockaddr *) (addr + 1),
3009 x->props.family);
3010 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003014 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 sizeof(struct sadb_address)+sockaddr_size);
3016 addr->sadb_address_len =
3017 (sizeof(struct sadb_address)+sockaddr_size)/
3018 sizeof(uint64_t);
3019 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3020 addr->sadb_address_proto = 0;
3021 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003022 addr->sadb_address_prefixlen =
3023 pfkey_sockaddr_fill(&x->id.daddr, 0,
3024 (struct sockaddr *) (addr + 1),
3025 x->props.family);
3026 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 BUG();
3028
3029 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3030 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3031 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3032 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3033 pol->sadb_x_policy_dir = dir+1;
3034 pol->sadb_x_policy_id = xp->index;
3035
3036 /* Set sadb_comb's. */
3037 if (x->id.proto == IPPROTO_AH)
3038 dump_ah_combs(skb, t);
3039 else if (x->id.proto == IPPROTO_ESP)
3040 dump_esp_combs(skb, t);
3041
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003042 /* security context */
3043 if (xfrm_ctx) {
3044 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3045 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3046 sec_ctx->sadb_x_sec_len =
3047 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3048 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3049 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3050 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3051 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3052 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3053 xfrm_ctx->ctx_len);
3054 }
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3057}
3058
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003059static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003060 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 struct xfrm_policy *xp;
3063 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003064 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003066 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 case AF_INET:
3068 if (opt != IP_IPSEC_POLICY) {
3069 *dir = -EOPNOTSUPP;
3070 return NULL;
3071 }
3072 break;
3073#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3074 case AF_INET6:
3075 if (opt != IPV6_IPSEC_POLICY) {
3076 *dir = -EOPNOTSUPP;
3077 return NULL;
3078 }
3079 break;
3080#endif
3081 default:
3082 *dir = -EINVAL;
3083 return NULL;
3084 }
3085
3086 *dir = -EINVAL;
3087
3088 if (len < sizeof(struct sadb_x_policy) ||
3089 pol->sadb_x_policy_len*8 > len ||
3090 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3091 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3092 return NULL;
3093
3094 xp = xfrm_policy_alloc(GFP_ATOMIC);
3095 if (xp == NULL) {
3096 *dir = -ENOBUFS;
3097 return NULL;
3098 }
3099
3100 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3101 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3102
3103 xp->lft.soft_byte_limit = XFRM_INF;
3104 xp->lft.hard_byte_limit = XFRM_INF;
3105 xp->lft.soft_packet_limit = XFRM_INF;
3106 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003107 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
3109 xp->xfrm_nr = 0;
3110 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3111 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3112 goto out;
3113
Trent Jaegerdf718372005-12-13 23:12:27 -08003114 /* security context too */
3115 if (len >= (pol->sadb_x_policy_len*8 +
3116 sizeof(struct sadb_x_sec_ctx))) {
3117 char *p = (char *)pol;
3118 struct xfrm_user_sec_ctx *uctx;
3119
3120 p += pol->sadb_x_policy_len*8;
3121 sec_ctx = (struct sadb_x_sec_ctx *)p;
3122 if (len < pol->sadb_x_policy_len*8 +
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003123 sec_ctx->sadb_x_sec_len) {
3124 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003125 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003126 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003127 if ((*dir = verify_sec_ctx_len(p)))
3128 goto out;
3129 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
Paul Moore03e1ad72008-04-12 19:07:52 -07003130 *dir = security_xfrm_policy_alloc(&xp->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08003131 kfree(uctx);
3132
3133 if (*dir)
3134 goto out;
3135 }
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 *dir = pol->sadb_x_policy_dir-1;
3138 return xp;
3139
3140out:
WANG Cong64c31b32008-01-07 22:34:29 -08003141 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return NULL;
3143}
3144
Al Viro5d36b182006-11-08 00:24:06 -08003145static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
3147 struct sk_buff *skb;
3148 struct sadb_msg *hdr;
3149 struct sadb_sa *sa;
3150 struct sadb_address *addr;
3151 struct sadb_x_nat_t_port *n_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 int sockaddr_size;
3153 int size;
3154 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3155 struct xfrm_encap_tmpl *natt = NULL;
3156
3157 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3158 if (!sockaddr_size)
3159 return -EINVAL;
3160
3161 if (!satype)
3162 return -EINVAL;
3163
3164 if (!x->encap)
3165 return -EINVAL;
3166
3167 natt = x->encap;
3168
3169 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3170 *
3171 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3172 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3173 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003174
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 size = sizeof(struct sadb_msg) +
3176 sizeof(struct sadb_sa) +
3177 (sizeof(struct sadb_address) * 2) +
3178 (sockaddr_size * 2) +
3179 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 skb = alloc_skb(size + 16, GFP_ATOMIC);
3182 if (skb == NULL)
3183 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3186 hdr->sadb_msg_version = PF_KEY_V2;
3187 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3188 hdr->sadb_msg_satype = satype;
3189 hdr->sadb_msg_len = size / sizeof(uint64_t);
3190 hdr->sadb_msg_errno = 0;
3191 hdr->sadb_msg_reserved = 0;
3192 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3193 hdr->sadb_msg_pid = 0;
3194
3195 /* SA */
3196 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3197 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3198 sa->sadb_sa_exttype = SADB_EXT_SA;
3199 sa->sadb_sa_spi = x->id.spi;
3200 sa->sadb_sa_replay = 0;
3201 sa->sadb_sa_state = 0;
3202 sa->sadb_sa_auth = 0;
3203 sa->sadb_sa_encrypt = 0;
3204 sa->sadb_sa_flags = 0;
3205
3206 /* ADDRESS_SRC (old addr) */
3207 addr = (struct sadb_address*)
3208 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003209 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 (sizeof(struct sadb_address)+sockaddr_size)/
3211 sizeof(uint64_t);
3212 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3213 addr->sadb_address_proto = 0;
3214 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003215 addr->sadb_address_prefixlen =
3216 pfkey_sockaddr_fill(&x->props.saddr, 0,
3217 (struct sockaddr *) (addr + 1),
3218 x->props.family);
3219 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 BUG();
3221
3222 /* NAT_T_SPORT (old port) */
3223 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3224 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3225 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3226 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3227 n_port->sadb_x_nat_t_port_reserved = 0;
3228
3229 /* ADDRESS_DST (new addr) */
3230 addr = (struct sadb_address*)
3231 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003232 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 (sizeof(struct sadb_address)+sockaddr_size)/
3234 sizeof(uint64_t);
3235 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3236 addr->sadb_address_proto = 0;
3237 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003238 addr->sadb_address_prefixlen =
3239 pfkey_sockaddr_fill(ipaddr, 0,
3240 (struct sockaddr *) (addr + 1),
3241 x->props.family);
3242 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 BUG();
3244
3245 /* NAT_T_DPORT (new port) */
3246 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3247 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3248 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3249 n_port->sadb_x_nat_t_port_port = sport;
3250 n_port->sadb_x_nat_t_port_reserved = 0;
3251
3252 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3253}
3254
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003255#ifdef CONFIG_NET_KEY_MIGRATE
3256static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3257 struct xfrm_selector *sel)
3258{
3259 struct sadb_address *addr;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003260 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3261 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3262 addr->sadb_address_exttype = type;
3263 addr->sadb_address_proto = sel->proto;
3264 addr->sadb_address_reserved = 0;
3265
3266 switch (type) {
3267 case SADB_EXT_ADDRESS_SRC:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003268 addr->sadb_address_prefixlen = sel->prefixlen_s;
3269 pfkey_sockaddr_fill(&sel->saddr, 0,
3270 (struct sockaddr *)(addr + 1),
3271 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003272 break;
3273 case SADB_EXT_ADDRESS_DST:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003274 addr->sadb_address_prefixlen = sel->prefixlen_d;
3275 pfkey_sockaddr_fill(&sel->daddr, 0,
3276 (struct sockaddr *)(addr + 1),
3277 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003278 break;
3279 default:
3280 return -EINVAL;
3281 }
3282
3283 return 0;
3284}
3285
3286static int set_ipsecrequest(struct sk_buff *skb,
3287 uint8_t proto, uint8_t mode, int level,
3288 uint32_t reqid, uint8_t family,
3289 xfrm_address_t *src, xfrm_address_t *dst)
3290{
3291 struct sadb_x_ipsecrequest *rq;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003292 u8 *sa;
3293 int socklen = pfkey_sockaddr_len(family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003294 int size_req;
3295
3296 size_req = sizeof(struct sadb_x_ipsecrequest) +
3297 pfkey_sockaddr_pair_size(family);
3298
3299 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3300 memset(rq, 0, size_req);
3301 rq->sadb_x_ipsecrequest_len = size_req;
3302 rq->sadb_x_ipsecrequest_proto = proto;
3303 rq->sadb_x_ipsecrequest_mode = mode;
3304 rq->sadb_x_ipsecrequest_level = level;
3305 rq->sadb_x_ipsecrequest_reqid = reqid;
3306
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003307 sa = (u8 *) (rq + 1);
3308 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3309 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003310 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003311
3312 return 0;
3313}
3314#endif
3315
3316#ifdef CONFIG_NET_KEY_MIGRATE
3317static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3318 struct xfrm_migrate *m, int num_bundles)
3319{
3320 int i;
3321 int sasize_sel;
3322 int size = 0;
3323 int size_pol = 0;
3324 struct sk_buff *skb;
3325 struct sadb_msg *hdr;
3326 struct sadb_x_policy *pol;
3327 struct xfrm_migrate *mp;
3328
3329 if (type != XFRM_POLICY_TYPE_MAIN)
3330 return 0;
3331
3332 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3333 return -EINVAL;
3334
3335 /* selector */
3336 sasize_sel = pfkey_sockaddr_size(sel->family);
3337 if (!sasize_sel)
3338 return -EINVAL;
3339 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3340
3341 /* policy info */
3342 size_pol += sizeof(struct sadb_x_policy);
3343
3344 /* ipsecrequests */
3345 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3346 /* old locator pair */
3347 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3348 pfkey_sockaddr_pair_size(mp->old_family);
3349 /* new locator pair */
3350 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3351 pfkey_sockaddr_pair_size(mp->new_family);
3352 }
3353
3354 size += sizeof(struct sadb_msg) + size_pol;
3355
3356 /* alloc buffer */
3357 skb = alloc_skb(size, GFP_ATOMIC);
3358 if (skb == NULL)
3359 return -ENOMEM;
3360
3361 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3362 hdr->sadb_msg_version = PF_KEY_V2;
3363 hdr->sadb_msg_type = SADB_X_MIGRATE;
3364 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3365 hdr->sadb_msg_len = size / 8;
3366 hdr->sadb_msg_errno = 0;
3367 hdr->sadb_msg_reserved = 0;
3368 hdr->sadb_msg_seq = 0;
3369 hdr->sadb_msg_pid = 0;
3370
3371 /* selector src */
3372 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3373
3374 /* selector dst */
3375 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3376
3377 /* policy information */
3378 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3379 pol->sadb_x_policy_len = size_pol / 8;
3380 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3381 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3382 pol->sadb_x_policy_dir = dir + 1;
3383 pol->sadb_x_policy_id = 0;
3384 pol->sadb_x_policy_priority = 0;
3385
3386 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3387 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003388 int mode = pfkey_mode_from_xfrm(mp->mode);
3389 if (mode < 0)
Patrick McHardyd4782c32008-01-20 17:24:29 -08003390 goto err;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003391 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003392 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3393 mp->reqid, mp->old_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003394 &mp->old_saddr, &mp->old_daddr) < 0)
3395 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003396
3397 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003398 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003399 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3400 mp->reqid, mp->new_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003401 &mp->new_saddr, &mp->new_daddr) < 0)
3402 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003403 }
3404
3405 /* broadcast migrate message to sockets */
3406 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
3407
3408 return 0;
Patrick McHardyd4782c32008-01-20 17:24:29 -08003409
3410err:
3411 kfree_skb(skb);
3412 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003413}
3414#else
3415static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3416 struct xfrm_migrate *m, int num_bundles)
3417{
3418 return -ENOPROTOOPT;
3419}
3420#endif
3421
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422static int pfkey_sendmsg(struct kiocb *kiocb,
3423 struct socket *sock, struct msghdr *msg, size_t len)
3424{
3425 struct sock *sk = sock->sk;
3426 struct sk_buff *skb = NULL;
3427 struct sadb_msg *hdr = NULL;
3428 int err;
3429
3430 err = -EOPNOTSUPP;
3431 if (msg->msg_flags & MSG_OOB)
3432 goto out;
3433
3434 err = -EMSGSIZE;
3435 if ((unsigned)len > sk->sk_sndbuf - 32)
3436 goto out;
3437
3438 err = -ENOBUFS;
3439 skb = alloc_skb(len, GFP_KERNEL);
3440 if (skb == NULL)
3441 goto out;
3442
3443 err = -EFAULT;
3444 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3445 goto out;
3446
3447 hdr = pfkey_get_base_msg(skb, &err);
3448 if (!hdr)
3449 goto out;
3450
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003451 mutex_lock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 err = pfkey_process(sk, skb, hdr);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003453 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
3455out:
3456 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3457 err = 0;
3458 if (skb)
3459 kfree_skb(skb);
3460
3461 return err ? : len;
3462}
3463
3464static int pfkey_recvmsg(struct kiocb *kiocb,
3465 struct socket *sock, struct msghdr *msg, size_t len,
3466 int flags)
3467{
3468 struct sock *sk = sock->sk;
Timo Teras83321d62008-03-03 23:40:12 -08003469 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 struct sk_buff *skb;
3471 int copied, err;
3472
3473 err = -EINVAL;
3474 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3475 goto out;
3476
3477 msg->msg_namelen = 0;
3478 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3479 if (skb == NULL)
3480 goto out;
3481
3482 copied = skb->len;
3483 if (copied > len) {
3484 msg->msg_flags |= MSG_TRUNC;
3485 copied = len;
3486 }
3487
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003488 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3490 if (err)
3491 goto out_free;
3492
3493 sock_recv_timestamp(msg, sk, skb);
3494
3495 err = (flags & MSG_TRUNC) ? skb->len : copied;
3496
Timo Teras83321d62008-03-03 23:40:12 -08003497 if (pfk->dump.dump != NULL &&
3498 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3499 pfkey_do_dump(pfk);
3500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501out_free:
3502 skb_free_datagram(sk, skb);
3503out:
3504 return err;
3505}
3506
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003507static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 .family = PF_KEY,
3509 .owner = THIS_MODULE,
3510 /* Operations that make no sense on pfkey sockets. */
3511 .bind = sock_no_bind,
3512 .connect = sock_no_connect,
3513 .socketpair = sock_no_socketpair,
3514 .accept = sock_no_accept,
3515 .getname = sock_no_getname,
3516 .ioctl = sock_no_ioctl,
3517 .listen = sock_no_listen,
3518 .shutdown = sock_no_shutdown,
3519 .setsockopt = sock_no_setsockopt,
3520 .getsockopt = sock_no_getsockopt,
3521 .mmap = sock_no_mmap,
3522 .sendpage = sock_no_sendpage,
3523
3524 /* Now the operations that really occur. */
3525 .release = pfkey_release,
3526 .poll = datagram_poll,
3527 .sendmsg = pfkey_sendmsg,
3528 .recvmsg = pfkey_recvmsg,
3529};
3530
3531static struct net_proto_family pfkey_family_ops = {
3532 .family = PF_KEY,
3533 .create = pfkey_create,
3534 .owner = THIS_MODULE,
3535};
3536
3537#ifdef CONFIG_PROC_FS
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003538static int pfkey_seq_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003542 s = (struct sock *)v;
3543 if (v == SEQ_START_TOKEN)
3544 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3545 else
3546 seq_printf(f ,"%p %-6d %-6u %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 s,
3548 atomic_read(&s->sk_refcnt),
3549 atomic_read(&s->sk_rmem_alloc),
3550 atomic_read(&s->sk_wmem_alloc),
3551 sock_i_uid(s),
3552 sock_i_ino(s)
3553 );
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555}
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003556
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003557static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3558{
3559 struct sock *s;
3560 struct hlist_node *node;
3561 loff_t pos = *ppos;
3562
3563 read_lock(&pfkey_table_lock);
3564 if (pos == 0)
3565 return SEQ_START_TOKEN;
3566
3567 sk_for_each(s, node, &pfkey_table)
3568 if (pos-- == 1)
3569 return s;
3570
3571 return NULL;
3572}
3573
3574static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3575{
3576 ++*ppos;
3577 return (v == SEQ_START_TOKEN) ?
3578 sk_head(&pfkey_table) :
3579 sk_next((struct sock *)v);
3580}
3581
3582static void pfkey_seq_stop(struct seq_file *f, void *v)
3583{
3584 read_unlock(&pfkey_table_lock);
3585}
3586
3587static struct seq_operations pfkey_seq_ops = {
3588 .start = pfkey_seq_start,
3589 .next = pfkey_seq_next,
3590 .stop = pfkey_seq_stop,
3591 .show = pfkey_seq_show,
3592};
3593
3594static int pfkey_seq_open(struct inode *inode, struct file *file)
3595{
3596 return seq_open(file, &pfkey_seq_ops);
3597}
3598
3599static struct file_operations pfkey_proc_ops = {
3600 .open = pfkey_seq_open,
3601 .read = seq_read,
3602 .llseek = seq_lseek,
3603 .release = seq_release,
3604};
3605
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003606static int pfkey_init_proc(void)
3607{
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003608 struct proc_dir_entry *e;
3609
David S. Millerd9595a72008-02-26 22:20:44 -08003610 e = proc_net_fops_create(&init_net, "pfkey", 0, &pfkey_proc_ops);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003611 if (e == NULL)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003612 return -ENOMEM;
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003613
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003614 return 0;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003615}
3616
3617static void pfkey_exit_proc(void)
3618{
David S. Millerd9595a72008-02-26 22:20:44 -08003619 proc_net_remove(&init_net, "pfkey");
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003620}
3621#else
3622static inline int pfkey_init_proc(void)
3623{
3624 return 0;
3625}
3626
3627static inline void pfkey_exit_proc(void)
3628{
3629}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630#endif
3631
3632static struct xfrm_mgr pfkeyv2_mgr =
3633{
3634 .id = "pfkeyv2",
3635 .notify = pfkey_send_notify,
3636 .acquire = pfkey_send_acquire,
3637 .compile_policy = pfkey_compile_policy,
3638 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003639 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003640 .migrate = pfkey_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641};
3642
3643static void __exit ipsec_pfkey_exit(void)
3644{
3645 xfrm_unregister_km(&pfkeyv2_mgr);
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003646 pfkey_exit_proc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 sock_unregister(PF_KEY);
3648 proto_unregister(&key_proto);
3649}
3650
3651static int __init ipsec_pfkey_init(void)
3652{
3653 int err = proto_register(&key_proto, 0);
3654
3655 if (err != 0)
3656 goto out;
3657
3658 err = sock_register(&pfkey_family_ops);
3659 if (err != 0)
3660 goto out_unregister_key_proto;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003661 err = pfkey_init_proc();
3662 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 goto out_sock_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 err = xfrm_register_km(&pfkeyv2_mgr);
3665 if (err != 0)
3666 goto out_remove_proc_entry;
3667out:
3668 return err;
3669out_remove_proc_entry:
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003670 pfkey_exit_proc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671out_sock_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 sock_unregister(PF_KEY);
3673out_unregister_key_proto:
3674 proto_unregister(&key_proto);
3675 goto out;
3676}
3677
3678module_init(ipsec_pfkey_init);
3679module_exit(ipsec_pfkey_exit);
3680MODULE_LICENSE("GPL");
3681MODULE_ALIAS_NETPROTO(PF_KEY);