blob: a50d979b592660aacfe711b9c73d93da00c7178a [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020030#include <net/net_namespace.h>
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080031#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/xfrm.h>
33
34#include <net/sock.h>
35
36#define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
37#define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
38
Eric Dumazetf99189b2009-11-17 10:42:49 +000039static int pfkey_net_id __read_mostly;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080040struct netns_pfkey {
41 /* List of all pfkey sockets. */
42 struct hlist_head table;
43 atomic_t socks_nr;
44};
stephen hemminger7f6b9db2010-02-22 07:57:19 +000045static DEFINE_MUTEX(pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jamal Hadi Salimbd557752010-02-22 16:20:22 -080047#define DUMMY_MARK 0
Mathias Krausee473fcb2013-06-26 23:56:58 +020048static const struct xfrm_mark dummy_mark = {0, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049struct pfkey_sock {
50 /* struct sock must be the first member of struct pfkey_sock */
51 struct sock sk;
52 int registered;
53 int promisc;
Timo Teras83321d62008-03-03 23:40:12 -080054
55 struct {
56 uint8_t msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +000057 uint32_t msg_portid;
Timo Teras83321d62008-03-03 23:40:12 -080058 int (*dump)(struct pfkey_sock *sk);
59 void (*done)(struct pfkey_sock *sk);
60 union {
61 struct xfrm_policy_walk policy;
62 struct xfrm_state_walk state;
63 } u;
Herbert Xu12a169e2008-10-01 07:03:24 -070064 struct sk_buff *skb;
Timo Teras83321d62008-03-03 23:40:12 -080065 } dump;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
68static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
69{
70 return (struct pfkey_sock *)sk;
71}
72
David S. Miller4c93fbb2011-02-25 18:07:06 -080073static int pfkey_can_dump(const struct sock *sk)
Timo Teras83321d62008-03-03 23:40:12 -080074{
75 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
76 return 1;
77 return 0;
78}
79
Timo Teras05238202008-10-01 05:17:54 -070080static void pfkey_terminate_dump(struct pfkey_sock *pfk)
Timo Teras83321d62008-03-03 23:40:12 -080081{
Timo Teras05238202008-10-01 05:17:54 -070082 if (pfk->dump.dump) {
Herbert Xu12a169e2008-10-01 07:03:24 -070083 if (pfk->dump.skb) {
84 kfree_skb(pfk->dump.skb);
85 pfk->dump.skb = NULL;
86 }
Timo Teras05238202008-10-01 05:17:54 -070087 pfk->dump.done(pfk);
88 pfk->dump.dump = NULL;
89 pfk->dump.done = NULL;
90 }
Timo Teras83321d62008-03-03 23:40:12 -080091}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static void pfkey_sock_destruct(struct sock *sk)
94{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080095 struct net *net = sock_net(sk);
96 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
97
Timo Teras05238202008-10-01 05:17:54 -070098 pfkey_terminate_dump(pfkey_sk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 skb_queue_purge(&sk->sk_receive_queue);
100
101 if (!sock_flag(sk, SOCK_DEAD)) {
stephen hemminger207024b2010-05-12 06:37:07 +0000102 pr_err("Attempt to release alive pfkey socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return;
104 }
105
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700106 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
107 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800109 atomic_dec(&net_pfkey->socks_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800112static const struct proto_ops pfkey_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114static void pfkey_insert(struct sock *sk)
115{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800116 struct net *net = sock_net(sk);
117 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
118
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000119 mutex_lock(&pfkey_mutex);
120 sk_add_node_rcu(sk, &net_pfkey->table);
121 mutex_unlock(&pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static void pfkey_remove(struct sock *sk)
125{
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000126 mutex_lock(&pfkey_mutex);
127 sk_del_node_init_rcu(sk);
128 mutex_unlock(&pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131static struct proto key_proto = {
132 .name = "KEY",
133 .owner = THIS_MODULE,
134 .obj_size = sizeof(struct pfkey_sock),
135};
136
Eric Paris3f378b62009-11-05 22:18:14 -0800137static int pfkey_create(struct net *net, struct socket *sock, int protocol,
138 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800140 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct sock *sk;
142 int err;
143
Eric W. Biedermandf008c92012-11-16 03:03:07 +0000144 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -EPERM;
146 if (sock->type != SOCK_RAW)
147 return -ESOCKTNOSUPPORT;
148 if (protocol != PF_KEY_V2)
149 return -EPROTONOSUPPORT;
150
151 err = -ENOMEM;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700152 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (sk == NULL)
154 goto out;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 sock->ops = &pfkey_ops;
157 sock_init_data(sock, sk);
158
159 sk->sk_family = PF_KEY;
160 sk->sk_destruct = pfkey_sock_destruct;
161
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800162 atomic_inc(&net_pfkey->socks_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 pfkey_insert(sk);
165
166 return 0;
167out:
168 return err;
169}
170
171static int pfkey_release(struct socket *sock)
172{
173 struct sock *sk = sock->sk;
174
175 if (!sk)
176 return 0;
177
178 pfkey_remove(sk);
179
180 sock_orphan(sk);
181 sock->sk = NULL;
182 skb_queue_purge(&sk->sk_write_queue);
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000183
184 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 sock_put(sk);
186
187 return 0;
188}
189
190static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
Al Virodd0fc662005-10-07 07:46:04 +0100191 gfp_t allocation, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int err = -ENOBUFS;
194
195 sock_hold(sk);
196 if (*skb2 == NULL) {
197 if (atomic_read(&skb->users) != 1) {
198 *skb2 = skb_clone(skb, allocation);
199 } else {
200 *skb2 = skb;
201 atomic_inc(&skb->users);
202 }
203 }
204 if (*skb2 != NULL) {
205 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 skb_set_owner_r(*skb2, sk);
207 skb_queue_tail(&sk->sk_receive_queue, *skb2);
208 sk->sk_data_ready(sk, (*skb2)->len);
209 *skb2 = NULL;
210 err = 0;
211 }
212 }
213 sock_put(sk);
214 return err;
215}
216
217/* Send SKB to all pfkey sockets matching selected criteria. */
218#define BROADCAST_ALL 0
219#define BROADCAST_ONE 1
220#define BROADCAST_REGISTERED 2
221#define BROADCAST_PROMISC_ONLY 4
Al Virodd0fc662005-10-07 07:46:04 +0100222static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800223 int broadcast_flags, struct sock *one_sk,
224 struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800226 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct sk_buff *skb2 = NULL;
229 int err = -ESRCH;
230
231 /* XXX Do we need something like netlink_overrun? I think
232 * XXX PF_KEY socket apps will not mind current behavior.
233 */
234 if (!skb)
235 return -ENOMEM;
236
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000237 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800238 sk_for_each_rcu(sk, &net_pfkey->table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 struct pfkey_sock *pfk = pfkey_sk(sk);
240 int err2;
241
242 /* Yes, it means that if you are meant to receive this
243 * pfkey message you receive it twice as promiscuous
244 * socket.
245 */
246 if (pfk->promisc)
247 pfkey_broadcast_one(skb, &skb2, allocation, sk);
248
249 /* the exact target will be processed later */
250 if (sk == one_sk)
251 continue;
252 if (broadcast_flags != BROADCAST_ALL) {
253 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
254 continue;
255 if ((broadcast_flags & BROADCAST_REGISTERED) &&
256 !pfk->registered)
257 continue;
258 if (broadcast_flags & BROADCAST_ONE)
259 continue;
260 }
261
262 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
263
264 /* Error is cleare after succecful sending to at least one
265 * registered KM */
266 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
267 err = err2;
268 }
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000269 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (one_sk != NULL)
272 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
273
Wei Yongjun6f961062009-02-25 00:31:04 +0000274 kfree_skb(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 kfree_skb(skb);
276 return err;
277}
278
Timo Teras05238202008-10-01 05:17:54 -0700279static int pfkey_do_dump(struct pfkey_sock *pfk)
280{
Herbert Xu12a169e2008-10-01 07:03:24 -0700281 struct sadb_msg *hdr;
Timo Teras05238202008-10-01 05:17:54 -0700282 int rc;
283
284 rc = pfk->dump.dump(pfk);
285 if (rc == -ENOBUFS)
286 return 0;
287
Herbert Xu12a169e2008-10-01 07:03:24 -0700288 if (pfk->dump.skb) {
289 if (!pfkey_can_dump(&pfk->sk))
290 return 0;
291
292 hdr = (struct sadb_msg *) pfk->dump.skb->data;
293 hdr->sadb_msg_seq = 0;
294 hdr->sadb_msg_errno = rc;
295 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800296 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -0700297 pfk->dump.skb = NULL;
298 }
299
Timo Teras05238202008-10-01 05:17:54 -0700300 pfkey_terminate_dump(pfk);
301 return rc;
302}
303
David S. Miller4c93fbb2011-02-25 18:07:06 -0800304static inline void pfkey_hdr_dup(struct sadb_msg *new,
305 const struct sadb_msg *orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 *new = *orig;
308}
309
David S. Miller4c93fbb2011-02-25 18:07:06 -0800310static int pfkey_error(const struct sadb_msg *orig, int err, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
313 struct sadb_msg *hdr;
314
315 if (!skb)
316 return -ENOBUFS;
317
318 /* Woe be to the platform trying to support PFKEY yet
319 * having normal errnos outside the 1-255 range, inclusive.
320 */
321 err = -err;
322 if (err == ERESTARTSYS ||
323 err == ERESTARTNOHAND ||
324 err == ERESTARTNOINTR)
325 err = EINTR;
326 if (err >= 512)
327 err = EINVAL;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800328 BUG_ON(err <= 0 || err >= 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
331 pfkey_hdr_dup(hdr, orig);
332 hdr->sadb_msg_errno = (uint8_t) err;
333 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
334 sizeof(uint64_t));
335
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800336 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 return 0;
339}
340
Mathias Krause8603b9552013-06-26 23:56:59 +0200341static const u8 sadb_ext_min_len[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 [SADB_EXT_RESERVED] = (u8) 0,
343 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
344 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
345 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
346 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
347 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
348 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
349 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
350 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
351 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
352 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
353 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
354 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
355 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
356 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
357 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
358 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
359 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
360 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
361 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
362 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
363 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
364 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
365 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
Trent Jaegerdf718372005-12-13 23:12:27 -0800366 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -0700367 [SADB_X_EXT_KMADDRESS] = (u8) sizeof(struct sadb_x_kmaddress),
Nicolas Dichteld2c5f652014-02-20 14:52:41 +0100368 [SADB_X_EXT_FILTER] = (u8) sizeof(struct sadb_x_filter),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369};
370
371/* Verify sadb_address_{len,prefixlen} against sa_family. */
David S. Miller4c93fbb2011-02-25 18:07:06 -0800372static int verify_address_len(const void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800374 const struct sadb_address *sp = p;
375 const struct sockaddr *addr = (const struct sockaddr *)(sp + 1);
376 const struct sockaddr_in *sin;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000377#if IS_ENABLED(CONFIG_IPV6)
David S. Miller4c93fbb2011-02-25 18:07:06 -0800378 const struct sockaddr_in6 *sin6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379#endif
380 int len;
381
382 switch (addr->sa_family) {
383 case AF_INET:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700384 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (sp->sadb_address_len != len ||
386 sp->sadb_address_prefixlen > 32)
387 return -EINVAL;
388 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000389#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 case AF_INET6:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700391 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (sp->sadb_address_len != len ||
393 sp->sadb_address_prefixlen > 128)
394 return -EINVAL;
395 break;
396#endif
397 default:
398 /* It is user using kernel to keep track of security
399 * associations for another protocol, such as
400 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
401 * lengths.
402 *
403 * XXX Actually, association/policy database is not yet
404 * XXX able to cope with arbitrary sockaddr families.
405 * XXX When it can, remove this -EINVAL. -DaveM
406 */
407 return -EINVAL;
408 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 return 0;
412}
413
David S. Miller4c93fbb2011-02-25 18:07:06 -0800414static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800415{
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700416 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
417 sec_ctx->sadb_x_ctx_len,
418 sizeof(uint64_t));
Trent Jaegerdf718372005-12-13 23:12:27 -0800419}
420
David S. Miller4c93fbb2011-02-25 18:07:06 -0800421static inline int verify_sec_ctx_len(const void *p)
Trent Jaegerdf718372005-12-13 23:12:27 -0800422{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800423 const struct sadb_x_sec_ctx *sec_ctx = p;
Stephen Rothwell298bb622007-10-30 23:57:05 -0700424 int len = sec_ctx->sadb_x_ctx_len;
Trent Jaegerdf718372005-12-13 23:12:27 -0800425
Stephen Rothwell298bb622007-10-30 23:57:05 -0700426 if (len > PAGE_SIZE)
Trent Jaegerdf718372005-12-13 23:12:27 -0800427 return -EINVAL;
428
429 len = pfkey_sec_ctx_len(sec_ctx);
430
431 if (sec_ctx->sadb_x_sec_len != len)
432 return -EINVAL;
433
434 return 0;
435}
436
David S. Miller4c93fbb2011-02-25 18:07:06 -0800437static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800438{
439 struct xfrm_user_sec_ctx *uctx = NULL;
440 int ctx_size = sec_ctx->sadb_x_ctx_len;
441
442 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
443
444 if (!uctx)
445 return NULL;
446
447 uctx->len = pfkey_sec_ctx_len(sec_ctx);
448 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
449 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
450 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
451 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
452 memcpy(uctx + 1, sec_ctx + 1,
453 uctx->ctx_len);
454
455 return uctx;
456}
457
David S. Miller4c93fbb2011-02-25 18:07:06 -0800458static int present_and_same_family(const struct sadb_address *src,
459 const struct sadb_address *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800461 const struct sockaddr *s_addr, *d_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (!src || !dst)
464 return 0;
465
David S. Miller4c93fbb2011-02-25 18:07:06 -0800466 s_addr = (const struct sockaddr *)(src + 1);
467 d_addr = (const struct sockaddr *)(dst + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (s_addr->sa_family != d_addr->sa_family)
469 return 0;
470 if (s_addr->sa_family != AF_INET
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000471#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 && s_addr->sa_family != AF_INET6
473#endif
474 )
475 return 0;
476
477 return 1;
478}
479
David S. Miller4c93fbb2011-02-25 18:07:06 -0800480static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800482 const char *p = (char *) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int len = skb->len;
484
485 len -= sizeof(*hdr);
486 p += sizeof(*hdr);
487 while (len > 0) {
David S. Miller4c93fbb2011-02-25 18:07:06 -0800488 const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 uint16_t ext_type;
490 int ext_len;
491
492 ext_len = ehdr->sadb_ext_len;
493 ext_len *= sizeof(uint64_t);
494 ext_type = ehdr->sadb_ext_type;
495 if (ext_len < sizeof(uint64_t) ||
496 ext_len > len ||
497 ext_type == SADB_EXT_RESERVED)
498 return -EINVAL;
499
500 if (ext_type <= SADB_EXT_MAX) {
501 int min = (int) sadb_ext_min_len[ext_type];
502 if (ext_len < min)
503 return -EINVAL;
504 if (ext_hdrs[ext_type-1] != NULL)
505 return -EINVAL;
506 if (ext_type == SADB_EXT_ADDRESS_SRC ||
507 ext_type == SADB_EXT_ADDRESS_DST ||
508 ext_type == SADB_EXT_ADDRESS_PROXY ||
509 ext_type == SADB_X_EXT_NAT_T_OA) {
510 if (verify_address_len(p))
511 return -EINVAL;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900512 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800513 if (ext_type == SADB_X_EXT_SEC_CTX) {
514 if (verify_sec_ctx_len(p))
515 return -EINVAL;
516 }
David S. Miller4c93fbb2011-02-25 18:07:06 -0800517 ext_hdrs[ext_type-1] = (void *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 p += ext_len;
520 len -= ext_len;
521 }
522
523 return 0;
524}
525
526static uint16_t
527pfkey_satype2proto(uint8_t satype)
528{
529 switch (satype) {
530 case SADB_SATYPE_UNSPEC:
531 return IPSEC_PROTO_ANY;
532 case SADB_SATYPE_AH:
533 return IPPROTO_AH;
534 case SADB_SATYPE_ESP:
535 return IPPROTO_ESP;
536 case SADB_X_SATYPE_IPCOMP:
537 return IPPROTO_COMP;
538 break;
539 default:
540 return 0;
541 }
542 /* NOTREACHED */
543}
544
545static uint8_t
546pfkey_proto2satype(uint16_t proto)
547{
548 switch (proto) {
549 case IPPROTO_AH:
550 return SADB_SATYPE_AH;
551 case IPPROTO_ESP:
552 return SADB_SATYPE_ESP;
553 case IPPROTO_COMP:
554 return SADB_X_SATYPE_IPCOMP;
555 break;
556 default:
557 return 0;
558 }
559 /* NOTREACHED */
560}
561
562/* BTW, this scheme means that there is no way with PFKEY2 sockets to
563 * say specifically 'just raw sockets' as we encode them as 255.
564 */
565
566static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
567{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000568 return proto == IPSEC_PROTO_ANY ? 0 : proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
572{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000573 return proto ? proto : IPSEC_PROTO_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900576static inline int pfkey_sockaddr_len(sa_family_t family)
577{
578 switch (family) {
579 case AF_INET:
580 return sizeof(struct sockaddr_in);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000581#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900582 case AF_INET6:
583 return sizeof(struct sockaddr_in6);
584#endif
585 }
586 return 0;
587}
588
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900589static
590int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900592 switch (sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 case AF_INET:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900594 xaddr->a4 =
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900595 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return AF_INET;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000597#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 case AF_INET6:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900599 memcpy(xaddr->a6,
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900600 &((struct sockaddr_in6 *)sa)->sin6_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 sizeof(struct in6_addr));
602 return AF_INET6;
603#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900605 return 0;
606}
607
608static
David S. Miller4c93fbb2011-02-25 18:07:06 -0800609int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900610{
611 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
612 xaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
David S. Miller4c93fbb2011-02-25 18:07:06 -0800615static struct xfrm_state *pfkey_xfrm_state_lookup(struct net *net, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800617 const struct sadb_sa *sa;
618 const struct sadb_address *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 uint16_t proto;
620 unsigned short family;
621 xfrm_address_t *xaddr;
622
Joe Perchesea110732011-06-13 16:21:26 +0000623 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (sa == NULL)
625 return NULL;
626
627 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
628 if (proto == 0)
629 return NULL;
630
631 /* sadb_address_len should be checked by caller */
Joe Perchesea110732011-06-13 16:21:26 +0000632 addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (addr == NULL)
634 return NULL;
635
David S. Miller4c93fbb2011-02-25 18:07:06 -0800636 family = ((const struct sockaddr *)(addr + 1))->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 switch (family) {
638 case AF_INET:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800639 xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000641#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 case AF_INET6:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800643 xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 break;
645#endif
646 default:
647 xaddr = NULL;
648 }
649
650 if (!xaddr)
651 return NULL;
652
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800653 return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658static int
659pfkey_sockaddr_size(sa_family_t family)
660{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900661 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700664static inline int pfkey_mode_from_xfrm(int mode)
665{
666 switch(mode) {
667 case XFRM_MODE_TRANSPORT:
668 return IPSEC_MODE_TRANSPORT;
669 case XFRM_MODE_TUNNEL:
670 return IPSEC_MODE_TUNNEL;
671 case XFRM_MODE_BEET:
672 return IPSEC_MODE_BEET;
673 default:
674 return -1;
675 }
676}
677
678static inline int pfkey_mode_to_xfrm(int mode)
679{
680 switch(mode) {
681 case IPSEC_MODE_ANY: /*XXX*/
682 case IPSEC_MODE_TRANSPORT:
683 return XFRM_MODE_TRANSPORT;
684 case IPSEC_MODE_TUNNEL:
685 return XFRM_MODE_TUNNEL;
686 case IPSEC_MODE_BEET:
687 return XFRM_MODE_BEET;
688 default:
689 return -1;
690 }
691}
692
David S. Miller183cad12011-02-24 00:28:01 -0500693static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
David S. Miller4c93fbb2011-02-25 18:07:06 -0800694 struct sockaddr *sa,
695 unsigned short family)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900696{
697 switch (family) {
698 case AF_INET:
699 {
700 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
701 sin->sin_family = AF_INET;
702 sin->sin_port = port;
703 sin->sin_addr.s_addr = xaddr->a4;
704 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
705 return 32;
706 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000707#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900708 case AF_INET6:
709 {
710 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
711 sin6->sin6_family = AF_INET6;
712 sin6->sin6_port = port;
713 sin6->sin6_flowinfo = 0;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000714 sin6->sin6_addr = *(struct in6_addr *)xaddr->a6;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900715 sin6->sin6_scope_id = 0;
716 return 128;
717 }
718#endif
719 }
720 return 0;
721}
722
David S. Miller4c93fbb2011-02-25 18:07:06 -0800723static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -0700724 int add_keys, int hsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct sk_buff *skb;
727 struct sadb_msg *hdr;
728 struct sadb_sa *sa;
729 struct sadb_lifetime *lifetime;
730 struct sadb_address *addr;
731 struct sadb_key *key;
732 struct sadb_x_sa2 *sa2;
Trent Jaegerdf718372005-12-13 23:12:27 -0800733 struct sadb_x_sec_ctx *sec_ctx;
734 struct xfrm_sec_ctx *xfrm_ctx;
735 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int size;
737 int auth_key_size = 0;
738 int encrypt_key_size = 0;
739 int sockaddr_size;
740 struct xfrm_encap_tmpl *natt = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700741 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 /* address family check */
744 sockaddr_size = pfkey_sockaddr_size(x->props.family);
745 if (!sockaddr_size)
746 return ERR_PTR(-EINVAL);
747
748 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
749 key(AE), (identity(SD),) (sensitivity)> */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900750 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 sizeof(struct sadb_lifetime) +
752 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
753 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900754 sizeof(struct sadb_address)*2 +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 sockaddr_size*2 +
756 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800757
758 if ((xfrm_ctx = x->security)) {
759 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
760 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
761 }
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /* identity & sensitivity */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000764 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 size += sizeof(struct sadb_address) + sockaddr_size;
766
767 if (add_keys) {
768 if (x->aalg && x->aalg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900769 auth_key_size =
770 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 size += sizeof(struct sadb_key) + auth_key_size;
772 }
773 if (x->ealg && x->ealg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900774 encrypt_key_size =
775 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 size += sizeof(struct sadb_key) + encrypt_key_size;
777 }
778 }
779 if (x->encap)
780 natt = x->encap;
781
782 if (natt && natt->encap_type) {
783 size += sizeof(struct sadb_x_nat_t_type);
784 size += sizeof(struct sadb_x_nat_t_port);
785 size += sizeof(struct sadb_x_nat_t_port);
786 }
787
788 skb = alloc_skb(size + 16, GFP_ATOMIC);
789 if (skb == NULL)
790 return ERR_PTR(-ENOBUFS);
791
792 /* call should fill header later */
793 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
794 memset(hdr, 0, size); /* XXX do we need this ? */
795 hdr->sadb_msg_len = size / sizeof(uint64_t);
796
797 /* sa */
798 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
799 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
800 sa->sadb_sa_exttype = SADB_EXT_SA;
801 sa->sadb_sa_spi = x->id.spi;
802 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700803 switch (x->km.state) {
804 case XFRM_STATE_VALID:
805 sa->sadb_sa_state = x->km.dying ?
806 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
807 break;
808 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700810 break;
811 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700813 break;
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 sa->sadb_sa_auth = 0;
816 if (x->aalg) {
817 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200818 sa->sadb_sa_auth = (a && a->pfkey_supported) ?
819 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821 sa->sadb_sa_encrypt = 0;
822 BUG_ON(x->ealg && x->calg);
823 if (x->ealg) {
824 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200825 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
826 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
829 if (x->calg) {
830 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200831 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
832 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
835 sa->sadb_sa_flags = 0;
836 if (x->props.flags & XFRM_STATE_NOECN)
837 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
838 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
839 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700840 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
841 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /* hard time */
844 if (hsc & 2) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900845 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 sizeof(struct sadb_lifetime));
847 lifetime->sadb_lifetime_len =
848 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
849 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
850 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
851 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
852 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
853 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
854 }
855 /* soft time */
856 if (hsc & 1) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900857 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 sizeof(struct sadb_lifetime));
859 lifetime->sadb_lifetime_len =
860 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
861 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
862 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
863 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
864 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
865 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
866 }
867 /* current time */
868 lifetime = (struct sadb_lifetime *) skb_put(skb,
869 sizeof(struct sadb_lifetime));
870 lifetime->sadb_lifetime_len =
871 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
872 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
873 lifetime->sadb_lifetime_allocations = x->curlft.packets;
874 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
875 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
876 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
877 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900878 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900880 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 (sizeof(struct sadb_address)+sockaddr_size)/
882 sizeof(uint64_t);
883 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900884 /* "if the ports are non-zero, then the sadb_address_proto field,
885 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900887 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900890 addr->sadb_address_prefixlen =
891 pfkey_sockaddr_fill(&x->props.saddr, 0,
892 (struct sockaddr *) (addr + 1),
893 x->props.family);
894 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 BUG();
896
897 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900898 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900900 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 (sizeof(struct sadb_address)+sockaddr_size)/
902 sizeof(uint64_t);
903 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900904 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900907 addr->sadb_address_prefixlen =
908 pfkey_sockaddr_fill(&x->id.daddr, 0,
909 (struct sockaddr *) (addr + 1),
910 x->props.family);
911 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 BUG();
913
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000914 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
915 x->props.family)) {
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900916 addr = (struct sadb_address*) skb_put(skb,
917 sizeof(struct sadb_address)+sockaddr_size);
918 addr->sadb_address_len =
919 (sizeof(struct sadb_address)+sockaddr_size)/
920 sizeof(uint64_t);
921 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
922 addr->sadb_address_proto =
923 pfkey_proto_from_xfrm(x->sel.proto);
924 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
925 addr->sadb_address_reserved = 0;
926
927 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
928 (struct sockaddr *) (addr + 1),
929 x->props.family);
930 }
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /* auth key */
933 if (add_keys && auth_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900934 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 sizeof(struct sadb_key)+auth_key_size);
936 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
937 sizeof(uint64_t);
938 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
939 key->sadb_key_bits = x->aalg->alg_key_len;
940 key->sadb_key_reserved = 0;
941 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
942 }
943 /* encrypt key */
944 if (add_keys && encrypt_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900945 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 sizeof(struct sadb_key)+encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900947 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 encrypt_key_size) / sizeof(uint64_t);
949 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
950 key->sadb_key_bits = x->ealg->alg_key_len;
951 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900952 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 (x->ealg->alg_key_len+7)/8);
954 }
955
956 /* sa */
957 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
958 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
959 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700960 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
961 kfree_skb(skb);
962 return ERR_PTR(-EINVAL);
963 }
964 sa2->sadb_x_sa2_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 sa2->sadb_x_sa2_reserved1 = 0;
966 sa2->sadb_x_sa2_reserved2 = 0;
967 sa2->sadb_x_sa2_sequence = 0;
968 sa2->sadb_x_sa2_reqid = x->props.reqid;
969
970 if (natt && natt->encap_type) {
971 struct sadb_x_nat_t_type *n_type;
972 struct sadb_x_nat_t_port *n_port;
973
974 /* type */
975 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
976 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
977 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
978 n_type->sadb_x_nat_t_type_type = natt->encap_type;
979 n_type->sadb_x_nat_t_type_reserved[0] = 0;
980 n_type->sadb_x_nat_t_type_reserved[1] = 0;
981 n_type->sadb_x_nat_t_type_reserved[2] = 0;
982
983 /* source port */
984 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
985 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
986 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
987 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
988 n_port->sadb_x_nat_t_port_reserved = 0;
989
990 /* dest port */
991 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
992 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
993 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
994 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
995 n_port->sadb_x_nat_t_port_reserved = 0;
996 }
997
Trent Jaegerdf718372005-12-13 23:12:27 -0800998 /* security context */
999 if (xfrm_ctx) {
1000 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1001 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1002 sec_ctx->sadb_x_sec_len =
1003 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1004 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1005 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1006 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1007 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1008 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1009 xfrm_ctx->ctx_len);
1010 }
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return skb;
1013}
1014
Herbert Xu050f0092007-10-09 13:31:47 -07001015
David S. Miller4c93fbb2011-02-25 18:07:06 -08001016static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
Herbert Xu050f0092007-10-09 13:31:47 -07001017{
1018 struct sk_buff *skb;
1019
Herbert Xu050f0092007-10-09 13:31:47 -07001020 skb = __pfkey_xfrm_state2msg(x, 1, 3);
Herbert Xu050f0092007-10-09 13:31:47 -07001021
1022 return skb;
1023}
1024
David S. Miller4c93fbb2011-02-25 18:07:06 -08001025static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -07001026 int hsc)
1027{
1028 return __pfkey_xfrm_state2msg(x, 0, hsc);
1029}
1030
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001031static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
David S. Miller4c93fbb2011-02-25 18:07:06 -08001032 const struct sadb_msg *hdr,
1033 void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001035 struct xfrm_state *x;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001036 const struct sadb_lifetime *lifetime;
1037 const struct sadb_sa *sa;
1038 const struct sadb_key *key;
1039 const struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 uint16_t proto;
1041 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Joe Perchesea110732011-06-13 16:21:26 +00001044 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (!sa ||
1046 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1047 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1048 return ERR_PTR(-EINVAL);
1049 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1050 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1051 return ERR_PTR(-EINVAL);
1052 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1053 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1054 return ERR_PTR(-EINVAL);
1055 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1056 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1057 return ERR_PTR(-EINVAL);
1058
1059 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1060 if (proto == 0)
1061 return ERR_PTR(-EINVAL);
1062
1063 /* default error is no buffer space */
1064 err = -ENOBUFS;
1065
1066 /* RFC2367:
1067
1068 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1069 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1070 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1071 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1072 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1073 not true.
1074
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001075 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1077 */
1078 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1079 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1080 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1081 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1082 return ERR_PTR(-EINVAL);
Joe Perchesea110732011-06-13 16:21:26 +00001083 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (key != NULL &&
1085 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1086 ((key->sadb_key_bits+7) / 8 == 0 ||
1087 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1088 return ERR_PTR(-EINVAL);
1089 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1090 if (key != NULL &&
1091 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1092 ((key->sadb_key_bits+7) / 8 == 0 ||
1093 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1094 return ERR_PTR(-EINVAL);
1095
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001096 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (x == NULL)
1098 return ERR_PTR(-ENOBUFS);
1099
1100 x->id.proto = proto;
1101 x->id.spi = sa->sadb_sa_spi;
Fan Du33fce602013-09-17 15:14:13 +08001102 x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay,
1103 (sizeof(x->replay.bitmap) * 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1105 x->props.flags |= XFRM_STATE_NOECN;
1106 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1107 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001108 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1109 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Joe Perchesea110732011-06-13 16:21:26 +00001111 lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (lifetime != NULL) {
1113 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1114 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1115 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1116 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1117 }
Joe Perchesea110732011-06-13 16:21:26 +00001118 lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (lifetime != NULL) {
1120 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1121 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1122 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1123 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1124 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001125
Joe Perchesea110732011-06-13 16:21:26 +00001126 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001127 if (sec_ctx != NULL) {
1128 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1129
1130 if (!uctx)
1131 goto out;
1132
1133 err = security_xfrm_state_alloc(x, uctx);
1134 kfree(uctx);
1135
1136 if (err)
1137 goto out;
1138 }
1139
Joe Perchesea110732011-06-13 16:21:26 +00001140 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (sa->sadb_sa_auth) {
1142 int keysize = 0;
1143 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001144 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 err = -ENOSYS;
1146 goto out;
1147 }
1148 if (key)
1149 keysize = (key->sadb_key_bits + 7) / 8;
1150 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1151 if (!x->aalg)
1152 goto out;
1153 strcpy(x->aalg->alg_name, a->name);
1154 x->aalg->alg_key_len = 0;
1155 if (key) {
1156 x->aalg->alg_key_len = key->sadb_key_bits;
1157 memcpy(x->aalg->alg_key, key+1, keysize);
1158 }
Martin Willic20a66f2009-12-09 06:11:15 +00001159 x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 x->props.aalgo = sa->sadb_sa_auth;
1161 /* x->algo.flags = sa->sadb_sa_flags; */
1162 }
1163 if (sa->sadb_sa_encrypt) {
1164 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1165 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001166 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 err = -ENOSYS;
1168 goto out;
1169 }
1170 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1171 if (!x->calg)
1172 goto out;
1173 strcpy(x->calg->alg_name, a->name);
1174 x->props.calgo = sa->sadb_sa_encrypt;
1175 } else {
1176 int keysize = 0;
1177 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001178 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 err = -ENOSYS;
1180 goto out;
1181 }
1182 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1183 if (key)
1184 keysize = (key->sadb_key_bits + 7) / 8;
1185 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1186 if (!x->ealg)
1187 goto out;
1188 strcpy(x->ealg->alg_name, a->name);
1189 x->ealg->alg_key_len = 0;
1190 if (key) {
1191 x->ealg->alg_key_len = key->sadb_key_bits;
1192 memcpy(x->ealg->alg_key, key+1, keysize);
1193 }
1194 x->props.ealgo = sa->sadb_sa_encrypt;
1195 }
1196 }
1197 /* x->algo.flags = sa->sadb_sa_flags; */
1198
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001199 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 &x->props.saddr);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001201 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 &x->id.daddr);
1203
1204 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001205 const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001206 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1207 if (mode < 0) {
1208 err = -EINVAL;
1209 goto out;
1210 }
1211 x->props.mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 x->props.reqid = sa2->sadb_x_sa2_reqid;
1213 }
1214
1215 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001216 const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 /* Nobody uses this, but we try. */
1219 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1220 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1221 }
1222
Kazunori MIYAZAWA4da51052008-05-21 13:26:11 -07001223 if (!x->sel.family)
Joy Latten4a4b6272007-08-02 19:25:43 -07001224 x->sel.family = x->props.family;
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001227 const struct sadb_x_nat_t_type* n_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 struct xfrm_encap_tmpl *natt;
1229
1230 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1231 if (!x->encap)
1232 goto out;
1233
1234 natt = x->encap;
1235 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1236 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1237
1238 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001239 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1241 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1242 }
1243 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001244 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1246 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1247 }
Timo Terasa8d694c2009-01-25 20:49:14 -08001248 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250
Herbert Xu72cb6962005-06-20 13:18:08 -07001251 err = xfrm_init_state(x);
1252 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return x;
1257
1258out:
1259 x->km.state = XFRM_STATE_DEAD;
1260 xfrm_state_put(x);
1261 return ERR_PTR(err);
1262}
1263
David S. Miller4c93fbb2011-02-25 18:07:06 -08001264static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 return -EOPNOTSUPP;
1267}
1268
David S. Miller4c93fbb2011-02-25 18:07:06 -08001269static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001271 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 struct sk_buff *resp_skb;
1273 struct sadb_x_sa2 *sa2;
1274 struct sadb_address *saddr, *daddr;
1275 struct sadb_msg *out_hdr;
Herbert Xu658b2192007-10-09 13:29:52 -07001276 struct sadb_spirange *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 struct xfrm_state *x = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001278 int mode;
Herbert Xu658b2192007-10-09 13:29:52 -07001279 int err;
1280 u32 min_spi, max_spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 u32 reqid;
1282 u8 proto;
1283 unsigned short family;
1284 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1285
1286 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1287 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1288 return -EINVAL;
1289
1290 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1291 if (proto == 0)
1292 return -EINVAL;
1293
1294 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001295 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1296 if (mode < 0)
1297 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 reqid = sa2->sadb_x_sa2_reqid;
1299 } else {
1300 mode = 0;
1301 reqid = 0;
1302 }
1303
1304 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1305 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1306
1307 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1308 switch (family) {
1309 case AF_INET:
1310 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1311 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1312 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001313#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 case AF_INET6:
1315 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1316 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1317 break;
1318#endif
1319 }
1320
1321 if (hdr->sadb_msg_seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001322 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001323 if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 xfrm_state_put(x);
1325 x = NULL;
1326 }
1327 }
1328
1329 if (!x)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001330 x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 if (x == NULL)
1333 return -ENOENT;
1334
Herbert Xu658b2192007-10-09 13:29:52 -07001335 min_spi = 0x100;
1336 max_spi = 0x0fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Herbert Xu658b2192007-10-09 13:29:52 -07001338 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1339 if (range) {
1340 min_spi = range->sadb_spirange_min;
1341 max_spi = range->sadb_spirange_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
Herbert Xu658b2192007-10-09 13:29:52 -07001343
Fan Du776e9dd2013-12-16 18:47:49 +08001344 err = verify_spi_info(x->id.proto, min_spi, max_spi);
1345 if (err) {
1346 xfrm_state_put(x);
1347 return err;
1348 }
1349
Herbert Xu658b2192007-10-09 13:29:52 -07001350 err = xfrm_alloc_spi(x, min_spi, max_spi);
Herbert Xu050f0092007-10-09 13:31:47 -07001351 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (IS_ERR(resp_skb)) {
1354 xfrm_state_put(x);
1355 return PTR_ERR(resp_skb);
1356 }
1357
1358 out_hdr = (struct sadb_msg *) resp_skb->data;
1359 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1360 out_hdr->sadb_msg_type = SADB_GETSPI;
1361 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1362 out_hdr->sadb_msg_errno = 0;
1363 out_hdr->sadb_msg_reserved = 0;
1364 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1365 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1366
1367 xfrm_state_put(x);
1368
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001369 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 return 0;
1372}
1373
David S. Miller4c93fbb2011-02-25 18:07:06 -08001374static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001376 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 struct xfrm_state *x;
1378
1379 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1380 return -EOPNOTSUPP;
1381
1382 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1383 return 0;
1384
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001385 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (x == NULL)
1387 return 0;
1388
1389 spin_lock_bh(&x->lock);
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001390 if (x->km.state == XFRM_STATE_ACQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 x->km.state = XFRM_STATE_ERROR;
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 spin_unlock_bh(&x->lock);
1394 xfrm_state_put(x);
1395 return 0;
1396}
1397
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001398static inline int event2poltype(int event)
1399{
1400 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001401 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001402 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001403 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001404 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001405 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001406 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001407 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001408 // return SADB_X_SPDEXPIRE;
1409 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001410 pr_err("pfkey: Unknown policy event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001411 break;
1412 }
1413
1414 return 0;
1415}
1416
1417static inline int event2keytype(int event)
1418{
1419 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001420 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001421 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001422 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001423 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001424 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001425 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001426 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001427 return SADB_EXPIRE;
1428 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001429 pr_err("pfkey: Unknown SA event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001430 break;
1431 }
1432
1433 return 0;
1434}
1435
1436/* ADD/UPD/DEL */
David S. Miller214e0052011-02-24 00:02:38 -05001437static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001438{
1439 struct sk_buff *skb;
1440 struct sadb_msg *hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001441
Herbert Xu050f0092007-10-09 13:31:47 -07001442 skb = pfkey_xfrm_state2msg(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001443
1444 if (IS_ERR(skb))
1445 return PTR_ERR(skb);
1446
1447 hdr = (struct sadb_msg *) skb->data;
1448 hdr->sadb_msg_version = PF_KEY_V2;
1449 hdr->sadb_msg_type = event2keytype(c->event);
1450 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1451 hdr->sadb_msg_errno = 0;
1452 hdr->sadb_msg_reserved = 0;
1453 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001454 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001455
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001456 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001457
1458 return 0;
1459}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
David S. Miller4c93fbb2011-02-25 18:07:06 -08001461static int pfkey_add(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001463 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 struct xfrm_state *x;
1465 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001466 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001468 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (IS_ERR(x))
1470 return PTR_ERR(x);
1471
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001472 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (hdr->sadb_msg_type == SADB_ADD)
1474 err = xfrm_state_add(x);
1475 else
1476 err = xfrm_state_update(x);
1477
Joy Lattenab5f5e82007-09-17 11:51:22 -07001478 xfrm_audit_state_add(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001479 audit_get_loginuid(current),
1480 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (err < 0) {
1483 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001484 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001488 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001489 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001490 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001491 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001492 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001493 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001494 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001495out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001496 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001497 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
David S. Miller4c93fbb2011-02-25 18:07:06 -08001500static int pfkey_delete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001502 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001504 struct km_event c;
1505 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 if (!ext_hdrs[SADB_EXT_SA-1] ||
1508 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1509 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1510 return -EINVAL;
1511
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001512 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (x == NULL)
1514 return -ESRCH;
1515
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001516 if ((err = security_xfrm_state_delete(x)))
1517 goto out;
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001520 err = -EPERM;
1521 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
Joy Latten161a09e2006-11-27 13:11:54 -06001523
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001524 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001525
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001526 if (err < 0)
1527 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001528
1529 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001530 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001531 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001532 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001533out:
Joy Lattenab5f5e82007-09-17 11:51:22 -07001534 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001535 audit_get_loginuid(current),
1536 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 xfrm_state_put(x);
1538
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001539 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
David S. Miller4c93fbb2011-02-25 18:07:06 -08001542static int pfkey_get(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001544 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 __u8 proto;
1546 struct sk_buff *out_skb;
1547 struct sadb_msg *out_hdr;
1548 struct xfrm_state *x;
1549
1550 if (!ext_hdrs[SADB_EXT_SA-1] ||
1551 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1552 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1553 return -EINVAL;
1554
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001555 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (x == NULL)
1557 return -ESRCH;
1558
Herbert Xu050f0092007-10-09 13:31:47 -07001559 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 proto = x->id.proto;
1561 xfrm_state_put(x);
1562 if (IS_ERR(out_skb))
1563 return PTR_ERR(out_skb);
1564
1565 out_hdr = (struct sadb_msg *) out_skb->data;
1566 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Charles Hardin435000b2007-11-22 19:35:15 +08001567 out_hdr->sadb_msg_type = SADB_GET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1569 out_hdr->sadb_msg_errno = 0;
1570 out_hdr->sadb_msg_reserved = 0;
1571 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1572 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001573 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 return 0;
1576}
1577
David S. Miller4c93fbb2011-02-25 18:07:06 -08001578static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001579 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 struct sk_buff *skb;
1582 struct sadb_msg *hdr;
1583 int len, auth_len, enc_len, i;
1584
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001585 auth_len = xfrm_count_pfkey_auth_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (auth_len) {
1587 auth_len *= sizeof(struct sadb_alg);
1588 auth_len += sizeof(struct sadb_supported);
1589 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001590
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001591 enc_len = xfrm_count_pfkey_enc_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (enc_len) {
1593 enc_len *= sizeof(struct sadb_alg);
1594 enc_len += sizeof(struct sadb_supported);
1595 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 len = enc_len + auth_len + sizeof(struct sadb_msg);
1598
1599 skb = alloc_skb(len + 16, allocation);
1600 if (!skb)
1601 goto out_put_algs;
1602
1603 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1604 pfkey_hdr_dup(hdr, orig);
1605 hdr->sadb_msg_errno = 0;
1606 hdr->sadb_msg_len = len / sizeof(uint64_t);
1607
1608 if (auth_len) {
1609 struct sadb_supported *sp;
1610 struct sadb_alg *ap;
1611
1612 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1613 ap = (struct sadb_alg *) (sp + 1);
1614
1615 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1616 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1617
1618 for (i = 0; ; i++) {
1619 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1620 if (!aalg)
1621 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001622 if (!aalg->pfkey_supported)
1623 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (aalg->available)
1625 *ap++ = aalg->desc;
1626 }
1627 }
1628
1629 if (enc_len) {
1630 struct sadb_supported *sp;
1631 struct sadb_alg *ap;
1632
1633 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1634 ap = (struct sadb_alg *) (sp + 1);
1635
1636 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1637 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1638
1639 for (i = 0; ; i++) {
1640 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1641 if (!ealg)
1642 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001643 if (!ealg->pfkey_supported)
1644 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (ealg->available)
1646 *ap++ = ealg->desc;
1647 }
1648 }
1649
1650out_put_algs:
1651 return skb;
1652}
1653
David S. Miller4c93fbb2011-02-25 18:07:06 -08001654static int pfkey_register(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 struct pfkey_sock *pfk = pfkey_sk(sk);
1657 struct sk_buff *supp_skb;
1658
1659 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1660 return -EINVAL;
1661
1662 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1663 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1664 return -EEXIST;
1665 pfk->registered |= (1<<hdr->sadb_msg_satype);
1666 }
1667
1668 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1671 if (!supp_skb) {
1672 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1673 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1674
1675 return -ENOBUFS;
1676 }
1677
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001678 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 return 0;
1681}
1682
David S. Miller4c93fbb2011-02-25 18:07:06 -08001683static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001684{
1685 struct sk_buff *skb;
1686 struct sadb_msg *hdr;
1687
1688 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1689 if (!skb)
1690 return -ENOBUFS;
1691
1692 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1693 memcpy(hdr, ihdr, sizeof(struct sadb_msg));
1694 hdr->sadb_msg_errno = (uint8_t) 0;
1695 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1696
1697 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
1698}
1699
David S. Miller214e0052011-02-24 00:02:38 -05001700static int key_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001701{
1702 struct sk_buff *skb;
1703 struct sadb_msg *hdr;
1704
1705 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1706 if (!skb)
1707 return -ENOBUFS;
1708 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf088672005-06-18 22:44:00 -07001709 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001710 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001711 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001712 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001713 hdr->sadb_msg_version = PF_KEY_V2;
1714 hdr->sadb_msg_errno = (uint8_t) 0;
1715 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02001716 hdr->sadb_msg_reserved = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001717
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001718 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001719
1720 return 0;
1721}
1722
David S. Miller4c93fbb2011-02-25 18:07:06 -08001723static int pfkey_flush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001725 struct net *net = sock_net(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +00001726 unsigned int proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001727 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06001728 struct xfrm_audit audit_info;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001729 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1732 if (proto == 0)
1733 return -EINVAL;
1734
Al Viro0c11b942008-01-10 04:20:52 -05001735 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04001736 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06001737 audit_info.secid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001738 err = xfrm_state_flush(net, proto, &audit_info);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001739 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001740 if (err || err2) {
1741 if (err == -ESRCH) /* empty table - go quietly */
1742 err = 0;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001743 return err ? err : err2;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001744 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001745
Herbert Xubf088672005-06-18 22:44:00 -07001746 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001747 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001748 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001749 c.event = XFRM_MSG_FLUSHSA;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001750 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001751 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 return 0;
1754}
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1757{
Timo Teras83321d62008-03-03 23:40:12 -08001758 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 struct sk_buff *out_skb;
1760 struct sadb_msg *out_hdr;
1761
Timo Teras83321d62008-03-03 23:40:12 -08001762 if (!pfkey_can_dump(&pfk->sk))
1763 return -ENOBUFS;
1764
Herbert Xu050f0092007-10-09 13:31:47 -07001765 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (IS_ERR(out_skb))
1767 return PTR_ERR(out_skb);
1768
1769 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08001770 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 out_hdr->sadb_msg_type = SADB_DUMP;
1772 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1773 out_hdr->sadb_msg_errno = 0;
1774 out_hdr->sadb_msg_reserved = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07001775 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001776 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07001777
1778 if (pfk->dump.skb)
1779 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001780 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07001781 pfk->dump.skb = out_skb;
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return 0;
1784}
1785
Timo Teras83321d62008-03-03 23:40:12 -08001786static int pfkey_dump_sa(struct pfkey_sock *pfk)
1787{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001788 struct net *net = sock_net(&pfk->sk);
1789 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08001790}
1791
1792static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1793{
Fan Du283bc9f2013-11-07 17:47:50 +08001794 struct net *net = sock_net(&pfk->sk);
1795
1796 xfrm_state_walk_done(&pfk->dump.u.state, net);
Timo Teras83321d62008-03-03 23:40:12 -08001797}
1798
David S. Miller4c93fbb2011-02-25 18:07:06 -08001799static int pfkey_dump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 u8 proto;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001802 struct xfrm_filter *filter = NULL;
Timo Teras83321d62008-03-03 23:40:12 -08001803 struct pfkey_sock *pfk = pfkey_sk(sk);
1804
1805 if (pfk->dump.dump != NULL)
1806 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1809 if (proto == 0)
1810 return -EINVAL;
1811
Nicolas Dichteld3623092014-02-14 15:30:36 +01001812 if (ext_hdrs[SADB_X_EXT_FILTER - 1]) {
1813 struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1];
1814
1815 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
1816 if (filter == NULL)
1817 return -ENOMEM;
1818
1819 memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr,
1820 sizeof(xfrm_address_t));
1821 memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr,
1822 sizeof(xfrm_address_t));
1823 filter->family = xfilter->sadb_x_filter_family;
1824 filter->splen = xfilter->sadb_x_filter_splen;
1825 filter->dplen = xfilter->sadb_x_filter_dplen;
1826 }
1827
Timo Teras83321d62008-03-03 23:40:12 -08001828 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001829 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08001830 pfk->dump.dump = pfkey_dump_sa;
1831 pfk->dump.done = pfkey_dump_sa_done;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001832 xfrm_state_walk_init(&pfk->dump.u.state, proto, filter);
Timo Teras4c563f72008-02-28 21:31:08 -08001833
Timo Teras83321d62008-03-03 23:40:12 -08001834 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
David S. Miller4c93fbb2011-02-25 18:07:06 -08001837static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 struct pfkey_sock *pfk = pfkey_sk(sk);
1840 int satype = hdr->sadb_msg_satype;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001841 bool reset_errno = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001844 reset_errno = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (satype != 0 && satype != 1)
1846 return -EINVAL;
1847 pfk->promisc = satype;
1848 }
David S. Miller4c93fbb2011-02-25 18:07:06 -08001849 if (reset_errno && skb_cloned(skb))
1850 skb = skb_copy(skb, GFP_KERNEL);
1851 else
1852 skb = skb_clone(skb, GFP_KERNEL);
1853
1854 if (reset_errno && skb) {
1855 struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
1856 new_hdr->sadb_msg_errno = 0;
1857 }
1858
1859 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return 0;
1861}
1862
1863static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1864{
1865 int i;
1866 u32 reqid = *(u32*)ptr;
1867
1868 for (i=0; i<xp->xfrm_nr; i++) {
1869 if (xp->xfrm_vec[i].reqid == reqid)
1870 return -EEXIST;
1871 }
1872 return 0;
1873}
1874
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001875static u32 gen_reqid(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Timo Teras4c563f72008-02-28 21:31:08 -08001877 struct xfrm_policy_walk walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 u32 start;
Timo Teras4c563f72008-02-28 21:31:08 -08001879 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1881
1882 start = reqid;
1883 do {
1884 ++reqid;
1885 if (reqid == 0)
1886 reqid = IPSEC_MANUAL_REQID_MAX+1;
Timo Teras4c563f72008-02-28 21:31:08 -08001887 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001888 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
Fan Du283bc9f2013-11-07 17:47:50 +08001889 xfrm_policy_walk_done(&walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001890 if (rc != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return reqid;
1892 } while (reqid != start);
1893 return 0;
1894}
1895
1896static int
1897parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1898{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001899 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001901 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1904 return -ELOOP;
1905
1906 if (rq->sadb_x_ipsecrequest_mode == 0)
1907 return -EINVAL;
1908
1909 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001910 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1911 return -EINVAL;
1912 t->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1914 t->optional = 1;
1915 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1916 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1917 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1918 t->reqid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001919 if (!t->reqid && !(t->reqid = gen_reqid(net)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return -ENOBUFS;
1921 }
1922
1923 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001924 if (t->mode == XFRM_MODE_TUNNEL) {
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001925 u8 *sa = (u8 *) (rq + 1);
1926 int family, socklen;
1927
1928 family = pfkey_sockaddr_extract((struct sockaddr *)sa,
1929 &t->saddr);
1930 if (!family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return -EINVAL;
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001932
1933 socklen = pfkey_sockaddr_len(family);
1934 if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen),
1935 &t->id.daddr) != family)
1936 return -EINVAL;
1937 t->encap_family = family;
Miika Komu2718aa72006-11-30 16:41:50 -08001938 } else
1939 t->encap_family = xp->family;
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /* No way to set this via kame pfkey */
Herbert Xuc5d18e92008-04-22 00:46:42 -07001942 t->allalgs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 xp->xfrm_nr++;
1944 return 0;
1945}
1946
1947static int
1948parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1949{
1950 int err;
1951 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1952 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1953
Dan Carpenterf674e722012-09-27 22:21:19 +00001954 if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
1955 return -EINVAL;
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1958 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1959 return err;
1960 len -= rq->sadb_x_ipsecrequest_len;
1961 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1962 }
1963 return 0;
1964}
1965
David S. Miller4c93fbb2011-02-25 18:07:06 -08001966static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
Trent Jaegerdf718372005-12-13 23:12:27 -08001967{
1968 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1969
1970 if (xfrm_ctx) {
1971 int len = sizeof(struct sadb_x_sec_ctx);
1972 len += xfrm_ctx->ctx_len;
1973 return PFKEY_ALIGN8(len);
1974 }
1975 return 0;
1976}
1977
David S. Miller4c93fbb2011-02-25 18:07:06 -08001978static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
David S. Miller4c93fbb2011-02-25 18:07:06 -08001980 const struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08001982 int socklen = 0;
1983 int i;
1984
1985 for (i=0; i<xp->xfrm_nr; i++) {
1986 t = xp->xfrm_vec + i;
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09001987 socklen += pfkey_sockaddr_len(t->encap_family);
Miika Komu2718aa72006-11-30 16:41:50 -08001988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 return sizeof(struct sadb_msg) +
1991 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001992 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 (sockaddr_size * 2) +
1994 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08001995 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1996 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08001997 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
David S. Miller4c93fbb2011-02-25 18:07:06 -08002000static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
2002 struct sk_buff *skb;
2003 int size;
2004
2005 size = pfkey_xfrm_policy2msg_size(xp);
2006
2007 skb = alloc_skb(size + 16, GFP_ATOMIC);
2008 if (skb == NULL)
2009 return ERR_PTR(-ENOBUFS);
2010
2011 return skb;
2012}
2013
David S. Miller4c93fbb2011-02-25 18:07:06 -08002014static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 struct sadb_msg *hdr;
2017 struct sadb_address *addr;
2018 struct sadb_lifetime *lifetime;
2019 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08002020 struct sadb_x_sec_ctx *sec_ctx;
2021 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 int i;
2023 int size;
2024 int sockaddr_size = pfkey_sockaddr_size(xp->family);
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002025 int socklen = pfkey_sockaddr_len(xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 size = pfkey_xfrm_policy2msg_size(xp);
2028
2029 /* call should fill header later */
2030 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2031 memset(hdr, 0, size); /* XXX do we need this ? */
2032
2033 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002034 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002036 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 (sizeof(struct sadb_address)+sockaddr_size)/
2038 sizeof(uint64_t);
2039 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2040 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2041 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2042 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002043 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2044 xp->selector.sport,
2045 (struct sockaddr *) (addr + 1),
2046 xp->family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 BUG();
2048
2049 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002050 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 sizeof(struct sadb_address)+sockaddr_size);
2052 addr->sadb_address_len =
2053 (sizeof(struct sadb_address)+sockaddr_size)/
2054 sizeof(uint64_t);
2055 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2056 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002057 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002059
2060 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2061 (struct sockaddr *) (addr + 1),
2062 xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 /* hard time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002065 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 sizeof(struct sadb_lifetime));
2067 lifetime->sadb_lifetime_len =
2068 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2069 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2070 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2071 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2072 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2073 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2074 /* soft time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002075 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 sizeof(struct sadb_lifetime));
2077 lifetime->sadb_lifetime_len =
2078 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2079 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2080 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2081 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2082 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2083 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2084 /* current time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002085 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 sizeof(struct sadb_lifetime));
2087 lifetime->sadb_lifetime_len =
2088 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2089 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2090 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2091 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2092 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2093 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2094
2095 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2096 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2097 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2098 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2099 if (xp->action == XFRM_POLICY_ALLOW) {
2100 if (xp->xfrm_nr)
2101 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2102 else
2103 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2104 }
2105 pol->sadb_x_policy_dir = dir+1;
Dan Carpenterff862a42013-07-28 23:04:45 +03002106 pol->sadb_x_policy_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 pol->sadb_x_policy_id = xp->index;
2108 pol->sadb_x_policy_priority = xp->priority;
2109
2110 for (i=0; i<xp->xfrm_nr; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002111 const struct xfrm_tmpl *t = xp->xfrm_vec + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 struct sadb_x_ipsecrequest *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 int req_size;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002114 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 req_size = sizeof(struct sadb_x_ipsecrequest);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002117 if (t->mode == XFRM_MODE_TUNNEL) {
2118 socklen = pfkey_sockaddr_len(t->encap_family);
2119 req_size += socklen * 2;
2120 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 size -= 2*socklen;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 rq = (void*)skb_put(skb, req_size);
2124 pol->sadb_x_policy_len += req_size/8;
2125 memset(rq, 0, sizeof(*rq));
2126 rq->sadb_x_ipsecrequest_len = req_size;
2127 rq->sadb_x_ipsecrequest_proto = t->id.proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002128 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2129 return -EINVAL;
David S. Millerfefaa752007-04-17 21:48:10 -07002130 rq->sadb_x_ipsecrequest_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2132 if (t->reqid)
2133 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2134 if (t->optional)
2135 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2136 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002138 if (t->mode == XFRM_MODE_TUNNEL) {
2139 u8 *sa = (void *)(rq + 1);
2140 pfkey_sockaddr_fill(&t->saddr, 0,
2141 (struct sockaddr *)sa,
2142 t->encap_family);
2143 pfkey_sockaddr_fill(&t->id.daddr, 0,
2144 (struct sockaddr *) (sa + socklen),
2145 t->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002148
2149 /* security context */
2150 if ((xfrm_ctx = xp->security)) {
2151 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2152
2153 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2154 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2155 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2156 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2157 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2158 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2159 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2160 xfrm_ctx->ctx_len);
2161 }
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 hdr->sadb_msg_len = size / sizeof(uint64_t);
2164 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002165
2166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
David S. Miller214e0052011-02-24 00:02:38 -05002169static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002170{
2171 struct sk_buff *out_skb;
2172 struct sadb_msg *out_hdr;
2173 int err;
2174
2175 out_skb = pfkey_xfrm_policy2msg_prep(xp);
Dan Carpenter9a127aa2010-03-24 01:47:00 +00002176 if (IS_ERR(out_skb))
2177 return PTR_ERR(out_skb);
2178
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002179 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2180 if (err < 0)
2181 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002182
2183 out_hdr = (struct sadb_msg *) out_skb->data;
2184 out_hdr->sadb_msg_version = PF_KEY_V2;
2185
Herbert Xuf60f6b82005-06-18 22:44:37 -07002186 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002187 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2188 else
2189 out_hdr->sadb_msg_type = event2poltype(c->event);
2190 out_hdr->sadb_msg_errno = 0;
2191 out_hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002192 out_hdr->sadb_msg_pid = c->portid;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002193 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002194 return 0;
2195
2196}
2197
David S. Miller4c93fbb2011-02-25 18:07:06 -08002198static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002200 struct net *net = sock_net(sk);
Trent Jaegerdf718372005-12-13 23:12:27 -08002201 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 struct sadb_lifetime *lifetime;
2203 struct sadb_address *sa;
2204 struct sadb_x_policy *pol;
2205 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002206 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002207 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2210 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2211 !ext_hdrs[SADB_X_EXT_POLICY-1])
2212 return -EINVAL;
2213
2214 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2215 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2216 return -EINVAL;
2217 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2218 return -EINVAL;
2219
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002220 xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (xp == NULL)
2222 return -ENOBUFS;
2223
2224 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2225 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2226 xp->priority = pol->sadb_x_policy_priority;
2227
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002228 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 xp->selector.family = xp->family;
2231 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2232 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2233 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2234 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002235 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002237 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2239 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2240
2241 /* Amusing, we set this twice. KAME apps appear to set same value
2242 * in both addresses.
2243 */
2244 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2245
2246 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2247 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002248 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Joe Perchesea110732011-06-13 16:21:26 +00002250 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002251 if (sec_ctx != NULL) {
2252 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2253
2254 if (!uctx) {
2255 err = -ENOBUFS;
2256 goto out;
2257 }
2258
Paul Moore03e1ad72008-04-12 19:07:52 -07002259 err = security_xfrm_policy_alloc(&xp->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002260 kfree(uctx);
2261
2262 if (err)
2263 goto out;
2264 }
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 xp->lft.soft_byte_limit = XFRM_INF;
2267 xp->lft.hard_byte_limit = XFRM_INF;
2268 xp->lft.soft_packet_limit = XFRM_INF;
2269 xp->lft.hard_packet_limit = XFRM_INF;
2270 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2271 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2272 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2273 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2274 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2275 }
2276 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2277 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2278 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2279 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2280 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2281 }
2282 xp->xfrm_nr = 0;
2283 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2284 (err = parse_ipsecrequests(xp, pol)) < 0)
2285 goto out;
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2288 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002289
Joy Lattenab5f5e82007-09-17 11:51:22 -07002290 xfrm_audit_policy_add(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002291 audit_get_loginuid(current),
2292 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06002293
Trent Jaegerdf718372005-12-13 23:12:27 -08002294 if (err)
2295 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002297 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002298 c.event = XFRM_MSG_UPDPOLICY;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002299 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07002300 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002302 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002303 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002304
2305 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return 0;
2308
2309out:
Herbert Xu12a169e2008-10-01 07:03:24 -07002310 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08002311 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return err;
2313}
2314
David S. Miller4c93fbb2011-02-25 18:07:06 -08002315static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002317 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 int err;
2319 struct sadb_address *sa;
2320 struct sadb_x_policy *pol;
Paul Moore03e1ad72008-04-12 19:07:52 -07002321 struct xfrm_policy *xp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002323 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002324 struct sadb_x_sec_ctx *sec_ctx;
Brian Haley2db3e472008-04-24 20:38:31 -07002325 struct xfrm_sec_ctx *pol_ctx = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2328 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2329 !ext_hdrs[SADB_X_EXT_POLICY-1])
2330 return -EINVAL;
2331
2332 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2333 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2334 return -EINVAL;
2335
2336 memset(&sel, 0, sizeof(sel));
2337
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002338 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2340 sel.prefixlen_s = sa->sadb_address_prefixlen;
2341 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2342 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2343 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002344 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002346 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2348 sel.prefixlen_d = sa->sadb_address_prefixlen;
2349 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2350 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2351 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002352 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Joe Perchesea110732011-06-13 16:21:26 +00002354 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002355 if (sec_ctx != NULL) {
2356 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2357
2358 if (!uctx)
2359 return -ENOMEM;
2360
Paul Moore03e1ad72008-04-12 19:07:52 -07002361 err = security_xfrm_policy_alloc(&pol_ctx, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002362 kfree(uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002363 if (err)
2364 return err;
Brian Haley2db3e472008-04-24 20:38:31 -07002365 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002366
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002367 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Paul Moore03e1ad72008-04-12 19:07:52 -07002368 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2369 1, &err);
2370 security_xfrm_policy_free(pol_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (xp == NULL)
2372 return -ENOENT;
2373
Joy Lattenab5f5e82007-09-17 11:51:22 -07002374 xfrm_audit_policy_delete(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002375 audit_get_loginuid(current),
2376 audit_get_sessionid(current), 0);
David S. Miller13fcfbb02007-02-12 13:53:54 -08002377
2378 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002379 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08002380
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002381 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002382 c.portid = hdr->sadb_msg_pid;
Tobias Brunner1839faa2008-10-10 14:07:03 -07002383 c.data.byid = 0;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002384 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002385 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2386
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002387out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002388 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00002389 if (err == 0)
2390 xfrm_garbage_collect(net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002391 return err;
2392}
2393
David S. Miller4c93fbb2011-02-25 18:07:06 -08002394static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, const struct sadb_msg *hdr, int dir)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002395{
2396 int err;
2397 struct sk_buff *out_skb;
2398 struct sadb_msg *out_hdr;
2399 err = 0;
2400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2402 if (IS_ERR(out_skb)) {
2403 err = PTR_ERR(out_skb);
2404 goto out;
2405 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002406 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2407 if (err < 0)
2408 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 out_hdr = (struct sadb_msg *) out_skb->data;
2411 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002412 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 out_hdr->sadb_msg_satype = 0;
2414 out_hdr->sadb_msg_errno = 0;
2415 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2416 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002417 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 err = 0;
2419
2420out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 return err;
2422}
2423
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002424#ifdef CONFIG_NET_KEY_MIGRATE
2425static int pfkey_sockaddr_pair_size(sa_family_t family)
2426{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002427 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002428}
2429
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002430static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002431 xfrm_address_t *saddr, xfrm_address_t *daddr,
2432 u16 *family)
2433{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002434 int af, socklen;
2435
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002436 if (ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002437 return -EINVAL;
2438
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002439 af = pfkey_sockaddr_extract(sa, saddr);
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002440 if (!af)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002441 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002442
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002443 socklen = pfkey_sockaddr_len(af);
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002444 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002445 daddr) != af)
2446 return -EINVAL;
2447
2448 *family = af;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002449 return 0;
2450}
2451
2452static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2453 struct xfrm_migrate *m)
2454{
2455 int err;
2456 struct sadb_x_ipsecrequest *rq2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002457 int mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002458
2459 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2460 len < rq1->sadb_x_ipsecrequest_len)
2461 return -EINVAL;
2462
2463 /* old endoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002464 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
2465 rq1->sadb_x_ipsecrequest_len,
2466 &m->old_saddr, &m->old_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002467 &m->old_family);
2468 if (err)
2469 return err;
2470
2471 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2472 len -= rq1->sadb_x_ipsecrequest_len;
2473
2474 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2475 len < rq2->sadb_x_ipsecrequest_len)
2476 return -EINVAL;
2477
2478 /* new endpoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002479 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
2480 rq2->sadb_x_ipsecrequest_len,
2481 &m->new_saddr, &m->new_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002482 &m->new_family);
2483 if (err)
2484 return err;
2485
2486 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2487 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2488 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2489 return -EINVAL;
2490
2491 m->proto = rq1->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002492 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2493 return -EINVAL;
2494 m->mode = mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002495 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2496
2497 return ((int)(rq1->sadb_x_ipsecrequest_len +
2498 rq2->sadb_x_ipsecrequest_len));
2499}
2500
2501static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002502 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002503{
2504 int i, len, ret, err = -EINVAL;
2505 u8 dir;
2506 struct sadb_address *sa;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002507 struct sadb_x_kmaddress *kma;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002508 struct sadb_x_policy *pol;
2509 struct sadb_x_ipsecrequest *rq;
2510 struct xfrm_selector sel;
2511 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002512 struct xfrm_kmaddress k;
Fan Du8d549c42013-11-07 17:47:49 +08002513 struct net *net = sock_net(sk);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002514
2515 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002516 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002517 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2518 err = -EINVAL;
2519 goto out;
2520 }
2521
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002522 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002523 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002524
2525 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2526 err = -EINVAL;
2527 goto out;
2528 }
2529
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002530 if (kma) {
2531 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2532 k.reserved = kma->sadb_x_kmaddress_reserved;
2533 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2534 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2535 &k.local, &k.remote, &k.family);
2536 if (ret < 0) {
2537 err = ret;
2538 goto out;
2539 }
2540 }
2541
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002542 dir = pol->sadb_x_policy_dir - 1;
2543 memset(&sel, 0, sizeof(sel));
2544
2545 /* set source address info of selector */
2546 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2547 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2548 sel.prefixlen_s = sa->sadb_address_prefixlen;
2549 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2550 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2551 if (sel.sport)
Al Viro582ee432007-07-26 17:33:39 +01002552 sel.sport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002553
2554 /* set destination address info of selector */
2555 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2556 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2557 sel.prefixlen_d = sa->sadb_address_prefixlen;
2558 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2559 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2560 if (sel.dport)
Al Viro582ee432007-07-26 17:33:39 +01002561 sel.dport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002562
2563 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2564
2565 /* extract ipsecrequests */
2566 i = 0;
2567 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2568
2569 while (len > 0 && i < XFRM_MAX_DEPTH) {
2570 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2571 if (ret < 0) {
2572 err = ret;
2573 goto out;
2574 } else {
2575 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2576 len -= ret;
2577 i++;
2578 }
2579 }
2580
2581 if (!i || len > 0) {
2582 err = -EINVAL;
2583 goto out;
2584 }
2585
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002586 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
Fan Du8d549c42013-11-07 17:47:49 +08002587 kma ? &k : NULL, net);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002588
2589 out:
2590 return err;
2591}
2592#else
2593static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
Stephen Hemminger7f6daa62011-03-01 22:51:52 -08002594 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002595{
2596 return -ENOPROTOOPT;
2597}
2598#endif
2599
2600
David S. Miller4c93fbb2011-02-25 18:07:06 -08002601static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002603 struct net *net = sock_net(sk);
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002604 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002605 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 struct sadb_x_policy *pol;
2607 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002608 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2611 return -EINVAL;
2612
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002613 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2614 if (dir >= XFRM_POLICY_MAX)
2615 return -EINVAL;
2616
Eric Paris215a2dd2007-03-07 16:01:45 -08002617 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002618 xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002619 dir, pol->sadb_x_policy_id, delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 if (xp == NULL)
2621 return -ENOENT;
2622
Eric Paris215a2dd2007-03-07 16:01:45 -08002623 if (delete) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002624 xfrm_audit_policy_delete(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002625 audit_get_loginuid(current),
2626 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Eric Paris215a2dd2007-03-07 16:01:45 -08002628 if (err)
2629 goto out;
2630 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002631 c.portid = hdr->sadb_msg_pid;
Herbert Xubf088672005-06-18 22:44:00 -07002632 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002633 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002634 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002635 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002636 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Eric Paris215a2dd2007-03-07 16:01:45 -08002639out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00002641 if (delete && err == 0)
2642 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return err;
2644}
2645
2646static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2647{
Timo Teras83321d62008-03-03 23:40:12 -08002648 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 struct sk_buff *out_skb;
2650 struct sadb_msg *out_hdr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002651 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Timo Teras83321d62008-03-03 23:40:12 -08002653 if (!pfkey_can_dump(&pfk->sk))
2654 return -ENOBUFS;
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2657 if (IS_ERR(out_skb))
2658 return PTR_ERR(out_skb);
2659
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002660 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2661 if (err < 0)
2662 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
2664 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08002665 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2667 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2668 out_hdr->sadb_msg_errno = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07002669 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002670 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07002671
2672 if (pfk->dump.skb)
2673 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002674 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07002675 pfk->dump.skb = out_skb;
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 return 0;
2678}
2679
Timo Teras83321d62008-03-03 23:40:12 -08002680static int pfkey_dump_sp(struct pfkey_sock *pfk)
2681{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002682 struct net *net = sock_net(&pfk->sk);
2683 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08002684}
2685
2686static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2687{
Fan Du283bc9f2013-11-07 17:47:50 +08002688 struct net *net = sock_net((struct sock *)pfk);
2689
2690 xfrm_policy_walk_done(&pfk->dump.u.policy, net);
Timo Teras83321d62008-03-03 23:40:12 -08002691}
2692
David S. Miller4c93fbb2011-02-25 18:07:06 -08002693static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Timo Teras83321d62008-03-03 23:40:12 -08002695 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Timo Teras83321d62008-03-03 23:40:12 -08002697 if (pfk->dump.dump != NULL)
2698 return -EBUSY;
Timo Teras4c563f72008-02-28 21:31:08 -08002699
Timo Teras83321d62008-03-03 23:40:12 -08002700 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002701 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08002702 pfk->dump.dump = pfkey_dump_sp;
2703 pfk->dump.done = pfkey_dump_sp_done;
2704 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2705
2706 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707}
2708
David S. Miller214e0052011-02-24 00:02:38 -05002709static int key_notify_policy_flush(const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
2711 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002712 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002714 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 if (!skb_out)
2716 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002717 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002718 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002719 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002720 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002721 hdr->sadb_msg_version = PF_KEY_V2;
2722 hdr->sadb_msg_errno = (uint8_t) 0;
Nicolas Dichtel85dfb742013-02-18 16:24:20 +01002723 hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002724 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02002725 hdr->sadb_msg_reserved = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002726 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002727 return 0;
2728
2729}
2730
David S. Miller4c93fbb2011-02-25 18:07:06 -08002731static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002732{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002733 struct net *net = sock_net(sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002734 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06002735 struct xfrm_audit audit_info;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002736 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Al Viro0c11b942008-01-10 04:20:52 -05002738 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04002739 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06002740 audit_info.secid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002741 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002742 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002743 if (err || err2) {
2744 if (err == -ESRCH) /* empty table - old silent behavior */
2745 return 0;
2746 return err;
2747 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002748
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002749 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002750 c.event = XFRM_MSG_FLUSHPOLICY;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002751 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002752 c.seq = hdr->sadb_msg_seq;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002753 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002754 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 return 0;
2757}
2758
2759typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002760 const struct sadb_msg *hdr, void * const *ext_hdrs);
Mathias Krause8603b9552013-06-26 23:56:59 +02002761static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 [SADB_RESERVED] = pfkey_reserved,
2763 [SADB_GETSPI] = pfkey_getspi,
2764 [SADB_UPDATE] = pfkey_add,
2765 [SADB_ADD] = pfkey_add,
2766 [SADB_DELETE] = pfkey_delete,
2767 [SADB_GET] = pfkey_get,
2768 [SADB_ACQUIRE] = pfkey_acquire,
2769 [SADB_REGISTER] = pfkey_register,
2770 [SADB_EXPIRE] = NULL,
2771 [SADB_FLUSH] = pfkey_flush,
2772 [SADB_DUMP] = pfkey_dump,
2773 [SADB_X_PROMISC] = pfkey_promisc,
2774 [SADB_X_PCHANGE] = NULL,
2775 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2776 [SADB_X_SPDADD] = pfkey_spdadd,
2777 [SADB_X_SPDDELETE] = pfkey_spddelete,
2778 [SADB_X_SPDGET] = pfkey_spdget,
2779 [SADB_X_SPDACQUIRE] = NULL,
2780 [SADB_X_SPDDUMP] = pfkey_spddump,
2781 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2782 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2783 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002784 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785};
2786
David S. Miller4c93fbb2011-02-25 18:07:06 -08002787static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
2789 void *ext_hdrs[SADB_EXT_MAX];
2790 int err;
2791
2792 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002793 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2796 err = parse_exthdrs(skb, hdr, ext_hdrs);
2797 if (!err) {
2798 err = -EOPNOTSUPP;
2799 if (pfkey_funcs[hdr->sadb_msg_type])
2800 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2801 }
2802 return err;
2803}
2804
2805static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2806{
2807 struct sadb_msg *hdr = NULL;
2808
2809 if (skb->len < sizeof(*hdr)) {
2810 *errp = -EMSGSIZE;
2811 } else {
2812 hdr = (struct sadb_msg *) skb->data;
2813 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2814 hdr->sadb_msg_reserved != 0 ||
2815 (hdr->sadb_msg_type <= SADB_RESERVED ||
2816 hdr->sadb_msg_type > SADB_MAX)) {
2817 hdr = NULL;
2818 *errp = -EINVAL;
2819 } else if (hdr->sadb_msg_len != (skb->len /
2820 sizeof(uint64_t)) ||
2821 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2822 sizeof(uint64_t))) {
2823 hdr = NULL;
2824 *errp = -EMSGSIZE;
2825 } else {
2826 *errp = 0;
2827 }
2828 }
2829 return hdr;
2830}
2831
David S. Miller4c93fbb2011-02-25 18:07:06 -08002832static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
2833 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
Herbert Xuf3980352007-12-19 23:44:29 -08002835 unsigned int id = d->desc.sadb_alg_id;
2836
2837 if (id >= sizeof(t->aalgos) * 8)
2838 return 0;
2839
2840 return (t->aalgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841}
2842
David S. Miller4c93fbb2011-02-25 18:07:06 -08002843static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
2844 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
Herbert Xuf3980352007-12-19 23:44:29 -08002846 unsigned int id = d->desc.sadb_alg_id;
2847
2848 if (id >= sizeof(t->ealgos) * 8)
2849 return 0;
2850
2851 return (t->ealgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
2853
David S. Miller4c93fbb2011-02-25 18:07:06 -08002854static int count_ah_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 int i, sz = 0;
2857
2858 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002859 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if (!aalg)
2861 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002862 if (!aalg->pfkey_supported)
2863 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (aalg_tmpl_set(t, aalg) && aalg->available)
2865 sz += sizeof(struct sadb_comb);
2866 }
2867 return sz + sizeof(struct sadb_prop);
2868}
2869
David S. Miller4c93fbb2011-02-25 18:07:06 -08002870static int count_esp_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
2872 int i, k, sz = 0;
2873
2874 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002875 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 if (!ealg)
2877 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002878
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002879 if (!ealg->pfkey_supported)
2880 continue;
2881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2883 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 for (k = 1; ; k++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002886 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (!aalg)
2888 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002889
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002890 if (!aalg->pfkey_supported)
2891 continue;
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (aalg_tmpl_set(t, aalg) && aalg->available)
2894 sz += sizeof(struct sadb_comb);
2895 }
2896 }
2897 return sz + sizeof(struct sadb_prop);
2898}
2899
David S. Miller4c93fbb2011-02-25 18:07:06 -08002900static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
2902 struct sadb_prop *p;
2903 int i;
2904
2905 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2906 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2907 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2908 p->sadb_prop_replay = 32;
2909 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2910
2911 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002912 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 if (!aalg)
2914 break;
2915
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002916 if (!aalg->pfkey_supported)
2917 continue;
2918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2920 struct sadb_comb *c;
2921 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2922 memset(c, 0, sizeof(*c));
2923 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2924 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2925 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2926 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2927 c->sadb_comb_hard_addtime = 24*60*60;
2928 c->sadb_comb_soft_addtime = 20*60*60;
2929 c->sadb_comb_hard_usetime = 8*60*60;
2930 c->sadb_comb_soft_usetime = 7*60*60;
2931 }
2932 }
2933}
2934
David S. Miller4c93fbb2011-02-25 18:07:06 -08002935static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
2937 struct sadb_prop *p;
2938 int i, k;
2939
2940 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2941 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2942 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2943 p->sadb_prop_replay = 32;
2944 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2945
2946 for (i=0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002947 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (!ealg)
2949 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002950
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002951 if (!ealg->pfkey_supported)
2952 continue;
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2955 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 for (k = 1; ; k++) {
2958 struct sadb_comb *c;
David S. Miller4c93fbb2011-02-25 18:07:06 -08002959 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 if (!aalg)
2961 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002962 if (!aalg->pfkey_supported)
2963 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2965 continue;
2966 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2967 memset(c, 0, sizeof(*c));
2968 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2969 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2970 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2971 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2972 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2973 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2974 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2975 c->sadb_comb_hard_addtime = 24*60*60;
2976 c->sadb_comb_soft_addtime = 20*60*60;
2977 c->sadb_comb_hard_usetime = 8*60*60;
2978 c->sadb_comb_soft_usetime = 7*60*60;
2979 }
2980 }
2981}
2982
David S. Miller214e0052011-02-24 00:02:38 -05002983static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002984{
2985 return 0;
2986}
2987
David S. Miller214e0052011-02-24 00:02:38 -05002988static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
2990 struct sk_buff *out_skb;
2991 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002992 int hard;
2993 int hsc;
2994
Herbert Xubf088672005-06-18 22:44:00 -07002995 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002996 if (hard)
2997 hsc = 2;
2998 else
2999 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Herbert Xu050f0092007-10-09 13:31:47 -07003001 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 if (IS_ERR(out_skb))
3003 return PTR_ERR(out_skb);
3004
3005 out_hdr = (struct sadb_msg *) out_skb->data;
3006 out_hdr->sadb_msg_version = PF_KEY_V2;
3007 out_hdr->sadb_msg_type = SADB_EXPIRE;
3008 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3009 out_hdr->sadb_msg_errno = 0;
3010 out_hdr->sadb_msg_reserved = 0;
3011 out_hdr->sadb_msg_seq = 0;
3012 out_hdr->sadb_msg_pid = 0;
3013
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003014 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return 0;
3016}
3017
David S. Miller214e0052011-02-24 00:02:38 -05003018static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003019{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003020 struct net *net = x ? xs_net(x) : c->net;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003021 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3022
3023 if (atomic_read(&net_pfkey->socks_nr) == 0)
Jamal Hadi Salim99c6f602008-06-10 14:25:34 -07003024 return 0;
3025
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003026 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003027 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003028 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003029 case XFRM_MSG_DELSA:
3030 case XFRM_MSG_NEWSA:
3031 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003032 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003033 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003034 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003035 case XFRM_MSG_NEWAE: /* not yet supported */
3036 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003037 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003038 pr_err("pfkey: Unknown SA event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003039 break;
3040 }
3041
3042 return 0;
3043}
3044
David S. Miller214e0052011-02-24 00:02:38 -05003045static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003046{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003047 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3048 return 0;
3049
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003050 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003051 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003052 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003053 case XFRM_MSG_DELPOLICY:
3054 case XFRM_MSG_NEWPOLICY:
3055 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003056 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003057 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003058 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3059 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003060 return key_notify_policy_flush(c);
3061 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003062 pr_err("pfkey: Unknown policy event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003063 break;
3064 }
3065
3066 return 0;
3067}
3068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069static u32 get_acqseq(void)
3070{
3071 u32 res;
Eric Dumazet28aecb92010-01-29 04:05:52 +00003072 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Eric Dumazet28aecb92010-01-29 04:05:52 +00003074 do {
3075 res = atomic_inc_return(&acqseq);
3076 } while (!res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return res;
3078}
3079
Horia Geanta0f245582014-02-12 16:20:06 +02003080static bool pfkey_is_alive(const struct km_event *c)
3081{
3082 struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id);
3083 struct sock *sk;
3084 bool is_alive = false;
3085
3086 rcu_read_lock();
3087 sk_for_each_rcu(sk, &net_pfkey->table) {
3088 if (pfkey_sk(sk)->registered) {
3089 is_alive = true;
3090 break;
3091 }
3092 }
3093 rcu_read_unlock();
3094
3095 return is_alive;
3096}
3097
Fan Du65e07362012-08-15 10:13:47 +08003098static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
3100 struct sk_buff *skb;
3101 struct sadb_msg *hdr;
3102 struct sadb_address *addr;
3103 struct sadb_x_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 int sockaddr_size;
3105 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003106 struct sadb_x_sec_ctx *sec_ctx;
3107 struct xfrm_sec_ctx *xfrm_ctx;
3108 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3111 if (!sockaddr_size)
3112 return -EINVAL;
3113
3114 size = sizeof(struct sadb_msg) +
3115 (sizeof(struct sadb_address) * 2) +
3116 (sockaddr_size * 2) +
3117 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 if (x->id.proto == IPPROTO_AH)
3120 size += count_ah_combs(t);
3121 else if (x->id.proto == IPPROTO_ESP)
3122 size += count_esp_combs(t);
3123
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003124 if ((xfrm_ctx = x->security)) {
3125 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3126 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3127 }
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 skb = alloc_skb(size + 16, GFP_ATOMIC);
3130 if (skb == NULL)
3131 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3134 hdr->sadb_msg_version = PF_KEY_V2;
3135 hdr->sadb_msg_type = SADB_ACQUIRE;
3136 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3137 hdr->sadb_msg_len = size / sizeof(uint64_t);
3138 hdr->sadb_msg_errno = 0;
3139 hdr->sadb_msg_reserved = 0;
3140 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3141 hdr->sadb_msg_pid = 0;
3142
3143 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003144 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003146 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 (sizeof(struct sadb_address)+sockaddr_size)/
3148 sizeof(uint64_t);
3149 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3150 addr->sadb_address_proto = 0;
3151 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003152 addr->sadb_address_prefixlen =
3153 pfkey_sockaddr_fill(&x->props.saddr, 0,
3154 (struct sockaddr *) (addr + 1),
3155 x->props.family);
3156 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003160 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 sizeof(struct sadb_address)+sockaddr_size);
3162 addr->sadb_address_len =
3163 (sizeof(struct sadb_address)+sockaddr_size)/
3164 sizeof(uint64_t);
3165 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3166 addr->sadb_address_proto = 0;
3167 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003168 addr->sadb_address_prefixlen =
3169 pfkey_sockaddr_fill(&x->id.daddr, 0,
3170 (struct sockaddr *) (addr + 1),
3171 x->props.family);
3172 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 BUG();
3174
3175 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3176 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3177 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3178 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
Fan Du65e07362012-08-15 10:13:47 +08003179 pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1;
Dan Carpenterff862a42013-07-28 23:04:45 +03003180 pol->sadb_x_policy_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 pol->sadb_x_policy_id = xp->index;
Dan Carpenterff862a42013-07-28 23:04:45 +03003182 pol->sadb_x_policy_priority = xp->priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184 /* Set sadb_comb's. */
3185 if (x->id.proto == IPPROTO_AH)
3186 dump_ah_combs(skb, t);
3187 else if (x->id.proto == IPPROTO_ESP)
3188 dump_esp_combs(skb, t);
3189
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003190 /* security context */
3191 if (xfrm_ctx) {
3192 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3193 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3194 sec_ctx->sadb_x_sec_len =
3195 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3196 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3197 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3198 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3199 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3200 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3201 xfrm_ctx->ctx_len);
3202 }
3203
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003204 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003207static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003208 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003210 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 struct xfrm_policy *xp;
3212 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003213 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003215 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 case AF_INET:
3217 if (opt != IP_IPSEC_POLICY) {
3218 *dir = -EOPNOTSUPP;
3219 return NULL;
3220 }
3221 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00003222#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 case AF_INET6:
3224 if (opt != IPV6_IPSEC_POLICY) {
3225 *dir = -EOPNOTSUPP;
3226 return NULL;
3227 }
3228 break;
3229#endif
3230 default:
3231 *dir = -EINVAL;
3232 return NULL;
3233 }
3234
3235 *dir = -EINVAL;
3236
3237 if (len < sizeof(struct sadb_x_policy) ||
3238 pol->sadb_x_policy_len*8 > len ||
3239 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3240 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3241 return NULL;
3242
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003243 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (xp == NULL) {
3245 *dir = -ENOBUFS;
3246 return NULL;
3247 }
3248
3249 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3250 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3251
3252 xp->lft.soft_byte_limit = XFRM_INF;
3253 xp->lft.hard_byte_limit = XFRM_INF;
3254 xp->lft.soft_packet_limit = XFRM_INF;
3255 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003256 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
3258 xp->xfrm_nr = 0;
3259 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3260 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3261 goto out;
3262
Trent Jaegerdf718372005-12-13 23:12:27 -08003263 /* security context too */
3264 if (len >= (pol->sadb_x_policy_len*8 +
3265 sizeof(struct sadb_x_sec_ctx))) {
3266 char *p = (char *)pol;
3267 struct xfrm_user_sec_ctx *uctx;
3268
3269 p += pol->sadb_x_policy_len*8;
3270 sec_ctx = (struct sadb_x_sec_ctx *)p;
3271 if (len < pol->sadb_x_policy_len*8 +
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003272 sec_ctx->sadb_x_sec_len) {
3273 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003274 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003275 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003276 if ((*dir = verify_sec_ctx_len(p)))
3277 goto out;
3278 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
Paul Moore03e1ad72008-04-12 19:07:52 -07003279 *dir = security_xfrm_policy_alloc(&xp->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08003280 kfree(uctx);
3281
3282 if (*dir)
3283 goto out;
3284 }
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 *dir = pol->sadb_x_policy_dir-1;
3287 return xp;
3288
3289out:
Alexey Dobriyan70e90672008-11-06 23:08:37 -08003290 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08003291 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 return NULL;
3293}
3294
Al Viro5d36b182006-11-08 00:24:06 -08003295static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
3297 struct sk_buff *skb;
3298 struct sadb_msg *hdr;
3299 struct sadb_sa *sa;
3300 struct sadb_address *addr;
3301 struct sadb_x_nat_t_port *n_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 int sockaddr_size;
3303 int size;
3304 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3305 struct xfrm_encap_tmpl *natt = NULL;
3306
3307 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3308 if (!sockaddr_size)
3309 return -EINVAL;
3310
3311 if (!satype)
3312 return -EINVAL;
3313
3314 if (!x->encap)
3315 return -EINVAL;
3316
3317 natt = x->encap;
3318
3319 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3320 *
3321 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3322 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3323 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 size = sizeof(struct sadb_msg) +
3326 sizeof(struct sadb_sa) +
3327 (sizeof(struct sadb_address) * 2) +
3328 (sockaddr_size * 2) +
3329 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 skb = alloc_skb(size + 16, GFP_ATOMIC);
3332 if (skb == NULL)
3333 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3336 hdr->sadb_msg_version = PF_KEY_V2;
3337 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3338 hdr->sadb_msg_satype = satype;
3339 hdr->sadb_msg_len = size / sizeof(uint64_t);
3340 hdr->sadb_msg_errno = 0;
3341 hdr->sadb_msg_reserved = 0;
3342 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3343 hdr->sadb_msg_pid = 0;
3344
3345 /* SA */
3346 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3347 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3348 sa->sadb_sa_exttype = SADB_EXT_SA;
3349 sa->sadb_sa_spi = x->id.spi;
3350 sa->sadb_sa_replay = 0;
3351 sa->sadb_sa_state = 0;
3352 sa->sadb_sa_auth = 0;
3353 sa->sadb_sa_encrypt = 0;
3354 sa->sadb_sa_flags = 0;
3355
3356 /* ADDRESS_SRC (old addr) */
3357 addr = (struct sadb_address*)
3358 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003359 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 (sizeof(struct sadb_address)+sockaddr_size)/
3361 sizeof(uint64_t);
3362 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3363 addr->sadb_address_proto = 0;
3364 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003365 addr->sadb_address_prefixlen =
3366 pfkey_sockaddr_fill(&x->props.saddr, 0,
3367 (struct sockaddr *) (addr + 1),
3368 x->props.family);
3369 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 BUG();
3371
3372 /* NAT_T_SPORT (old port) */
3373 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3374 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3375 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3376 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3377 n_port->sadb_x_nat_t_port_reserved = 0;
3378
3379 /* ADDRESS_DST (new addr) */
3380 addr = (struct sadb_address*)
3381 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003382 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 (sizeof(struct sadb_address)+sockaddr_size)/
3384 sizeof(uint64_t);
3385 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3386 addr->sadb_address_proto = 0;
3387 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003388 addr->sadb_address_prefixlen =
3389 pfkey_sockaddr_fill(ipaddr, 0,
3390 (struct sockaddr *) (addr + 1),
3391 x->props.family);
3392 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 BUG();
3394
3395 /* NAT_T_DPORT (new port) */
3396 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3397 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3398 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3399 n_port->sadb_x_nat_t_port_port = sport;
3400 n_port->sadb_x_nat_t_port_reserved = 0;
3401
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003402 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003405#ifdef CONFIG_NET_KEY_MIGRATE
3406static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
David S. Miller183cad12011-02-24 00:28:01 -05003407 const struct xfrm_selector *sel)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003408{
3409 struct sadb_address *addr;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003410 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3411 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3412 addr->sadb_address_exttype = type;
3413 addr->sadb_address_proto = sel->proto;
3414 addr->sadb_address_reserved = 0;
3415
3416 switch (type) {
3417 case SADB_EXT_ADDRESS_SRC:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003418 addr->sadb_address_prefixlen = sel->prefixlen_s;
3419 pfkey_sockaddr_fill(&sel->saddr, 0,
3420 (struct sockaddr *)(addr + 1),
3421 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003422 break;
3423 case SADB_EXT_ADDRESS_DST:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003424 addr->sadb_address_prefixlen = sel->prefixlen_d;
3425 pfkey_sockaddr_fill(&sel->daddr, 0,
3426 (struct sockaddr *)(addr + 1),
3427 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003428 break;
3429 default:
3430 return -EINVAL;
3431 }
3432
3433 return 0;
3434}
3435
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003436
David S. Miller183cad12011-02-24 00:28:01 -05003437static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003438{
3439 struct sadb_x_kmaddress *kma;
3440 u8 *sa;
3441 int family = k->family;
3442 int socklen = pfkey_sockaddr_len(family);
3443 int size_req;
3444
3445 size_req = (sizeof(struct sadb_x_kmaddress) +
3446 pfkey_sockaddr_pair_size(family));
3447
3448 kma = (struct sadb_x_kmaddress *)skb_put(skb, size_req);
3449 memset(kma, 0, size_req);
3450 kma->sadb_x_kmaddress_len = size_req / 8;
3451 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3452 kma->sadb_x_kmaddress_reserved = k->reserved;
3453
3454 sa = (u8 *)(kma + 1);
3455 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3456 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3457 return -EINVAL;
3458
3459 return 0;
3460}
3461
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003462static int set_ipsecrequest(struct sk_buff *skb,
3463 uint8_t proto, uint8_t mode, int level,
3464 uint32_t reqid, uint8_t family,
David S. Miller183cad12011-02-24 00:28:01 -05003465 const xfrm_address_t *src, const xfrm_address_t *dst)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003466{
3467 struct sadb_x_ipsecrequest *rq;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003468 u8 *sa;
3469 int socklen = pfkey_sockaddr_len(family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003470 int size_req;
3471
3472 size_req = sizeof(struct sadb_x_ipsecrequest) +
3473 pfkey_sockaddr_pair_size(family);
3474
3475 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3476 memset(rq, 0, size_req);
3477 rq->sadb_x_ipsecrequest_len = size_req;
3478 rq->sadb_x_ipsecrequest_proto = proto;
3479 rq->sadb_x_ipsecrequest_mode = mode;
3480 rq->sadb_x_ipsecrequest_level = level;
3481 rq->sadb_x_ipsecrequest_reqid = reqid;
3482
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003483 sa = (u8 *) (rq + 1);
3484 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3485 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003486 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003487
3488 return 0;
3489}
3490#endif
3491
3492#ifdef CONFIG_NET_KEY_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05003493static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3494 const struct xfrm_migrate *m, int num_bundles,
3495 const struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003496{
3497 int i;
3498 int sasize_sel;
3499 int size = 0;
3500 int size_pol = 0;
3501 struct sk_buff *skb;
3502 struct sadb_msg *hdr;
3503 struct sadb_x_policy *pol;
David S. Miller183cad12011-02-24 00:28:01 -05003504 const struct xfrm_migrate *mp;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003505
3506 if (type != XFRM_POLICY_TYPE_MAIN)
3507 return 0;
3508
3509 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3510 return -EINVAL;
3511
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003512 if (k != NULL) {
3513 /* addresses for KM */
3514 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3515 pfkey_sockaddr_pair_size(k->family));
3516 }
3517
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003518 /* selector */
3519 sasize_sel = pfkey_sockaddr_size(sel->family);
3520 if (!sasize_sel)
3521 return -EINVAL;
3522 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3523
3524 /* policy info */
3525 size_pol += sizeof(struct sadb_x_policy);
3526
3527 /* ipsecrequests */
3528 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3529 /* old locator pair */
3530 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3531 pfkey_sockaddr_pair_size(mp->old_family);
3532 /* new locator pair */
3533 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3534 pfkey_sockaddr_pair_size(mp->new_family);
3535 }
3536
3537 size += sizeof(struct sadb_msg) + size_pol;
3538
3539 /* alloc buffer */
3540 skb = alloc_skb(size, GFP_ATOMIC);
3541 if (skb == NULL)
3542 return -ENOMEM;
3543
3544 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3545 hdr->sadb_msg_version = PF_KEY_V2;
3546 hdr->sadb_msg_type = SADB_X_MIGRATE;
3547 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3548 hdr->sadb_msg_len = size / 8;
3549 hdr->sadb_msg_errno = 0;
3550 hdr->sadb_msg_reserved = 0;
3551 hdr->sadb_msg_seq = 0;
3552 hdr->sadb_msg_pid = 0;
3553
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003554 /* Addresses to be used by KM for negotiation, if ext is available */
3555 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
Julia Lawall89eb06f2012-04-08 22:41:10 +00003556 goto err;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003557
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003558 /* selector src */
3559 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3560
3561 /* selector dst */
3562 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3563
3564 /* policy information */
3565 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3566 pol->sadb_x_policy_len = size_pol / 8;
3567 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3568 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3569 pol->sadb_x_policy_dir = dir + 1;
Dan Carpenterff862a42013-07-28 23:04:45 +03003570 pol->sadb_x_policy_reserved = 0;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003571 pol->sadb_x_policy_id = 0;
3572 pol->sadb_x_policy_priority = 0;
3573
3574 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3575 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003576 int mode = pfkey_mode_from_xfrm(mp->mode);
3577 if (mode < 0)
Patrick McHardyd4782c32008-01-20 17:24:29 -08003578 goto err;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003579 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003580 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3581 mp->reqid, mp->old_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003582 &mp->old_saddr, &mp->old_daddr) < 0)
3583 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003584
3585 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003586 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003587 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3588 mp->reqid, mp->new_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003589 &mp->new_saddr, &mp->new_daddr) < 0)
3590 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003591 }
3592
3593 /* broadcast migrate message to sockets */
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003594 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003595
3596 return 0;
Patrick McHardyd4782c32008-01-20 17:24:29 -08003597
3598err:
3599 kfree_skb(skb);
3600 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003601}
3602#else
David S. Miller183cad12011-02-24 00:28:01 -05003603static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3604 const struct xfrm_migrate *m, int num_bundles,
3605 const struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003606{
3607 return -ENOPROTOOPT;
3608}
3609#endif
3610
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611static int pfkey_sendmsg(struct kiocb *kiocb,
3612 struct socket *sock, struct msghdr *msg, size_t len)
3613{
3614 struct sock *sk = sock->sk;
3615 struct sk_buff *skb = NULL;
3616 struct sadb_msg *hdr = NULL;
3617 int err;
Fan Du283bc9f2013-11-07 17:47:50 +08003618 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
3620 err = -EOPNOTSUPP;
3621 if (msg->msg_flags & MSG_OOB)
3622 goto out;
3623
3624 err = -EMSGSIZE;
Eric Dumazet95c96172012-04-15 05:58:06 +00003625 if ((unsigned int)len > sk->sk_sndbuf - 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 goto out;
3627
3628 err = -ENOBUFS;
3629 skb = alloc_skb(len, GFP_KERNEL);
3630 if (skb == NULL)
3631 goto out;
3632
3633 err = -EFAULT;
3634 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3635 goto out;
3636
3637 hdr = pfkey_get_base_msg(skb, &err);
3638 if (!hdr)
3639 goto out;
3640
Fan Du283bc9f2013-11-07 17:47:50 +08003641 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 err = pfkey_process(sk, skb, hdr);
Fan Du283bc9f2013-11-07 17:47:50 +08003643 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
3645out:
3646 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3647 err = 0;
Wei Yongjun6f961062009-02-25 00:31:04 +00003648 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
3650 return err ? : len;
3651}
3652
3653static int pfkey_recvmsg(struct kiocb *kiocb,
3654 struct socket *sock, struct msghdr *msg, size_t len,
3655 int flags)
3656{
3657 struct sock *sk = sock->sk;
Timo Teras83321d62008-03-03 23:40:12 -08003658 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 struct sk_buff *skb;
3660 int copied, err;
3661
3662 err = -EINVAL;
3663 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3664 goto out;
3665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3667 if (skb == NULL)
3668 goto out;
3669
3670 copied = skb->len;
3671 if (copied > len) {
3672 msg->msg_flags |= MSG_TRUNC;
3673 copied = len;
3674 }
3675
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003676 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3678 if (err)
3679 goto out_free;
3680
Neil Horman3b885782009-10-12 13:26:31 -07003681 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
3683 err = (flags & MSG_TRUNC) ? skb->len : copied;
3684
Timo Teras83321d62008-03-03 23:40:12 -08003685 if (pfk->dump.dump != NULL &&
3686 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3687 pfkey_do_dump(pfk);
3688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689out_free:
3690 skb_free_datagram(sk, skb);
3691out:
3692 return err;
3693}
3694
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003695static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 .family = PF_KEY,
3697 .owner = THIS_MODULE,
3698 /* Operations that make no sense on pfkey sockets. */
3699 .bind = sock_no_bind,
3700 .connect = sock_no_connect,
3701 .socketpair = sock_no_socketpair,
3702 .accept = sock_no_accept,
3703 .getname = sock_no_getname,
3704 .ioctl = sock_no_ioctl,
3705 .listen = sock_no_listen,
3706 .shutdown = sock_no_shutdown,
3707 .setsockopt = sock_no_setsockopt,
3708 .getsockopt = sock_no_getsockopt,
3709 .mmap = sock_no_mmap,
3710 .sendpage = sock_no_sendpage,
3711
3712 /* Now the operations that really occur. */
3713 .release = pfkey_release,
3714 .poll = datagram_poll,
3715 .sendmsg = pfkey_sendmsg,
3716 .recvmsg = pfkey_recvmsg,
3717};
3718
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003719static const struct net_proto_family pfkey_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 .family = PF_KEY,
3721 .create = pfkey_create,
3722 .owner = THIS_MODULE,
3723};
3724
3725#ifdef CONFIG_PROC_FS
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003726static int pfkey_seq_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727{
Li Zefan27b5b862010-02-08 23:20:29 +00003728 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003730 if (v == SEQ_START_TOKEN)
3731 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3732 else
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003733 seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 s,
3735 atomic_read(&s->sk_refcnt),
Eric Dumazet31e6d362009-06-17 19:05:41 -07003736 sk_rmem_alloc_get(s),
3737 sk_wmem_alloc_get(s),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06003738 from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 sock_i_ino(s)
3740 );
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003741 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003743
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003744static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
stephen hemmingerada440e2011-02-23 09:06:50 +00003745 __acquires(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003746{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003747 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003748 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003749
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003750 rcu_read_lock();
3751 return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003752}
3753
3754static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3755{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003756 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003757 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3758
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003759 return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003760}
3761
3762static void pfkey_seq_stop(struct seq_file *f, void *v)
stephen hemmingerada440e2011-02-23 09:06:50 +00003763 __releases(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003764{
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003765 rcu_read_unlock();
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003766}
3767
Stephen Hemminger98147d52009-09-01 19:25:02 +00003768static const struct seq_operations pfkey_seq_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003769 .start = pfkey_seq_start,
3770 .next = pfkey_seq_next,
3771 .stop = pfkey_seq_stop,
3772 .show = pfkey_seq_show,
3773};
3774
3775static int pfkey_seq_open(struct inode *inode, struct file *file)
3776{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003777 return seq_open_net(inode, file, &pfkey_seq_ops,
3778 sizeof(struct seq_net_private));
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003779}
3780
Stephen Hemminger5ca1b992009-09-01 19:25:05 +00003781static const struct file_operations pfkey_proc_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003782 .open = pfkey_seq_open,
3783 .read = seq_read,
3784 .llseek = seq_lseek,
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003785 .release = seq_release_net,
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003786};
3787
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003788static int __net_init pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003789{
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003790 struct proc_dir_entry *e;
3791
Gao fengd4beaa62013-02-18 01:34:54 +00003792 e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003793 if (e == NULL)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003794 return -ENOMEM;
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003795
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003796 return 0;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003797}
3798
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003799static void __net_exit pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003800{
Gao fengece31ff2013-02-18 01:34:56 +00003801 remove_proc_entry("pfkey", net->proc_net);
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003802}
3803#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003804static inline int pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003805{
3806 return 0;
3807}
3808
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003809static inline void pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003810{
3811}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812#endif
3813
3814static struct xfrm_mgr pfkeyv2_mgr =
3815{
3816 .id = "pfkeyv2",
3817 .notify = pfkey_send_notify,
3818 .acquire = pfkey_send_acquire,
3819 .compile_policy = pfkey_compile_policy,
3820 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003821 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003822 .migrate = pfkey_send_migrate,
Horia Geanta0f245582014-02-12 16:20:06 +02003823 .is_alive = pfkey_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824};
3825
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003826static int __net_init pfkey_net_init(struct net *net)
3827{
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003828 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003829 int rv;
3830
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003831 INIT_HLIST_HEAD(&net_pfkey->table);
3832 atomic_set(&net_pfkey->socks_nr, 0);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003833
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003834 rv = pfkey_init_proc(net);
3835
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003836 return rv;
3837}
3838
3839static void __net_exit pfkey_net_exit(struct net *net)
3840{
3841 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3842
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003843 pfkey_exit_proc(net);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003844 BUG_ON(!hlist_empty(&net_pfkey->table));
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003845}
3846
3847static struct pernet_operations pfkey_net_ops = {
3848 .init = pfkey_net_init,
3849 .exit = pfkey_net_exit,
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003850 .id = &pfkey_net_id,
3851 .size = sizeof(struct netns_pfkey),
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003852};
3853
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854static void __exit ipsec_pfkey_exit(void)
3855{
3856 xfrm_unregister_km(&pfkeyv2_mgr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003858 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 proto_unregister(&key_proto);
3860}
3861
3862static int __init ipsec_pfkey_init(void)
3863{
3864 int err = proto_register(&key_proto, 0);
3865
3866 if (err != 0)
3867 goto out;
3868
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003869 err = register_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 if (err != 0)
3871 goto out_unregister_key_proto;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003872 err = sock_register(&pfkey_family_ops);
3873 if (err != 0)
3874 goto out_unregister_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 err = xfrm_register_km(&pfkeyv2_mgr);
3876 if (err != 0)
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003877 goto out_sock_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878out:
3879 return err;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003880
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881out_sock_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003883out_unregister_pernet:
3884 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885out_unregister_key_proto:
3886 proto_unregister(&key_proto);
3887 goto out;
3888}
3889
3890module_init(ipsec_pfkey_init);
3891module_exit(ipsec_pfkey_exit);
3892MODULE_LICENSE("GPL");
3893MODULE_ALIAS_NETPROTO(PF_KEY);