blob: ca268116ac1109573de81110d4c1fb9f568f6568 [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),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -0700401 [SADB_X_EXT_KMADDRESS] = (u8) sizeof(struct sadb_x_kmaddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402};
403
404/* Verify sadb_address_{len,prefixlen} against sa_family. */
405static int verify_address_len(void *p)
406{
407 struct sadb_address *sp = p;
408 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
409 struct sockaddr_in *sin;
410#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
411 struct sockaddr_in6 *sin6;
412#endif
413 int len;
414
415 switch (addr->sa_family) {
416 case AF_INET:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700417 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (sp->sadb_address_len != len ||
419 sp->sadb_address_prefixlen > 32)
420 return -EINVAL;
421 break;
422#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
423 case AF_INET6:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700424 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (sp->sadb_address_len != len ||
426 sp->sadb_address_prefixlen > 128)
427 return -EINVAL;
428 break;
429#endif
430 default:
431 /* It is user using kernel to keep track of security
432 * associations for another protocol, such as
433 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
434 * lengths.
435 *
436 * XXX Actually, association/policy database is not yet
437 * XXX able to cope with arbitrary sockaddr families.
438 * XXX When it can, remove this -EINVAL. -DaveM
439 */
440 return -EINVAL;
441 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 return 0;
445}
446
Trent Jaegerdf718372005-12-13 23:12:27 -0800447static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx)
448{
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700449 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
450 sec_ctx->sadb_x_ctx_len,
451 sizeof(uint64_t));
Trent Jaegerdf718372005-12-13 23:12:27 -0800452}
453
454static inline int verify_sec_ctx_len(void *p)
455{
456 struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p;
Stephen Rothwell298bb622007-10-30 23:57:05 -0700457 int len = sec_ctx->sadb_x_ctx_len;
Trent Jaegerdf718372005-12-13 23:12:27 -0800458
Stephen Rothwell298bb622007-10-30 23:57:05 -0700459 if (len > PAGE_SIZE)
Trent Jaegerdf718372005-12-13 23:12:27 -0800460 return -EINVAL;
461
462 len = pfkey_sec_ctx_len(sec_ctx);
463
464 if (sec_ctx->sadb_x_sec_len != len)
465 return -EINVAL;
466
467 return 0;
468}
469
470static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx)
471{
472 struct xfrm_user_sec_ctx *uctx = NULL;
473 int ctx_size = sec_ctx->sadb_x_ctx_len;
474
475 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
476
477 if (!uctx)
478 return NULL;
479
480 uctx->len = pfkey_sec_ctx_len(sec_ctx);
481 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
482 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
483 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
484 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
485 memcpy(uctx + 1, sec_ctx + 1,
486 uctx->ctx_len);
487
488 return uctx;
489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491static int present_and_same_family(struct sadb_address *src,
492 struct sadb_address *dst)
493{
494 struct sockaddr *s_addr, *d_addr;
495
496 if (!src || !dst)
497 return 0;
498
499 s_addr = (struct sockaddr *)(src + 1);
500 d_addr = (struct sockaddr *)(dst + 1);
501 if (s_addr->sa_family != d_addr->sa_family)
502 return 0;
503 if (s_addr->sa_family != AF_INET
504#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
505 && s_addr->sa_family != AF_INET6
506#endif
507 )
508 return 0;
509
510 return 1;
511}
512
513static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
514{
515 char *p = (char *) hdr;
516 int len = skb->len;
517
518 len -= sizeof(*hdr);
519 p += sizeof(*hdr);
520 while (len > 0) {
521 struct sadb_ext *ehdr = (struct sadb_ext *) p;
522 uint16_t ext_type;
523 int ext_len;
524
525 ext_len = ehdr->sadb_ext_len;
526 ext_len *= sizeof(uint64_t);
527 ext_type = ehdr->sadb_ext_type;
528 if (ext_len < sizeof(uint64_t) ||
529 ext_len > len ||
530 ext_type == SADB_EXT_RESERVED)
531 return -EINVAL;
532
533 if (ext_type <= SADB_EXT_MAX) {
534 int min = (int) sadb_ext_min_len[ext_type];
535 if (ext_len < min)
536 return -EINVAL;
537 if (ext_hdrs[ext_type-1] != NULL)
538 return -EINVAL;
539 if (ext_type == SADB_EXT_ADDRESS_SRC ||
540 ext_type == SADB_EXT_ADDRESS_DST ||
541 ext_type == SADB_EXT_ADDRESS_PROXY ||
542 ext_type == SADB_X_EXT_NAT_T_OA) {
543 if (verify_address_len(p))
544 return -EINVAL;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900545 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800546 if (ext_type == SADB_X_EXT_SEC_CTX) {
547 if (verify_sec_ctx_len(p))
548 return -EINVAL;
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 ext_hdrs[ext_type-1] = p;
551 }
552 p += ext_len;
553 len -= ext_len;
554 }
555
556 return 0;
557}
558
559static uint16_t
560pfkey_satype2proto(uint8_t satype)
561{
562 switch (satype) {
563 case SADB_SATYPE_UNSPEC:
564 return IPSEC_PROTO_ANY;
565 case SADB_SATYPE_AH:
566 return IPPROTO_AH;
567 case SADB_SATYPE_ESP:
568 return IPPROTO_ESP;
569 case SADB_X_SATYPE_IPCOMP:
570 return IPPROTO_COMP;
571 break;
572 default:
573 return 0;
574 }
575 /* NOTREACHED */
576}
577
578static uint8_t
579pfkey_proto2satype(uint16_t proto)
580{
581 switch (proto) {
582 case IPPROTO_AH:
583 return SADB_SATYPE_AH;
584 case IPPROTO_ESP:
585 return SADB_SATYPE_ESP;
586 case IPPROTO_COMP:
587 return SADB_X_SATYPE_IPCOMP;
588 break;
589 default:
590 return 0;
591 }
592 /* NOTREACHED */
593}
594
595/* BTW, this scheme means that there is no way with PFKEY2 sockets to
596 * say specifically 'just raw sockets' as we encode them as 255.
597 */
598
599static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
600{
601 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
602}
603
604static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
605{
606 return (proto ? proto : IPSEC_PROTO_ANY);
607}
608
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900609static inline int pfkey_sockaddr_len(sa_family_t family)
610{
611 switch (family) {
612 case AF_INET:
613 return sizeof(struct sockaddr_in);
614#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
615 case AF_INET6:
616 return sizeof(struct sockaddr_in6);
617#endif
618 }
619 return 0;
620}
621
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900622static
623int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900625 switch (sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 case AF_INET:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900627 xaddr->a4 =
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900628 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return AF_INET;
630#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
631 case AF_INET6:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900632 memcpy(xaddr->a6,
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900633 &((struct sockaddr_in6 *)sa)->sin6_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 sizeof(struct in6_addr));
635 return AF_INET6;
636#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900638 return 0;
639}
640
641static
642int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr, xfrm_address_t *xaddr)
643{
644 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
645 xaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
649{
650 struct sadb_sa *sa;
651 struct sadb_address *addr;
652 uint16_t proto;
653 unsigned short family;
654 xfrm_address_t *xaddr;
655
656 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
657 if (sa == NULL)
658 return NULL;
659
660 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
661 if (proto == 0)
662 return NULL;
663
664 /* sadb_address_len should be checked by caller */
665 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
666 if (addr == NULL)
667 return NULL;
668
669 family = ((struct sockaddr *)(addr + 1))->sa_family;
670 switch (family) {
671 case AF_INET:
672 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
673 break;
674#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
675 case AF_INET6:
676 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
677 break;
678#endif
679 default:
680 xaddr = NULL;
681 }
682
683 if (!xaddr)
684 return NULL;
685
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800686 return xfrm_state_lookup(&init_net, xaddr, sa->sadb_sa_spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691static int
692pfkey_sockaddr_size(sa_family_t family)
693{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900694 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700697static inline int pfkey_mode_from_xfrm(int mode)
698{
699 switch(mode) {
700 case XFRM_MODE_TRANSPORT:
701 return IPSEC_MODE_TRANSPORT;
702 case XFRM_MODE_TUNNEL:
703 return IPSEC_MODE_TUNNEL;
704 case XFRM_MODE_BEET:
705 return IPSEC_MODE_BEET;
706 default:
707 return -1;
708 }
709}
710
711static inline int pfkey_mode_to_xfrm(int mode)
712{
713 switch(mode) {
714 case IPSEC_MODE_ANY: /*XXX*/
715 case IPSEC_MODE_TRANSPORT:
716 return XFRM_MODE_TRANSPORT;
717 case IPSEC_MODE_TUNNEL:
718 return XFRM_MODE_TUNNEL;
719 case IPSEC_MODE_BEET:
720 return XFRM_MODE_BEET;
721 default:
722 return -1;
723 }
724}
725
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900726static unsigned int pfkey_sockaddr_fill(xfrm_address_t *xaddr, __be16 port,
727 struct sockaddr *sa,
728 unsigned short family)
729{
730 switch (family) {
731 case AF_INET:
732 {
733 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
734 sin->sin_family = AF_INET;
735 sin->sin_port = port;
736 sin->sin_addr.s_addr = xaddr->a4;
737 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
738 return 32;
739 }
740#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
741 case AF_INET6:
742 {
743 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
744 sin6->sin6_family = AF_INET6;
745 sin6->sin6_port = port;
746 sin6->sin6_flowinfo = 0;
747 ipv6_addr_copy(&sin6->sin6_addr, (struct in6_addr *)xaddr->a6);
748 sin6->sin6_scope_id = 0;
749 return 128;
750 }
751#endif
752 }
753 return 0;
754}
755
Herbert Xu050f0092007-10-09 13:31:47 -0700756static struct sk_buff *__pfkey_xfrm_state2msg(struct xfrm_state *x,
757 int add_keys, int hsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct sk_buff *skb;
760 struct sadb_msg *hdr;
761 struct sadb_sa *sa;
762 struct sadb_lifetime *lifetime;
763 struct sadb_address *addr;
764 struct sadb_key *key;
765 struct sadb_x_sa2 *sa2;
Trent Jaegerdf718372005-12-13 23:12:27 -0800766 struct sadb_x_sec_ctx *sec_ctx;
767 struct xfrm_sec_ctx *xfrm_ctx;
768 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int size;
770 int auth_key_size = 0;
771 int encrypt_key_size = 0;
772 int sockaddr_size;
773 struct xfrm_encap_tmpl *natt = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700774 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 /* address family check */
777 sockaddr_size = pfkey_sockaddr_size(x->props.family);
778 if (!sockaddr_size)
779 return ERR_PTR(-EINVAL);
780
781 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
782 key(AE), (identity(SD),) (sensitivity)> */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900783 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 sizeof(struct sadb_lifetime) +
785 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
786 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900787 sizeof(struct sadb_address)*2 +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 sockaddr_size*2 +
789 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800790
791 if ((xfrm_ctx = x->security)) {
792 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
793 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
794 }
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 /* identity & sensitivity */
YOSHIFUJI Hideaki81b302a2008-04-28 03:17:38 +0900797 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr, x->props.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 size += sizeof(struct sadb_address) + sockaddr_size;
799
800 if (add_keys) {
801 if (x->aalg && x->aalg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900802 auth_key_size =
803 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 size += sizeof(struct sadb_key) + auth_key_size;
805 }
806 if (x->ealg && x->ealg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900807 encrypt_key_size =
808 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 size += sizeof(struct sadb_key) + encrypt_key_size;
810 }
811 }
812 if (x->encap)
813 natt = x->encap;
814
815 if (natt && natt->encap_type) {
816 size += sizeof(struct sadb_x_nat_t_type);
817 size += sizeof(struct sadb_x_nat_t_port);
818 size += sizeof(struct sadb_x_nat_t_port);
819 }
820
821 skb = alloc_skb(size + 16, GFP_ATOMIC);
822 if (skb == NULL)
823 return ERR_PTR(-ENOBUFS);
824
825 /* call should fill header later */
826 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
827 memset(hdr, 0, size); /* XXX do we need this ? */
828 hdr->sadb_msg_len = size / sizeof(uint64_t);
829
830 /* sa */
831 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
832 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
833 sa->sadb_sa_exttype = SADB_EXT_SA;
834 sa->sadb_sa_spi = x->id.spi;
835 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700836 switch (x->km.state) {
837 case XFRM_STATE_VALID:
838 sa->sadb_sa_state = x->km.dying ?
839 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
840 break;
841 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700843 break;
844 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700846 break;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 sa->sadb_sa_auth = 0;
849 if (x->aalg) {
850 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
851 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
852 }
853 sa->sadb_sa_encrypt = 0;
854 BUG_ON(x->ealg && x->calg);
855 if (x->ealg) {
856 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
857 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
858 }
859 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
860 if (x->calg) {
861 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
862 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
863 }
864
865 sa->sadb_sa_flags = 0;
866 if (x->props.flags & XFRM_STATE_NOECN)
867 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
868 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
869 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700870 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
871 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 /* hard time */
874 if (hsc & 2) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900875 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 sizeof(struct sadb_lifetime));
877 lifetime->sadb_lifetime_len =
878 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
879 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
880 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
881 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
882 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
883 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
884 }
885 /* soft time */
886 if (hsc & 1) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900887 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 sizeof(struct sadb_lifetime));
889 lifetime->sadb_lifetime_len =
890 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
891 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
892 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
893 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
894 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
895 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
896 }
897 /* current time */
898 lifetime = (struct sadb_lifetime *) skb_put(skb,
899 sizeof(struct sadb_lifetime));
900 lifetime->sadb_lifetime_len =
901 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
902 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
903 lifetime->sadb_lifetime_allocations = x->curlft.packets;
904 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
905 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
906 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
907 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900908 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900910 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 (sizeof(struct sadb_address)+sockaddr_size)/
912 sizeof(uint64_t);
913 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900914 /* "if the ports are non-zero, then the sadb_address_proto field,
915 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900917 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900920 addr->sadb_address_prefixlen =
921 pfkey_sockaddr_fill(&x->props.saddr, 0,
922 (struct sockaddr *) (addr + 1),
923 x->props.family);
924 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 BUG();
926
927 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900928 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900930 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 (sizeof(struct sadb_address)+sockaddr_size)/
932 sizeof(uint64_t);
933 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900934 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900937 addr->sadb_address_prefixlen =
938 pfkey_sockaddr_fill(&x->id.daddr, 0,
939 (struct sockaddr *) (addr + 1),
940 x->props.family);
941 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 BUG();
943
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900944 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr,
945 x->props.family)) {
946 addr = (struct sadb_address*) skb_put(skb,
947 sizeof(struct sadb_address)+sockaddr_size);
948 addr->sadb_address_len =
949 (sizeof(struct sadb_address)+sockaddr_size)/
950 sizeof(uint64_t);
951 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
952 addr->sadb_address_proto =
953 pfkey_proto_from_xfrm(x->sel.proto);
954 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
955 addr->sadb_address_reserved = 0;
956
957 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
958 (struct sockaddr *) (addr + 1),
959 x->props.family);
960 }
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /* auth key */
963 if (add_keys && auth_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900964 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 sizeof(struct sadb_key)+auth_key_size);
966 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
967 sizeof(uint64_t);
968 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
969 key->sadb_key_bits = x->aalg->alg_key_len;
970 key->sadb_key_reserved = 0;
971 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
972 }
973 /* encrypt key */
974 if (add_keys && encrypt_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900975 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 sizeof(struct sadb_key)+encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900977 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 encrypt_key_size) / sizeof(uint64_t);
979 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
980 key->sadb_key_bits = x->ealg->alg_key_len;
981 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900982 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 (x->ealg->alg_key_len+7)/8);
984 }
985
986 /* sa */
987 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
988 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
989 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700990 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
991 kfree_skb(skb);
992 return ERR_PTR(-EINVAL);
993 }
994 sa2->sadb_x_sa2_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 sa2->sadb_x_sa2_reserved1 = 0;
996 sa2->sadb_x_sa2_reserved2 = 0;
997 sa2->sadb_x_sa2_sequence = 0;
998 sa2->sadb_x_sa2_reqid = x->props.reqid;
999
1000 if (natt && natt->encap_type) {
1001 struct sadb_x_nat_t_type *n_type;
1002 struct sadb_x_nat_t_port *n_port;
1003
1004 /* type */
1005 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
1006 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
1007 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
1008 n_type->sadb_x_nat_t_type_type = natt->encap_type;
1009 n_type->sadb_x_nat_t_type_reserved[0] = 0;
1010 n_type->sadb_x_nat_t_type_reserved[1] = 0;
1011 n_type->sadb_x_nat_t_type_reserved[2] = 0;
1012
1013 /* source port */
1014 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
1015 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1016 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
1017 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
1018 n_port->sadb_x_nat_t_port_reserved = 0;
1019
1020 /* dest port */
1021 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
1022 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1023 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
1024 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
1025 n_port->sadb_x_nat_t_port_reserved = 0;
1026 }
1027
Trent Jaegerdf718372005-12-13 23:12:27 -08001028 /* security context */
1029 if (xfrm_ctx) {
1030 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1031 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1032 sec_ctx->sadb_x_sec_len =
1033 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1034 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1035 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1036 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1037 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1038 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1039 xfrm_ctx->ctx_len);
1040 }
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return skb;
1043}
1044
Herbert Xu050f0092007-10-09 13:31:47 -07001045
1046static inline struct sk_buff *pfkey_xfrm_state2msg(struct xfrm_state *x)
1047{
1048 struct sk_buff *skb;
1049
Herbert Xu050f0092007-10-09 13:31:47 -07001050 skb = __pfkey_xfrm_state2msg(x, 1, 3);
Herbert Xu050f0092007-10-09 13:31:47 -07001051
1052 return skb;
1053}
1054
1055static inline struct sk_buff *pfkey_xfrm_state2msg_expire(struct xfrm_state *x,
1056 int hsc)
1057{
1058 return __pfkey_xfrm_state2msg(x, 0, hsc);
1059}
1060
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001061static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 void **ext_hdrs)
1063{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001064 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct sadb_lifetime *lifetime;
1066 struct sadb_sa *sa;
1067 struct sadb_key *key;
Trent Jaegerdf718372005-12-13 23:12:27 -08001068 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 uint16_t proto;
1070 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
1074 if (!sa ||
1075 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1076 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1077 return ERR_PTR(-EINVAL);
1078 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1079 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1080 return ERR_PTR(-EINVAL);
1081 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1082 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1083 return ERR_PTR(-EINVAL);
1084 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1085 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1086 return ERR_PTR(-EINVAL);
1087
1088 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1089 if (proto == 0)
1090 return ERR_PTR(-EINVAL);
1091
1092 /* default error is no buffer space */
1093 err = -ENOBUFS;
1094
1095 /* RFC2367:
1096
1097 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1098 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1099 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1100 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1101 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1102 not true.
1103
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001104 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1106 */
1107 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1108 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1109 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1110 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1111 return ERR_PTR(-EINVAL);
1112 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1113 if (key != NULL &&
1114 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1115 ((key->sadb_key_bits+7) / 8 == 0 ||
1116 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1117 return ERR_PTR(-EINVAL);
1118 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1119 if (key != NULL &&
1120 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1121 ((key->sadb_key_bits+7) / 8 == 0 ||
1122 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1123 return ERR_PTR(-EINVAL);
1124
Alexey Dobriyan673c09b2008-11-25 17:15:16 -08001125 x = xfrm_state_alloc(&init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (x == NULL)
1127 return ERR_PTR(-ENOBUFS);
1128
1129 x->id.proto = proto;
1130 x->id.spi = sa->sadb_sa_spi;
1131 x->props.replay_window = sa->sadb_sa_replay;
1132 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1133 x->props.flags |= XFRM_STATE_NOECN;
1134 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1135 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001136 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1137 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
1140 if (lifetime != NULL) {
1141 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1142 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1143 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1144 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1145 }
1146 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
1147 if (lifetime != NULL) {
1148 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1149 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1150 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1151 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1152 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001153
1154 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
1155 if (sec_ctx != NULL) {
1156 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1157
1158 if (!uctx)
1159 goto out;
1160
1161 err = security_xfrm_state_alloc(x, uctx);
1162 kfree(uctx);
1163
1164 if (err)
1165 goto out;
1166 }
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1169 if (sa->sadb_sa_auth) {
1170 int keysize = 0;
1171 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1172 if (!a) {
1173 err = -ENOSYS;
1174 goto out;
1175 }
1176 if (key)
1177 keysize = (key->sadb_key_bits + 7) / 8;
1178 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1179 if (!x->aalg)
1180 goto out;
1181 strcpy(x->aalg->alg_name, a->name);
1182 x->aalg->alg_key_len = 0;
1183 if (key) {
1184 x->aalg->alg_key_len = key->sadb_key_bits;
1185 memcpy(x->aalg->alg_key, key+1, keysize);
1186 }
1187 x->props.aalgo = sa->sadb_sa_auth;
1188 /* x->algo.flags = sa->sadb_sa_flags; */
1189 }
1190 if (sa->sadb_sa_encrypt) {
1191 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1192 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1193 if (!a) {
1194 err = -ENOSYS;
1195 goto out;
1196 }
1197 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1198 if (!x->calg)
1199 goto out;
1200 strcpy(x->calg->alg_name, a->name);
1201 x->props.calgo = sa->sadb_sa_encrypt;
1202 } else {
1203 int keysize = 0;
1204 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1205 if (!a) {
1206 err = -ENOSYS;
1207 goto out;
1208 }
1209 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1210 if (key)
1211 keysize = (key->sadb_key_bits + 7) / 8;
1212 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1213 if (!x->ealg)
1214 goto out;
1215 strcpy(x->ealg->alg_name, a->name);
1216 x->ealg->alg_key_len = 0;
1217 if (key) {
1218 x->ealg->alg_key_len = key->sadb_key_bits;
1219 memcpy(x->ealg->alg_key, key+1, keysize);
1220 }
1221 x->props.ealgo = sa->sadb_sa_encrypt;
1222 }
1223 }
1224 /* x->algo.flags = sa->sadb_sa_flags; */
1225
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001226 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 &x->props.saddr);
1228 if (!x->props.family) {
1229 err = -EAFNOSUPPORT;
1230 goto out;
1231 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001232 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 &x->id.daddr);
1234
1235 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1236 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001237 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1238 if (mode < 0) {
1239 err = -EINVAL;
1240 goto out;
1241 }
1242 x->props.mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 x->props.reqid = sa2->sadb_x_sa2_reqid;
1244 }
1245
1246 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1247 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1248
1249 /* Nobody uses this, but we try. */
1250 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1251 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1252 }
1253
Kazunori MIYAZAWA4da51052008-05-21 13:26:11 -07001254 if (!x->sel.family)
Joy Latten4a4b6272007-08-02 19:25:43 -07001255 x->sel.family = x->props.family;
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1258 struct sadb_x_nat_t_type* n_type;
1259 struct xfrm_encap_tmpl *natt;
1260
1261 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1262 if (!x->encap)
1263 goto out;
1264
1265 natt = x->encap;
1266 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1267 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1268
1269 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1270 struct sadb_x_nat_t_port* n_port =
1271 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1272 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1273 }
1274 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1275 struct sadb_x_nat_t_port* n_port =
1276 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1277 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1278 }
1279 }
1280
Herbert Xu72cb6962005-06-20 13:18:08 -07001281 err = xfrm_init_state(x);
1282 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return x;
1287
1288out:
1289 x->km.state = XFRM_STATE_DEAD;
1290 xfrm_state_put(x);
1291 return ERR_PTR(err);
1292}
1293
1294static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1295{
1296 return -EOPNOTSUPP;
1297}
1298
1299static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1300{
1301 struct sk_buff *resp_skb;
1302 struct sadb_x_sa2 *sa2;
1303 struct sadb_address *saddr, *daddr;
1304 struct sadb_msg *out_hdr;
Herbert Xu658b2192007-10-09 13:29:52 -07001305 struct sadb_spirange *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 struct xfrm_state *x = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001307 int mode;
Herbert Xu658b2192007-10-09 13:29:52 -07001308 int err;
1309 u32 min_spi, max_spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 u32 reqid;
1311 u8 proto;
1312 unsigned short family;
1313 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1314
1315 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1316 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1317 return -EINVAL;
1318
1319 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1320 if (proto == 0)
1321 return -EINVAL;
1322
1323 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001324 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1325 if (mode < 0)
1326 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 reqid = sa2->sadb_x_sa2_reqid;
1328 } else {
1329 mode = 0;
1330 reqid = 0;
1331 }
1332
1333 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1334 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1335
1336 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1337 switch (family) {
1338 case AF_INET:
1339 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1340 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1341 break;
1342#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1343 case AF_INET6:
1344 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1345 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1346 break;
1347#endif
1348 }
1349
1350 if (hdr->sadb_msg_seq) {
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001351 x = xfrm_find_acq_byseq(&init_net, hdr->sadb_msg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1353 xfrm_state_put(x);
1354 x = NULL;
1355 }
1356 }
1357
1358 if (!x)
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001359 x = xfrm_find_acq(&init_net, mode, reqid, proto, xdaddr, xsaddr, 1, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 if (x == NULL)
1362 return -ENOENT;
1363
Herbert Xu658b2192007-10-09 13:29:52 -07001364 min_spi = 0x100;
1365 max_spi = 0x0fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Herbert Xu658b2192007-10-09 13:29:52 -07001367 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1368 if (range) {
1369 min_spi = range->sadb_spirange_min;
1370 max_spi = range->sadb_spirange_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Herbert Xu658b2192007-10-09 13:29:52 -07001372
1373 err = xfrm_alloc_spi(x, min_spi, max_spi);
Herbert Xu050f0092007-10-09 13:31:47 -07001374 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 if (IS_ERR(resp_skb)) {
1377 xfrm_state_put(x);
1378 return PTR_ERR(resp_skb);
1379 }
1380
1381 out_hdr = (struct sadb_msg *) resp_skb->data;
1382 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1383 out_hdr->sadb_msg_type = SADB_GETSPI;
1384 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1385 out_hdr->sadb_msg_errno = 0;
1386 out_hdr->sadb_msg_reserved = 0;
1387 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1388 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1389
1390 xfrm_state_put(x);
1391
1392 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1393
1394 return 0;
1395}
1396
1397static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1398{
1399 struct xfrm_state *x;
1400
1401 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1402 return -EOPNOTSUPP;
1403
1404 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1405 return 0;
1406
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001407 x = xfrm_find_acq_byseq(&init_net, hdr->sadb_msg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (x == NULL)
1409 return 0;
1410
1411 spin_lock_bh(&x->lock);
1412 if (x->km.state == XFRM_STATE_ACQ) {
1413 x->km.state = XFRM_STATE_ERROR;
Alexey Dobriyan50a30652008-11-25 17:21:01 -08001414 wake_up(&init_net.xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416 spin_unlock_bh(&x->lock);
1417 xfrm_state_put(x);
1418 return 0;
1419}
1420
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001421static inline int event2poltype(int event)
1422{
1423 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001424 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001425 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001426 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001427 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001428 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001429 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001430 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001431 // return SADB_X_SPDEXPIRE;
1432 default:
1433 printk("pfkey: Unknown policy event %d\n", event);
1434 break;
1435 }
1436
1437 return 0;
1438}
1439
1440static inline int event2keytype(int event)
1441{
1442 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001443 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001444 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001445 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001446 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001447 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001448 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001449 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001450 return SADB_EXPIRE;
1451 default:
1452 printk("pfkey: Unknown SA event %d\n", event);
1453 break;
1454 }
1455
1456 return 0;
1457}
1458
1459/* ADD/UPD/DEL */
1460static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1461{
1462 struct sk_buff *skb;
1463 struct sadb_msg *hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001464
Herbert Xu050f0092007-10-09 13:31:47 -07001465 skb = pfkey_xfrm_state2msg(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001466
1467 if (IS_ERR(skb))
1468 return PTR_ERR(skb);
1469
1470 hdr = (struct sadb_msg *) skb->data;
1471 hdr->sadb_msg_version = PF_KEY_V2;
1472 hdr->sadb_msg_type = event2keytype(c->event);
1473 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1474 hdr->sadb_msg_errno = 0;
1475 hdr->sadb_msg_reserved = 0;
1476 hdr->sadb_msg_seq = c->seq;
1477 hdr->sadb_msg_pid = c->pid;
1478
1479 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1480
1481 return 0;
1482}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1485{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 struct xfrm_state *x;
1487 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001488 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1491 if (IS_ERR(x))
1492 return PTR_ERR(x);
1493
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001494 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (hdr->sadb_msg_type == SADB_ADD)
1496 err = xfrm_state_add(x);
1497 else
1498 err = xfrm_state_update(x);
1499
Joy Lattenab5f5e82007-09-17 11:51:22 -07001500 xfrm_audit_state_add(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001501 audit_get_loginuid(current),
1502 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (err < 0) {
1505 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001506 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001507 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001510 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001511 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001512 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001513 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001514 c.seq = hdr->sadb_msg_seq;
1515 c.pid = hdr->sadb_msg_pid;
1516 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001517out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001518 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001519 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1523{
1524 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001525 struct km_event c;
1526 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 if (!ext_hdrs[SADB_EXT_SA-1] ||
1529 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1530 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1531 return -EINVAL;
1532
1533 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1534 if (x == NULL)
1535 return -ESRCH;
1536
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001537 if ((err = security_xfrm_state_delete(x)))
1538 goto out;
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001541 err = -EPERM;
1542 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
Joy Latten161a09e2006-11-27 13:11:54 -06001544
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001545 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001546
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001547 if (err < 0)
1548 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001549
1550 c.seq = hdr->sadb_msg_seq;
1551 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001552 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001553 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001554out:
Joy Lattenab5f5e82007-09-17 11:51:22 -07001555 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001556 audit_get_loginuid(current),
1557 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 xfrm_state_put(x);
1559
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001560 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
1563static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1564{
1565 __u8 proto;
1566 struct sk_buff *out_skb;
1567 struct sadb_msg *out_hdr;
1568 struct xfrm_state *x;
1569
1570 if (!ext_hdrs[SADB_EXT_SA-1] ||
1571 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1572 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1573 return -EINVAL;
1574
1575 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1576 if (x == NULL)
1577 return -ESRCH;
1578
Herbert Xu050f0092007-10-09 13:31:47 -07001579 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 proto = x->id.proto;
1581 xfrm_state_put(x);
1582 if (IS_ERR(out_skb))
1583 return PTR_ERR(out_skb);
1584
1585 out_hdr = (struct sadb_msg *) out_skb->data;
1586 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Charles Hardin435000b2007-11-22 19:35:15 +08001587 out_hdr->sadb_msg_type = SADB_GET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1589 out_hdr->sadb_msg_errno = 0;
1590 out_hdr->sadb_msg_reserved = 0;
1591 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1592 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1593 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1594
1595 return 0;
1596}
1597
Randy Dunlap00fa0232005-10-04 22:43:04 -07001598static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001599 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 struct sk_buff *skb;
1602 struct sadb_msg *hdr;
1603 int len, auth_len, enc_len, i;
1604
1605 auth_len = xfrm_count_auth_supported();
1606 if (auth_len) {
1607 auth_len *= sizeof(struct sadb_alg);
1608 auth_len += sizeof(struct sadb_supported);
1609 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 enc_len = xfrm_count_enc_supported();
1612 if (enc_len) {
1613 enc_len *= sizeof(struct sadb_alg);
1614 enc_len += sizeof(struct sadb_supported);
1615 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 len = enc_len + auth_len + sizeof(struct sadb_msg);
1618
1619 skb = alloc_skb(len + 16, allocation);
1620 if (!skb)
1621 goto out_put_algs;
1622
1623 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1624 pfkey_hdr_dup(hdr, orig);
1625 hdr->sadb_msg_errno = 0;
1626 hdr->sadb_msg_len = len / sizeof(uint64_t);
1627
1628 if (auth_len) {
1629 struct sadb_supported *sp;
1630 struct sadb_alg *ap;
1631
1632 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1633 ap = (struct sadb_alg *) (sp + 1);
1634
1635 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1636 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1637
1638 for (i = 0; ; i++) {
1639 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1640 if (!aalg)
1641 break;
1642 if (aalg->available)
1643 *ap++ = aalg->desc;
1644 }
1645 }
1646
1647 if (enc_len) {
1648 struct sadb_supported *sp;
1649 struct sadb_alg *ap;
1650
1651 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1652 ap = (struct sadb_alg *) (sp + 1);
1653
1654 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1655 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1656
1657 for (i = 0; ; i++) {
1658 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1659 if (!ealg)
1660 break;
1661 if (ealg->available)
1662 *ap++ = ealg->desc;
1663 }
1664 }
1665
1666out_put_algs:
1667 return skb;
1668}
1669
1670static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1671{
1672 struct pfkey_sock *pfk = pfkey_sk(sk);
1673 struct sk_buff *supp_skb;
1674
1675 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1676 return -EINVAL;
1677
1678 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1679 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1680 return -EEXIST;
1681 pfk->registered |= (1<<hdr->sadb_msg_satype);
1682 }
1683
1684 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1687 if (!supp_skb) {
1688 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1689 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1690
1691 return -ENOBUFS;
1692 }
1693
1694 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1695
1696 return 0;
1697}
1698
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001699static int key_notify_sa_flush(struct km_event *c)
1700{
1701 struct sk_buff *skb;
1702 struct sadb_msg *hdr;
1703
1704 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1705 if (!skb)
1706 return -ENOBUFS;
1707 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf088672005-06-18 22:44:00 -07001708 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001709 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001710 hdr->sadb_msg_seq = c->seq;
1711 hdr->sadb_msg_pid = c->pid;
1712 hdr->sadb_msg_version = PF_KEY_V2;
1713 hdr->sadb_msg_errno = (uint8_t) 0;
1714 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1715
1716 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1717
1718 return 0;
1719}
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1722{
1723 unsigned proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001724 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06001725 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001726 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1729 if (proto == 0)
1730 return -EINVAL;
1731
Al Viro0c11b942008-01-10 04:20:52 -05001732 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04001733 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06001734 audit_info.secid = 0;
Alexey Dobriyan0e602452008-11-25 17:30:18 -08001735 err = xfrm_state_flush(&init_net, proto, &audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -04001736 if (err)
1737 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001738 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001739 c.seq = hdr->sadb_msg_seq;
1740 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001741 c.event = XFRM_MSG_FLUSHSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001742 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 return 0;
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1748{
Timo Teras83321d62008-03-03 23:40:12 -08001749 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 struct sk_buff *out_skb;
1751 struct sadb_msg *out_hdr;
1752
Timo Teras83321d62008-03-03 23:40:12 -08001753 if (!pfkey_can_dump(&pfk->sk))
1754 return -ENOBUFS;
1755
Herbert Xu050f0092007-10-09 13:31:47 -07001756 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (IS_ERR(out_skb))
1758 return PTR_ERR(out_skb);
1759
1760 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08001761 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 out_hdr->sadb_msg_type = SADB_DUMP;
1763 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1764 out_hdr->sadb_msg_errno = 0;
1765 out_hdr->sadb_msg_reserved = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07001766 out_hdr->sadb_msg_seq = count + 1;
Timo Teras83321d62008-03-03 23:40:12 -08001767 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
Herbert Xu12a169e2008-10-01 07:03:24 -07001768
1769 if (pfk->dump.skb)
1770 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
1771 &pfk->sk);
1772 pfk->dump.skb = out_skb;
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return 0;
1775}
1776
Timo Teras83321d62008-03-03 23:40:12 -08001777static int pfkey_dump_sa(struct pfkey_sock *pfk)
1778{
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001779 return xfrm_state_walk(&init_net, &pfk->dump.u.state, dump_sa, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08001780}
1781
1782static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1783{
1784 xfrm_state_walk_done(&pfk->dump.u.state);
1785}
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1788{
1789 u8 proto;
Timo Teras83321d62008-03-03 23:40:12 -08001790 struct pfkey_sock *pfk = pfkey_sk(sk);
1791
1792 if (pfk->dump.dump != NULL)
1793 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1796 if (proto == 0)
1797 return -EINVAL;
1798
Timo Teras83321d62008-03-03 23:40:12 -08001799 pfk->dump.msg_version = hdr->sadb_msg_version;
1800 pfk->dump.msg_pid = hdr->sadb_msg_pid;
1801 pfk->dump.dump = pfkey_dump_sa;
1802 pfk->dump.done = pfkey_dump_sa_done;
1803 xfrm_state_walk_init(&pfk->dump.u.state, proto);
Timo Teras4c563f72008-02-28 21:31:08 -08001804
Timo Teras83321d62008-03-03 23:40:12 -08001805 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
1808static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1809{
1810 struct pfkey_sock *pfk = pfkey_sk(sk);
1811 int satype = hdr->sadb_msg_satype;
1812
1813 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1814 /* XXX we mangle packet... */
1815 hdr->sadb_msg_errno = 0;
1816 if (satype != 0 && satype != 1)
1817 return -EINVAL;
1818 pfk->promisc = satype;
1819 }
1820 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1821 return 0;
1822}
1823
1824static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1825{
1826 int i;
1827 u32 reqid = *(u32*)ptr;
1828
1829 for (i=0; i<xp->xfrm_nr; i++) {
1830 if (xp->xfrm_vec[i].reqid == reqid)
1831 return -EEXIST;
1832 }
1833 return 0;
1834}
1835
1836static u32 gen_reqid(void)
1837{
Timo Teras4c563f72008-02-28 21:31:08 -08001838 struct xfrm_policy_walk walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 u32 start;
Timo Teras4c563f72008-02-28 21:31:08 -08001840 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1842
1843 start = reqid;
1844 do {
1845 ++reqid;
1846 if (reqid == 0)
1847 reqid = IPSEC_MANUAL_REQID_MAX+1;
Timo Teras4c563f72008-02-28 21:31:08 -08001848 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
1849 rc = xfrm_policy_walk(&walk, check_reqid, (void*)&reqid);
1850 xfrm_policy_walk_done(&walk);
1851 if (rc != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return reqid;
1853 } while (reqid != start);
1854 return 0;
1855}
1856
1857static int
1858parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1859{
1860 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001861 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1864 return -ELOOP;
1865
1866 if (rq->sadb_x_ipsecrequest_mode == 0)
1867 return -EINVAL;
1868
1869 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001870 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1871 return -EINVAL;
1872 t->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1874 t->optional = 1;
1875 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1876 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1877 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1878 t->reqid = 0;
1879 if (!t->reqid && !(t->reqid = gen_reqid()))
1880 return -ENOBUFS;
1881 }
1882
1883 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001884 if (t->mode == XFRM_MODE_TUNNEL) {
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001885 u8 *sa = (u8 *) (rq + 1);
1886 int family, socklen;
1887
1888 family = pfkey_sockaddr_extract((struct sockaddr *)sa,
1889 &t->saddr);
1890 if (!family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return -EINVAL;
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001892
1893 socklen = pfkey_sockaddr_len(family);
1894 if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen),
1895 &t->id.daddr) != family)
1896 return -EINVAL;
1897 t->encap_family = family;
Miika Komu2718aa72006-11-30 16:41:50 -08001898 } else
1899 t->encap_family = xp->family;
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* No way to set this via kame pfkey */
Herbert Xuc5d18e92008-04-22 00:46:42 -07001902 t->allalgs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 xp->xfrm_nr++;
1904 return 0;
1905}
1906
1907static int
1908parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1909{
1910 int err;
1911 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1912 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1913
1914 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1915 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1916 return err;
1917 len -= rq->sadb_x_ipsecrequest_len;
1918 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1919 }
1920 return 0;
1921}
1922
Trent Jaegerdf718372005-12-13 23:12:27 -08001923static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1924{
1925 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1926
1927 if (xfrm_ctx) {
1928 int len = sizeof(struct sadb_x_sec_ctx);
1929 len += xfrm_ctx->ctx_len;
1930 return PFKEY_ALIGN8(len);
1931 }
1932 return 0;
1933}
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1936{
Miika Komu2718aa72006-11-30 16:41:50 -08001937 struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08001939 int socklen = 0;
1940 int i;
1941
1942 for (i=0; i<xp->xfrm_nr; i++) {
1943 t = xp->xfrm_vec + i;
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09001944 socklen += pfkey_sockaddr_len(t->encap_family);
Miika Komu2718aa72006-11-30 16:41:50 -08001945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 return sizeof(struct sadb_msg) +
1948 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001949 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 (sockaddr_size * 2) +
1951 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08001952 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1953 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08001954 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
1957static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1958{
1959 struct sk_buff *skb;
1960 int size;
1961
1962 size = pfkey_xfrm_policy2msg_size(xp);
1963
1964 skb = alloc_skb(size + 16, GFP_ATOMIC);
1965 if (skb == NULL)
1966 return ERR_PTR(-ENOBUFS);
1967
1968 return skb;
1969}
1970
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001971static int pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
1973 struct sadb_msg *hdr;
1974 struct sadb_address *addr;
1975 struct sadb_lifetime *lifetime;
1976 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08001977 struct sadb_x_sec_ctx *sec_ctx;
1978 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 int i;
1980 int size;
1981 int sockaddr_size = pfkey_sockaddr_size(xp->family);
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09001982 int socklen = pfkey_sockaddr_len(xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 size = pfkey_xfrm_policy2msg_size(xp);
1985
1986 /* call should fill header later */
1987 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1988 memset(hdr, 0, size); /* XXX do we need this ? */
1989
1990 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001991 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001993 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 (sizeof(struct sadb_address)+sockaddr_size)/
1995 sizeof(uint64_t);
1996 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1997 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1998 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1999 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002000 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2001 xp->selector.sport,
2002 (struct sockaddr *) (addr + 1),
2003 xp->family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 BUG();
2005
2006 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002007 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 sizeof(struct sadb_address)+sockaddr_size);
2009 addr->sadb_address_len =
2010 (sizeof(struct sadb_address)+sockaddr_size)/
2011 sizeof(uint64_t);
2012 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2013 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002014 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002016
2017 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2018 (struct sockaddr *) (addr + 1),
2019 xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 /* hard time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002022 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 sizeof(struct sadb_lifetime));
2024 lifetime->sadb_lifetime_len =
2025 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2026 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2027 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2028 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2029 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2030 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2031 /* soft time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002032 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 sizeof(struct sadb_lifetime));
2034 lifetime->sadb_lifetime_len =
2035 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2036 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2037 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2038 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2039 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2040 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2041 /* current time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002042 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 sizeof(struct sadb_lifetime));
2044 lifetime->sadb_lifetime_len =
2045 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2046 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2047 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2048 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2049 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2050 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2051
2052 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2053 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2054 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2055 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2056 if (xp->action == XFRM_POLICY_ALLOW) {
2057 if (xp->xfrm_nr)
2058 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2059 else
2060 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2061 }
2062 pol->sadb_x_policy_dir = dir+1;
2063 pol->sadb_x_policy_id = xp->index;
2064 pol->sadb_x_policy_priority = xp->priority;
2065
2066 for (i=0; i<xp->xfrm_nr; i++) {
2067 struct sadb_x_ipsecrequest *rq;
2068 struct xfrm_tmpl *t = xp->xfrm_vec + i;
2069 int req_size;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002070 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 req_size = sizeof(struct sadb_x_ipsecrequest);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002073 if (t->mode == XFRM_MODE_TUNNEL) {
2074 socklen = pfkey_sockaddr_len(t->encap_family);
2075 req_size += socklen * 2;
2076 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 size -= 2*socklen;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002078 }
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
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08002177 xp = xfrm_policy_alloc(&init_net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 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
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08002327 xp = xfrm_policy_bysel_ctx(&init_net, XFRM_POLICY_TYPE_MAIN,
Paul Moore03e1ad72008-04-12 19:07:52 -07002328 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;
Tobias Brunner1839faa2008-10-10 14:07:03 -07002343 c.data.byid = 0;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002344 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002345 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2346
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002347out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002348 xfrm_pol_put(xp);
2349 return err;
2350}
2351
2352static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2353{
2354 int err;
2355 struct sk_buff *out_skb;
2356 struct sadb_msg *out_hdr;
2357 err = 0;
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2360 if (IS_ERR(out_skb)) {
2361 err = PTR_ERR(out_skb);
2362 goto out;
2363 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002364 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2365 if (err < 0)
2366 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 out_hdr = (struct sadb_msg *) out_skb->data;
2369 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002370 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 out_hdr->sadb_msg_satype = 0;
2372 out_hdr->sadb_msg_errno = 0;
2373 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2374 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002375 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 err = 0;
2377
2378out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 return err;
2380}
2381
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002382#ifdef CONFIG_NET_KEY_MIGRATE
2383static int pfkey_sockaddr_pair_size(sa_family_t family)
2384{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002385 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002386}
2387
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002388static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002389 xfrm_address_t *saddr, xfrm_address_t *daddr,
2390 u16 *family)
2391{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002392 int af, socklen;
2393
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002394 if (ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002395 return -EINVAL;
2396
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002397 af = pfkey_sockaddr_extract(sa, saddr);
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002398 if (!af)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002399 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002400
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002401 socklen = pfkey_sockaddr_len(af);
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002402 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002403 daddr) != af)
2404 return -EINVAL;
2405
2406 *family = af;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002407 return 0;
2408}
2409
2410static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2411 struct xfrm_migrate *m)
2412{
2413 int err;
2414 struct sadb_x_ipsecrequest *rq2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002415 int mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002416
2417 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2418 len < rq1->sadb_x_ipsecrequest_len)
2419 return -EINVAL;
2420
2421 /* old endoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002422 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
2423 rq1->sadb_x_ipsecrequest_len,
2424 &m->old_saddr, &m->old_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002425 &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 */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002437 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
2438 rq2->sadb_x_ipsecrequest_len,
2439 &m->new_saddr, &m->new_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002440 &m->new_family);
2441 if (err)
2442 return err;
2443
2444 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2445 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2446 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2447 return -EINVAL;
2448
2449 m->proto = rq1->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002450 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2451 return -EINVAL;
2452 m->mode = mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002453 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2454
2455 return ((int)(rq1->sadb_x_ipsecrequest_len +
2456 rq2->sadb_x_ipsecrequest_len));
2457}
2458
2459static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2460 struct sadb_msg *hdr, void **ext_hdrs)
2461{
2462 int i, len, ret, err = -EINVAL;
2463 u8 dir;
2464 struct sadb_address *sa;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002465 struct sadb_x_kmaddress *kma;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002466 struct sadb_x_policy *pol;
2467 struct sadb_x_ipsecrequest *rq;
2468 struct xfrm_selector sel;
2469 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002470 struct xfrm_kmaddress k;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002471
2472 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002473 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002474 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2475 err = -EINVAL;
2476 goto out;
2477 }
2478
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002479 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002480 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002481
2482 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2483 err = -EINVAL;
2484 goto out;
2485 }
2486
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002487 if (kma) {
2488 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2489 k.reserved = kma->sadb_x_kmaddress_reserved;
2490 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2491 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2492 &k.local, &k.remote, &k.family);
2493 if (ret < 0) {
2494 err = ret;
2495 goto out;
2496 }
2497 }
2498
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002499 dir = pol->sadb_x_policy_dir - 1;
2500 memset(&sel, 0, sizeof(sel));
2501
2502 /* set source address info of selector */
2503 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2504 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2505 sel.prefixlen_s = sa->sadb_address_prefixlen;
2506 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2507 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2508 if (sel.sport)
Al Viro582ee432007-07-26 17:33:39 +01002509 sel.sport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002510
2511 /* set destination address info of selector */
2512 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2513 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2514 sel.prefixlen_d = sa->sadb_address_prefixlen;
2515 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2516 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2517 if (sel.dport)
Al Viro582ee432007-07-26 17:33:39 +01002518 sel.dport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002519
2520 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2521
2522 /* extract ipsecrequests */
2523 i = 0;
2524 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2525
2526 while (len > 0 && i < XFRM_MAX_DEPTH) {
2527 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2528 if (ret < 0) {
2529 err = ret;
2530 goto out;
2531 } else {
2532 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2533 len -= ret;
2534 i++;
2535 }
2536 }
2537
2538 if (!i || len > 0) {
2539 err = -EINVAL;
2540 goto out;
2541 }
2542
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002543 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
2544 kma ? &k : NULL);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002545
2546 out:
2547 return err;
2548}
2549#else
2550static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2551 struct sadb_msg *hdr, void **ext_hdrs)
2552{
2553 return -ENOPROTOOPT;
2554}
2555#endif
2556
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2559{
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002560 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002561 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 struct sadb_x_policy *pol;
2563 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002564 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2567 return -EINVAL;
2568
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002569 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2570 if (dir >= XFRM_POLICY_MAX)
2571 return -EINVAL;
2572
Eric Paris215a2dd2007-03-07 16:01:45 -08002573 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08002574 xp = xfrm_policy_byid(&init_net, XFRM_POLICY_TYPE_MAIN, dir,
2575 pol->sadb_x_policy_id, delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 if (xp == NULL)
2577 return -ENOENT;
2578
Eric Paris215a2dd2007-03-07 16:01:45 -08002579 if (delete) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002580 xfrm_audit_policy_delete(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002581 audit_get_loginuid(current),
2582 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Eric Paris215a2dd2007-03-07 16:01:45 -08002584 if (err)
2585 goto out;
2586 c.seq = hdr->sadb_msg_seq;
2587 c.pid = hdr->sadb_msg_pid;
Herbert Xubf088672005-06-18 22:44:00 -07002588 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002589 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002590 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002591 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002592 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Eric Paris215a2dd2007-03-07 16:01:45 -08002595out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 xfrm_pol_put(xp);
2597 return err;
2598}
2599
2600static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2601{
Timo Teras83321d62008-03-03 23:40:12 -08002602 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 struct sk_buff *out_skb;
2604 struct sadb_msg *out_hdr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002605 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Timo Teras83321d62008-03-03 23:40:12 -08002607 if (!pfkey_can_dump(&pfk->sk))
2608 return -ENOBUFS;
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2611 if (IS_ERR(out_skb))
2612 return PTR_ERR(out_skb);
2613
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002614 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2615 if (err < 0)
2616 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08002619 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2621 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2622 out_hdr->sadb_msg_errno = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07002623 out_hdr->sadb_msg_seq = count + 1;
Timo Teras83321d62008-03-03 23:40:12 -08002624 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
Herbert Xu12a169e2008-10-01 07:03:24 -07002625
2626 if (pfk->dump.skb)
2627 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
2628 &pfk->sk);
2629 pfk->dump.skb = out_skb;
2630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 return 0;
2632}
2633
Timo Teras83321d62008-03-03 23:40:12 -08002634static int pfkey_dump_sp(struct pfkey_sock *pfk)
2635{
2636 return xfrm_policy_walk(&pfk->dump.u.policy, dump_sp, (void *) pfk);
2637}
2638
2639static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2640{
2641 xfrm_policy_walk_done(&pfk->dump.u.policy);
2642}
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2645{
Timo Teras83321d62008-03-03 23:40:12 -08002646 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Timo Teras83321d62008-03-03 23:40:12 -08002648 if (pfk->dump.dump != NULL)
2649 return -EBUSY;
Timo Teras4c563f72008-02-28 21:31:08 -08002650
Timo Teras83321d62008-03-03 23:40:12 -08002651 pfk->dump.msg_version = hdr->sadb_msg_version;
2652 pfk->dump.msg_pid = hdr->sadb_msg_pid;
2653 pfk->dump.dump = pfkey_dump_sp;
2654 pfk->dump.done = pfkey_dump_sp_done;
2655 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2656
2657 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002660static int key_notify_policy_flush(struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
2662 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002663 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002665 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (!skb_out)
2667 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002668 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002669 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002670 hdr->sadb_msg_seq = c->seq;
2671 hdr->sadb_msg_pid = c->pid;
2672 hdr->sadb_msg_version = PF_KEY_V2;
2673 hdr->sadb_msg_errno = (uint8_t) 0;
2674 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2675 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2676 return 0;
2677
2678}
2679
2680static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2681{
2682 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06002683 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04002684 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Al Viro0c11b942008-01-10 04:20:52 -05002686 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04002687 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06002688 audit_info.secid = 0;
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -08002689 err = xfrm_policy_flush(&init_net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -04002690 if (err)
2691 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002692 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002693 c.event = XFRM_MSG_FLUSHPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002694 c.pid = hdr->sadb_msg_pid;
2695 c.seq = hdr->sadb_msg_seq;
2696 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698 return 0;
2699}
2700
2701typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2702 struct sadb_msg *hdr, void **ext_hdrs);
2703static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2704 [SADB_RESERVED] = pfkey_reserved,
2705 [SADB_GETSPI] = pfkey_getspi,
2706 [SADB_UPDATE] = pfkey_add,
2707 [SADB_ADD] = pfkey_add,
2708 [SADB_DELETE] = pfkey_delete,
2709 [SADB_GET] = pfkey_get,
2710 [SADB_ACQUIRE] = pfkey_acquire,
2711 [SADB_REGISTER] = pfkey_register,
2712 [SADB_EXPIRE] = NULL,
2713 [SADB_FLUSH] = pfkey_flush,
2714 [SADB_DUMP] = pfkey_dump,
2715 [SADB_X_PROMISC] = pfkey_promisc,
2716 [SADB_X_PCHANGE] = NULL,
2717 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2718 [SADB_X_SPDADD] = pfkey_spdadd,
2719 [SADB_X_SPDDELETE] = pfkey_spddelete,
2720 [SADB_X_SPDGET] = pfkey_spdget,
2721 [SADB_X_SPDACQUIRE] = NULL,
2722 [SADB_X_SPDDUMP] = pfkey_spddump,
2723 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2724 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2725 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002726 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727};
2728
2729static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2730{
2731 void *ext_hdrs[SADB_EXT_MAX];
2732 int err;
2733
2734 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2735 BROADCAST_PROMISC_ONLY, NULL);
2736
2737 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2738 err = parse_exthdrs(skb, hdr, ext_hdrs);
2739 if (!err) {
2740 err = -EOPNOTSUPP;
2741 if (pfkey_funcs[hdr->sadb_msg_type])
2742 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2743 }
2744 return err;
2745}
2746
2747static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2748{
2749 struct sadb_msg *hdr = NULL;
2750
2751 if (skb->len < sizeof(*hdr)) {
2752 *errp = -EMSGSIZE;
2753 } else {
2754 hdr = (struct sadb_msg *) skb->data;
2755 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2756 hdr->sadb_msg_reserved != 0 ||
2757 (hdr->sadb_msg_type <= SADB_RESERVED ||
2758 hdr->sadb_msg_type > SADB_MAX)) {
2759 hdr = NULL;
2760 *errp = -EINVAL;
2761 } else if (hdr->sadb_msg_len != (skb->len /
2762 sizeof(uint64_t)) ||
2763 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2764 sizeof(uint64_t))) {
2765 hdr = NULL;
2766 *errp = -EMSGSIZE;
2767 } else {
2768 *errp = 0;
2769 }
2770 }
2771 return hdr;
2772}
2773
2774static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2775{
Herbert Xuf3980352007-12-19 23:44:29 -08002776 unsigned int id = d->desc.sadb_alg_id;
2777
2778 if (id >= sizeof(t->aalgos) * 8)
2779 return 0;
2780
2781 return (t->aalgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782}
2783
2784static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2785{
Herbert Xuf3980352007-12-19 23:44:29 -08002786 unsigned int id = d->desc.sadb_alg_id;
2787
2788 if (id >= sizeof(t->ealgos) * 8)
2789 return 0;
2790
2791 return (t->ealgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
2793
2794static int count_ah_combs(struct xfrm_tmpl *t)
2795{
2796 int i, sz = 0;
2797
2798 for (i = 0; ; i++) {
2799 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2800 if (!aalg)
2801 break;
2802 if (aalg_tmpl_set(t, aalg) && aalg->available)
2803 sz += sizeof(struct sadb_comb);
2804 }
2805 return sz + sizeof(struct sadb_prop);
2806}
2807
2808static int count_esp_combs(struct xfrm_tmpl *t)
2809{
2810 int i, k, sz = 0;
2811
2812 for (i = 0; ; i++) {
2813 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2814 if (!ealg)
2815 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2818 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 for (k = 1; ; k++) {
2821 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2822 if (!aalg)
2823 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (aalg_tmpl_set(t, aalg) && aalg->available)
2826 sz += sizeof(struct sadb_comb);
2827 }
2828 }
2829 return sz + sizeof(struct sadb_prop);
2830}
2831
2832static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2833{
2834 struct sadb_prop *p;
2835 int i;
2836
2837 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2838 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2839 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2840 p->sadb_prop_replay = 32;
2841 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2842
2843 for (i = 0; ; i++) {
2844 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2845 if (!aalg)
2846 break;
2847
2848 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2849 struct sadb_comb *c;
2850 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2851 memset(c, 0, sizeof(*c));
2852 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2853 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2854 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2855 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2856 c->sadb_comb_hard_addtime = 24*60*60;
2857 c->sadb_comb_soft_addtime = 20*60*60;
2858 c->sadb_comb_hard_usetime = 8*60*60;
2859 c->sadb_comb_soft_usetime = 7*60*60;
2860 }
2861 }
2862}
2863
2864static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2865{
2866 struct sadb_prop *p;
2867 int i, k;
2868
2869 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2870 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2871 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2872 p->sadb_prop_replay = 32;
2873 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2874
2875 for (i=0; ; i++) {
2876 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2877 if (!ealg)
2878 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2881 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 for (k = 1; ; k++) {
2884 struct sadb_comb *c;
2885 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2886 if (!aalg)
2887 break;
2888 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2889 continue;
2890 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2891 memset(c, 0, sizeof(*c));
2892 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2893 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2894 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2895 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2896 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2897 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2898 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2899 c->sadb_comb_hard_addtime = 24*60*60;
2900 c->sadb_comb_soft_addtime = 20*60*60;
2901 c->sadb_comb_hard_usetime = 8*60*60;
2902 c->sadb_comb_soft_usetime = 7*60*60;
2903 }
2904 }
2905}
2906
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002907static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2908{
2909 return 0;
2910}
2911
2912static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
2914 struct sk_buff *out_skb;
2915 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002916 int hard;
2917 int hsc;
2918
Herbert Xubf088672005-06-18 22:44:00 -07002919 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002920 if (hard)
2921 hsc = 2;
2922 else
2923 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Herbert Xu050f0092007-10-09 13:31:47 -07002925 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 if (IS_ERR(out_skb))
2927 return PTR_ERR(out_skb);
2928
2929 out_hdr = (struct sadb_msg *) out_skb->data;
2930 out_hdr->sadb_msg_version = PF_KEY_V2;
2931 out_hdr->sadb_msg_type = SADB_EXPIRE;
2932 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2933 out_hdr->sadb_msg_errno = 0;
2934 out_hdr->sadb_msg_reserved = 0;
2935 out_hdr->sadb_msg_seq = 0;
2936 out_hdr->sadb_msg_pid = 0;
2937
2938 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2939 return 0;
2940}
2941
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002942static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2943{
Jamal Hadi Salim99c6f602008-06-10 14:25:34 -07002944 if (atomic_read(&pfkey_socks_nr) == 0)
2945 return 0;
2946
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002947 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002948 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002949 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002950 case XFRM_MSG_DELSA:
2951 case XFRM_MSG_NEWSA:
2952 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002953 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002954 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002955 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002956 case XFRM_MSG_NEWAE: /* not yet supported */
2957 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002958 default:
2959 printk("pfkey: Unknown SA event %d\n", c->event);
2960 break;
2961 }
2962
2963 return 0;
2964}
2965
2966static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2967{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002968 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
2969 return 0;
2970
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002971 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002972 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002973 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002974 case XFRM_MSG_DELPOLICY:
2975 case XFRM_MSG_NEWPOLICY:
2976 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002977 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002978 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002979 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
2980 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002981 return key_notify_policy_flush(c);
2982 default:
2983 printk("pfkey: Unknown policy event %d\n", c->event);
2984 break;
2985 }
2986
2987 return 0;
2988}
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990static u32 get_acqseq(void)
2991{
2992 u32 res;
2993 static u32 acqseq;
2994 static DEFINE_SPINLOCK(acqseq_lock);
2995
2996 spin_lock_bh(&acqseq_lock);
2997 res = (++acqseq ? : ++acqseq);
2998 spin_unlock_bh(&acqseq_lock);
2999 return res;
3000}
3001
3002static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
3003{
3004 struct sk_buff *skb;
3005 struct sadb_msg *hdr;
3006 struct sadb_address *addr;
3007 struct sadb_x_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 int sockaddr_size;
3009 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003010 struct sadb_x_sec_ctx *sec_ctx;
3011 struct xfrm_sec_ctx *xfrm_ctx;
3012 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3015 if (!sockaddr_size)
3016 return -EINVAL;
3017
3018 size = sizeof(struct sadb_msg) +
3019 (sizeof(struct sadb_address) * 2) +
3020 (sockaddr_size * 2) +
3021 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 if (x->id.proto == IPPROTO_AH)
3024 size += count_ah_combs(t);
3025 else if (x->id.proto == IPPROTO_ESP)
3026 size += count_esp_combs(t);
3027
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003028 if ((xfrm_ctx = x->security)) {
3029 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3030 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3031 }
3032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 skb = alloc_skb(size + 16, GFP_ATOMIC);
3034 if (skb == NULL)
3035 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3038 hdr->sadb_msg_version = PF_KEY_V2;
3039 hdr->sadb_msg_type = SADB_ACQUIRE;
3040 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3041 hdr->sadb_msg_len = size / sizeof(uint64_t);
3042 hdr->sadb_msg_errno = 0;
3043 hdr->sadb_msg_reserved = 0;
3044 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3045 hdr->sadb_msg_pid = 0;
3046
3047 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003048 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003050 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 (sizeof(struct sadb_address)+sockaddr_size)/
3052 sizeof(uint64_t);
3053 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3054 addr->sadb_address_proto = 0;
3055 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003056 addr->sadb_address_prefixlen =
3057 pfkey_sockaddr_fill(&x->props.saddr, 0,
3058 (struct sockaddr *) (addr + 1),
3059 x->props.family);
3060 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003064 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 sizeof(struct sadb_address)+sockaddr_size);
3066 addr->sadb_address_len =
3067 (sizeof(struct sadb_address)+sockaddr_size)/
3068 sizeof(uint64_t);
3069 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3070 addr->sadb_address_proto = 0;
3071 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003072 addr->sadb_address_prefixlen =
3073 pfkey_sockaddr_fill(&x->id.daddr, 0,
3074 (struct sockaddr *) (addr + 1),
3075 x->props.family);
3076 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 BUG();
3078
3079 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3080 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3081 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3082 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3083 pol->sadb_x_policy_dir = dir+1;
3084 pol->sadb_x_policy_id = xp->index;
3085
3086 /* Set sadb_comb's. */
3087 if (x->id.proto == IPPROTO_AH)
3088 dump_ah_combs(skb, t);
3089 else if (x->id.proto == IPPROTO_ESP)
3090 dump_esp_combs(skb, t);
3091
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003092 /* security context */
3093 if (xfrm_ctx) {
3094 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3095 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3096 sec_ctx->sadb_x_sec_len =
3097 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3098 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3099 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3100 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3101 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3102 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3103 xfrm_ctx->ctx_len);
3104 }
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3107}
3108
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003109static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003110 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
3112 struct xfrm_policy *xp;
3113 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003114 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003116 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 case AF_INET:
3118 if (opt != IP_IPSEC_POLICY) {
3119 *dir = -EOPNOTSUPP;
3120 return NULL;
3121 }
3122 break;
3123#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3124 case AF_INET6:
3125 if (opt != IPV6_IPSEC_POLICY) {
3126 *dir = -EOPNOTSUPP;
3127 return NULL;
3128 }
3129 break;
3130#endif
3131 default:
3132 *dir = -EINVAL;
3133 return NULL;
3134 }
3135
3136 *dir = -EINVAL;
3137
3138 if (len < sizeof(struct sadb_x_policy) ||
3139 pol->sadb_x_policy_len*8 > len ||
3140 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3141 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3142 return NULL;
3143
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08003144 xp = xfrm_policy_alloc(&init_net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 if (xp == NULL) {
3146 *dir = -ENOBUFS;
3147 return NULL;
3148 }
3149
3150 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3151 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3152
3153 xp->lft.soft_byte_limit = XFRM_INF;
3154 xp->lft.hard_byte_limit = XFRM_INF;
3155 xp->lft.soft_packet_limit = XFRM_INF;
3156 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003157 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
3159 xp->xfrm_nr = 0;
3160 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3161 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3162 goto out;
3163
Trent Jaegerdf718372005-12-13 23:12:27 -08003164 /* security context too */
3165 if (len >= (pol->sadb_x_policy_len*8 +
3166 sizeof(struct sadb_x_sec_ctx))) {
3167 char *p = (char *)pol;
3168 struct xfrm_user_sec_ctx *uctx;
3169
3170 p += pol->sadb_x_policy_len*8;
3171 sec_ctx = (struct sadb_x_sec_ctx *)p;
3172 if (len < pol->sadb_x_policy_len*8 +
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003173 sec_ctx->sadb_x_sec_len) {
3174 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003175 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003176 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003177 if ((*dir = verify_sec_ctx_len(p)))
3178 goto out;
3179 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
Paul Moore03e1ad72008-04-12 19:07:52 -07003180 *dir = security_xfrm_policy_alloc(&xp->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08003181 kfree(uctx);
3182
3183 if (*dir)
3184 goto out;
3185 }
3186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 *dir = pol->sadb_x_policy_dir-1;
3188 return xp;
3189
3190out:
Alexey Dobriyan70e90672008-11-06 23:08:37 -08003191 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08003192 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return NULL;
3194}
3195
Al Viro5d36b182006-11-08 00:24:06 -08003196static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
3198 struct sk_buff *skb;
3199 struct sadb_msg *hdr;
3200 struct sadb_sa *sa;
3201 struct sadb_address *addr;
3202 struct sadb_x_nat_t_port *n_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 int sockaddr_size;
3204 int size;
3205 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3206 struct xfrm_encap_tmpl *natt = NULL;
3207
3208 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3209 if (!sockaddr_size)
3210 return -EINVAL;
3211
3212 if (!satype)
3213 return -EINVAL;
3214
3215 if (!x->encap)
3216 return -EINVAL;
3217
3218 natt = x->encap;
3219
3220 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3221 *
3222 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3223 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3224 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 size = sizeof(struct sadb_msg) +
3227 sizeof(struct sadb_sa) +
3228 (sizeof(struct sadb_address) * 2) +
3229 (sockaddr_size * 2) +
3230 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 skb = alloc_skb(size + 16, GFP_ATOMIC);
3233 if (skb == NULL)
3234 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3237 hdr->sadb_msg_version = PF_KEY_V2;
3238 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3239 hdr->sadb_msg_satype = satype;
3240 hdr->sadb_msg_len = size / sizeof(uint64_t);
3241 hdr->sadb_msg_errno = 0;
3242 hdr->sadb_msg_reserved = 0;
3243 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3244 hdr->sadb_msg_pid = 0;
3245
3246 /* SA */
3247 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3248 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3249 sa->sadb_sa_exttype = SADB_EXT_SA;
3250 sa->sadb_sa_spi = x->id.spi;
3251 sa->sadb_sa_replay = 0;
3252 sa->sadb_sa_state = 0;
3253 sa->sadb_sa_auth = 0;
3254 sa->sadb_sa_encrypt = 0;
3255 sa->sadb_sa_flags = 0;
3256
3257 /* ADDRESS_SRC (old addr) */
3258 addr = (struct sadb_address*)
3259 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003260 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 (sizeof(struct sadb_address)+sockaddr_size)/
3262 sizeof(uint64_t);
3263 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3264 addr->sadb_address_proto = 0;
3265 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003266 addr->sadb_address_prefixlen =
3267 pfkey_sockaddr_fill(&x->props.saddr, 0,
3268 (struct sockaddr *) (addr + 1),
3269 x->props.family);
3270 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 BUG();
3272
3273 /* NAT_T_SPORT (old port) */
3274 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3275 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3276 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3277 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3278 n_port->sadb_x_nat_t_port_reserved = 0;
3279
3280 /* ADDRESS_DST (new addr) */
3281 addr = (struct sadb_address*)
3282 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003283 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 (sizeof(struct sadb_address)+sockaddr_size)/
3285 sizeof(uint64_t);
3286 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3287 addr->sadb_address_proto = 0;
3288 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003289 addr->sadb_address_prefixlen =
3290 pfkey_sockaddr_fill(ipaddr, 0,
3291 (struct sockaddr *) (addr + 1),
3292 x->props.family);
3293 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 BUG();
3295
3296 /* NAT_T_DPORT (new port) */
3297 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3298 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3299 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3300 n_port->sadb_x_nat_t_port_port = sport;
3301 n_port->sadb_x_nat_t_port_reserved = 0;
3302
3303 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3304}
3305
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003306#ifdef CONFIG_NET_KEY_MIGRATE
3307static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3308 struct xfrm_selector *sel)
3309{
3310 struct sadb_address *addr;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003311 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3312 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3313 addr->sadb_address_exttype = type;
3314 addr->sadb_address_proto = sel->proto;
3315 addr->sadb_address_reserved = 0;
3316
3317 switch (type) {
3318 case SADB_EXT_ADDRESS_SRC:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003319 addr->sadb_address_prefixlen = sel->prefixlen_s;
3320 pfkey_sockaddr_fill(&sel->saddr, 0,
3321 (struct sockaddr *)(addr + 1),
3322 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003323 break;
3324 case SADB_EXT_ADDRESS_DST:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003325 addr->sadb_address_prefixlen = sel->prefixlen_d;
3326 pfkey_sockaddr_fill(&sel->daddr, 0,
3327 (struct sockaddr *)(addr + 1),
3328 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003329 break;
3330 default:
3331 return -EINVAL;
3332 }
3333
3334 return 0;
3335}
3336
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003337
3338static int set_sadb_kmaddress(struct sk_buff *skb, struct xfrm_kmaddress *k)
3339{
3340 struct sadb_x_kmaddress *kma;
3341 u8 *sa;
3342 int family = k->family;
3343 int socklen = pfkey_sockaddr_len(family);
3344 int size_req;
3345
3346 size_req = (sizeof(struct sadb_x_kmaddress) +
3347 pfkey_sockaddr_pair_size(family));
3348
3349 kma = (struct sadb_x_kmaddress *)skb_put(skb, size_req);
3350 memset(kma, 0, size_req);
3351 kma->sadb_x_kmaddress_len = size_req / 8;
3352 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3353 kma->sadb_x_kmaddress_reserved = k->reserved;
3354
3355 sa = (u8 *)(kma + 1);
3356 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3357 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3358 return -EINVAL;
3359
3360 return 0;
3361}
3362
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003363static int set_ipsecrequest(struct sk_buff *skb,
3364 uint8_t proto, uint8_t mode, int level,
3365 uint32_t reqid, uint8_t family,
3366 xfrm_address_t *src, xfrm_address_t *dst)
3367{
3368 struct sadb_x_ipsecrequest *rq;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003369 u8 *sa;
3370 int socklen = pfkey_sockaddr_len(family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003371 int size_req;
3372
3373 size_req = sizeof(struct sadb_x_ipsecrequest) +
3374 pfkey_sockaddr_pair_size(family);
3375
3376 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3377 memset(rq, 0, size_req);
3378 rq->sadb_x_ipsecrequest_len = size_req;
3379 rq->sadb_x_ipsecrequest_proto = proto;
3380 rq->sadb_x_ipsecrequest_mode = mode;
3381 rq->sadb_x_ipsecrequest_level = level;
3382 rq->sadb_x_ipsecrequest_reqid = reqid;
3383
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003384 sa = (u8 *) (rq + 1);
3385 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3386 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003387 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003388
3389 return 0;
3390}
3391#endif
3392
3393#ifdef CONFIG_NET_KEY_MIGRATE
3394static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003395 struct xfrm_migrate *m, int num_bundles,
3396 struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003397{
3398 int i;
3399 int sasize_sel;
3400 int size = 0;
3401 int size_pol = 0;
3402 struct sk_buff *skb;
3403 struct sadb_msg *hdr;
3404 struct sadb_x_policy *pol;
3405 struct xfrm_migrate *mp;
3406
3407 if (type != XFRM_POLICY_TYPE_MAIN)
3408 return 0;
3409
3410 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3411 return -EINVAL;
3412
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003413 if (k != NULL) {
3414 /* addresses for KM */
3415 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3416 pfkey_sockaddr_pair_size(k->family));
3417 }
3418
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003419 /* selector */
3420 sasize_sel = pfkey_sockaddr_size(sel->family);
3421 if (!sasize_sel)
3422 return -EINVAL;
3423 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3424
3425 /* policy info */
3426 size_pol += sizeof(struct sadb_x_policy);
3427
3428 /* ipsecrequests */
3429 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3430 /* old locator pair */
3431 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3432 pfkey_sockaddr_pair_size(mp->old_family);
3433 /* new locator pair */
3434 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3435 pfkey_sockaddr_pair_size(mp->new_family);
3436 }
3437
3438 size += sizeof(struct sadb_msg) + size_pol;
3439
3440 /* alloc buffer */
3441 skb = alloc_skb(size, GFP_ATOMIC);
3442 if (skb == NULL)
3443 return -ENOMEM;
3444
3445 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3446 hdr->sadb_msg_version = PF_KEY_V2;
3447 hdr->sadb_msg_type = SADB_X_MIGRATE;
3448 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3449 hdr->sadb_msg_len = size / 8;
3450 hdr->sadb_msg_errno = 0;
3451 hdr->sadb_msg_reserved = 0;
3452 hdr->sadb_msg_seq = 0;
3453 hdr->sadb_msg_pid = 0;
3454
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003455 /* Addresses to be used by KM for negotiation, if ext is available */
3456 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
3457 return -EINVAL;
3458
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003459 /* selector src */
3460 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3461
3462 /* selector dst */
3463 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3464
3465 /* policy information */
3466 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3467 pol->sadb_x_policy_len = size_pol / 8;
3468 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3469 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3470 pol->sadb_x_policy_dir = dir + 1;
3471 pol->sadb_x_policy_id = 0;
3472 pol->sadb_x_policy_priority = 0;
3473
3474 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3475 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003476 int mode = pfkey_mode_from_xfrm(mp->mode);
3477 if (mode < 0)
Patrick McHardyd4782c32008-01-20 17:24:29 -08003478 goto err;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003479 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003480 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3481 mp->reqid, mp->old_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003482 &mp->old_saddr, &mp->old_daddr) < 0)
3483 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003484
3485 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003486 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003487 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3488 mp->reqid, mp->new_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003489 &mp->new_saddr, &mp->new_daddr) < 0)
3490 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003491 }
3492
3493 /* broadcast migrate message to sockets */
3494 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
3495
3496 return 0;
Patrick McHardyd4782c32008-01-20 17:24:29 -08003497
3498err:
3499 kfree_skb(skb);
3500 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003501}
3502#else
3503static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003504 struct xfrm_migrate *m, int num_bundles,
3505 struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003506{
3507 return -ENOPROTOOPT;
3508}
3509#endif
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511static int pfkey_sendmsg(struct kiocb *kiocb,
3512 struct socket *sock, struct msghdr *msg, size_t len)
3513{
3514 struct sock *sk = sock->sk;
3515 struct sk_buff *skb = NULL;
3516 struct sadb_msg *hdr = NULL;
3517 int err;
3518
3519 err = -EOPNOTSUPP;
3520 if (msg->msg_flags & MSG_OOB)
3521 goto out;
3522
3523 err = -EMSGSIZE;
3524 if ((unsigned)len > sk->sk_sndbuf - 32)
3525 goto out;
3526
3527 err = -ENOBUFS;
3528 skb = alloc_skb(len, GFP_KERNEL);
3529 if (skb == NULL)
3530 goto out;
3531
3532 err = -EFAULT;
3533 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3534 goto out;
3535
3536 hdr = pfkey_get_base_msg(skb, &err);
3537 if (!hdr)
3538 goto out;
3539
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003540 mutex_lock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 err = pfkey_process(sk, skb, hdr);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003542 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
3544out:
3545 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3546 err = 0;
3547 if (skb)
3548 kfree_skb(skb);
3549
3550 return err ? : len;
3551}
3552
3553static int pfkey_recvmsg(struct kiocb *kiocb,
3554 struct socket *sock, struct msghdr *msg, size_t len,
3555 int flags)
3556{
3557 struct sock *sk = sock->sk;
Timo Teras83321d62008-03-03 23:40:12 -08003558 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 struct sk_buff *skb;
3560 int copied, err;
3561
3562 err = -EINVAL;
3563 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3564 goto out;
3565
3566 msg->msg_namelen = 0;
3567 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3568 if (skb == NULL)
3569 goto out;
3570
3571 copied = skb->len;
3572 if (copied > len) {
3573 msg->msg_flags |= MSG_TRUNC;
3574 copied = len;
3575 }
3576
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003577 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3579 if (err)
3580 goto out_free;
3581
3582 sock_recv_timestamp(msg, sk, skb);
3583
3584 err = (flags & MSG_TRUNC) ? skb->len : copied;
3585
Timo Teras83321d62008-03-03 23:40:12 -08003586 if (pfk->dump.dump != NULL &&
3587 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3588 pfkey_do_dump(pfk);
3589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590out_free:
3591 skb_free_datagram(sk, skb);
3592out:
3593 return err;
3594}
3595
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003596static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 .family = PF_KEY,
3598 .owner = THIS_MODULE,
3599 /* Operations that make no sense on pfkey sockets. */
3600 .bind = sock_no_bind,
3601 .connect = sock_no_connect,
3602 .socketpair = sock_no_socketpair,
3603 .accept = sock_no_accept,
3604 .getname = sock_no_getname,
3605 .ioctl = sock_no_ioctl,
3606 .listen = sock_no_listen,
3607 .shutdown = sock_no_shutdown,
3608 .setsockopt = sock_no_setsockopt,
3609 .getsockopt = sock_no_getsockopt,
3610 .mmap = sock_no_mmap,
3611 .sendpage = sock_no_sendpage,
3612
3613 /* Now the operations that really occur. */
3614 .release = pfkey_release,
3615 .poll = datagram_poll,
3616 .sendmsg = pfkey_sendmsg,
3617 .recvmsg = pfkey_recvmsg,
3618};
3619
3620static struct net_proto_family pfkey_family_ops = {
3621 .family = PF_KEY,
3622 .create = pfkey_create,
3623 .owner = THIS_MODULE,
3624};
3625
3626#ifdef CONFIG_PROC_FS
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003627static int pfkey_seq_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003631 s = (struct sock *)v;
3632 if (v == SEQ_START_TOKEN)
3633 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3634 else
3635 seq_printf(f ,"%p %-6d %-6u %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 s,
3637 atomic_read(&s->sk_refcnt),
3638 atomic_read(&s->sk_rmem_alloc),
3639 atomic_read(&s->sk_wmem_alloc),
3640 sock_i_uid(s),
3641 sock_i_ino(s)
3642 );
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644}
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003645
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003646static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3647{
3648 struct sock *s;
3649 struct hlist_node *node;
3650 loff_t pos = *ppos;
3651
3652 read_lock(&pfkey_table_lock);
3653 if (pos == 0)
3654 return SEQ_START_TOKEN;
3655
3656 sk_for_each(s, node, &pfkey_table)
3657 if (pos-- == 1)
3658 return s;
3659
3660 return NULL;
3661}
3662
3663static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3664{
3665 ++*ppos;
3666 return (v == SEQ_START_TOKEN) ?
3667 sk_head(&pfkey_table) :
3668 sk_next((struct sock *)v);
3669}
3670
3671static void pfkey_seq_stop(struct seq_file *f, void *v)
3672{
3673 read_unlock(&pfkey_table_lock);
3674}
3675
3676static struct seq_operations pfkey_seq_ops = {
3677 .start = pfkey_seq_start,
3678 .next = pfkey_seq_next,
3679 .stop = pfkey_seq_stop,
3680 .show = pfkey_seq_show,
3681};
3682
3683static int pfkey_seq_open(struct inode *inode, struct file *file)
3684{
3685 return seq_open(file, &pfkey_seq_ops);
3686}
3687
3688static struct file_operations pfkey_proc_ops = {
3689 .open = pfkey_seq_open,
3690 .read = seq_read,
3691 .llseek = seq_lseek,
3692 .release = seq_release,
3693};
3694
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003695static int pfkey_init_proc(void)
3696{
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003697 struct proc_dir_entry *e;
3698
David S. Millerd9595a72008-02-26 22:20:44 -08003699 e = proc_net_fops_create(&init_net, "pfkey", 0, &pfkey_proc_ops);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003700 if (e == NULL)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003701 return -ENOMEM;
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003702
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003703 return 0;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003704}
3705
3706static void pfkey_exit_proc(void)
3707{
David S. Millerd9595a72008-02-26 22:20:44 -08003708 proc_net_remove(&init_net, "pfkey");
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003709}
3710#else
3711static inline int pfkey_init_proc(void)
3712{
3713 return 0;
3714}
3715
3716static inline void pfkey_exit_proc(void)
3717{
3718}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719#endif
3720
3721static struct xfrm_mgr pfkeyv2_mgr =
3722{
3723 .id = "pfkeyv2",
3724 .notify = pfkey_send_notify,
3725 .acquire = pfkey_send_acquire,
3726 .compile_policy = pfkey_compile_policy,
3727 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003728 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003729 .migrate = pfkey_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730};
3731
3732static void __exit ipsec_pfkey_exit(void)
3733{
3734 xfrm_unregister_km(&pfkeyv2_mgr);
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003735 pfkey_exit_proc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 sock_unregister(PF_KEY);
3737 proto_unregister(&key_proto);
3738}
3739
3740static int __init ipsec_pfkey_init(void)
3741{
3742 int err = proto_register(&key_proto, 0);
3743
3744 if (err != 0)
3745 goto out;
3746
3747 err = sock_register(&pfkey_family_ops);
3748 if (err != 0)
3749 goto out_unregister_key_proto;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003750 err = pfkey_init_proc();
3751 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 goto out_sock_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 err = xfrm_register_km(&pfkeyv2_mgr);
3754 if (err != 0)
3755 goto out_remove_proc_entry;
3756out:
3757 return err;
3758out_remove_proc_entry:
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003759 pfkey_exit_proc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760out_sock_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 sock_unregister(PF_KEY);
3762out_unregister_key_proto:
3763 proto_unregister(&key_proto);
3764 goto out;
3765}
3766
3767module_init(ipsec_pfkey_init);
3768module_exit(ipsec_pfkey_exit);
3769MODULE_LICENSE("GPL");
3770MODULE_ALIAS_NETPROTO(PF_KEY);