blob: b67ed3a8486c259b33a79bf4758aa1a16e972705 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Authors: Maxim Giryaev <gem@asplinux.ru>
6 * David S. Miller <davem@redhat.com>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
10 * Derek Atkins <derek@ihtfp.com>
11 */
12
Randy Dunlap4fc268d2006-01-11 12:17:47 -080013#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/socket.h>
17#include <linux/pfkeyv2.h>
18#include <linux/ipsec.h>
19#include <linux/skbuff.h>
20#include <linux/rtnetlink.h>
21#include <linux/in.h>
22#include <linux/in6.h>
23#include <linux/proc_fs.h>
24#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020026#include <net/net_namespace.h>
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080027#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/xfrm.h>
29
30#include <net/sock.h>
31
32#define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
33#define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
34
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030035static unsigned int pfkey_net_id __read_mostly;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080036struct netns_pfkey {
37 /* List of all pfkey sockets. */
38 struct hlist_head table;
39 atomic_t socks_nr;
40};
stephen hemminger7f6b9db2010-02-22 07:57:19 +000041static DEFINE_MUTEX(pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jamal Hadi Salimbd557752010-02-22 16:20:22 -080043#define DUMMY_MARK 0
Mathias Krausee473fcb2013-06-26 23:56:58 +020044static const struct xfrm_mark dummy_mark = {0, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -070045struct pfkey_sock {
46 /* struct sock must be the first member of struct pfkey_sock */
47 struct sock sk;
48 int registered;
49 int promisc;
Timo Teras83321d62008-03-03 23:40:12 -080050
51 struct {
52 uint8_t msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +000053 uint32_t msg_portid;
Timo Teras83321d62008-03-03 23:40:12 -080054 int (*dump)(struct pfkey_sock *sk);
55 void (*done)(struct pfkey_sock *sk);
56 union {
57 struct xfrm_policy_walk policy;
58 struct xfrm_state_walk state;
59 } u;
Herbert Xu12a169e2008-10-01 07:03:24 -070060 struct sk_buff *skb;
Timo Teras83321d62008-03-03 23:40:12 -080061 } dump;
Yuejie Shi89e357d2017-03-31 15:10:20 +080062 struct mutex dump_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
Herbert Xu096f41d2017-04-13 18:35:59 +080065static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
66 xfrm_address_t *saddr, xfrm_address_t *daddr,
67 u16 *family);
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
70{
71 return (struct pfkey_sock *)sk;
72}
73
David S. Miller4c93fbb2011-02-25 18:07:06 -080074static int pfkey_can_dump(const struct sock *sk)
Timo Teras83321d62008-03-03 23:40:12 -080075{
76 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
77 return 1;
78 return 0;
79}
80
Timo Teras05238202008-10-01 05:17:54 -070081static void pfkey_terminate_dump(struct pfkey_sock *pfk)
Timo Teras83321d62008-03-03 23:40:12 -080082{
Timo Teras05238202008-10-01 05:17:54 -070083 if (pfk->dump.dump) {
Herbert Xu12a169e2008-10-01 07:03:24 -070084 if (pfk->dump.skb) {
85 kfree_skb(pfk->dump.skb);
86 pfk->dump.skb = NULL;
87 }
Timo Teras05238202008-10-01 05:17:54 -070088 pfk->dump.done(pfk);
89 pfk->dump.dump = NULL;
90 pfk->dump.done = NULL;
91 }
Timo Teras83321d62008-03-03 23:40:12 -080092}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static void pfkey_sock_destruct(struct sock *sk)
95{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080096 struct net *net = sock_net(sk);
97 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
98
Timo Teras05238202008-10-01 05:17:54 -070099 pfkey_terminate_dump(pfkey_sk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 skb_queue_purge(&sk->sk_receive_queue);
101
102 if (!sock_flag(sk, SOCK_DEAD)) {
stephen hemminger207024b2010-05-12 06:37:07 +0000103 pr_err("Attempt to release alive pfkey socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return;
105 }
106
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700107 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
Reshetova, Elena14afee42017-06-30 13:08:00 +0300108 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800110 atomic_dec(&net_pfkey->socks_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800113static const struct proto_ops pfkey_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115static void pfkey_insert(struct sock *sk)
116{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800117 struct net *net = sock_net(sk);
118 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
119
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000120 mutex_lock(&pfkey_mutex);
121 sk_add_node_rcu(sk, &net_pfkey->table);
122 mutex_unlock(&pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125static void pfkey_remove(struct sock *sk)
126{
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000127 mutex_lock(&pfkey_mutex);
128 sk_del_node_init_rcu(sk);
129 mutex_unlock(&pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132static struct proto key_proto = {
133 .name = "KEY",
134 .owner = THIS_MODULE,
135 .obj_size = sizeof(struct pfkey_sock),
136};
137
Eric Paris3f378b62009-11-05 22:18:14 -0800138static int pfkey_create(struct net *net, struct socket *sock, int protocol,
139 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800141 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct sock *sk;
Yuejie Shi89e357d2017-03-31 15:10:20 +0800143 struct pfkey_sock *pfk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int err;
145
Eric W. Biedermandf008c92012-11-16 03:03:07 +0000146 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return -EPERM;
148 if (sock->type != SOCK_RAW)
149 return -ESOCKTNOSUPPORT;
150 if (protocol != PF_KEY_V2)
151 return -EPROTONOSUPPORT;
152
153 err = -ENOMEM;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500154 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (sk == NULL)
156 goto out;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900157
Yuejie Shi89e357d2017-03-31 15:10:20 +0800158 pfk = pfkey_sk(sk);
159 mutex_init(&pfk->dump_lock);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 sock->ops = &pfkey_ops;
162 sock_init_data(sock, sk);
163
164 sk->sk_family = PF_KEY;
165 sk->sk_destruct = pfkey_sock_destruct;
166
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800167 atomic_inc(&net_pfkey->socks_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 pfkey_insert(sk);
170
171 return 0;
172out:
173 return err;
174}
175
176static int pfkey_release(struct socket *sock)
177{
178 struct sock *sk = sock->sk;
179
180 if (!sk)
181 return 0;
182
183 pfkey_remove(sk);
184
185 sock_orphan(sk);
186 sock->sk = NULL;
187 skb_queue_purge(&sk->sk_write_queue);
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000188
189 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 sock_put(sk);
191
192 return 0;
193}
194
Sean Tranchettifc2d5cf2019-02-07 13:33:21 -0700195static int pfkey_broadcast_one(struct sk_buff *skb, gfp_t allocation,
196 struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 int err = -ENOBUFS;
199
Sean Tranchettifc2d5cf2019-02-07 13:33:21 -0700200 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
201 return err;
202
203 skb = skb_clone(skb, allocation);
204
205 if (skb) {
206 skb_set_owner_r(skb, sk);
207 skb_queue_tail(&sk->sk_receive_queue, skb);
208 sk->sk_data_ready(sk);
209 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return err;
212}
213
214/* Send SKB to all pfkey sockets matching selected criteria. */
215#define BROADCAST_ALL 0
216#define BROADCAST_ONE 1
217#define BROADCAST_REGISTERED 2
218#define BROADCAST_PROMISC_ONLY 4
Eric Dumazet36f41f82017-08-14 10:16:45 -0700219static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800220 int broadcast_flags, struct sock *one_sk,
221 struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800223 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int err = -ESRCH;
226
227 /* XXX Do we need something like netlink_overrun? I think
228 * XXX PF_KEY socket apps will not mind current behavior.
229 */
230 if (!skb)
231 return -ENOMEM;
232
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000233 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800234 sk_for_each_rcu(sk, &net_pfkey->table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct pfkey_sock *pfk = pfkey_sk(sk);
236 int err2;
237
238 /* Yes, it means that if you are meant to receive this
239 * pfkey message you receive it twice as promiscuous
240 * socket.
241 */
242 if (pfk->promisc)
Sean Tranchettifc2d5cf2019-02-07 13:33:21 -0700243 pfkey_broadcast_one(skb, GFP_ATOMIC, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /* the exact target will be processed later */
246 if (sk == one_sk)
247 continue;
248 if (broadcast_flags != BROADCAST_ALL) {
249 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
250 continue;
251 if ((broadcast_flags & BROADCAST_REGISTERED) &&
252 !pfk->registered)
253 continue;
254 if (broadcast_flags & BROADCAST_ONE)
255 continue;
256 }
257
Sean Tranchettifc2d5cf2019-02-07 13:33:21 -0700258 err2 = pfkey_broadcast_one(skb, GFP_ATOMIC, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Li RongQingf6b8dec92015-10-22 11:35:05 +0800260 /* Error is cleared after successful sending to at least one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * registered KM */
262 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
263 err = err2;
264 }
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000265 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if (one_sk != NULL)
Sean Tranchettifc2d5cf2019-02-07 13:33:21 -0700268 err = pfkey_broadcast_one(skb, allocation, one_sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 kfree_skb(skb);
271 return err;
272}
273
Timo Teras05238202008-10-01 05:17:54 -0700274static int pfkey_do_dump(struct pfkey_sock *pfk)
275{
Herbert Xu12a169e2008-10-01 07:03:24 -0700276 struct sadb_msg *hdr;
Timo Teras05238202008-10-01 05:17:54 -0700277 int rc;
278
Yuejie Shi89e357d2017-03-31 15:10:20 +0800279 mutex_lock(&pfk->dump_lock);
280 if (!pfk->dump.dump) {
281 rc = 0;
282 goto out;
283 }
284
Timo Teras05238202008-10-01 05:17:54 -0700285 rc = pfk->dump.dump(pfk);
Yuejie Shi89e357d2017-03-31 15:10:20 +0800286 if (rc == -ENOBUFS) {
287 rc = 0;
288 goto out;
289 }
Timo Teras05238202008-10-01 05:17:54 -0700290
Herbert Xu12a169e2008-10-01 07:03:24 -0700291 if (pfk->dump.skb) {
Yuejie Shi89e357d2017-03-31 15:10:20 +0800292 if (!pfkey_can_dump(&pfk->sk)) {
293 rc = 0;
294 goto out;
295 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700296
297 hdr = (struct sadb_msg *) pfk->dump.skb->data;
298 hdr->sadb_msg_seq = 0;
299 hdr->sadb_msg_errno = rc;
Eric Dumazet36f41f82017-08-14 10:16:45 -0700300 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800301 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -0700302 pfk->dump.skb = NULL;
303 }
304
Timo Teras05238202008-10-01 05:17:54 -0700305 pfkey_terminate_dump(pfk);
Yuejie Shi89e357d2017-03-31 15:10:20 +0800306
307out:
308 mutex_unlock(&pfk->dump_lock);
Timo Teras05238202008-10-01 05:17:54 -0700309 return rc;
310}
311
David S. Miller4c93fbb2011-02-25 18:07:06 -0800312static inline void pfkey_hdr_dup(struct sadb_msg *new,
313 const struct sadb_msg *orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 *new = *orig;
316}
317
David S. Miller4c93fbb2011-02-25 18:07:06 -0800318static int pfkey_error(const struct sadb_msg *orig, int err, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
321 struct sadb_msg *hdr;
322
323 if (!skb)
324 return -ENOBUFS;
325
326 /* Woe be to the platform trying to support PFKEY yet
327 * having normal errnos outside the 1-255 range, inclusive.
328 */
329 err = -err;
330 if (err == ERESTARTSYS ||
331 err == ERESTARTNOHAND ||
332 err == ERESTARTNOINTR)
333 err = EINTR;
334 if (err >= 512)
335 err = EINVAL;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800336 BUG_ON(err <= 0 || err >= 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Johannes Berg4df864c2017-06-16 14:29:21 +0200338 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 pfkey_hdr_dup(hdr, orig);
340 hdr->sadb_msg_errno = (uint8_t) err;
341 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
342 sizeof(uint64_t));
343
Eric Dumazet36f41f82017-08-14 10:16:45 -0700344 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 return 0;
347}
348
Mathias Krause8603b9552013-06-26 23:56:59 +0200349static const u8 sadb_ext_min_len[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 [SADB_EXT_RESERVED] = (u8) 0,
351 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
352 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
353 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
354 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
355 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
356 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
357 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
358 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
359 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
360 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
361 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
362 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
363 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
364 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
365 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
366 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
367 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
368 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
369 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
370 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
371 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
372 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
373 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
Trent Jaegerdf718372005-12-13 23:12:27 -0800374 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -0700375 [SADB_X_EXT_KMADDRESS] = (u8) sizeof(struct sadb_x_kmaddress),
Nicolas Dichteld2c5f652014-02-20 14:52:41 +0100376 [SADB_X_EXT_FILTER] = (u8) sizeof(struct sadb_x_filter),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377};
378
379/* Verify sadb_address_{len,prefixlen} against sa_family. */
David S. Miller4c93fbb2011-02-25 18:07:06 -0800380static int verify_address_len(const void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800382 const struct sadb_address *sp = p;
383 const struct sockaddr *addr = (const struct sockaddr *)(sp + 1);
384 const struct sockaddr_in *sin;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000385#if IS_ENABLED(CONFIG_IPV6)
David S. Miller4c93fbb2011-02-25 18:07:06 -0800386 const struct sockaddr_in6 *sin6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387#endif
388 int len;
389
Eric Biggers06b335c2017-12-29 18:13:05 -0600390 if (sp->sadb_address_len <
391 DIV_ROUND_UP(sizeof(*sp) + offsetofend(typeof(*addr), sa_family),
392 sizeof(uint64_t)))
393 return -EINVAL;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 switch (addr->sa_family) {
396 case AF_INET:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700397 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (sp->sadb_address_len != len ||
399 sp->sadb_address_prefixlen > 32)
400 return -EINVAL;
401 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000402#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 case AF_INET6:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700404 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (sp->sadb_address_len != len ||
406 sp->sadb_address_prefixlen > 128)
407 return -EINVAL;
408 break;
409#endif
410 default:
411 /* It is user using kernel to keep track of security
412 * associations for another protocol, such as
413 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
414 * lengths.
415 *
416 * XXX Actually, association/policy database is not yet
417 * XXX able to cope with arbitrary sockaddr families.
418 * XXX When it can, remove this -EINVAL. -DaveM
419 */
420 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 return 0;
424}
425
Kevin Easton4b66af2d62018-04-07 11:40:33 -0400426static inline int sadb_key_len(const struct sadb_key *key)
427{
428 int key_bytes = DIV_ROUND_UP(key->sadb_key_bits, 8);
429
430 return DIV_ROUND_UP(sizeof(struct sadb_key) + key_bytes,
431 sizeof(uint64_t));
432}
433
434static int verify_key_len(const void *p)
435{
436 const struct sadb_key *key = p;
437
438 if (sadb_key_len(key) > key->sadb_key_len)
439 return -EINVAL;
440
441 return 0;
442}
443
David S. Miller4c93fbb2011-02-25 18:07:06 -0800444static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800445{
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700446 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
447 sec_ctx->sadb_x_ctx_len,
448 sizeof(uint64_t));
Trent Jaegerdf718372005-12-13 23:12:27 -0800449}
450
David S. Miller4c93fbb2011-02-25 18:07:06 -0800451static inline int verify_sec_ctx_len(const void *p)
Trent Jaegerdf718372005-12-13 23:12:27 -0800452{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800453 const struct sadb_x_sec_ctx *sec_ctx = p;
Stephen Rothwell298bb622007-10-30 23:57:05 -0700454 int len = sec_ctx->sadb_x_ctx_len;
Trent Jaegerdf718372005-12-13 23:12:27 -0800455
Stephen Rothwell298bb622007-10-30 23:57:05 -0700456 if (len > PAGE_SIZE)
Trent Jaegerdf718372005-12-13 23:12:27 -0800457 return -EINVAL;
458
459 len = pfkey_sec_ctx_len(sec_ctx);
460
461 if (sec_ctx->sadb_x_sec_len != len)
462 return -EINVAL;
463
464 return 0;
465}
466
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +0100467static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx,
468 gfp_t gfp)
Trent Jaegerdf718372005-12-13 23:12:27 -0800469{
470 struct xfrm_user_sec_ctx *uctx = NULL;
471 int ctx_size = sec_ctx->sadb_x_ctx_len;
472
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +0100473 uctx = kmalloc((sizeof(*uctx)+ctx_size), gfp);
Trent Jaegerdf718372005-12-13 23:12:27 -0800474
475 if (!uctx)
476 return NULL;
477
478 uctx->len = pfkey_sec_ctx_len(sec_ctx);
479 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
480 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
481 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
482 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
483 memcpy(uctx + 1, sec_ctx + 1,
484 uctx->ctx_len);
485
486 return uctx;
487}
488
David S. Miller4c93fbb2011-02-25 18:07:06 -0800489static int present_and_same_family(const struct sadb_address *src,
490 const struct sadb_address *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800492 const struct sockaddr *s_addr, *d_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (!src || !dst)
495 return 0;
496
David S. Miller4c93fbb2011-02-25 18:07:06 -0800497 s_addr = (const struct sockaddr *)(src + 1);
498 d_addr = (const struct sockaddr *)(dst + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (s_addr->sa_family != d_addr->sa_family)
500 return 0;
501 if (s_addr->sa_family != AF_INET
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000502#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 && s_addr->sa_family != AF_INET6
504#endif
505 )
506 return 0;
507
508 return 1;
509}
510
David S. Miller4c93fbb2011-02-25 18:07:06 -0800511static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800513 const char *p = (char *) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 int len = skb->len;
515
516 len -= sizeof(*hdr);
517 p += sizeof(*hdr);
518 while (len > 0) {
David S. Miller4c93fbb2011-02-25 18:07:06 -0800519 const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 uint16_t ext_type;
521 int ext_len;
522
Eric Biggers4e765b42017-12-29 18:15:23 -0600523 if (len < sizeof(*ehdr))
524 return -EINVAL;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 ext_len = ehdr->sadb_ext_len;
527 ext_len *= sizeof(uint64_t);
528 ext_type = ehdr->sadb_ext_type;
529 if (ext_len < sizeof(uint64_t) ||
530 ext_len > len ||
531 ext_type == SADB_EXT_RESERVED)
532 return -EINVAL;
533
534 if (ext_type <= SADB_EXT_MAX) {
535 int min = (int) sadb_ext_min_len[ext_type];
536 if (ext_len < min)
537 return -EINVAL;
538 if (ext_hdrs[ext_type-1] != NULL)
539 return -EINVAL;
Kevin Easton4b66af2d62018-04-07 11:40:33 -0400540 switch (ext_type) {
541 case SADB_EXT_ADDRESS_SRC:
542 case SADB_EXT_ADDRESS_DST:
543 case SADB_EXT_ADDRESS_PROXY:
544 case SADB_X_EXT_NAT_T_OA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (verify_address_len(p))
546 return -EINVAL;
Kevin Easton4b66af2d62018-04-07 11:40:33 -0400547 break;
548 case SADB_X_EXT_SEC_CTX:
Trent Jaegerdf718372005-12-13 23:12:27 -0800549 if (verify_sec_ctx_len(p))
550 return -EINVAL;
Kevin Easton4b66af2d62018-04-07 11:40:33 -0400551 break;
552 case SADB_EXT_KEY_AUTH:
553 case SADB_EXT_KEY_ENCRYPT:
554 if (verify_key_len(p))
555 return -EINVAL;
556 break;
557 default:
558 break;
Trent Jaegerdf718372005-12-13 23:12:27 -0800559 }
David S. Miller4c93fbb2011-02-25 18:07:06 -0800560 ext_hdrs[ext_type-1] = (void *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562 p += ext_len;
563 len -= ext_len;
564 }
565
566 return 0;
567}
568
569static uint16_t
570pfkey_satype2proto(uint8_t satype)
571{
572 switch (satype) {
573 case SADB_SATYPE_UNSPEC:
574 return IPSEC_PROTO_ANY;
575 case SADB_SATYPE_AH:
576 return IPPROTO_AH;
577 case SADB_SATYPE_ESP:
578 return IPPROTO_ESP;
579 case SADB_X_SATYPE_IPCOMP:
580 return IPPROTO_COMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 default:
582 return 0;
583 }
584 /* NOTREACHED */
585}
586
587static uint8_t
588pfkey_proto2satype(uint16_t proto)
589{
590 switch (proto) {
591 case IPPROTO_AH:
592 return SADB_SATYPE_AH;
593 case IPPROTO_ESP:
594 return SADB_SATYPE_ESP;
595 case IPPROTO_COMP:
596 return SADB_X_SATYPE_IPCOMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 default:
598 return 0;
599 }
600 /* NOTREACHED */
601}
602
603/* BTW, this scheme means that there is no way with PFKEY2 sockets to
604 * say specifically 'just raw sockets' as we encode them as 255.
605 */
606
607static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
608{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000609 return proto == IPSEC_PROTO_ANY ? 0 : proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
613{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000614 return proto ? proto : IPSEC_PROTO_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900617static inline int pfkey_sockaddr_len(sa_family_t family)
618{
619 switch (family) {
620 case AF_INET:
621 return sizeof(struct sockaddr_in);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000622#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900623 case AF_INET6:
624 return sizeof(struct sockaddr_in6);
625#endif
626 }
627 return 0;
628}
629
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900630static
631int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900633 switch (sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 case AF_INET:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900635 xaddr->a4 =
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900636 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return AF_INET;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000638#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 case AF_INET6:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900640 memcpy(xaddr->a6,
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900641 &((struct sockaddr_in6 *)sa)->sin6_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 sizeof(struct in6_addr));
643 return AF_INET6;
644#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900646 return 0;
647}
648
649static
David S. Miller4c93fbb2011-02-25 18:07:06 -0800650int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900651{
652 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
653 xaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
David S. Miller4c93fbb2011-02-25 18:07:06 -0800656static 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 -0700657{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800658 const struct sadb_sa *sa;
659 const struct sadb_address *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 uint16_t proto;
661 unsigned short family;
662 xfrm_address_t *xaddr;
663
Joe Perchesea110732011-06-13 16:21:26 +0000664 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (sa == NULL)
666 return NULL;
667
668 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
669 if (proto == 0)
670 return NULL;
671
672 /* sadb_address_len should be checked by caller */
Joe Perchesea110732011-06-13 16:21:26 +0000673 addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (addr == NULL)
675 return NULL;
676
David S. Miller4c93fbb2011-02-25 18:07:06 -0800677 family = ((const struct sockaddr *)(addr + 1))->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 switch (family) {
679 case AF_INET:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800680 xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000682#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 case AF_INET6:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800684 xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 break;
686#endif
687 default:
688 xaddr = NULL;
689 }
690
691 if (!xaddr)
692 return NULL;
693
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800694 return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699static int
700pfkey_sockaddr_size(sa_family_t family)
701{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900702 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700705static inline int pfkey_mode_from_xfrm(int mode)
706{
707 switch(mode) {
708 case XFRM_MODE_TRANSPORT:
709 return IPSEC_MODE_TRANSPORT;
710 case XFRM_MODE_TUNNEL:
711 return IPSEC_MODE_TUNNEL;
712 case XFRM_MODE_BEET:
713 return IPSEC_MODE_BEET;
714 default:
715 return -1;
716 }
717}
718
719static inline int pfkey_mode_to_xfrm(int mode)
720{
721 switch(mode) {
722 case IPSEC_MODE_ANY: /*XXX*/
723 case IPSEC_MODE_TRANSPORT:
724 return XFRM_MODE_TRANSPORT;
725 case IPSEC_MODE_TUNNEL:
726 return XFRM_MODE_TUNNEL;
727 case IPSEC_MODE_BEET:
728 return XFRM_MODE_BEET;
729 default:
730 return -1;
731 }
732}
733
David S. Miller183cad12011-02-24 00:28:01 -0500734static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
David S. Miller4c93fbb2011-02-25 18:07:06 -0800735 struct sockaddr *sa,
736 unsigned short family)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900737{
738 switch (family) {
739 case AF_INET:
740 {
741 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
742 sin->sin_family = AF_INET;
743 sin->sin_port = port;
744 sin->sin_addr.s_addr = xaddr->a4;
745 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
746 return 32;
747 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000748#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900749 case AF_INET6:
750 {
751 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
752 sin6->sin6_family = AF_INET6;
753 sin6->sin6_port = port;
754 sin6->sin6_flowinfo = 0;
Jiri Benc15e318b2015-03-29 16:59:24 +0200755 sin6->sin6_addr = xaddr->in6;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900756 sin6->sin6_scope_id = 0;
757 return 128;
758 }
759#endif
760 }
761 return 0;
762}
763
David S. Miller4c93fbb2011-02-25 18:07:06 -0800764static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -0700765 int add_keys, int hsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 struct sk_buff *skb;
768 struct sadb_msg *hdr;
769 struct sadb_sa *sa;
770 struct sadb_lifetime *lifetime;
771 struct sadb_address *addr;
772 struct sadb_key *key;
773 struct sadb_x_sa2 *sa2;
Trent Jaegerdf718372005-12-13 23:12:27 -0800774 struct sadb_x_sec_ctx *sec_ctx;
775 struct xfrm_sec_ctx *xfrm_ctx;
776 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int size;
778 int auth_key_size = 0;
779 int encrypt_key_size = 0;
780 int sockaddr_size;
781 struct xfrm_encap_tmpl *natt = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700782 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /* address family check */
785 sockaddr_size = pfkey_sockaddr_size(x->props.family);
786 if (!sockaddr_size)
787 return ERR_PTR(-EINVAL);
788
789 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
790 key(AE), (identity(SD),) (sensitivity)> */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900791 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 sizeof(struct sadb_lifetime) +
793 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
794 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900795 sizeof(struct sadb_address)*2 +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 sockaddr_size*2 +
797 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800798
799 if ((xfrm_ctx = x->security)) {
800 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
801 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
802 }
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* identity & sensitivity */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000805 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 size += sizeof(struct sadb_address) + sockaddr_size;
807
808 if (add_keys) {
809 if (x->aalg && x->aalg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900810 auth_key_size =
811 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 size += sizeof(struct sadb_key) + auth_key_size;
813 }
814 if (x->ealg && x->ealg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900815 encrypt_key_size =
816 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 size += sizeof(struct sadb_key) + encrypt_key_size;
818 }
819 }
820 if (x->encap)
821 natt = x->encap;
822
823 if (natt && natt->encap_type) {
824 size += sizeof(struct sadb_x_nat_t_type);
825 size += sizeof(struct sadb_x_nat_t_port);
826 size += sizeof(struct sadb_x_nat_t_port);
827 }
828
829 skb = alloc_skb(size + 16, GFP_ATOMIC);
830 if (skb == NULL)
831 return ERR_PTR(-ENOBUFS);
832
833 /* call should fill header later */
Johannes Berg4df864c2017-06-16 14:29:21 +0200834 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 memset(hdr, 0, size); /* XXX do we need this ? */
836 hdr->sadb_msg_len = size / sizeof(uint64_t);
837
838 /* sa */
Johannes Berg4df864c2017-06-16 14:29:21 +0200839 sa = skb_put(skb, sizeof(struct sadb_sa));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
841 sa->sadb_sa_exttype = SADB_EXT_SA;
842 sa->sadb_sa_spi = x->id.spi;
843 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700844 switch (x->km.state) {
845 case XFRM_STATE_VALID:
846 sa->sadb_sa_state = x->km.dying ?
847 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
848 break;
849 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700851 break;
852 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700854 break;
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 sa->sadb_sa_auth = 0;
857 if (x->aalg) {
858 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200859 sa->sadb_sa_auth = (a && a->pfkey_supported) ?
860 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862 sa->sadb_sa_encrypt = 0;
863 BUG_ON(x->ealg && x->calg);
864 if (x->ealg) {
865 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200866 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
867 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
870 if (x->calg) {
871 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200872 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
873 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
876 sa->sadb_sa_flags = 0;
877 if (x->props.flags & XFRM_STATE_NOECN)
878 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
879 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
880 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700881 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
882 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 /* hard time */
885 if (hsc & 2) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200886 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 lifetime->sadb_lifetime_len =
888 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
889 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
890 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
891 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
892 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
893 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
894 }
895 /* soft time */
896 if (hsc & 1) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200897 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 lifetime->sadb_lifetime_len =
899 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
900 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
901 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
902 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
903 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
904 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
905 }
906 /* current time */
Johannes Berg4df864c2017-06-16 14:29:21 +0200907 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 lifetime->sadb_lifetime_len =
909 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
910 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
911 lifetime->sadb_lifetime_allocations = x->curlft.packets;
912 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
913 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
914 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
915 /* src address */
Johannes Berg4df864c2017-06-16 14:29:21 +0200916 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900917 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 (sizeof(struct sadb_address)+sockaddr_size)/
919 sizeof(uint64_t);
920 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900921 /* "if the ports are non-zero, then the sadb_address_proto field,
922 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900924 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900927 addr->sadb_address_prefixlen =
928 pfkey_sockaddr_fill(&x->props.saddr, 0,
929 (struct sockaddr *) (addr + 1),
930 x->props.family);
Hariprasad Kelamde47c5d2019-06-08 14:30:50 +0530931 BUG_ON(!addr->sadb_address_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /* dst address */
Johannes Berg4df864c2017-06-16 14:29:21 +0200934 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900935 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 (sizeof(struct sadb_address)+sockaddr_size)/
937 sizeof(uint64_t);
938 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900939 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900942 addr->sadb_address_prefixlen =
943 pfkey_sockaddr_fill(&x->id.daddr, 0,
944 (struct sockaddr *) (addr + 1),
945 x->props.family);
Hariprasad Kelamde47c5d2019-06-08 14:30:50 +0530946 BUG_ON(!addr->sadb_address_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000948 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
949 x->props.family)) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200950 addr = skb_put(skb,
951 sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900952 addr->sadb_address_len =
953 (sizeof(struct sadb_address)+sockaddr_size)/
954 sizeof(uint64_t);
955 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
956 addr->sadb_address_proto =
957 pfkey_proto_from_xfrm(x->sel.proto);
958 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
959 addr->sadb_address_reserved = 0;
960
961 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
962 (struct sockaddr *) (addr + 1),
963 x->props.family);
964 }
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /* auth key */
967 if (add_keys && auth_key_size) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200968 key = skb_put(skb, sizeof(struct sadb_key) + auth_key_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
970 sizeof(uint64_t);
971 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
972 key->sadb_key_bits = x->aalg->alg_key_len;
973 key->sadb_key_reserved = 0;
974 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
975 }
976 /* encrypt key */
977 if (add_keys && encrypt_key_size) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200978 key = skb_put(skb, sizeof(struct sadb_key) + encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900979 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 encrypt_key_size) / sizeof(uint64_t);
981 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
982 key->sadb_key_bits = x->ealg->alg_key_len;
983 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900984 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 (x->ealg->alg_key_len+7)/8);
986 }
987
988 /* sa */
Johannes Berg4df864c2017-06-16 14:29:21 +0200989 sa2 = skb_put(skb, sizeof(struct sadb_x_sa2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
991 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700992 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
993 kfree_skb(skb);
994 return ERR_PTR(-EINVAL);
995 }
996 sa2->sadb_x_sa2_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 sa2->sadb_x_sa2_reserved1 = 0;
998 sa2->sadb_x_sa2_reserved2 = 0;
999 sa2->sadb_x_sa2_sequence = 0;
1000 sa2->sadb_x_sa2_reqid = x->props.reqid;
1001
1002 if (natt && natt->encap_type) {
1003 struct sadb_x_nat_t_type *n_type;
1004 struct sadb_x_nat_t_port *n_port;
1005
1006 /* type */
Johannes Berg4df864c2017-06-16 14:29:21 +02001007 n_type = skb_put(skb, sizeof(*n_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
1009 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
1010 n_type->sadb_x_nat_t_type_type = natt->encap_type;
1011 n_type->sadb_x_nat_t_type_reserved[0] = 0;
1012 n_type->sadb_x_nat_t_type_reserved[1] = 0;
1013 n_type->sadb_x_nat_t_type_reserved[2] = 0;
1014
1015 /* source port */
Johannes Berg4df864c2017-06-16 14:29:21 +02001016 n_port = skb_put(skb, sizeof(*n_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1018 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
1019 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
1020 n_port->sadb_x_nat_t_port_reserved = 0;
1021
1022 /* dest port */
Johannes Berg4df864c2017-06-16 14:29:21 +02001023 n_port = skb_put(skb, sizeof(*n_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1025 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
1026 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
1027 n_port->sadb_x_nat_t_port_reserved = 0;
1028 }
1029
Trent Jaegerdf718372005-12-13 23:12:27 -08001030 /* security context */
1031 if (xfrm_ctx) {
Johannes Berg4df864c2017-06-16 14:29:21 +02001032 sec_ctx = skb_put(skb,
1033 sizeof(struct sadb_x_sec_ctx) + ctx_size);
Trent Jaegerdf718372005-12-13 23:12:27 -08001034 sec_ctx->sadb_x_sec_len =
1035 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1036 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1037 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1038 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1039 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1040 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1041 xfrm_ctx->ctx_len);
1042 }
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return skb;
1045}
1046
Herbert Xu050f0092007-10-09 13:31:47 -07001047
David S. Miller4c93fbb2011-02-25 18:07:06 -08001048static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
Herbert Xu050f0092007-10-09 13:31:47 -07001049{
1050 struct sk_buff *skb;
1051
Herbert Xu050f0092007-10-09 13:31:47 -07001052 skb = __pfkey_xfrm_state2msg(x, 1, 3);
Herbert Xu050f0092007-10-09 13:31:47 -07001053
1054 return skb;
1055}
1056
David S. Miller4c93fbb2011-02-25 18:07:06 -08001057static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -07001058 int hsc)
1059{
1060 return __pfkey_xfrm_state2msg(x, 0, hsc);
1061}
1062
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001063static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
David S. Miller4c93fbb2011-02-25 18:07:06 -08001064 const struct sadb_msg *hdr,
1065 void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001067 struct xfrm_state *x;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001068 const struct sadb_lifetime *lifetime;
1069 const struct sadb_sa *sa;
1070 const struct sadb_key *key;
1071 const struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 uint16_t proto;
1073 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Joe Perchesea110732011-06-13 16:21:26 +00001076 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (!sa ||
1078 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1079 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1080 return ERR_PTR(-EINVAL);
1081 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1082 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1083 return ERR_PTR(-EINVAL);
1084 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1085 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1086 return ERR_PTR(-EINVAL);
1087 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1088 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1089 return ERR_PTR(-EINVAL);
1090
1091 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1092 if (proto == 0)
1093 return ERR_PTR(-EINVAL);
1094
1095 /* default error is no buffer space */
1096 err = -ENOBUFS;
1097
1098 /* RFC2367:
1099
1100 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1101 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1102 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1103 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1104 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1105 not true.
1106
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001107 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1109 */
1110 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1111 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1112 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1113 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1114 return ERR_PTR(-EINVAL);
Joe Perchesea110732011-06-13 16:21:26 +00001115 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (key != NULL &&
1117 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
Kevin Easton4b66af2d62018-04-07 11:40:33 -04001118 key->sadb_key_bits == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return ERR_PTR(-EINVAL);
1120 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1121 if (key != NULL &&
1122 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
Kevin Easton4b66af2d62018-04-07 11:40:33 -04001123 key->sadb_key_bits == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return ERR_PTR(-EINVAL);
1125
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001126 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (x == NULL)
1128 return ERR_PTR(-ENOBUFS);
1129
1130 x->id.proto = proto;
1131 x->id.spi = sa->sadb_sa_spi;
Fan Du33fce602013-09-17 15:14:13 +08001132 x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay,
1133 (sizeof(x->replay.bitmap) * 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1135 x->props.flags |= XFRM_STATE_NOECN;
1136 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1137 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001138 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1139 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Joe Perchesea110732011-06-13 16:21:26 +00001141 lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (lifetime != NULL) {
1143 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1144 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1145 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1146 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1147 }
Joe Perchesea110732011-06-13 16:21:26 +00001148 lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (lifetime != NULL) {
1150 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1151 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1152 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1153 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1154 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001155
Joe Perchesea110732011-06-13 16:21:26 +00001156 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001157 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01001158 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001159
1160 if (!uctx)
1161 goto out;
1162
1163 err = security_xfrm_state_alloc(x, uctx);
1164 kfree(uctx);
1165
1166 if (err)
1167 goto out;
1168 }
1169
Dan Carpentere747f642017-06-14 13:35:37 +03001170 err = -ENOBUFS;
Joe Perchesea110732011-06-13 16:21:26 +00001171 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (sa->sadb_sa_auth) {
1173 int keysize = 0;
1174 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001175 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 err = -ENOSYS;
1177 goto out;
1178 }
1179 if (key)
1180 keysize = (key->sadb_key_bits + 7) / 8;
1181 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001182 if (!x->aalg) {
1183 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 goto out;
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 strcpy(x->aalg->alg_name, a->name);
1187 x->aalg->alg_key_len = 0;
1188 if (key) {
1189 x->aalg->alg_key_len = key->sadb_key_bits;
1190 memcpy(x->aalg->alg_key, key+1, keysize);
1191 }
Martin Willic20a66f2009-12-09 06:11:15 +00001192 x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 x->props.aalgo = sa->sadb_sa_auth;
1194 /* x->algo.flags = sa->sadb_sa_flags; */
1195 }
1196 if (sa->sadb_sa_encrypt) {
1197 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1198 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001199 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 err = -ENOSYS;
1201 goto out;
1202 }
1203 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001204 if (!x->calg) {
1205 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 goto out;
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 strcpy(x->calg->alg_name, a->name);
1209 x->props.calgo = sa->sadb_sa_encrypt;
1210 } else {
1211 int keysize = 0;
1212 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001213 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 err = -ENOSYS;
1215 goto out;
1216 }
1217 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1218 if (key)
1219 keysize = (key->sadb_key_bits + 7) / 8;
1220 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001221 if (!x->ealg) {
1222 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 goto out;
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 strcpy(x->ealg->alg_name, a->name);
1226 x->ealg->alg_key_len = 0;
1227 if (key) {
1228 x->ealg->alg_key_len = key->sadb_key_bits;
1229 memcpy(x->ealg->alg_key, key+1, keysize);
1230 }
1231 x->props.ealgo = sa->sadb_sa_encrypt;
Herbert Xu69b01372015-05-27 16:03:45 +08001232 x->geniv = a->uinfo.encr.geniv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234 }
1235 /* x->algo.flags = sa->sadb_sa_flags; */
1236
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001237 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 &x->props.saddr);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001239 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 &x->id.daddr);
1241
1242 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001243 const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001244 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1245 if (mode < 0) {
1246 err = -EINVAL;
1247 goto out;
1248 }
1249 x->props.mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 x->props.reqid = sa2->sadb_x_sa2_reqid;
1251 }
1252
1253 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001254 const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 /* Nobody uses this, but we try. */
1257 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1258 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1259 }
1260
Kazunori MIYAZAWA4da51052008-05-21 13:26:11 -07001261 if (!x->sel.family)
Joy Latten4a4b6272007-08-02 19:25:43 -07001262 x->sel.family = x->props.family;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001265 const struct sadb_x_nat_t_type* n_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 struct xfrm_encap_tmpl *natt;
1267
1268 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001269 if (!x->encap) {
1270 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 goto out;
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 natt = x->encap;
1275 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1276 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1277
1278 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001279 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1281 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1282 }
1283 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001284 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1286 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1287 }
Timo Terasa8d694c2009-01-25 20:49:14 -08001288 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
Herbert Xu72cb6962005-06-20 13:18:08 -07001291 err = xfrm_init_state(x);
1292 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return x;
1297
1298out:
1299 x->km.state = XFRM_STATE_DEAD;
1300 xfrm_state_put(x);
1301 return ERR_PTR(err);
1302}
1303
David S. Miller4c93fbb2011-02-25 18:07:06 -08001304static 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 -07001305{
1306 return -EOPNOTSUPP;
1307}
1308
David S. Miller4c93fbb2011-02-25 18:07:06 -08001309static 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 -07001310{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001311 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 struct sk_buff *resp_skb;
1313 struct sadb_x_sa2 *sa2;
1314 struct sadb_address *saddr, *daddr;
1315 struct sadb_msg *out_hdr;
Herbert Xu658b2192007-10-09 13:29:52 -07001316 struct sadb_spirange *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 struct xfrm_state *x = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001318 int mode;
Herbert Xu658b2192007-10-09 13:29:52 -07001319 int err;
1320 u32 min_spi, max_spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 u32 reqid;
1322 u8 proto;
1323 unsigned short family;
1324 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1325
1326 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1327 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1328 return -EINVAL;
1329
1330 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1331 if (proto == 0)
1332 return -EINVAL;
1333
1334 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001335 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1336 if (mode < 0)
1337 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 reqid = sa2->sadb_x_sa2_reqid;
1339 } else {
1340 mode = 0;
1341 reqid = 0;
1342 }
1343
1344 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1345 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1346
1347 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1348 switch (family) {
1349 case AF_INET:
1350 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1351 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1352 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001353#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 case AF_INET6:
1355 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1356 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1357 break;
1358#endif
1359 }
1360
1361 if (hdr->sadb_msg_seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001362 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001363 if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 xfrm_state_put(x);
1365 x = NULL;
1366 }
1367 }
1368
1369 if (!x)
Steffen Klassert7e652642018-06-12 14:07:07 +02001370 x = xfrm_find_acq(net, &dummy_mark, mode, reqid, 0, proto, xdaddr, xsaddr, 1, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 if (x == NULL)
1373 return -ENOENT;
1374
Herbert Xu658b2192007-10-09 13:29:52 -07001375 min_spi = 0x100;
1376 max_spi = 0x0fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Herbert Xu658b2192007-10-09 13:29:52 -07001378 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1379 if (range) {
1380 min_spi = range->sadb_spirange_min;
1381 max_spi = range->sadb_spirange_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Herbert Xu658b2192007-10-09 13:29:52 -07001383
Fan Du776e9dd2013-12-16 18:47:49 +08001384 err = verify_spi_info(x->id.proto, min_spi, max_spi);
1385 if (err) {
1386 xfrm_state_put(x);
1387 return err;
1388 }
1389
Herbert Xu658b2192007-10-09 13:29:52 -07001390 err = xfrm_alloc_spi(x, min_spi, max_spi);
Herbert Xu050f0092007-10-09 13:31:47 -07001391 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 if (IS_ERR(resp_skb)) {
1394 xfrm_state_put(x);
1395 return PTR_ERR(resp_skb);
1396 }
1397
1398 out_hdr = (struct sadb_msg *) resp_skb->data;
1399 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1400 out_hdr->sadb_msg_type = SADB_GETSPI;
1401 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1402 out_hdr->sadb_msg_errno = 0;
1403 out_hdr->sadb_msg_reserved = 0;
1404 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1405 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1406
1407 xfrm_state_put(x);
1408
Eric Dumazet36f41f82017-08-14 10:16:45 -07001409 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 return 0;
1412}
1413
David S. Miller4c93fbb2011-02-25 18:07:06 -08001414static 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 -07001415{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001416 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 struct xfrm_state *x;
1418
1419 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1420 return -EOPNOTSUPP;
1421
1422 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1423 return 0;
1424
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001425 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (x == NULL)
1427 return 0;
1428
1429 spin_lock_bh(&x->lock);
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001430 if (x->km.state == XFRM_STATE_ACQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 x->km.state = XFRM_STATE_ERROR;
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 spin_unlock_bh(&x->lock);
1434 xfrm_state_put(x);
1435 return 0;
1436}
1437
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001438static inline int event2poltype(int event)
1439{
1440 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001441 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001442 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001443 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001444 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001445 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001446 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001447 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001448 // return SADB_X_SPDEXPIRE;
1449 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001450 pr_err("pfkey: Unknown policy event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001451 break;
1452 }
1453
1454 return 0;
1455}
1456
1457static inline int event2keytype(int event)
1458{
1459 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001460 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001461 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001462 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001463 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001464 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001465 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001466 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001467 return SADB_EXPIRE;
1468 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001469 pr_err("pfkey: Unknown SA event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001470 break;
1471 }
1472
1473 return 0;
1474}
1475
1476/* ADD/UPD/DEL */
David S. Miller214e0052011-02-24 00:02:38 -05001477static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001478{
1479 struct sk_buff *skb;
1480 struct sadb_msg *hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001481
Herbert Xu050f0092007-10-09 13:31:47 -07001482 skb = pfkey_xfrm_state2msg(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001483
1484 if (IS_ERR(skb))
1485 return PTR_ERR(skb);
1486
1487 hdr = (struct sadb_msg *) skb->data;
1488 hdr->sadb_msg_version = PF_KEY_V2;
1489 hdr->sadb_msg_type = event2keytype(c->event);
1490 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1491 hdr->sadb_msg_errno = 0;
1492 hdr->sadb_msg_reserved = 0;
1493 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001494 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001495
Eric Dumazet36f41f82017-08-14 10:16:45 -07001496 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001497
1498 return 0;
1499}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
David S. Miller4c93fbb2011-02-25 18:07:06 -08001501static 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 -07001502{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001503 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 struct xfrm_state *x;
1505 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001506 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001508 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (IS_ERR(x))
1510 return PTR_ERR(x);
1511
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001512 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (hdr->sadb_msg_type == SADB_ADD)
1514 err = xfrm_state_add(x);
1515 else
1516 err = xfrm_state_update(x);
1517
Tetsuo Handa2e710292014-04-22 21:48:30 +09001518 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (err < 0) {
1521 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001522 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001523 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001526 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001527 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001528 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001529 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001530 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001531 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001532 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001533out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001534 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001535 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
David S. Miller4c93fbb2011-02-25 18:07:06 -08001538static 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 -07001539{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001540 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001542 struct km_event c;
1543 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 if (!ext_hdrs[SADB_EXT_SA-1] ||
1546 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1547 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1548 return -EINVAL;
1549
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001550 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (x == NULL)
1552 return -ESRCH;
1553
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001554 if ((err = security_xfrm_state_delete(x)))
1555 goto out;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001558 err = -EPERM;
1559 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
Joy Latten161a09e2006-11-27 13:11:54 -06001561
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001562 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001563
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001564 if (err < 0)
1565 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001566
1567 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001568 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001569 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001570 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001571out:
Tetsuo Handa2e710292014-04-22 21:48:30 +09001572 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 xfrm_state_put(x);
1574
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001575 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
David S. Miller4c93fbb2011-02-25 18:07:06 -08001578static 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 -07001579{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001580 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 __u8 proto;
1582 struct sk_buff *out_skb;
1583 struct sadb_msg *out_hdr;
1584 struct xfrm_state *x;
1585
1586 if (!ext_hdrs[SADB_EXT_SA-1] ||
1587 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1588 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1589 return -EINVAL;
1590
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001591 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (x == NULL)
1593 return -ESRCH;
1594
Herbert Xu050f0092007-10-09 13:31:47 -07001595 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 proto = x->id.proto;
1597 xfrm_state_put(x);
1598 if (IS_ERR(out_skb))
1599 return PTR_ERR(out_skb);
1600
1601 out_hdr = (struct sadb_msg *) out_skb->data;
1602 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Charles Hardin435000b2007-11-22 19:35:15 +08001603 out_hdr->sadb_msg_type = SADB_GET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1605 out_hdr->sadb_msg_errno = 0;
1606 out_hdr->sadb_msg_reserved = 0;
1607 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1608 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Eric Dumazet36f41f82017-08-14 10:16:45 -07001609 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 return 0;
1612}
1613
David S. Miller4c93fbb2011-02-25 18:07:06 -08001614static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001615 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 struct sk_buff *skb;
1618 struct sadb_msg *hdr;
1619 int len, auth_len, enc_len, i;
1620
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001621 auth_len = xfrm_count_pfkey_auth_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (auth_len) {
1623 auth_len *= sizeof(struct sadb_alg);
1624 auth_len += sizeof(struct sadb_supported);
1625 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001626
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001627 enc_len = xfrm_count_pfkey_enc_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (enc_len) {
1629 enc_len *= sizeof(struct sadb_alg);
1630 enc_len += sizeof(struct sadb_supported);
1631 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 len = enc_len + auth_len + sizeof(struct sadb_msg);
1634
1635 skb = alloc_skb(len + 16, allocation);
1636 if (!skb)
1637 goto out_put_algs;
1638
Johannes Berg4df864c2017-06-16 14:29:21 +02001639 hdr = skb_put(skb, sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 pfkey_hdr_dup(hdr, orig);
1641 hdr->sadb_msg_errno = 0;
1642 hdr->sadb_msg_len = len / sizeof(uint64_t);
1643
1644 if (auth_len) {
1645 struct sadb_supported *sp;
1646 struct sadb_alg *ap;
1647
Johannes Berg4df864c2017-06-16 14:29:21 +02001648 sp = skb_put(skb, auth_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 ap = (struct sadb_alg *) (sp + 1);
1650
1651 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1652 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1653
1654 for (i = 0; ; i++) {
1655 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1656 if (!aalg)
1657 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001658 if (!aalg->pfkey_supported)
1659 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (aalg->available)
1661 *ap++ = aalg->desc;
1662 }
1663 }
1664
1665 if (enc_len) {
1666 struct sadb_supported *sp;
1667 struct sadb_alg *ap;
1668
Johannes Berg4df864c2017-06-16 14:29:21 +02001669 sp = skb_put(skb, enc_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 ap = (struct sadb_alg *) (sp + 1);
1671
1672 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1673 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1674
1675 for (i = 0; ; i++) {
1676 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1677 if (!ealg)
1678 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001679 if (!ealg->pfkey_supported)
1680 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (ealg->available)
1682 *ap++ = ealg->desc;
1683 }
1684 }
1685
1686out_put_algs:
1687 return skb;
1688}
1689
David S. Miller4c93fbb2011-02-25 18:07:06 -08001690static 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 -07001691{
1692 struct pfkey_sock *pfk = pfkey_sk(sk);
1693 struct sk_buff *supp_skb;
1694
1695 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1696 return -EINVAL;
1697
1698 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1699 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1700 return -EEXIST;
1701 pfk->registered |= (1<<hdr->sadb_msg_satype);
1702 }
1703
1704 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1707 if (!supp_skb) {
1708 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1709 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1710
1711 return -ENOBUFS;
1712 }
1713
Eric Dumazet36f41f82017-08-14 10:16:45 -07001714 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk,
1715 sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return 0;
1717}
1718
David S. Miller4c93fbb2011-02-25 18:07:06 -08001719static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001720{
1721 struct sk_buff *skb;
1722 struct sadb_msg *hdr;
1723
1724 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1725 if (!skb)
1726 return -ENOBUFS;
1727
Johannes Berg59ae1d12017-06-16 14:29:20 +02001728 hdr = skb_put_data(skb, ihdr, sizeof(struct sadb_msg));
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001729 hdr->sadb_msg_errno = (uint8_t) 0;
1730 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1731
Eric Dumazet36f41f82017-08-14 10:16:45 -07001732 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ONE, sk,
1733 sock_net(sk));
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001734}
1735
David S. Miller214e0052011-02-24 00:02:38 -05001736static int key_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001737{
1738 struct sk_buff *skb;
1739 struct sadb_msg *hdr;
1740
1741 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1742 if (!skb)
1743 return -ENOBUFS;
Johannes Berg4df864c2017-06-16 14:29:21 +02001744 hdr = skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf08867f92005-06-18 22:44:00 -07001745 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001746 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001747 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001748 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001749 hdr->sadb_msg_version = PF_KEY_V2;
1750 hdr->sadb_msg_errno = (uint8_t) 0;
1751 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02001752 hdr->sadb_msg_reserved = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001753
Eric Dumazet36f41f82017-08-14 10:16:45 -07001754 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001755
1756 return 0;
1757}
1758
David S. Miller4c93fbb2011-02-25 18:07:06 -08001759static 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 -07001760{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001761 struct net *net = sock_net(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +00001762 unsigned int proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001763 struct km_event c;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001764 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1767 if (proto == 0)
1768 return -EINVAL;
1769
Cong Wangf75a2802019-01-31 13:05:49 -08001770 err = xfrm_state_flush(net, proto, true, false);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001771 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001772 if (err || err2) {
1773 if (err == -ESRCH) /* empty table - go quietly */
1774 err = 0;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001775 return err ? err : err2;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001776 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001777
Herbert Xubf08867f92005-06-18 22:44:00 -07001778 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001779 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001780 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001781 c.event = XFRM_MSG_FLUSHSA;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001782 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001783 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 return 0;
1786}
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1789{
Timo Teras83321d62008-03-03 23:40:12 -08001790 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 struct sk_buff *out_skb;
1792 struct sadb_msg *out_hdr;
1793
Timo Teras83321d62008-03-03 23:40:12 -08001794 if (!pfkey_can_dump(&pfk->sk))
1795 return -ENOBUFS;
1796
Herbert Xu050f0092007-10-09 13:31:47 -07001797 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (IS_ERR(out_skb))
1799 return PTR_ERR(out_skb);
1800
1801 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08001802 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 out_hdr->sadb_msg_type = SADB_DUMP;
1804 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1805 out_hdr->sadb_msg_errno = 0;
1806 out_hdr->sadb_msg_reserved = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07001807 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001808 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07001809
1810 if (pfk->dump.skb)
Eric Dumazet36f41f82017-08-14 10:16:45 -07001811 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001812 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07001813 pfk->dump.skb = out_skb;
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return 0;
1816}
1817
Timo Teras83321d62008-03-03 23:40:12 -08001818static int pfkey_dump_sa(struct pfkey_sock *pfk)
1819{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001820 struct net *net = sock_net(&pfk->sk);
1821 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08001822}
1823
1824static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1825{
Fan Du283bc9f2013-11-07 17:47:50 +08001826 struct net *net = sock_net(&pfk->sk);
1827
1828 xfrm_state_walk_done(&pfk->dump.u.state, net);
Timo Teras83321d62008-03-03 23:40:12 -08001829}
1830
David S. Miller4c93fbb2011-02-25 18:07:06 -08001831static 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 -07001832{
1833 u8 proto;
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001834 struct xfrm_address_filter *filter = NULL;
Timo Teras83321d62008-03-03 23:40:12 -08001835 struct pfkey_sock *pfk = pfkey_sk(sk);
1836
Yuejie Shi89e357d2017-03-31 15:10:20 +08001837 mutex_lock(&pfk->dump_lock);
1838 if (pfk->dump.dump != NULL) {
1839 mutex_unlock(&pfk->dump_lock);
Timo Teras83321d62008-03-03 23:40:12 -08001840 return -EBUSY;
Yuejie Shi89e357d2017-03-31 15:10:20 +08001841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
Yuejie Shi89e357d2017-03-31 15:10:20 +08001844 if (proto == 0) {
1845 mutex_unlock(&pfk->dump_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return -EINVAL;
Yuejie Shi89e357d2017-03-31 15:10:20 +08001847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Nicolas Dichteld3623092014-02-14 15:30:36 +01001849 if (ext_hdrs[SADB_X_EXT_FILTER - 1]) {
1850 struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1];
1851
1852 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
Yuejie Shi89e357d2017-03-31 15:10:20 +08001853 if (filter == NULL) {
1854 mutex_unlock(&pfk->dump_lock);
Nicolas Dichteld3623092014-02-14 15:30:36 +01001855 return -ENOMEM;
Yuejie Shi89e357d2017-03-31 15:10:20 +08001856 }
Nicolas Dichteld3623092014-02-14 15:30:36 +01001857
1858 memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr,
1859 sizeof(xfrm_address_t));
1860 memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr,
1861 sizeof(xfrm_address_t));
1862 filter->family = xfilter->sadb_x_filter_family;
1863 filter->splen = xfilter->sadb_x_filter_splen;
1864 filter->dplen = xfilter->sadb_x_filter_dplen;
1865 }
1866
Timo Teras83321d62008-03-03 23:40:12 -08001867 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001868 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08001869 pfk->dump.dump = pfkey_dump_sa;
1870 pfk->dump.done = pfkey_dump_sa_done;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001871 xfrm_state_walk_init(&pfk->dump.u.state, proto, filter);
Yuejie Shi89e357d2017-03-31 15:10:20 +08001872 mutex_unlock(&pfk->dump_lock);
Timo Teras4c563f72008-02-28 21:31:08 -08001873
Timo Teras83321d62008-03-03 23:40:12 -08001874 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
David S. Miller4c93fbb2011-02-25 18:07:06 -08001877static 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 -07001878{
1879 struct pfkey_sock *pfk = pfkey_sk(sk);
1880 int satype = hdr->sadb_msg_satype;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001881 bool reset_errno = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001884 reset_errno = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (satype != 0 && satype != 1)
1886 return -EINVAL;
1887 pfk->promisc = satype;
1888 }
David S. Miller4c93fbb2011-02-25 18:07:06 -08001889 if (reset_errno && skb_cloned(skb))
1890 skb = skb_copy(skb, GFP_KERNEL);
1891 else
1892 skb = skb_clone(skb, GFP_KERNEL);
1893
1894 if (reset_errno && skb) {
1895 struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
1896 new_hdr->sadb_msg_errno = 0;
1897 }
1898
Eric Dumazet36f41f82017-08-14 10:16:45 -07001899 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return 0;
1901}
1902
1903static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1904{
1905 int i;
1906 u32 reqid = *(u32*)ptr;
1907
1908 for (i=0; i<xp->xfrm_nr; i++) {
1909 if (xp->xfrm_vec[i].reqid == reqid)
1910 return -EEXIST;
1911 }
1912 return 0;
1913}
1914
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001915static u32 gen_reqid(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Timo Teras4c563f72008-02-28 21:31:08 -08001917 struct xfrm_policy_walk walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 u32 start;
Timo Teras4c563f72008-02-28 21:31:08 -08001919 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1921
1922 start = reqid;
1923 do {
1924 ++reqid;
1925 if (reqid == 0)
1926 reqid = IPSEC_MANUAL_REQID_MAX+1;
Timo Teras4c563f72008-02-28 21:31:08 -08001927 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001928 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
Fan Du283bc9f2013-11-07 17:47:50 +08001929 xfrm_policy_walk_done(&walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001930 if (rc != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return reqid;
1932 } while (reqid != start);
1933 return 0;
1934}
1935
1936static int
1937parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1938{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001939 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001941 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1944 return -ELOOP;
1945
1946 if (rq->sadb_x_ipsecrequest_mode == 0)
1947 return -EINVAL;
Cong Wangdbb24832019-03-22 16:26:19 -07001948 if (!xfrm_id_proto_valid(rq->sadb_x_ipsecrequest_proto))
1949 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Cong Wangdbb24832019-03-22 16:26:19 -07001951 t->id.proto = rq->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001952 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1953 return -EINVAL;
1954 t->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1956 t->optional = 1;
1957 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1958 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1959 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1960 t->reqid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001961 if (!t->reqid && !(t->reqid = gen_reqid(net)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return -ENOBUFS;
1963 }
1964
1965 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001966 if (t->mode == XFRM_MODE_TUNNEL) {
Herbert Xu096f41d2017-04-13 18:35:59 +08001967 int err;
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001968
Herbert Xu096f41d2017-04-13 18:35:59 +08001969 err = parse_sockaddr_pair(
1970 (struct sockaddr *)(rq + 1),
1971 rq->sadb_x_ipsecrequest_len - sizeof(*rq),
1972 &t->saddr, &t->id.daddr, &t->encap_family);
1973 if (err)
1974 return err;
Miika Komu2718aa72006-11-30 16:41:50 -08001975 } else
1976 t->encap_family = xp->family;
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 /* No way to set this via kame pfkey */
Herbert Xuc5d18e92008-04-22 00:46:42 -07001979 t->allalgs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 xp->xfrm_nr++;
1981 return 0;
1982}
1983
1984static int
1985parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1986{
1987 int err;
1988 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1989 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1990
Dan Carpenterf674e722012-09-27 22:21:19 +00001991 if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
1992 return -EINVAL;
1993
Herbert Xu096f41d2017-04-13 18:35:59 +08001994 while (len >= sizeof(*rq)) {
1995 if (len < rq->sadb_x_ipsecrequest_len ||
1996 rq->sadb_x_ipsecrequest_len < sizeof(*rq))
1997 return -EINVAL;
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if ((err = parse_ipsecrequest(xp, rq)) < 0)
2000 return err;
2001 len -= rq->sadb_x_ipsecrequest_len;
2002 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
2003 }
2004 return 0;
2005}
2006
David S. Miller4c93fbb2011-02-25 18:07:06 -08002007static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
Trent Jaegerdf718372005-12-13 23:12:27 -08002008{
Colin Ian King4fc735d2018-11-13 14:23:28 +00002009 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
Trent Jaegerdf718372005-12-13 23:12:27 -08002010
2011 if (xfrm_ctx) {
2012 int len = sizeof(struct sadb_x_sec_ctx);
2013 len += xfrm_ctx->ctx_len;
2014 return PFKEY_ALIGN8(len);
2015 }
2016 return 0;
2017}
2018
David S. Miller4c93fbb2011-02-25 18:07:06 -08002019static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
David S. Miller4c93fbb2011-02-25 18:07:06 -08002021 const struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08002023 int socklen = 0;
2024 int i;
2025
2026 for (i=0; i<xp->xfrm_nr; i++) {
2027 t = xp->xfrm_vec + i;
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002028 socklen += pfkey_sockaddr_len(t->encap_family);
Miika Komu2718aa72006-11-30 16:41:50 -08002029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 return sizeof(struct sadb_msg) +
2032 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002033 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 (sockaddr_size * 2) +
2035 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08002036 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
2037 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08002038 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
David S. Miller4c93fbb2011-02-25 18:07:06 -08002041static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 struct sk_buff *skb;
2044 int size;
2045
2046 size = pfkey_xfrm_policy2msg_size(xp);
2047
2048 skb = alloc_skb(size + 16, GFP_ATOMIC);
2049 if (skb == NULL)
2050 return ERR_PTR(-ENOBUFS);
2051
2052 return skb;
2053}
2054
David S. Miller4c93fbb2011-02-25 18:07:06 -08002055static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 struct sadb_msg *hdr;
2058 struct sadb_address *addr;
2059 struct sadb_lifetime *lifetime;
2060 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08002061 struct sadb_x_sec_ctx *sec_ctx;
2062 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 int i;
2064 int size;
2065 int sockaddr_size = pfkey_sockaddr_size(xp->family);
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002066 int socklen = pfkey_sockaddr_len(xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 size = pfkey_xfrm_policy2msg_size(xp);
2069
2070 /* call should fill header later */
Johannes Berg4df864c2017-06-16 14:29:21 +02002071 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 memset(hdr, 0, size); /* XXX do we need this ? */
2073
2074 /* src address */
Johannes Berg4df864c2017-06-16 14:29:21 +02002075 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002076 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 (sizeof(struct sadb_address)+sockaddr_size)/
2078 sizeof(uint64_t);
2079 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2080 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2081 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2082 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002083 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2084 xp->selector.sport,
2085 (struct sockaddr *) (addr + 1),
2086 xp->family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 BUG();
2088
2089 /* dst address */
Johannes Berg4df864c2017-06-16 14:29:21 +02002090 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 addr->sadb_address_len =
2092 (sizeof(struct sadb_address)+sockaddr_size)/
2093 sizeof(uint64_t);
2094 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2095 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002096 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002098
2099 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2100 (struct sockaddr *) (addr + 1),
2101 xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 /* hard time */
Johannes Berg4df864c2017-06-16 14:29:21 +02002104 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 lifetime->sadb_lifetime_len =
2106 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2107 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2108 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2109 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2110 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2111 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2112 /* soft time */
Johannes Berg4df864c2017-06-16 14:29:21 +02002113 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 lifetime->sadb_lifetime_len =
2115 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2116 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2117 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2118 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2119 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2120 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2121 /* current time */
Johannes Berg4df864c2017-06-16 14:29:21 +02002122 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 lifetime->sadb_lifetime_len =
2124 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2125 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2126 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2127 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2128 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2129 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2130
Johannes Berg4df864c2017-06-16 14:29:21 +02002131 pol = skb_put(skb, sizeof(struct sadb_x_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2133 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2134 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2135 if (xp->action == XFRM_POLICY_ALLOW) {
2136 if (xp->xfrm_nr)
2137 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2138 else
2139 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2140 }
2141 pol->sadb_x_policy_dir = dir+1;
Dan Carpenterff862a42013-07-28 23:04:45 +03002142 pol->sadb_x_policy_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 pol->sadb_x_policy_id = xp->index;
2144 pol->sadb_x_policy_priority = xp->priority;
2145
2146 for (i=0; i<xp->xfrm_nr; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002147 const struct xfrm_tmpl *t = xp->xfrm_vec + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 struct sadb_x_ipsecrequest *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 int req_size;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002150 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 req_size = sizeof(struct sadb_x_ipsecrequest);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002153 if (t->mode == XFRM_MODE_TUNNEL) {
2154 socklen = pfkey_sockaddr_len(t->encap_family);
2155 req_size += socklen * 2;
2156 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 size -= 2*socklen;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002158 }
Johannes Berg4df864c2017-06-16 14:29:21 +02002159 rq = skb_put(skb, req_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 pol->sadb_x_policy_len += req_size/8;
2161 memset(rq, 0, sizeof(*rq));
2162 rq->sadb_x_ipsecrequest_len = req_size;
2163 rq->sadb_x_ipsecrequest_proto = t->id.proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002164 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2165 return -EINVAL;
David S. Millerfefaa752007-04-17 21:48:10 -07002166 rq->sadb_x_ipsecrequest_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2168 if (t->reqid)
2169 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2170 if (t->optional)
2171 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2172 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002174 if (t->mode == XFRM_MODE_TUNNEL) {
2175 u8 *sa = (void *)(rq + 1);
2176 pfkey_sockaddr_fill(&t->saddr, 0,
2177 (struct sockaddr *)sa,
2178 t->encap_family);
2179 pfkey_sockaddr_fill(&t->id.daddr, 0,
2180 (struct sockaddr *) (sa + socklen),
2181 t->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002184
2185 /* security context */
2186 if ((xfrm_ctx = xp->security)) {
2187 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2188
Johannes Berg4df864c2017-06-16 14:29:21 +02002189 sec_ctx = skb_put(skb, ctx_size);
Trent Jaegerdf718372005-12-13 23:12:27 -08002190 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2191 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2192 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2193 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2194 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2195 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2196 xfrm_ctx->ctx_len);
2197 }
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 hdr->sadb_msg_len = size / sizeof(uint64_t);
Reshetova, Elena850a6212017-07-04 15:53:22 +03002200 hdr->sadb_msg_reserved = refcount_read(&xp->refcnt);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002201
2202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
David S. Miller214e0052011-02-24 00:02:38 -05002205static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002206{
2207 struct sk_buff *out_skb;
2208 struct sadb_msg *out_hdr;
2209 int err;
2210
2211 out_skb = pfkey_xfrm_policy2msg_prep(xp);
Dan Carpenter9a127aa2010-03-24 01:47:00 +00002212 if (IS_ERR(out_skb))
2213 return PTR_ERR(out_skb);
2214
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002215 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
Steffen Klassert1e532d22018-01-10 09:33:26 +01002216 if (err < 0) {
2217 kfree_skb(out_skb);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002218 return err;
Steffen Klassert1e532d22018-01-10 09:33:26 +01002219 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002220
2221 out_hdr = (struct sadb_msg *) out_skb->data;
2222 out_hdr->sadb_msg_version = PF_KEY_V2;
2223
Herbert Xuf60f6b82005-06-18 22:44:37 -07002224 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002225 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2226 else
2227 out_hdr->sadb_msg_type = event2poltype(c->event);
2228 out_hdr->sadb_msg_errno = 0;
2229 out_hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002230 out_hdr->sadb_msg_pid = c->portid;
Eric Dumazet36f41f82017-08-14 10:16:45 -07002231 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002232 return 0;
2233
2234}
2235
David S. Miller4c93fbb2011-02-25 18:07:06 -08002236static 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 -07002237{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002238 struct net *net = sock_net(sk);
Trent Jaegerdf718372005-12-13 23:12:27 -08002239 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 struct sadb_lifetime *lifetime;
2241 struct sadb_address *sa;
2242 struct sadb_x_policy *pol;
2243 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002244 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002245 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2248 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2249 !ext_hdrs[SADB_X_EXT_POLICY-1])
2250 return -EINVAL;
2251
2252 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2253 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2254 return -EINVAL;
2255 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2256 return -EINVAL;
2257
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002258 xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (xp == NULL)
2260 return -ENOBUFS;
2261
2262 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2263 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2264 xp->priority = pol->sadb_x_policy_priority;
2265
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002266 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 xp->selector.family = xp->family;
2269 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2270 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2271 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2272 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002273 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002275 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2277 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2278
2279 /* Amusing, we set this twice. KAME apps appear to set same value
2280 * in both addresses.
2281 */
2282 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2283
2284 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2285 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002286 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Joe Perchesea110732011-06-13 16:21:26 +00002288 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002289 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01002290 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002291
2292 if (!uctx) {
2293 err = -ENOBUFS;
2294 goto out;
2295 }
2296
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002297 err = security_xfrm_policy_alloc(&xp->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002298 kfree(uctx);
2299
2300 if (err)
2301 goto out;
2302 }
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 xp->lft.soft_byte_limit = XFRM_INF;
2305 xp->lft.hard_byte_limit = XFRM_INF;
2306 xp->lft.soft_packet_limit = XFRM_INF;
2307 xp->lft.hard_packet_limit = XFRM_INF;
2308 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2309 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2310 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2311 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2312 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2313 }
2314 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2315 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2316 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2317 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2318 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2319 }
2320 xp->xfrm_nr = 0;
2321 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2322 (err = parse_ipsecrequests(xp, pol)) < 0)
2323 goto out;
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2326 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002327
Tetsuo Handa2e710292014-04-22 21:48:30 +09002328 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002329
Trent Jaegerdf718372005-12-13 23:12:27 -08002330 if (err)
2331 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002333 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002334 c.event = XFRM_MSG_UPDPOLICY;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002335 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07002336 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002338 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002339 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002340
2341 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return 0;
2344
2345out:
Herbert Xu12a169e2008-10-01 07:03:24 -07002346 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08002347 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return err;
2349}
2350
David S. Miller4c93fbb2011-02-25 18:07:06 -08002351static 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 -07002352{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002353 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 int err;
2355 struct sadb_address *sa;
2356 struct sadb_x_policy *pol;
Paul Moore03e1ad72008-04-12 19:07:52 -07002357 struct xfrm_policy *xp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002359 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002360 struct sadb_x_sec_ctx *sec_ctx;
Brian Haley2db3e472008-04-24 20:38:31 -07002361 struct xfrm_sec_ctx *pol_ctx = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2364 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2365 !ext_hdrs[SADB_X_EXT_POLICY-1])
2366 return -EINVAL;
2367
2368 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2369 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2370 return -EINVAL;
2371
2372 memset(&sel, 0, sizeof(sel));
2373
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002374 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2376 sel.prefixlen_s = sa->sadb_address_prefixlen;
2377 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2378 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2379 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002380 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002382 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2384 sel.prefixlen_d = sa->sadb_address_prefixlen;
2385 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2386 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2387 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002388 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Joe Perchesea110732011-06-13 16:21:26 +00002390 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002391 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01002392 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002393
2394 if (!uctx)
2395 return -ENOMEM;
2396
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002397 err = security_xfrm_policy_alloc(&pol_ctx, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002398 kfree(uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002399 if (err)
2400 return err;
Brian Haley2db3e472008-04-24 20:38:31 -07002401 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002402
Steffen Klassert7e652642018-06-12 14:07:07 +02002403 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, 0, XFRM_POLICY_TYPE_MAIN,
Paul Moore03e1ad72008-04-12 19:07:52 -07002404 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2405 1, &err);
2406 security_xfrm_policy_free(pol_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (xp == NULL)
2408 return -ENOENT;
2409
Tetsuo Handa2e710292014-04-22 21:48:30 +09002410 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08002411
2412 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002413 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08002414
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002415 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002416 c.portid = hdr->sadb_msg_pid;
Tobias Brunner1839faa2008-10-10 14:07:03 -07002417 c.data.byid = 0;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002418 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002419 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2420
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002421out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002422 xfrm_pol_put(xp);
2423 return err;
2424}
2425
David S. Miller4c93fbb2011-02-25 18:07:06 -08002426static 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 -07002427{
2428 int err;
2429 struct sk_buff *out_skb;
2430 struct sadb_msg *out_hdr;
2431 err = 0;
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2434 if (IS_ERR(out_skb)) {
2435 err = PTR_ERR(out_skb);
2436 goto out;
2437 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002438 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
Jeremy Sowden7c80eb12019-05-25 19:09:35 +01002439 if (err < 0) {
2440 kfree_skb(out_skb);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002441 goto out;
Jeremy Sowden7c80eb12019-05-25 19:09:35 +01002442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 out_hdr = (struct sadb_msg *) out_skb->data;
2445 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002446 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 out_hdr->sadb_msg_satype = 0;
2448 out_hdr->sadb_msg_errno = 0;
2449 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2450 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Eric Dumazet36f41f82017-08-14 10:16:45 -07002451 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 err = 0;
2453
2454out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return err;
2456}
2457
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002458static int pfkey_sockaddr_pair_size(sa_family_t family)
2459{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002460 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002461}
2462
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002463static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002464 xfrm_address_t *saddr, xfrm_address_t *daddr,
2465 u16 *family)
2466{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002467 int af, socklen;
2468
Herbert Xu096f41d2017-04-13 18:35:59 +08002469 if (ext_len < 2 || ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002470 return -EINVAL;
2471
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002472 af = pfkey_sockaddr_extract(sa, saddr);
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002473 if (!af)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002474 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002475
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002476 socklen = pfkey_sockaddr_len(af);
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002477 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002478 daddr) != af)
2479 return -EINVAL;
2480
2481 *family = af;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002482 return 0;
2483}
2484
Herbert Xu096f41d2017-04-13 18:35:59 +08002485#ifdef CONFIG_NET_KEY_MIGRATE
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002486static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2487 struct xfrm_migrate *m)
2488{
2489 int err;
2490 struct sadb_x_ipsecrequest *rq2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002491 int mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002492
Herbert Xu096f41d2017-04-13 18:35:59 +08002493 if (len < sizeof(*rq1) ||
2494 len < rq1->sadb_x_ipsecrequest_len ||
2495 rq1->sadb_x_ipsecrequest_len < sizeof(*rq1))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002496 return -EINVAL;
2497
2498 /* old endoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002499 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
Herbert Xu096f41d2017-04-13 18:35:59 +08002500 rq1->sadb_x_ipsecrequest_len - sizeof(*rq1),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002501 &m->old_saddr, &m->old_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002502 &m->old_family);
2503 if (err)
2504 return err;
2505
2506 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2507 len -= rq1->sadb_x_ipsecrequest_len;
2508
Herbert Xu096f41d2017-04-13 18:35:59 +08002509 if (len <= sizeof(*rq2) ||
2510 len < rq2->sadb_x_ipsecrequest_len ||
2511 rq2->sadb_x_ipsecrequest_len < sizeof(*rq2))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002512 return -EINVAL;
2513
2514 /* new endpoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002515 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
Herbert Xu096f41d2017-04-13 18:35:59 +08002516 rq2->sadb_x_ipsecrequest_len - sizeof(*rq2),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002517 &m->new_saddr, &m->new_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002518 &m->new_family);
2519 if (err)
2520 return err;
2521
2522 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2523 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2524 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2525 return -EINVAL;
2526
2527 m->proto = rq1->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002528 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2529 return -EINVAL;
2530 m->mode = mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002531 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2532
2533 return ((int)(rq1->sadb_x_ipsecrequest_len +
2534 rq2->sadb_x_ipsecrequest_len));
2535}
2536
2537static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002538 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002539{
2540 int i, len, ret, err = -EINVAL;
2541 u8 dir;
2542 struct sadb_address *sa;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002543 struct sadb_x_kmaddress *kma;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002544 struct sadb_x_policy *pol;
2545 struct sadb_x_ipsecrequest *rq;
2546 struct xfrm_selector sel;
2547 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002548 struct xfrm_kmaddress k;
Fan Du8d549c42013-11-07 17:47:49 +08002549 struct net *net = sock_net(sk);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002550
2551 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002552 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002553 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2554 err = -EINVAL;
2555 goto out;
2556 }
2557
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002558 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002559 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002560
2561 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2562 err = -EINVAL;
2563 goto out;
2564 }
2565
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002566 if (kma) {
2567 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2568 k.reserved = kma->sadb_x_kmaddress_reserved;
2569 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2570 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2571 &k.local, &k.remote, &k.family);
2572 if (ret < 0) {
2573 err = ret;
2574 goto out;
2575 }
2576 }
2577
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002578 dir = pol->sadb_x_policy_dir - 1;
2579 memset(&sel, 0, sizeof(sel));
2580
2581 /* set source address info of selector */
2582 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2583 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2584 sel.prefixlen_s = sa->sadb_address_prefixlen;
2585 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2586 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2587 if (sel.sport)
Al Viro582ee432007-07-26 17:33:39 +01002588 sel.sport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002589
2590 /* set destination address info of selector */
Himangi Saraogi47162c02014-05-30 21:18:12 +05302591 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002592 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2593 sel.prefixlen_d = sa->sadb_address_prefixlen;
2594 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2595 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2596 if (sel.dport)
Al Viro582ee432007-07-26 17:33:39 +01002597 sel.dport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002598
2599 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2600
2601 /* extract ipsecrequests */
2602 i = 0;
2603 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2604
2605 while (len > 0 && i < XFRM_MAX_DEPTH) {
2606 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2607 if (ret < 0) {
2608 err = ret;
2609 goto out;
2610 } else {
2611 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2612 len -= ret;
2613 i++;
2614 }
2615 }
2616
2617 if (!i || len > 0) {
2618 err = -EINVAL;
2619 goto out;
2620 }
2621
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002622 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
Antony Antony4ab47d42017-06-06 12:12:13 +02002623 kma ? &k : NULL, net, NULL);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002624
2625 out:
2626 return err;
2627}
2628#else
2629static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
Stephen Hemminger7f6daa62011-03-01 22:51:52 -08002630 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002631{
2632 return -ENOPROTOOPT;
2633}
2634#endif
2635
2636
David S. Miller4c93fbb2011-02-25 18:07:06 -08002637static 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 -07002638{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002639 struct net *net = sock_net(sk);
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002640 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002641 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 struct sadb_x_policy *pol;
2643 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002644 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2647 return -EINVAL;
2648
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002649 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2650 if (dir >= XFRM_POLICY_MAX)
2651 return -EINVAL;
2652
Eric Paris215a2dd2007-03-07 16:01:45 -08002653 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Steffen Klassert7e652642018-06-12 14:07:07 +02002654 xp = xfrm_policy_byid(net, DUMMY_MARK, 0, XFRM_POLICY_TYPE_MAIN,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002655 dir, pol->sadb_x_policy_id, delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if (xp == NULL)
2657 return -ENOENT;
2658
Eric Paris215a2dd2007-03-07 16:01:45 -08002659 if (delete) {
Tetsuo Handa2e710292014-04-22 21:48:30 +09002660 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Eric Paris215a2dd2007-03-07 16:01:45 -08002662 if (err)
2663 goto out;
2664 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002665 c.portid = hdr->sadb_msg_pid;
Herbert Xubf08867f92005-06-18 22:44:00 -07002666 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002667 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002668 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002669 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002670 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Eric Paris215a2dd2007-03-07 16:01:45 -08002673out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 xfrm_pol_put(xp);
2675 return err;
2676}
2677
2678static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2679{
Timo Teras83321d62008-03-03 23:40:12 -08002680 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 struct sk_buff *out_skb;
2682 struct sadb_msg *out_hdr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002683 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Timo Teras83321d62008-03-03 23:40:12 -08002685 if (!pfkey_can_dump(&pfk->sk))
2686 return -ENOBUFS;
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2689 if (IS_ERR(out_skb))
2690 return PTR_ERR(out_skb);
2691
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002692 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
Jeremy Sowden7c80eb12019-05-25 19:09:35 +01002693 if (err < 0) {
2694 kfree_skb(out_skb);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002695 return err;
Jeremy Sowden7c80eb12019-05-25 19:09:35 +01002696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08002699 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2701 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2702 out_hdr->sadb_msg_errno = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07002703 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002704 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07002705
2706 if (pfk->dump.skb)
Eric Dumazet36f41f82017-08-14 10:16:45 -07002707 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002708 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07002709 pfk->dump.skb = out_skb;
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 return 0;
2712}
2713
Timo Teras83321d62008-03-03 23:40:12 -08002714static int pfkey_dump_sp(struct pfkey_sock *pfk)
2715{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002716 struct net *net = sock_net(&pfk->sk);
2717 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08002718}
2719
2720static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2721{
Fan Du283bc9f2013-11-07 17:47:50 +08002722 struct net *net = sock_net((struct sock *)pfk);
2723
2724 xfrm_policy_walk_done(&pfk->dump.u.policy, net);
Timo Teras83321d62008-03-03 23:40:12 -08002725}
2726
David S. Miller4c93fbb2011-02-25 18:07:06 -08002727static 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 -07002728{
Timo Teras83321d62008-03-03 23:40:12 -08002729 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Yuejie Shi89e357d2017-03-31 15:10:20 +08002731 mutex_lock(&pfk->dump_lock);
2732 if (pfk->dump.dump != NULL) {
2733 mutex_unlock(&pfk->dump_lock);
Timo Teras83321d62008-03-03 23:40:12 -08002734 return -EBUSY;
Yuejie Shi89e357d2017-03-31 15:10:20 +08002735 }
Timo Teras4c563f72008-02-28 21:31:08 -08002736
Timo Teras83321d62008-03-03 23:40:12 -08002737 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002738 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08002739 pfk->dump.dump = pfkey_dump_sp;
2740 pfk->dump.done = pfkey_dump_sp_done;
2741 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
Yuejie Shi89e357d2017-03-31 15:10:20 +08002742 mutex_unlock(&pfk->dump_lock);
Timo Teras83321d62008-03-03 23:40:12 -08002743
2744 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
David S. Miller214e0052011-02-24 00:02:38 -05002747static int key_notify_policy_flush(const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002750 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002752 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (!skb_out)
2754 return -ENOBUFS;
Johannes Berg4df864c2017-06-16 14:29:21 +02002755 hdr = skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002756 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002757 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002758 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002759 hdr->sadb_msg_version = PF_KEY_V2;
2760 hdr->sadb_msg_errno = (uint8_t) 0;
Nicolas Dichtel85dfb742013-02-18 16:24:20 +01002761 hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002762 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02002763 hdr->sadb_msg_reserved = 0;
Eric Dumazet36f41f82017-08-14 10:16:45 -07002764 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002765 return 0;
2766
2767}
2768
David S. Miller4c93fbb2011-02-25 18:07:06 -08002769static 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 -07002770{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002771 struct net *net = sock_net(sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002772 struct km_event c;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002773 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Tetsuo Handa2e710292014-04-22 21:48:30 +09002775 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002776 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002777 if (err || err2) {
2778 if (err == -ESRCH) /* empty table - old silent behavior */
2779 return 0;
2780 return err;
2781 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002782
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002783 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002784 c.event = XFRM_MSG_FLUSHPOLICY;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002785 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002786 c.seq = hdr->sadb_msg_seq;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002787 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002788 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
2790 return 0;
2791}
2792
2793typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002794 const struct sadb_msg *hdr, void * const *ext_hdrs);
Mathias Krause8603b9552013-06-26 23:56:59 +02002795static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 [SADB_RESERVED] = pfkey_reserved,
2797 [SADB_GETSPI] = pfkey_getspi,
2798 [SADB_UPDATE] = pfkey_add,
2799 [SADB_ADD] = pfkey_add,
2800 [SADB_DELETE] = pfkey_delete,
2801 [SADB_GET] = pfkey_get,
2802 [SADB_ACQUIRE] = pfkey_acquire,
2803 [SADB_REGISTER] = pfkey_register,
2804 [SADB_EXPIRE] = NULL,
2805 [SADB_FLUSH] = pfkey_flush,
2806 [SADB_DUMP] = pfkey_dump,
2807 [SADB_X_PROMISC] = pfkey_promisc,
2808 [SADB_X_PCHANGE] = NULL,
2809 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2810 [SADB_X_SPDADD] = pfkey_spdadd,
2811 [SADB_X_SPDDELETE] = pfkey_spddelete,
2812 [SADB_X_SPDGET] = pfkey_spdget,
2813 [SADB_X_SPDACQUIRE] = NULL,
2814 [SADB_X_SPDDUMP] = pfkey_spddump,
2815 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2816 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2817 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002818 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819};
2820
David S. Miller4c93fbb2011-02-25 18:07:06 -08002821static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
2823 void *ext_hdrs[SADB_EXT_MAX];
2824 int err;
2825
Eric Dumazet36f41f82017-08-14 10:16:45 -07002826 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002827 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2830 err = parse_exthdrs(skb, hdr, ext_hdrs);
2831 if (!err) {
2832 err = -EOPNOTSUPP;
2833 if (pfkey_funcs[hdr->sadb_msg_type])
2834 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2835 }
2836 return err;
2837}
2838
2839static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2840{
2841 struct sadb_msg *hdr = NULL;
2842
2843 if (skb->len < sizeof(*hdr)) {
2844 *errp = -EMSGSIZE;
2845 } else {
2846 hdr = (struct sadb_msg *) skb->data;
2847 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2848 hdr->sadb_msg_reserved != 0 ||
2849 (hdr->sadb_msg_type <= SADB_RESERVED ||
2850 hdr->sadb_msg_type > SADB_MAX)) {
2851 hdr = NULL;
2852 *errp = -EINVAL;
2853 } else if (hdr->sadb_msg_len != (skb->len /
2854 sizeof(uint64_t)) ||
2855 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2856 sizeof(uint64_t))) {
2857 hdr = NULL;
2858 *errp = -EMSGSIZE;
2859 } else {
2860 *errp = 0;
2861 }
2862 }
2863 return hdr;
2864}
2865
David S. Miller4c93fbb2011-02-25 18:07:06 -08002866static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
2867 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
Herbert Xuf3980352007-12-19 23:44:29 -08002869 unsigned int id = d->desc.sadb_alg_id;
2870
2871 if (id >= sizeof(t->aalgos) * 8)
2872 return 0;
2873
2874 return (t->aalgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
David S. Miller4c93fbb2011-02-25 18:07:06 -08002877static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
2878 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
Herbert Xuf3980352007-12-19 23:44:29 -08002880 unsigned int id = d->desc.sadb_alg_id;
2881
2882 if (id >= sizeof(t->ealgos) * 8)
2883 return 0;
2884
2885 return (t->ealgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
2887
David S. Miller4c93fbb2011-02-25 18:07:06 -08002888static int count_ah_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
2890 int i, sz = 0;
2891
2892 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002893 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (!aalg)
2895 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002896 if (!aalg->pfkey_supported)
2897 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (aalg_tmpl_set(t, aalg) && aalg->available)
2899 sz += sizeof(struct sadb_comb);
2900 }
2901 return sz + sizeof(struct sadb_prop);
2902}
2903
David S. Miller4c93fbb2011-02-25 18:07:06 -08002904static int count_esp_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
2906 int i, k, sz = 0;
2907
2908 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002909 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 if (!ealg)
2911 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002912
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002913 if (!ealg->pfkey_supported)
2914 continue;
2915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2917 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 for (k = 1; ; k++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002920 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 if (!aalg)
2922 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002923
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002924 if (!aalg->pfkey_supported)
2925 continue;
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 if (aalg_tmpl_set(t, aalg) && aalg->available)
2928 sz += sizeof(struct sadb_comb);
2929 }
2930 }
2931 return sz + sizeof(struct sadb_prop);
2932}
2933
David S. Miller4c93fbb2011-02-25 18:07:06 -08002934static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
2936 struct sadb_prop *p;
2937 int i;
2938
Johannes Berg4df864c2017-06-16 14:29:21 +02002939 p = skb_put(skb, sizeof(struct sadb_prop));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2941 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2942 p->sadb_prop_replay = 32;
2943 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2944
2945 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002946 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 if (!aalg)
2948 break;
2949
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002950 if (!aalg->pfkey_supported)
2951 continue;
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2954 struct sadb_comb *c;
Johannes Bergb080db52017-06-16 14:29:19 +02002955 c = skb_put_zero(skb, sizeof(struct sadb_comb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2957 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2958 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2959 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2960 c->sadb_comb_hard_addtime = 24*60*60;
2961 c->sadb_comb_soft_addtime = 20*60*60;
2962 c->sadb_comb_hard_usetime = 8*60*60;
2963 c->sadb_comb_soft_usetime = 7*60*60;
2964 }
2965 }
2966}
2967
David S. Miller4c93fbb2011-02-25 18:07:06 -08002968static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
2970 struct sadb_prop *p;
2971 int i, k;
2972
Johannes Berg4df864c2017-06-16 14:29:21 +02002973 p = skb_put(skb, sizeof(struct sadb_prop));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2975 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2976 p->sadb_prop_replay = 32;
2977 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2978
2979 for (i=0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002980 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (!ealg)
2982 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002983
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002984 if (!ealg->pfkey_supported)
2985 continue;
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2988 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 for (k = 1; ; k++) {
2991 struct sadb_comb *c;
David S. Miller4c93fbb2011-02-25 18:07:06 -08002992 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 if (!aalg)
2994 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002995 if (!aalg->pfkey_supported)
2996 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2998 continue;
Johannes Berg4df864c2017-06-16 14:29:21 +02002999 c = skb_put(skb, sizeof(struct sadb_comb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 memset(c, 0, sizeof(*c));
3001 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
3002 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
3003 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
3004 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
3005 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
3006 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
3007 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
3008 c->sadb_comb_hard_addtime = 24*60*60;
3009 c->sadb_comb_soft_addtime = 20*60*60;
3010 c->sadb_comb_hard_usetime = 8*60*60;
3011 c->sadb_comb_soft_usetime = 7*60*60;
3012 }
3013 }
3014}
3015
David S. Miller214e0052011-02-24 00:02:38 -05003016static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003017{
3018 return 0;
3019}
3020
David S. Miller214e0052011-02-24 00:02:38 -05003021static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
3023 struct sk_buff *out_skb;
3024 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003025 int hard;
3026 int hsc;
3027
Herbert Xubf08867f92005-06-18 22:44:00 -07003028 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003029 if (hard)
3030 hsc = 2;
3031 else
3032 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Herbert Xu050f0092007-10-09 13:31:47 -07003034 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 if (IS_ERR(out_skb))
3036 return PTR_ERR(out_skb);
3037
3038 out_hdr = (struct sadb_msg *) out_skb->data;
3039 out_hdr->sadb_msg_version = PF_KEY_V2;
3040 out_hdr->sadb_msg_type = SADB_EXPIRE;
3041 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3042 out_hdr->sadb_msg_errno = 0;
3043 out_hdr->sadb_msg_reserved = 0;
3044 out_hdr->sadb_msg_seq = 0;
3045 out_hdr->sadb_msg_pid = 0;
3046
Eric Dumazet36f41f82017-08-14 10:16:45 -07003047 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3048 xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 return 0;
3050}
3051
David S. Miller214e0052011-02-24 00:02:38 -05003052static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003053{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003054 struct net *net = x ? xs_net(x) : c->net;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003055 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3056
3057 if (atomic_read(&net_pfkey->socks_nr) == 0)
Jamal Hadi Salim99c6f602008-06-10 14:25:34 -07003058 return 0;
3059
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003060 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003061 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003062 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003063 case XFRM_MSG_DELSA:
3064 case XFRM_MSG_NEWSA:
3065 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003066 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003067 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003068 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003069 case XFRM_MSG_NEWAE: /* not yet supported */
3070 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003071 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003072 pr_err("pfkey: Unknown SA event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003073 break;
3074 }
3075
3076 return 0;
3077}
3078
David S. Miller214e0052011-02-24 00:02:38 -05003079static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003080{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003081 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3082 return 0;
3083
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003084 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003085 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003086 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003087 case XFRM_MSG_DELPOLICY:
3088 case XFRM_MSG_NEWPOLICY:
3089 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003090 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003091 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003092 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3093 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003094 return key_notify_policy_flush(c);
3095 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003096 pr_err("pfkey: Unknown policy event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003097 break;
3098 }
3099
3100 return 0;
3101}
3102
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103static u32 get_acqseq(void)
3104{
3105 u32 res;
Eric Dumazet28aecb92010-01-29 04:05:52 +00003106 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Eric Dumazet28aecb92010-01-29 04:05:52 +00003108 do {
3109 res = atomic_inc_return(&acqseq);
3110 } while (!res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return res;
3112}
3113
Horia Geanta0f245582014-02-12 16:20:06 +02003114static bool pfkey_is_alive(const struct km_event *c)
3115{
3116 struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id);
3117 struct sock *sk;
3118 bool is_alive = false;
3119
3120 rcu_read_lock();
3121 sk_for_each_rcu(sk, &net_pfkey->table) {
3122 if (pfkey_sk(sk)->registered) {
3123 is_alive = true;
3124 break;
3125 }
3126 }
3127 rcu_read_unlock();
3128
3129 return is_alive;
3130}
3131
Fan Du65e07362012-08-15 10:13:47 +08003132static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
3134 struct sk_buff *skb;
3135 struct sadb_msg *hdr;
3136 struct sadb_address *addr;
3137 struct sadb_x_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 int sockaddr_size;
3139 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003140 struct sadb_x_sec_ctx *sec_ctx;
3141 struct xfrm_sec_ctx *xfrm_ctx;
3142 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3145 if (!sockaddr_size)
3146 return -EINVAL;
3147
3148 size = sizeof(struct sadb_msg) +
3149 (sizeof(struct sadb_address) * 2) +
3150 (sockaddr_size * 2) +
3151 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 if (x->id.proto == IPPROTO_AH)
3154 size += count_ah_combs(t);
3155 else if (x->id.proto == IPPROTO_ESP)
3156 size += count_esp_combs(t);
3157
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003158 if ((xfrm_ctx = x->security)) {
3159 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3160 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3161 }
3162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 skb = alloc_skb(size + 16, GFP_ATOMIC);
3164 if (skb == NULL)
3165 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003166
Johannes Berg4df864c2017-06-16 14:29:21 +02003167 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 hdr->sadb_msg_version = PF_KEY_V2;
3169 hdr->sadb_msg_type = SADB_ACQUIRE;
3170 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3171 hdr->sadb_msg_len = size / sizeof(uint64_t);
3172 hdr->sadb_msg_errno = 0;
3173 hdr->sadb_msg_reserved = 0;
3174 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3175 hdr->sadb_msg_pid = 0;
3176
3177 /* src address */
Johannes Berg4df864c2017-06-16 14:29:21 +02003178 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003179 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 (sizeof(struct sadb_address)+sockaddr_size)/
3181 sizeof(uint64_t);
3182 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3183 addr->sadb_address_proto = 0;
3184 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003185 addr->sadb_address_prefixlen =
3186 pfkey_sockaddr_fill(&x->props.saddr, 0,
3187 (struct sockaddr *) (addr + 1),
3188 x->props.family);
3189 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003191
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 /* dst address */
Johannes Berg4df864c2017-06-16 14:29:21 +02003193 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 addr->sadb_address_len =
3195 (sizeof(struct sadb_address)+sockaddr_size)/
3196 sizeof(uint64_t);
3197 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3198 addr->sadb_address_proto = 0;
3199 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003200 addr->sadb_address_prefixlen =
3201 pfkey_sockaddr_fill(&x->id.daddr, 0,
3202 (struct sockaddr *) (addr + 1),
3203 x->props.family);
3204 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 BUG();
3206
Johannes Berg4df864c2017-06-16 14:29:21 +02003207 pol = skb_put(skb, sizeof(struct sadb_x_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3209 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3210 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
Fan Du65e07362012-08-15 10:13:47 +08003211 pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1;
Dan Carpenterff862a42013-07-28 23:04:45 +03003212 pol->sadb_x_policy_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 pol->sadb_x_policy_id = xp->index;
Dan Carpenterff862a42013-07-28 23:04:45 +03003214 pol->sadb_x_policy_priority = xp->priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
3216 /* Set sadb_comb's. */
3217 if (x->id.proto == IPPROTO_AH)
3218 dump_ah_combs(skb, t);
3219 else if (x->id.proto == IPPROTO_ESP)
3220 dump_esp_combs(skb, t);
3221
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003222 /* security context */
3223 if (xfrm_ctx) {
Johannes Berg4df864c2017-06-16 14:29:21 +02003224 sec_ctx = skb_put(skb,
3225 sizeof(struct sadb_x_sec_ctx) + ctx_size);
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003226 sec_ctx->sadb_x_sec_len =
3227 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3228 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3229 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3230 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3231 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3232 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3233 xfrm_ctx->ctx_len);
3234 }
3235
Eric Dumazet36f41f82017-08-14 10:16:45 -07003236 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3237 xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003240static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003241 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003243 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 struct xfrm_policy *xp;
3245 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003246 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003248 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 case AF_INET:
3250 if (opt != IP_IPSEC_POLICY) {
3251 *dir = -EOPNOTSUPP;
3252 return NULL;
3253 }
3254 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00003255#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 case AF_INET6:
3257 if (opt != IPV6_IPSEC_POLICY) {
3258 *dir = -EOPNOTSUPP;
3259 return NULL;
3260 }
3261 break;
3262#endif
3263 default:
3264 *dir = -EINVAL;
3265 return NULL;
3266 }
3267
3268 *dir = -EINVAL;
3269
3270 if (len < sizeof(struct sadb_x_policy) ||
3271 pol->sadb_x_policy_len*8 > len ||
3272 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3273 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3274 return NULL;
3275
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003276 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 if (xp == NULL) {
3278 *dir = -ENOBUFS;
3279 return NULL;
3280 }
3281
3282 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3283 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3284
3285 xp->lft.soft_byte_limit = XFRM_INF;
3286 xp->lft.hard_byte_limit = XFRM_INF;
3287 xp->lft.soft_packet_limit = XFRM_INF;
3288 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003289 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
3291 xp->xfrm_nr = 0;
3292 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3293 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3294 goto out;
3295
Trent Jaegerdf718372005-12-13 23:12:27 -08003296 /* security context too */
3297 if (len >= (pol->sadb_x_policy_len*8 +
3298 sizeof(struct sadb_x_sec_ctx))) {
3299 char *p = (char *)pol;
3300 struct xfrm_user_sec_ctx *uctx;
3301
3302 p += pol->sadb_x_policy_len*8;
3303 sec_ctx = (struct sadb_x_sec_ctx *)p;
3304 if (len < pol->sadb_x_policy_len*8 +
Steffen Klassertd90c9022017-05-05 07:40:42 +02003305 sec_ctx->sadb_x_sec_len*8) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003306 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003307 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003308 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003309 if ((*dir = verify_sec_ctx_len(p)))
3310 goto out;
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01003311 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_ATOMIC);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01003312 *dir = security_xfrm_policy_alloc(&xp->security, uctx, GFP_ATOMIC);
Trent Jaegerdf718372005-12-13 23:12:27 -08003313 kfree(uctx);
3314
3315 if (*dir)
3316 goto out;
3317 }
3318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 *dir = pol->sadb_x_policy_dir-1;
3320 return xp;
3321
3322out:
Alexey Dobriyan70e90672008-11-06 23:08:37 -08003323 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08003324 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 return NULL;
3326}
3327
Al Viro5d36b182006-11-08 00:24:06 -08003328static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329{
3330 struct sk_buff *skb;
3331 struct sadb_msg *hdr;
3332 struct sadb_sa *sa;
3333 struct sadb_address *addr;
3334 struct sadb_x_nat_t_port *n_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 int sockaddr_size;
3336 int size;
3337 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3338 struct xfrm_encap_tmpl *natt = NULL;
3339
3340 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3341 if (!sockaddr_size)
3342 return -EINVAL;
3343
3344 if (!satype)
3345 return -EINVAL;
3346
3347 if (!x->encap)
3348 return -EINVAL;
3349
3350 natt = x->encap;
3351
3352 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3353 *
3354 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3355 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3356 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 size = sizeof(struct sadb_msg) +
3359 sizeof(struct sadb_sa) +
3360 (sizeof(struct sadb_address) * 2) +
3361 (sockaddr_size * 2) +
3362 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 skb = alloc_skb(size + 16, GFP_ATOMIC);
3365 if (skb == NULL)
3366 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003367
Johannes Berg4df864c2017-06-16 14:29:21 +02003368 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 hdr->sadb_msg_version = PF_KEY_V2;
3370 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3371 hdr->sadb_msg_satype = satype;
3372 hdr->sadb_msg_len = size / sizeof(uint64_t);
3373 hdr->sadb_msg_errno = 0;
3374 hdr->sadb_msg_reserved = 0;
3375 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3376 hdr->sadb_msg_pid = 0;
3377
3378 /* SA */
Johannes Berg4df864c2017-06-16 14:29:21 +02003379 sa = skb_put(skb, sizeof(struct sadb_sa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3381 sa->sadb_sa_exttype = SADB_EXT_SA;
3382 sa->sadb_sa_spi = x->id.spi;
3383 sa->sadb_sa_replay = 0;
3384 sa->sadb_sa_state = 0;
3385 sa->sadb_sa_auth = 0;
3386 sa->sadb_sa_encrypt = 0;
3387 sa->sadb_sa_flags = 0;
3388
3389 /* ADDRESS_SRC (old addr) */
Johannes Berg4df864c2017-06-16 14:29:21 +02003390 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003391 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 (sizeof(struct sadb_address)+sockaddr_size)/
3393 sizeof(uint64_t);
3394 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3395 addr->sadb_address_proto = 0;
3396 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003397 addr->sadb_address_prefixlen =
3398 pfkey_sockaddr_fill(&x->props.saddr, 0,
3399 (struct sockaddr *) (addr + 1),
3400 x->props.family);
3401 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 BUG();
3403
3404 /* NAT_T_SPORT (old port) */
Johannes Berg4df864c2017-06-16 14:29:21 +02003405 n_port = skb_put(skb, sizeof(*n_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3407 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3408 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3409 n_port->sadb_x_nat_t_port_reserved = 0;
3410
3411 /* ADDRESS_DST (new addr) */
Johannes Berg4df864c2017-06-16 14:29:21 +02003412 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003413 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 (sizeof(struct sadb_address)+sockaddr_size)/
3415 sizeof(uint64_t);
3416 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3417 addr->sadb_address_proto = 0;
3418 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003419 addr->sadb_address_prefixlen =
3420 pfkey_sockaddr_fill(ipaddr, 0,
3421 (struct sockaddr *) (addr + 1),
3422 x->props.family);
3423 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 BUG();
3425
3426 /* NAT_T_DPORT (new port) */
Johannes Berg4df864c2017-06-16 14:29:21 +02003427 n_port = skb_put(skb, sizeof(*n_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3429 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3430 n_port->sadb_x_nat_t_port_port = sport;
3431 n_port->sadb_x_nat_t_port_reserved = 0;
3432
Eric Dumazet36f41f82017-08-14 10:16:45 -07003433 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3434 xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435}
3436
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003437#ifdef CONFIG_NET_KEY_MIGRATE
3438static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
David S. Miller183cad12011-02-24 00:28:01 -05003439 const struct xfrm_selector *sel)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003440{
3441 struct sadb_address *addr;
Johannes Berg4df864c2017-06-16 14:29:21 +02003442 addr = skb_put(skb, sizeof(struct sadb_address) + sasize);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003443 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3444 addr->sadb_address_exttype = type;
3445 addr->sadb_address_proto = sel->proto;
3446 addr->sadb_address_reserved = 0;
3447
3448 switch (type) {
3449 case SADB_EXT_ADDRESS_SRC:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003450 addr->sadb_address_prefixlen = sel->prefixlen_s;
3451 pfkey_sockaddr_fill(&sel->saddr, 0,
3452 (struct sockaddr *)(addr + 1),
3453 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003454 break;
3455 case SADB_EXT_ADDRESS_DST:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003456 addr->sadb_address_prefixlen = sel->prefixlen_d;
3457 pfkey_sockaddr_fill(&sel->daddr, 0,
3458 (struct sockaddr *)(addr + 1),
3459 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003460 break;
3461 default:
3462 return -EINVAL;
3463 }
3464
3465 return 0;
3466}
3467
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003468
David S. Miller183cad12011-02-24 00:28:01 -05003469static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003470{
3471 struct sadb_x_kmaddress *kma;
3472 u8 *sa;
3473 int family = k->family;
3474 int socklen = pfkey_sockaddr_len(family);
3475 int size_req;
3476
3477 size_req = (sizeof(struct sadb_x_kmaddress) +
3478 pfkey_sockaddr_pair_size(family));
3479
Johannes Bergb080db52017-06-16 14:29:19 +02003480 kma = skb_put_zero(skb, size_req);
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003481 kma->sadb_x_kmaddress_len = size_req / 8;
3482 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3483 kma->sadb_x_kmaddress_reserved = k->reserved;
3484
3485 sa = (u8 *)(kma + 1);
3486 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3487 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3488 return -EINVAL;
3489
3490 return 0;
3491}
3492
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003493static int set_ipsecrequest(struct sk_buff *skb,
3494 uint8_t proto, uint8_t mode, int level,
3495 uint32_t reqid, uint8_t family,
David S. Miller183cad12011-02-24 00:28:01 -05003496 const xfrm_address_t *src, const xfrm_address_t *dst)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003497{
3498 struct sadb_x_ipsecrequest *rq;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003499 u8 *sa;
3500 int socklen = pfkey_sockaddr_len(family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003501 int size_req;
3502
3503 size_req = sizeof(struct sadb_x_ipsecrequest) +
3504 pfkey_sockaddr_pair_size(family);
3505
Johannes Bergb080db52017-06-16 14:29:19 +02003506 rq = skb_put_zero(skb, size_req);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003507 rq->sadb_x_ipsecrequest_len = size_req;
3508 rq->sadb_x_ipsecrequest_proto = proto;
3509 rq->sadb_x_ipsecrequest_mode = mode;
3510 rq->sadb_x_ipsecrequest_level = level;
3511 rq->sadb_x_ipsecrequest_reqid = reqid;
3512
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003513 sa = (u8 *) (rq + 1);
3514 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3515 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003516 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003517
3518 return 0;
3519}
3520#endif
3521
3522#ifdef CONFIG_NET_KEY_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05003523static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3524 const struct xfrm_migrate *m, int num_bundles,
Antony Antony8bafd732017-06-06 12:12:14 +02003525 const struct xfrm_kmaddress *k,
3526 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003527{
3528 int i;
3529 int sasize_sel;
3530 int size = 0;
3531 int size_pol = 0;
3532 struct sk_buff *skb;
3533 struct sadb_msg *hdr;
3534 struct sadb_x_policy *pol;
David S. Miller183cad12011-02-24 00:28:01 -05003535 const struct xfrm_migrate *mp;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003536
3537 if (type != XFRM_POLICY_TYPE_MAIN)
3538 return 0;
3539
3540 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3541 return -EINVAL;
3542
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003543 if (k != NULL) {
3544 /* addresses for KM */
3545 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3546 pfkey_sockaddr_pair_size(k->family));
3547 }
3548
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003549 /* selector */
3550 sasize_sel = pfkey_sockaddr_size(sel->family);
3551 if (!sasize_sel)
3552 return -EINVAL;
3553 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3554
3555 /* policy info */
3556 size_pol += sizeof(struct sadb_x_policy);
3557
3558 /* ipsecrequests */
3559 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3560 /* old locator pair */
3561 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3562 pfkey_sockaddr_pair_size(mp->old_family);
3563 /* new locator pair */
3564 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3565 pfkey_sockaddr_pair_size(mp->new_family);
3566 }
3567
3568 size += sizeof(struct sadb_msg) + size_pol;
3569
3570 /* alloc buffer */
3571 skb = alloc_skb(size, GFP_ATOMIC);
3572 if (skb == NULL)
3573 return -ENOMEM;
3574
Johannes Berg4df864c2017-06-16 14:29:21 +02003575 hdr = skb_put(skb, sizeof(struct sadb_msg));
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003576 hdr->sadb_msg_version = PF_KEY_V2;
3577 hdr->sadb_msg_type = SADB_X_MIGRATE;
3578 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3579 hdr->sadb_msg_len = size / 8;
3580 hdr->sadb_msg_errno = 0;
3581 hdr->sadb_msg_reserved = 0;
3582 hdr->sadb_msg_seq = 0;
3583 hdr->sadb_msg_pid = 0;
3584
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003585 /* Addresses to be used by KM for negotiation, if ext is available */
3586 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
Julia Lawall89eb06f2012-04-08 22:41:10 +00003587 goto err;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003588
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003589 /* selector src */
3590 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3591
3592 /* selector dst */
3593 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3594
3595 /* policy information */
Johannes Berg4df864c2017-06-16 14:29:21 +02003596 pol = skb_put(skb, sizeof(struct sadb_x_policy));
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003597 pol->sadb_x_policy_len = size_pol / 8;
3598 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3599 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3600 pol->sadb_x_policy_dir = dir + 1;
Dan Carpenterff862a42013-07-28 23:04:45 +03003601 pol->sadb_x_policy_reserved = 0;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003602 pol->sadb_x_policy_id = 0;
3603 pol->sadb_x_policy_priority = 0;
3604
3605 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3606 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003607 int mode = pfkey_mode_from_xfrm(mp->mode);
3608 if (mode < 0)
Patrick McHardyd4782c32008-01-20 17:24:29 -08003609 goto err;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003610 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003611 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3612 mp->reqid, mp->old_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003613 &mp->old_saddr, &mp->old_daddr) < 0)
3614 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003615
3616 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003617 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003618 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3619 mp->reqid, mp->new_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003620 &mp->new_saddr, &mp->new_daddr) < 0)
3621 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003622 }
3623
3624 /* broadcast migrate message to sockets */
Eric Dumazet36f41f82017-08-14 10:16:45 -07003625 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003626
3627 return 0;
Patrick McHardyd4782c32008-01-20 17:24:29 -08003628
3629err:
3630 kfree_skb(skb);
3631 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003632}
3633#else
David S. Miller183cad12011-02-24 00:28:01 -05003634static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3635 const struct xfrm_migrate *m, int num_bundles,
Antony Antony8bafd732017-06-06 12:12:14 +02003636 const struct xfrm_kmaddress *k,
3637 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003638{
3639 return -ENOPROTOOPT;
3640}
3641#endif
3642
Ying Xue1b784142015-03-02 15:37:48 +08003643static int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
3645 struct sock *sk = sock->sk;
3646 struct sk_buff *skb = NULL;
3647 struct sadb_msg *hdr = NULL;
3648 int err;
Fan Du283bc9f2013-11-07 17:47:50 +08003649 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
3651 err = -EOPNOTSUPP;
3652 if (msg->msg_flags & MSG_OOB)
3653 goto out;
3654
3655 err = -EMSGSIZE;
Eric Dumazet95c96172012-04-15 05:58:06 +00003656 if ((unsigned int)len > sk->sk_sndbuf - 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 goto out;
3658
3659 err = -ENOBUFS;
3660 skb = alloc_skb(len, GFP_KERNEL);
3661 if (skb == NULL)
3662 goto out;
3663
3664 err = -EFAULT;
Al Viro6ce8e9c2014-04-06 21:25:44 -04003665 if (memcpy_from_msg(skb_put(skb,len), msg, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 goto out;
3667
3668 hdr = pfkey_get_base_msg(skb, &err);
3669 if (!hdr)
3670 goto out;
3671
Fan Du283bc9f2013-11-07 17:47:50 +08003672 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 err = pfkey_process(sk, skb, hdr);
Fan Du283bc9f2013-11-07 17:47:50 +08003674 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
3676out:
3677 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3678 err = 0;
Wei Yongjun6f961062009-02-25 00:31:04 +00003679 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
3681 return err ? : len;
3682}
3683
Ying Xue1b784142015-03-02 15:37:48 +08003684static int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 int flags)
3686{
3687 struct sock *sk = sock->sk;
Timo Teras83321d62008-03-03 23:40:12 -08003688 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 struct sk_buff *skb;
3690 int copied, err;
3691
3692 err = -EINVAL;
3693 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3694 goto out;
3695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3697 if (skb == NULL)
3698 goto out;
3699
3700 copied = skb->len;
3701 if (copied > len) {
3702 msg->msg_flags |= MSG_TRUNC;
3703 copied = len;
3704 }
3705
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003706 skb_reset_transport_header(skb);
David S. Miller51f3d022014-11-05 16:46:40 -05003707 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 if (err)
3709 goto out_free;
3710
Neil Horman3b885782009-10-12 13:26:31 -07003711 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
3713 err = (flags & MSG_TRUNC) ? skb->len : copied;
3714
Timo Teras83321d62008-03-03 23:40:12 -08003715 if (pfk->dump.dump != NULL &&
3716 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3717 pfkey_do_dump(pfk);
3718
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719out_free:
3720 skb_free_datagram(sk, skb);
3721out:
3722 return err;
3723}
3724
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003725static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 .family = PF_KEY,
3727 .owner = THIS_MODULE,
3728 /* Operations that make no sense on pfkey sockets. */
3729 .bind = sock_no_bind,
3730 .connect = sock_no_connect,
3731 .socketpair = sock_no_socketpair,
3732 .accept = sock_no_accept,
3733 .getname = sock_no_getname,
3734 .ioctl = sock_no_ioctl,
3735 .listen = sock_no_listen,
3736 .shutdown = sock_no_shutdown,
3737 .setsockopt = sock_no_setsockopt,
3738 .getsockopt = sock_no_getsockopt,
3739 .mmap = sock_no_mmap,
3740 .sendpage = sock_no_sendpage,
3741
3742 /* Now the operations that really occur. */
3743 .release = pfkey_release,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003744 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 .sendmsg = pfkey_sendmsg,
3746 .recvmsg = pfkey_recvmsg,
3747};
3748
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003749static const struct net_proto_family pfkey_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 .family = PF_KEY,
3751 .create = pfkey_create,
3752 .owner = THIS_MODULE,
3753};
3754
3755#ifdef CONFIG_PROC_FS
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003756static int pfkey_seq_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
Li Zefan27b5b862010-02-08 23:20:29 +00003758 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003760 if (v == SEQ_START_TOKEN)
3761 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3762 else
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003763 seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 s,
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003765 refcount_read(&s->sk_refcnt),
Eric Dumazet31e6d362009-06-17 19:05:41 -07003766 sk_rmem_alloc_get(s),
3767 sk_wmem_alloc_get(s),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06003768 from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 sock_i_ino(s)
3770 );
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772}
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003773
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003774static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
stephen hemmingerada440e2011-02-23 09:06:50 +00003775 __acquires(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003776{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003777 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003778 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003779
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003780 rcu_read_lock();
3781 return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003782}
3783
3784static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3785{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003786 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003787 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3788
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003789 return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003790}
3791
3792static void pfkey_seq_stop(struct seq_file *f, void *v)
stephen hemmingerada440e2011-02-23 09:06:50 +00003793 __releases(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003794{
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003795 rcu_read_unlock();
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003796}
3797
Stephen Hemminger98147d52009-09-01 19:25:02 +00003798static const struct seq_operations pfkey_seq_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003799 .start = pfkey_seq_start,
3800 .next = pfkey_seq_next,
3801 .stop = pfkey_seq_stop,
3802 .show = pfkey_seq_show,
3803};
3804
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003805static int __net_init pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003806{
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003807 struct proc_dir_entry *e;
3808
Christoph Hellwigc3506372018-04-10 19:42:55 +02003809 e = proc_create_net("pfkey", 0, net->proc_net, &pfkey_seq_ops,
3810 sizeof(struct seq_net_private));
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003811 if (e == NULL)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003812 return -ENOMEM;
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003813
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003814 return 0;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003815}
3816
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003817static void __net_exit pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003818{
Gao fengece31ff2013-02-18 01:34:56 +00003819 remove_proc_entry("pfkey", net->proc_net);
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003820}
3821#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003822static inline int pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003823{
3824 return 0;
3825}
3826
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003827static inline void pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003828{
3829}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830#endif
3831
3832static struct xfrm_mgr pfkeyv2_mgr =
3833{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 .notify = pfkey_send_notify,
3835 .acquire = pfkey_send_acquire,
3836 .compile_policy = pfkey_compile_policy,
3837 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003838 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003839 .migrate = pfkey_send_migrate,
Horia Geanta0f245582014-02-12 16:20:06 +02003840 .is_alive = pfkey_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841};
3842
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003843static int __net_init pfkey_net_init(struct net *net)
3844{
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003845 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003846 int rv;
3847
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003848 INIT_HLIST_HEAD(&net_pfkey->table);
3849 atomic_set(&net_pfkey->socks_nr, 0);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003850
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003851 rv = pfkey_init_proc(net);
3852
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003853 return rv;
3854}
3855
3856static void __net_exit pfkey_net_exit(struct net *net)
3857{
3858 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3859
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003860 pfkey_exit_proc(net);
Vasily Averin663faea2017-11-12 22:26:53 +03003861 WARN_ON(!hlist_empty(&net_pfkey->table));
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003862}
3863
3864static struct pernet_operations pfkey_net_ops = {
3865 .init = pfkey_net_init,
3866 .exit = pfkey_net_exit,
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003867 .id = &pfkey_net_id,
3868 .size = sizeof(struct netns_pfkey),
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003869};
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871static void __exit ipsec_pfkey_exit(void)
3872{
3873 xfrm_unregister_km(&pfkeyv2_mgr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003875 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 proto_unregister(&key_proto);
3877}
3878
3879static int __init ipsec_pfkey_init(void)
3880{
3881 int err = proto_register(&key_proto, 0);
3882
3883 if (err != 0)
3884 goto out;
3885
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003886 err = register_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 if (err != 0)
3888 goto out_unregister_key_proto;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003889 err = sock_register(&pfkey_family_ops);
3890 if (err != 0)
3891 goto out_unregister_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 err = xfrm_register_km(&pfkeyv2_mgr);
3893 if (err != 0)
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003894 goto out_sock_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895out:
3896 return err;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003897
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898out_sock_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003900out_unregister_pernet:
3901 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902out_unregister_key_proto:
3903 proto_unregister(&key_proto);
3904 goto out;
3905}
3906
3907module_init(ipsec_pfkey_init);
3908module_exit(ipsec_pfkey_exit);
3909MODULE_LICENSE("GPL");
3910MODULE_ALIAS_NETPROTO(PF_KEY);