blob: 7ae641df70bdb1c70598360c777759163a0cbf93 [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;
Herbert Xu12a169e2008-10-01 07:03:24 -070061 struct sk_buff *skb;
Timo Teras83321d62008-03-03 23:40:12 -080062 } dump;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
66{
67 return (struct pfkey_sock *)sk;
68}
69
Timo Teras83321d62008-03-03 23:40:12 -080070static int pfkey_can_dump(struct sock *sk)
71{
72 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
73 return 1;
74 return 0;
75}
76
Timo Teras05238202008-10-01 05:17:54 -070077static void pfkey_terminate_dump(struct pfkey_sock *pfk)
Timo Teras83321d62008-03-03 23:40:12 -080078{
Timo Teras05238202008-10-01 05:17:54 -070079 if (pfk->dump.dump) {
Herbert Xu12a169e2008-10-01 07:03:24 -070080 if (pfk->dump.skb) {
81 kfree_skb(pfk->dump.skb);
82 pfk->dump.skb = NULL;
83 }
Timo Teras05238202008-10-01 05:17:54 -070084 pfk->dump.done(pfk);
85 pfk->dump.dump = NULL;
86 pfk->dump.done = NULL;
87 }
Timo Teras83321d62008-03-03 23:40:12 -080088}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void pfkey_sock_destruct(struct sock *sk)
91{
Timo Teras05238202008-10-01 05:17:54 -070092 pfkey_terminate_dump(pfkey_sk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 skb_queue_purge(&sk->sk_receive_queue);
94
95 if (!sock_flag(sk, SOCK_DEAD)) {
96 printk("Attempt to release alive pfkey socket: %p\n", sk);
97 return;
98 }
99
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700100 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
101 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 atomic_dec(&pfkey_socks_nr);
104}
105
106static void pfkey_table_grab(void)
107{
108 write_lock_bh(&pfkey_table_lock);
109
110 if (atomic_read(&pfkey_table_users)) {
111 DECLARE_WAITQUEUE(wait, current);
112
113 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
114 for(;;) {
115 set_current_state(TASK_UNINTERRUPTIBLE);
116 if (atomic_read(&pfkey_table_users) == 0)
117 break;
118 write_unlock_bh(&pfkey_table_lock);
119 schedule();
120 write_lock_bh(&pfkey_table_lock);
121 }
122
123 __set_current_state(TASK_RUNNING);
124 remove_wait_queue(&pfkey_table_wait, &wait);
125 }
126}
127
128static __inline__ void pfkey_table_ungrab(void)
129{
130 write_unlock_bh(&pfkey_table_lock);
131 wake_up(&pfkey_table_wait);
132}
133
134static __inline__ void pfkey_lock_table(void)
135{
136 /* read_lock() synchronizes us to pfkey_table_grab */
137
138 read_lock(&pfkey_table_lock);
139 atomic_inc(&pfkey_table_users);
140 read_unlock(&pfkey_table_lock);
141}
142
143static __inline__ void pfkey_unlock_table(void)
144{
145 if (atomic_dec_and_test(&pfkey_table_users))
146 wake_up(&pfkey_table_wait);
147}
148
149
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800150static const struct proto_ops pfkey_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152static void pfkey_insert(struct sock *sk)
153{
154 pfkey_table_grab();
155 sk_add_node(sk, &pfkey_table);
156 pfkey_table_ungrab();
157}
158
159static void pfkey_remove(struct sock *sk)
160{
161 pfkey_table_grab();
162 sk_del_node_init(sk);
163 pfkey_table_ungrab();
164}
165
166static struct proto key_proto = {
167 .name = "KEY",
168 .owner = THIS_MODULE,
169 .obj_size = sizeof(struct pfkey_sock),
170};
171
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700172static int pfkey_create(struct net *net, struct socket *sock, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct sock *sk;
175 int err;
176
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700177 if (net != &init_net)
178 return -EAFNOSUPPORT;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (!capable(CAP_NET_ADMIN))
181 return -EPERM;
182 if (sock->type != SOCK_RAW)
183 return -ESOCKTNOSUPPORT;
184 if (protocol != PF_KEY_V2)
185 return -EPROTONOSUPPORT;
186
187 err = -ENOMEM;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700188 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (sk == NULL)
190 goto out;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 sock->ops = &pfkey_ops;
193 sock_init_data(sock, sk);
194
195 sk->sk_family = PF_KEY;
196 sk->sk_destruct = pfkey_sock_destruct;
197
198 atomic_inc(&pfkey_socks_nr);
199
200 pfkey_insert(sk);
201
202 return 0;
203out:
204 return err;
205}
206
207static int pfkey_release(struct socket *sock)
208{
209 struct sock *sk = sock->sk;
210
211 if (!sk)
212 return 0;
213
214 pfkey_remove(sk);
215
216 sock_orphan(sk);
217 sock->sk = NULL;
218 skb_queue_purge(&sk->sk_write_queue);
219 sock_put(sk);
220
221 return 0;
222}
223
224static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
Al Virodd0fc662005-10-07 07:46:04 +0100225 gfp_t allocation, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 int err = -ENOBUFS;
228
229 sock_hold(sk);
230 if (*skb2 == NULL) {
231 if (atomic_read(&skb->users) != 1) {
232 *skb2 = skb_clone(skb, allocation);
233 } else {
234 *skb2 = skb;
235 atomic_inc(&skb->users);
236 }
237 }
238 if (*skb2 != NULL) {
239 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
240 skb_orphan(*skb2);
241 skb_set_owner_r(*skb2, sk);
242 skb_queue_tail(&sk->sk_receive_queue, *skb2);
243 sk->sk_data_ready(sk, (*skb2)->len);
244 *skb2 = NULL;
245 err = 0;
246 }
247 }
248 sock_put(sk);
249 return err;
250}
251
252/* Send SKB to all pfkey sockets matching selected criteria. */
253#define BROADCAST_ALL 0
254#define BROADCAST_ONE 1
255#define BROADCAST_REGISTERED 2
256#define BROADCAST_PROMISC_ONLY 4
Al Virodd0fc662005-10-07 07:46:04 +0100257static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int broadcast_flags, struct sock *one_sk)
259{
260 struct sock *sk;
261 struct hlist_node *node;
262 struct sk_buff *skb2 = NULL;
263 int err = -ESRCH;
264
265 /* XXX Do we need something like netlink_overrun? I think
266 * XXX PF_KEY socket apps will not mind current behavior.
267 */
268 if (!skb)
269 return -ENOMEM;
270
271 pfkey_lock_table();
272 sk_for_each(sk, node, &pfkey_table) {
273 struct pfkey_sock *pfk = pfkey_sk(sk);
274 int err2;
275
276 /* Yes, it means that if you are meant to receive this
277 * pfkey message you receive it twice as promiscuous
278 * socket.
279 */
280 if (pfk->promisc)
281 pfkey_broadcast_one(skb, &skb2, allocation, sk);
282
283 /* the exact target will be processed later */
284 if (sk == one_sk)
285 continue;
286 if (broadcast_flags != BROADCAST_ALL) {
287 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
288 continue;
289 if ((broadcast_flags & BROADCAST_REGISTERED) &&
290 !pfk->registered)
291 continue;
292 if (broadcast_flags & BROADCAST_ONE)
293 continue;
294 }
295
296 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
297
298 /* Error is cleare after succecful sending to at least one
299 * registered KM */
300 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
301 err = err2;
302 }
303 pfkey_unlock_table();
304
305 if (one_sk != NULL)
306 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
307
308 if (skb2)
309 kfree_skb(skb2);
310 kfree_skb(skb);
311 return err;
312}
313
Timo Teras05238202008-10-01 05:17:54 -0700314static int pfkey_do_dump(struct pfkey_sock *pfk)
315{
Herbert Xu12a169e2008-10-01 07:03:24 -0700316 struct sadb_msg *hdr;
Timo Teras05238202008-10-01 05:17:54 -0700317 int rc;
318
319 rc = pfk->dump.dump(pfk);
320 if (rc == -ENOBUFS)
321 return 0;
322
Herbert Xu12a169e2008-10-01 07:03:24 -0700323 if (pfk->dump.skb) {
324 if (!pfkey_can_dump(&pfk->sk))
325 return 0;
326
327 hdr = (struct sadb_msg *) pfk->dump.skb->data;
328 hdr->sadb_msg_seq = 0;
329 hdr->sadb_msg_errno = rc;
330 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
331 &pfk->sk);
332 pfk->dump.skb = NULL;
333 }
334
Timo Teras05238202008-10-01 05:17:54 -0700335 pfkey_terminate_dump(pfk);
336 return rc;
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
340{
341 *new = *orig;
342}
343
344static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
345{
346 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
347 struct sadb_msg *hdr;
348
349 if (!skb)
350 return -ENOBUFS;
351
352 /* Woe be to the platform trying to support PFKEY yet
353 * having normal errnos outside the 1-255 range, inclusive.
354 */
355 err = -err;
356 if (err == ERESTARTSYS ||
357 err == ERESTARTNOHAND ||
358 err == ERESTARTNOINTR)
359 err = EINTR;
360 if (err >= 512)
361 err = EINVAL;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800362 BUG_ON(err <= 0 || err >= 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
365 pfkey_hdr_dup(hdr, orig);
366 hdr->sadb_msg_errno = (uint8_t) err;
367 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
368 sizeof(uint64_t));
369
370 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);
371
372 return 0;
373}
374
375static u8 sadb_ext_min_len[] = {
376 [SADB_EXT_RESERVED] = (u8) 0,
377 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
378 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
379 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
380 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
381 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
382 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
383 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
384 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
385 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
386 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
387 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
388 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
389 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
390 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
391 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
392 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
393 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
394 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
395 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
396 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
397 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
398 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
399 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
Trent Jaegerdf718372005-12-13 23:12:27 -0800400 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401};
402
403/* Verify sadb_address_{len,prefixlen} against sa_family. */
404static int verify_address_len(void *p)
405{
406 struct sadb_address *sp = p;
407 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
408 struct sockaddr_in *sin;
409#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
410 struct sockaddr_in6 *sin6;
411#endif
412 int len;
413
414 switch (addr->sa_family) {
415 case AF_INET:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700416 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (sp->sadb_address_len != len ||
418 sp->sadb_address_prefixlen > 32)
419 return -EINVAL;
420 break;
421#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
422 case AF_INET6:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700423 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (sp->sadb_address_len != len ||
425 sp->sadb_address_prefixlen > 128)
426 return -EINVAL;
427 break;
428#endif
429 default:
430 /* It is user using kernel to keep track of security
431 * associations for another protocol, such as
432 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
433 * lengths.
434 *
435 * XXX Actually, association/policy database is not yet
436 * XXX able to cope with arbitrary sockaddr families.
437 * XXX When it can, remove this -EINVAL. -DaveM
438 */
439 return -EINVAL;
440 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 return 0;
444}
445
Trent Jaegerdf718372005-12-13 23:12:27 -0800446static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx)
447{
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700448 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
449 sec_ctx->sadb_x_ctx_len,
450 sizeof(uint64_t));
Trent Jaegerdf718372005-12-13 23:12:27 -0800451}
452
453static inline int verify_sec_ctx_len(void *p)
454{
455 struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p;
Stephen Rothwell298bb622007-10-30 23:57:05 -0700456 int len = sec_ctx->sadb_x_ctx_len;
Trent Jaegerdf718372005-12-13 23:12:27 -0800457
Stephen Rothwell298bb622007-10-30 23:57:05 -0700458 if (len > PAGE_SIZE)
Trent Jaegerdf718372005-12-13 23:12:27 -0800459 return -EINVAL;
460
461 len = pfkey_sec_ctx_len(sec_ctx);
462
463 if (sec_ctx->sadb_x_sec_len != len)
464 return -EINVAL;
465
466 return 0;
467}
468
469static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx)
470{
471 struct xfrm_user_sec_ctx *uctx = NULL;
472 int ctx_size = sec_ctx->sadb_x_ctx_len;
473
474 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
475
476 if (!uctx)
477 return NULL;
478
479 uctx->len = pfkey_sec_ctx_len(sec_ctx);
480 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
481 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
482 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
483 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
484 memcpy(uctx + 1, sec_ctx + 1,
485 uctx->ctx_len);
486
487 return uctx;
488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490static int present_and_same_family(struct sadb_address *src,
491 struct sadb_address *dst)
492{
493 struct sockaddr *s_addr, *d_addr;
494
495 if (!src || !dst)
496 return 0;
497
498 s_addr = (struct sockaddr *)(src + 1);
499 d_addr = (struct sockaddr *)(dst + 1);
500 if (s_addr->sa_family != d_addr->sa_family)
501 return 0;
502 if (s_addr->sa_family != AF_INET
503#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
504 && s_addr->sa_family != AF_INET6
505#endif
506 )
507 return 0;
508
509 return 1;
510}
511
512static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
513{
514 char *p = (char *) hdr;
515 int len = skb->len;
516
517 len -= sizeof(*hdr);
518 p += sizeof(*hdr);
519 while (len > 0) {
520 struct sadb_ext *ehdr = (struct sadb_ext *) p;
521 uint16_t ext_type;
522 int ext_len;
523
524 ext_len = ehdr->sadb_ext_len;
525 ext_len *= sizeof(uint64_t);
526 ext_type = ehdr->sadb_ext_type;
527 if (ext_len < sizeof(uint64_t) ||
528 ext_len > len ||
529 ext_type == SADB_EXT_RESERVED)
530 return -EINVAL;
531
532 if (ext_type <= SADB_EXT_MAX) {
533 int min = (int) sadb_ext_min_len[ext_type];
534 if (ext_len < min)
535 return -EINVAL;
536 if (ext_hdrs[ext_type-1] != NULL)
537 return -EINVAL;
538 if (ext_type == SADB_EXT_ADDRESS_SRC ||
539 ext_type == SADB_EXT_ADDRESS_DST ||
540 ext_type == SADB_EXT_ADDRESS_PROXY ||
541 ext_type == SADB_X_EXT_NAT_T_OA) {
542 if (verify_address_len(p))
543 return -EINVAL;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900544 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800545 if (ext_type == SADB_X_EXT_SEC_CTX) {
546 if (verify_sec_ctx_len(p))
547 return -EINVAL;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 ext_hdrs[ext_type-1] = p;
550 }
551 p += ext_len;
552 len -= ext_len;
553 }
554
555 return 0;
556}
557
558static uint16_t
559pfkey_satype2proto(uint8_t satype)
560{
561 switch (satype) {
562 case SADB_SATYPE_UNSPEC:
563 return IPSEC_PROTO_ANY;
564 case SADB_SATYPE_AH:
565 return IPPROTO_AH;
566 case SADB_SATYPE_ESP:
567 return IPPROTO_ESP;
568 case SADB_X_SATYPE_IPCOMP:
569 return IPPROTO_COMP;
570 break;
571 default:
572 return 0;
573 }
574 /* NOTREACHED */
575}
576
577static uint8_t
578pfkey_proto2satype(uint16_t proto)
579{
580 switch (proto) {
581 case IPPROTO_AH:
582 return SADB_SATYPE_AH;
583 case IPPROTO_ESP:
584 return SADB_SATYPE_ESP;
585 case IPPROTO_COMP:
586 return SADB_X_SATYPE_IPCOMP;
587 break;
588 default:
589 return 0;
590 }
591 /* NOTREACHED */
592}
593
594/* BTW, this scheme means that there is no way with PFKEY2 sockets to
595 * say specifically 'just raw sockets' as we encode them as 255.
596 */
597
598static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
599{
600 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
601}
602
603static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
604{
605 return (proto ? proto : IPSEC_PROTO_ANY);
606}
607
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900608static inline int pfkey_sockaddr_len(sa_family_t family)
609{
610 switch (family) {
611 case AF_INET:
612 return sizeof(struct sockaddr_in);
613#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
614 case AF_INET6:
615 return sizeof(struct sockaddr_in6);
616#endif
617 }
618 return 0;
619}
620
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900621static
622int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900624 switch (sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 case AF_INET:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900626 xaddr->a4 =
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900627 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return AF_INET;
629#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
630 case AF_INET6:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900631 memcpy(xaddr->a6,
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900632 &((struct sockaddr_in6 *)sa)->sin6_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 sizeof(struct in6_addr));
634 return AF_INET6;
635#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900637 return 0;
638}
639
640static
641int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr, xfrm_address_t *xaddr)
642{
643 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
644 xaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
647static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
648{
649 struct sadb_sa *sa;
650 struct sadb_address *addr;
651 uint16_t proto;
652 unsigned short family;
653 xfrm_address_t *xaddr;
654
655 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
656 if (sa == NULL)
657 return NULL;
658
659 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
660 if (proto == 0)
661 return NULL;
662
663 /* sadb_address_len should be checked by caller */
664 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
665 if (addr == NULL)
666 return NULL;
667
668 family = ((struct sockaddr *)(addr + 1))->sa_family;
669 switch (family) {
670 case AF_INET:
671 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
672 break;
673#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
674 case AF_INET6:
675 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
676 break;
677#endif
678 default:
679 xaddr = NULL;
680 }
681
682 if (!xaddr)
683 return NULL;
684
685 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
686}
687
688#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static int
691pfkey_sockaddr_size(sa_family_t family)
692{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900693 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700696static inline int pfkey_mode_from_xfrm(int mode)
697{
698 switch(mode) {
699 case XFRM_MODE_TRANSPORT:
700 return IPSEC_MODE_TRANSPORT;
701 case XFRM_MODE_TUNNEL:
702 return IPSEC_MODE_TUNNEL;
703 case XFRM_MODE_BEET:
704 return IPSEC_MODE_BEET;
705 default:
706 return -1;
707 }
708}
709
710static inline int pfkey_mode_to_xfrm(int mode)
711{
712 switch(mode) {
713 case IPSEC_MODE_ANY: /*XXX*/
714 case IPSEC_MODE_TRANSPORT:
715 return XFRM_MODE_TRANSPORT;
716 case IPSEC_MODE_TUNNEL:
717 return XFRM_MODE_TUNNEL;
718 case IPSEC_MODE_BEET:
719 return XFRM_MODE_BEET;
720 default:
721 return -1;
722 }
723}
724
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900725static unsigned int pfkey_sockaddr_fill(xfrm_address_t *xaddr, __be16 port,
726 struct sockaddr *sa,
727 unsigned short family)
728{
729 switch (family) {
730 case AF_INET:
731 {
732 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
733 sin->sin_family = AF_INET;
734 sin->sin_port = port;
735 sin->sin_addr.s_addr = xaddr->a4;
736 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
737 return 32;
738 }
739#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
740 case AF_INET6:
741 {
742 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
743 sin6->sin6_family = AF_INET6;
744 sin6->sin6_port = port;
745 sin6->sin6_flowinfo = 0;
746 ipv6_addr_copy(&sin6->sin6_addr, (struct in6_addr *)xaddr->a6);
747 sin6->sin6_scope_id = 0;
748 return 128;
749 }
750#endif
751 }
752 return 0;
753}
754
Herbert Xu050f0092007-10-09 13:31:47 -0700755static struct sk_buff *__pfkey_xfrm_state2msg(struct xfrm_state *x,
756 int add_keys, int hsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct sk_buff *skb;
759 struct sadb_msg *hdr;
760 struct sadb_sa *sa;
761 struct sadb_lifetime *lifetime;
762 struct sadb_address *addr;
763 struct sadb_key *key;
764 struct sadb_x_sa2 *sa2;
Trent Jaegerdf718372005-12-13 23:12:27 -0800765 struct sadb_x_sec_ctx *sec_ctx;
766 struct xfrm_sec_ctx *xfrm_ctx;
767 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int size;
769 int auth_key_size = 0;
770 int encrypt_key_size = 0;
771 int sockaddr_size;
772 struct xfrm_encap_tmpl *natt = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700773 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 /* address family check */
776 sockaddr_size = pfkey_sockaddr_size(x->props.family);
777 if (!sockaddr_size)
778 return ERR_PTR(-EINVAL);
779
780 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
781 key(AE), (identity(SD),) (sensitivity)> */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900782 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 sizeof(struct sadb_lifetime) +
784 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
785 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900786 sizeof(struct sadb_address)*2 +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 sockaddr_size*2 +
788 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800789
790 if ((xfrm_ctx = x->security)) {
791 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
792 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
793 }
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* identity & sensitivity */
YOSHIFUJI Hideaki81b302a2008-04-28 03:17:38 +0900796 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr, x->props.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 size += sizeof(struct sadb_address) + sockaddr_size;
798
799 if (add_keys) {
800 if (x->aalg && x->aalg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900801 auth_key_size =
802 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 size += sizeof(struct sadb_key) + auth_key_size;
804 }
805 if (x->ealg && x->ealg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900806 encrypt_key_size =
807 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 size += sizeof(struct sadb_key) + encrypt_key_size;
809 }
810 }
811 if (x->encap)
812 natt = x->encap;
813
814 if (natt && natt->encap_type) {
815 size += sizeof(struct sadb_x_nat_t_type);
816 size += sizeof(struct sadb_x_nat_t_port);
817 size += sizeof(struct sadb_x_nat_t_port);
818 }
819
820 skb = alloc_skb(size + 16, GFP_ATOMIC);
821 if (skb == NULL)
822 return ERR_PTR(-ENOBUFS);
823
824 /* call should fill header later */
825 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
826 memset(hdr, 0, size); /* XXX do we need this ? */
827 hdr->sadb_msg_len = size / sizeof(uint64_t);
828
829 /* sa */
830 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
831 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
832 sa->sadb_sa_exttype = SADB_EXT_SA;
833 sa->sadb_sa_spi = x->id.spi;
834 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700835 switch (x->km.state) {
836 case XFRM_STATE_VALID:
837 sa->sadb_sa_state = x->km.dying ?
838 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
839 break;
840 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700842 break;
843 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700845 break;
846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 sa->sadb_sa_auth = 0;
848 if (x->aalg) {
849 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
850 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
851 }
852 sa->sadb_sa_encrypt = 0;
853 BUG_ON(x->ealg && x->calg);
854 if (x->ealg) {
855 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
856 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
857 }
858 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
859 if (x->calg) {
860 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
861 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
862 }
863
864 sa->sadb_sa_flags = 0;
865 if (x->props.flags & XFRM_STATE_NOECN)
866 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
867 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
868 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700869 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
870 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 /* hard time */
873 if (hsc & 2) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900874 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 sizeof(struct sadb_lifetime));
876 lifetime->sadb_lifetime_len =
877 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
878 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
879 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
880 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
881 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
882 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
883 }
884 /* soft time */
885 if (hsc & 1) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900886 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 sizeof(struct sadb_lifetime));
888 lifetime->sadb_lifetime_len =
889 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
890 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
891 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
892 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
893 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
894 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
895 }
896 /* current time */
897 lifetime = (struct sadb_lifetime *) skb_put(skb,
898 sizeof(struct sadb_lifetime));
899 lifetime->sadb_lifetime_len =
900 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
901 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
902 lifetime->sadb_lifetime_allocations = x->curlft.packets;
903 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
904 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
905 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
906 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900907 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900909 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 (sizeof(struct sadb_address)+sockaddr_size)/
911 sizeof(uint64_t);
912 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900913 /* "if the ports are non-zero, then the sadb_address_proto field,
914 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900916 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900919 addr->sadb_address_prefixlen =
920 pfkey_sockaddr_fill(&x->props.saddr, 0,
921 (struct sockaddr *) (addr + 1),
922 x->props.family);
923 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 BUG();
925
926 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900927 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900929 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 (sizeof(struct sadb_address)+sockaddr_size)/
931 sizeof(uint64_t);
932 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900933 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900936 addr->sadb_address_prefixlen =
937 pfkey_sockaddr_fill(&x->id.daddr, 0,
938 (struct sockaddr *) (addr + 1),
939 x->props.family);
940 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 BUG();
942
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900943 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr,
944 x->props.family)) {
945 addr = (struct sadb_address*) skb_put(skb,
946 sizeof(struct sadb_address)+sockaddr_size);
947 addr->sadb_address_len =
948 (sizeof(struct sadb_address)+sockaddr_size)/
949 sizeof(uint64_t);
950 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
951 addr->sadb_address_proto =
952 pfkey_proto_from_xfrm(x->sel.proto);
953 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
954 addr->sadb_address_reserved = 0;
955
956 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
957 (struct sockaddr *) (addr + 1),
958 x->props.family);
959 }
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /* auth key */
962 if (add_keys && auth_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900963 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 sizeof(struct sadb_key)+auth_key_size);
965 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
966 sizeof(uint64_t);
967 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
968 key->sadb_key_bits = x->aalg->alg_key_len;
969 key->sadb_key_reserved = 0;
970 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
971 }
972 /* encrypt key */
973 if (add_keys && encrypt_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900974 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 sizeof(struct sadb_key)+encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900976 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 encrypt_key_size) / sizeof(uint64_t);
978 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
979 key->sadb_key_bits = x->ealg->alg_key_len;
980 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900981 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 (x->ealg->alg_key_len+7)/8);
983 }
984
985 /* sa */
986 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
987 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
988 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700989 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
990 kfree_skb(skb);
991 return ERR_PTR(-EINVAL);
992 }
993 sa2->sadb_x_sa2_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 sa2->sadb_x_sa2_reserved1 = 0;
995 sa2->sadb_x_sa2_reserved2 = 0;
996 sa2->sadb_x_sa2_sequence = 0;
997 sa2->sadb_x_sa2_reqid = x->props.reqid;
998
999 if (natt && natt->encap_type) {
1000 struct sadb_x_nat_t_type *n_type;
1001 struct sadb_x_nat_t_port *n_port;
1002
1003 /* type */
1004 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
1005 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
1006 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
1007 n_type->sadb_x_nat_t_type_type = natt->encap_type;
1008 n_type->sadb_x_nat_t_type_reserved[0] = 0;
1009 n_type->sadb_x_nat_t_type_reserved[1] = 0;
1010 n_type->sadb_x_nat_t_type_reserved[2] = 0;
1011
1012 /* source port */
1013 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
1014 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1015 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
1016 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
1017 n_port->sadb_x_nat_t_port_reserved = 0;
1018
1019 /* dest port */
1020 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
1021 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1022 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
1023 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
1024 n_port->sadb_x_nat_t_port_reserved = 0;
1025 }
1026
Trent Jaegerdf718372005-12-13 23:12:27 -08001027 /* security context */
1028 if (xfrm_ctx) {
1029 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1030 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1031 sec_ctx->sadb_x_sec_len =
1032 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1033 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1034 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1035 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1036 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1037 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1038 xfrm_ctx->ctx_len);
1039 }
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return skb;
1042}
1043
Herbert Xu050f0092007-10-09 13:31:47 -07001044
1045static inline struct sk_buff *pfkey_xfrm_state2msg(struct xfrm_state *x)
1046{
1047 struct sk_buff *skb;
1048
Herbert Xu050f0092007-10-09 13:31:47 -07001049 skb = __pfkey_xfrm_state2msg(x, 1, 3);
Herbert Xu050f0092007-10-09 13:31:47 -07001050
1051 return skb;
1052}
1053
1054static inline struct sk_buff *pfkey_xfrm_state2msg_expire(struct xfrm_state *x,
1055 int hsc)
1056{
1057 return __pfkey_xfrm_state2msg(x, 0, hsc);
1058}
1059
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001060static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 void **ext_hdrs)
1062{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001063 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct sadb_lifetime *lifetime;
1065 struct sadb_sa *sa;
1066 struct sadb_key *key;
Trent Jaegerdf718372005-12-13 23:12:27 -08001067 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 uint16_t proto;
1069 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
1073 if (!sa ||
1074 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1075 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1076 return ERR_PTR(-EINVAL);
1077 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1078 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1079 return ERR_PTR(-EINVAL);
1080 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1081 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1082 return ERR_PTR(-EINVAL);
1083 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1084 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1085 return ERR_PTR(-EINVAL);
1086
1087 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1088 if (proto == 0)
1089 return ERR_PTR(-EINVAL);
1090
1091 /* default error is no buffer space */
1092 err = -ENOBUFS;
1093
1094 /* RFC2367:
1095
1096 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1097 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1098 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1099 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1100 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1101 not true.
1102
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001103 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1105 */
1106 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1107 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1108 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1109 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1110 return ERR_PTR(-EINVAL);
1111 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1112 if (key != NULL &&
1113 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1114 ((key->sadb_key_bits+7) / 8 == 0 ||
1115 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1116 return ERR_PTR(-EINVAL);
1117 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1118 if (key != NULL &&
1119 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1120 ((key->sadb_key_bits+7) / 8 == 0 ||
1121 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1122 return ERR_PTR(-EINVAL);
1123
1124 x = xfrm_state_alloc();
1125 if (x == NULL)
1126 return ERR_PTR(-ENOBUFS);
1127
1128 x->id.proto = proto;
1129 x->id.spi = sa->sadb_sa_spi;
1130 x->props.replay_window = sa->sadb_sa_replay;
1131 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1132 x->props.flags |= XFRM_STATE_NOECN;
1133 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1134 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001135 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1136 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
1139 if (lifetime != NULL) {
1140 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1141 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1142 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1143 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1144 }
1145 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
1146 if (lifetime != NULL) {
1147 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1148 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1149 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1150 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1151 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001152
1153 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
1154 if (sec_ctx != NULL) {
1155 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1156
1157 if (!uctx)
1158 goto out;
1159
1160 err = security_xfrm_state_alloc(x, uctx);
1161 kfree(uctx);
1162
1163 if (err)
1164 goto out;
1165 }
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1168 if (sa->sadb_sa_auth) {
1169 int keysize = 0;
1170 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1171 if (!a) {
1172 err = -ENOSYS;
1173 goto out;
1174 }
1175 if (key)
1176 keysize = (key->sadb_key_bits + 7) / 8;
1177 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1178 if (!x->aalg)
1179 goto out;
1180 strcpy(x->aalg->alg_name, a->name);
1181 x->aalg->alg_key_len = 0;
1182 if (key) {
1183 x->aalg->alg_key_len = key->sadb_key_bits;
1184 memcpy(x->aalg->alg_key, key+1, keysize);
1185 }
1186 x->props.aalgo = sa->sadb_sa_auth;
1187 /* x->algo.flags = sa->sadb_sa_flags; */
1188 }
1189 if (sa->sadb_sa_encrypt) {
1190 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1191 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1192 if (!a) {
1193 err = -ENOSYS;
1194 goto out;
1195 }
1196 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1197 if (!x->calg)
1198 goto out;
1199 strcpy(x->calg->alg_name, a->name);
1200 x->props.calgo = sa->sadb_sa_encrypt;
1201 } else {
1202 int keysize = 0;
1203 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1204 if (!a) {
1205 err = -ENOSYS;
1206 goto out;
1207 }
1208 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1209 if (key)
1210 keysize = (key->sadb_key_bits + 7) / 8;
1211 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1212 if (!x->ealg)
1213 goto out;
1214 strcpy(x->ealg->alg_name, a->name);
1215 x->ealg->alg_key_len = 0;
1216 if (key) {
1217 x->ealg->alg_key_len = key->sadb_key_bits;
1218 memcpy(x->ealg->alg_key, key+1, keysize);
1219 }
1220 x->props.ealgo = sa->sadb_sa_encrypt;
1221 }
1222 }
1223 /* x->algo.flags = sa->sadb_sa_flags; */
1224
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001225 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 &x->props.saddr);
1227 if (!x->props.family) {
1228 err = -EAFNOSUPPORT;
1229 goto out;
1230 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001231 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 &x->id.daddr);
1233
1234 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1235 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001236 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1237 if (mode < 0) {
1238 err = -EINVAL;
1239 goto out;
1240 }
1241 x->props.mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 x->props.reqid = sa2->sadb_x_sa2_reqid;
1243 }
1244
1245 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1246 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1247
1248 /* Nobody uses this, but we try. */
1249 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1250 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1251 }
1252
Kazunori MIYAZAWA4da51052008-05-21 13:26:11 -07001253 if (!x->sel.family)
Joy Latten4a4b6272007-08-02 19:25:43 -07001254 x->sel.family = x->props.family;
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1257 struct sadb_x_nat_t_type* n_type;
1258 struct xfrm_encap_tmpl *natt;
1259
1260 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1261 if (!x->encap)
1262 goto out;
1263
1264 natt = x->encap;
1265 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1266 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1267
1268 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1269 struct sadb_x_nat_t_port* n_port =
1270 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1271 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1272 }
1273 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1274 struct sadb_x_nat_t_port* n_port =
1275 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1276 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1277 }
1278 }
1279
Herbert Xu72cb6962005-06-20 13:18:08 -07001280 err = xfrm_init_state(x);
1281 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return x;
1286
1287out:
1288 x->km.state = XFRM_STATE_DEAD;
1289 xfrm_state_put(x);
1290 return ERR_PTR(err);
1291}
1292
1293static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1294{
1295 return -EOPNOTSUPP;
1296}
1297
1298static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1299{
1300 struct sk_buff *resp_skb;
1301 struct sadb_x_sa2 *sa2;
1302 struct sadb_address *saddr, *daddr;
1303 struct sadb_msg *out_hdr;
Herbert Xu658b2192007-10-09 13:29:52 -07001304 struct sadb_spirange *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 struct xfrm_state *x = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001306 int mode;
Herbert Xu658b2192007-10-09 13:29:52 -07001307 int err;
1308 u32 min_spi, max_spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 u32 reqid;
1310 u8 proto;
1311 unsigned short family;
1312 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1313
1314 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1315 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1316 return -EINVAL;
1317
1318 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1319 if (proto == 0)
1320 return -EINVAL;
1321
1322 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001323 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1324 if (mode < 0)
1325 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 reqid = sa2->sadb_x_sa2_reqid;
1327 } else {
1328 mode = 0;
1329 reqid = 0;
1330 }
1331
1332 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1333 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1334
1335 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1336 switch (family) {
1337 case AF_INET:
1338 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1339 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1340 break;
1341#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1342 case AF_INET6:
1343 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1344 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1345 break;
1346#endif
1347 }
1348
1349 if (hdr->sadb_msg_seq) {
1350 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1351 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1352 xfrm_state_put(x);
1353 x = NULL;
1354 }
1355 }
1356
1357 if (!x)
1358 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1359
1360 if (x == NULL)
1361 return -ENOENT;
1362
Herbert Xu658b2192007-10-09 13:29:52 -07001363 min_spi = 0x100;
1364 max_spi = 0x0fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Herbert Xu658b2192007-10-09 13:29:52 -07001366 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1367 if (range) {
1368 min_spi = range->sadb_spirange_min;
1369 max_spi = range->sadb_spirange_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
Herbert Xu658b2192007-10-09 13:29:52 -07001371
1372 err = xfrm_alloc_spi(x, min_spi, max_spi);
Herbert Xu050f0092007-10-09 13:31:47 -07001373 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 if (IS_ERR(resp_skb)) {
1376 xfrm_state_put(x);
1377 return PTR_ERR(resp_skb);
1378 }
1379
1380 out_hdr = (struct sadb_msg *) resp_skb->data;
1381 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1382 out_hdr->sadb_msg_type = SADB_GETSPI;
1383 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1384 out_hdr->sadb_msg_errno = 0;
1385 out_hdr->sadb_msg_reserved = 0;
1386 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1387 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1388
1389 xfrm_state_put(x);
1390
1391 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1392
1393 return 0;
1394}
1395
1396static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1397{
1398 struct xfrm_state *x;
1399
1400 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1401 return -EOPNOTSUPP;
1402
1403 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1404 return 0;
1405
1406 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1407 if (x == NULL)
1408 return 0;
1409
1410 spin_lock_bh(&x->lock);
1411 if (x->km.state == XFRM_STATE_ACQ) {
1412 x->km.state = XFRM_STATE_ERROR;
1413 wake_up(&km_waitq);
1414 }
1415 spin_unlock_bh(&x->lock);
1416 xfrm_state_put(x);
1417 return 0;
1418}
1419
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001420static inline int event2poltype(int event)
1421{
1422 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001423 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001424 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001425 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001426 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001427 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001428 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001429 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001430 // return SADB_X_SPDEXPIRE;
1431 default:
1432 printk("pfkey: Unknown policy event %d\n", event);
1433 break;
1434 }
1435
1436 return 0;
1437}
1438
1439static inline int event2keytype(int event)
1440{
1441 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001442 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001443 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001444 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001445 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001446 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001447 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001448 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001449 return SADB_EXPIRE;
1450 default:
1451 printk("pfkey: Unknown SA event %d\n", event);
1452 break;
1453 }
1454
1455 return 0;
1456}
1457
1458/* ADD/UPD/DEL */
1459static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1460{
1461 struct sk_buff *skb;
1462 struct sadb_msg *hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001463
Herbert Xu050f0092007-10-09 13:31:47 -07001464 skb = pfkey_xfrm_state2msg(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001465
1466 if (IS_ERR(skb))
1467 return PTR_ERR(skb);
1468
1469 hdr = (struct sadb_msg *) skb->data;
1470 hdr->sadb_msg_version = PF_KEY_V2;
1471 hdr->sadb_msg_type = event2keytype(c->event);
1472 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1473 hdr->sadb_msg_errno = 0;
1474 hdr->sadb_msg_reserved = 0;
1475 hdr->sadb_msg_seq = c->seq;
1476 hdr->sadb_msg_pid = c->pid;
1477
1478 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1479
1480 return 0;
1481}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1484{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 struct xfrm_state *x;
1486 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001487 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1490 if (IS_ERR(x))
1491 return PTR_ERR(x);
1492
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001493 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (hdr->sadb_msg_type == SADB_ADD)
1495 err = xfrm_state_add(x);
1496 else
1497 err = xfrm_state_update(x);
1498
Joy Lattenab5f5e82007-09-17 11:51:22 -07001499 xfrm_audit_state_add(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001500 audit_get_loginuid(current),
1501 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (err < 0) {
1504 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001505 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
1508
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001509 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001510 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001511 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001512 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001513 c.seq = hdr->sadb_msg_seq;
1514 c.pid = hdr->sadb_msg_pid;
1515 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001516out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001517 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001518 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
1521static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1522{
1523 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001524 struct km_event c;
1525 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 if (!ext_hdrs[SADB_EXT_SA-1] ||
1528 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1529 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1530 return -EINVAL;
1531
1532 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1533 if (x == NULL)
1534 return -ESRCH;
1535
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001536 if ((err = security_xfrm_state_delete(x)))
1537 goto out;
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001540 err = -EPERM;
1541 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
Joy Latten161a09e2006-11-27 13:11:54 -06001543
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001544 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001545
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001546 if (err < 0)
1547 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001548
1549 c.seq = hdr->sadb_msg_seq;
1550 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001551 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001552 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001553out:
Joy Lattenab5f5e82007-09-17 11:51:22 -07001554 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001555 audit_get_loginuid(current),
1556 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 xfrm_state_put(x);
1558
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001559 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
1562static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1563{
1564 __u8 proto;
1565 struct sk_buff *out_skb;
1566 struct sadb_msg *out_hdr;
1567 struct xfrm_state *x;
1568
1569 if (!ext_hdrs[SADB_EXT_SA-1] ||
1570 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1571 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1572 return -EINVAL;
1573
1574 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1575 if (x == NULL)
1576 return -ESRCH;
1577
Herbert Xu050f0092007-10-09 13:31:47 -07001578 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 proto = x->id.proto;
1580 xfrm_state_put(x);
1581 if (IS_ERR(out_skb))
1582 return PTR_ERR(out_skb);
1583
1584 out_hdr = (struct sadb_msg *) out_skb->data;
1585 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Charles Hardin435000b2007-11-22 19:35:15 +08001586 out_hdr->sadb_msg_type = SADB_GET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1588 out_hdr->sadb_msg_errno = 0;
1589 out_hdr->sadb_msg_reserved = 0;
1590 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1591 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1592 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1593
1594 return 0;
1595}
1596
Randy Dunlap00fa0232005-10-04 22:43:04 -07001597static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001598 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 struct sk_buff *skb;
1601 struct sadb_msg *hdr;
1602 int len, auth_len, enc_len, i;
1603
1604 auth_len = xfrm_count_auth_supported();
1605 if (auth_len) {
1606 auth_len *= sizeof(struct sadb_alg);
1607 auth_len += sizeof(struct sadb_supported);
1608 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 enc_len = xfrm_count_enc_supported();
1611 if (enc_len) {
1612 enc_len *= sizeof(struct sadb_alg);
1613 enc_len += sizeof(struct sadb_supported);
1614 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 len = enc_len + auth_len + sizeof(struct sadb_msg);
1617
1618 skb = alloc_skb(len + 16, allocation);
1619 if (!skb)
1620 goto out_put_algs;
1621
1622 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1623 pfkey_hdr_dup(hdr, orig);
1624 hdr->sadb_msg_errno = 0;
1625 hdr->sadb_msg_len = len / sizeof(uint64_t);
1626
1627 if (auth_len) {
1628 struct sadb_supported *sp;
1629 struct sadb_alg *ap;
1630
1631 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1632 ap = (struct sadb_alg *) (sp + 1);
1633
1634 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1635 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1636
1637 for (i = 0; ; i++) {
1638 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1639 if (!aalg)
1640 break;
1641 if (aalg->available)
1642 *ap++ = aalg->desc;
1643 }
1644 }
1645
1646 if (enc_len) {
1647 struct sadb_supported *sp;
1648 struct sadb_alg *ap;
1649
1650 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1651 ap = (struct sadb_alg *) (sp + 1);
1652
1653 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1654 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1655
1656 for (i = 0; ; i++) {
1657 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1658 if (!ealg)
1659 break;
1660 if (ealg->available)
1661 *ap++ = ealg->desc;
1662 }
1663 }
1664
1665out_put_algs:
1666 return skb;
1667}
1668
1669static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1670{
1671 struct pfkey_sock *pfk = pfkey_sk(sk);
1672 struct sk_buff *supp_skb;
1673
1674 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1675 return -EINVAL;
1676
1677 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1678 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1679 return -EEXIST;
1680 pfk->registered |= (1<<hdr->sadb_msg_satype);
1681 }
1682
1683 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1686 if (!supp_skb) {
1687 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1688 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1689
1690 return -ENOBUFS;
1691 }
1692
1693 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1694
1695 return 0;
1696}
1697
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001698static int key_notify_sa_flush(struct km_event *c)
1699{
1700 struct sk_buff *skb;
1701 struct sadb_msg *hdr;
1702
1703 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1704 if (!skb)
1705 return -ENOBUFS;
1706 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf088672005-06-18 22:44:00 -07001707 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001708 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001709 hdr->sadb_msg_seq = c->seq;
1710 hdr->sadb_msg_pid = c->pid;
1711 hdr->sadb_msg_version = PF_KEY_V2;
1712 hdr->sadb_msg_errno = (uint8_t) 0;
1713 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1714
1715 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1716
1717 return 0;
1718}
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1721{
1722 unsigned proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001723 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06001724 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001725 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1728 if (proto == 0)
1729 return -EINVAL;
1730
Al Viro0c11b942008-01-10 04:20:52 -05001731 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04001732 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06001733 audit_info.secid = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -04001734 err = xfrm_state_flush(proto, &audit_info);
1735 if (err)
1736 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001737 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001738 c.seq = hdr->sadb_msg_seq;
1739 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001740 c.event = XFRM_MSG_FLUSHSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001741 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 return 0;
1744}
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1747{
Timo Teras83321d62008-03-03 23:40:12 -08001748 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 struct sk_buff *out_skb;
1750 struct sadb_msg *out_hdr;
1751
Timo Teras83321d62008-03-03 23:40:12 -08001752 if (!pfkey_can_dump(&pfk->sk))
1753 return -ENOBUFS;
1754
Herbert Xu050f0092007-10-09 13:31:47 -07001755 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (IS_ERR(out_skb))
1757 return PTR_ERR(out_skb);
1758
1759 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08001760 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 out_hdr->sadb_msg_type = SADB_DUMP;
1762 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1763 out_hdr->sadb_msg_errno = 0;
1764 out_hdr->sadb_msg_reserved = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07001765 out_hdr->sadb_msg_seq = count + 1;
Timo Teras83321d62008-03-03 23:40:12 -08001766 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
Herbert Xu12a169e2008-10-01 07:03:24 -07001767
1768 if (pfk->dump.skb)
1769 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
1770 &pfk->sk);
1771 pfk->dump.skb = out_skb;
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return 0;
1774}
1775
Timo Teras83321d62008-03-03 23:40:12 -08001776static int pfkey_dump_sa(struct pfkey_sock *pfk)
1777{
1778 return xfrm_state_walk(&pfk->dump.u.state, dump_sa, (void *) pfk);
1779}
1780
1781static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1782{
1783 xfrm_state_walk_done(&pfk->dump.u.state);
1784}
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1787{
1788 u8 proto;
Timo Teras83321d62008-03-03 23:40:12 -08001789 struct pfkey_sock *pfk = pfkey_sk(sk);
1790
1791 if (pfk->dump.dump != NULL)
1792 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1795 if (proto == 0)
1796 return -EINVAL;
1797
Timo Teras83321d62008-03-03 23:40:12 -08001798 pfk->dump.msg_version = hdr->sadb_msg_version;
1799 pfk->dump.msg_pid = hdr->sadb_msg_pid;
1800 pfk->dump.dump = pfkey_dump_sa;
1801 pfk->dump.done = pfkey_dump_sa_done;
1802 xfrm_state_walk_init(&pfk->dump.u.state, proto);
Timo Teras4c563f72008-02-28 21:31:08 -08001803
Timo Teras83321d62008-03-03 23:40:12 -08001804 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
1807static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1808{
1809 struct pfkey_sock *pfk = pfkey_sk(sk);
1810 int satype = hdr->sadb_msg_satype;
1811
1812 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1813 /* XXX we mangle packet... */
1814 hdr->sadb_msg_errno = 0;
1815 if (satype != 0 && satype != 1)
1816 return -EINVAL;
1817 pfk->promisc = satype;
1818 }
1819 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1820 return 0;
1821}
1822
1823static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1824{
1825 int i;
1826 u32 reqid = *(u32*)ptr;
1827
1828 for (i=0; i<xp->xfrm_nr; i++) {
1829 if (xp->xfrm_vec[i].reqid == reqid)
1830 return -EEXIST;
1831 }
1832 return 0;
1833}
1834
1835static u32 gen_reqid(void)
1836{
Timo Teras4c563f72008-02-28 21:31:08 -08001837 struct xfrm_policy_walk walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 u32 start;
Timo Teras4c563f72008-02-28 21:31:08 -08001839 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1841
1842 start = reqid;
1843 do {
1844 ++reqid;
1845 if (reqid == 0)
1846 reqid = IPSEC_MANUAL_REQID_MAX+1;
Timo Teras4c563f72008-02-28 21:31:08 -08001847 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
1848 rc = xfrm_policy_walk(&walk, check_reqid, (void*)&reqid);
1849 xfrm_policy_walk_done(&walk);
1850 if (rc != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return reqid;
1852 } while (reqid != start);
1853 return 0;
1854}
1855
1856static int
1857parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1858{
1859 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001860 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1863 return -ELOOP;
1864
1865 if (rq->sadb_x_ipsecrequest_mode == 0)
1866 return -EINVAL;
1867
1868 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001869 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1870 return -EINVAL;
1871 t->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1873 t->optional = 1;
1874 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1875 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1876 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1877 t->reqid = 0;
1878 if (!t->reqid && !(t->reqid = gen_reqid()))
1879 return -ENOBUFS;
1880 }
1881
1882 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001883 if (t->mode == XFRM_MODE_TUNNEL) {
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001884 u8 *sa = (u8 *) (rq + 1);
1885 int family, socklen;
1886
1887 family = pfkey_sockaddr_extract((struct sockaddr *)sa,
1888 &t->saddr);
1889 if (!family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return -EINVAL;
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001891
1892 socklen = pfkey_sockaddr_len(family);
1893 if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen),
1894 &t->id.daddr) != family)
1895 return -EINVAL;
1896 t->encap_family = family;
Miika Komu2718aa72006-11-30 16:41:50 -08001897 } else
1898 t->encap_family = xp->family;
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 /* No way to set this via kame pfkey */
Herbert Xuc5d18e92008-04-22 00:46:42 -07001901 t->allalgs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 xp->xfrm_nr++;
1903 return 0;
1904}
1905
1906static int
1907parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1908{
1909 int err;
1910 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1911 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1912
1913 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1914 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1915 return err;
1916 len -= rq->sadb_x_ipsecrequest_len;
1917 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1918 }
1919 return 0;
1920}
1921
Trent Jaegerdf718372005-12-13 23:12:27 -08001922static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1923{
1924 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1925
1926 if (xfrm_ctx) {
1927 int len = sizeof(struct sadb_x_sec_ctx);
1928 len += xfrm_ctx->ctx_len;
1929 return PFKEY_ALIGN8(len);
1930 }
1931 return 0;
1932}
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1935{
Miika Komu2718aa72006-11-30 16:41:50 -08001936 struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08001938 int socklen = 0;
1939 int i;
1940
1941 for (i=0; i<xp->xfrm_nr; i++) {
1942 t = xp->xfrm_vec + i;
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09001943 socklen += pfkey_sockaddr_len(t->encap_family);
Miika Komu2718aa72006-11-30 16:41:50 -08001944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 return sizeof(struct sadb_msg) +
1947 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001948 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 (sockaddr_size * 2) +
1950 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08001951 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1952 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08001953 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
1956static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1957{
1958 struct sk_buff *skb;
1959 int size;
1960
1961 size = pfkey_xfrm_policy2msg_size(xp);
1962
1963 skb = alloc_skb(size + 16, GFP_ATOMIC);
1964 if (skb == NULL)
1965 return ERR_PTR(-ENOBUFS);
1966
1967 return skb;
1968}
1969
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001970static int pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 struct sadb_msg *hdr;
1973 struct sadb_address *addr;
1974 struct sadb_lifetime *lifetime;
1975 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08001976 struct sadb_x_sec_ctx *sec_ctx;
1977 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 int i;
1979 int size;
1980 int sockaddr_size = pfkey_sockaddr_size(xp->family);
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09001981 int socklen = pfkey_sockaddr_len(xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 size = pfkey_xfrm_policy2msg_size(xp);
1984
1985 /* call should fill header later */
1986 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1987 memset(hdr, 0, size); /* XXX do we need this ? */
1988
1989 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001990 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001992 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 (sizeof(struct sadb_address)+sockaddr_size)/
1994 sizeof(uint64_t);
1995 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1996 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1997 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1998 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09001999 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2000 xp->selector.sport,
2001 (struct sockaddr *) (addr + 1),
2002 xp->family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 BUG();
2004
2005 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002006 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 sizeof(struct sadb_address)+sockaddr_size);
2008 addr->sadb_address_len =
2009 (sizeof(struct sadb_address)+sockaddr_size)/
2010 sizeof(uint64_t);
2011 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2012 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002013 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002015
2016 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2017 (struct sockaddr *) (addr + 1),
2018 xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 /* hard time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002021 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 sizeof(struct sadb_lifetime));
2023 lifetime->sadb_lifetime_len =
2024 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2025 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2026 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2027 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2028 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2029 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2030 /* soft time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002031 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 sizeof(struct sadb_lifetime));
2033 lifetime->sadb_lifetime_len =
2034 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2035 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2036 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2037 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2038 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2039 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2040 /* current time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002041 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 sizeof(struct sadb_lifetime));
2043 lifetime->sadb_lifetime_len =
2044 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2045 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2046 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2047 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2048 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2049 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2050
2051 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2052 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2053 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2054 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2055 if (xp->action == XFRM_POLICY_ALLOW) {
2056 if (xp->xfrm_nr)
2057 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2058 else
2059 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2060 }
2061 pol->sadb_x_policy_dir = dir+1;
2062 pol->sadb_x_policy_id = xp->index;
2063 pol->sadb_x_policy_priority = xp->priority;
2064
2065 for (i=0; i<xp->xfrm_nr; i++) {
2066 struct sadb_x_ipsecrequest *rq;
2067 struct xfrm_tmpl *t = xp->xfrm_vec + i;
2068 int req_size;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002069 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 req_size = sizeof(struct sadb_x_ipsecrequest);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002072 if (t->mode == XFRM_MODE_TUNNEL) {
2073 socklen = pfkey_sockaddr_len(t->encap_family);
2074 req_size += socklen * 2;
2075 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 size -= 2*socklen;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002077 socklen = 0;
2078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 rq = (void*)skb_put(skb, req_size);
2080 pol->sadb_x_policy_len += req_size/8;
2081 memset(rq, 0, sizeof(*rq));
2082 rq->sadb_x_ipsecrequest_len = req_size;
2083 rq->sadb_x_ipsecrequest_proto = t->id.proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002084 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2085 return -EINVAL;
David S. Millerfefaa752007-04-17 21:48:10 -07002086 rq->sadb_x_ipsecrequest_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2088 if (t->reqid)
2089 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2090 if (t->optional)
2091 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2092 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002094 if (t->mode == XFRM_MODE_TUNNEL) {
2095 u8 *sa = (void *)(rq + 1);
2096 pfkey_sockaddr_fill(&t->saddr, 0,
2097 (struct sockaddr *)sa,
2098 t->encap_family);
2099 pfkey_sockaddr_fill(&t->id.daddr, 0,
2100 (struct sockaddr *) (sa + socklen),
2101 t->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002104
2105 /* security context */
2106 if ((xfrm_ctx = xp->security)) {
2107 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2108
2109 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2110 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2111 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2112 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2113 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2114 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2115 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2116 xfrm_ctx->ctx_len);
2117 }
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 hdr->sadb_msg_len = size / sizeof(uint64_t);
2120 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002121
2122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002125static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2126{
2127 struct sk_buff *out_skb;
2128 struct sadb_msg *out_hdr;
2129 int err;
2130
2131 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2132 if (IS_ERR(out_skb)) {
2133 err = PTR_ERR(out_skb);
2134 goto out;
2135 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002136 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2137 if (err < 0)
2138 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002139
2140 out_hdr = (struct sadb_msg *) out_skb->data;
2141 out_hdr->sadb_msg_version = PF_KEY_V2;
2142
Herbert Xuf60f6b82005-06-18 22:44:37 -07002143 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002144 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2145 else
2146 out_hdr->sadb_msg_type = event2poltype(c->event);
2147 out_hdr->sadb_msg_errno = 0;
2148 out_hdr->sadb_msg_seq = c->seq;
2149 out_hdr->sadb_msg_pid = c->pid;
2150 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
2151out:
2152 return 0;
2153
2154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2157{
Trent Jaegerdf718372005-12-13 23:12:27 -08002158 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 struct sadb_lifetime *lifetime;
2160 struct sadb_address *sa;
2161 struct sadb_x_policy *pol;
2162 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002163 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002164 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2167 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2168 !ext_hdrs[SADB_X_EXT_POLICY-1])
2169 return -EINVAL;
2170
2171 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2172 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2173 return -EINVAL;
2174 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2175 return -EINVAL;
2176
2177 xp = xfrm_policy_alloc(GFP_KERNEL);
2178 if (xp == NULL)
2179 return -ENOBUFS;
2180
2181 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2182 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2183 xp->priority = pol->sadb_x_policy_priority;
2184
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002185 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2187 if (!xp->family) {
2188 err = -EINVAL;
2189 goto out;
2190 }
2191 xp->selector.family = xp->family;
2192 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2193 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2194 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2195 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002196 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002198 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2200 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2201
2202 /* Amusing, we set this twice. KAME apps appear to set same value
2203 * in both addresses.
2204 */
2205 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2206
2207 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2208 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002209 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Trent Jaegerdf718372005-12-13 23:12:27 -08002211 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2212 if (sec_ctx != NULL) {
2213 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2214
2215 if (!uctx) {
2216 err = -ENOBUFS;
2217 goto out;
2218 }
2219
Paul Moore03e1ad72008-04-12 19:07:52 -07002220 err = security_xfrm_policy_alloc(&xp->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002221 kfree(uctx);
2222
2223 if (err)
2224 goto out;
2225 }
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 xp->lft.soft_byte_limit = XFRM_INF;
2228 xp->lft.hard_byte_limit = XFRM_INF;
2229 xp->lft.soft_packet_limit = XFRM_INF;
2230 xp->lft.hard_packet_limit = XFRM_INF;
2231 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2232 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2233 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2234 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2235 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2236 }
2237 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2238 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2239 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2240 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2241 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2242 }
2243 xp->xfrm_nr = 0;
2244 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2245 (err = parse_ipsecrequests(xp, pol)) < 0)
2246 goto out;
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2249 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002250
Joy Lattenab5f5e82007-09-17 11:51:22 -07002251 xfrm_audit_policy_add(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002252 audit_get_loginuid(current),
2253 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06002254
Trent Jaegerdf718372005-12-13 23:12:27 -08002255 if (err)
2256 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002258 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002259 c.event = XFRM_MSG_UPDPOLICY;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002260 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07002261 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002263 c.seq = hdr->sadb_msg_seq;
2264 c.pid = hdr->sadb_msg_pid;
2265
2266 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return 0;
2269
2270out:
Herbert Xu12a169e2008-10-01 07:03:24 -07002271 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08002272 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return err;
2274}
2275
2276static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2277{
2278 int err;
2279 struct sadb_address *sa;
2280 struct sadb_x_policy *pol;
Paul Moore03e1ad72008-04-12 19:07:52 -07002281 struct xfrm_policy *xp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002283 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002284 struct sadb_x_sec_ctx *sec_ctx;
Brian Haley2db3e472008-04-24 20:38:31 -07002285 struct xfrm_sec_ctx *pol_ctx = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2288 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2289 !ext_hdrs[SADB_X_EXT_POLICY-1])
2290 return -EINVAL;
2291
2292 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2293 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2294 return -EINVAL;
2295
2296 memset(&sel, 0, sizeof(sel));
2297
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002298 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2300 sel.prefixlen_s = sa->sadb_address_prefixlen;
2301 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2302 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2303 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002304 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002306 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2308 sel.prefixlen_d = sa->sadb_address_prefixlen;
2309 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2310 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2311 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002312 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Trent Jaegerdf718372005-12-13 23:12:27 -08002314 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002315 if (sec_ctx != NULL) {
2316 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2317
2318 if (!uctx)
2319 return -ENOMEM;
2320
Paul Moore03e1ad72008-04-12 19:07:52 -07002321 err = security_xfrm_policy_alloc(&pol_ctx, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002322 kfree(uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002323 if (err)
2324 return err;
Brian Haley2db3e472008-04-24 20:38:31 -07002325 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002326
Paul Moore03e1ad72008-04-12 19:07:52 -07002327 xp = xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN,
2328 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2329 1, &err);
2330 security_xfrm_policy_free(pol_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (xp == NULL)
2332 return -ENOENT;
2333
Joy Lattenab5f5e82007-09-17 11:51:22 -07002334 xfrm_audit_policy_delete(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002335 audit_get_loginuid(current),
2336 audit_get_sessionid(current), 0);
David S. Miller13fcfbb2007-02-12 13:53:54 -08002337
2338 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002339 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08002340
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002341 c.seq = hdr->sadb_msg_seq;
2342 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002343 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002344 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2345
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002346out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002347 xfrm_pol_put(xp);
2348 return err;
2349}
2350
2351static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2352{
2353 int err;
2354 struct sk_buff *out_skb;
2355 struct sadb_msg *out_hdr;
2356 err = 0;
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2359 if (IS_ERR(out_skb)) {
2360 err = PTR_ERR(out_skb);
2361 goto out;
2362 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002363 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2364 if (err < 0)
2365 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 out_hdr = (struct sadb_msg *) out_skb->data;
2368 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002369 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 out_hdr->sadb_msg_satype = 0;
2371 out_hdr->sadb_msg_errno = 0;
2372 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2373 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002374 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 err = 0;
2376
2377out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return err;
2379}
2380
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002381#ifdef CONFIG_NET_KEY_MIGRATE
2382static int pfkey_sockaddr_pair_size(sa_family_t family)
2383{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002384 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002385}
2386
2387static int parse_sockaddr_pair(struct sadb_x_ipsecrequest *rq,
2388 xfrm_address_t *saddr, xfrm_address_t *daddr,
2389 u16 *family)
2390{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002391 u8 *sa = (u8 *) (rq + 1);
2392 int af, socklen;
2393
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002394 if (rq->sadb_x_ipsecrequest_len <
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002395 pfkey_sockaddr_pair_size(((struct sockaddr *)sa)->sa_family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002396 return -EINVAL;
2397
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002398 af = pfkey_sockaddr_extract((struct sockaddr *) sa,
2399 saddr);
2400 if (!af)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002401 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002402
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002403 socklen = pfkey_sockaddr_len(af);
2404 if (pfkey_sockaddr_extract((struct sockaddr *) (sa + socklen),
2405 daddr) != af)
2406 return -EINVAL;
2407
2408 *family = af;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002409 return 0;
2410}
2411
2412static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2413 struct xfrm_migrate *m)
2414{
2415 int err;
2416 struct sadb_x_ipsecrequest *rq2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002417 int mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002418
2419 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2420 len < rq1->sadb_x_ipsecrequest_len)
2421 return -EINVAL;
2422
2423 /* old endoints */
2424 err = parse_sockaddr_pair(rq1, &m->old_saddr, &m->old_daddr,
2425 &m->old_family);
2426 if (err)
2427 return err;
2428
2429 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2430 len -= rq1->sadb_x_ipsecrequest_len;
2431
2432 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2433 len < rq2->sadb_x_ipsecrequest_len)
2434 return -EINVAL;
2435
2436 /* new endpoints */
2437 err = parse_sockaddr_pair(rq2, &m->new_saddr, &m->new_daddr,
2438 &m->new_family);
2439 if (err)
2440 return err;
2441
2442 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2443 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2444 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2445 return -EINVAL;
2446
2447 m->proto = rq1->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002448 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2449 return -EINVAL;
2450 m->mode = mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002451 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2452
2453 return ((int)(rq1->sadb_x_ipsecrequest_len +
2454 rq2->sadb_x_ipsecrequest_len));
2455}
2456
2457static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2458 struct sadb_msg *hdr, void **ext_hdrs)
2459{
2460 int i, len, ret, err = -EINVAL;
2461 u8 dir;
2462 struct sadb_address *sa;
2463 struct sadb_x_policy *pol;
2464 struct sadb_x_ipsecrequest *rq;
2465 struct xfrm_selector sel;
2466 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2467
2468 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2469 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2470 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2471 err = -EINVAL;
2472 goto out;
2473 }
2474
2475 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2476 if (!pol) {
2477 err = -EINVAL;
2478 goto out;
2479 }
2480
2481 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2482 err = -EINVAL;
2483 goto out;
2484 }
2485
2486 dir = pol->sadb_x_policy_dir - 1;
2487 memset(&sel, 0, sizeof(sel));
2488
2489 /* set source address info of selector */
2490 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2491 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2492 sel.prefixlen_s = sa->sadb_address_prefixlen;
2493 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2494 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2495 if (sel.sport)
Al Viro582ee432007-07-26 17:33:39 +01002496 sel.sport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002497
2498 /* set destination address info of selector */
2499 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2500 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2501 sel.prefixlen_d = sa->sadb_address_prefixlen;
2502 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2503 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2504 if (sel.dport)
Al Viro582ee432007-07-26 17:33:39 +01002505 sel.dport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002506
2507 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2508
2509 /* extract ipsecrequests */
2510 i = 0;
2511 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2512
2513 while (len > 0 && i < XFRM_MAX_DEPTH) {
2514 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2515 if (ret < 0) {
2516 err = ret;
2517 goto out;
2518 } else {
2519 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2520 len -= ret;
2521 i++;
2522 }
2523 }
2524
2525 if (!i || len > 0) {
2526 err = -EINVAL;
2527 goto out;
2528 }
2529
2530 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i);
2531
2532 out:
2533 return err;
2534}
2535#else
2536static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2537 struct sadb_msg *hdr, void **ext_hdrs)
2538{
2539 return -ENOPROTOOPT;
2540}
2541#endif
2542
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2545{
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002546 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002547 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 struct sadb_x_policy *pol;
2549 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002550 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2553 return -EINVAL;
2554
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002555 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2556 if (dir >= XFRM_POLICY_MAX)
2557 return -EINVAL;
2558
Eric Paris215a2dd2007-03-07 16:01:45 -08002559 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002560 xp = xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN, dir, pol->sadb_x_policy_id,
Eric Paris215a2dd2007-03-07 16:01:45 -08002561 delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 if (xp == NULL)
2563 return -ENOENT;
2564
Eric Paris215a2dd2007-03-07 16:01:45 -08002565 if (delete) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002566 xfrm_audit_policy_delete(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002567 audit_get_loginuid(current),
2568 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Eric Paris215a2dd2007-03-07 16:01:45 -08002570 if (err)
2571 goto out;
2572 c.seq = hdr->sadb_msg_seq;
2573 c.pid = hdr->sadb_msg_pid;
Herbert Xubf088672005-06-18 22:44:00 -07002574 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002575 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002576 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002577 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002578 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Eric Paris215a2dd2007-03-07 16:01:45 -08002581out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 xfrm_pol_put(xp);
2583 return err;
2584}
2585
2586static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2587{
Timo Teras83321d62008-03-03 23:40:12 -08002588 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 struct sk_buff *out_skb;
2590 struct sadb_msg *out_hdr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002591 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Timo Teras83321d62008-03-03 23:40:12 -08002593 if (!pfkey_can_dump(&pfk->sk))
2594 return -ENOBUFS;
2595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2597 if (IS_ERR(out_skb))
2598 return PTR_ERR(out_skb);
2599
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002600 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2601 if (err < 0)
2602 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08002605 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2607 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2608 out_hdr->sadb_msg_errno = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07002609 out_hdr->sadb_msg_seq = count + 1;
Timo Teras83321d62008-03-03 23:40:12 -08002610 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
Herbert Xu12a169e2008-10-01 07:03:24 -07002611
2612 if (pfk->dump.skb)
2613 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
2614 &pfk->sk);
2615 pfk->dump.skb = out_skb;
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return 0;
2618}
2619
Timo Teras83321d62008-03-03 23:40:12 -08002620static int pfkey_dump_sp(struct pfkey_sock *pfk)
2621{
2622 return xfrm_policy_walk(&pfk->dump.u.policy, dump_sp, (void *) pfk);
2623}
2624
2625static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2626{
2627 xfrm_policy_walk_done(&pfk->dump.u.policy);
2628}
2629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2631{
Timo Teras83321d62008-03-03 23:40:12 -08002632 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Timo Teras83321d62008-03-03 23:40:12 -08002634 if (pfk->dump.dump != NULL)
2635 return -EBUSY;
Timo Teras4c563f72008-02-28 21:31:08 -08002636
Timo Teras83321d62008-03-03 23:40:12 -08002637 pfk->dump.msg_version = hdr->sadb_msg_version;
2638 pfk->dump.msg_pid = hdr->sadb_msg_pid;
2639 pfk->dump.dump = pfkey_dump_sp;
2640 pfk->dump.done = pfkey_dump_sp_done;
2641 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2642
2643 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002646static int key_notify_policy_flush(struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
2648 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002649 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002651 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 if (!skb_out)
2653 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002654 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002655 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002656 hdr->sadb_msg_seq = c->seq;
2657 hdr->sadb_msg_pid = c->pid;
2658 hdr->sadb_msg_version = PF_KEY_V2;
2659 hdr->sadb_msg_errno = (uint8_t) 0;
2660 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2661 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2662 return 0;
2663
2664}
2665
2666static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2667{
2668 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06002669 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04002670 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Al Viro0c11b942008-01-10 04:20:52 -05002672 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04002673 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06002674 audit_info.secid = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -04002675 err = xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN, &audit_info);
2676 if (err)
2677 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002678 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002679 c.event = XFRM_MSG_FLUSHPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002680 c.pid = hdr->sadb_msg_pid;
2681 c.seq = hdr->sadb_msg_seq;
2682 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 return 0;
2685}
2686
2687typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2688 struct sadb_msg *hdr, void **ext_hdrs);
2689static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2690 [SADB_RESERVED] = pfkey_reserved,
2691 [SADB_GETSPI] = pfkey_getspi,
2692 [SADB_UPDATE] = pfkey_add,
2693 [SADB_ADD] = pfkey_add,
2694 [SADB_DELETE] = pfkey_delete,
2695 [SADB_GET] = pfkey_get,
2696 [SADB_ACQUIRE] = pfkey_acquire,
2697 [SADB_REGISTER] = pfkey_register,
2698 [SADB_EXPIRE] = NULL,
2699 [SADB_FLUSH] = pfkey_flush,
2700 [SADB_DUMP] = pfkey_dump,
2701 [SADB_X_PROMISC] = pfkey_promisc,
2702 [SADB_X_PCHANGE] = NULL,
2703 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2704 [SADB_X_SPDADD] = pfkey_spdadd,
2705 [SADB_X_SPDDELETE] = pfkey_spddelete,
2706 [SADB_X_SPDGET] = pfkey_spdget,
2707 [SADB_X_SPDACQUIRE] = NULL,
2708 [SADB_X_SPDDUMP] = pfkey_spddump,
2709 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2710 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2711 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002712 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713};
2714
2715static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2716{
2717 void *ext_hdrs[SADB_EXT_MAX];
2718 int err;
2719
2720 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2721 BROADCAST_PROMISC_ONLY, NULL);
2722
2723 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2724 err = parse_exthdrs(skb, hdr, ext_hdrs);
2725 if (!err) {
2726 err = -EOPNOTSUPP;
2727 if (pfkey_funcs[hdr->sadb_msg_type])
2728 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2729 }
2730 return err;
2731}
2732
2733static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2734{
2735 struct sadb_msg *hdr = NULL;
2736
2737 if (skb->len < sizeof(*hdr)) {
2738 *errp = -EMSGSIZE;
2739 } else {
2740 hdr = (struct sadb_msg *) skb->data;
2741 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2742 hdr->sadb_msg_reserved != 0 ||
2743 (hdr->sadb_msg_type <= SADB_RESERVED ||
2744 hdr->sadb_msg_type > SADB_MAX)) {
2745 hdr = NULL;
2746 *errp = -EINVAL;
2747 } else if (hdr->sadb_msg_len != (skb->len /
2748 sizeof(uint64_t)) ||
2749 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2750 sizeof(uint64_t))) {
2751 hdr = NULL;
2752 *errp = -EMSGSIZE;
2753 } else {
2754 *errp = 0;
2755 }
2756 }
2757 return hdr;
2758}
2759
2760static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2761{
Herbert Xuf3980352007-12-19 23:44:29 -08002762 unsigned int id = d->desc.sadb_alg_id;
2763
2764 if (id >= sizeof(t->aalgos) * 8)
2765 return 0;
2766
2767 return (t->aalgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
2770static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2771{
Herbert Xuf3980352007-12-19 23:44:29 -08002772 unsigned int id = d->desc.sadb_alg_id;
2773
2774 if (id >= sizeof(t->ealgos) * 8)
2775 return 0;
2776
2777 return (t->ealgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779
2780static int count_ah_combs(struct xfrm_tmpl *t)
2781{
2782 int i, sz = 0;
2783
2784 for (i = 0; ; i++) {
2785 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2786 if (!aalg)
2787 break;
2788 if (aalg_tmpl_set(t, aalg) && aalg->available)
2789 sz += sizeof(struct sadb_comb);
2790 }
2791 return sz + sizeof(struct sadb_prop);
2792}
2793
2794static int count_esp_combs(struct xfrm_tmpl *t)
2795{
2796 int i, k, sz = 0;
2797
2798 for (i = 0; ; i++) {
2799 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2800 if (!ealg)
2801 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2804 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 for (k = 1; ; k++) {
2807 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2808 if (!aalg)
2809 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 if (aalg_tmpl_set(t, aalg) && aalg->available)
2812 sz += sizeof(struct sadb_comb);
2813 }
2814 }
2815 return sz + sizeof(struct sadb_prop);
2816}
2817
2818static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2819{
2820 struct sadb_prop *p;
2821 int i;
2822
2823 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2824 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2825 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2826 p->sadb_prop_replay = 32;
2827 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2828
2829 for (i = 0; ; i++) {
2830 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2831 if (!aalg)
2832 break;
2833
2834 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2835 struct sadb_comb *c;
2836 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2837 memset(c, 0, sizeof(*c));
2838 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2839 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2840 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2841 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2842 c->sadb_comb_hard_addtime = 24*60*60;
2843 c->sadb_comb_soft_addtime = 20*60*60;
2844 c->sadb_comb_hard_usetime = 8*60*60;
2845 c->sadb_comb_soft_usetime = 7*60*60;
2846 }
2847 }
2848}
2849
2850static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2851{
2852 struct sadb_prop *p;
2853 int i, k;
2854
2855 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2856 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2857 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2858 p->sadb_prop_replay = 32;
2859 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2860
2861 for (i=0; ; i++) {
2862 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2863 if (!ealg)
2864 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2867 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 for (k = 1; ; k++) {
2870 struct sadb_comb *c;
2871 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2872 if (!aalg)
2873 break;
2874 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2875 continue;
2876 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2877 memset(c, 0, sizeof(*c));
2878 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2879 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2880 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2881 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2882 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2883 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2884 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2885 c->sadb_comb_hard_addtime = 24*60*60;
2886 c->sadb_comb_soft_addtime = 20*60*60;
2887 c->sadb_comb_hard_usetime = 8*60*60;
2888 c->sadb_comb_soft_usetime = 7*60*60;
2889 }
2890 }
2891}
2892
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002893static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2894{
2895 return 0;
2896}
2897
2898static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
2900 struct sk_buff *out_skb;
2901 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002902 int hard;
2903 int hsc;
2904
Herbert Xubf088672005-06-18 22:44:00 -07002905 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002906 if (hard)
2907 hsc = 2;
2908 else
2909 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Herbert Xu050f0092007-10-09 13:31:47 -07002911 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (IS_ERR(out_skb))
2913 return PTR_ERR(out_skb);
2914
2915 out_hdr = (struct sadb_msg *) out_skb->data;
2916 out_hdr->sadb_msg_version = PF_KEY_V2;
2917 out_hdr->sadb_msg_type = SADB_EXPIRE;
2918 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2919 out_hdr->sadb_msg_errno = 0;
2920 out_hdr->sadb_msg_reserved = 0;
2921 out_hdr->sadb_msg_seq = 0;
2922 out_hdr->sadb_msg_pid = 0;
2923
2924 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2925 return 0;
2926}
2927
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002928static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2929{
Jamal Hadi Salim99c6f602008-06-10 14:25:34 -07002930 if (atomic_read(&pfkey_socks_nr) == 0)
2931 return 0;
2932
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002933 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002934 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002935 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002936 case XFRM_MSG_DELSA:
2937 case XFRM_MSG_NEWSA:
2938 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002939 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002940 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002941 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002942 case XFRM_MSG_NEWAE: /* not yet supported */
2943 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002944 default:
2945 printk("pfkey: Unknown SA event %d\n", c->event);
2946 break;
2947 }
2948
2949 return 0;
2950}
2951
2952static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2953{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002954 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
2955 return 0;
2956
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002957 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002958 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002959 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002960 case XFRM_MSG_DELPOLICY:
2961 case XFRM_MSG_NEWPOLICY:
2962 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002963 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002964 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002965 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
2966 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002967 return key_notify_policy_flush(c);
2968 default:
2969 printk("pfkey: Unknown policy event %d\n", c->event);
2970 break;
2971 }
2972
2973 return 0;
2974}
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976static u32 get_acqseq(void)
2977{
2978 u32 res;
2979 static u32 acqseq;
2980 static DEFINE_SPINLOCK(acqseq_lock);
2981
2982 spin_lock_bh(&acqseq_lock);
2983 res = (++acqseq ? : ++acqseq);
2984 spin_unlock_bh(&acqseq_lock);
2985 return res;
2986}
2987
2988static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2989{
2990 struct sk_buff *skb;
2991 struct sadb_msg *hdr;
2992 struct sadb_address *addr;
2993 struct sadb_x_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 int sockaddr_size;
2995 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07002996 struct sadb_x_sec_ctx *sec_ctx;
2997 struct xfrm_sec_ctx *xfrm_ctx;
2998 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3001 if (!sockaddr_size)
3002 return -EINVAL;
3003
3004 size = sizeof(struct sadb_msg) +
3005 (sizeof(struct sadb_address) * 2) +
3006 (sockaddr_size * 2) +
3007 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 if (x->id.proto == IPPROTO_AH)
3010 size += count_ah_combs(t);
3011 else if (x->id.proto == IPPROTO_ESP)
3012 size += count_esp_combs(t);
3013
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003014 if ((xfrm_ctx = x->security)) {
3015 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3016 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3017 }
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 skb = alloc_skb(size + 16, GFP_ATOMIC);
3020 if (skb == NULL)
3021 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3024 hdr->sadb_msg_version = PF_KEY_V2;
3025 hdr->sadb_msg_type = SADB_ACQUIRE;
3026 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3027 hdr->sadb_msg_len = size / sizeof(uint64_t);
3028 hdr->sadb_msg_errno = 0;
3029 hdr->sadb_msg_reserved = 0;
3030 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3031 hdr->sadb_msg_pid = 0;
3032
3033 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003034 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003036 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 (sizeof(struct sadb_address)+sockaddr_size)/
3038 sizeof(uint64_t);
3039 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3040 addr->sadb_address_proto = 0;
3041 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003042 addr->sadb_address_prefixlen =
3043 pfkey_sockaddr_fill(&x->props.saddr, 0,
3044 (struct sockaddr *) (addr + 1),
3045 x->props.family);
3046 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003048
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003050 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 sizeof(struct sadb_address)+sockaddr_size);
3052 addr->sadb_address_len =
3053 (sizeof(struct sadb_address)+sockaddr_size)/
3054 sizeof(uint64_t);
3055 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3056 addr->sadb_address_proto = 0;
3057 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003058 addr->sadb_address_prefixlen =
3059 pfkey_sockaddr_fill(&x->id.daddr, 0,
3060 (struct sockaddr *) (addr + 1),
3061 x->props.family);
3062 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 BUG();
3064
3065 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3066 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3067 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3068 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3069 pol->sadb_x_policy_dir = dir+1;
3070 pol->sadb_x_policy_id = xp->index;
3071
3072 /* Set sadb_comb's. */
3073 if (x->id.proto == IPPROTO_AH)
3074 dump_ah_combs(skb, t);
3075 else if (x->id.proto == IPPROTO_ESP)
3076 dump_esp_combs(skb, t);
3077
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003078 /* security context */
3079 if (xfrm_ctx) {
3080 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3081 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3082 sec_ctx->sadb_x_sec_len =
3083 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3084 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3085 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3086 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3087 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3088 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3089 xfrm_ctx->ctx_len);
3090 }
3091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3093}
3094
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003095static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003096 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
3098 struct xfrm_policy *xp;
3099 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003100 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003102 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 case AF_INET:
3104 if (opt != IP_IPSEC_POLICY) {
3105 *dir = -EOPNOTSUPP;
3106 return NULL;
3107 }
3108 break;
3109#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3110 case AF_INET6:
3111 if (opt != IPV6_IPSEC_POLICY) {
3112 *dir = -EOPNOTSUPP;
3113 return NULL;
3114 }
3115 break;
3116#endif
3117 default:
3118 *dir = -EINVAL;
3119 return NULL;
3120 }
3121
3122 *dir = -EINVAL;
3123
3124 if (len < sizeof(struct sadb_x_policy) ||
3125 pol->sadb_x_policy_len*8 > len ||
3126 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3127 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3128 return NULL;
3129
3130 xp = xfrm_policy_alloc(GFP_ATOMIC);
3131 if (xp == NULL) {
3132 *dir = -ENOBUFS;
3133 return NULL;
3134 }
3135
3136 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3137 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3138
3139 xp->lft.soft_byte_limit = XFRM_INF;
3140 xp->lft.hard_byte_limit = XFRM_INF;
3141 xp->lft.soft_packet_limit = XFRM_INF;
3142 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003143 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
3145 xp->xfrm_nr = 0;
3146 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3147 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3148 goto out;
3149
Trent Jaegerdf718372005-12-13 23:12:27 -08003150 /* security context too */
3151 if (len >= (pol->sadb_x_policy_len*8 +
3152 sizeof(struct sadb_x_sec_ctx))) {
3153 char *p = (char *)pol;
3154 struct xfrm_user_sec_ctx *uctx;
3155
3156 p += pol->sadb_x_policy_len*8;
3157 sec_ctx = (struct sadb_x_sec_ctx *)p;
3158 if (len < pol->sadb_x_policy_len*8 +
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003159 sec_ctx->sadb_x_sec_len) {
3160 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003161 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003162 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003163 if ((*dir = verify_sec_ctx_len(p)))
3164 goto out;
3165 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
Paul Moore03e1ad72008-04-12 19:07:52 -07003166 *dir = security_xfrm_policy_alloc(&xp->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08003167 kfree(uctx);
3168
3169 if (*dir)
3170 goto out;
3171 }
3172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 *dir = pol->sadb_x_policy_dir-1;
3174 return xp;
3175
3176out:
WANG Cong64c31b32008-01-07 22:34:29 -08003177 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 return NULL;
3179}
3180
Al Viro5d36b182006-11-08 00:24:06 -08003181static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
3183 struct sk_buff *skb;
3184 struct sadb_msg *hdr;
3185 struct sadb_sa *sa;
3186 struct sadb_address *addr;
3187 struct sadb_x_nat_t_port *n_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 int sockaddr_size;
3189 int size;
3190 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3191 struct xfrm_encap_tmpl *natt = NULL;
3192
3193 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3194 if (!sockaddr_size)
3195 return -EINVAL;
3196
3197 if (!satype)
3198 return -EINVAL;
3199
3200 if (!x->encap)
3201 return -EINVAL;
3202
3203 natt = x->encap;
3204
3205 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3206 *
3207 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3208 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3209 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 size = sizeof(struct sadb_msg) +
3212 sizeof(struct sadb_sa) +
3213 (sizeof(struct sadb_address) * 2) +
3214 (sockaddr_size * 2) +
3215 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 skb = alloc_skb(size + 16, GFP_ATOMIC);
3218 if (skb == NULL)
3219 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3222 hdr->sadb_msg_version = PF_KEY_V2;
3223 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3224 hdr->sadb_msg_satype = satype;
3225 hdr->sadb_msg_len = size / sizeof(uint64_t);
3226 hdr->sadb_msg_errno = 0;
3227 hdr->sadb_msg_reserved = 0;
3228 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3229 hdr->sadb_msg_pid = 0;
3230
3231 /* SA */
3232 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3233 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3234 sa->sadb_sa_exttype = SADB_EXT_SA;
3235 sa->sadb_sa_spi = x->id.spi;
3236 sa->sadb_sa_replay = 0;
3237 sa->sadb_sa_state = 0;
3238 sa->sadb_sa_auth = 0;
3239 sa->sadb_sa_encrypt = 0;
3240 sa->sadb_sa_flags = 0;
3241
3242 /* ADDRESS_SRC (old addr) */
3243 addr = (struct sadb_address*)
3244 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003245 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 (sizeof(struct sadb_address)+sockaddr_size)/
3247 sizeof(uint64_t);
3248 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3249 addr->sadb_address_proto = 0;
3250 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003251 addr->sadb_address_prefixlen =
3252 pfkey_sockaddr_fill(&x->props.saddr, 0,
3253 (struct sockaddr *) (addr + 1),
3254 x->props.family);
3255 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 BUG();
3257
3258 /* NAT_T_SPORT (old port) */
3259 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3260 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3261 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3262 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3263 n_port->sadb_x_nat_t_port_reserved = 0;
3264
3265 /* ADDRESS_DST (new addr) */
3266 addr = (struct sadb_address*)
3267 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003268 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 (sizeof(struct sadb_address)+sockaddr_size)/
3270 sizeof(uint64_t);
3271 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3272 addr->sadb_address_proto = 0;
3273 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003274 addr->sadb_address_prefixlen =
3275 pfkey_sockaddr_fill(ipaddr, 0,
3276 (struct sockaddr *) (addr + 1),
3277 x->props.family);
3278 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 BUG();
3280
3281 /* NAT_T_DPORT (new port) */
3282 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3283 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3284 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3285 n_port->sadb_x_nat_t_port_port = sport;
3286 n_port->sadb_x_nat_t_port_reserved = 0;
3287
3288 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3289}
3290
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003291#ifdef CONFIG_NET_KEY_MIGRATE
3292static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3293 struct xfrm_selector *sel)
3294{
3295 struct sadb_address *addr;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003296 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3297 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3298 addr->sadb_address_exttype = type;
3299 addr->sadb_address_proto = sel->proto;
3300 addr->sadb_address_reserved = 0;
3301
3302 switch (type) {
3303 case SADB_EXT_ADDRESS_SRC:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003304 addr->sadb_address_prefixlen = sel->prefixlen_s;
3305 pfkey_sockaddr_fill(&sel->saddr, 0,
3306 (struct sockaddr *)(addr + 1),
3307 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003308 break;
3309 case SADB_EXT_ADDRESS_DST:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003310 addr->sadb_address_prefixlen = sel->prefixlen_d;
3311 pfkey_sockaddr_fill(&sel->daddr, 0,
3312 (struct sockaddr *)(addr + 1),
3313 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003314 break;
3315 default:
3316 return -EINVAL;
3317 }
3318
3319 return 0;
3320}
3321
3322static int set_ipsecrequest(struct sk_buff *skb,
3323 uint8_t proto, uint8_t mode, int level,
3324 uint32_t reqid, uint8_t family,
3325 xfrm_address_t *src, xfrm_address_t *dst)
3326{
3327 struct sadb_x_ipsecrequest *rq;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003328 u8 *sa;
3329 int socklen = pfkey_sockaddr_len(family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003330 int size_req;
3331
3332 size_req = sizeof(struct sadb_x_ipsecrequest) +
3333 pfkey_sockaddr_pair_size(family);
3334
3335 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3336 memset(rq, 0, size_req);
3337 rq->sadb_x_ipsecrequest_len = size_req;
3338 rq->sadb_x_ipsecrequest_proto = proto;
3339 rq->sadb_x_ipsecrequest_mode = mode;
3340 rq->sadb_x_ipsecrequest_level = level;
3341 rq->sadb_x_ipsecrequest_reqid = reqid;
3342
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003343 sa = (u8 *) (rq + 1);
3344 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3345 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003346 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003347
3348 return 0;
3349}
3350#endif
3351
3352#ifdef CONFIG_NET_KEY_MIGRATE
3353static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3354 struct xfrm_migrate *m, int num_bundles)
3355{
3356 int i;
3357 int sasize_sel;
3358 int size = 0;
3359 int size_pol = 0;
3360 struct sk_buff *skb;
3361 struct sadb_msg *hdr;
3362 struct sadb_x_policy *pol;
3363 struct xfrm_migrate *mp;
3364
3365 if (type != XFRM_POLICY_TYPE_MAIN)
3366 return 0;
3367
3368 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3369 return -EINVAL;
3370
3371 /* selector */
3372 sasize_sel = pfkey_sockaddr_size(sel->family);
3373 if (!sasize_sel)
3374 return -EINVAL;
3375 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3376
3377 /* policy info */
3378 size_pol += sizeof(struct sadb_x_policy);
3379
3380 /* ipsecrequests */
3381 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3382 /* old locator pair */
3383 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3384 pfkey_sockaddr_pair_size(mp->old_family);
3385 /* new locator pair */
3386 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3387 pfkey_sockaddr_pair_size(mp->new_family);
3388 }
3389
3390 size += sizeof(struct sadb_msg) + size_pol;
3391
3392 /* alloc buffer */
3393 skb = alloc_skb(size, GFP_ATOMIC);
3394 if (skb == NULL)
3395 return -ENOMEM;
3396
3397 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3398 hdr->sadb_msg_version = PF_KEY_V2;
3399 hdr->sadb_msg_type = SADB_X_MIGRATE;
3400 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3401 hdr->sadb_msg_len = size / 8;
3402 hdr->sadb_msg_errno = 0;
3403 hdr->sadb_msg_reserved = 0;
3404 hdr->sadb_msg_seq = 0;
3405 hdr->sadb_msg_pid = 0;
3406
3407 /* selector src */
3408 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3409
3410 /* selector dst */
3411 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3412
3413 /* policy information */
3414 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3415 pol->sadb_x_policy_len = size_pol / 8;
3416 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3417 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3418 pol->sadb_x_policy_dir = dir + 1;
3419 pol->sadb_x_policy_id = 0;
3420 pol->sadb_x_policy_priority = 0;
3421
3422 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3423 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003424 int mode = pfkey_mode_from_xfrm(mp->mode);
3425 if (mode < 0)
Patrick McHardyd4782c32008-01-20 17:24:29 -08003426 goto err;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003427 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003428 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3429 mp->reqid, mp->old_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003430 &mp->old_saddr, &mp->old_daddr) < 0)
3431 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003432
3433 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003434 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003435 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3436 mp->reqid, mp->new_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003437 &mp->new_saddr, &mp->new_daddr) < 0)
3438 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003439 }
3440
3441 /* broadcast migrate message to sockets */
3442 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
3443
3444 return 0;
Patrick McHardyd4782c32008-01-20 17:24:29 -08003445
3446err:
3447 kfree_skb(skb);
3448 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003449}
3450#else
3451static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3452 struct xfrm_migrate *m, int num_bundles)
3453{
3454 return -ENOPROTOOPT;
3455}
3456#endif
3457
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458static int pfkey_sendmsg(struct kiocb *kiocb,
3459 struct socket *sock, struct msghdr *msg, size_t len)
3460{
3461 struct sock *sk = sock->sk;
3462 struct sk_buff *skb = NULL;
3463 struct sadb_msg *hdr = NULL;
3464 int err;
3465
3466 err = -EOPNOTSUPP;
3467 if (msg->msg_flags & MSG_OOB)
3468 goto out;
3469
3470 err = -EMSGSIZE;
3471 if ((unsigned)len > sk->sk_sndbuf - 32)
3472 goto out;
3473
3474 err = -ENOBUFS;
3475 skb = alloc_skb(len, GFP_KERNEL);
3476 if (skb == NULL)
3477 goto out;
3478
3479 err = -EFAULT;
3480 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3481 goto out;
3482
3483 hdr = pfkey_get_base_msg(skb, &err);
3484 if (!hdr)
3485 goto out;
3486
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003487 mutex_lock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 err = pfkey_process(sk, skb, hdr);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003489 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
3491out:
3492 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3493 err = 0;
3494 if (skb)
3495 kfree_skb(skb);
3496
3497 return err ? : len;
3498}
3499
3500static int pfkey_recvmsg(struct kiocb *kiocb,
3501 struct socket *sock, struct msghdr *msg, size_t len,
3502 int flags)
3503{
3504 struct sock *sk = sock->sk;
Timo Teras83321d62008-03-03 23:40:12 -08003505 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 struct sk_buff *skb;
3507 int copied, err;
3508
3509 err = -EINVAL;
3510 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3511 goto out;
3512
3513 msg->msg_namelen = 0;
3514 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3515 if (skb == NULL)
3516 goto out;
3517
3518 copied = skb->len;
3519 if (copied > len) {
3520 msg->msg_flags |= MSG_TRUNC;
3521 copied = len;
3522 }
3523
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003524 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3526 if (err)
3527 goto out_free;
3528
3529 sock_recv_timestamp(msg, sk, skb);
3530
3531 err = (flags & MSG_TRUNC) ? skb->len : copied;
3532
Timo Teras83321d62008-03-03 23:40:12 -08003533 if (pfk->dump.dump != NULL &&
3534 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3535 pfkey_do_dump(pfk);
3536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537out_free:
3538 skb_free_datagram(sk, skb);
3539out:
3540 return err;
3541}
3542
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003543static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 .family = PF_KEY,
3545 .owner = THIS_MODULE,
3546 /* Operations that make no sense on pfkey sockets. */
3547 .bind = sock_no_bind,
3548 .connect = sock_no_connect,
3549 .socketpair = sock_no_socketpair,
3550 .accept = sock_no_accept,
3551 .getname = sock_no_getname,
3552 .ioctl = sock_no_ioctl,
3553 .listen = sock_no_listen,
3554 .shutdown = sock_no_shutdown,
3555 .setsockopt = sock_no_setsockopt,
3556 .getsockopt = sock_no_getsockopt,
3557 .mmap = sock_no_mmap,
3558 .sendpage = sock_no_sendpage,
3559
3560 /* Now the operations that really occur. */
3561 .release = pfkey_release,
3562 .poll = datagram_poll,
3563 .sendmsg = pfkey_sendmsg,
3564 .recvmsg = pfkey_recvmsg,
3565};
3566
3567static struct net_proto_family pfkey_family_ops = {
3568 .family = PF_KEY,
3569 .create = pfkey_create,
3570 .owner = THIS_MODULE,
3571};
3572
3573#ifdef CONFIG_PROC_FS
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003574static int pfkey_seq_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003578 s = (struct sock *)v;
3579 if (v == SEQ_START_TOKEN)
3580 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3581 else
3582 seq_printf(f ,"%p %-6d %-6u %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 s,
3584 atomic_read(&s->sk_refcnt),
3585 atomic_read(&s->sk_rmem_alloc),
3586 atomic_read(&s->sk_wmem_alloc),
3587 sock_i_uid(s),
3588 sock_i_ino(s)
3589 );
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003592
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003593static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3594{
3595 struct sock *s;
3596 struct hlist_node *node;
3597 loff_t pos = *ppos;
3598
3599 read_lock(&pfkey_table_lock);
3600 if (pos == 0)
3601 return SEQ_START_TOKEN;
3602
3603 sk_for_each(s, node, &pfkey_table)
3604 if (pos-- == 1)
3605 return s;
3606
3607 return NULL;
3608}
3609
3610static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3611{
3612 ++*ppos;
3613 return (v == SEQ_START_TOKEN) ?
3614 sk_head(&pfkey_table) :
3615 sk_next((struct sock *)v);
3616}
3617
3618static void pfkey_seq_stop(struct seq_file *f, void *v)
3619{
3620 read_unlock(&pfkey_table_lock);
3621}
3622
3623static struct seq_operations pfkey_seq_ops = {
3624 .start = pfkey_seq_start,
3625 .next = pfkey_seq_next,
3626 .stop = pfkey_seq_stop,
3627 .show = pfkey_seq_show,
3628};
3629
3630static int pfkey_seq_open(struct inode *inode, struct file *file)
3631{
3632 return seq_open(file, &pfkey_seq_ops);
3633}
3634
3635static struct file_operations pfkey_proc_ops = {
3636 .open = pfkey_seq_open,
3637 .read = seq_read,
3638 .llseek = seq_lseek,
3639 .release = seq_release,
3640};
3641
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003642static int pfkey_init_proc(void)
3643{
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003644 struct proc_dir_entry *e;
3645
David S. Millerd9595a72008-02-26 22:20:44 -08003646 e = proc_net_fops_create(&init_net, "pfkey", 0, &pfkey_proc_ops);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003647 if (e == NULL)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003648 return -ENOMEM;
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003649
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003650 return 0;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003651}
3652
3653static void pfkey_exit_proc(void)
3654{
David S. Millerd9595a72008-02-26 22:20:44 -08003655 proc_net_remove(&init_net, "pfkey");
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003656}
3657#else
3658static inline int pfkey_init_proc(void)
3659{
3660 return 0;
3661}
3662
3663static inline void pfkey_exit_proc(void)
3664{
3665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666#endif
3667
3668static struct xfrm_mgr pfkeyv2_mgr =
3669{
3670 .id = "pfkeyv2",
3671 .notify = pfkey_send_notify,
3672 .acquire = pfkey_send_acquire,
3673 .compile_policy = pfkey_compile_policy,
3674 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003675 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003676 .migrate = pfkey_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677};
3678
3679static void __exit ipsec_pfkey_exit(void)
3680{
3681 xfrm_unregister_km(&pfkeyv2_mgr);
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003682 pfkey_exit_proc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 sock_unregister(PF_KEY);
3684 proto_unregister(&key_proto);
3685}
3686
3687static int __init ipsec_pfkey_init(void)
3688{
3689 int err = proto_register(&key_proto, 0);
3690
3691 if (err != 0)
3692 goto out;
3693
3694 err = sock_register(&pfkey_family_ops);
3695 if (err != 0)
3696 goto out_unregister_key_proto;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003697 err = pfkey_init_proc();
3698 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 goto out_sock_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 err = xfrm_register_km(&pfkeyv2_mgr);
3701 if (err != 0)
3702 goto out_remove_proc_entry;
3703out:
3704 return err;
3705out_remove_proc_entry:
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003706 pfkey_exit_proc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707out_sock_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 sock_unregister(PF_KEY);
3709out_unregister_key_proto:
3710 proto_unregister(&key_proto);
3711 goto out;
3712}
3713
3714module_init(ipsec_pfkey_init);
3715module_exit(ipsec_pfkey_exit);
3716MODULE_LICENSE("GPL");
3717MODULE_ALIAS_NETPROTO(PF_KEY);