blob: 7b1e99b5b4d27eee3863be4ec0e889f315b0ac62 [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>
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080030#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/xfrm.h>
32
33#include <net/sock.h>
34
35#define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
36#define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
37
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080038static int pfkey_net_id;
39struct netns_pfkey {
40 /* List of all pfkey sockets. */
41 struct hlist_head table;
42 atomic_t socks_nr;
43};
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait);
45static DEFINE_RWLOCK(pfkey_table_lock);
46static atomic_t pfkey_table_users = ATOMIC_INIT(0);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048struct pfkey_sock {
49 /* struct sock must be the first member of struct pfkey_sock */
50 struct sock sk;
51 int registered;
52 int promisc;
Timo Teras83321d62008-03-03 23:40:12 -080053
54 struct {
55 uint8_t msg_version;
56 uint32_t msg_pid;
57 int (*dump)(struct pfkey_sock *sk);
58 void (*done)(struct pfkey_sock *sk);
59 union {
60 struct xfrm_policy_walk policy;
61 struct xfrm_state_walk state;
62 } u;
Herbert Xu12a169e2008-10-01 07:03:24 -070063 struct sk_buff *skb;
Timo Teras83321d62008-03-03 23:40:12 -080064 } dump;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
67static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
68{
69 return (struct pfkey_sock *)sk;
70}
71
Timo Teras83321d62008-03-03 23:40:12 -080072static int pfkey_can_dump(struct sock *sk)
73{
74 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
75 return 1;
76 return 0;
77}
78
Timo Teras05238202008-10-01 05:17:54 -070079static void pfkey_terminate_dump(struct pfkey_sock *pfk)
Timo Teras83321d62008-03-03 23:40:12 -080080{
Timo Teras05238202008-10-01 05:17:54 -070081 if (pfk->dump.dump) {
Herbert Xu12a169e2008-10-01 07:03:24 -070082 if (pfk->dump.skb) {
83 kfree_skb(pfk->dump.skb);
84 pfk->dump.skb = NULL;
85 }
Timo Teras05238202008-10-01 05:17:54 -070086 pfk->dump.done(pfk);
87 pfk->dump.dump = NULL;
88 pfk->dump.done = NULL;
89 }
Timo Teras83321d62008-03-03 23:40:12 -080090}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static void pfkey_sock_destruct(struct sock *sk)
93{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080094 struct net *net = sock_net(sk);
95 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
96
Timo Teras05238202008-10-01 05:17:54 -070097 pfkey_terminate_dump(pfkey_sk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 skb_queue_purge(&sk->sk_receive_queue);
99
100 if (!sock_flag(sk, SOCK_DEAD)) {
101 printk("Attempt to release alive pfkey socket: %p\n", sk);
102 return;
103 }
104
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700105 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
106 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800108 atomic_dec(&net_pfkey->socks_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111static void pfkey_table_grab(void)
112{
113 write_lock_bh(&pfkey_table_lock);
114
115 if (atomic_read(&pfkey_table_users)) {
116 DECLARE_WAITQUEUE(wait, current);
117
118 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
119 for(;;) {
120 set_current_state(TASK_UNINTERRUPTIBLE);
121 if (atomic_read(&pfkey_table_users) == 0)
122 break;
123 write_unlock_bh(&pfkey_table_lock);
124 schedule();
125 write_lock_bh(&pfkey_table_lock);
126 }
127
128 __set_current_state(TASK_RUNNING);
129 remove_wait_queue(&pfkey_table_wait, &wait);
130 }
131}
132
133static __inline__ void pfkey_table_ungrab(void)
134{
135 write_unlock_bh(&pfkey_table_lock);
136 wake_up(&pfkey_table_wait);
137}
138
139static __inline__ void pfkey_lock_table(void)
140{
141 /* read_lock() synchronizes us to pfkey_table_grab */
142
143 read_lock(&pfkey_table_lock);
144 atomic_inc(&pfkey_table_users);
145 read_unlock(&pfkey_table_lock);
146}
147
148static __inline__ void pfkey_unlock_table(void)
149{
150 if (atomic_dec_and_test(&pfkey_table_users))
151 wake_up(&pfkey_table_wait);
152}
153
154
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800155static const struct proto_ops pfkey_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157static void pfkey_insert(struct sock *sk)
158{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800159 struct net *net = sock_net(sk);
160 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 pfkey_table_grab();
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800163 sk_add_node(sk, &net_pfkey->table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 pfkey_table_ungrab();
165}
166
167static void pfkey_remove(struct sock *sk)
168{
169 pfkey_table_grab();
170 sk_del_node_init(sk);
171 pfkey_table_ungrab();
172}
173
174static struct proto key_proto = {
175 .name = "KEY",
176 .owner = THIS_MODULE,
177 .obj_size = sizeof(struct pfkey_sock),
178};
179
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700180static int pfkey_create(struct net *net, struct socket *sock, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800182 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct sock *sk;
184 int err;
185
186 if (!capable(CAP_NET_ADMIN))
187 return -EPERM;
188 if (sock->type != SOCK_RAW)
189 return -ESOCKTNOSUPPORT;
190 if (protocol != PF_KEY_V2)
191 return -EPROTONOSUPPORT;
192
193 err = -ENOMEM;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700194 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (sk == NULL)
196 goto out;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 sock->ops = &pfkey_ops;
199 sock_init_data(sock, sk);
200
201 sk->sk_family = PF_KEY;
202 sk->sk_destruct = pfkey_sock_destruct;
203
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800204 atomic_inc(&net_pfkey->socks_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 pfkey_insert(sk);
207
208 return 0;
209out:
210 return err;
211}
212
213static int pfkey_release(struct socket *sock)
214{
215 struct sock *sk = sock->sk;
216
217 if (!sk)
218 return 0;
219
220 pfkey_remove(sk);
221
222 sock_orphan(sk);
223 sock->sk = NULL;
224 skb_queue_purge(&sk->sk_write_queue);
225 sock_put(sk);
226
227 return 0;
228}
229
230static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
Al Virodd0fc662005-10-07 07:46:04 +0100231 gfp_t allocation, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 int err = -ENOBUFS;
234
235 sock_hold(sk);
236 if (*skb2 == NULL) {
237 if (atomic_read(&skb->users) != 1) {
238 *skb2 = skb_clone(skb, allocation);
239 } else {
240 *skb2 = skb;
241 atomic_inc(&skb->users);
242 }
243 }
244 if (*skb2 != NULL) {
245 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
246 skb_orphan(*skb2);
247 skb_set_owner_r(*skb2, sk);
248 skb_queue_tail(&sk->sk_receive_queue, *skb2);
249 sk->sk_data_ready(sk, (*skb2)->len);
250 *skb2 = NULL;
251 err = 0;
252 }
253 }
254 sock_put(sk);
255 return err;
256}
257
258/* Send SKB to all pfkey sockets matching selected criteria. */
259#define BROADCAST_ALL 0
260#define BROADCAST_ONE 1
261#define BROADCAST_REGISTERED 2
262#define BROADCAST_PROMISC_ONLY 4
Al Virodd0fc662005-10-07 07:46:04 +0100263static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800264 int broadcast_flags, struct sock *one_sk,
265 struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800267 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct sock *sk;
269 struct hlist_node *node;
270 struct sk_buff *skb2 = NULL;
271 int err = -ESRCH;
272
273 /* XXX Do we need something like netlink_overrun? I think
274 * XXX PF_KEY socket apps will not mind current behavior.
275 */
276 if (!skb)
277 return -ENOMEM;
278
279 pfkey_lock_table();
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800280 sk_for_each(sk, node, &net_pfkey->table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct pfkey_sock *pfk = pfkey_sk(sk);
282 int err2;
283
284 /* Yes, it means that if you are meant to receive this
285 * pfkey message you receive it twice as promiscuous
286 * socket.
287 */
288 if (pfk->promisc)
289 pfkey_broadcast_one(skb, &skb2, allocation, sk);
290
291 /* the exact target will be processed later */
292 if (sk == one_sk)
293 continue;
294 if (broadcast_flags != BROADCAST_ALL) {
295 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
296 continue;
297 if ((broadcast_flags & BROADCAST_REGISTERED) &&
298 !pfk->registered)
299 continue;
300 if (broadcast_flags & BROADCAST_ONE)
301 continue;
302 }
303
304 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
305
306 /* Error is cleare after succecful sending to at least one
307 * registered KM */
308 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
309 err = err2;
310 }
311 pfkey_unlock_table();
312
313 if (one_sk != NULL)
314 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
315
Wei Yongjun6f961062009-02-25 00:31:04 +0000316 kfree_skb(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 kfree_skb(skb);
318 return err;
319}
320
Timo Teras05238202008-10-01 05:17:54 -0700321static int pfkey_do_dump(struct pfkey_sock *pfk)
322{
Herbert Xu12a169e2008-10-01 07:03:24 -0700323 struct sadb_msg *hdr;
Timo Teras05238202008-10-01 05:17:54 -0700324 int rc;
325
326 rc = pfk->dump.dump(pfk);
327 if (rc == -ENOBUFS)
328 return 0;
329
Herbert Xu12a169e2008-10-01 07:03:24 -0700330 if (pfk->dump.skb) {
331 if (!pfkey_can_dump(&pfk->sk))
332 return 0;
333
334 hdr = (struct sadb_msg *) pfk->dump.skb->data;
335 hdr->sadb_msg_seq = 0;
336 hdr->sadb_msg_errno = rc;
337 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800338 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -0700339 pfk->dump.skb = NULL;
340 }
341
Timo Teras05238202008-10-01 05:17:54 -0700342 pfkey_terminate_dump(pfk);
343 return rc;
344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
347{
348 *new = *orig;
349}
350
351static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
352{
353 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
354 struct sadb_msg *hdr;
355
356 if (!skb)
357 return -ENOBUFS;
358
359 /* Woe be to the platform trying to support PFKEY yet
360 * having normal errnos outside the 1-255 range, inclusive.
361 */
362 err = -err;
363 if (err == ERESTARTSYS ||
364 err == ERESTARTNOHAND ||
365 err == ERESTARTNOINTR)
366 err = EINTR;
367 if (err >= 512)
368 err = EINVAL;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800369 BUG_ON(err <= 0 || err >= 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
372 pfkey_hdr_dup(hdr, orig);
373 hdr->sadb_msg_errno = (uint8_t) err;
374 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
375 sizeof(uint64_t));
376
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800377 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 return 0;
380}
381
382static u8 sadb_ext_min_len[] = {
383 [SADB_EXT_RESERVED] = (u8) 0,
384 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
385 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
386 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
387 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
388 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
389 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
390 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
391 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
392 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
393 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
394 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
395 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
396 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
397 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
398 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
399 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
400 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
401 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
402 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
403 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
404 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
405 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
406 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
Trent Jaegerdf718372005-12-13 23:12:27 -0800407 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -0700408 [SADB_X_EXT_KMADDRESS] = (u8) sizeof(struct sadb_x_kmaddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409};
410
411/* Verify sadb_address_{len,prefixlen} against sa_family. */
412static int verify_address_len(void *p)
413{
414 struct sadb_address *sp = p;
415 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
416 struct sockaddr_in *sin;
417#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
418 struct sockaddr_in6 *sin6;
419#endif
420 int len;
421
422 switch (addr->sa_family) {
423 case AF_INET:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700424 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (sp->sadb_address_len != len ||
426 sp->sadb_address_prefixlen > 32)
427 return -EINVAL;
428 break;
429#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
430 case AF_INET6:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700431 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (sp->sadb_address_len != len ||
433 sp->sadb_address_prefixlen > 128)
434 return -EINVAL;
435 break;
436#endif
437 default:
438 /* It is user using kernel to keep track of security
439 * associations for another protocol, such as
440 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
441 * lengths.
442 *
443 * XXX Actually, association/policy database is not yet
444 * XXX able to cope with arbitrary sockaddr families.
445 * XXX When it can, remove this -EINVAL. -DaveM
446 */
447 return -EINVAL;
448 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 return 0;
452}
453
Trent Jaegerdf718372005-12-13 23:12:27 -0800454static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx)
455{
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700456 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
457 sec_ctx->sadb_x_ctx_len,
458 sizeof(uint64_t));
Trent Jaegerdf718372005-12-13 23:12:27 -0800459}
460
461static inline int verify_sec_ctx_len(void *p)
462{
463 struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p;
Stephen Rothwell298bb622007-10-30 23:57:05 -0700464 int len = sec_ctx->sadb_x_ctx_len;
Trent Jaegerdf718372005-12-13 23:12:27 -0800465
Stephen Rothwell298bb622007-10-30 23:57:05 -0700466 if (len > PAGE_SIZE)
Trent Jaegerdf718372005-12-13 23:12:27 -0800467 return -EINVAL;
468
469 len = pfkey_sec_ctx_len(sec_ctx);
470
471 if (sec_ctx->sadb_x_sec_len != len)
472 return -EINVAL;
473
474 return 0;
475}
476
477static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx)
478{
479 struct xfrm_user_sec_ctx *uctx = NULL;
480 int ctx_size = sec_ctx->sadb_x_ctx_len;
481
482 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
483
484 if (!uctx)
485 return NULL;
486
487 uctx->len = pfkey_sec_ctx_len(sec_ctx);
488 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
489 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
490 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
491 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
492 memcpy(uctx + 1, sec_ctx + 1,
493 uctx->ctx_len);
494
495 return uctx;
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static int present_and_same_family(struct sadb_address *src,
499 struct sadb_address *dst)
500{
501 struct sockaddr *s_addr, *d_addr;
502
503 if (!src || !dst)
504 return 0;
505
506 s_addr = (struct sockaddr *)(src + 1);
507 d_addr = (struct sockaddr *)(dst + 1);
508 if (s_addr->sa_family != d_addr->sa_family)
509 return 0;
510 if (s_addr->sa_family != AF_INET
511#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
512 && s_addr->sa_family != AF_INET6
513#endif
514 )
515 return 0;
516
517 return 1;
518}
519
520static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
521{
522 char *p = (char *) hdr;
523 int len = skb->len;
524
525 len -= sizeof(*hdr);
526 p += sizeof(*hdr);
527 while (len > 0) {
528 struct sadb_ext *ehdr = (struct sadb_ext *) p;
529 uint16_t ext_type;
530 int ext_len;
531
532 ext_len = ehdr->sadb_ext_len;
533 ext_len *= sizeof(uint64_t);
534 ext_type = ehdr->sadb_ext_type;
535 if (ext_len < sizeof(uint64_t) ||
536 ext_len > len ||
537 ext_type == SADB_EXT_RESERVED)
538 return -EINVAL;
539
540 if (ext_type <= SADB_EXT_MAX) {
541 int min = (int) sadb_ext_min_len[ext_type];
542 if (ext_len < min)
543 return -EINVAL;
544 if (ext_hdrs[ext_type-1] != NULL)
545 return -EINVAL;
546 if (ext_type == SADB_EXT_ADDRESS_SRC ||
547 ext_type == SADB_EXT_ADDRESS_DST ||
548 ext_type == SADB_EXT_ADDRESS_PROXY ||
549 ext_type == SADB_X_EXT_NAT_T_OA) {
550 if (verify_address_len(p))
551 return -EINVAL;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900552 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800553 if (ext_type == SADB_X_EXT_SEC_CTX) {
554 if (verify_sec_ctx_len(p))
555 return -EINVAL;
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 ext_hdrs[ext_type-1] = p;
558 }
559 p += ext_len;
560 len -= ext_len;
561 }
562
563 return 0;
564}
565
566static uint16_t
567pfkey_satype2proto(uint8_t satype)
568{
569 switch (satype) {
570 case SADB_SATYPE_UNSPEC:
571 return IPSEC_PROTO_ANY;
572 case SADB_SATYPE_AH:
573 return IPPROTO_AH;
574 case SADB_SATYPE_ESP:
575 return IPPROTO_ESP;
576 case SADB_X_SATYPE_IPCOMP:
577 return IPPROTO_COMP;
578 break;
579 default:
580 return 0;
581 }
582 /* NOTREACHED */
583}
584
585static uint8_t
586pfkey_proto2satype(uint16_t proto)
587{
588 switch (proto) {
589 case IPPROTO_AH:
590 return SADB_SATYPE_AH;
591 case IPPROTO_ESP:
592 return SADB_SATYPE_ESP;
593 case IPPROTO_COMP:
594 return SADB_X_SATYPE_IPCOMP;
595 break;
596 default:
597 return 0;
598 }
599 /* NOTREACHED */
600}
601
602/* BTW, this scheme means that there is no way with PFKEY2 sockets to
603 * say specifically 'just raw sockets' as we encode them as 255.
604 */
605
606static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
607{
608 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
609}
610
611static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
612{
613 return (proto ? proto : IPSEC_PROTO_ANY);
614}
615
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900616static inline int pfkey_sockaddr_len(sa_family_t family)
617{
618 switch (family) {
619 case AF_INET:
620 return sizeof(struct sockaddr_in);
621#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
622 case AF_INET6:
623 return sizeof(struct sockaddr_in6);
624#endif
625 }
626 return 0;
627}
628
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900629static
630int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900632 switch (sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 case AF_INET:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900634 xaddr->a4 =
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900635 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return AF_INET;
637#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
638 case AF_INET6:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900639 memcpy(xaddr->a6,
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900640 &((struct sockaddr_in6 *)sa)->sin6_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 sizeof(struct in6_addr));
642 return AF_INET6;
643#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900645 return 0;
646}
647
648static
649int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr, xfrm_address_t *xaddr)
650{
651 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
652 xaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800655static struct xfrm_state *pfkey_xfrm_state_lookup(struct net *net, struct sadb_msg *hdr, void **ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct sadb_sa *sa;
658 struct sadb_address *addr;
659 uint16_t proto;
660 unsigned short family;
661 xfrm_address_t *xaddr;
662
663 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
664 if (sa == NULL)
665 return NULL;
666
667 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
668 if (proto == 0)
669 return NULL;
670
671 /* sadb_address_len should be checked by caller */
672 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
673 if (addr == NULL)
674 return NULL;
675
676 family = ((struct sockaddr *)(addr + 1))->sa_family;
677 switch (family) {
678 case AF_INET:
679 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
680 break;
681#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
682 case AF_INET6:
683 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
684 break;
685#endif
686 default:
687 xaddr = NULL;
688 }
689
690 if (!xaddr)
691 return NULL;
692
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800693 return xfrm_state_lookup(net, xaddr, sa->sadb_sa_spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698static int
699pfkey_sockaddr_size(sa_family_t family)
700{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900701 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700704static inline int pfkey_mode_from_xfrm(int mode)
705{
706 switch(mode) {
707 case XFRM_MODE_TRANSPORT:
708 return IPSEC_MODE_TRANSPORT;
709 case XFRM_MODE_TUNNEL:
710 return IPSEC_MODE_TUNNEL;
711 case XFRM_MODE_BEET:
712 return IPSEC_MODE_BEET;
713 default:
714 return -1;
715 }
716}
717
718static inline int pfkey_mode_to_xfrm(int mode)
719{
720 switch(mode) {
721 case IPSEC_MODE_ANY: /*XXX*/
722 case IPSEC_MODE_TRANSPORT:
723 return XFRM_MODE_TRANSPORT;
724 case IPSEC_MODE_TUNNEL:
725 return XFRM_MODE_TUNNEL;
726 case IPSEC_MODE_BEET:
727 return XFRM_MODE_BEET;
728 default:
729 return -1;
730 }
731}
732
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900733static unsigned int pfkey_sockaddr_fill(xfrm_address_t *xaddr, __be16 port,
734 struct sockaddr *sa,
735 unsigned short family)
736{
737 switch (family) {
738 case AF_INET:
739 {
740 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
741 sin->sin_family = AF_INET;
742 sin->sin_port = port;
743 sin->sin_addr.s_addr = xaddr->a4;
744 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
745 return 32;
746 }
747#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
748 case AF_INET6:
749 {
750 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
751 sin6->sin6_family = AF_INET6;
752 sin6->sin6_port = port;
753 sin6->sin6_flowinfo = 0;
754 ipv6_addr_copy(&sin6->sin6_addr, (struct in6_addr *)xaddr->a6);
755 sin6->sin6_scope_id = 0;
756 return 128;
757 }
758#endif
759 }
760 return 0;
761}
762
Herbert Xu050f0092007-10-09 13:31:47 -0700763static struct sk_buff *__pfkey_xfrm_state2msg(struct xfrm_state *x,
764 int add_keys, int hsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct sk_buff *skb;
767 struct sadb_msg *hdr;
768 struct sadb_sa *sa;
769 struct sadb_lifetime *lifetime;
770 struct sadb_address *addr;
771 struct sadb_key *key;
772 struct sadb_x_sa2 *sa2;
Trent Jaegerdf718372005-12-13 23:12:27 -0800773 struct sadb_x_sec_ctx *sec_ctx;
774 struct xfrm_sec_ctx *xfrm_ctx;
775 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int size;
777 int auth_key_size = 0;
778 int encrypt_key_size = 0;
779 int sockaddr_size;
780 struct xfrm_encap_tmpl *natt = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700781 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 /* address family check */
784 sockaddr_size = pfkey_sockaddr_size(x->props.family);
785 if (!sockaddr_size)
786 return ERR_PTR(-EINVAL);
787
788 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
789 key(AE), (identity(SD),) (sensitivity)> */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900790 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 sizeof(struct sadb_lifetime) +
792 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
793 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900794 sizeof(struct sadb_address)*2 +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 sockaddr_size*2 +
796 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800797
798 if ((xfrm_ctx = x->security)) {
799 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
800 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
801 }
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /* identity & sensitivity */
YOSHIFUJI Hideaki81b302a2008-04-28 03:17:38 +0900804 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr, x->props.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 size += sizeof(struct sadb_address) + sockaddr_size;
806
807 if (add_keys) {
808 if (x->aalg && x->aalg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900809 auth_key_size =
810 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 size += sizeof(struct sadb_key) + auth_key_size;
812 }
813 if (x->ealg && x->ealg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900814 encrypt_key_size =
815 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 size += sizeof(struct sadb_key) + encrypt_key_size;
817 }
818 }
819 if (x->encap)
820 natt = x->encap;
821
822 if (natt && natt->encap_type) {
823 size += sizeof(struct sadb_x_nat_t_type);
824 size += sizeof(struct sadb_x_nat_t_port);
825 size += sizeof(struct sadb_x_nat_t_port);
826 }
827
828 skb = alloc_skb(size + 16, GFP_ATOMIC);
829 if (skb == NULL)
830 return ERR_PTR(-ENOBUFS);
831
832 /* call should fill header later */
833 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
834 memset(hdr, 0, size); /* XXX do we need this ? */
835 hdr->sadb_msg_len = size / sizeof(uint64_t);
836
837 /* sa */
838 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
839 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
840 sa->sadb_sa_exttype = SADB_EXT_SA;
841 sa->sadb_sa_spi = x->id.spi;
842 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700843 switch (x->km.state) {
844 case XFRM_STATE_VALID:
845 sa->sadb_sa_state = x->km.dying ?
846 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
847 break;
848 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700850 break;
851 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700853 break;
854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 sa->sadb_sa_auth = 0;
856 if (x->aalg) {
857 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
858 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
859 }
860 sa->sadb_sa_encrypt = 0;
861 BUG_ON(x->ealg && x->calg);
862 if (x->ealg) {
863 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
864 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
865 }
866 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
867 if (x->calg) {
868 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
869 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
870 }
871
872 sa->sadb_sa_flags = 0;
873 if (x->props.flags & XFRM_STATE_NOECN)
874 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
875 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
876 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700877 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
878 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 /* hard time */
881 if (hsc & 2) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900882 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 sizeof(struct sadb_lifetime));
884 lifetime->sadb_lifetime_len =
885 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
886 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
887 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
888 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
889 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
890 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
891 }
892 /* soft time */
893 if (hsc & 1) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900894 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 sizeof(struct sadb_lifetime));
896 lifetime->sadb_lifetime_len =
897 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
898 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
899 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
900 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
901 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
902 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
903 }
904 /* current time */
905 lifetime = (struct sadb_lifetime *) skb_put(skb,
906 sizeof(struct sadb_lifetime));
907 lifetime->sadb_lifetime_len =
908 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
909 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
910 lifetime->sadb_lifetime_allocations = x->curlft.packets;
911 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
912 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
913 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
914 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900915 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900917 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 (sizeof(struct sadb_address)+sockaddr_size)/
919 sizeof(uint64_t);
920 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900921 /* "if the ports are non-zero, then the sadb_address_proto field,
922 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900924 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900927 addr->sadb_address_prefixlen =
928 pfkey_sockaddr_fill(&x->props.saddr, 0,
929 (struct sockaddr *) (addr + 1),
930 x->props.family);
931 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 BUG();
933
934 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900935 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900937 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 (sizeof(struct sadb_address)+sockaddr_size)/
939 sizeof(uint64_t);
940 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900941 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900944 addr->sadb_address_prefixlen =
945 pfkey_sockaddr_fill(&x->id.daddr, 0,
946 (struct sockaddr *) (addr + 1),
947 x->props.family);
948 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 BUG();
950
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900951 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr,
952 x->props.family)) {
953 addr = (struct sadb_address*) skb_put(skb,
954 sizeof(struct sadb_address)+sockaddr_size);
955 addr->sadb_address_len =
956 (sizeof(struct sadb_address)+sockaddr_size)/
957 sizeof(uint64_t);
958 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
959 addr->sadb_address_proto =
960 pfkey_proto_from_xfrm(x->sel.proto);
961 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
962 addr->sadb_address_reserved = 0;
963
964 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
965 (struct sockaddr *) (addr + 1),
966 x->props.family);
967 }
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* auth key */
970 if (add_keys && auth_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900971 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 sizeof(struct sadb_key)+auth_key_size);
973 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
974 sizeof(uint64_t);
975 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
976 key->sadb_key_bits = x->aalg->alg_key_len;
977 key->sadb_key_reserved = 0;
978 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
979 }
980 /* encrypt key */
981 if (add_keys && encrypt_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900982 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 sizeof(struct sadb_key)+encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900984 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 encrypt_key_size) / sizeof(uint64_t);
986 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
987 key->sadb_key_bits = x->ealg->alg_key_len;
988 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900989 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 (x->ealg->alg_key_len+7)/8);
991 }
992
993 /* sa */
994 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
995 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
996 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700997 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
998 kfree_skb(skb);
999 return ERR_PTR(-EINVAL);
1000 }
1001 sa2->sadb_x_sa2_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 sa2->sadb_x_sa2_reserved1 = 0;
1003 sa2->sadb_x_sa2_reserved2 = 0;
1004 sa2->sadb_x_sa2_sequence = 0;
1005 sa2->sadb_x_sa2_reqid = x->props.reqid;
1006
1007 if (natt && natt->encap_type) {
1008 struct sadb_x_nat_t_type *n_type;
1009 struct sadb_x_nat_t_port *n_port;
1010
1011 /* type */
1012 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
1013 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
1014 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
1015 n_type->sadb_x_nat_t_type_type = natt->encap_type;
1016 n_type->sadb_x_nat_t_type_reserved[0] = 0;
1017 n_type->sadb_x_nat_t_type_reserved[1] = 0;
1018 n_type->sadb_x_nat_t_type_reserved[2] = 0;
1019
1020 /* source 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_SPORT;
1024 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
1025 n_port->sadb_x_nat_t_port_reserved = 0;
1026
1027 /* dest port */
1028 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
1029 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1030 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
1031 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
1032 n_port->sadb_x_nat_t_port_reserved = 0;
1033 }
1034
Trent Jaegerdf718372005-12-13 23:12:27 -08001035 /* security context */
1036 if (xfrm_ctx) {
1037 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1038 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1039 sec_ctx->sadb_x_sec_len =
1040 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1041 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1042 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1043 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1044 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1045 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1046 xfrm_ctx->ctx_len);
1047 }
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return skb;
1050}
1051
Herbert Xu050f0092007-10-09 13:31:47 -07001052
1053static inline struct sk_buff *pfkey_xfrm_state2msg(struct xfrm_state *x)
1054{
1055 struct sk_buff *skb;
1056
Herbert Xu050f0092007-10-09 13:31:47 -07001057 skb = __pfkey_xfrm_state2msg(x, 1, 3);
Herbert Xu050f0092007-10-09 13:31:47 -07001058
1059 return skb;
1060}
1061
1062static inline struct sk_buff *pfkey_xfrm_state2msg_expire(struct xfrm_state *x,
1063 int hsc)
1064{
1065 return __pfkey_xfrm_state2msg(x, 0, hsc);
1066}
1067
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001068static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
1069 struct sadb_msg *hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 void **ext_hdrs)
1071{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001072 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 struct sadb_lifetime *lifetime;
1074 struct sadb_sa *sa;
1075 struct sadb_key *key;
Trent Jaegerdf718372005-12-13 23:12:27 -08001076 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 uint16_t proto;
1078 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
1082 if (!sa ||
1083 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1084 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1085 return ERR_PTR(-EINVAL);
1086 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1087 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1088 return ERR_PTR(-EINVAL);
1089 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1090 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1091 return ERR_PTR(-EINVAL);
1092 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1093 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1094 return ERR_PTR(-EINVAL);
1095
1096 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1097 if (proto == 0)
1098 return ERR_PTR(-EINVAL);
1099
1100 /* default error is no buffer space */
1101 err = -ENOBUFS;
1102
1103 /* RFC2367:
1104
1105 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1106 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1107 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1108 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1109 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1110 not true.
1111
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001112 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1114 */
1115 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1116 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1117 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1118 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1119 return ERR_PTR(-EINVAL);
1120 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1121 if (key != NULL &&
1122 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1123 ((key->sadb_key_bits+7) / 8 == 0 ||
1124 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1125 return ERR_PTR(-EINVAL);
1126 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1127 if (key != NULL &&
1128 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1129 ((key->sadb_key_bits+7) / 8 == 0 ||
1130 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1131 return ERR_PTR(-EINVAL);
1132
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001133 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (x == NULL)
1135 return ERR_PTR(-ENOBUFS);
1136
1137 x->id.proto = proto;
1138 x->id.spi = sa->sadb_sa_spi;
1139 x->props.replay_window = sa->sadb_sa_replay;
1140 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1141 x->props.flags |= XFRM_STATE_NOECN;
1142 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1143 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001144 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1145 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
1148 if (lifetime != NULL) {
1149 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1150 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1151 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1152 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1153 }
1154 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
1155 if (lifetime != NULL) {
1156 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1157 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1158 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1159 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1160 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001161
1162 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
1163 if (sec_ctx != NULL) {
1164 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1165
1166 if (!uctx)
1167 goto out;
1168
1169 err = security_xfrm_state_alloc(x, uctx);
1170 kfree(uctx);
1171
1172 if (err)
1173 goto out;
1174 }
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1177 if (sa->sadb_sa_auth) {
1178 int keysize = 0;
1179 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1180 if (!a) {
1181 err = -ENOSYS;
1182 goto out;
1183 }
1184 if (key)
1185 keysize = (key->sadb_key_bits + 7) / 8;
1186 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1187 if (!x->aalg)
1188 goto out;
1189 strcpy(x->aalg->alg_name, a->name);
1190 x->aalg->alg_key_len = 0;
1191 if (key) {
1192 x->aalg->alg_key_len = key->sadb_key_bits;
1193 memcpy(x->aalg->alg_key, key+1, keysize);
1194 }
1195 x->props.aalgo = sa->sadb_sa_auth;
1196 /* x->algo.flags = sa->sadb_sa_flags; */
1197 }
1198 if (sa->sadb_sa_encrypt) {
1199 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1200 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1201 if (!a) {
1202 err = -ENOSYS;
1203 goto out;
1204 }
1205 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1206 if (!x->calg)
1207 goto out;
1208 strcpy(x->calg->alg_name, a->name);
1209 x->props.calgo = sa->sadb_sa_encrypt;
1210 } else {
1211 int keysize = 0;
1212 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1213 if (!a) {
1214 err = -ENOSYS;
1215 goto out;
1216 }
1217 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1218 if (key)
1219 keysize = (key->sadb_key_bits + 7) / 8;
1220 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1221 if (!x->ealg)
1222 goto out;
1223 strcpy(x->ealg->alg_name, a->name);
1224 x->ealg->alg_key_len = 0;
1225 if (key) {
1226 x->ealg->alg_key_len = key->sadb_key_bits;
1227 memcpy(x->ealg->alg_key, key+1, keysize);
1228 }
1229 x->props.ealgo = sa->sadb_sa_encrypt;
1230 }
1231 }
1232 /* x->algo.flags = sa->sadb_sa_flags; */
1233
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001234 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 &x->props.saddr);
1236 if (!x->props.family) {
1237 err = -EAFNOSUPPORT;
1238 goto out;
1239 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001240 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 &x->id.daddr);
1242
1243 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1244 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001245 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1246 if (mode < 0) {
1247 err = -EINVAL;
1248 goto out;
1249 }
1250 x->props.mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 x->props.reqid = sa2->sadb_x_sa2_reqid;
1252 }
1253
1254 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1255 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1256
1257 /* Nobody uses this, but we try. */
1258 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1259 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1260 }
1261
Kazunori MIYAZAWA4da51052008-05-21 13:26:11 -07001262 if (!x->sel.family)
Joy Latten4a4b6272007-08-02 19:25:43 -07001263 x->sel.family = x->props.family;
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1266 struct sadb_x_nat_t_type* n_type;
1267 struct xfrm_encap_tmpl *natt;
1268
1269 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1270 if (!x->encap)
1271 goto out;
1272
1273 natt = x->encap;
1274 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1275 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1276
1277 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1278 struct sadb_x_nat_t_port* n_port =
1279 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1280 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1281 }
1282 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1283 struct sadb_x_nat_t_port* n_port =
1284 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1285 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1286 }
Timo Terasa8d694c2009-01-25 20:49:14 -08001287 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
Herbert Xu72cb6962005-06-20 13:18:08 -07001290 err = xfrm_init_state(x);
1291 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return x;
1296
1297out:
1298 x->km.state = XFRM_STATE_DEAD;
1299 xfrm_state_put(x);
1300 return ERR_PTR(err);
1301}
1302
1303static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1304{
1305 return -EOPNOTSUPP;
1306}
1307
1308static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1309{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001310 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 struct sk_buff *resp_skb;
1312 struct sadb_x_sa2 *sa2;
1313 struct sadb_address *saddr, *daddr;
1314 struct sadb_msg *out_hdr;
Herbert Xu658b2192007-10-09 13:29:52 -07001315 struct sadb_spirange *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 struct xfrm_state *x = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001317 int mode;
Herbert Xu658b2192007-10-09 13:29:52 -07001318 int err;
1319 u32 min_spi, max_spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 u32 reqid;
1321 u8 proto;
1322 unsigned short family;
1323 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1324
1325 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1326 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1327 return -EINVAL;
1328
1329 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1330 if (proto == 0)
1331 return -EINVAL;
1332
1333 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001334 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1335 if (mode < 0)
1336 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 reqid = sa2->sadb_x_sa2_reqid;
1338 } else {
1339 mode = 0;
1340 reqid = 0;
1341 }
1342
1343 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1344 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1345
1346 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1347 switch (family) {
1348 case AF_INET:
1349 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1350 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1351 break;
1352#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1353 case AF_INET6:
1354 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1355 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1356 break;
1357#endif
1358 }
1359
1360 if (hdr->sadb_msg_seq) {
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001361 x = xfrm_find_acq_byseq(net, hdr->sadb_msg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1363 xfrm_state_put(x);
1364 x = NULL;
1365 }
1366 }
1367
1368 if (!x)
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001369 x = xfrm_find_acq(net, mode, reqid, proto, xdaddr, xsaddr, 1, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 if (x == NULL)
1372 return -ENOENT;
1373
Herbert Xu658b2192007-10-09 13:29:52 -07001374 min_spi = 0x100;
1375 max_spi = 0x0fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Herbert Xu658b2192007-10-09 13:29:52 -07001377 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1378 if (range) {
1379 min_spi = range->sadb_spirange_min;
1380 max_spi = range->sadb_spirange_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Herbert Xu658b2192007-10-09 13:29:52 -07001382
1383 err = xfrm_alloc_spi(x, min_spi, max_spi);
Herbert Xu050f0092007-10-09 13:31:47 -07001384 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 if (IS_ERR(resp_skb)) {
1387 xfrm_state_put(x);
1388 return PTR_ERR(resp_skb);
1389 }
1390
1391 out_hdr = (struct sadb_msg *) resp_skb->data;
1392 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1393 out_hdr->sadb_msg_type = SADB_GETSPI;
1394 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1395 out_hdr->sadb_msg_errno = 0;
1396 out_hdr->sadb_msg_reserved = 0;
1397 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1398 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1399
1400 xfrm_state_put(x);
1401
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001402 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 return 0;
1405}
1406
1407static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1408{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001409 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 struct xfrm_state *x;
1411
1412 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1413 return -EOPNOTSUPP;
1414
1415 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1416 return 0;
1417
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001418 x = xfrm_find_acq_byseq(net, hdr->sadb_msg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (x == NULL)
1420 return 0;
1421
1422 spin_lock_bh(&x->lock);
1423 if (x->km.state == XFRM_STATE_ACQ) {
1424 x->km.state = XFRM_STATE_ERROR;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001425 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427 spin_unlock_bh(&x->lock);
1428 xfrm_state_put(x);
1429 return 0;
1430}
1431
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001432static inline int event2poltype(int event)
1433{
1434 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001435 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001436 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001437 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001438 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001439 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001440 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001441 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001442 // return SADB_X_SPDEXPIRE;
1443 default:
1444 printk("pfkey: Unknown policy event %d\n", event);
1445 break;
1446 }
1447
1448 return 0;
1449}
1450
1451static inline int event2keytype(int event)
1452{
1453 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001454 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001455 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001456 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001457 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001458 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001459 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001460 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001461 return SADB_EXPIRE;
1462 default:
1463 printk("pfkey: Unknown SA event %d\n", event);
1464 break;
1465 }
1466
1467 return 0;
1468}
1469
1470/* ADD/UPD/DEL */
1471static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1472{
1473 struct sk_buff *skb;
1474 struct sadb_msg *hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001475
Herbert Xu050f0092007-10-09 13:31:47 -07001476 skb = pfkey_xfrm_state2msg(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001477
1478 if (IS_ERR(skb))
1479 return PTR_ERR(skb);
1480
1481 hdr = (struct sadb_msg *) skb->data;
1482 hdr->sadb_msg_version = PF_KEY_V2;
1483 hdr->sadb_msg_type = event2keytype(c->event);
1484 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1485 hdr->sadb_msg_errno = 0;
1486 hdr->sadb_msg_reserved = 0;
1487 hdr->sadb_msg_seq = c->seq;
1488 hdr->sadb_msg_pid = c->pid;
1489
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001490 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001491
1492 return 0;
1493}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1496{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001497 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 struct xfrm_state *x;
1499 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001500 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001502 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (IS_ERR(x))
1504 return PTR_ERR(x);
1505
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001506 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (hdr->sadb_msg_type == SADB_ADD)
1508 err = xfrm_state_add(x);
1509 else
1510 err = xfrm_state_update(x);
1511
Joy Lattenab5f5e82007-09-17 11:51:22 -07001512 xfrm_audit_state_add(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001513 audit_get_loginuid(current),
1514 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (err < 0) {
1517 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001518 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001519 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001522 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001523 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001524 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001525 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001526 c.seq = hdr->sadb_msg_seq;
1527 c.pid = hdr->sadb_msg_pid;
1528 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001529out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001530 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001531 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
1534static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1535{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001536 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001538 struct km_event c;
1539 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 if (!ext_hdrs[SADB_EXT_SA-1] ||
1542 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1543 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1544 return -EINVAL;
1545
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001546 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (x == NULL)
1548 return -ESRCH;
1549
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001550 if ((err = security_xfrm_state_delete(x)))
1551 goto out;
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001554 err = -EPERM;
1555 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
Joy Latten161a09e2006-11-27 13:11:54 -06001557
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001558 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001559
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001560 if (err < 0)
1561 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001562
1563 c.seq = hdr->sadb_msg_seq;
1564 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001565 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001566 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001567out:
Joy Lattenab5f5e82007-09-17 11:51:22 -07001568 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001569 audit_get_loginuid(current),
1570 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 xfrm_state_put(x);
1572
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001573 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
1576static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1577{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001578 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 __u8 proto;
1580 struct sk_buff *out_skb;
1581 struct sadb_msg *out_hdr;
1582 struct xfrm_state *x;
1583
1584 if (!ext_hdrs[SADB_EXT_SA-1] ||
1585 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1586 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1587 return -EINVAL;
1588
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001589 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (x == NULL)
1591 return -ESRCH;
1592
Herbert Xu050f0092007-10-09 13:31:47 -07001593 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 proto = x->id.proto;
1595 xfrm_state_put(x);
1596 if (IS_ERR(out_skb))
1597 return PTR_ERR(out_skb);
1598
1599 out_hdr = (struct sadb_msg *) out_skb->data;
1600 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Charles Hardin435000b2007-11-22 19:35:15 +08001601 out_hdr->sadb_msg_type = SADB_GET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1603 out_hdr->sadb_msg_errno = 0;
1604 out_hdr->sadb_msg_reserved = 0;
1605 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1606 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001607 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 return 0;
1610}
1611
Randy Dunlap00fa0232005-10-04 22:43:04 -07001612static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001613 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 struct sk_buff *skb;
1616 struct sadb_msg *hdr;
1617 int len, auth_len, enc_len, i;
1618
1619 auth_len = xfrm_count_auth_supported();
1620 if (auth_len) {
1621 auth_len *= sizeof(struct sadb_alg);
1622 auth_len += sizeof(struct sadb_supported);
1623 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 enc_len = xfrm_count_enc_supported();
1626 if (enc_len) {
1627 enc_len *= sizeof(struct sadb_alg);
1628 enc_len += sizeof(struct sadb_supported);
1629 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 len = enc_len + auth_len + sizeof(struct sadb_msg);
1632
1633 skb = alloc_skb(len + 16, allocation);
1634 if (!skb)
1635 goto out_put_algs;
1636
1637 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1638 pfkey_hdr_dup(hdr, orig);
1639 hdr->sadb_msg_errno = 0;
1640 hdr->sadb_msg_len = len / sizeof(uint64_t);
1641
1642 if (auth_len) {
1643 struct sadb_supported *sp;
1644 struct sadb_alg *ap;
1645
1646 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1647 ap = (struct sadb_alg *) (sp + 1);
1648
1649 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1650 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1651
1652 for (i = 0; ; i++) {
1653 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1654 if (!aalg)
1655 break;
1656 if (aalg->available)
1657 *ap++ = aalg->desc;
1658 }
1659 }
1660
1661 if (enc_len) {
1662 struct sadb_supported *sp;
1663 struct sadb_alg *ap;
1664
1665 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1666 ap = (struct sadb_alg *) (sp + 1);
1667
1668 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1669 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1670
1671 for (i = 0; ; i++) {
1672 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1673 if (!ealg)
1674 break;
1675 if (ealg->available)
1676 *ap++ = ealg->desc;
1677 }
1678 }
1679
1680out_put_algs:
1681 return skb;
1682}
1683
1684static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1685{
1686 struct pfkey_sock *pfk = pfkey_sk(sk);
1687 struct sk_buff *supp_skb;
1688
1689 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1690 return -EINVAL;
1691
1692 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1693 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1694 return -EEXIST;
1695 pfk->registered |= (1<<hdr->sadb_msg_satype);
1696 }
1697
1698 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1701 if (!supp_skb) {
1702 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1703 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1704
1705 return -ENOBUFS;
1706 }
1707
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001708 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 return 0;
1711}
1712
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001713static int key_notify_sa_flush(struct km_event *c)
1714{
1715 struct sk_buff *skb;
1716 struct sadb_msg *hdr;
1717
1718 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1719 if (!skb)
1720 return -ENOBUFS;
1721 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf088672005-06-18 22:44:00 -07001722 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001723 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001724 hdr->sadb_msg_seq = c->seq;
1725 hdr->sadb_msg_pid = c->pid;
1726 hdr->sadb_msg_version = PF_KEY_V2;
1727 hdr->sadb_msg_errno = (uint8_t) 0;
1728 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1729
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001730 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001731
1732 return 0;
1733}
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1736{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001737 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 unsigned proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001739 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06001740 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001741 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1744 if (proto == 0)
1745 return -EINVAL;
1746
Al Viro0c11b942008-01-10 04:20:52 -05001747 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04001748 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06001749 audit_info.secid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001750 err = xfrm_state_flush(net, proto, &audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -04001751 if (err)
1752 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001753 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001754 c.seq = hdr->sadb_msg_seq;
1755 c.pid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001756 c.event = XFRM_MSG_FLUSHSA;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001757 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001758 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 return 0;
1761}
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1764{
Timo Teras83321d62008-03-03 23:40:12 -08001765 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 struct sk_buff *out_skb;
1767 struct sadb_msg *out_hdr;
1768
Timo Teras83321d62008-03-03 23:40:12 -08001769 if (!pfkey_can_dump(&pfk->sk))
1770 return -ENOBUFS;
1771
Herbert Xu050f0092007-10-09 13:31:47 -07001772 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (IS_ERR(out_skb))
1774 return PTR_ERR(out_skb);
1775
1776 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08001777 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 out_hdr->sadb_msg_type = SADB_DUMP;
1779 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1780 out_hdr->sadb_msg_errno = 0;
1781 out_hdr->sadb_msg_reserved = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07001782 out_hdr->sadb_msg_seq = count + 1;
Timo Teras83321d62008-03-03 23:40:12 -08001783 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
Herbert Xu12a169e2008-10-01 07:03:24 -07001784
1785 if (pfk->dump.skb)
1786 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001787 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07001788 pfk->dump.skb = out_skb;
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
1791}
1792
Timo Teras83321d62008-03-03 23:40:12 -08001793static int pfkey_dump_sa(struct pfkey_sock *pfk)
1794{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001795 struct net *net = sock_net(&pfk->sk);
1796 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08001797}
1798
1799static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1800{
1801 xfrm_state_walk_done(&pfk->dump.u.state);
1802}
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1805{
1806 u8 proto;
Timo Teras83321d62008-03-03 23:40:12 -08001807 struct pfkey_sock *pfk = pfkey_sk(sk);
1808
1809 if (pfk->dump.dump != NULL)
1810 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1813 if (proto == 0)
1814 return -EINVAL;
1815
Timo Teras83321d62008-03-03 23:40:12 -08001816 pfk->dump.msg_version = hdr->sadb_msg_version;
1817 pfk->dump.msg_pid = hdr->sadb_msg_pid;
1818 pfk->dump.dump = pfkey_dump_sa;
1819 pfk->dump.done = pfkey_dump_sa_done;
1820 xfrm_state_walk_init(&pfk->dump.u.state, proto);
Timo Teras4c563f72008-02-28 21:31:08 -08001821
Timo Teras83321d62008-03-03 23:40:12 -08001822 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
1825static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1826{
1827 struct pfkey_sock *pfk = pfkey_sk(sk);
1828 int satype = hdr->sadb_msg_satype;
1829
1830 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1831 /* XXX we mangle packet... */
1832 hdr->sadb_msg_errno = 0;
1833 if (satype != 0 && satype != 1)
1834 return -EINVAL;
1835 pfk->promisc = satype;
1836 }
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001837 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return 0;
1839}
1840
1841static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1842{
1843 int i;
1844 u32 reqid = *(u32*)ptr;
1845
1846 for (i=0; i<xp->xfrm_nr; i++) {
1847 if (xp->xfrm_vec[i].reqid == reqid)
1848 return -EEXIST;
1849 }
1850 return 0;
1851}
1852
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001853static u32 gen_reqid(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Timo Teras4c563f72008-02-28 21:31:08 -08001855 struct xfrm_policy_walk walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 u32 start;
Timo Teras4c563f72008-02-28 21:31:08 -08001857 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1859
1860 start = reqid;
1861 do {
1862 ++reqid;
1863 if (reqid == 0)
1864 reqid = IPSEC_MANUAL_REQID_MAX+1;
Timo Teras4c563f72008-02-28 21:31:08 -08001865 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001866 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
Timo Teras4c563f72008-02-28 21:31:08 -08001867 xfrm_policy_walk_done(&walk);
1868 if (rc != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return reqid;
1870 } while (reqid != start);
1871 return 0;
1872}
1873
1874static int
1875parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1876{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001877 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001879 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1882 return -ELOOP;
1883
1884 if (rq->sadb_x_ipsecrequest_mode == 0)
1885 return -EINVAL;
1886
1887 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001888 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1889 return -EINVAL;
1890 t->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1892 t->optional = 1;
1893 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1894 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1895 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1896 t->reqid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001897 if (!t->reqid && !(t->reqid = gen_reqid(net)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return -ENOBUFS;
1899 }
1900
1901 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001902 if (t->mode == XFRM_MODE_TUNNEL) {
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001903 u8 *sa = (u8 *) (rq + 1);
1904 int family, socklen;
1905
1906 family = pfkey_sockaddr_extract((struct sockaddr *)sa,
1907 &t->saddr);
1908 if (!family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return -EINVAL;
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001910
1911 socklen = pfkey_sockaddr_len(family);
1912 if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen),
1913 &t->id.daddr) != family)
1914 return -EINVAL;
1915 t->encap_family = family;
Miika Komu2718aa72006-11-30 16:41:50 -08001916 } else
1917 t->encap_family = xp->family;
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 /* No way to set this via kame pfkey */
Herbert Xuc5d18e92008-04-22 00:46:42 -07001920 t->allalgs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 xp->xfrm_nr++;
1922 return 0;
1923}
1924
1925static int
1926parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1927{
1928 int err;
1929 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1930 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1931
1932 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1933 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1934 return err;
1935 len -= rq->sadb_x_ipsecrequest_len;
1936 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1937 }
1938 return 0;
1939}
1940
Trent Jaegerdf718372005-12-13 23:12:27 -08001941static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1942{
1943 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1944
1945 if (xfrm_ctx) {
1946 int len = sizeof(struct sadb_x_sec_ctx);
1947 len += xfrm_ctx->ctx_len;
1948 return PFKEY_ALIGN8(len);
1949 }
1950 return 0;
1951}
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1954{
Miika Komu2718aa72006-11-30 16:41:50 -08001955 struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08001957 int socklen = 0;
1958 int i;
1959
1960 for (i=0; i<xp->xfrm_nr; i++) {
1961 t = xp->xfrm_vec + i;
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09001962 socklen += pfkey_sockaddr_len(t->encap_family);
Miika Komu2718aa72006-11-30 16:41:50 -08001963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 return sizeof(struct sadb_msg) +
1966 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001967 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 (sockaddr_size * 2) +
1969 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08001970 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1971 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08001972 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
1975static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1976{
1977 struct sk_buff *skb;
1978 int size;
1979
1980 size = pfkey_xfrm_policy2msg_size(xp);
1981
1982 skb = alloc_skb(size + 16, GFP_ATOMIC);
1983 if (skb == NULL)
1984 return ERR_PTR(-ENOBUFS);
1985
1986 return skb;
1987}
1988
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001989static int pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 struct sadb_msg *hdr;
1992 struct sadb_address *addr;
1993 struct sadb_lifetime *lifetime;
1994 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08001995 struct sadb_x_sec_ctx *sec_ctx;
1996 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 int i;
1998 int size;
1999 int sockaddr_size = pfkey_sockaddr_size(xp->family);
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002000 int socklen = pfkey_sockaddr_len(xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 size = pfkey_xfrm_policy2msg_size(xp);
2003
2004 /* call should fill header later */
2005 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2006 memset(hdr, 0, size); /* XXX do we need this ? */
2007
2008 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002009 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002011 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 (sizeof(struct sadb_address)+sockaddr_size)/
2013 sizeof(uint64_t);
2014 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2015 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2016 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2017 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002018 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2019 xp->selector.sport,
2020 (struct sockaddr *) (addr + 1),
2021 xp->family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 BUG();
2023
2024 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002025 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 sizeof(struct sadb_address)+sockaddr_size);
2027 addr->sadb_address_len =
2028 (sizeof(struct sadb_address)+sockaddr_size)/
2029 sizeof(uint64_t);
2030 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2031 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002032 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002034
2035 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2036 (struct sockaddr *) (addr + 1),
2037 xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 /* hard time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002040 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 sizeof(struct sadb_lifetime));
2042 lifetime->sadb_lifetime_len =
2043 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2044 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2045 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2046 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2047 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2048 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2049 /* soft time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002050 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 sizeof(struct sadb_lifetime));
2052 lifetime->sadb_lifetime_len =
2053 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2054 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2055 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2056 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2057 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2058 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2059 /* current time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002060 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 sizeof(struct sadb_lifetime));
2062 lifetime->sadb_lifetime_len =
2063 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2064 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2065 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2066 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2067 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2068 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2069
2070 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2071 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2072 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2073 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2074 if (xp->action == XFRM_POLICY_ALLOW) {
2075 if (xp->xfrm_nr)
2076 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2077 else
2078 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2079 }
2080 pol->sadb_x_policy_dir = dir+1;
2081 pol->sadb_x_policy_id = xp->index;
2082 pol->sadb_x_policy_priority = xp->priority;
2083
2084 for (i=0; i<xp->xfrm_nr; i++) {
2085 struct sadb_x_ipsecrequest *rq;
2086 struct xfrm_tmpl *t = xp->xfrm_vec + i;
2087 int req_size;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002088 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 req_size = sizeof(struct sadb_x_ipsecrequest);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002091 if (t->mode == XFRM_MODE_TUNNEL) {
2092 socklen = pfkey_sockaddr_len(t->encap_family);
2093 req_size += socklen * 2;
2094 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 size -= 2*socklen;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 rq = (void*)skb_put(skb, req_size);
2098 pol->sadb_x_policy_len += req_size/8;
2099 memset(rq, 0, sizeof(*rq));
2100 rq->sadb_x_ipsecrequest_len = req_size;
2101 rq->sadb_x_ipsecrequest_proto = t->id.proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002102 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2103 return -EINVAL;
David S. Millerfefaa752007-04-17 21:48:10 -07002104 rq->sadb_x_ipsecrequest_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2106 if (t->reqid)
2107 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2108 if (t->optional)
2109 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2110 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002112 if (t->mode == XFRM_MODE_TUNNEL) {
2113 u8 *sa = (void *)(rq + 1);
2114 pfkey_sockaddr_fill(&t->saddr, 0,
2115 (struct sockaddr *)sa,
2116 t->encap_family);
2117 pfkey_sockaddr_fill(&t->id.daddr, 0,
2118 (struct sockaddr *) (sa + socklen),
2119 t->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
2121 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002122
2123 /* security context */
2124 if ((xfrm_ctx = xp->security)) {
2125 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2126
2127 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2128 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2129 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2130 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2131 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2132 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2133 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2134 xfrm_ctx->ctx_len);
2135 }
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 hdr->sadb_msg_len = size / sizeof(uint64_t);
2138 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002139
2140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002143static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2144{
2145 struct sk_buff *out_skb;
2146 struct sadb_msg *out_hdr;
2147 int err;
2148
2149 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2150 if (IS_ERR(out_skb)) {
2151 err = PTR_ERR(out_skb);
2152 goto out;
2153 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002154 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2155 if (err < 0)
2156 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002157
2158 out_hdr = (struct sadb_msg *) out_skb->data;
2159 out_hdr->sadb_msg_version = PF_KEY_V2;
2160
Herbert Xuf60f6b82005-06-18 22:44:37 -07002161 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002162 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2163 else
2164 out_hdr->sadb_msg_type = event2poltype(c->event);
2165 out_hdr->sadb_msg_errno = 0;
2166 out_hdr->sadb_msg_seq = c->seq;
2167 out_hdr->sadb_msg_pid = c->pid;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002168 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002169out:
2170 return 0;
2171
2172}
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2175{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002176 struct net *net = sock_net(sk);
Trent Jaegerdf718372005-12-13 23:12:27 -08002177 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 struct sadb_lifetime *lifetime;
2179 struct sadb_address *sa;
2180 struct sadb_x_policy *pol;
2181 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002182 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002183 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2186 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2187 !ext_hdrs[SADB_X_EXT_POLICY-1])
2188 return -EINVAL;
2189
2190 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2191 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2192 return -EINVAL;
2193 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2194 return -EINVAL;
2195
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002196 xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (xp == NULL)
2198 return -ENOBUFS;
2199
2200 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2201 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2202 xp->priority = pol->sadb_x_policy_priority;
2203
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002204 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2206 if (!xp->family) {
2207 err = -EINVAL;
2208 goto out;
2209 }
2210 xp->selector.family = xp->family;
2211 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2212 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2213 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2214 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002215 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002217 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2219 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2220
2221 /* Amusing, we set this twice. KAME apps appear to set same value
2222 * in both addresses.
2223 */
2224 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2225
2226 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2227 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002228 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Trent Jaegerdf718372005-12-13 23:12:27 -08002230 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2231 if (sec_ctx != NULL) {
2232 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2233
2234 if (!uctx) {
2235 err = -ENOBUFS;
2236 goto out;
2237 }
2238
Paul Moore03e1ad72008-04-12 19:07:52 -07002239 err = security_xfrm_policy_alloc(&xp->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002240 kfree(uctx);
2241
2242 if (err)
2243 goto out;
2244 }
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 xp->lft.soft_byte_limit = XFRM_INF;
2247 xp->lft.hard_byte_limit = XFRM_INF;
2248 xp->lft.soft_packet_limit = XFRM_INF;
2249 xp->lft.hard_packet_limit = XFRM_INF;
2250 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2251 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2252 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2253 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2254 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2255 }
2256 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2257 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2258 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2259 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2260 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2261 }
2262 xp->xfrm_nr = 0;
2263 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2264 (err = parse_ipsecrequests(xp, pol)) < 0)
2265 goto out;
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2268 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002269
Joy Lattenab5f5e82007-09-17 11:51:22 -07002270 xfrm_audit_policy_add(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002271 audit_get_loginuid(current),
2272 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06002273
Trent Jaegerdf718372005-12-13 23:12:27 -08002274 if (err)
2275 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002277 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002278 c.event = XFRM_MSG_UPDPOLICY;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002279 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07002280 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002282 c.seq = hdr->sadb_msg_seq;
2283 c.pid = hdr->sadb_msg_pid;
2284
2285 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return 0;
2288
2289out:
Herbert Xu12a169e2008-10-01 07:03:24 -07002290 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08002291 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return err;
2293}
2294
2295static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2296{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002297 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 int err;
2299 struct sadb_address *sa;
2300 struct sadb_x_policy *pol;
Paul Moore03e1ad72008-04-12 19:07:52 -07002301 struct xfrm_policy *xp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002303 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002304 struct sadb_x_sec_ctx *sec_ctx;
Brian Haley2db3e472008-04-24 20:38:31 -07002305 struct xfrm_sec_ctx *pol_ctx = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2308 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2309 !ext_hdrs[SADB_X_EXT_POLICY-1])
2310 return -EINVAL;
2311
2312 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2313 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2314 return -EINVAL;
2315
2316 memset(&sel, 0, sizeof(sel));
2317
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002318 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2320 sel.prefixlen_s = sa->sadb_address_prefixlen;
2321 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2322 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2323 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002324 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002326 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2328 sel.prefixlen_d = sa->sadb_address_prefixlen;
2329 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2330 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2331 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002332 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Trent Jaegerdf718372005-12-13 23:12:27 -08002334 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002335 if (sec_ctx != NULL) {
2336 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2337
2338 if (!uctx)
2339 return -ENOMEM;
2340
Paul Moore03e1ad72008-04-12 19:07:52 -07002341 err = security_xfrm_policy_alloc(&pol_ctx, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002342 kfree(uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002343 if (err)
2344 return err;
Brian Haley2db3e472008-04-24 20:38:31 -07002345 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002346
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002347 xp = xfrm_policy_bysel_ctx(net, XFRM_POLICY_TYPE_MAIN,
Paul Moore03e1ad72008-04-12 19:07:52 -07002348 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2349 1, &err);
2350 security_xfrm_policy_free(pol_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (xp == NULL)
2352 return -ENOENT;
2353
Joy Lattenab5f5e82007-09-17 11:51:22 -07002354 xfrm_audit_policy_delete(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002355 audit_get_loginuid(current),
2356 audit_get_sessionid(current), 0);
David S. Miller13fcfbb2007-02-12 13:53:54 -08002357
2358 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002359 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08002360
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002361 c.seq = hdr->sadb_msg_seq;
2362 c.pid = hdr->sadb_msg_pid;
Tobias Brunner1839faa2008-10-10 14:07:03 -07002363 c.data.byid = 0;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002364 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002365 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2366
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002367out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002368 xfrm_pol_put(xp);
2369 return err;
2370}
2371
2372static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2373{
2374 int err;
2375 struct sk_buff *out_skb;
2376 struct sadb_msg *out_hdr;
2377 err = 0;
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2380 if (IS_ERR(out_skb)) {
2381 err = PTR_ERR(out_skb);
2382 goto out;
2383 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002384 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2385 if (err < 0)
2386 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 out_hdr = (struct sadb_msg *) out_skb->data;
2389 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002390 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 out_hdr->sadb_msg_satype = 0;
2392 out_hdr->sadb_msg_errno = 0;
2393 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2394 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002395 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 err = 0;
2397
2398out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return err;
2400}
2401
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002402#ifdef CONFIG_NET_KEY_MIGRATE
2403static int pfkey_sockaddr_pair_size(sa_family_t family)
2404{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002405 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002406}
2407
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002408static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002409 xfrm_address_t *saddr, xfrm_address_t *daddr,
2410 u16 *family)
2411{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002412 int af, socklen;
2413
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002414 if (ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002415 return -EINVAL;
2416
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002417 af = pfkey_sockaddr_extract(sa, saddr);
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002418 if (!af)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002419 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002420
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002421 socklen = pfkey_sockaddr_len(af);
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002422 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002423 daddr) != af)
2424 return -EINVAL;
2425
2426 *family = af;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002427 return 0;
2428}
2429
2430static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2431 struct xfrm_migrate *m)
2432{
2433 int err;
2434 struct sadb_x_ipsecrequest *rq2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002435 int mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002436
2437 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2438 len < rq1->sadb_x_ipsecrequest_len)
2439 return -EINVAL;
2440
2441 /* old endoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002442 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
2443 rq1->sadb_x_ipsecrequest_len,
2444 &m->old_saddr, &m->old_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002445 &m->old_family);
2446 if (err)
2447 return err;
2448
2449 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2450 len -= rq1->sadb_x_ipsecrequest_len;
2451
2452 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2453 len < rq2->sadb_x_ipsecrequest_len)
2454 return -EINVAL;
2455
2456 /* new endpoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002457 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
2458 rq2->sadb_x_ipsecrequest_len,
2459 &m->new_saddr, &m->new_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002460 &m->new_family);
2461 if (err)
2462 return err;
2463
2464 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2465 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2466 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2467 return -EINVAL;
2468
2469 m->proto = rq1->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002470 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2471 return -EINVAL;
2472 m->mode = mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002473 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2474
2475 return ((int)(rq1->sadb_x_ipsecrequest_len +
2476 rq2->sadb_x_ipsecrequest_len));
2477}
2478
2479static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2480 struct sadb_msg *hdr, void **ext_hdrs)
2481{
2482 int i, len, ret, err = -EINVAL;
2483 u8 dir;
2484 struct sadb_address *sa;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002485 struct sadb_x_kmaddress *kma;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002486 struct sadb_x_policy *pol;
2487 struct sadb_x_ipsecrequest *rq;
2488 struct xfrm_selector sel;
2489 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002490 struct xfrm_kmaddress k;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002491
2492 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002493 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002494 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2495 err = -EINVAL;
2496 goto out;
2497 }
2498
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002499 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002500 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002501
2502 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2503 err = -EINVAL;
2504 goto out;
2505 }
2506
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002507 if (kma) {
2508 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2509 k.reserved = kma->sadb_x_kmaddress_reserved;
2510 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2511 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2512 &k.local, &k.remote, &k.family);
2513 if (ret < 0) {
2514 err = ret;
2515 goto out;
2516 }
2517 }
2518
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002519 dir = pol->sadb_x_policy_dir - 1;
2520 memset(&sel, 0, sizeof(sel));
2521
2522 /* set source address info of selector */
2523 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2524 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2525 sel.prefixlen_s = sa->sadb_address_prefixlen;
2526 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2527 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2528 if (sel.sport)
Al Viro582ee432007-07-26 17:33:39 +01002529 sel.sport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002530
2531 /* set destination address info of selector */
2532 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2533 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2534 sel.prefixlen_d = sa->sadb_address_prefixlen;
2535 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2536 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2537 if (sel.dport)
Al Viro582ee432007-07-26 17:33:39 +01002538 sel.dport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002539
2540 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2541
2542 /* extract ipsecrequests */
2543 i = 0;
2544 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2545
2546 while (len > 0 && i < XFRM_MAX_DEPTH) {
2547 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2548 if (ret < 0) {
2549 err = ret;
2550 goto out;
2551 } else {
2552 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2553 len -= ret;
2554 i++;
2555 }
2556 }
2557
2558 if (!i || len > 0) {
2559 err = -EINVAL;
2560 goto out;
2561 }
2562
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002563 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
2564 kma ? &k : NULL);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002565
2566 out:
2567 return err;
2568}
2569#else
2570static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2571 struct sadb_msg *hdr, void **ext_hdrs)
2572{
2573 return -ENOPROTOOPT;
2574}
2575#endif
2576
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2579{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002580 struct net *net = sock_net(sk);
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002581 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002582 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 struct sadb_x_policy *pol;
2584 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002585 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2588 return -EINVAL;
2589
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002590 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2591 if (dir >= XFRM_POLICY_MAX)
2592 return -EINVAL;
2593
Eric Paris215a2dd2007-03-07 16:01:45 -08002594 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002595 xp = xfrm_policy_byid(net, XFRM_POLICY_TYPE_MAIN, dir,
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08002596 pol->sadb_x_policy_id, delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 if (xp == NULL)
2598 return -ENOENT;
2599
Eric Paris215a2dd2007-03-07 16:01:45 -08002600 if (delete) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002601 xfrm_audit_policy_delete(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002602 audit_get_loginuid(current),
2603 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Eric Paris215a2dd2007-03-07 16:01:45 -08002605 if (err)
2606 goto out;
2607 c.seq = hdr->sadb_msg_seq;
2608 c.pid = hdr->sadb_msg_pid;
Herbert Xubf088672005-06-18 22:44:00 -07002609 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002610 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002611 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002612 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002613 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Eric Paris215a2dd2007-03-07 16:01:45 -08002616out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 xfrm_pol_put(xp);
2618 return err;
2619}
2620
2621static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2622{
Timo Teras83321d62008-03-03 23:40:12 -08002623 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 struct sk_buff *out_skb;
2625 struct sadb_msg *out_hdr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002626 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Timo Teras83321d62008-03-03 23:40:12 -08002628 if (!pfkey_can_dump(&pfk->sk))
2629 return -ENOBUFS;
2630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2632 if (IS_ERR(out_skb))
2633 return PTR_ERR(out_skb);
2634
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002635 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2636 if (err < 0)
2637 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08002640 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2642 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2643 out_hdr->sadb_msg_errno = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07002644 out_hdr->sadb_msg_seq = count + 1;
Timo Teras83321d62008-03-03 23:40:12 -08002645 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
Herbert Xu12a169e2008-10-01 07:03:24 -07002646
2647 if (pfk->dump.skb)
2648 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002649 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07002650 pfk->dump.skb = out_skb;
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 return 0;
2653}
2654
Timo Teras83321d62008-03-03 23:40:12 -08002655static int pfkey_dump_sp(struct pfkey_sock *pfk)
2656{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002657 struct net *net = sock_net(&pfk->sk);
2658 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08002659}
2660
2661static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2662{
2663 xfrm_policy_walk_done(&pfk->dump.u.policy);
2664}
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2667{
Timo Teras83321d62008-03-03 23:40:12 -08002668 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Timo Teras83321d62008-03-03 23:40:12 -08002670 if (pfk->dump.dump != NULL)
2671 return -EBUSY;
Timo Teras4c563f72008-02-28 21:31:08 -08002672
Timo Teras83321d62008-03-03 23:40:12 -08002673 pfk->dump.msg_version = hdr->sadb_msg_version;
2674 pfk->dump.msg_pid = hdr->sadb_msg_pid;
2675 pfk->dump.dump = pfkey_dump_sp;
2676 pfk->dump.done = pfkey_dump_sp_done;
2677 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2678
2679 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002682static int key_notify_policy_flush(struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
2684 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002685 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002687 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 if (!skb_out)
2689 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002690 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002691 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002692 hdr->sadb_msg_seq = c->seq;
2693 hdr->sadb_msg_pid = c->pid;
2694 hdr->sadb_msg_version = PF_KEY_V2;
2695 hdr->sadb_msg_errno = (uint8_t) 0;
2696 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002697 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002698 return 0;
2699
2700}
2701
2702static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2703{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002704 struct net *net = sock_net(sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002705 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06002706 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04002707 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Al Viro0c11b942008-01-10 04:20:52 -05002709 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04002710 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06002711 audit_info.secid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002712 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -04002713 if (err)
2714 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002715 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002716 c.event = XFRM_MSG_FLUSHPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002717 c.pid = hdr->sadb_msg_pid;
2718 c.seq = hdr->sadb_msg_seq;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002719 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002720 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 return 0;
2723}
2724
2725typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2726 struct sadb_msg *hdr, void **ext_hdrs);
2727static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2728 [SADB_RESERVED] = pfkey_reserved,
2729 [SADB_GETSPI] = pfkey_getspi,
2730 [SADB_UPDATE] = pfkey_add,
2731 [SADB_ADD] = pfkey_add,
2732 [SADB_DELETE] = pfkey_delete,
2733 [SADB_GET] = pfkey_get,
2734 [SADB_ACQUIRE] = pfkey_acquire,
2735 [SADB_REGISTER] = pfkey_register,
2736 [SADB_EXPIRE] = NULL,
2737 [SADB_FLUSH] = pfkey_flush,
2738 [SADB_DUMP] = pfkey_dump,
2739 [SADB_X_PROMISC] = pfkey_promisc,
2740 [SADB_X_PCHANGE] = NULL,
2741 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2742 [SADB_X_SPDADD] = pfkey_spdadd,
2743 [SADB_X_SPDDELETE] = pfkey_spddelete,
2744 [SADB_X_SPDGET] = pfkey_spdget,
2745 [SADB_X_SPDACQUIRE] = NULL,
2746 [SADB_X_SPDDUMP] = pfkey_spddump,
2747 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2748 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2749 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002750 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751};
2752
2753static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2754{
2755 void *ext_hdrs[SADB_EXT_MAX];
2756 int err;
2757
2758 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002759 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2762 err = parse_exthdrs(skb, hdr, ext_hdrs);
2763 if (!err) {
2764 err = -EOPNOTSUPP;
2765 if (pfkey_funcs[hdr->sadb_msg_type])
2766 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2767 }
2768 return err;
2769}
2770
2771static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2772{
2773 struct sadb_msg *hdr = NULL;
2774
2775 if (skb->len < sizeof(*hdr)) {
2776 *errp = -EMSGSIZE;
2777 } else {
2778 hdr = (struct sadb_msg *) skb->data;
2779 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2780 hdr->sadb_msg_reserved != 0 ||
2781 (hdr->sadb_msg_type <= SADB_RESERVED ||
2782 hdr->sadb_msg_type > SADB_MAX)) {
2783 hdr = NULL;
2784 *errp = -EINVAL;
2785 } else if (hdr->sadb_msg_len != (skb->len /
2786 sizeof(uint64_t)) ||
2787 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2788 sizeof(uint64_t))) {
2789 hdr = NULL;
2790 *errp = -EMSGSIZE;
2791 } else {
2792 *errp = 0;
2793 }
2794 }
2795 return hdr;
2796}
2797
2798static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2799{
Herbert Xuf3980352007-12-19 23:44:29 -08002800 unsigned int id = d->desc.sadb_alg_id;
2801
2802 if (id >= sizeof(t->aalgos) * 8)
2803 return 0;
2804
2805 return (t->aalgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806}
2807
2808static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2809{
Herbert Xuf3980352007-12-19 23:44:29 -08002810 unsigned int id = d->desc.sadb_alg_id;
2811
2812 if (id >= sizeof(t->ealgos) * 8)
2813 return 0;
2814
2815 return (t->ealgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
2818static int count_ah_combs(struct xfrm_tmpl *t)
2819{
2820 int i, sz = 0;
2821
2822 for (i = 0; ; i++) {
2823 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2824 if (!aalg)
2825 break;
2826 if (aalg_tmpl_set(t, aalg) && aalg->available)
2827 sz += sizeof(struct sadb_comb);
2828 }
2829 return sz + sizeof(struct sadb_prop);
2830}
2831
2832static int count_esp_combs(struct xfrm_tmpl *t)
2833{
2834 int i, k, sz = 0;
2835
2836 for (i = 0; ; i++) {
2837 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2838 if (!ealg)
2839 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2842 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 for (k = 1; ; k++) {
2845 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2846 if (!aalg)
2847 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 if (aalg_tmpl_set(t, aalg) && aalg->available)
2850 sz += sizeof(struct sadb_comb);
2851 }
2852 }
2853 return sz + sizeof(struct sadb_prop);
2854}
2855
2856static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2857{
2858 struct sadb_prop *p;
2859 int i;
2860
2861 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2862 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2863 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2864 p->sadb_prop_replay = 32;
2865 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2866
2867 for (i = 0; ; i++) {
2868 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2869 if (!aalg)
2870 break;
2871
2872 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2873 struct sadb_comb *c;
2874 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2875 memset(c, 0, sizeof(*c));
2876 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2877 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2878 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2879 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2880 c->sadb_comb_hard_addtime = 24*60*60;
2881 c->sadb_comb_soft_addtime = 20*60*60;
2882 c->sadb_comb_hard_usetime = 8*60*60;
2883 c->sadb_comb_soft_usetime = 7*60*60;
2884 }
2885 }
2886}
2887
2888static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2889{
2890 struct sadb_prop *p;
2891 int i, k;
2892
2893 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2894 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2895 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2896 p->sadb_prop_replay = 32;
2897 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2898
2899 for (i=0; ; i++) {
2900 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2901 if (!ealg)
2902 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2905 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 for (k = 1; ; k++) {
2908 struct sadb_comb *c;
2909 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2910 if (!aalg)
2911 break;
2912 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2913 continue;
2914 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2915 memset(c, 0, sizeof(*c));
2916 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2917 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2918 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2919 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2920 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2921 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2922 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2923 c->sadb_comb_hard_addtime = 24*60*60;
2924 c->sadb_comb_soft_addtime = 20*60*60;
2925 c->sadb_comb_hard_usetime = 8*60*60;
2926 c->sadb_comb_soft_usetime = 7*60*60;
2927 }
2928 }
2929}
2930
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002931static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2932{
2933 return 0;
2934}
2935
2936static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
2938 struct sk_buff *out_skb;
2939 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002940 int hard;
2941 int hsc;
2942
Herbert Xubf088672005-06-18 22:44:00 -07002943 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002944 if (hard)
2945 hsc = 2;
2946 else
2947 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Herbert Xu050f0092007-10-09 13:31:47 -07002949 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (IS_ERR(out_skb))
2951 return PTR_ERR(out_skb);
2952
2953 out_hdr = (struct sadb_msg *) out_skb->data;
2954 out_hdr->sadb_msg_version = PF_KEY_V2;
2955 out_hdr->sadb_msg_type = SADB_EXPIRE;
2956 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2957 out_hdr->sadb_msg_errno = 0;
2958 out_hdr->sadb_msg_reserved = 0;
2959 out_hdr->sadb_msg_seq = 0;
2960 out_hdr->sadb_msg_pid = 0;
2961
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002962 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return 0;
2964}
2965
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002966static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2967{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002968 struct net *net = x ? xs_net(x) : c->net;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08002969 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
2970
2971 if (atomic_read(&net_pfkey->socks_nr) == 0)
Jamal Hadi Salim99c6f602008-06-10 14:25:34 -07002972 return 0;
2973
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002974 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002975 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002976 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002977 case XFRM_MSG_DELSA:
2978 case XFRM_MSG_NEWSA:
2979 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002980 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002981 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002982 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002983 case XFRM_MSG_NEWAE: /* not yet supported */
2984 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002985 default:
2986 printk("pfkey: Unknown SA event %d\n", c->event);
2987 break;
2988 }
2989
2990 return 0;
2991}
2992
2993static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2994{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002995 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
2996 return 0;
2997
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002998 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002999 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003000 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003001 case XFRM_MSG_DELPOLICY:
3002 case XFRM_MSG_NEWPOLICY:
3003 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003004 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003005 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003006 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3007 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003008 return key_notify_policy_flush(c);
3009 default:
3010 printk("pfkey: Unknown policy event %d\n", c->event);
3011 break;
3012 }
3013
3014 return 0;
3015}
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017static u32 get_acqseq(void)
3018{
3019 u32 res;
3020 static u32 acqseq;
3021 static DEFINE_SPINLOCK(acqseq_lock);
3022
3023 spin_lock_bh(&acqseq_lock);
3024 res = (++acqseq ? : ++acqseq);
3025 spin_unlock_bh(&acqseq_lock);
3026 return res;
3027}
3028
3029static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
3030{
3031 struct sk_buff *skb;
3032 struct sadb_msg *hdr;
3033 struct sadb_address *addr;
3034 struct sadb_x_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 int sockaddr_size;
3036 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003037 struct sadb_x_sec_ctx *sec_ctx;
3038 struct xfrm_sec_ctx *xfrm_ctx;
3039 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3042 if (!sockaddr_size)
3043 return -EINVAL;
3044
3045 size = sizeof(struct sadb_msg) +
3046 (sizeof(struct sadb_address) * 2) +
3047 (sockaddr_size * 2) +
3048 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 if (x->id.proto == IPPROTO_AH)
3051 size += count_ah_combs(t);
3052 else if (x->id.proto == IPPROTO_ESP)
3053 size += count_esp_combs(t);
3054
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003055 if ((xfrm_ctx = x->security)) {
3056 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3057 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3058 }
3059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 skb = alloc_skb(size + 16, GFP_ATOMIC);
3061 if (skb == NULL)
3062 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3065 hdr->sadb_msg_version = PF_KEY_V2;
3066 hdr->sadb_msg_type = SADB_ACQUIRE;
3067 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3068 hdr->sadb_msg_len = size / sizeof(uint64_t);
3069 hdr->sadb_msg_errno = 0;
3070 hdr->sadb_msg_reserved = 0;
3071 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3072 hdr->sadb_msg_pid = 0;
3073
3074 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003075 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003077 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 (sizeof(struct sadb_address)+sockaddr_size)/
3079 sizeof(uint64_t);
3080 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3081 addr->sadb_address_proto = 0;
3082 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003083 addr->sadb_address_prefixlen =
3084 pfkey_sockaddr_fill(&x->props.saddr, 0,
3085 (struct sockaddr *) (addr + 1),
3086 x->props.family);
3087 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003091 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 sizeof(struct sadb_address)+sockaddr_size);
3093 addr->sadb_address_len =
3094 (sizeof(struct sadb_address)+sockaddr_size)/
3095 sizeof(uint64_t);
3096 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3097 addr->sadb_address_proto = 0;
3098 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003099 addr->sadb_address_prefixlen =
3100 pfkey_sockaddr_fill(&x->id.daddr, 0,
3101 (struct sockaddr *) (addr + 1),
3102 x->props.family);
3103 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 BUG();
3105
3106 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3107 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3108 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3109 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3110 pol->sadb_x_policy_dir = dir+1;
3111 pol->sadb_x_policy_id = xp->index;
3112
3113 /* Set sadb_comb's. */
3114 if (x->id.proto == IPPROTO_AH)
3115 dump_ah_combs(skb, t);
3116 else if (x->id.proto == IPPROTO_ESP)
3117 dump_esp_combs(skb, t);
3118
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003119 /* security context */
3120 if (xfrm_ctx) {
3121 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3122 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3123 sec_ctx->sadb_x_sec_len =
3124 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3125 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3126 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3127 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3128 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3129 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3130 xfrm_ctx->ctx_len);
3131 }
3132
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003133 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134}
3135
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003136static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003137 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003139 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 struct xfrm_policy *xp;
3141 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003142 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003144 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 case AF_INET:
3146 if (opt != IP_IPSEC_POLICY) {
3147 *dir = -EOPNOTSUPP;
3148 return NULL;
3149 }
3150 break;
3151#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3152 case AF_INET6:
3153 if (opt != IPV6_IPSEC_POLICY) {
3154 *dir = -EOPNOTSUPP;
3155 return NULL;
3156 }
3157 break;
3158#endif
3159 default:
3160 *dir = -EINVAL;
3161 return NULL;
3162 }
3163
3164 *dir = -EINVAL;
3165
3166 if (len < sizeof(struct sadb_x_policy) ||
3167 pol->sadb_x_policy_len*8 > len ||
3168 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3169 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3170 return NULL;
3171
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003172 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if (xp == NULL) {
3174 *dir = -ENOBUFS;
3175 return NULL;
3176 }
3177
3178 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3179 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3180
3181 xp->lft.soft_byte_limit = XFRM_INF;
3182 xp->lft.hard_byte_limit = XFRM_INF;
3183 xp->lft.soft_packet_limit = XFRM_INF;
3184 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003185 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 xp->xfrm_nr = 0;
3188 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3189 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3190 goto out;
3191
Trent Jaegerdf718372005-12-13 23:12:27 -08003192 /* security context too */
3193 if (len >= (pol->sadb_x_policy_len*8 +
3194 sizeof(struct sadb_x_sec_ctx))) {
3195 char *p = (char *)pol;
3196 struct xfrm_user_sec_ctx *uctx;
3197
3198 p += pol->sadb_x_policy_len*8;
3199 sec_ctx = (struct sadb_x_sec_ctx *)p;
3200 if (len < pol->sadb_x_policy_len*8 +
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003201 sec_ctx->sadb_x_sec_len) {
3202 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003203 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003204 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003205 if ((*dir = verify_sec_ctx_len(p)))
3206 goto out;
3207 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
Paul Moore03e1ad72008-04-12 19:07:52 -07003208 *dir = security_xfrm_policy_alloc(&xp->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08003209 kfree(uctx);
3210
3211 if (*dir)
3212 goto out;
3213 }
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 *dir = pol->sadb_x_policy_dir-1;
3216 return xp;
3217
3218out:
Alexey Dobriyan70e90672008-11-06 23:08:37 -08003219 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08003220 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 return NULL;
3222}
3223
Al Viro5d36b182006-11-08 00:24:06 -08003224static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 struct sk_buff *skb;
3227 struct sadb_msg *hdr;
3228 struct sadb_sa *sa;
3229 struct sadb_address *addr;
3230 struct sadb_x_nat_t_port *n_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 int sockaddr_size;
3232 int size;
3233 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3234 struct xfrm_encap_tmpl *natt = NULL;
3235
3236 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3237 if (!sockaddr_size)
3238 return -EINVAL;
3239
3240 if (!satype)
3241 return -EINVAL;
3242
3243 if (!x->encap)
3244 return -EINVAL;
3245
3246 natt = x->encap;
3247
3248 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3249 *
3250 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3251 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3252 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 size = sizeof(struct sadb_msg) +
3255 sizeof(struct sadb_sa) +
3256 (sizeof(struct sadb_address) * 2) +
3257 (sockaddr_size * 2) +
3258 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 skb = alloc_skb(size + 16, GFP_ATOMIC);
3261 if (skb == NULL)
3262 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3265 hdr->sadb_msg_version = PF_KEY_V2;
3266 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3267 hdr->sadb_msg_satype = satype;
3268 hdr->sadb_msg_len = size / sizeof(uint64_t);
3269 hdr->sadb_msg_errno = 0;
3270 hdr->sadb_msg_reserved = 0;
3271 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3272 hdr->sadb_msg_pid = 0;
3273
3274 /* SA */
3275 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3276 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3277 sa->sadb_sa_exttype = SADB_EXT_SA;
3278 sa->sadb_sa_spi = x->id.spi;
3279 sa->sadb_sa_replay = 0;
3280 sa->sadb_sa_state = 0;
3281 sa->sadb_sa_auth = 0;
3282 sa->sadb_sa_encrypt = 0;
3283 sa->sadb_sa_flags = 0;
3284
3285 /* ADDRESS_SRC (old addr) */
3286 addr = (struct sadb_address*)
3287 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003288 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 (sizeof(struct sadb_address)+sockaddr_size)/
3290 sizeof(uint64_t);
3291 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3292 addr->sadb_address_proto = 0;
3293 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003294 addr->sadb_address_prefixlen =
3295 pfkey_sockaddr_fill(&x->props.saddr, 0,
3296 (struct sockaddr *) (addr + 1),
3297 x->props.family);
3298 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 BUG();
3300
3301 /* NAT_T_SPORT (old port) */
3302 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3303 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3304 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3305 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3306 n_port->sadb_x_nat_t_port_reserved = 0;
3307
3308 /* ADDRESS_DST (new addr) */
3309 addr = (struct sadb_address*)
3310 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003311 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 (sizeof(struct sadb_address)+sockaddr_size)/
3313 sizeof(uint64_t);
3314 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3315 addr->sadb_address_proto = 0;
3316 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003317 addr->sadb_address_prefixlen =
3318 pfkey_sockaddr_fill(ipaddr, 0,
3319 (struct sockaddr *) (addr + 1),
3320 x->props.family);
3321 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 BUG();
3323
3324 /* NAT_T_DPORT (new port) */
3325 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3326 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3327 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3328 n_port->sadb_x_nat_t_port_port = sport;
3329 n_port->sadb_x_nat_t_port_reserved = 0;
3330
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003331 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003334#ifdef CONFIG_NET_KEY_MIGRATE
3335static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3336 struct xfrm_selector *sel)
3337{
3338 struct sadb_address *addr;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003339 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3340 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3341 addr->sadb_address_exttype = type;
3342 addr->sadb_address_proto = sel->proto;
3343 addr->sadb_address_reserved = 0;
3344
3345 switch (type) {
3346 case SADB_EXT_ADDRESS_SRC:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003347 addr->sadb_address_prefixlen = sel->prefixlen_s;
3348 pfkey_sockaddr_fill(&sel->saddr, 0,
3349 (struct sockaddr *)(addr + 1),
3350 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003351 break;
3352 case SADB_EXT_ADDRESS_DST:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003353 addr->sadb_address_prefixlen = sel->prefixlen_d;
3354 pfkey_sockaddr_fill(&sel->daddr, 0,
3355 (struct sockaddr *)(addr + 1),
3356 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003357 break;
3358 default:
3359 return -EINVAL;
3360 }
3361
3362 return 0;
3363}
3364
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003365
3366static int set_sadb_kmaddress(struct sk_buff *skb, struct xfrm_kmaddress *k)
3367{
3368 struct sadb_x_kmaddress *kma;
3369 u8 *sa;
3370 int family = k->family;
3371 int socklen = pfkey_sockaddr_len(family);
3372 int size_req;
3373
3374 size_req = (sizeof(struct sadb_x_kmaddress) +
3375 pfkey_sockaddr_pair_size(family));
3376
3377 kma = (struct sadb_x_kmaddress *)skb_put(skb, size_req);
3378 memset(kma, 0, size_req);
3379 kma->sadb_x_kmaddress_len = size_req / 8;
3380 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3381 kma->sadb_x_kmaddress_reserved = k->reserved;
3382
3383 sa = (u8 *)(kma + 1);
3384 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3385 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3386 return -EINVAL;
3387
3388 return 0;
3389}
3390
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003391static int set_ipsecrequest(struct sk_buff *skb,
3392 uint8_t proto, uint8_t mode, int level,
3393 uint32_t reqid, uint8_t family,
3394 xfrm_address_t *src, xfrm_address_t *dst)
3395{
3396 struct sadb_x_ipsecrequest *rq;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003397 u8 *sa;
3398 int socklen = pfkey_sockaddr_len(family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003399 int size_req;
3400
3401 size_req = sizeof(struct sadb_x_ipsecrequest) +
3402 pfkey_sockaddr_pair_size(family);
3403
3404 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3405 memset(rq, 0, size_req);
3406 rq->sadb_x_ipsecrequest_len = size_req;
3407 rq->sadb_x_ipsecrequest_proto = proto;
3408 rq->sadb_x_ipsecrequest_mode = mode;
3409 rq->sadb_x_ipsecrequest_level = level;
3410 rq->sadb_x_ipsecrequest_reqid = reqid;
3411
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003412 sa = (u8 *) (rq + 1);
3413 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3414 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003415 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003416
3417 return 0;
3418}
3419#endif
3420
3421#ifdef CONFIG_NET_KEY_MIGRATE
3422static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003423 struct xfrm_migrate *m, int num_bundles,
3424 struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003425{
3426 int i;
3427 int sasize_sel;
3428 int size = 0;
3429 int size_pol = 0;
3430 struct sk_buff *skb;
3431 struct sadb_msg *hdr;
3432 struct sadb_x_policy *pol;
3433 struct xfrm_migrate *mp;
3434
3435 if (type != XFRM_POLICY_TYPE_MAIN)
3436 return 0;
3437
3438 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3439 return -EINVAL;
3440
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003441 if (k != NULL) {
3442 /* addresses for KM */
3443 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3444 pfkey_sockaddr_pair_size(k->family));
3445 }
3446
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003447 /* selector */
3448 sasize_sel = pfkey_sockaddr_size(sel->family);
3449 if (!sasize_sel)
3450 return -EINVAL;
3451 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3452
3453 /* policy info */
3454 size_pol += sizeof(struct sadb_x_policy);
3455
3456 /* ipsecrequests */
3457 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3458 /* old locator pair */
3459 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3460 pfkey_sockaddr_pair_size(mp->old_family);
3461 /* new locator pair */
3462 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3463 pfkey_sockaddr_pair_size(mp->new_family);
3464 }
3465
3466 size += sizeof(struct sadb_msg) + size_pol;
3467
3468 /* alloc buffer */
3469 skb = alloc_skb(size, GFP_ATOMIC);
3470 if (skb == NULL)
3471 return -ENOMEM;
3472
3473 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3474 hdr->sadb_msg_version = PF_KEY_V2;
3475 hdr->sadb_msg_type = SADB_X_MIGRATE;
3476 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3477 hdr->sadb_msg_len = size / 8;
3478 hdr->sadb_msg_errno = 0;
3479 hdr->sadb_msg_reserved = 0;
3480 hdr->sadb_msg_seq = 0;
3481 hdr->sadb_msg_pid = 0;
3482
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003483 /* Addresses to be used by KM for negotiation, if ext is available */
3484 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
3485 return -EINVAL;
3486
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003487 /* selector src */
3488 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3489
3490 /* selector dst */
3491 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3492
3493 /* policy information */
3494 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3495 pol->sadb_x_policy_len = size_pol / 8;
3496 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3497 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3498 pol->sadb_x_policy_dir = dir + 1;
3499 pol->sadb_x_policy_id = 0;
3500 pol->sadb_x_policy_priority = 0;
3501
3502 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3503 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003504 int mode = pfkey_mode_from_xfrm(mp->mode);
3505 if (mode < 0)
Patrick McHardyd4782c32008-01-20 17:24:29 -08003506 goto err;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003507 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003508 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3509 mp->reqid, mp->old_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003510 &mp->old_saddr, &mp->old_daddr) < 0)
3511 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003512
3513 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003514 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003515 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3516 mp->reqid, mp->new_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003517 &mp->new_saddr, &mp->new_daddr) < 0)
3518 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003519 }
3520
3521 /* broadcast migrate message to sockets */
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003522 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003523
3524 return 0;
Patrick McHardyd4782c32008-01-20 17:24:29 -08003525
3526err:
3527 kfree_skb(skb);
3528 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003529}
3530#else
3531static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003532 struct xfrm_migrate *m, int num_bundles,
3533 struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003534{
3535 return -ENOPROTOOPT;
3536}
3537#endif
3538
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539static int pfkey_sendmsg(struct kiocb *kiocb,
3540 struct socket *sock, struct msghdr *msg, size_t len)
3541{
3542 struct sock *sk = sock->sk;
3543 struct sk_buff *skb = NULL;
3544 struct sadb_msg *hdr = NULL;
3545 int err;
3546
3547 err = -EOPNOTSUPP;
3548 if (msg->msg_flags & MSG_OOB)
3549 goto out;
3550
3551 err = -EMSGSIZE;
3552 if ((unsigned)len > sk->sk_sndbuf - 32)
3553 goto out;
3554
3555 err = -ENOBUFS;
3556 skb = alloc_skb(len, GFP_KERNEL);
3557 if (skb == NULL)
3558 goto out;
3559
3560 err = -EFAULT;
3561 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3562 goto out;
3563
3564 hdr = pfkey_get_base_msg(skb, &err);
3565 if (!hdr)
3566 goto out;
3567
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003568 mutex_lock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 err = pfkey_process(sk, skb, hdr);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003570 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572out:
3573 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3574 err = 0;
Wei Yongjun6f961062009-02-25 00:31:04 +00003575 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
3577 return err ? : len;
3578}
3579
3580static int pfkey_recvmsg(struct kiocb *kiocb,
3581 struct socket *sock, struct msghdr *msg, size_t len,
3582 int flags)
3583{
3584 struct sock *sk = sock->sk;
Timo Teras83321d62008-03-03 23:40:12 -08003585 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 struct sk_buff *skb;
3587 int copied, err;
3588
3589 err = -EINVAL;
3590 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3591 goto out;
3592
3593 msg->msg_namelen = 0;
3594 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3595 if (skb == NULL)
3596 goto out;
3597
3598 copied = skb->len;
3599 if (copied > len) {
3600 msg->msg_flags |= MSG_TRUNC;
3601 copied = len;
3602 }
3603
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003604 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3606 if (err)
3607 goto out_free;
3608
3609 sock_recv_timestamp(msg, sk, skb);
3610
3611 err = (flags & MSG_TRUNC) ? skb->len : copied;
3612
Timo Teras83321d62008-03-03 23:40:12 -08003613 if (pfk->dump.dump != NULL &&
3614 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3615 pfkey_do_dump(pfk);
3616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617out_free:
3618 skb_free_datagram(sk, skb);
3619out:
3620 return err;
3621}
3622
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003623static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 .family = PF_KEY,
3625 .owner = THIS_MODULE,
3626 /* Operations that make no sense on pfkey sockets. */
3627 .bind = sock_no_bind,
3628 .connect = sock_no_connect,
3629 .socketpair = sock_no_socketpair,
3630 .accept = sock_no_accept,
3631 .getname = sock_no_getname,
3632 .ioctl = sock_no_ioctl,
3633 .listen = sock_no_listen,
3634 .shutdown = sock_no_shutdown,
3635 .setsockopt = sock_no_setsockopt,
3636 .getsockopt = sock_no_getsockopt,
3637 .mmap = sock_no_mmap,
3638 .sendpage = sock_no_sendpage,
3639
3640 /* Now the operations that really occur. */
3641 .release = pfkey_release,
3642 .poll = datagram_poll,
3643 .sendmsg = pfkey_sendmsg,
3644 .recvmsg = pfkey_recvmsg,
3645};
3646
3647static struct net_proto_family pfkey_family_ops = {
3648 .family = PF_KEY,
3649 .create = pfkey_create,
3650 .owner = THIS_MODULE,
3651};
3652
3653#ifdef CONFIG_PROC_FS
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003654static int pfkey_seq_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003658 s = (struct sock *)v;
3659 if (v == SEQ_START_TOKEN)
3660 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3661 else
3662 seq_printf(f ,"%p %-6d %-6u %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 s,
3664 atomic_read(&s->sk_refcnt),
Eric Dumazet31e6d362009-06-17 19:05:41 -07003665 sk_rmem_alloc_get(s),
3666 sk_wmem_alloc_get(s),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 sock_i_uid(s),
3668 sock_i_ino(s)
3669 );
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003670 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671}
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003672
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003673static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3674{
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003675 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003676 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003677 struct sock *s;
3678 struct hlist_node *node;
3679 loff_t pos = *ppos;
3680
3681 read_lock(&pfkey_table_lock);
3682 if (pos == 0)
3683 return SEQ_START_TOKEN;
3684
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003685 sk_for_each(s, node, &net_pfkey->table)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003686 if (pos-- == 1)
3687 return s;
3688
3689 return NULL;
3690}
3691
3692static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3693{
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003694 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003695 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3696
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003697 ++*ppos;
3698 return (v == SEQ_START_TOKEN) ?
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003699 sk_head(&net_pfkey->table) :
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003700 sk_next((struct sock *)v);
3701}
3702
3703static void pfkey_seq_stop(struct seq_file *f, void *v)
3704{
3705 read_unlock(&pfkey_table_lock);
3706}
3707
Stephen Hemminger98147d52009-09-01 19:25:02 +00003708static const struct seq_operations pfkey_seq_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003709 .start = pfkey_seq_start,
3710 .next = pfkey_seq_next,
3711 .stop = pfkey_seq_stop,
3712 .show = pfkey_seq_show,
3713};
3714
3715static int pfkey_seq_open(struct inode *inode, struct file *file)
3716{
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003717 return seq_open_net(inode, file, &pfkey_seq_ops,
3718 sizeof(struct seq_net_private));
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003719}
3720
3721static struct file_operations pfkey_proc_ops = {
3722 .open = pfkey_seq_open,
3723 .read = seq_read,
3724 .llseek = seq_lseek,
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003725 .release = seq_release_net,
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003726};
3727
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003728static int __net_init pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003729{
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003730 struct proc_dir_entry *e;
3731
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003732 e = proc_net_fops_create(net, "pfkey", 0, &pfkey_proc_ops);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003733 if (e == NULL)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003734 return -ENOMEM;
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003735
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003736 return 0;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003737}
3738
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003739static void pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003740{
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003741 proc_net_remove(net, "pfkey");
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003742}
3743#else
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003744static int __net_init pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003745{
3746 return 0;
3747}
3748
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003749static void pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003750{
3751}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752#endif
3753
3754static struct xfrm_mgr pfkeyv2_mgr =
3755{
3756 .id = "pfkeyv2",
3757 .notify = pfkey_send_notify,
3758 .acquire = pfkey_send_acquire,
3759 .compile_policy = pfkey_compile_policy,
3760 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003761 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003762 .migrate = pfkey_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763};
3764
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003765static int __net_init pfkey_net_init(struct net *net)
3766{
3767 struct netns_pfkey *net_pfkey;
3768 int rv;
3769
3770 net_pfkey = kmalloc(sizeof(struct netns_pfkey), GFP_KERNEL);
3771 if (!net_pfkey) {
3772 rv = -ENOMEM;
3773 goto out_kmalloc;
3774 }
3775 INIT_HLIST_HEAD(&net_pfkey->table);
3776 atomic_set(&net_pfkey->socks_nr, 0);
3777 rv = net_assign_generic(net, pfkey_net_id, net_pfkey);
3778 if (rv < 0)
3779 goto out_assign;
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003780 rv = pfkey_init_proc(net);
3781 if (rv < 0)
3782 goto out_proc;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003783 return 0;
3784
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003785out_proc:
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003786out_assign:
3787 kfree(net_pfkey);
3788out_kmalloc:
3789 return rv;
3790}
3791
3792static void __net_exit pfkey_net_exit(struct net *net)
3793{
3794 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3795
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003796 pfkey_exit_proc(net);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003797 BUG_ON(!hlist_empty(&net_pfkey->table));
3798 kfree(net_pfkey);
3799}
3800
3801static struct pernet_operations pfkey_net_ops = {
3802 .init = pfkey_net_init,
3803 .exit = pfkey_net_exit,
3804};
3805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806static void __exit ipsec_pfkey_exit(void)
3807{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003808 unregister_pernet_gen_subsys(pfkey_net_id, &pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 xfrm_unregister_km(&pfkeyv2_mgr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 sock_unregister(PF_KEY);
3811 proto_unregister(&key_proto);
3812}
3813
3814static int __init ipsec_pfkey_init(void)
3815{
3816 int err = proto_register(&key_proto, 0);
3817
3818 if (err != 0)
3819 goto out;
3820
3821 err = sock_register(&pfkey_family_ops);
3822 if (err != 0)
3823 goto out_unregister_key_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 err = xfrm_register_km(&pfkeyv2_mgr);
3825 if (err != 0)
Alexey Dobriyan7013ec302008-11-25 17:59:00 -08003826 goto out_sock_unregister;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003827 err = register_pernet_gen_subsys(&pfkey_net_id, &pfkey_net_ops);
3828 if (err != 0)
3829 goto out_xfrm_unregister_km;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830out:
3831 return err;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003832out_xfrm_unregister_km:
3833 xfrm_unregister_km(&pfkeyv2_mgr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834out_sock_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 sock_unregister(PF_KEY);
3836out_unregister_key_proto:
3837 proto_unregister(&key_proto);
3838 goto out;
3839}
3840
3841module_init(ipsec_pfkey_init);
3842module_exit(ipsec_pfkey_exit);
3843MODULE_LICENSE("GPL");
3844MODULE_ALIAS_NETPROTO(PF_KEY);