blob: 841af9f2d5e0d5d341ac077d44d459a9a1946fcd [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. Miller13fcfbb02007-02-12 13:53:54 -08002306
2307 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002308 goto out;
David S. Miller13fcfbb02007-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{
2894 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002895 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002896 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002897 case XFRM_MSG_DELSA:
2898 case XFRM_MSG_NEWSA:
2899 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002900 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002901 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002902 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002903 case XFRM_MSG_NEWAE: /* not yet supported */
2904 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002905 default:
2906 printk("pfkey: Unknown SA event %d\n", c->event);
2907 break;
2908 }
2909
2910 return 0;
2911}
2912
2913static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2914{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002915 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
2916 return 0;
2917
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002918 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002919 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002920 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002921 case XFRM_MSG_DELPOLICY:
2922 case XFRM_MSG_NEWPOLICY:
2923 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002924 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002925 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002926 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
2927 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002928 return key_notify_policy_flush(c);
2929 default:
2930 printk("pfkey: Unknown policy event %d\n", c->event);
2931 break;
2932 }
2933
2934 return 0;
2935}
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937static u32 get_acqseq(void)
2938{
2939 u32 res;
2940 static u32 acqseq;
2941 static DEFINE_SPINLOCK(acqseq_lock);
2942
2943 spin_lock_bh(&acqseq_lock);
2944 res = (++acqseq ? : ++acqseq);
2945 spin_unlock_bh(&acqseq_lock);
2946 return res;
2947}
2948
2949static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2950{
2951 struct sk_buff *skb;
2952 struct sadb_msg *hdr;
2953 struct sadb_address *addr;
2954 struct sadb_x_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 int sockaddr_size;
2956 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07002957 struct sadb_x_sec_ctx *sec_ctx;
2958 struct xfrm_sec_ctx *xfrm_ctx;
2959 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2962 if (!sockaddr_size)
2963 return -EINVAL;
2964
2965 size = sizeof(struct sadb_msg) +
2966 (sizeof(struct sadb_address) * 2) +
2967 (sockaddr_size * 2) +
2968 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (x->id.proto == IPPROTO_AH)
2971 size += count_ah_combs(t);
2972 else if (x->id.proto == IPPROTO_ESP)
2973 size += count_esp_combs(t);
2974
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07002975 if ((xfrm_ctx = x->security)) {
2976 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
2977 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
2978 }
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 skb = alloc_skb(size + 16, GFP_ATOMIC);
2981 if (skb == NULL)
2982 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2985 hdr->sadb_msg_version = PF_KEY_V2;
2986 hdr->sadb_msg_type = SADB_ACQUIRE;
2987 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2988 hdr->sadb_msg_len = size / sizeof(uint64_t);
2989 hdr->sadb_msg_errno = 0;
2990 hdr->sadb_msg_reserved = 0;
2991 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2992 hdr->sadb_msg_pid = 0;
2993
2994 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002995 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002997 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 (sizeof(struct sadb_address)+sockaddr_size)/
2999 sizeof(uint64_t);
3000 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3001 addr->sadb_address_proto = 0;
3002 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003003 addr->sadb_address_prefixlen =
3004 pfkey_sockaddr_fill(&x->props.saddr, 0,
3005 (struct sockaddr *) (addr + 1),
3006 x->props.family);
3007 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003011 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 sizeof(struct sadb_address)+sockaddr_size);
3013 addr->sadb_address_len =
3014 (sizeof(struct sadb_address)+sockaddr_size)/
3015 sizeof(uint64_t);
3016 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3017 addr->sadb_address_proto = 0;
3018 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003019 addr->sadb_address_prefixlen =
3020 pfkey_sockaddr_fill(&x->id.daddr, 0,
3021 (struct sockaddr *) (addr + 1),
3022 x->props.family);
3023 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 BUG();
3025
3026 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3027 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3028 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3029 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3030 pol->sadb_x_policy_dir = dir+1;
3031 pol->sadb_x_policy_id = xp->index;
3032
3033 /* Set sadb_comb's. */
3034 if (x->id.proto == IPPROTO_AH)
3035 dump_ah_combs(skb, t);
3036 else if (x->id.proto == IPPROTO_ESP)
3037 dump_esp_combs(skb, t);
3038
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003039 /* security context */
3040 if (xfrm_ctx) {
3041 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3042 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3043 sec_ctx->sadb_x_sec_len =
3044 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3045 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3046 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3047 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3048 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3049 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3050 xfrm_ctx->ctx_len);
3051 }
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3054}
3055
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003056static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003057 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
3059 struct xfrm_policy *xp;
3060 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003061 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003063 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 case AF_INET:
3065 if (opt != IP_IPSEC_POLICY) {
3066 *dir = -EOPNOTSUPP;
3067 return NULL;
3068 }
3069 break;
3070#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3071 case AF_INET6:
3072 if (opt != IPV6_IPSEC_POLICY) {
3073 *dir = -EOPNOTSUPP;
3074 return NULL;
3075 }
3076 break;
3077#endif
3078 default:
3079 *dir = -EINVAL;
3080 return NULL;
3081 }
3082
3083 *dir = -EINVAL;
3084
3085 if (len < sizeof(struct sadb_x_policy) ||
3086 pol->sadb_x_policy_len*8 > len ||
3087 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3088 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3089 return NULL;
3090
3091 xp = xfrm_policy_alloc(GFP_ATOMIC);
3092 if (xp == NULL) {
3093 *dir = -ENOBUFS;
3094 return NULL;
3095 }
3096
3097 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3098 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3099
3100 xp->lft.soft_byte_limit = XFRM_INF;
3101 xp->lft.hard_byte_limit = XFRM_INF;
3102 xp->lft.soft_packet_limit = XFRM_INF;
3103 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003104 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
3106 xp->xfrm_nr = 0;
3107 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3108 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3109 goto out;
3110
Trent Jaegerdf718372005-12-13 23:12:27 -08003111 /* security context too */
3112 if (len >= (pol->sadb_x_policy_len*8 +
3113 sizeof(struct sadb_x_sec_ctx))) {
3114 char *p = (char *)pol;
3115 struct xfrm_user_sec_ctx *uctx;
3116
3117 p += pol->sadb_x_policy_len*8;
3118 sec_ctx = (struct sadb_x_sec_ctx *)p;
3119 if (len < pol->sadb_x_policy_len*8 +
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003120 sec_ctx->sadb_x_sec_len) {
3121 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003122 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003123 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003124 if ((*dir = verify_sec_ctx_len(p)))
3125 goto out;
3126 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
Paul Moore03e1ad72008-04-12 19:07:52 -07003127 *dir = security_xfrm_policy_alloc(&xp->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08003128 kfree(uctx);
3129
3130 if (*dir)
3131 goto out;
3132 }
3133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 *dir = pol->sadb_x_policy_dir-1;
3135 return xp;
3136
3137out:
WANG Cong64c31b32008-01-07 22:34:29 -08003138 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 return NULL;
3140}
3141
Al Viro5d36b182006-11-08 00:24:06 -08003142static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
3144 struct sk_buff *skb;
3145 struct sadb_msg *hdr;
3146 struct sadb_sa *sa;
3147 struct sadb_address *addr;
3148 struct sadb_x_nat_t_port *n_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 int sockaddr_size;
3150 int size;
3151 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3152 struct xfrm_encap_tmpl *natt = NULL;
3153
3154 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3155 if (!sockaddr_size)
3156 return -EINVAL;
3157
3158 if (!satype)
3159 return -EINVAL;
3160
3161 if (!x->encap)
3162 return -EINVAL;
3163
3164 natt = x->encap;
3165
3166 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3167 *
3168 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3169 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3170 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 size = sizeof(struct sadb_msg) +
3173 sizeof(struct sadb_sa) +
3174 (sizeof(struct sadb_address) * 2) +
3175 (sockaddr_size * 2) +
3176 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 skb = alloc_skb(size + 16, GFP_ATOMIC);
3179 if (skb == NULL)
3180 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3183 hdr->sadb_msg_version = PF_KEY_V2;
3184 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3185 hdr->sadb_msg_satype = satype;
3186 hdr->sadb_msg_len = size / sizeof(uint64_t);
3187 hdr->sadb_msg_errno = 0;
3188 hdr->sadb_msg_reserved = 0;
3189 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3190 hdr->sadb_msg_pid = 0;
3191
3192 /* SA */
3193 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3194 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3195 sa->sadb_sa_exttype = SADB_EXT_SA;
3196 sa->sadb_sa_spi = x->id.spi;
3197 sa->sadb_sa_replay = 0;
3198 sa->sadb_sa_state = 0;
3199 sa->sadb_sa_auth = 0;
3200 sa->sadb_sa_encrypt = 0;
3201 sa->sadb_sa_flags = 0;
3202
3203 /* ADDRESS_SRC (old addr) */
3204 addr = (struct sadb_address*)
3205 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003206 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 (sizeof(struct sadb_address)+sockaddr_size)/
3208 sizeof(uint64_t);
3209 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3210 addr->sadb_address_proto = 0;
3211 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003212 addr->sadb_address_prefixlen =
3213 pfkey_sockaddr_fill(&x->props.saddr, 0,
3214 (struct sockaddr *) (addr + 1),
3215 x->props.family);
3216 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 BUG();
3218
3219 /* NAT_T_SPORT (old port) */
3220 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3221 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3222 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3223 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3224 n_port->sadb_x_nat_t_port_reserved = 0;
3225
3226 /* ADDRESS_DST (new addr) */
3227 addr = (struct sadb_address*)
3228 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003229 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 (sizeof(struct sadb_address)+sockaddr_size)/
3231 sizeof(uint64_t);
3232 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3233 addr->sadb_address_proto = 0;
3234 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003235 addr->sadb_address_prefixlen =
3236 pfkey_sockaddr_fill(ipaddr, 0,
3237 (struct sockaddr *) (addr + 1),
3238 x->props.family);
3239 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 BUG();
3241
3242 /* NAT_T_DPORT (new port) */
3243 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3244 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3245 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3246 n_port->sadb_x_nat_t_port_port = sport;
3247 n_port->sadb_x_nat_t_port_reserved = 0;
3248
3249 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3250}
3251
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003252#ifdef CONFIG_NET_KEY_MIGRATE
3253static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3254 struct xfrm_selector *sel)
3255{
3256 struct sadb_address *addr;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003257 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3258 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3259 addr->sadb_address_exttype = type;
3260 addr->sadb_address_proto = sel->proto;
3261 addr->sadb_address_reserved = 0;
3262
3263 switch (type) {
3264 case SADB_EXT_ADDRESS_SRC:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003265 addr->sadb_address_prefixlen = sel->prefixlen_s;
3266 pfkey_sockaddr_fill(&sel->saddr, 0,
3267 (struct sockaddr *)(addr + 1),
3268 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003269 break;
3270 case SADB_EXT_ADDRESS_DST:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003271 addr->sadb_address_prefixlen = sel->prefixlen_d;
3272 pfkey_sockaddr_fill(&sel->daddr, 0,
3273 (struct sockaddr *)(addr + 1),
3274 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003275 break;
3276 default:
3277 return -EINVAL;
3278 }
3279
3280 return 0;
3281}
3282
3283static int set_ipsecrequest(struct sk_buff *skb,
3284 uint8_t proto, uint8_t mode, int level,
3285 uint32_t reqid, uint8_t family,
3286 xfrm_address_t *src, xfrm_address_t *dst)
3287{
3288 struct sadb_x_ipsecrequest *rq;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003289 u8 *sa;
3290 int socklen = pfkey_sockaddr_len(family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003291 int size_req;
3292
3293 size_req = sizeof(struct sadb_x_ipsecrequest) +
3294 pfkey_sockaddr_pair_size(family);
3295
3296 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3297 memset(rq, 0, size_req);
3298 rq->sadb_x_ipsecrequest_len = size_req;
3299 rq->sadb_x_ipsecrequest_proto = proto;
3300 rq->sadb_x_ipsecrequest_mode = mode;
3301 rq->sadb_x_ipsecrequest_level = level;
3302 rq->sadb_x_ipsecrequest_reqid = reqid;
3303
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003304 sa = (u8 *) (rq + 1);
3305 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3306 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003307 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003308
3309 return 0;
3310}
3311#endif
3312
3313#ifdef CONFIG_NET_KEY_MIGRATE
3314static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3315 struct xfrm_migrate *m, int num_bundles)
3316{
3317 int i;
3318 int sasize_sel;
3319 int size = 0;
3320 int size_pol = 0;
3321 struct sk_buff *skb;
3322 struct sadb_msg *hdr;
3323 struct sadb_x_policy *pol;
3324 struct xfrm_migrate *mp;
3325
3326 if (type != XFRM_POLICY_TYPE_MAIN)
3327 return 0;
3328
3329 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3330 return -EINVAL;
3331
3332 /* selector */
3333 sasize_sel = pfkey_sockaddr_size(sel->family);
3334 if (!sasize_sel)
3335 return -EINVAL;
3336 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3337
3338 /* policy info */
3339 size_pol += sizeof(struct sadb_x_policy);
3340
3341 /* ipsecrequests */
3342 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3343 /* old locator pair */
3344 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3345 pfkey_sockaddr_pair_size(mp->old_family);
3346 /* new locator pair */
3347 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3348 pfkey_sockaddr_pair_size(mp->new_family);
3349 }
3350
3351 size += sizeof(struct sadb_msg) + size_pol;
3352
3353 /* alloc buffer */
3354 skb = alloc_skb(size, GFP_ATOMIC);
3355 if (skb == NULL)
3356 return -ENOMEM;
3357
3358 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3359 hdr->sadb_msg_version = PF_KEY_V2;
3360 hdr->sadb_msg_type = SADB_X_MIGRATE;
3361 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3362 hdr->sadb_msg_len = size / 8;
3363 hdr->sadb_msg_errno = 0;
3364 hdr->sadb_msg_reserved = 0;
3365 hdr->sadb_msg_seq = 0;
3366 hdr->sadb_msg_pid = 0;
3367
3368 /* selector src */
3369 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3370
3371 /* selector dst */
3372 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3373
3374 /* policy information */
3375 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3376 pol->sadb_x_policy_len = size_pol / 8;
3377 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3378 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3379 pol->sadb_x_policy_dir = dir + 1;
3380 pol->sadb_x_policy_id = 0;
3381 pol->sadb_x_policy_priority = 0;
3382
3383 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3384 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003385 int mode = pfkey_mode_from_xfrm(mp->mode);
3386 if (mode < 0)
Patrick McHardyd4782c32008-01-20 17:24:29 -08003387 goto err;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003388 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003389 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3390 mp->reqid, mp->old_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003391 &mp->old_saddr, &mp->old_daddr) < 0)
3392 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003393
3394 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003395 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003396 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3397 mp->reqid, mp->new_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003398 &mp->new_saddr, &mp->new_daddr) < 0)
3399 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003400 }
3401
3402 /* broadcast migrate message to sockets */
3403 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
3404
3405 return 0;
Patrick McHardyd4782c32008-01-20 17:24:29 -08003406
3407err:
3408 kfree_skb(skb);
3409 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003410}
3411#else
3412static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3413 struct xfrm_migrate *m, int num_bundles)
3414{
3415 return -ENOPROTOOPT;
3416}
3417#endif
3418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419static int pfkey_sendmsg(struct kiocb *kiocb,
3420 struct socket *sock, struct msghdr *msg, size_t len)
3421{
3422 struct sock *sk = sock->sk;
3423 struct sk_buff *skb = NULL;
3424 struct sadb_msg *hdr = NULL;
3425 int err;
3426
3427 err = -EOPNOTSUPP;
3428 if (msg->msg_flags & MSG_OOB)
3429 goto out;
3430
3431 err = -EMSGSIZE;
3432 if ((unsigned)len > sk->sk_sndbuf - 32)
3433 goto out;
3434
3435 err = -ENOBUFS;
3436 skb = alloc_skb(len, GFP_KERNEL);
3437 if (skb == NULL)
3438 goto out;
3439
3440 err = -EFAULT;
3441 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3442 goto out;
3443
3444 hdr = pfkey_get_base_msg(skb, &err);
3445 if (!hdr)
3446 goto out;
3447
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003448 mutex_lock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 err = pfkey_process(sk, skb, hdr);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003450 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
3452out:
3453 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3454 err = 0;
3455 if (skb)
3456 kfree_skb(skb);
3457
3458 return err ? : len;
3459}
3460
3461static int pfkey_recvmsg(struct kiocb *kiocb,
3462 struct socket *sock, struct msghdr *msg, size_t len,
3463 int flags)
3464{
3465 struct sock *sk = sock->sk;
Timo Teras83321d62008-03-03 23:40:12 -08003466 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 struct sk_buff *skb;
3468 int copied, err;
3469
3470 err = -EINVAL;
3471 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3472 goto out;
3473
3474 msg->msg_namelen = 0;
3475 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3476 if (skb == NULL)
3477 goto out;
3478
3479 copied = skb->len;
3480 if (copied > len) {
3481 msg->msg_flags |= MSG_TRUNC;
3482 copied = len;
3483 }
3484
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003485 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3487 if (err)
3488 goto out_free;
3489
3490 sock_recv_timestamp(msg, sk, skb);
3491
3492 err = (flags & MSG_TRUNC) ? skb->len : copied;
3493
Timo Teras83321d62008-03-03 23:40:12 -08003494 if (pfk->dump.dump != NULL &&
3495 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3496 pfkey_do_dump(pfk);
3497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498out_free:
3499 skb_free_datagram(sk, skb);
3500out:
3501 return err;
3502}
3503
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003504static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 .family = PF_KEY,
3506 .owner = THIS_MODULE,
3507 /* Operations that make no sense on pfkey sockets. */
3508 .bind = sock_no_bind,
3509 .connect = sock_no_connect,
3510 .socketpair = sock_no_socketpair,
3511 .accept = sock_no_accept,
3512 .getname = sock_no_getname,
3513 .ioctl = sock_no_ioctl,
3514 .listen = sock_no_listen,
3515 .shutdown = sock_no_shutdown,
3516 .setsockopt = sock_no_setsockopt,
3517 .getsockopt = sock_no_getsockopt,
3518 .mmap = sock_no_mmap,
3519 .sendpage = sock_no_sendpage,
3520
3521 /* Now the operations that really occur. */
3522 .release = pfkey_release,
3523 .poll = datagram_poll,
3524 .sendmsg = pfkey_sendmsg,
3525 .recvmsg = pfkey_recvmsg,
3526};
3527
3528static struct net_proto_family pfkey_family_ops = {
3529 .family = PF_KEY,
3530 .create = pfkey_create,
3531 .owner = THIS_MODULE,
3532};
3533
3534#ifdef CONFIG_PROC_FS
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003535static int pfkey_seq_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003539 s = (struct sock *)v;
3540 if (v == SEQ_START_TOKEN)
3541 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3542 else
3543 seq_printf(f ,"%p %-6d %-6u %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 s,
3545 atomic_read(&s->sk_refcnt),
3546 atomic_read(&s->sk_rmem_alloc),
3547 atomic_read(&s->sk_wmem_alloc),
3548 sock_i_uid(s),
3549 sock_i_ino(s)
3550 );
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552}
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003553
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003554static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3555{
3556 struct sock *s;
3557 struct hlist_node *node;
3558 loff_t pos = *ppos;
3559
3560 read_lock(&pfkey_table_lock);
3561 if (pos == 0)
3562 return SEQ_START_TOKEN;
3563
3564 sk_for_each(s, node, &pfkey_table)
3565 if (pos-- == 1)
3566 return s;
3567
3568 return NULL;
3569}
3570
3571static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3572{
3573 ++*ppos;
3574 return (v == SEQ_START_TOKEN) ?
3575 sk_head(&pfkey_table) :
3576 sk_next((struct sock *)v);
3577}
3578
3579static void pfkey_seq_stop(struct seq_file *f, void *v)
3580{
3581 read_unlock(&pfkey_table_lock);
3582}
3583
3584static struct seq_operations pfkey_seq_ops = {
3585 .start = pfkey_seq_start,
3586 .next = pfkey_seq_next,
3587 .stop = pfkey_seq_stop,
3588 .show = pfkey_seq_show,
3589};
3590
3591static int pfkey_seq_open(struct inode *inode, struct file *file)
3592{
3593 return seq_open(file, &pfkey_seq_ops);
3594}
3595
3596static struct file_operations pfkey_proc_ops = {
3597 .open = pfkey_seq_open,
3598 .read = seq_read,
3599 .llseek = seq_lseek,
3600 .release = seq_release,
3601};
3602
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003603static int pfkey_init_proc(void)
3604{
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003605 struct proc_dir_entry *e;
3606
David S. Millerd9595a72008-02-26 22:20:44 -08003607 e = proc_net_fops_create(&init_net, "pfkey", 0, &pfkey_proc_ops);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003608 if (e == NULL)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003609 return -ENOMEM;
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003610
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003611 return 0;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003612}
3613
3614static void pfkey_exit_proc(void)
3615{
David S. Millerd9595a72008-02-26 22:20:44 -08003616 proc_net_remove(&init_net, "pfkey");
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003617}
3618#else
3619static inline int pfkey_init_proc(void)
3620{
3621 return 0;
3622}
3623
3624static inline void pfkey_exit_proc(void)
3625{
3626}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627#endif
3628
3629static struct xfrm_mgr pfkeyv2_mgr =
3630{
3631 .id = "pfkeyv2",
3632 .notify = pfkey_send_notify,
3633 .acquire = pfkey_send_acquire,
3634 .compile_policy = pfkey_compile_policy,
3635 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003636 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003637 .migrate = pfkey_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638};
3639
3640static void __exit ipsec_pfkey_exit(void)
3641{
3642 xfrm_unregister_km(&pfkeyv2_mgr);
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003643 pfkey_exit_proc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 sock_unregister(PF_KEY);
3645 proto_unregister(&key_proto);
3646}
3647
3648static int __init ipsec_pfkey_init(void)
3649{
3650 int err = proto_register(&key_proto, 0);
3651
3652 if (err != 0)
3653 goto out;
3654
3655 err = sock_register(&pfkey_family_ops);
3656 if (err != 0)
3657 goto out_unregister_key_proto;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003658 err = pfkey_init_proc();
3659 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 goto out_sock_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 err = xfrm_register_km(&pfkeyv2_mgr);
3662 if (err != 0)
3663 goto out_remove_proc_entry;
3664out:
3665 return err;
3666out_remove_proc_entry:
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003667 pfkey_exit_proc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668out_sock_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 sock_unregister(PF_KEY);
3670out_unregister_key_proto:
3671 proto_unregister(&key_proto);
3672 goto out;
3673}
3674
3675module_init(ipsec_pfkey_init);
3676module_exit(ipsec_pfkey_exit);
3677MODULE_LICENSE("GPL");
3678MODULE_ALIAS_NETPROTO(PF_KEY);