blob: d40861a048fe0e7c0088c7b05594c891dac3941e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Maxim Giryaev <gem@asplinux.ru>
10 * David S. Miller <davem@redhat.com>
11 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
12 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
14 * Derek Atkins <derek@ihtfp.com>
15 */
16
Randy Dunlap4fc268d2006-01-11 12:17:47 -080017#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/socket.h>
21#include <linux/pfkeyv2.h>
22#include <linux/ipsec.h>
23#include <linux/skbuff.h>
24#include <linux/rtnetlink.h>
25#include <linux/in.h>
26#include <linux/in6.h>
27#include <linux/proc_fs.h>
28#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020030#include <net/net_namespace.h>
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080031#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/xfrm.h>
33
34#include <net/sock.h>
35
36#define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
37#define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
38
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030039static unsigned int pfkey_net_id __read_mostly;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080040struct netns_pfkey {
41 /* List of all pfkey sockets. */
42 struct hlist_head table;
43 atomic_t socks_nr;
44};
stephen hemminger7f6b9db2010-02-22 07:57:19 +000045static DEFINE_MUTEX(pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jamal Hadi Salimbd557752010-02-22 16:20:22 -080047#define DUMMY_MARK 0
Mathias Krausee473fcb2013-06-26 23:56:58 +020048static const struct xfrm_mark dummy_mark = {0, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049struct pfkey_sock {
50 /* struct sock must be the first member of struct pfkey_sock */
51 struct sock sk;
52 int registered;
53 int promisc;
Timo Teras83321d62008-03-03 23:40:12 -080054
55 struct {
56 uint8_t msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +000057 uint32_t msg_portid;
Timo Teras83321d62008-03-03 23:40:12 -080058 int (*dump)(struct pfkey_sock *sk);
59 void (*done)(struct pfkey_sock *sk);
60 union {
61 struct xfrm_policy_walk policy;
62 struct xfrm_state_walk state;
63 } u;
Herbert Xu12a169e2008-10-01 07:03:24 -070064 struct sk_buff *skb;
Timo Teras83321d62008-03-03 23:40:12 -080065 } dump;
Yuejie Shi89e357d2017-03-31 15:10:20 +080066 struct mutex dump_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Herbert Xu096f41d2017-04-13 18:35:59 +080069static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
70 xfrm_address_t *saddr, xfrm_address_t *daddr,
71 u16 *family);
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
74{
75 return (struct pfkey_sock *)sk;
76}
77
David S. Miller4c93fbb2011-02-25 18:07:06 -080078static int pfkey_can_dump(const struct sock *sk)
Timo Teras83321d62008-03-03 23:40:12 -080079{
80 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
81 return 1;
82 return 0;
83}
84
Timo Teras05238202008-10-01 05:17:54 -070085static void pfkey_terminate_dump(struct pfkey_sock *pfk)
Timo Teras83321d62008-03-03 23:40:12 -080086{
Timo Teras05238202008-10-01 05:17:54 -070087 if (pfk->dump.dump) {
Herbert Xu12a169e2008-10-01 07:03:24 -070088 if (pfk->dump.skb) {
89 kfree_skb(pfk->dump.skb);
90 pfk->dump.skb = NULL;
91 }
Timo Teras05238202008-10-01 05:17:54 -070092 pfk->dump.done(pfk);
93 pfk->dump.dump = NULL;
94 pfk->dump.done = NULL;
95 }
Timo Teras83321d62008-03-03 23:40:12 -080096}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static void pfkey_sock_destruct(struct sock *sk)
99{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800100 struct net *net = sock_net(sk);
101 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
102
Timo Teras05238202008-10-01 05:17:54 -0700103 pfkey_terminate_dump(pfkey_sk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 skb_queue_purge(&sk->sk_receive_queue);
105
106 if (!sock_flag(sk, SOCK_DEAD)) {
stephen hemminger207024b2010-05-12 06:37:07 +0000107 pr_err("Attempt to release alive pfkey socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return;
109 }
110
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700111 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
Reshetova, Elena14afee42017-06-30 13:08:00 +0300112 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800114 atomic_dec(&net_pfkey->socks_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800117static const struct proto_ops pfkey_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119static void pfkey_insert(struct sock *sk)
120{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800121 struct net *net = sock_net(sk);
122 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
123
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000124 mutex_lock(&pfkey_mutex);
125 sk_add_node_rcu(sk, &net_pfkey->table);
126 mutex_unlock(&pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static void pfkey_remove(struct sock *sk)
130{
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000131 mutex_lock(&pfkey_mutex);
132 sk_del_node_init_rcu(sk);
133 mutex_unlock(&pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static struct proto key_proto = {
137 .name = "KEY",
138 .owner = THIS_MODULE,
139 .obj_size = sizeof(struct pfkey_sock),
140};
141
Eric Paris3f378b62009-11-05 22:18:14 -0800142static int pfkey_create(struct net *net, struct socket *sock, int protocol,
143 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800145 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct sock *sk;
Yuejie Shi89e357d2017-03-31 15:10:20 +0800147 struct pfkey_sock *pfk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int err;
149
Eric W. Biedermandf008c92012-11-16 03:03:07 +0000150 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return -EPERM;
152 if (sock->type != SOCK_RAW)
153 return -ESOCKTNOSUPPORT;
154 if (protocol != PF_KEY_V2)
155 return -EPROTONOSUPPORT;
156
157 err = -ENOMEM;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500158 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (sk == NULL)
160 goto out;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900161
Yuejie Shi89e357d2017-03-31 15:10:20 +0800162 pfk = pfkey_sk(sk);
163 mutex_init(&pfk->dump_lock);
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 sock->ops = &pfkey_ops;
166 sock_init_data(sock, sk);
167
168 sk->sk_family = PF_KEY;
169 sk->sk_destruct = pfkey_sock_destruct;
170
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800171 atomic_inc(&net_pfkey->socks_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 pfkey_insert(sk);
174
175 return 0;
176out:
177 return err;
178}
179
180static int pfkey_release(struct socket *sock)
181{
182 struct sock *sk = sock->sk;
183
184 if (!sk)
185 return 0;
186
187 pfkey_remove(sk);
188
189 sock_orphan(sk);
190 sock->sk = NULL;
191 skb_queue_purge(&sk->sk_write_queue);
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000192
193 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 sock_put(sk);
195
196 return 0;
197}
198
199static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
Al Virodd0fc662005-10-07 07:46:04 +0100200 gfp_t allocation, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 int err = -ENOBUFS;
203
204 sock_hold(sk);
205 if (*skb2 == NULL) {
Reshetova, Elena63354792017-06-30 13:07:58 +0300206 if (refcount_read(&skb->users) != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *skb2 = skb_clone(skb, allocation);
208 } else {
209 *skb2 = skb;
Reshetova, Elena63354792017-06-30 13:07:58 +0300210 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212 }
213 if (*skb2 != NULL) {
214 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 skb_set_owner_r(*skb2, sk);
216 skb_queue_tail(&sk->sk_receive_queue, *skb2);
David S. Miller676d2362014-04-11 16:15:36 -0400217 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 *skb2 = NULL;
219 err = 0;
220 }
221 }
222 sock_put(sk);
223 return err;
224}
225
226/* Send SKB to all pfkey sockets matching selected criteria. */
227#define BROADCAST_ALL 0
228#define BROADCAST_ONE 1
229#define BROADCAST_REGISTERED 2
230#define BROADCAST_PROMISC_ONLY 4
Eric Dumazet36f41f82017-08-14 10:16:45 -0700231static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800232 int broadcast_flags, struct sock *one_sk,
233 struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800235 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 struct sk_buff *skb2 = NULL;
238 int err = -ESRCH;
239
240 /* XXX Do we need something like netlink_overrun? I think
241 * XXX PF_KEY socket apps will not mind current behavior.
242 */
243 if (!skb)
244 return -ENOMEM;
245
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000246 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800247 sk_for_each_rcu(sk, &net_pfkey->table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct pfkey_sock *pfk = pfkey_sk(sk);
249 int err2;
250
251 /* Yes, it means that if you are meant to receive this
252 * pfkey message you receive it twice as promiscuous
253 * socket.
254 */
255 if (pfk->promisc)
David Ahernba51b6b2015-08-24 15:17:17 -0600256 pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* the exact target will be processed later */
259 if (sk == one_sk)
260 continue;
261 if (broadcast_flags != BROADCAST_ALL) {
262 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
263 continue;
264 if ((broadcast_flags & BROADCAST_REGISTERED) &&
265 !pfk->registered)
266 continue;
267 if (broadcast_flags & BROADCAST_ONE)
268 continue;
269 }
270
David Ahernba51b6b2015-08-24 15:17:17 -0600271 err2 = pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Li RongQingf6b8dec92015-10-22 11:35:05 +0800273 /* Error is cleared after successful sending to at least one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * registered KM */
275 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
276 err = err2;
277 }
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000278 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (one_sk != NULL)
Eric Dumazet36f41f82017-08-14 10:16:45 -0700281 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Wei Yongjun6f961062009-02-25 00:31:04 +0000283 kfree_skb(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 kfree_skb(skb);
285 return err;
286}
287
Timo Teras05238202008-10-01 05:17:54 -0700288static int pfkey_do_dump(struct pfkey_sock *pfk)
289{
Herbert Xu12a169e2008-10-01 07:03:24 -0700290 struct sadb_msg *hdr;
Timo Teras05238202008-10-01 05:17:54 -0700291 int rc;
292
Yuejie Shi89e357d2017-03-31 15:10:20 +0800293 mutex_lock(&pfk->dump_lock);
294 if (!pfk->dump.dump) {
295 rc = 0;
296 goto out;
297 }
298
Timo Teras05238202008-10-01 05:17:54 -0700299 rc = pfk->dump.dump(pfk);
Yuejie Shi89e357d2017-03-31 15:10:20 +0800300 if (rc == -ENOBUFS) {
301 rc = 0;
302 goto out;
303 }
Timo Teras05238202008-10-01 05:17:54 -0700304
Herbert Xu12a169e2008-10-01 07:03:24 -0700305 if (pfk->dump.skb) {
Yuejie Shi89e357d2017-03-31 15:10:20 +0800306 if (!pfkey_can_dump(&pfk->sk)) {
307 rc = 0;
308 goto out;
309 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700310
311 hdr = (struct sadb_msg *) pfk->dump.skb->data;
312 hdr->sadb_msg_seq = 0;
313 hdr->sadb_msg_errno = rc;
Eric Dumazet36f41f82017-08-14 10:16:45 -0700314 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800315 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -0700316 pfk->dump.skb = NULL;
317 }
318
Timo Teras05238202008-10-01 05:17:54 -0700319 pfkey_terminate_dump(pfk);
Yuejie Shi89e357d2017-03-31 15:10:20 +0800320
321out:
322 mutex_unlock(&pfk->dump_lock);
Timo Teras05238202008-10-01 05:17:54 -0700323 return rc;
324}
325
David S. Miller4c93fbb2011-02-25 18:07:06 -0800326static inline void pfkey_hdr_dup(struct sadb_msg *new,
327 const struct sadb_msg *orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 *new = *orig;
330}
331
David S. Miller4c93fbb2011-02-25 18:07:06 -0800332static int pfkey_error(const struct sadb_msg *orig, int err, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
335 struct sadb_msg *hdr;
336
337 if (!skb)
338 return -ENOBUFS;
339
340 /* Woe be to the platform trying to support PFKEY yet
341 * having normal errnos outside the 1-255 range, inclusive.
342 */
343 err = -err;
344 if (err == ERESTARTSYS ||
345 err == ERESTARTNOHAND ||
346 err == ERESTARTNOINTR)
347 err = EINTR;
348 if (err >= 512)
349 err = EINVAL;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800350 BUG_ON(err <= 0 || err >= 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Johannes Berg4df864c2017-06-16 14:29:21 +0200352 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 pfkey_hdr_dup(hdr, orig);
354 hdr->sadb_msg_errno = (uint8_t) err;
355 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
356 sizeof(uint64_t));
357
Eric Dumazet36f41f82017-08-14 10:16:45 -0700358 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 return 0;
361}
362
Mathias Krause8603b9552013-06-26 23:56:59 +0200363static const u8 sadb_ext_min_len[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 [SADB_EXT_RESERVED] = (u8) 0,
365 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
366 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
367 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
368 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
369 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
370 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
371 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
372 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
373 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
374 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
375 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
376 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
377 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
378 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
379 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
380 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
381 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
382 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
383 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
384 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
385 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
386 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
387 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
Trent Jaegerdf718372005-12-13 23:12:27 -0800388 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -0700389 [SADB_X_EXT_KMADDRESS] = (u8) sizeof(struct sadb_x_kmaddress),
Nicolas Dichteld2c5f652014-02-20 14:52:41 +0100390 [SADB_X_EXT_FILTER] = (u8) sizeof(struct sadb_x_filter),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391};
392
393/* Verify sadb_address_{len,prefixlen} against sa_family. */
David S. Miller4c93fbb2011-02-25 18:07:06 -0800394static int verify_address_len(const void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800396 const struct sadb_address *sp = p;
397 const struct sockaddr *addr = (const struct sockaddr *)(sp + 1);
398 const struct sockaddr_in *sin;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000399#if IS_ENABLED(CONFIG_IPV6)
David S. Miller4c93fbb2011-02-25 18:07:06 -0800400 const struct sockaddr_in6 *sin6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401#endif
402 int len;
403
Eric Biggers06b335c2017-12-29 18:13:05 -0600404 if (sp->sadb_address_len <
405 DIV_ROUND_UP(sizeof(*sp) + offsetofend(typeof(*addr), sa_family),
406 sizeof(uint64_t)))
407 return -EINVAL;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 switch (addr->sa_family) {
410 case AF_INET:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700411 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (sp->sadb_address_len != len ||
413 sp->sadb_address_prefixlen > 32)
414 return -EINVAL;
415 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000416#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 case AF_INET6:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700418 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (sp->sadb_address_len != len ||
420 sp->sadb_address_prefixlen > 128)
421 return -EINVAL;
422 break;
423#endif
424 default:
425 /* It is user using kernel to keep track of security
426 * associations for another protocol, such as
427 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
428 * lengths.
429 *
430 * XXX Actually, association/policy database is not yet
431 * XXX able to cope with arbitrary sockaddr families.
432 * XXX When it can, remove this -EINVAL. -DaveM
433 */
434 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 return 0;
438}
439
David S. Miller4c93fbb2011-02-25 18:07:06 -0800440static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800441{
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700442 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
443 sec_ctx->sadb_x_ctx_len,
444 sizeof(uint64_t));
Trent Jaegerdf718372005-12-13 23:12:27 -0800445}
446
David S. Miller4c93fbb2011-02-25 18:07:06 -0800447static inline int verify_sec_ctx_len(const void *p)
Trent Jaegerdf718372005-12-13 23:12:27 -0800448{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800449 const struct sadb_x_sec_ctx *sec_ctx = p;
Stephen Rothwell298bb622007-10-30 23:57:05 -0700450 int len = sec_ctx->sadb_x_ctx_len;
Trent Jaegerdf718372005-12-13 23:12:27 -0800451
Stephen Rothwell298bb622007-10-30 23:57:05 -0700452 if (len > PAGE_SIZE)
Trent Jaegerdf718372005-12-13 23:12:27 -0800453 return -EINVAL;
454
455 len = pfkey_sec_ctx_len(sec_ctx);
456
457 if (sec_ctx->sadb_x_sec_len != len)
458 return -EINVAL;
459
460 return 0;
461}
462
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +0100463static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx,
464 gfp_t gfp)
Trent Jaegerdf718372005-12-13 23:12:27 -0800465{
466 struct xfrm_user_sec_ctx *uctx = NULL;
467 int ctx_size = sec_ctx->sadb_x_ctx_len;
468
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +0100469 uctx = kmalloc((sizeof(*uctx)+ctx_size), gfp);
Trent Jaegerdf718372005-12-13 23:12:27 -0800470
471 if (!uctx)
472 return NULL;
473
474 uctx->len = pfkey_sec_ctx_len(sec_ctx);
475 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
476 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
477 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
478 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
479 memcpy(uctx + 1, sec_ctx + 1,
480 uctx->ctx_len);
481
482 return uctx;
483}
484
David S. Miller4c93fbb2011-02-25 18:07:06 -0800485static int present_and_same_family(const struct sadb_address *src,
486 const struct sadb_address *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800488 const struct sockaddr *s_addr, *d_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (!src || !dst)
491 return 0;
492
David S. Miller4c93fbb2011-02-25 18:07:06 -0800493 s_addr = (const struct sockaddr *)(src + 1);
494 d_addr = (const struct sockaddr *)(dst + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (s_addr->sa_family != d_addr->sa_family)
496 return 0;
497 if (s_addr->sa_family != AF_INET
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000498#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 && s_addr->sa_family != AF_INET6
500#endif
501 )
502 return 0;
503
504 return 1;
505}
506
David S. Miller4c93fbb2011-02-25 18:07:06 -0800507static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800509 const char *p = (char *) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 int len = skb->len;
511
512 len -= sizeof(*hdr);
513 p += sizeof(*hdr);
514 while (len > 0) {
David S. Miller4c93fbb2011-02-25 18:07:06 -0800515 const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 uint16_t ext_type;
517 int ext_len;
518
Eric Biggers4e765b42017-12-29 18:15:23 -0600519 if (len < sizeof(*ehdr))
520 return -EINVAL;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 ext_len = ehdr->sadb_ext_len;
523 ext_len *= sizeof(uint64_t);
524 ext_type = ehdr->sadb_ext_type;
525 if (ext_len < sizeof(uint64_t) ||
526 ext_len > len ||
527 ext_type == SADB_EXT_RESERVED)
528 return -EINVAL;
529
530 if (ext_type <= SADB_EXT_MAX) {
531 int min = (int) sadb_ext_min_len[ext_type];
532 if (ext_len < min)
533 return -EINVAL;
534 if (ext_hdrs[ext_type-1] != NULL)
535 return -EINVAL;
536 if (ext_type == SADB_EXT_ADDRESS_SRC ||
537 ext_type == SADB_EXT_ADDRESS_DST ||
538 ext_type == SADB_EXT_ADDRESS_PROXY ||
539 ext_type == SADB_X_EXT_NAT_T_OA) {
540 if (verify_address_len(p))
541 return -EINVAL;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900542 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800543 if (ext_type == SADB_X_EXT_SEC_CTX) {
544 if (verify_sec_ctx_len(p))
545 return -EINVAL;
546 }
David S. Miller4c93fbb2011-02-25 18:07:06 -0800547 ext_hdrs[ext_type-1] = (void *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 p += ext_len;
550 len -= ext_len;
551 }
552
553 return 0;
554}
555
556static uint16_t
557pfkey_satype2proto(uint8_t satype)
558{
559 switch (satype) {
560 case SADB_SATYPE_UNSPEC:
561 return IPSEC_PROTO_ANY;
562 case SADB_SATYPE_AH:
563 return IPPROTO_AH;
564 case SADB_SATYPE_ESP:
565 return IPPROTO_ESP;
566 case SADB_X_SATYPE_IPCOMP:
567 return IPPROTO_COMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 default:
569 return 0;
570 }
571 /* NOTREACHED */
572}
573
574static uint8_t
575pfkey_proto2satype(uint16_t proto)
576{
577 switch (proto) {
578 case IPPROTO_AH:
579 return SADB_SATYPE_AH;
580 case IPPROTO_ESP:
581 return SADB_SATYPE_ESP;
582 case IPPROTO_COMP:
583 return SADB_X_SATYPE_IPCOMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 default:
585 return 0;
586 }
587 /* NOTREACHED */
588}
589
590/* BTW, this scheme means that there is no way with PFKEY2 sockets to
591 * say specifically 'just raw sockets' as we encode them as 255.
592 */
593
594static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
595{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000596 return proto == IPSEC_PROTO_ANY ? 0 : proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
600{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000601 return proto ? proto : IPSEC_PROTO_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900604static inline int pfkey_sockaddr_len(sa_family_t family)
605{
606 switch (family) {
607 case AF_INET:
608 return sizeof(struct sockaddr_in);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000609#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900610 case AF_INET6:
611 return sizeof(struct sockaddr_in6);
612#endif
613 }
614 return 0;
615}
616
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900617static
618int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900620 switch (sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 case AF_INET:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900622 xaddr->a4 =
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900623 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return AF_INET;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000625#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 case AF_INET6:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900627 memcpy(xaddr->a6,
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900628 &((struct sockaddr_in6 *)sa)->sin6_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 sizeof(struct in6_addr));
630 return AF_INET6;
631#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900633 return 0;
634}
635
636static
David S. Miller4c93fbb2011-02-25 18:07:06 -0800637int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900638{
639 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
640 xaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
David S. Miller4c93fbb2011-02-25 18:07:06 -0800643static 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 -0700644{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800645 const struct sadb_sa *sa;
646 const struct sadb_address *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 uint16_t proto;
648 unsigned short family;
649 xfrm_address_t *xaddr;
650
Joe Perchesea110732011-06-13 16:21:26 +0000651 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (sa == NULL)
653 return NULL;
654
655 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
656 if (proto == 0)
657 return NULL;
658
659 /* sadb_address_len should be checked by caller */
Joe Perchesea110732011-06-13 16:21:26 +0000660 addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (addr == NULL)
662 return NULL;
663
David S. Miller4c93fbb2011-02-25 18:07:06 -0800664 family = ((const struct sockaddr *)(addr + 1))->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 switch (family) {
666 case AF_INET:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800667 xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000669#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 case AF_INET6:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800671 xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 break;
673#endif
674 default:
675 xaddr = NULL;
676 }
677
678 if (!xaddr)
679 return NULL;
680
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800681 return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686static int
687pfkey_sockaddr_size(sa_family_t family)
688{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900689 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700692static inline int pfkey_mode_from_xfrm(int mode)
693{
694 switch(mode) {
695 case XFRM_MODE_TRANSPORT:
696 return IPSEC_MODE_TRANSPORT;
697 case XFRM_MODE_TUNNEL:
698 return IPSEC_MODE_TUNNEL;
699 case XFRM_MODE_BEET:
700 return IPSEC_MODE_BEET;
701 default:
702 return -1;
703 }
704}
705
706static inline int pfkey_mode_to_xfrm(int mode)
707{
708 switch(mode) {
709 case IPSEC_MODE_ANY: /*XXX*/
710 case IPSEC_MODE_TRANSPORT:
711 return XFRM_MODE_TRANSPORT;
712 case IPSEC_MODE_TUNNEL:
713 return XFRM_MODE_TUNNEL;
714 case IPSEC_MODE_BEET:
715 return XFRM_MODE_BEET;
716 default:
717 return -1;
718 }
719}
720
David S. Miller183cad12011-02-24 00:28:01 -0500721static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
David S. Miller4c93fbb2011-02-25 18:07:06 -0800722 struct sockaddr *sa,
723 unsigned short family)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900724{
725 switch (family) {
726 case AF_INET:
727 {
728 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
729 sin->sin_family = AF_INET;
730 sin->sin_port = port;
731 sin->sin_addr.s_addr = xaddr->a4;
732 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
733 return 32;
734 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000735#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900736 case AF_INET6:
737 {
738 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
739 sin6->sin6_family = AF_INET6;
740 sin6->sin6_port = port;
741 sin6->sin6_flowinfo = 0;
Jiri Benc15e318b2015-03-29 16:59:24 +0200742 sin6->sin6_addr = xaddr->in6;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900743 sin6->sin6_scope_id = 0;
744 return 128;
745 }
746#endif
747 }
748 return 0;
749}
750
David S. Miller4c93fbb2011-02-25 18:07:06 -0800751static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -0700752 int add_keys, int hsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct sk_buff *skb;
755 struct sadb_msg *hdr;
756 struct sadb_sa *sa;
757 struct sadb_lifetime *lifetime;
758 struct sadb_address *addr;
759 struct sadb_key *key;
760 struct sadb_x_sa2 *sa2;
Trent Jaegerdf718372005-12-13 23:12:27 -0800761 struct sadb_x_sec_ctx *sec_ctx;
762 struct xfrm_sec_ctx *xfrm_ctx;
763 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 int size;
765 int auth_key_size = 0;
766 int encrypt_key_size = 0;
767 int sockaddr_size;
768 struct xfrm_encap_tmpl *natt = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700769 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /* address family check */
772 sockaddr_size = pfkey_sockaddr_size(x->props.family);
773 if (!sockaddr_size)
774 return ERR_PTR(-EINVAL);
775
776 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
777 key(AE), (identity(SD),) (sensitivity)> */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900778 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 sizeof(struct sadb_lifetime) +
780 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
781 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900782 sizeof(struct sadb_address)*2 +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 sockaddr_size*2 +
784 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800785
786 if ((xfrm_ctx = x->security)) {
787 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
788 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
789 }
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /* identity & sensitivity */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000792 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 size += sizeof(struct sadb_address) + sockaddr_size;
794
795 if (add_keys) {
796 if (x->aalg && x->aalg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900797 auth_key_size =
798 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 size += sizeof(struct sadb_key) + auth_key_size;
800 }
801 if (x->ealg && x->ealg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900802 encrypt_key_size =
803 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 size += sizeof(struct sadb_key) + encrypt_key_size;
805 }
806 }
807 if (x->encap)
808 natt = x->encap;
809
810 if (natt && natt->encap_type) {
811 size += sizeof(struct sadb_x_nat_t_type);
812 size += sizeof(struct sadb_x_nat_t_port);
813 size += sizeof(struct sadb_x_nat_t_port);
814 }
815
816 skb = alloc_skb(size + 16, GFP_ATOMIC);
817 if (skb == NULL)
818 return ERR_PTR(-ENOBUFS);
819
820 /* call should fill header later */
Johannes Berg4df864c2017-06-16 14:29:21 +0200821 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 memset(hdr, 0, size); /* XXX do we need this ? */
823 hdr->sadb_msg_len = size / sizeof(uint64_t);
824
825 /* sa */
Johannes Berg4df864c2017-06-16 14:29:21 +0200826 sa = skb_put(skb, sizeof(struct sadb_sa));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
828 sa->sadb_sa_exttype = SADB_EXT_SA;
829 sa->sadb_sa_spi = x->id.spi;
830 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700831 switch (x->km.state) {
832 case XFRM_STATE_VALID:
833 sa->sadb_sa_state = x->km.dying ?
834 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
835 break;
836 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700838 break;
839 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700841 break;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 sa->sadb_sa_auth = 0;
844 if (x->aalg) {
845 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200846 sa->sadb_sa_auth = (a && a->pfkey_supported) ?
847 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849 sa->sadb_sa_encrypt = 0;
850 BUG_ON(x->ealg && x->calg);
851 if (x->ealg) {
852 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200853 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
854 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
857 if (x->calg) {
858 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200859 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
860 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
863 sa->sadb_sa_flags = 0;
864 if (x->props.flags & XFRM_STATE_NOECN)
865 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
866 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
867 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700868 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
869 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 /* hard time */
872 if (hsc & 2) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200873 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 lifetime->sadb_lifetime_len =
875 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
876 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
877 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
878 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
879 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
880 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
881 }
882 /* soft time */
883 if (hsc & 1) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200884 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 lifetime->sadb_lifetime_len =
886 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
887 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
888 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
889 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
890 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
891 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
892 }
893 /* current time */
Johannes Berg4df864c2017-06-16 14:29:21 +0200894 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 lifetime->sadb_lifetime_len =
896 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
897 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
898 lifetime->sadb_lifetime_allocations = x->curlft.packets;
899 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
900 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
901 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
902 /* src address */
Johannes Berg4df864c2017-06-16 14:29:21 +0200903 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900904 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 (sizeof(struct sadb_address)+sockaddr_size)/
906 sizeof(uint64_t);
907 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900908 /* "if the ports are non-zero, then the sadb_address_proto field,
909 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900911 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900914 addr->sadb_address_prefixlen =
915 pfkey_sockaddr_fill(&x->props.saddr, 0,
916 (struct sockaddr *) (addr + 1),
917 x->props.family);
918 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 BUG();
920
921 /* dst address */
Johannes Berg4df864c2017-06-16 14:29:21 +0200922 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900923 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 (sizeof(struct sadb_address)+sockaddr_size)/
925 sizeof(uint64_t);
926 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900927 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900930 addr->sadb_address_prefixlen =
931 pfkey_sockaddr_fill(&x->id.daddr, 0,
932 (struct sockaddr *) (addr + 1),
933 x->props.family);
934 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 BUG();
936
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000937 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
938 x->props.family)) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200939 addr = skb_put(skb,
940 sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900941 addr->sadb_address_len =
942 (sizeof(struct sadb_address)+sockaddr_size)/
943 sizeof(uint64_t);
944 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
945 addr->sadb_address_proto =
946 pfkey_proto_from_xfrm(x->sel.proto);
947 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
948 addr->sadb_address_reserved = 0;
949
950 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
951 (struct sockaddr *) (addr + 1),
952 x->props.family);
953 }
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /* auth key */
956 if (add_keys && auth_key_size) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200957 key = skb_put(skb, sizeof(struct sadb_key) + auth_key_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
959 sizeof(uint64_t);
960 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
961 key->sadb_key_bits = x->aalg->alg_key_len;
962 key->sadb_key_reserved = 0;
963 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
964 }
965 /* encrypt key */
966 if (add_keys && encrypt_key_size) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200967 key = skb_put(skb, sizeof(struct sadb_key) + encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900968 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 encrypt_key_size) / sizeof(uint64_t);
970 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
971 key->sadb_key_bits = x->ealg->alg_key_len;
972 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900973 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 (x->ealg->alg_key_len+7)/8);
975 }
976
977 /* sa */
Johannes Berg4df864c2017-06-16 14:29:21 +0200978 sa2 = skb_put(skb, sizeof(struct sadb_x_sa2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
980 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700981 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
982 kfree_skb(skb);
983 return ERR_PTR(-EINVAL);
984 }
985 sa2->sadb_x_sa2_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 sa2->sadb_x_sa2_reserved1 = 0;
987 sa2->sadb_x_sa2_reserved2 = 0;
988 sa2->sadb_x_sa2_sequence = 0;
989 sa2->sadb_x_sa2_reqid = x->props.reqid;
990
991 if (natt && natt->encap_type) {
992 struct sadb_x_nat_t_type *n_type;
993 struct sadb_x_nat_t_port *n_port;
994
995 /* type */
Johannes Berg4df864c2017-06-16 14:29:21 +0200996 n_type = skb_put(skb, sizeof(*n_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
998 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
999 n_type->sadb_x_nat_t_type_type = natt->encap_type;
1000 n_type->sadb_x_nat_t_type_reserved[0] = 0;
1001 n_type->sadb_x_nat_t_type_reserved[1] = 0;
1002 n_type->sadb_x_nat_t_type_reserved[2] = 0;
1003
1004 /* source port */
Johannes Berg4df864c2017-06-16 14:29:21 +02001005 n_port = skb_put(skb, sizeof(*n_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1007 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
1008 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
1009 n_port->sadb_x_nat_t_port_reserved = 0;
1010
1011 /* dest port */
Johannes Berg4df864c2017-06-16 14:29:21 +02001012 n_port = skb_put(skb, sizeof(*n_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1014 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
1015 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
1016 n_port->sadb_x_nat_t_port_reserved = 0;
1017 }
1018
Trent Jaegerdf718372005-12-13 23:12:27 -08001019 /* security context */
1020 if (xfrm_ctx) {
Johannes Berg4df864c2017-06-16 14:29:21 +02001021 sec_ctx = skb_put(skb,
1022 sizeof(struct sadb_x_sec_ctx) + ctx_size);
Trent Jaegerdf718372005-12-13 23:12:27 -08001023 sec_ctx->sadb_x_sec_len =
1024 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1025 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1026 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1027 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1028 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1029 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1030 xfrm_ctx->ctx_len);
1031 }
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return skb;
1034}
1035
Herbert Xu050f0092007-10-09 13:31:47 -07001036
David S. Miller4c93fbb2011-02-25 18:07:06 -08001037static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
Herbert Xu050f0092007-10-09 13:31:47 -07001038{
1039 struct sk_buff *skb;
1040
Herbert Xu050f0092007-10-09 13:31:47 -07001041 skb = __pfkey_xfrm_state2msg(x, 1, 3);
Herbert Xu050f0092007-10-09 13:31:47 -07001042
1043 return skb;
1044}
1045
David S. Miller4c93fbb2011-02-25 18:07:06 -08001046static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -07001047 int hsc)
1048{
1049 return __pfkey_xfrm_state2msg(x, 0, hsc);
1050}
1051
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001052static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
David S. Miller4c93fbb2011-02-25 18:07:06 -08001053 const struct sadb_msg *hdr,
1054 void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001056 struct xfrm_state *x;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001057 const struct sadb_lifetime *lifetime;
1058 const struct sadb_sa *sa;
1059 const struct sadb_key *key;
1060 const struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 uint16_t proto;
1062 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Joe Perchesea110732011-06-13 16:21:26 +00001065 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (!sa ||
1067 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1068 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1069 return ERR_PTR(-EINVAL);
1070 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1071 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1072 return ERR_PTR(-EINVAL);
1073 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1074 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1075 return ERR_PTR(-EINVAL);
1076 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1077 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1078 return ERR_PTR(-EINVAL);
1079
1080 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1081 if (proto == 0)
1082 return ERR_PTR(-EINVAL);
1083
1084 /* default error is no buffer space */
1085 err = -ENOBUFS;
1086
1087 /* RFC2367:
1088
1089 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1090 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1091 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1092 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1093 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1094 not true.
1095
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001096 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1098 */
1099 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1100 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1101 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1102 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1103 return ERR_PTR(-EINVAL);
Joe Perchesea110732011-06-13 16:21:26 +00001104 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (key != NULL &&
1106 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1107 ((key->sadb_key_bits+7) / 8 == 0 ||
1108 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1109 return ERR_PTR(-EINVAL);
1110 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1111 if (key != NULL &&
1112 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1113 ((key->sadb_key_bits+7) / 8 == 0 ||
1114 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1115 return ERR_PTR(-EINVAL);
1116
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001117 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (x == NULL)
1119 return ERR_PTR(-ENOBUFS);
1120
1121 x->id.proto = proto;
1122 x->id.spi = sa->sadb_sa_spi;
Fan Du33fce602013-09-17 15:14:13 +08001123 x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay,
1124 (sizeof(x->replay.bitmap) * 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1126 x->props.flags |= XFRM_STATE_NOECN;
1127 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1128 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001129 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1130 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Joe Perchesea110732011-06-13 16:21:26 +00001132 lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (lifetime != NULL) {
1134 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1135 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1136 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1137 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1138 }
Joe Perchesea110732011-06-13 16:21:26 +00001139 lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (lifetime != NULL) {
1141 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1142 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1143 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1144 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1145 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001146
Joe Perchesea110732011-06-13 16:21:26 +00001147 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001148 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01001149 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001150
1151 if (!uctx)
1152 goto out;
1153
1154 err = security_xfrm_state_alloc(x, uctx);
1155 kfree(uctx);
1156
1157 if (err)
1158 goto out;
1159 }
1160
Dan Carpentere747f642017-06-14 13:35:37 +03001161 err = -ENOBUFS;
Joe Perchesea110732011-06-13 16:21:26 +00001162 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (sa->sadb_sa_auth) {
1164 int keysize = 0;
1165 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001166 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 err = -ENOSYS;
1168 goto out;
1169 }
1170 if (key)
1171 keysize = (key->sadb_key_bits + 7) / 8;
1172 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001173 if (!x->aalg) {
1174 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 goto out;
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 strcpy(x->aalg->alg_name, a->name);
1178 x->aalg->alg_key_len = 0;
1179 if (key) {
1180 x->aalg->alg_key_len = key->sadb_key_bits;
1181 memcpy(x->aalg->alg_key, key+1, keysize);
1182 }
Martin Willic20a66f2009-12-09 06:11:15 +00001183 x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 x->props.aalgo = sa->sadb_sa_auth;
1185 /* x->algo.flags = sa->sadb_sa_flags; */
1186 }
1187 if (sa->sadb_sa_encrypt) {
1188 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1189 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001190 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 err = -ENOSYS;
1192 goto out;
1193 }
1194 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001195 if (!x->calg) {
1196 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 goto out;
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 strcpy(x->calg->alg_name, a->name);
1200 x->props.calgo = sa->sadb_sa_encrypt;
1201 } else {
1202 int keysize = 0;
1203 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001204 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 err = -ENOSYS;
1206 goto out;
1207 }
1208 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1209 if (key)
1210 keysize = (key->sadb_key_bits + 7) / 8;
1211 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001212 if (!x->ealg) {
1213 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 goto out;
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 strcpy(x->ealg->alg_name, a->name);
1217 x->ealg->alg_key_len = 0;
1218 if (key) {
1219 x->ealg->alg_key_len = key->sadb_key_bits;
1220 memcpy(x->ealg->alg_key, key+1, keysize);
1221 }
1222 x->props.ealgo = sa->sadb_sa_encrypt;
Herbert Xu69b01372015-05-27 16:03:45 +08001223 x->geniv = a->uinfo.encr.geniv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225 }
1226 /* x->algo.flags = sa->sadb_sa_flags; */
1227
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001228 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 &x->props.saddr);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001230 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 &x->id.daddr);
1232
1233 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001234 const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001235 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1236 if (mode < 0) {
1237 err = -EINVAL;
1238 goto out;
1239 }
1240 x->props.mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 x->props.reqid = sa2->sadb_x_sa2_reqid;
1242 }
1243
1244 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001245 const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 /* Nobody uses this, but we try. */
1248 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1249 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1250 }
1251
Kazunori MIYAZAWA4da51052008-05-21 13:26:11 -07001252 if (!x->sel.family)
Joy Latten4a4b6272007-08-02 19:25:43 -07001253 x->sel.family = x->props.family;
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001256 const struct sadb_x_nat_t_type* n_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 struct xfrm_encap_tmpl *natt;
1258
1259 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001260 if (!x->encap) {
1261 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 goto out;
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 natt = x->encap;
1266 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1267 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1268
1269 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001270 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1272 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1273 }
1274 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001275 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1277 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1278 }
Timo Terasa8d694c2009-01-25 20:49:14 -08001279 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281
Herbert Xu72cb6962005-06-20 13:18:08 -07001282 err = xfrm_init_state(x);
1283 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return x;
1288
1289out:
1290 x->km.state = XFRM_STATE_DEAD;
1291 xfrm_state_put(x);
1292 return ERR_PTR(err);
1293}
1294
David S. Miller4c93fbb2011-02-25 18:07:06 -08001295static 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 -07001296{
1297 return -EOPNOTSUPP;
1298}
1299
David S. Miller4c93fbb2011-02-25 18:07:06 -08001300static 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 -07001301{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001302 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 struct sk_buff *resp_skb;
1304 struct sadb_x_sa2 *sa2;
1305 struct sadb_address *saddr, *daddr;
1306 struct sadb_msg *out_hdr;
Herbert Xu658b2192007-10-09 13:29:52 -07001307 struct sadb_spirange *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 struct xfrm_state *x = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001309 int mode;
Herbert Xu658b2192007-10-09 13:29:52 -07001310 int err;
1311 u32 min_spi, max_spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 u32 reqid;
1313 u8 proto;
1314 unsigned short family;
1315 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1316
1317 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1318 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1319 return -EINVAL;
1320
1321 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1322 if (proto == 0)
1323 return -EINVAL;
1324
1325 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001326 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1327 if (mode < 0)
1328 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 reqid = sa2->sadb_x_sa2_reqid;
1330 } else {
1331 mode = 0;
1332 reqid = 0;
1333 }
1334
1335 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1336 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1337
1338 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1339 switch (family) {
1340 case AF_INET:
1341 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1342 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1343 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001344#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 case AF_INET6:
1346 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1347 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1348 break;
1349#endif
1350 }
1351
1352 if (hdr->sadb_msg_seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001353 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001354 if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 xfrm_state_put(x);
1356 x = NULL;
1357 }
1358 }
1359
1360 if (!x)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001361 x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 if (x == NULL)
1364 return -ENOENT;
1365
Herbert Xu658b2192007-10-09 13:29:52 -07001366 min_spi = 0x100;
1367 max_spi = 0x0fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Herbert Xu658b2192007-10-09 13:29:52 -07001369 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1370 if (range) {
1371 min_spi = range->sadb_spirange_min;
1372 max_spi = range->sadb_spirange_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Herbert Xu658b2192007-10-09 13:29:52 -07001374
Fan Du776e9dd2013-12-16 18:47:49 +08001375 err = verify_spi_info(x->id.proto, min_spi, max_spi);
1376 if (err) {
1377 xfrm_state_put(x);
1378 return err;
1379 }
1380
Herbert Xu658b2192007-10-09 13:29:52 -07001381 err = xfrm_alloc_spi(x, min_spi, max_spi);
Herbert Xu050f0092007-10-09 13:31:47 -07001382 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 if (IS_ERR(resp_skb)) {
1385 xfrm_state_put(x);
1386 return PTR_ERR(resp_skb);
1387 }
1388
1389 out_hdr = (struct sadb_msg *) resp_skb->data;
1390 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1391 out_hdr->sadb_msg_type = SADB_GETSPI;
1392 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1393 out_hdr->sadb_msg_errno = 0;
1394 out_hdr->sadb_msg_reserved = 0;
1395 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1396 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1397
1398 xfrm_state_put(x);
1399
Eric Dumazet36f41f82017-08-14 10:16:45 -07001400 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 return 0;
1403}
1404
David S. Miller4c93fbb2011-02-25 18:07:06 -08001405static 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 -07001406{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001407 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 struct xfrm_state *x;
1409
1410 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1411 return -EOPNOTSUPP;
1412
1413 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1414 return 0;
1415
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001416 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (x == NULL)
1418 return 0;
1419
1420 spin_lock_bh(&x->lock);
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001421 if (x->km.state == XFRM_STATE_ACQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 x->km.state = XFRM_STATE_ERROR;
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 spin_unlock_bh(&x->lock);
1425 xfrm_state_put(x);
1426 return 0;
1427}
1428
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001429static inline int event2poltype(int event)
1430{
1431 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001432 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001433 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001434 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001435 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001436 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001437 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001438 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001439 // return SADB_X_SPDEXPIRE;
1440 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001441 pr_err("pfkey: Unknown policy event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001442 break;
1443 }
1444
1445 return 0;
1446}
1447
1448static inline int event2keytype(int event)
1449{
1450 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001451 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001452 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001453 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001454 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001455 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001456 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001457 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001458 return SADB_EXPIRE;
1459 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001460 pr_err("pfkey: Unknown SA event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001461 break;
1462 }
1463
1464 return 0;
1465}
1466
1467/* ADD/UPD/DEL */
David S. Miller214e0052011-02-24 00:02:38 -05001468static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001469{
1470 struct sk_buff *skb;
1471 struct sadb_msg *hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001472
Herbert Xu050f0092007-10-09 13:31:47 -07001473 skb = pfkey_xfrm_state2msg(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001474
1475 if (IS_ERR(skb))
1476 return PTR_ERR(skb);
1477
1478 hdr = (struct sadb_msg *) skb->data;
1479 hdr->sadb_msg_version = PF_KEY_V2;
1480 hdr->sadb_msg_type = event2keytype(c->event);
1481 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1482 hdr->sadb_msg_errno = 0;
1483 hdr->sadb_msg_reserved = 0;
1484 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001485 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001486
Eric Dumazet36f41f82017-08-14 10:16:45 -07001487 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001488
1489 return 0;
1490}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
David S. Miller4c93fbb2011-02-25 18:07:06 -08001492static 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 -07001493{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001494 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 struct xfrm_state *x;
1496 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001497 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001499 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (IS_ERR(x))
1501 return PTR_ERR(x);
1502
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001503 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (hdr->sadb_msg_type == SADB_ADD)
1505 err = xfrm_state_add(x);
1506 else
1507 err = xfrm_state_update(x);
1508
Tetsuo Handa2e710292014-04-22 21:48:30 +09001509 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (err < 0) {
1512 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001513 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001514 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
1516
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001517 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001518 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001519 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001520 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001521 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001522 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001523 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001524out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001525 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001526 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
David S. Miller4c93fbb2011-02-25 18:07:06 -08001529static 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 -07001530{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001531 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001533 struct km_event c;
1534 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 if (!ext_hdrs[SADB_EXT_SA-1] ||
1537 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1538 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1539 return -EINVAL;
1540
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001541 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (x == NULL)
1543 return -ESRCH;
1544
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001545 if ((err = security_xfrm_state_delete(x)))
1546 goto out;
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001549 err = -EPERM;
1550 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
Joy Latten161a09e2006-11-27 13:11:54 -06001552
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001553 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001554
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001555 if (err < 0)
1556 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001557
1558 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001559 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001560 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001561 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001562out:
Tetsuo Handa2e710292014-04-22 21:48:30 +09001563 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 xfrm_state_put(x);
1565
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001566 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
David S. Miller4c93fbb2011-02-25 18:07:06 -08001569static 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 -07001570{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001571 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 __u8 proto;
1573 struct sk_buff *out_skb;
1574 struct sadb_msg *out_hdr;
1575 struct xfrm_state *x;
1576
1577 if (!ext_hdrs[SADB_EXT_SA-1] ||
1578 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1579 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1580 return -EINVAL;
1581
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001582 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (x == NULL)
1584 return -ESRCH;
1585
Herbert Xu050f0092007-10-09 13:31:47 -07001586 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 proto = x->id.proto;
1588 xfrm_state_put(x);
1589 if (IS_ERR(out_skb))
1590 return PTR_ERR(out_skb);
1591
1592 out_hdr = (struct sadb_msg *) out_skb->data;
1593 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Charles Hardin435000b2007-11-22 19:35:15 +08001594 out_hdr->sadb_msg_type = SADB_GET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1596 out_hdr->sadb_msg_errno = 0;
1597 out_hdr->sadb_msg_reserved = 0;
1598 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1599 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Eric Dumazet36f41f82017-08-14 10:16:45 -07001600 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 return 0;
1603}
1604
David S. Miller4c93fbb2011-02-25 18:07:06 -08001605static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001606 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 struct sk_buff *skb;
1609 struct sadb_msg *hdr;
1610 int len, auth_len, enc_len, i;
1611
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001612 auth_len = xfrm_count_pfkey_auth_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (auth_len) {
1614 auth_len *= sizeof(struct sadb_alg);
1615 auth_len += sizeof(struct sadb_supported);
1616 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001617
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001618 enc_len = xfrm_count_pfkey_enc_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (enc_len) {
1620 enc_len *= sizeof(struct sadb_alg);
1621 enc_len += sizeof(struct sadb_supported);
1622 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 len = enc_len + auth_len + sizeof(struct sadb_msg);
1625
1626 skb = alloc_skb(len + 16, allocation);
1627 if (!skb)
1628 goto out_put_algs;
1629
Johannes Berg4df864c2017-06-16 14:29:21 +02001630 hdr = skb_put(skb, sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 pfkey_hdr_dup(hdr, orig);
1632 hdr->sadb_msg_errno = 0;
1633 hdr->sadb_msg_len = len / sizeof(uint64_t);
1634
1635 if (auth_len) {
1636 struct sadb_supported *sp;
1637 struct sadb_alg *ap;
1638
Johannes Berg4df864c2017-06-16 14:29:21 +02001639 sp = skb_put(skb, auth_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 ap = (struct sadb_alg *) (sp + 1);
1641
1642 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1643 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1644
1645 for (i = 0; ; i++) {
1646 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1647 if (!aalg)
1648 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001649 if (!aalg->pfkey_supported)
1650 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (aalg->available)
1652 *ap++ = aalg->desc;
1653 }
1654 }
1655
1656 if (enc_len) {
1657 struct sadb_supported *sp;
1658 struct sadb_alg *ap;
1659
Johannes Berg4df864c2017-06-16 14:29:21 +02001660 sp = skb_put(skb, enc_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 ap = (struct sadb_alg *) (sp + 1);
1662
1663 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1664 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1665
1666 for (i = 0; ; i++) {
1667 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1668 if (!ealg)
1669 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001670 if (!ealg->pfkey_supported)
1671 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (ealg->available)
1673 *ap++ = ealg->desc;
1674 }
1675 }
1676
1677out_put_algs:
1678 return skb;
1679}
1680
David S. Miller4c93fbb2011-02-25 18:07:06 -08001681static 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 -07001682{
1683 struct pfkey_sock *pfk = pfkey_sk(sk);
1684 struct sk_buff *supp_skb;
1685
1686 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1687 return -EINVAL;
1688
1689 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1690 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1691 return -EEXIST;
1692 pfk->registered |= (1<<hdr->sadb_msg_satype);
1693 }
1694
1695 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1698 if (!supp_skb) {
1699 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1700 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1701
1702 return -ENOBUFS;
1703 }
1704
Eric Dumazet36f41f82017-08-14 10:16:45 -07001705 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk,
1706 sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return 0;
1708}
1709
David S. Miller4c93fbb2011-02-25 18:07:06 -08001710static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001711{
1712 struct sk_buff *skb;
1713 struct sadb_msg *hdr;
1714
1715 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1716 if (!skb)
1717 return -ENOBUFS;
1718
Johannes Berg59ae1d12017-06-16 14:29:20 +02001719 hdr = skb_put_data(skb, ihdr, sizeof(struct sadb_msg));
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001720 hdr->sadb_msg_errno = (uint8_t) 0;
1721 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1722
Eric Dumazet36f41f82017-08-14 10:16:45 -07001723 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ONE, sk,
1724 sock_net(sk));
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001725}
1726
David S. Miller214e0052011-02-24 00:02:38 -05001727static int key_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001728{
1729 struct sk_buff *skb;
1730 struct sadb_msg *hdr;
1731
1732 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1733 if (!skb)
1734 return -ENOBUFS;
Johannes Berg4df864c2017-06-16 14:29:21 +02001735 hdr = skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf08867f92005-06-18 22:44:00 -07001736 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001737 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001738 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001739 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001740 hdr->sadb_msg_version = PF_KEY_V2;
1741 hdr->sadb_msg_errno = (uint8_t) 0;
1742 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02001743 hdr->sadb_msg_reserved = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001744
Eric Dumazet36f41f82017-08-14 10:16:45 -07001745 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001746
1747 return 0;
1748}
1749
David S. Miller4c93fbb2011-02-25 18:07:06 -08001750static 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 -07001751{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001752 struct net *net = sock_net(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +00001753 unsigned int proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001754 struct km_event c;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001755 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1758 if (proto == 0)
1759 return -EINVAL;
1760
Tetsuo Handa2e710292014-04-22 21:48:30 +09001761 err = xfrm_state_flush(net, proto, true);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001762 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001763 if (err || err2) {
1764 if (err == -ESRCH) /* empty table - go quietly */
1765 err = 0;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001766 return err ? err : err2;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001767 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001768
Herbert Xubf08867f92005-06-18 22:44:00 -07001769 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001770 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001771 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001772 c.event = XFRM_MSG_FLUSHSA;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001773 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001774 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 return 0;
1777}
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1780{
Timo Teras83321d62008-03-03 23:40:12 -08001781 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 struct sk_buff *out_skb;
1783 struct sadb_msg *out_hdr;
1784
Timo Teras83321d62008-03-03 23:40:12 -08001785 if (!pfkey_can_dump(&pfk->sk))
1786 return -ENOBUFS;
1787
Herbert Xu050f0092007-10-09 13:31:47 -07001788 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (IS_ERR(out_skb))
1790 return PTR_ERR(out_skb);
1791
1792 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08001793 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 out_hdr->sadb_msg_type = SADB_DUMP;
1795 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1796 out_hdr->sadb_msg_errno = 0;
1797 out_hdr->sadb_msg_reserved = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07001798 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001799 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07001800
1801 if (pfk->dump.skb)
Eric Dumazet36f41f82017-08-14 10:16:45 -07001802 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001803 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07001804 pfk->dump.skb = out_skb;
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return 0;
1807}
1808
Timo Teras83321d62008-03-03 23:40:12 -08001809static int pfkey_dump_sa(struct pfkey_sock *pfk)
1810{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001811 struct net *net = sock_net(&pfk->sk);
1812 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08001813}
1814
1815static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1816{
Fan Du283bc9f2013-11-07 17:47:50 +08001817 struct net *net = sock_net(&pfk->sk);
1818
1819 xfrm_state_walk_done(&pfk->dump.u.state, net);
Timo Teras83321d62008-03-03 23:40:12 -08001820}
1821
David S. Miller4c93fbb2011-02-25 18:07:06 -08001822static 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 -07001823{
1824 u8 proto;
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001825 struct xfrm_address_filter *filter = NULL;
Timo Teras83321d62008-03-03 23:40:12 -08001826 struct pfkey_sock *pfk = pfkey_sk(sk);
1827
Yuejie Shi89e357d2017-03-31 15:10:20 +08001828 mutex_lock(&pfk->dump_lock);
1829 if (pfk->dump.dump != NULL) {
1830 mutex_unlock(&pfk->dump_lock);
Timo Teras83321d62008-03-03 23:40:12 -08001831 return -EBUSY;
Yuejie Shi89e357d2017-03-31 15:10:20 +08001832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
Yuejie Shi89e357d2017-03-31 15:10:20 +08001835 if (proto == 0) {
1836 mutex_unlock(&pfk->dump_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return -EINVAL;
Yuejie Shi89e357d2017-03-31 15:10:20 +08001838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Nicolas Dichteld3623092014-02-14 15:30:36 +01001840 if (ext_hdrs[SADB_X_EXT_FILTER - 1]) {
1841 struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1];
1842
1843 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
Yuejie Shi89e357d2017-03-31 15:10:20 +08001844 if (filter == NULL) {
1845 mutex_unlock(&pfk->dump_lock);
Nicolas Dichteld3623092014-02-14 15:30:36 +01001846 return -ENOMEM;
Yuejie Shi89e357d2017-03-31 15:10:20 +08001847 }
Nicolas Dichteld3623092014-02-14 15:30:36 +01001848
1849 memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr,
1850 sizeof(xfrm_address_t));
1851 memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr,
1852 sizeof(xfrm_address_t));
1853 filter->family = xfilter->sadb_x_filter_family;
1854 filter->splen = xfilter->sadb_x_filter_splen;
1855 filter->dplen = xfilter->sadb_x_filter_dplen;
1856 }
1857
Timo Teras83321d62008-03-03 23:40:12 -08001858 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001859 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08001860 pfk->dump.dump = pfkey_dump_sa;
1861 pfk->dump.done = pfkey_dump_sa_done;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001862 xfrm_state_walk_init(&pfk->dump.u.state, proto, filter);
Yuejie Shi89e357d2017-03-31 15:10:20 +08001863 mutex_unlock(&pfk->dump_lock);
Timo Teras4c563f72008-02-28 21:31:08 -08001864
Timo Teras83321d62008-03-03 23:40:12 -08001865 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
David S. Miller4c93fbb2011-02-25 18:07:06 -08001868static 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 -07001869{
1870 struct pfkey_sock *pfk = pfkey_sk(sk);
1871 int satype = hdr->sadb_msg_satype;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001872 bool reset_errno = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001875 reset_errno = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (satype != 0 && satype != 1)
1877 return -EINVAL;
1878 pfk->promisc = satype;
1879 }
David S. Miller4c93fbb2011-02-25 18:07:06 -08001880 if (reset_errno && skb_cloned(skb))
1881 skb = skb_copy(skb, GFP_KERNEL);
1882 else
1883 skb = skb_clone(skb, GFP_KERNEL);
1884
1885 if (reset_errno && skb) {
1886 struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
1887 new_hdr->sadb_msg_errno = 0;
1888 }
1889
Eric Dumazet36f41f82017-08-14 10:16:45 -07001890 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return 0;
1892}
1893
1894static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1895{
1896 int i;
1897 u32 reqid = *(u32*)ptr;
1898
1899 for (i=0; i<xp->xfrm_nr; i++) {
1900 if (xp->xfrm_vec[i].reqid == reqid)
1901 return -EEXIST;
1902 }
1903 return 0;
1904}
1905
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001906static u32 gen_reqid(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Timo Teras4c563f72008-02-28 21:31:08 -08001908 struct xfrm_policy_walk walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 u32 start;
Timo Teras4c563f72008-02-28 21:31:08 -08001910 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1912
1913 start = reqid;
1914 do {
1915 ++reqid;
1916 if (reqid == 0)
1917 reqid = IPSEC_MANUAL_REQID_MAX+1;
Timo Teras4c563f72008-02-28 21:31:08 -08001918 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001919 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
Fan Du283bc9f2013-11-07 17:47:50 +08001920 xfrm_policy_walk_done(&walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001921 if (rc != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return reqid;
1923 } while (reqid != start);
1924 return 0;
1925}
1926
1927static int
1928parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1929{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001930 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001932 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1935 return -ELOOP;
1936
1937 if (rq->sadb_x_ipsecrequest_mode == 0)
1938 return -EINVAL;
1939
1940 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001941 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1942 return -EINVAL;
1943 t->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1945 t->optional = 1;
1946 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1947 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1948 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1949 t->reqid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001950 if (!t->reqid && !(t->reqid = gen_reqid(net)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return -ENOBUFS;
1952 }
1953
1954 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001955 if (t->mode == XFRM_MODE_TUNNEL) {
Herbert Xu096f41d2017-04-13 18:35:59 +08001956 int err;
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001957
Herbert Xu096f41d2017-04-13 18:35:59 +08001958 err = parse_sockaddr_pair(
1959 (struct sockaddr *)(rq + 1),
1960 rq->sadb_x_ipsecrequest_len - sizeof(*rq),
1961 &t->saddr, &t->id.daddr, &t->encap_family);
1962 if (err)
1963 return err;
Miika Komu2718aa72006-11-30 16:41:50 -08001964 } else
1965 t->encap_family = xp->family;
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 /* No way to set this via kame pfkey */
Herbert Xuc5d18e92008-04-22 00:46:42 -07001968 t->allalgs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 xp->xfrm_nr++;
1970 return 0;
1971}
1972
1973static int
1974parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1975{
1976 int err;
1977 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1978 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1979
Dan Carpenterf674e722012-09-27 22:21:19 +00001980 if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
1981 return -EINVAL;
1982
Herbert Xu096f41d2017-04-13 18:35:59 +08001983 while (len >= sizeof(*rq)) {
1984 if (len < rq->sadb_x_ipsecrequest_len ||
1985 rq->sadb_x_ipsecrequest_len < sizeof(*rq))
1986 return -EINVAL;
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1989 return err;
1990 len -= rq->sadb_x_ipsecrequest_len;
1991 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1992 }
1993 return 0;
1994}
1995
David S. Miller4c93fbb2011-02-25 18:07:06 -08001996static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
Trent Jaegerdf718372005-12-13 23:12:27 -08001997{
1998 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1999
2000 if (xfrm_ctx) {
2001 int len = sizeof(struct sadb_x_sec_ctx);
2002 len += xfrm_ctx->ctx_len;
2003 return PFKEY_ALIGN8(len);
2004 }
2005 return 0;
2006}
2007
David S. Miller4c93fbb2011-02-25 18:07:06 -08002008static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
David S. Miller4c93fbb2011-02-25 18:07:06 -08002010 const struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08002012 int socklen = 0;
2013 int i;
2014
2015 for (i=0; i<xp->xfrm_nr; i++) {
2016 t = xp->xfrm_vec + i;
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002017 socklen += pfkey_sockaddr_len(t->encap_family);
Miika Komu2718aa72006-11-30 16:41:50 -08002018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 return sizeof(struct sadb_msg) +
2021 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002022 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 (sockaddr_size * 2) +
2024 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08002025 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
2026 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08002027 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
David S. Miller4c93fbb2011-02-25 18:07:06 -08002030static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 struct sk_buff *skb;
2033 int size;
2034
2035 size = pfkey_xfrm_policy2msg_size(xp);
2036
2037 skb = alloc_skb(size + 16, GFP_ATOMIC);
2038 if (skb == NULL)
2039 return ERR_PTR(-ENOBUFS);
2040
2041 return skb;
2042}
2043
David S. Miller4c93fbb2011-02-25 18:07:06 -08002044static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 struct sadb_msg *hdr;
2047 struct sadb_address *addr;
2048 struct sadb_lifetime *lifetime;
2049 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08002050 struct sadb_x_sec_ctx *sec_ctx;
2051 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int i;
2053 int size;
2054 int sockaddr_size = pfkey_sockaddr_size(xp->family);
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002055 int socklen = pfkey_sockaddr_len(xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 size = pfkey_xfrm_policy2msg_size(xp);
2058
2059 /* call should fill header later */
Johannes Berg4df864c2017-06-16 14:29:21 +02002060 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 memset(hdr, 0, size); /* XXX do we need this ? */
2062
2063 /* src address */
Johannes Berg4df864c2017-06-16 14:29:21 +02002064 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002065 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 (sizeof(struct sadb_address)+sockaddr_size)/
2067 sizeof(uint64_t);
2068 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2069 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2070 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2071 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002072 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2073 xp->selector.sport,
2074 (struct sockaddr *) (addr + 1),
2075 xp->family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 BUG();
2077
2078 /* dst address */
Johannes Berg4df864c2017-06-16 14:29:21 +02002079 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 addr->sadb_address_len =
2081 (sizeof(struct sadb_address)+sockaddr_size)/
2082 sizeof(uint64_t);
2083 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2084 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002085 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002087
2088 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2089 (struct sockaddr *) (addr + 1),
2090 xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 /* hard time */
Johannes Berg4df864c2017-06-16 14:29:21 +02002093 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 lifetime->sadb_lifetime_len =
2095 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2096 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2097 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2098 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2099 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2100 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2101 /* soft time */
Johannes Berg4df864c2017-06-16 14:29:21 +02002102 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 lifetime->sadb_lifetime_len =
2104 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2105 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2106 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2107 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2108 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2109 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2110 /* current time */
Johannes Berg4df864c2017-06-16 14:29:21 +02002111 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 lifetime->sadb_lifetime_len =
2113 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2114 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2115 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2116 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2117 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2118 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2119
Johannes Berg4df864c2017-06-16 14:29:21 +02002120 pol = skb_put(skb, sizeof(struct sadb_x_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2122 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2123 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2124 if (xp->action == XFRM_POLICY_ALLOW) {
2125 if (xp->xfrm_nr)
2126 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2127 else
2128 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2129 }
2130 pol->sadb_x_policy_dir = dir+1;
Dan Carpenterff862a42013-07-28 23:04:45 +03002131 pol->sadb_x_policy_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 pol->sadb_x_policy_id = xp->index;
2133 pol->sadb_x_policy_priority = xp->priority;
2134
2135 for (i=0; i<xp->xfrm_nr; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002136 const struct xfrm_tmpl *t = xp->xfrm_vec + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 struct sadb_x_ipsecrequest *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 int req_size;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002139 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 req_size = sizeof(struct sadb_x_ipsecrequest);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002142 if (t->mode == XFRM_MODE_TUNNEL) {
2143 socklen = pfkey_sockaddr_len(t->encap_family);
2144 req_size += socklen * 2;
2145 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 size -= 2*socklen;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002147 }
Johannes Berg4df864c2017-06-16 14:29:21 +02002148 rq = skb_put(skb, req_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 pol->sadb_x_policy_len += req_size/8;
2150 memset(rq, 0, sizeof(*rq));
2151 rq->sadb_x_ipsecrequest_len = req_size;
2152 rq->sadb_x_ipsecrequest_proto = t->id.proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002153 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2154 return -EINVAL;
David S. Millerfefaa752007-04-17 21:48:10 -07002155 rq->sadb_x_ipsecrequest_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2157 if (t->reqid)
2158 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2159 if (t->optional)
2160 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2161 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002163 if (t->mode == XFRM_MODE_TUNNEL) {
2164 u8 *sa = (void *)(rq + 1);
2165 pfkey_sockaddr_fill(&t->saddr, 0,
2166 (struct sockaddr *)sa,
2167 t->encap_family);
2168 pfkey_sockaddr_fill(&t->id.daddr, 0,
2169 (struct sockaddr *) (sa + socklen),
2170 t->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
2172 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002173
2174 /* security context */
2175 if ((xfrm_ctx = xp->security)) {
2176 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2177
Johannes Berg4df864c2017-06-16 14:29:21 +02002178 sec_ctx = skb_put(skb, ctx_size);
Trent Jaegerdf718372005-12-13 23:12:27 -08002179 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2180 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2181 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2182 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2183 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2184 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2185 xfrm_ctx->ctx_len);
2186 }
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 hdr->sadb_msg_len = size / sizeof(uint64_t);
Reshetova, Elena850a6212017-07-04 15:53:22 +03002189 hdr->sadb_msg_reserved = refcount_read(&xp->refcnt);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002190
2191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
David S. Miller214e0052011-02-24 00:02:38 -05002194static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002195{
2196 struct sk_buff *out_skb;
2197 struct sadb_msg *out_hdr;
2198 int err;
2199
2200 out_skb = pfkey_xfrm_policy2msg_prep(xp);
Dan Carpenter9a127aa2010-03-24 01:47:00 +00002201 if (IS_ERR(out_skb))
2202 return PTR_ERR(out_skb);
2203
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002204 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2205 if (err < 0)
2206 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002207
2208 out_hdr = (struct sadb_msg *) out_skb->data;
2209 out_hdr->sadb_msg_version = PF_KEY_V2;
2210
Herbert Xuf60f6b82005-06-18 22:44:37 -07002211 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002212 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2213 else
2214 out_hdr->sadb_msg_type = event2poltype(c->event);
2215 out_hdr->sadb_msg_errno = 0;
2216 out_hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002217 out_hdr->sadb_msg_pid = c->portid;
Eric Dumazet36f41f82017-08-14 10:16:45 -07002218 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002219 return 0;
2220
2221}
2222
David S. Miller4c93fbb2011-02-25 18:07:06 -08002223static 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 -07002224{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002225 struct net *net = sock_net(sk);
Trent Jaegerdf718372005-12-13 23:12:27 -08002226 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 struct sadb_lifetime *lifetime;
2228 struct sadb_address *sa;
2229 struct sadb_x_policy *pol;
2230 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002231 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002232 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2235 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2236 !ext_hdrs[SADB_X_EXT_POLICY-1])
2237 return -EINVAL;
2238
2239 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2240 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2241 return -EINVAL;
2242 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2243 return -EINVAL;
2244
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002245 xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (xp == NULL)
2247 return -ENOBUFS;
2248
2249 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2250 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2251 xp->priority = pol->sadb_x_policy_priority;
2252
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002253 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 xp->selector.family = xp->family;
2256 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2257 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2258 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2259 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002260 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002262 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2264 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2265
2266 /* Amusing, we set this twice. KAME apps appear to set same value
2267 * in both addresses.
2268 */
2269 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2270
2271 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2272 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002273 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Joe Perchesea110732011-06-13 16:21:26 +00002275 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002276 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01002277 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002278
2279 if (!uctx) {
2280 err = -ENOBUFS;
2281 goto out;
2282 }
2283
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002284 err = security_xfrm_policy_alloc(&xp->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002285 kfree(uctx);
2286
2287 if (err)
2288 goto out;
2289 }
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 xp->lft.soft_byte_limit = XFRM_INF;
2292 xp->lft.hard_byte_limit = XFRM_INF;
2293 xp->lft.soft_packet_limit = XFRM_INF;
2294 xp->lft.hard_packet_limit = XFRM_INF;
2295 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2296 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2297 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2298 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2299 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2300 }
2301 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2302 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2303 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2304 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2305 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2306 }
2307 xp->xfrm_nr = 0;
2308 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2309 (err = parse_ipsecrequests(xp, pol)) < 0)
2310 goto out;
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2313 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002314
Tetsuo Handa2e710292014-04-22 21:48:30 +09002315 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002316
Trent Jaegerdf718372005-12-13 23:12:27 -08002317 if (err)
2318 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002320 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002321 c.event = XFRM_MSG_UPDPOLICY;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002322 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07002323 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002325 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002326 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002327
2328 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return 0;
2331
2332out:
Herbert Xu12a169e2008-10-01 07:03:24 -07002333 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08002334 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return err;
2336}
2337
David S. Miller4c93fbb2011-02-25 18:07:06 -08002338static 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 -07002339{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002340 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 int err;
2342 struct sadb_address *sa;
2343 struct sadb_x_policy *pol;
Paul Moore03e1ad72008-04-12 19:07:52 -07002344 struct xfrm_policy *xp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002346 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002347 struct sadb_x_sec_ctx *sec_ctx;
Brian Haley2db3e472008-04-24 20:38:31 -07002348 struct xfrm_sec_ctx *pol_ctx = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2351 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2352 !ext_hdrs[SADB_X_EXT_POLICY-1])
2353 return -EINVAL;
2354
2355 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2356 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2357 return -EINVAL;
2358
2359 memset(&sel, 0, sizeof(sel));
2360
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002361 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2363 sel.prefixlen_s = sa->sadb_address_prefixlen;
2364 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2365 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2366 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002367 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002369 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2371 sel.prefixlen_d = sa->sadb_address_prefixlen;
2372 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2373 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2374 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002375 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Joe Perchesea110732011-06-13 16:21:26 +00002377 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002378 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01002379 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002380
2381 if (!uctx)
2382 return -ENOMEM;
2383
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002384 err = security_xfrm_policy_alloc(&pol_ctx, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002385 kfree(uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002386 if (err)
2387 return err;
Brian Haley2db3e472008-04-24 20:38:31 -07002388 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002389
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002390 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Paul Moore03e1ad72008-04-12 19:07:52 -07002391 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2392 1, &err);
2393 security_xfrm_policy_free(pol_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (xp == NULL)
2395 return -ENOENT;
2396
Tetsuo Handa2e710292014-04-22 21:48:30 +09002397 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08002398
2399 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002400 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08002401
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002402 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002403 c.portid = hdr->sadb_msg_pid;
Tobias Brunner1839faa2008-10-10 14:07:03 -07002404 c.data.byid = 0;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002405 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002406 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2407
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002408out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002409 xfrm_pol_put(xp);
2410 return err;
2411}
2412
David S. Miller4c93fbb2011-02-25 18:07:06 -08002413static 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 -07002414{
2415 int err;
2416 struct sk_buff *out_skb;
2417 struct sadb_msg *out_hdr;
2418 err = 0;
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2421 if (IS_ERR(out_skb)) {
2422 err = PTR_ERR(out_skb);
2423 goto out;
2424 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002425 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2426 if (err < 0)
2427 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429 out_hdr = (struct sadb_msg *) out_skb->data;
2430 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002431 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 out_hdr->sadb_msg_satype = 0;
2433 out_hdr->sadb_msg_errno = 0;
2434 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2435 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Eric Dumazet36f41f82017-08-14 10:16:45 -07002436 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 err = 0;
2438
2439out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 return err;
2441}
2442
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002443static int pfkey_sockaddr_pair_size(sa_family_t family)
2444{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002445 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002446}
2447
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002448static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002449 xfrm_address_t *saddr, xfrm_address_t *daddr,
2450 u16 *family)
2451{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002452 int af, socklen;
2453
Herbert Xu096f41d2017-04-13 18:35:59 +08002454 if (ext_len < 2 || ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002455 return -EINVAL;
2456
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002457 af = pfkey_sockaddr_extract(sa, saddr);
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002458 if (!af)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002459 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002460
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002461 socklen = pfkey_sockaddr_len(af);
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002462 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002463 daddr) != af)
2464 return -EINVAL;
2465
2466 *family = af;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002467 return 0;
2468}
2469
Herbert Xu096f41d2017-04-13 18:35:59 +08002470#ifdef CONFIG_NET_KEY_MIGRATE
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002471static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2472 struct xfrm_migrate *m)
2473{
2474 int err;
2475 struct sadb_x_ipsecrequest *rq2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002476 int mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002477
Herbert Xu096f41d2017-04-13 18:35:59 +08002478 if (len < sizeof(*rq1) ||
2479 len < rq1->sadb_x_ipsecrequest_len ||
2480 rq1->sadb_x_ipsecrequest_len < sizeof(*rq1))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002481 return -EINVAL;
2482
2483 /* old endoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002484 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
Herbert Xu096f41d2017-04-13 18:35:59 +08002485 rq1->sadb_x_ipsecrequest_len - sizeof(*rq1),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002486 &m->old_saddr, &m->old_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002487 &m->old_family);
2488 if (err)
2489 return err;
2490
2491 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2492 len -= rq1->sadb_x_ipsecrequest_len;
2493
Herbert Xu096f41d2017-04-13 18:35:59 +08002494 if (len <= sizeof(*rq2) ||
2495 len < rq2->sadb_x_ipsecrequest_len ||
2496 rq2->sadb_x_ipsecrequest_len < sizeof(*rq2))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002497 return -EINVAL;
2498
2499 /* new endpoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002500 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
Herbert Xu096f41d2017-04-13 18:35:59 +08002501 rq2->sadb_x_ipsecrequest_len - sizeof(*rq2),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002502 &m->new_saddr, &m->new_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002503 &m->new_family);
2504 if (err)
2505 return err;
2506
2507 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2508 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2509 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2510 return -EINVAL;
2511
2512 m->proto = rq1->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002513 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2514 return -EINVAL;
2515 m->mode = mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002516 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2517
2518 return ((int)(rq1->sadb_x_ipsecrequest_len +
2519 rq2->sadb_x_ipsecrequest_len));
2520}
2521
2522static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002523 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002524{
2525 int i, len, ret, err = -EINVAL;
2526 u8 dir;
2527 struct sadb_address *sa;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002528 struct sadb_x_kmaddress *kma;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002529 struct sadb_x_policy *pol;
2530 struct sadb_x_ipsecrequest *rq;
2531 struct xfrm_selector sel;
2532 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002533 struct xfrm_kmaddress k;
Fan Du8d549c42013-11-07 17:47:49 +08002534 struct net *net = sock_net(sk);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002535
2536 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002537 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002538 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2539 err = -EINVAL;
2540 goto out;
2541 }
2542
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002543 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002544 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002545
2546 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2547 err = -EINVAL;
2548 goto out;
2549 }
2550
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002551 if (kma) {
2552 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2553 k.reserved = kma->sadb_x_kmaddress_reserved;
2554 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2555 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2556 &k.local, &k.remote, &k.family);
2557 if (ret < 0) {
2558 err = ret;
2559 goto out;
2560 }
2561 }
2562
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002563 dir = pol->sadb_x_policy_dir - 1;
2564 memset(&sel, 0, sizeof(sel));
2565
2566 /* set source address info of selector */
2567 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2568 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2569 sel.prefixlen_s = sa->sadb_address_prefixlen;
2570 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2571 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2572 if (sel.sport)
Al Viro582ee432007-07-26 17:33:39 +01002573 sel.sport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002574
2575 /* set destination address info of selector */
Himangi Saraogi47162c02014-05-30 21:18:12 +05302576 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002577 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2578 sel.prefixlen_d = sa->sadb_address_prefixlen;
2579 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2580 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2581 if (sel.dport)
Al Viro582ee432007-07-26 17:33:39 +01002582 sel.dport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002583
2584 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2585
2586 /* extract ipsecrequests */
2587 i = 0;
2588 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2589
2590 while (len > 0 && i < XFRM_MAX_DEPTH) {
2591 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2592 if (ret < 0) {
2593 err = ret;
2594 goto out;
2595 } else {
2596 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2597 len -= ret;
2598 i++;
2599 }
2600 }
2601
2602 if (!i || len > 0) {
2603 err = -EINVAL;
2604 goto out;
2605 }
2606
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002607 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
Antony Antony4ab47d42017-06-06 12:12:13 +02002608 kma ? &k : NULL, net, NULL);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002609
2610 out:
2611 return err;
2612}
2613#else
2614static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
Stephen Hemminger7f6daa62011-03-01 22:51:52 -08002615 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002616{
2617 return -ENOPROTOOPT;
2618}
2619#endif
2620
2621
David S. Miller4c93fbb2011-02-25 18:07:06 -08002622static 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 -07002623{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002624 struct net *net = sock_net(sk);
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002625 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002626 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 struct sadb_x_policy *pol;
2628 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002629 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2632 return -EINVAL;
2633
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002634 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2635 if (dir >= XFRM_POLICY_MAX)
2636 return -EINVAL;
2637
Eric Paris215a2dd2007-03-07 16:01:45 -08002638 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002639 xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002640 dir, pol->sadb_x_policy_id, delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (xp == NULL)
2642 return -ENOENT;
2643
Eric Paris215a2dd2007-03-07 16:01:45 -08002644 if (delete) {
Tetsuo Handa2e710292014-04-22 21:48:30 +09002645 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Eric Paris215a2dd2007-03-07 16:01:45 -08002647 if (err)
2648 goto out;
2649 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002650 c.portid = hdr->sadb_msg_pid;
Herbert Xubf08867f92005-06-18 22:44:00 -07002651 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002652 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002653 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002654 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002655 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Eric Paris215a2dd2007-03-07 16:01:45 -08002658out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 xfrm_pol_put(xp);
2660 return err;
2661}
2662
2663static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2664{
Timo Teras83321d62008-03-03 23:40:12 -08002665 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 struct sk_buff *out_skb;
2667 struct sadb_msg *out_hdr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002668 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Timo Teras83321d62008-03-03 23:40:12 -08002670 if (!pfkey_can_dump(&pfk->sk))
2671 return -ENOBUFS;
2672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2674 if (IS_ERR(out_skb))
2675 return PTR_ERR(out_skb);
2676
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002677 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2678 if (err < 0)
2679 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
2681 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08002682 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2684 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2685 out_hdr->sadb_msg_errno = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07002686 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002687 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07002688
2689 if (pfk->dump.skb)
Eric Dumazet36f41f82017-08-14 10:16:45 -07002690 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002691 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07002692 pfk->dump.skb = out_skb;
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return 0;
2695}
2696
Timo Teras83321d62008-03-03 23:40:12 -08002697static int pfkey_dump_sp(struct pfkey_sock *pfk)
2698{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002699 struct net *net = sock_net(&pfk->sk);
2700 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08002701}
2702
2703static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2704{
Fan Du283bc9f2013-11-07 17:47:50 +08002705 struct net *net = sock_net((struct sock *)pfk);
2706
2707 xfrm_policy_walk_done(&pfk->dump.u.policy, net);
Timo Teras83321d62008-03-03 23:40:12 -08002708}
2709
David S. Miller4c93fbb2011-02-25 18:07:06 -08002710static 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 -07002711{
Timo Teras83321d62008-03-03 23:40:12 -08002712 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Yuejie Shi89e357d2017-03-31 15:10:20 +08002714 mutex_lock(&pfk->dump_lock);
2715 if (pfk->dump.dump != NULL) {
2716 mutex_unlock(&pfk->dump_lock);
Timo Teras83321d62008-03-03 23:40:12 -08002717 return -EBUSY;
Yuejie Shi89e357d2017-03-31 15:10:20 +08002718 }
Timo Teras4c563f72008-02-28 21:31:08 -08002719
Timo Teras83321d62008-03-03 23:40:12 -08002720 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002721 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08002722 pfk->dump.dump = pfkey_dump_sp;
2723 pfk->dump.done = pfkey_dump_sp_done;
2724 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
Yuejie Shi89e357d2017-03-31 15:10:20 +08002725 mutex_unlock(&pfk->dump_lock);
Timo Teras83321d62008-03-03 23:40:12 -08002726
2727 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728}
2729
David S. Miller214e0052011-02-24 00:02:38 -05002730static int key_notify_policy_flush(const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
2732 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002733 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002735 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 if (!skb_out)
2737 return -ENOBUFS;
Johannes Berg4df864c2017-06-16 14:29:21 +02002738 hdr = skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002739 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002740 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002741 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002742 hdr->sadb_msg_version = PF_KEY_V2;
2743 hdr->sadb_msg_errno = (uint8_t) 0;
Nicolas Dichtel85dfb742013-02-18 16:24:20 +01002744 hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002745 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02002746 hdr->sadb_msg_reserved = 0;
Eric Dumazet36f41f82017-08-14 10:16:45 -07002747 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002748 return 0;
2749
2750}
2751
David S. Miller4c93fbb2011-02-25 18:07:06 -08002752static 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 -07002753{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002754 struct net *net = sock_net(sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002755 struct km_event c;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002756 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Tetsuo Handa2e710292014-04-22 21:48:30 +09002758 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002759 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002760 if (err || err2) {
2761 if (err == -ESRCH) /* empty table - old silent behavior */
2762 return 0;
2763 return err;
2764 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002765
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002766 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002767 c.event = XFRM_MSG_FLUSHPOLICY;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002768 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002769 c.seq = hdr->sadb_msg_seq;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002770 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002771 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 return 0;
2774}
2775
2776typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002777 const struct sadb_msg *hdr, void * const *ext_hdrs);
Mathias Krause8603b9552013-06-26 23:56:59 +02002778static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 [SADB_RESERVED] = pfkey_reserved,
2780 [SADB_GETSPI] = pfkey_getspi,
2781 [SADB_UPDATE] = pfkey_add,
2782 [SADB_ADD] = pfkey_add,
2783 [SADB_DELETE] = pfkey_delete,
2784 [SADB_GET] = pfkey_get,
2785 [SADB_ACQUIRE] = pfkey_acquire,
2786 [SADB_REGISTER] = pfkey_register,
2787 [SADB_EXPIRE] = NULL,
2788 [SADB_FLUSH] = pfkey_flush,
2789 [SADB_DUMP] = pfkey_dump,
2790 [SADB_X_PROMISC] = pfkey_promisc,
2791 [SADB_X_PCHANGE] = NULL,
2792 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2793 [SADB_X_SPDADD] = pfkey_spdadd,
2794 [SADB_X_SPDDELETE] = pfkey_spddelete,
2795 [SADB_X_SPDGET] = pfkey_spdget,
2796 [SADB_X_SPDACQUIRE] = NULL,
2797 [SADB_X_SPDDUMP] = pfkey_spddump,
2798 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2799 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2800 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002801 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802};
2803
David S. Miller4c93fbb2011-02-25 18:07:06 -08002804static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
2806 void *ext_hdrs[SADB_EXT_MAX];
2807 int err;
2808
Eric Dumazet36f41f82017-08-14 10:16:45 -07002809 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002810 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2813 err = parse_exthdrs(skb, hdr, ext_hdrs);
2814 if (!err) {
2815 err = -EOPNOTSUPP;
2816 if (pfkey_funcs[hdr->sadb_msg_type])
2817 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2818 }
2819 return err;
2820}
2821
2822static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2823{
2824 struct sadb_msg *hdr = NULL;
2825
2826 if (skb->len < sizeof(*hdr)) {
2827 *errp = -EMSGSIZE;
2828 } else {
2829 hdr = (struct sadb_msg *) skb->data;
2830 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2831 hdr->sadb_msg_reserved != 0 ||
2832 (hdr->sadb_msg_type <= SADB_RESERVED ||
2833 hdr->sadb_msg_type > SADB_MAX)) {
2834 hdr = NULL;
2835 *errp = -EINVAL;
2836 } else if (hdr->sadb_msg_len != (skb->len /
2837 sizeof(uint64_t)) ||
2838 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2839 sizeof(uint64_t))) {
2840 hdr = NULL;
2841 *errp = -EMSGSIZE;
2842 } else {
2843 *errp = 0;
2844 }
2845 }
2846 return hdr;
2847}
2848
David S. Miller4c93fbb2011-02-25 18:07:06 -08002849static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
2850 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
Herbert Xuf3980352007-12-19 23:44:29 -08002852 unsigned int id = d->desc.sadb_alg_id;
2853
2854 if (id >= sizeof(t->aalgos) * 8)
2855 return 0;
2856
2857 return (t->aalgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858}
2859
David S. Miller4c93fbb2011-02-25 18:07:06 -08002860static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
2861 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Herbert Xuf3980352007-12-19 23:44:29 -08002863 unsigned int id = d->desc.sadb_alg_id;
2864
2865 if (id >= sizeof(t->ealgos) * 8)
2866 return 0;
2867
2868 return (t->ealgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
2870
David S. Miller4c93fbb2011-02-25 18:07:06 -08002871static int count_ah_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
2873 int i, sz = 0;
2874
2875 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002876 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (!aalg)
2878 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002879 if (!aalg->pfkey_supported)
2880 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 if (aalg_tmpl_set(t, aalg) && aalg->available)
2882 sz += sizeof(struct sadb_comb);
2883 }
2884 return sz + sizeof(struct sadb_prop);
2885}
2886
David S. Miller4c93fbb2011-02-25 18:07:06 -08002887static int count_esp_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
2889 int i, k, sz = 0;
2890
2891 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002892 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (!ealg)
2894 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002895
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002896 if (!ealg->pfkey_supported)
2897 continue;
2898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2900 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 for (k = 1; ; k++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002903 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if (!aalg)
2905 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002906
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002907 if (!aalg->pfkey_supported)
2908 continue;
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 if (aalg_tmpl_set(t, aalg) && aalg->available)
2911 sz += sizeof(struct sadb_comb);
2912 }
2913 }
2914 return sz + sizeof(struct sadb_prop);
2915}
2916
David S. Miller4c93fbb2011-02-25 18:07:06 -08002917static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
2919 struct sadb_prop *p;
2920 int i;
2921
Johannes Berg4df864c2017-06-16 14:29:21 +02002922 p = skb_put(skb, sizeof(struct sadb_prop));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2924 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2925 p->sadb_prop_replay = 32;
2926 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2927
2928 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002929 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (!aalg)
2931 break;
2932
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002933 if (!aalg->pfkey_supported)
2934 continue;
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2937 struct sadb_comb *c;
Johannes Bergb080db52017-06-16 14:29:19 +02002938 c = skb_put_zero(skb, sizeof(struct sadb_comb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2940 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2941 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2942 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2943 c->sadb_comb_hard_addtime = 24*60*60;
2944 c->sadb_comb_soft_addtime = 20*60*60;
2945 c->sadb_comb_hard_usetime = 8*60*60;
2946 c->sadb_comb_soft_usetime = 7*60*60;
2947 }
2948 }
2949}
2950
David S. Miller4c93fbb2011-02-25 18:07:06 -08002951static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
2953 struct sadb_prop *p;
2954 int i, k;
2955
Johannes Berg4df864c2017-06-16 14:29:21 +02002956 p = skb_put(skb, sizeof(struct sadb_prop));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2958 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2959 p->sadb_prop_replay = 32;
2960 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2961
2962 for (i=0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002963 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (!ealg)
2965 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002966
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002967 if (!ealg->pfkey_supported)
2968 continue;
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2971 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 for (k = 1; ; k++) {
2974 struct sadb_comb *c;
David S. Miller4c93fbb2011-02-25 18:07:06 -08002975 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 if (!aalg)
2977 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002978 if (!aalg->pfkey_supported)
2979 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2981 continue;
Johannes Berg4df864c2017-06-16 14:29:21 +02002982 c = skb_put(skb, sizeof(struct sadb_comb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 memset(c, 0, sizeof(*c));
2984 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2985 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2986 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2987 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2988 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2989 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2990 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2991 c->sadb_comb_hard_addtime = 24*60*60;
2992 c->sadb_comb_soft_addtime = 20*60*60;
2993 c->sadb_comb_hard_usetime = 8*60*60;
2994 c->sadb_comb_soft_usetime = 7*60*60;
2995 }
2996 }
2997}
2998
David S. Miller214e0052011-02-24 00:02:38 -05002999static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003000{
3001 return 0;
3002}
3003
David S. Miller214e0052011-02-24 00:02:38 -05003004static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
3006 struct sk_buff *out_skb;
3007 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003008 int hard;
3009 int hsc;
3010
Herbert Xubf08867f92005-06-18 22:44:00 -07003011 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003012 if (hard)
3013 hsc = 2;
3014 else
3015 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Herbert Xu050f0092007-10-09 13:31:47 -07003017 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 if (IS_ERR(out_skb))
3019 return PTR_ERR(out_skb);
3020
3021 out_hdr = (struct sadb_msg *) out_skb->data;
3022 out_hdr->sadb_msg_version = PF_KEY_V2;
3023 out_hdr->sadb_msg_type = SADB_EXPIRE;
3024 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3025 out_hdr->sadb_msg_errno = 0;
3026 out_hdr->sadb_msg_reserved = 0;
3027 out_hdr->sadb_msg_seq = 0;
3028 out_hdr->sadb_msg_pid = 0;
3029
Eric Dumazet36f41f82017-08-14 10:16:45 -07003030 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3031 xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 return 0;
3033}
3034
David S. Miller214e0052011-02-24 00:02:38 -05003035static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003036{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003037 struct net *net = x ? xs_net(x) : c->net;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003038 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3039
3040 if (atomic_read(&net_pfkey->socks_nr) == 0)
Jamal Hadi Salim99c6f602008-06-10 14:25:34 -07003041 return 0;
3042
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003043 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003044 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003045 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003046 case XFRM_MSG_DELSA:
3047 case XFRM_MSG_NEWSA:
3048 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003049 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003050 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003051 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003052 case XFRM_MSG_NEWAE: /* not yet supported */
3053 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003054 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003055 pr_err("pfkey: Unknown SA event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003056 break;
3057 }
3058
3059 return 0;
3060}
3061
David S. Miller214e0052011-02-24 00:02:38 -05003062static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003063{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003064 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3065 return 0;
3066
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003067 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003068 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003069 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003070 case XFRM_MSG_DELPOLICY:
3071 case XFRM_MSG_NEWPOLICY:
3072 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003073 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003074 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003075 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3076 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003077 return key_notify_policy_flush(c);
3078 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003079 pr_err("pfkey: Unknown policy event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003080 break;
3081 }
3082
3083 return 0;
3084}
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086static u32 get_acqseq(void)
3087{
3088 u32 res;
Eric Dumazet28aecb92010-01-29 04:05:52 +00003089 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Eric Dumazet28aecb92010-01-29 04:05:52 +00003091 do {
3092 res = atomic_inc_return(&acqseq);
3093 } while (!res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 return res;
3095}
3096
Horia Geanta0f245582014-02-12 16:20:06 +02003097static bool pfkey_is_alive(const struct km_event *c)
3098{
3099 struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id);
3100 struct sock *sk;
3101 bool is_alive = false;
3102
3103 rcu_read_lock();
3104 sk_for_each_rcu(sk, &net_pfkey->table) {
3105 if (pfkey_sk(sk)->registered) {
3106 is_alive = true;
3107 break;
3108 }
3109 }
3110 rcu_read_unlock();
3111
3112 return is_alive;
3113}
3114
Fan Du65e07362012-08-15 10:13:47 +08003115static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
3117 struct sk_buff *skb;
3118 struct sadb_msg *hdr;
3119 struct sadb_address *addr;
3120 struct sadb_x_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 int sockaddr_size;
3122 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003123 struct sadb_x_sec_ctx *sec_ctx;
3124 struct xfrm_sec_ctx *xfrm_ctx;
3125 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3128 if (!sockaddr_size)
3129 return -EINVAL;
3130
3131 size = sizeof(struct sadb_msg) +
3132 (sizeof(struct sadb_address) * 2) +
3133 (sockaddr_size * 2) +
3134 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 if (x->id.proto == IPPROTO_AH)
3137 size += count_ah_combs(t);
3138 else if (x->id.proto == IPPROTO_ESP)
3139 size += count_esp_combs(t);
3140
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003141 if ((xfrm_ctx = x->security)) {
3142 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3143 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3144 }
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 skb = alloc_skb(size + 16, GFP_ATOMIC);
3147 if (skb == NULL)
3148 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003149
Johannes Berg4df864c2017-06-16 14:29:21 +02003150 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 hdr->sadb_msg_version = PF_KEY_V2;
3152 hdr->sadb_msg_type = SADB_ACQUIRE;
3153 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3154 hdr->sadb_msg_len = size / sizeof(uint64_t);
3155 hdr->sadb_msg_errno = 0;
3156 hdr->sadb_msg_reserved = 0;
3157 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3158 hdr->sadb_msg_pid = 0;
3159
3160 /* src address */
Johannes Berg4df864c2017-06-16 14:29:21 +02003161 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003162 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 (sizeof(struct sadb_address)+sockaddr_size)/
3164 sizeof(uint64_t);
3165 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3166 addr->sadb_address_proto = 0;
3167 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003168 addr->sadb_address_prefixlen =
3169 pfkey_sockaddr_fill(&x->props.saddr, 0,
3170 (struct sockaddr *) (addr + 1),
3171 x->props.family);
3172 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003174
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 /* dst address */
Johannes Berg4df864c2017-06-16 14:29:21 +02003176 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 addr->sadb_address_len =
3178 (sizeof(struct sadb_address)+sockaddr_size)/
3179 sizeof(uint64_t);
3180 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3181 addr->sadb_address_proto = 0;
3182 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003183 addr->sadb_address_prefixlen =
3184 pfkey_sockaddr_fill(&x->id.daddr, 0,
3185 (struct sockaddr *) (addr + 1),
3186 x->props.family);
3187 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 BUG();
3189
Johannes Berg4df864c2017-06-16 14:29:21 +02003190 pol = skb_put(skb, sizeof(struct sadb_x_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3192 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3193 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
Fan Du65e07362012-08-15 10:13:47 +08003194 pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1;
Dan Carpenterff862a42013-07-28 23:04:45 +03003195 pol->sadb_x_policy_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 pol->sadb_x_policy_id = xp->index;
Dan Carpenterff862a42013-07-28 23:04:45 +03003197 pol->sadb_x_policy_priority = xp->priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
3199 /* Set sadb_comb's. */
3200 if (x->id.proto == IPPROTO_AH)
3201 dump_ah_combs(skb, t);
3202 else if (x->id.proto == IPPROTO_ESP)
3203 dump_esp_combs(skb, t);
3204
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003205 /* security context */
3206 if (xfrm_ctx) {
Johannes Berg4df864c2017-06-16 14:29:21 +02003207 sec_ctx = skb_put(skb,
3208 sizeof(struct sadb_x_sec_ctx) + ctx_size);
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003209 sec_ctx->sadb_x_sec_len =
3210 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3211 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3212 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3213 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3214 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3215 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3216 xfrm_ctx->ctx_len);
3217 }
3218
Eric Dumazet36f41f82017-08-14 10:16:45 -07003219 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3220 xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003223static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003224 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003226 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 struct xfrm_policy *xp;
3228 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003229 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003231 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 case AF_INET:
3233 if (opt != IP_IPSEC_POLICY) {
3234 *dir = -EOPNOTSUPP;
3235 return NULL;
3236 }
3237 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00003238#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 case AF_INET6:
3240 if (opt != IPV6_IPSEC_POLICY) {
3241 *dir = -EOPNOTSUPP;
3242 return NULL;
3243 }
3244 break;
3245#endif
3246 default:
3247 *dir = -EINVAL;
3248 return NULL;
3249 }
3250
3251 *dir = -EINVAL;
3252
3253 if (len < sizeof(struct sadb_x_policy) ||
3254 pol->sadb_x_policy_len*8 > len ||
3255 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3256 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3257 return NULL;
3258
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003259 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 if (xp == NULL) {
3261 *dir = -ENOBUFS;
3262 return NULL;
3263 }
3264
3265 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3266 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3267
3268 xp->lft.soft_byte_limit = XFRM_INF;
3269 xp->lft.hard_byte_limit = XFRM_INF;
3270 xp->lft.soft_packet_limit = XFRM_INF;
3271 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003272 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274 xp->xfrm_nr = 0;
3275 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3276 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3277 goto out;
3278
Trent Jaegerdf718372005-12-13 23:12:27 -08003279 /* security context too */
3280 if (len >= (pol->sadb_x_policy_len*8 +
3281 sizeof(struct sadb_x_sec_ctx))) {
3282 char *p = (char *)pol;
3283 struct xfrm_user_sec_ctx *uctx;
3284
3285 p += pol->sadb_x_policy_len*8;
3286 sec_ctx = (struct sadb_x_sec_ctx *)p;
3287 if (len < pol->sadb_x_policy_len*8 +
Steffen Klassertd90c9022017-05-05 07:40:42 +02003288 sec_ctx->sadb_x_sec_len*8) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003289 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003290 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003291 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003292 if ((*dir = verify_sec_ctx_len(p)))
3293 goto out;
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01003294 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_ATOMIC);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01003295 *dir = security_xfrm_policy_alloc(&xp->security, uctx, GFP_ATOMIC);
Trent Jaegerdf718372005-12-13 23:12:27 -08003296 kfree(uctx);
3297
3298 if (*dir)
3299 goto out;
3300 }
3301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 *dir = pol->sadb_x_policy_dir-1;
3303 return xp;
3304
3305out:
Alexey Dobriyan70e90672008-11-06 23:08:37 -08003306 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08003307 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 return NULL;
3309}
3310
Al Viro5d36b182006-11-08 00:24:06 -08003311static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
3313 struct sk_buff *skb;
3314 struct sadb_msg *hdr;
3315 struct sadb_sa *sa;
3316 struct sadb_address *addr;
3317 struct sadb_x_nat_t_port *n_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 int sockaddr_size;
3319 int size;
3320 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3321 struct xfrm_encap_tmpl *natt = NULL;
3322
3323 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3324 if (!sockaddr_size)
3325 return -EINVAL;
3326
3327 if (!satype)
3328 return -EINVAL;
3329
3330 if (!x->encap)
3331 return -EINVAL;
3332
3333 natt = x->encap;
3334
3335 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3336 *
3337 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3338 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3339 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 size = sizeof(struct sadb_msg) +
3342 sizeof(struct sadb_sa) +
3343 (sizeof(struct sadb_address) * 2) +
3344 (sockaddr_size * 2) +
3345 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 skb = alloc_skb(size + 16, GFP_ATOMIC);
3348 if (skb == NULL)
3349 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003350
Johannes Berg4df864c2017-06-16 14:29:21 +02003351 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 hdr->sadb_msg_version = PF_KEY_V2;
3353 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3354 hdr->sadb_msg_satype = satype;
3355 hdr->sadb_msg_len = size / sizeof(uint64_t);
3356 hdr->sadb_msg_errno = 0;
3357 hdr->sadb_msg_reserved = 0;
3358 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3359 hdr->sadb_msg_pid = 0;
3360
3361 /* SA */
Johannes Berg4df864c2017-06-16 14:29:21 +02003362 sa = skb_put(skb, sizeof(struct sadb_sa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3364 sa->sadb_sa_exttype = SADB_EXT_SA;
3365 sa->sadb_sa_spi = x->id.spi;
3366 sa->sadb_sa_replay = 0;
3367 sa->sadb_sa_state = 0;
3368 sa->sadb_sa_auth = 0;
3369 sa->sadb_sa_encrypt = 0;
3370 sa->sadb_sa_flags = 0;
3371
3372 /* ADDRESS_SRC (old addr) */
Johannes Berg4df864c2017-06-16 14:29:21 +02003373 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003374 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 (sizeof(struct sadb_address)+sockaddr_size)/
3376 sizeof(uint64_t);
3377 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3378 addr->sadb_address_proto = 0;
3379 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003380 addr->sadb_address_prefixlen =
3381 pfkey_sockaddr_fill(&x->props.saddr, 0,
3382 (struct sockaddr *) (addr + 1),
3383 x->props.family);
3384 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 BUG();
3386
3387 /* NAT_T_SPORT (old port) */
Johannes Berg4df864c2017-06-16 14:29:21 +02003388 n_port = skb_put(skb, sizeof(*n_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3390 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3391 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3392 n_port->sadb_x_nat_t_port_reserved = 0;
3393
3394 /* ADDRESS_DST (new addr) */
Johannes Berg4df864c2017-06-16 14:29:21 +02003395 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003396 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 (sizeof(struct sadb_address)+sockaddr_size)/
3398 sizeof(uint64_t);
3399 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3400 addr->sadb_address_proto = 0;
3401 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003402 addr->sadb_address_prefixlen =
3403 pfkey_sockaddr_fill(ipaddr, 0,
3404 (struct sockaddr *) (addr + 1),
3405 x->props.family);
3406 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 BUG();
3408
3409 /* NAT_T_DPORT (new port) */
Johannes Berg4df864c2017-06-16 14:29:21 +02003410 n_port = skb_put(skb, sizeof(*n_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3412 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3413 n_port->sadb_x_nat_t_port_port = sport;
3414 n_port->sadb_x_nat_t_port_reserved = 0;
3415
Eric Dumazet36f41f82017-08-14 10:16:45 -07003416 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3417 xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418}
3419
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003420#ifdef CONFIG_NET_KEY_MIGRATE
3421static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
David S. Miller183cad12011-02-24 00:28:01 -05003422 const struct xfrm_selector *sel)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003423{
3424 struct sadb_address *addr;
Johannes Berg4df864c2017-06-16 14:29:21 +02003425 addr = skb_put(skb, sizeof(struct sadb_address) + sasize);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003426 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3427 addr->sadb_address_exttype = type;
3428 addr->sadb_address_proto = sel->proto;
3429 addr->sadb_address_reserved = 0;
3430
3431 switch (type) {
3432 case SADB_EXT_ADDRESS_SRC:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003433 addr->sadb_address_prefixlen = sel->prefixlen_s;
3434 pfkey_sockaddr_fill(&sel->saddr, 0,
3435 (struct sockaddr *)(addr + 1),
3436 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003437 break;
3438 case SADB_EXT_ADDRESS_DST:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003439 addr->sadb_address_prefixlen = sel->prefixlen_d;
3440 pfkey_sockaddr_fill(&sel->daddr, 0,
3441 (struct sockaddr *)(addr + 1),
3442 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003443 break;
3444 default:
3445 return -EINVAL;
3446 }
3447
3448 return 0;
3449}
3450
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003451
David S. Miller183cad12011-02-24 00:28:01 -05003452static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003453{
3454 struct sadb_x_kmaddress *kma;
3455 u8 *sa;
3456 int family = k->family;
3457 int socklen = pfkey_sockaddr_len(family);
3458 int size_req;
3459
3460 size_req = (sizeof(struct sadb_x_kmaddress) +
3461 pfkey_sockaddr_pair_size(family));
3462
Johannes Bergb080db52017-06-16 14:29:19 +02003463 kma = skb_put_zero(skb, size_req);
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003464 kma->sadb_x_kmaddress_len = size_req / 8;
3465 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3466 kma->sadb_x_kmaddress_reserved = k->reserved;
3467
3468 sa = (u8 *)(kma + 1);
3469 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3470 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3471 return -EINVAL;
3472
3473 return 0;
3474}
3475
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003476static int set_ipsecrequest(struct sk_buff *skb,
3477 uint8_t proto, uint8_t mode, int level,
3478 uint32_t reqid, uint8_t family,
David S. Miller183cad12011-02-24 00:28:01 -05003479 const xfrm_address_t *src, const xfrm_address_t *dst)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003480{
3481 struct sadb_x_ipsecrequest *rq;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003482 u8 *sa;
3483 int socklen = pfkey_sockaddr_len(family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003484 int size_req;
3485
3486 size_req = sizeof(struct sadb_x_ipsecrequest) +
3487 pfkey_sockaddr_pair_size(family);
3488
Johannes Bergb080db52017-06-16 14:29:19 +02003489 rq = skb_put_zero(skb, size_req);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003490 rq->sadb_x_ipsecrequest_len = size_req;
3491 rq->sadb_x_ipsecrequest_proto = proto;
3492 rq->sadb_x_ipsecrequest_mode = mode;
3493 rq->sadb_x_ipsecrequest_level = level;
3494 rq->sadb_x_ipsecrequest_reqid = reqid;
3495
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003496 sa = (u8 *) (rq + 1);
3497 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3498 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003499 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003500
3501 return 0;
3502}
3503#endif
3504
3505#ifdef CONFIG_NET_KEY_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05003506static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3507 const struct xfrm_migrate *m, int num_bundles,
Antony Antony8bafd732017-06-06 12:12:14 +02003508 const struct xfrm_kmaddress *k,
3509 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003510{
3511 int i;
3512 int sasize_sel;
3513 int size = 0;
3514 int size_pol = 0;
3515 struct sk_buff *skb;
3516 struct sadb_msg *hdr;
3517 struct sadb_x_policy *pol;
David S. Miller183cad12011-02-24 00:28:01 -05003518 const struct xfrm_migrate *mp;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003519
3520 if (type != XFRM_POLICY_TYPE_MAIN)
3521 return 0;
3522
3523 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3524 return -EINVAL;
3525
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003526 if (k != NULL) {
3527 /* addresses for KM */
3528 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3529 pfkey_sockaddr_pair_size(k->family));
3530 }
3531
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003532 /* selector */
3533 sasize_sel = pfkey_sockaddr_size(sel->family);
3534 if (!sasize_sel)
3535 return -EINVAL;
3536 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3537
3538 /* policy info */
3539 size_pol += sizeof(struct sadb_x_policy);
3540
3541 /* ipsecrequests */
3542 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3543 /* old locator pair */
3544 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3545 pfkey_sockaddr_pair_size(mp->old_family);
3546 /* new locator pair */
3547 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3548 pfkey_sockaddr_pair_size(mp->new_family);
3549 }
3550
3551 size += sizeof(struct sadb_msg) + size_pol;
3552
3553 /* alloc buffer */
3554 skb = alloc_skb(size, GFP_ATOMIC);
3555 if (skb == NULL)
3556 return -ENOMEM;
3557
Johannes Berg4df864c2017-06-16 14:29:21 +02003558 hdr = skb_put(skb, sizeof(struct sadb_msg));
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003559 hdr->sadb_msg_version = PF_KEY_V2;
3560 hdr->sadb_msg_type = SADB_X_MIGRATE;
3561 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3562 hdr->sadb_msg_len = size / 8;
3563 hdr->sadb_msg_errno = 0;
3564 hdr->sadb_msg_reserved = 0;
3565 hdr->sadb_msg_seq = 0;
3566 hdr->sadb_msg_pid = 0;
3567
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003568 /* Addresses to be used by KM for negotiation, if ext is available */
3569 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
Julia Lawall89eb06f2012-04-08 22:41:10 +00003570 goto err;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003571
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003572 /* selector src */
3573 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3574
3575 /* selector dst */
3576 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3577
3578 /* policy information */
Johannes Berg4df864c2017-06-16 14:29:21 +02003579 pol = skb_put(skb, sizeof(struct sadb_x_policy));
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003580 pol->sadb_x_policy_len = size_pol / 8;
3581 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3582 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3583 pol->sadb_x_policy_dir = dir + 1;
Dan Carpenterff862a42013-07-28 23:04:45 +03003584 pol->sadb_x_policy_reserved = 0;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003585 pol->sadb_x_policy_id = 0;
3586 pol->sadb_x_policy_priority = 0;
3587
3588 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3589 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003590 int mode = pfkey_mode_from_xfrm(mp->mode);
3591 if (mode < 0)
Patrick McHardyd4782c32008-01-20 17:24:29 -08003592 goto err;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003593 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003594 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3595 mp->reqid, mp->old_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003596 &mp->old_saddr, &mp->old_daddr) < 0)
3597 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003598
3599 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003600 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003601 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3602 mp->reqid, mp->new_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003603 &mp->new_saddr, &mp->new_daddr) < 0)
3604 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003605 }
3606
3607 /* broadcast migrate message to sockets */
Eric Dumazet36f41f82017-08-14 10:16:45 -07003608 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003609
3610 return 0;
Patrick McHardyd4782c32008-01-20 17:24:29 -08003611
3612err:
3613 kfree_skb(skb);
3614 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003615}
3616#else
David S. Miller183cad12011-02-24 00:28:01 -05003617static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3618 const struct xfrm_migrate *m, int num_bundles,
Antony Antony8bafd732017-06-06 12:12:14 +02003619 const struct xfrm_kmaddress *k,
3620 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003621{
3622 return -ENOPROTOOPT;
3623}
3624#endif
3625
Ying Xue1b784142015-03-02 15:37:48 +08003626static int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 struct sock *sk = sock->sk;
3629 struct sk_buff *skb = NULL;
3630 struct sadb_msg *hdr = NULL;
3631 int err;
Fan Du283bc9f2013-11-07 17:47:50 +08003632 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
3634 err = -EOPNOTSUPP;
3635 if (msg->msg_flags & MSG_OOB)
3636 goto out;
3637
3638 err = -EMSGSIZE;
Eric Dumazet95c96172012-04-15 05:58:06 +00003639 if ((unsigned int)len > sk->sk_sndbuf - 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 goto out;
3641
3642 err = -ENOBUFS;
3643 skb = alloc_skb(len, GFP_KERNEL);
3644 if (skb == NULL)
3645 goto out;
3646
3647 err = -EFAULT;
Al Viro6ce8e9c2014-04-06 21:25:44 -04003648 if (memcpy_from_msg(skb_put(skb,len), msg, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 goto out;
3650
3651 hdr = pfkey_get_base_msg(skb, &err);
3652 if (!hdr)
3653 goto out;
3654
Fan Du283bc9f2013-11-07 17:47:50 +08003655 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 err = pfkey_process(sk, skb, hdr);
Fan Du283bc9f2013-11-07 17:47:50 +08003657 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
3659out:
3660 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3661 err = 0;
Wei Yongjun6f961062009-02-25 00:31:04 +00003662 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
3664 return err ? : len;
3665}
3666
Ying Xue1b784142015-03-02 15:37:48 +08003667static int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 int flags)
3669{
3670 struct sock *sk = sock->sk;
Timo Teras83321d62008-03-03 23:40:12 -08003671 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 struct sk_buff *skb;
3673 int copied, err;
3674
3675 err = -EINVAL;
3676 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3677 goto out;
3678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3680 if (skb == NULL)
3681 goto out;
3682
3683 copied = skb->len;
3684 if (copied > len) {
3685 msg->msg_flags |= MSG_TRUNC;
3686 copied = len;
3687 }
3688
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003689 skb_reset_transport_header(skb);
David S. Miller51f3d022014-11-05 16:46:40 -05003690 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 if (err)
3692 goto out_free;
3693
Neil Horman3b885782009-10-12 13:26:31 -07003694 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
3696 err = (flags & MSG_TRUNC) ? skb->len : copied;
3697
Timo Teras83321d62008-03-03 23:40:12 -08003698 if (pfk->dump.dump != NULL &&
3699 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3700 pfkey_do_dump(pfk);
3701
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702out_free:
3703 skb_free_datagram(sk, skb);
3704out:
3705 return err;
3706}
3707
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003708static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 .family = PF_KEY,
3710 .owner = THIS_MODULE,
3711 /* Operations that make no sense on pfkey sockets. */
3712 .bind = sock_no_bind,
3713 .connect = sock_no_connect,
3714 .socketpair = sock_no_socketpair,
3715 .accept = sock_no_accept,
3716 .getname = sock_no_getname,
3717 .ioctl = sock_no_ioctl,
3718 .listen = sock_no_listen,
3719 .shutdown = sock_no_shutdown,
3720 .setsockopt = sock_no_setsockopt,
3721 .getsockopt = sock_no_getsockopt,
3722 .mmap = sock_no_mmap,
3723 .sendpage = sock_no_sendpage,
3724
3725 /* Now the operations that really occur. */
3726 .release = pfkey_release,
3727 .poll = datagram_poll,
3728 .sendmsg = pfkey_sendmsg,
3729 .recvmsg = pfkey_recvmsg,
3730};
3731
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003732static const struct net_proto_family pfkey_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 .family = PF_KEY,
3734 .create = pfkey_create,
3735 .owner = THIS_MODULE,
3736};
3737
3738#ifdef CONFIG_PROC_FS
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003739static int pfkey_seq_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740{
Li Zefan27b5b862010-02-08 23:20:29 +00003741 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003743 if (v == SEQ_START_TOKEN)
3744 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3745 else
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003746 seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 s,
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003748 refcount_read(&s->sk_refcnt),
Eric Dumazet31e6d362009-06-17 19:05:41 -07003749 sk_rmem_alloc_get(s),
3750 sk_wmem_alloc_get(s),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06003751 from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 sock_i_ino(s)
3753 );
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755}
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003756
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003757static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
stephen hemmingerada440e2011-02-23 09:06:50 +00003758 __acquires(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003759{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003760 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003761 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003762
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003763 rcu_read_lock();
3764 return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003765}
3766
3767static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3768{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003769 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003770 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3771
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003772 return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003773}
3774
3775static void pfkey_seq_stop(struct seq_file *f, void *v)
stephen hemmingerada440e2011-02-23 09:06:50 +00003776 __releases(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003777{
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003778 rcu_read_unlock();
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003779}
3780
Stephen Hemminger98147d52009-09-01 19:25:02 +00003781static const struct seq_operations pfkey_seq_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003782 .start = pfkey_seq_start,
3783 .next = pfkey_seq_next,
3784 .stop = pfkey_seq_stop,
3785 .show = pfkey_seq_show,
3786};
3787
3788static int pfkey_seq_open(struct inode *inode, struct file *file)
3789{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003790 return seq_open_net(inode, file, &pfkey_seq_ops,
3791 sizeof(struct seq_net_private));
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003792}
3793
Stephen Hemminger5ca1b992009-09-01 19:25:05 +00003794static const struct file_operations pfkey_proc_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003795 .open = pfkey_seq_open,
3796 .read = seq_read,
3797 .llseek = seq_lseek,
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003798 .release = seq_release_net,
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003799};
3800
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003801static int __net_init pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003802{
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003803 struct proc_dir_entry *e;
3804
Gao fengd4beaa62013-02-18 01:34:54 +00003805 e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003806 if (e == NULL)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003807 return -ENOMEM;
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003808
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003809 return 0;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003810}
3811
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003812static void __net_exit pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003813{
Gao fengece31ff2013-02-18 01:34:56 +00003814 remove_proc_entry("pfkey", net->proc_net);
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003815}
3816#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003817static inline int pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003818{
3819 return 0;
3820}
3821
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003822static inline void pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003823{
3824}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825#endif
3826
3827static struct xfrm_mgr pfkeyv2_mgr =
3828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 .notify = pfkey_send_notify,
3830 .acquire = pfkey_send_acquire,
3831 .compile_policy = pfkey_compile_policy,
3832 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003833 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003834 .migrate = pfkey_send_migrate,
Horia Geanta0f245582014-02-12 16:20:06 +02003835 .is_alive = pfkey_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836};
3837
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003838static int __net_init pfkey_net_init(struct net *net)
3839{
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003840 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003841 int rv;
3842
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003843 INIT_HLIST_HEAD(&net_pfkey->table);
3844 atomic_set(&net_pfkey->socks_nr, 0);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003845
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003846 rv = pfkey_init_proc(net);
3847
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003848 return rv;
3849}
3850
3851static void __net_exit pfkey_net_exit(struct net *net)
3852{
3853 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3854
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003855 pfkey_exit_proc(net);
Vasily Averin663faea2017-11-12 22:26:53 +03003856 WARN_ON(!hlist_empty(&net_pfkey->table));
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003857}
3858
3859static struct pernet_operations pfkey_net_ops = {
3860 .init = pfkey_net_init,
3861 .exit = pfkey_net_exit,
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003862 .id = &pfkey_net_id,
3863 .size = sizeof(struct netns_pfkey),
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003864};
3865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866static void __exit ipsec_pfkey_exit(void)
3867{
3868 xfrm_unregister_km(&pfkeyv2_mgr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003870 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 proto_unregister(&key_proto);
3872}
3873
3874static int __init ipsec_pfkey_init(void)
3875{
3876 int err = proto_register(&key_proto, 0);
3877
3878 if (err != 0)
3879 goto out;
3880
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003881 err = register_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 if (err != 0)
3883 goto out_unregister_key_proto;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003884 err = sock_register(&pfkey_family_ops);
3885 if (err != 0)
3886 goto out_unregister_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 err = xfrm_register_km(&pfkeyv2_mgr);
3888 if (err != 0)
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003889 goto out_sock_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890out:
3891 return err;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003892
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893out_sock_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003895out_unregister_pernet:
3896 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897out_unregister_key_proto:
3898 proto_unregister(&key_proto);
3899 goto out;
3900}
3901
3902module_init(ipsec_pfkey_init);
3903module_exit(ipsec_pfkey_exit);
3904MODULE_LICENSE("GPL");
3905MODULE_ALIAS_NETPROTO(PF_KEY);