blob: 8ad430edb5b87882df5f25c3392a7aedb7eca841 [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));
112 WARN_ON(atomic_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) {
206 if (atomic_read(&skb->users) != 1) {
207 *skb2 = skb_clone(skb, allocation);
208 } else {
209 *skb2 = skb;
210 atomic_inc(&skb->users);
211 }
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
David Ahernba51b6b2015-08-24 15:17:17 -0600231static int pfkey_broadcast(struct sk_buff *skb,
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)
David Ahernba51b6b2015-08-24 15:17:17 -0600281 err = pfkey_broadcast_one(skb, &skb2, GFP_KERNEL, 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;
David Ahernba51b6b2015-08-24 15:17:17 -0600314 pfkey_broadcast(pfk->dump.skb, 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
352 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
353 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
David Ahernba51b6b2015-08-24 15:17:17 -0600358 pfkey_broadcast(skb, 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
404 switch (addr->sa_family) {
405 case AF_INET:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700406 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (sp->sadb_address_len != len ||
408 sp->sadb_address_prefixlen > 32)
409 return -EINVAL;
410 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000411#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 case AF_INET6:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700413 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (sp->sadb_address_len != len ||
415 sp->sadb_address_prefixlen > 128)
416 return -EINVAL;
417 break;
418#endif
419 default:
420 /* It is user using kernel to keep track of security
421 * associations for another protocol, such as
422 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
423 * lengths.
424 *
425 * XXX Actually, association/policy database is not yet
426 * XXX able to cope with arbitrary sockaddr families.
427 * XXX When it can, remove this -EINVAL. -DaveM
428 */
429 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 return 0;
433}
434
David S. Miller4c93fbb2011-02-25 18:07:06 -0800435static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800436{
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700437 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
438 sec_ctx->sadb_x_ctx_len,
439 sizeof(uint64_t));
Trent Jaegerdf718372005-12-13 23:12:27 -0800440}
441
David S. Miller4c93fbb2011-02-25 18:07:06 -0800442static inline int verify_sec_ctx_len(const void *p)
Trent Jaegerdf718372005-12-13 23:12:27 -0800443{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800444 const struct sadb_x_sec_ctx *sec_ctx = p;
Stephen Rothwell298bb622007-10-30 23:57:05 -0700445 int len = sec_ctx->sadb_x_ctx_len;
Trent Jaegerdf718372005-12-13 23:12:27 -0800446
Stephen Rothwell298bb622007-10-30 23:57:05 -0700447 if (len > PAGE_SIZE)
Trent Jaegerdf718372005-12-13 23:12:27 -0800448 return -EINVAL;
449
450 len = pfkey_sec_ctx_len(sec_ctx);
451
452 if (sec_ctx->sadb_x_sec_len != len)
453 return -EINVAL;
454
455 return 0;
456}
457
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +0100458static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx,
459 gfp_t gfp)
Trent Jaegerdf718372005-12-13 23:12:27 -0800460{
461 struct xfrm_user_sec_ctx *uctx = NULL;
462 int ctx_size = sec_ctx->sadb_x_ctx_len;
463
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +0100464 uctx = kmalloc((sizeof(*uctx)+ctx_size), gfp);
Trent Jaegerdf718372005-12-13 23:12:27 -0800465
466 if (!uctx)
467 return NULL;
468
469 uctx->len = pfkey_sec_ctx_len(sec_ctx);
470 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
471 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
472 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
473 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
474 memcpy(uctx + 1, sec_ctx + 1,
475 uctx->ctx_len);
476
477 return uctx;
478}
479
David S. Miller4c93fbb2011-02-25 18:07:06 -0800480static int present_and_same_family(const struct sadb_address *src,
481 const struct sadb_address *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800483 const struct sockaddr *s_addr, *d_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (!src || !dst)
486 return 0;
487
David S. Miller4c93fbb2011-02-25 18:07:06 -0800488 s_addr = (const struct sockaddr *)(src + 1);
489 d_addr = (const struct sockaddr *)(dst + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (s_addr->sa_family != d_addr->sa_family)
491 return 0;
492 if (s_addr->sa_family != AF_INET
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000493#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 && s_addr->sa_family != AF_INET6
495#endif
496 )
497 return 0;
498
499 return 1;
500}
501
David S. Miller4c93fbb2011-02-25 18:07:06 -0800502static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800504 const char *p = (char *) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 int len = skb->len;
506
507 len -= sizeof(*hdr);
508 p += sizeof(*hdr);
509 while (len > 0) {
David S. Miller4c93fbb2011-02-25 18:07:06 -0800510 const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 uint16_t ext_type;
512 int ext_len;
513
514 ext_len = ehdr->sadb_ext_len;
515 ext_len *= sizeof(uint64_t);
516 ext_type = ehdr->sadb_ext_type;
517 if (ext_len < sizeof(uint64_t) ||
518 ext_len > len ||
519 ext_type == SADB_EXT_RESERVED)
520 return -EINVAL;
521
522 if (ext_type <= SADB_EXT_MAX) {
523 int min = (int) sadb_ext_min_len[ext_type];
524 if (ext_len < min)
525 return -EINVAL;
526 if (ext_hdrs[ext_type-1] != NULL)
527 return -EINVAL;
528 if (ext_type == SADB_EXT_ADDRESS_SRC ||
529 ext_type == SADB_EXT_ADDRESS_DST ||
530 ext_type == SADB_EXT_ADDRESS_PROXY ||
531 ext_type == SADB_X_EXT_NAT_T_OA) {
532 if (verify_address_len(p))
533 return -EINVAL;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900534 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800535 if (ext_type == SADB_X_EXT_SEC_CTX) {
536 if (verify_sec_ctx_len(p))
537 return -EINVAL;
538 }
David S. Miller4c93fbb2011-02-25 18:07:06 -0800539 ext_hdrs[ext_type-1] = (void *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541 p += ext_len;
542 len -= ext_len;
543 }
544
545 return 0;
546}
547
548static uint16_t
549pfkey_satype2proto(uint8_t satype)
550{
551 switch (satype) {
552 case SADB_SATYPE_UNSPEC:
553 return IPSEC_PROTO_ANY;
554 case SADB_SATYPE_AH:
555 return IPPROTO_AH;
556 case SADB_SATYPE_ESP:
557 return IPPROTO_ESP;
558 case SADB_X_SATYPE_IPCOMP:
559 return IPPROTO_COMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 default:
561 return 0;
562 }
563 /* NOTREACHED */
564}
565
566static uint8_t
567pfkey_proto2satype(uint16_t proto)
568{
569 switch (proto) {
570 case IPPROTO_AH:
571 return SADB_SATYPE_AH;
572 case IPPROTO_ESP:
573 return SADB_SATYPE_ESP;
574 case IPPROTO_COMP:
575 return SADB_X_SATYPE_IPCOMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 default:
577 return 0;
578 }
579 /* NOTREACHED */
580}
581
582/* BTW, this scheme means that there is no way with PFKEY2 sockets to
583 * say specifically 'just raw sockets' as we encode them as 255.
584 */
585
586static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
587{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000588 return proto == IPSEC_PROTO_ANY ? 0 : proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
592{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000593 return proto ? proto : IPSEC_PROTO_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900596static inline int pfkey_sockaddr_len(sa_family_t family)
597{
598 switch (family) {
599 case AF_INET:
600 return sizeof(struct sockaddr_in);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000601#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900602 case AF_INET6:
603 return sizeof(struct sockaddr_in6);
604#endif
605 }
606 return 0;
607}
608
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900609static
610int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900612 switch (sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 case AF_INET:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900614 xaddr->a4 =
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900615 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return AF_INET;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000617#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 case AF_INET6:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900619 memcpy(xaddr->a6,
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900620 &((struct sockaddr_in6 *)sa)->sin6_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 sizeof(struct in6_addr));
622 return AF_INET6;
623#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900625 return 0;
626}
627
628static
David S. Miller4c93fbb2011-02-25 18:07:06 -0800629int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900630{
631 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
632 xaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
David S. Miller4c93fbb2011-02-25 18:07:06 -0800635static 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 -0700636{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800637 const struct sadb_sa *sa;
638 const struct sadb_address *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 uint16_t proto;
640 unsigned short family;
641 xfrm_address_t *xaddr;
642
Joe Perchesea110732011-06-13 16:21:26 +0000643 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (sa == NULL)
645 return NULL;
646
647 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
648 if (proto == 0)
649 return NULL;
650
651 /* sadb_address_len should be checked by caller */
Joe Perchesea110732011-06-13 16:21:26 +0000652 addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (addr == NULL)
654 return NULL;
655
David S. Miller4c93fbb2011-02-25 18:07:06 -0800656 family = ((const struct sockaddr *)(addr + 1))->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 switch (family) {
658 case AF_INET:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800659 xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000661#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 case AF_INET6:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800663 xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 break;
665#endif
666 default:
667 xaddr = NULL;
668 }
669
670 if (!xaddr)
671 return NULL;
672
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800673 return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678static int
679pfkey_sockaddr_size(sa_family_t family)
680{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900681 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700684static inline int pfkey_mode_from_xfrm(int mode)
685{
686 switch(mode) {
687 case XFRM_MODE_TRANSPORT:
688 return IPSEC_MODE_TRANSPORT;
689 case XFRM_MODE_TUNNEL:
690 return IPSEC_MODE_TUNNEL;
691 case XFRM_MODE_BEET:
692 return IPSEC_MODE_BEET;
693 default:
694 return -1;
695 }
696}
697
698static inline int pfkey_mode_to_xfrm(int mode)
699{
700 switch(mode) {
701 case IPSEC_MODE_ANY: /*XXX*/
702 case IPSEC_MODE_TRANSPORT:
703 return XFRM_MODE_TRANSPORT;
704 case IPSEC_MODE_TUNNEL:
705 return XFRM_MODE_TUNNEL;
706 case IPSEC_MODE_BEET:
707 return XFRM_MODE_BEET;
708 default:
709 return -1;
710 }
711}
712
David S. Miller183cad12011-02-24 00:28:01 -0500713static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
David S. Miller4c93fbb2011-02-25 18:07:06 -0800714 struct sockaddr *sa,
715 unsigned short family)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900716{
717 switch (family) {
718 case AF_INET:
719 {
720 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
721 sin->sin_family = AF_INET;
722 sin->sin_port = port;
723 sin->sin_addr.s_addr = xaddr->a4;
724 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
725 return 32;
726 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000727#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900728 case AF_INET6:
729 {
730 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
731 sin6->sin6_family = AF_INET6;
732 sin6->sin6_port = port;
733 sin6->sin6_flowinfo = 0;
Jiri Benc15e318b2015-03-29 16:59:24 +0200734 sin6->sin6_addr = xaddr->in6;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900735 sin6->sin6_scope_id = 0;
736 return 128;
737 }
738#endif
739 }
740 return 0;
741}
742
David S. Miller4c93fbb2011-02-25 18:07:06 -0800743static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -0700744 int add_keys, int hsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct sk_buff *skb;
747 struct sadb_msg *hdr;
748 struct sadb_sa *sa;
749 struct sadb_lifetime *lifetime;
750 struct sadb_address *addr;
751 struct sadb_key *key;
752 struct sadb_x_sa2 *sa2;
Trent Jaegerdf718372005-12-13 23:12:27 -0800753 struct sadb_x_sec_ctx *sec_ctx;
754 struct xfrm_sec_ctx *xfrm_ctx;
755 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 int size;
757 int auth_key_size = 0;
758 int encrypt_key_size = 0;
759 int sockaddr_size;
760 struct xfrm_encap_tmpl *natt = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700761 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 /* address family check */
764 sockaddr_size = pfkey_sockaddr_size(x->props.family);
765 if (!sockaddr_size)
766 return ERR_PTR(-EINVAL);
767
768 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
769 key(AE), (identity(SD),) (sensitivity)> */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900770 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 sizeof(struct sadb_lifetime) +
772 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
773 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900774 sizeof(struct sadb_address)*2 +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 sockaddr_size*2 +
776 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800777
778 if ((xfrm_ctx = x->security)) {
779 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
780 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
781 }
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* identity & sensitivity */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000784 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 size += sizeof(struct sadb_address) + sockaddr_size;
786
787 if (add_keys) {
788 if (x->aalg && x->aalg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900789 auth_key_size =
790 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 size += sizeof(struct sadb_key) + auth_key_size;
792 }
793 if (x->ealg && x->ealg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900794 encrypt_key_size =
795 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 size += sizeof(struct sadb_key) + encrypt_key_size;
797 }
798 }
799 if (x->encap)
800 natt = x->encap;
801
802 if (natt && natt->encap_type) {
803 size += sizeof(struct sadb_x_nat_t_type);
804 size += sizeof(struct sadb_x_nat_t_port);
805 size += sizeof(struct sadb_x_nat_t_port);
806 }
807
808 skb = alloc_skb(size + 16, GFP_ATOMIC);
809 if (skb == NULL)
810 return ERR_PTR(-ENOBUFS);
811
812 /* call should fill header later */
813 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
814 memset(hdr, 0, size); /* XXX do we need this ? */
815 hdr->sadb_msg_len = size / sizeof(uint64_t);
816
817 /* sa */
818 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
819 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
820 sa->sadb_sa_exttype = SADB_EXT_SA;
821 sa->sadb_sa_spi = x->id.spi;
822 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700823 switch (x->km.state) {
824 case XFRM_STATE_VALID:
825 sa->sadb_sa_state = x->km.dying ?
826 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
827 break;
828 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700830 break;
831 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700833 break;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 sa->sadb_sa_auth = 0;
836 if (x->aalg) {
837 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200838 sa->sadb_sa_auth = (a && a->pfkey_supported) ?
839 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841 sa->sadb_sa_encrypt = 0;
842 BUG_ON(x->ealg && x->calg);
843 if (x->ealg) {
844 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200845 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
846 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
849 if (x->calg) {
850 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200851 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
852 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
855 sa->sadb_sa_flags = 0;
856 if (x->props.flags & XFRM_STATE_NOECN)
857 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
858 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
859 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700860 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
861 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 /* hard time */
864 if (hsc & 2) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900865 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 sizeof(struct sadb_lifetime));
867 lifetime->sadb_lifetime_len =
868 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
869 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
870 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
871 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
872 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
873 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
874 }
875 /* soft time */
876 if (hsc & 1) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900877 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 sizeof(struct sadb_lifetime));
879 lifetime->sadb_lifetime_len =
880 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
881 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
882 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
883 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
884 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
885 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
886 }
887 /* current time */
888 lifetime = (struct sadb_lifetime *) skb_put(skb,
889 sizeof(struct sadb_lifetime));
890 lifetime->sadb_lifetime_len =
891 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
892 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
893 lifetime->sadb_lifetime_allocations = x->curlft.packets;
894 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
895 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
896 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
897 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900898 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900900 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 (sizeof(struct sadb_address)+sockaddr_size)/
902 sizeof(uint64_t);
903 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900904 /* "if the ports are non-zero, then the sadb_address_proto field,
905 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900907 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900910 addr->sadb_address_prefixlen =
911 pfkey_sockaddr_fill(&x->props.saddr, 0,
912 (struct sockaddr *) (addr + 1),
913 x->props.family);
914 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 BUG();
916
917 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900918 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900920 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 (sizeof(struct sadb_address)+sockaddr_size)/
922 sizeof(uint64_t);
923 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900924 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900927 addr->sadb_address_prefixlen =
928 pfkey_sockaddr_fill(&x->id.daddr, 0,
929 (struct sockaddr *) (addr + 1),
930 x->props.family);
931 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 BUG();
933
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000934 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
935 x->props.family)) {
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900936 addr = (struct sadb_address*) skb_put(skb,
937 sizeof(struct sadb_address)+sockaddr_size);
938 addr->sadb_address_len =
939 (sizeof(struct sadb_address)+sockaddr_size)/
940 sizeof(uint64_t);
941 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
942 addr->sadb_address_proto =
943 pfkey_proto_from_xfrm(x->sel.proto);
944 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
945 addr->sadb_address_reserved = 0;
946
947 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
948 (struct sockaddr *) (addr + 1),
949 x->props.family);
950 }
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* auth key */
953 if (add_keys && auth_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900954 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 sizeof(struct sadb_key)+auth_key_size);
956 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
957 sizeof(uint64_t);
958 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
959 key->sadb_key_bits = x->aalg->alg_key_len;
960 key->sadb_key_reserved = 0;
961 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
962 }
963 /* encrypt key */
964 if (add_keys && encrypt_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900965 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 sizeof(struct sadb_key)+encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900967 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 encrypt_key_size) / sizeof(uint64_t);
969 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
970 key->sadb_key_bits = x->ealg->alg_key_len;
971 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900972 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 (x->ealg->alg_key_len+7)/8);
974 }
975
976 /* sa */
977 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
978 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
979 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700980 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
981 kfree_skb(skb);
982 return ERR_PTR(-EINVAL);
983 }
984 sa2->sadb_x_sa2_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 sa2->sadb_x_sa2_reserved1 = 0;
986 sa2->sadb_x_sa2_reserved2 = 0;
987 sa2->sadb_x_sa2_sequence = 0;
988 sa2->sadb_x_sa2_reqid = x->props.reqid;
989
990 if (natt && natt->encap_type) {
991 struct sadb_x_nat_t_type *n_type;
992 struct sadb_x_nat_t_port *n_port;
993
994 /* type */
995 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
996 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
997 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
998 n_type->sadb_x_nat_t_type_type = natt->encap_type;
999 n_type->sadb_x_nat_t_type_reserved[0] = 0;
1000 n_type->sadb_x_nat_t_type_reserved[1] = 0;
1001 n_type->sadb_x_nat_t_type_reserved[2] = 0;
1002
1003 /* source port */
1004 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
1005 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1006 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
1007 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
1008 n_port->sadb_x_nat_t_port_reserved = 0;
1009
1010 /* dest port */
1011 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
1012 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1013 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
1014 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
1015 n_port->sadb_x_nat_t_port_reserved = 0;
1016 }
1017
Trent Jaegerdf718372005-12-13 23:12:27 -08001018 /* security context */
1019 if (xfrm_ctx) {
1020 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1021 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1022 sec_ctx->sadb_x_sec_len =
1023 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1024 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1025 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1026 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1027 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1028 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1029 xfrm_ctx->ctx_len);
1030 }
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return skb;
1033}
1034
Herbert Xu050f0092007-10-09 13:31:47 -07001035
David S. Miller4c93fbb2011-02-25 18:07:06 -08001036static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
Herbert Xu050f0092007-10-09 13:31:47 -07001037{
1038 struct sk_buff *skb;
1039
Herbert Xu050f0092007-10-09 13:31:47 -07001040 skb = __pfkey_xfrm_state2msg(x, 1, 3);
Herbert Xu050f0092007-10-09 13:31:47 -07001041
1042 return skb;
1043}
1044
David S. Miller4c93fbb2011-02-25 18:07:06 -08001045static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -07001046 int hsc)
1047{
1048 return __pfkey_xfrm_state2msg(x, 0, hsc);
1049}
1050
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001051static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
David S. Miller4c93fbb2011-02-25 18:07:06 -08001052 const struct sadb_msg *hdr,
1053 void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001055 struct xfrm_state *x;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001056 const struct sadb_lifetime *lifetime;
1057 const struct sadb_sa *sa;
1058 const struct sadb_key *key;
1059 const struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 uint16_t proto;
1061 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Joe Perchesea110732011-06-13 16:21:26 +00001064 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (!sa ||
1066 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1067 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1068 return ERR_PTR(-EINVAL);
1069 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1070 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1071 return ERR_PTR(-EINVAL);
1072 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1073 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1074 return ERR_PTR(-EINVAL);
1075 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1076 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1077 return ERR_PTR(-EINVAL);
1078
1079 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1080 if (proto == 0)
1081 return ERR_PTR(-EINVAL);
1082
1083 /* default error is no buffer space */
1084 err = -ENOBUFS;
1085
1086 /* RFC2367:
1087
1088 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1089 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1090 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1091 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1092 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1093 not true.
1094
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001095 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1097 */
1098 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1099 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1100 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1101 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1102 return ERR_PTR(-EINVAL);
Joe Perchesea110732011-06-13 16:21:26 +00001103 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (key != NULL &&
1105 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1106 ((key->sadb_key_bits+7) / 8 == 0 ||
1107 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1108 return ERR_PTR(-EINVAL);
1109 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1110 if (key != NULL &&
1111 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1112 ((key->sadb_key_bits+7) / 8 == 0 ||
1113 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1114 return ERR_PTR(-EINVAL);
1115
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001116 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (x == NULL)
1118 return ERR_PTR(-ENOBUFS);
1119
1120 x->id.proto = proto;
1121 x->id.spi = sa->sadb_sa_spi;
Fan Du33fce602013-09-17 15:14:13 +08001122 x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay,
1123 (sizeof(x->replay.bitmap) * 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1125 x->props.flags |= XFRM_STATE_NOECN;
1126 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1127 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001128 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1129 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Joe Perchesea110732011-06-13 16:21:26 +00001131 lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (lifetime != NULL) {
1133 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1134 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1135 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1136 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1137 }
Joe Perchesea110732011-06-13 16:21:26 +00001138 lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (lifetime != NULL) {
1140 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1141 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1142 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1143 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1144 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001145
Joe Perchesea110732011-06-13 16:21:26 +00001146 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001147 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01001148 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001149
1150 if (!uctx)
1151 goto out;
1152
1153 err = security_xfrm_state_alloc(x, uctx);
1154 kfree(uctx);
1155
1156 if (err)
1157 goto out;
1158 }
1159
Joe Perchesea110732011-06-13 16:21:26 +00001160 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (sa->sadb_sa_auth) {
1162 int keysize = 0;
1163 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001164 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 err = -ENOSYS;
1166 goto out;
1167 }
1168 if (key)
1169 keysize = (key->sadb_key_bits + 7) / 8;
1170 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1171 if (!x->aalg)
1172 goto out;
1173 strcpy(x->aalg->alg_name, a->name);
1174 x->aalg->alg_key_len = 0;
1175 if (key) {
1176 x->aalg->alg_key_len = key->sadb_key_bits;
1177 memcpy(x->aalg->alg_key, key+1, keysize);
1178 }
Martin Willic20a66f2009-12-09 06:11:15 +00001179 x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 x->props.aalgo = sa->sadb_sa_auth;
1181 /* x->algo.flags = sa->sadb_sa_flags; */
1182 }
1183 if (sa->sadb_sa_encrypt) {
1184 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1185 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001186 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 err = -ENOSYS;
1188 goto out;
1189 }
1190 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1191 if (!x->calg)
1192 goto out;
1193 strcpy(x->calg->alg_name, a->name);
1194 x->props.calgo = sa->sadb_sa_encrypt;
1195 } else {
1196 int keysize = 0;
1197 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001198 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 err = -ENOSYS;
1200 goto out;
1201 }
1202 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1203 if (key)
1204 keysize = (key->sadb_key_bits + 7) / 8;
1205 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1206 if (!x->ealg)
1207 goto out;
1208 strcpy(x->ealg->alg_name, a->name);
1209 x->ealg->alg_key_len = 0;
1210 if (key) {
1211 x->ealg->alg_key_len = key->sadb_key_bits;
1212 memcpy(x->ealg->alg_key, key+1, keysize);
1213 }
1214 x->props.ealgo = sa->sadb_sa_encrypt;
Herbert Xu69b01372015-05-27 16:03:45 +08001215 x->geniv = a->uinfo.encr.geniv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217 }
1218 /* x->algo.flags = sa->sadb_sa_flags; */
1219
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001220 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 &x->props.saddr);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001222 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 &x->id.daddr);
1224
1225 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001226 const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001227 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1228 if (mode < 0) {
1229 err = -EINVAL;
1230 goto out;
1231 }
1232 x->props.mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 x->props.reqid = sa2->sadb_x_sa2_reqid;
1234 }
1235
1236 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001237 const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 /* Nobody uses this, but we try. */
1240 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1241 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1242 }
1243
Kazunori MIYAZAWA4da51052008-05-21 13:26:11 -07001244 if (!x->sel.family)
Joy Latten4a4b6272007-08-02 19:25:43 -07001245 x->sel.family = x->props.family;
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001248 const struct sadb_x_nat_t_type* n_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct xfrm_encap_tmpl *natt;
1250
1251 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1252 if (!x->encap)
1253 goto out;
1254
1255 natt = x->encap;
1256 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1257 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1258
1259 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001260 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1262 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1263 }
1264 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001265 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1267 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1268 }
Timo Terasa8d694c2009-01-25 20:49:14 -08001269 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271
Herbert Xu72cb6962005-06-20 13:18:08 -07001272 err = xfrm_init_state(x);
1273 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return x;
1278
1279out:
1280 x->km.state = XFRM_STATE_DEAD;
1281 xfrm_state_put(x);
1282 return ERR_PTR(err);
1283}
1284
David S. Miller4c93fbb2011-02-25 18:07:06 -08001285static 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 -07001286{
1287 return -EOPNOTSUPP;
1288}
1289
David S. Miller4c93fbb2011-02-25 18:07:06 -08001290static 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 -07001291{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001292 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct sk_buff *resp_skb;
1294 struct sadb_x_sa2 *sa2;
1295 struct sadb_address *saddr, *daddr;
1296 struct sadb_msg *out_hdr;
Herbert Xu658b2192007-10-09 13:29:52 -07001297 struct sadb_spirange *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 struct xfrm_state *x = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001299 int mode;
Herbert Xu658b2192007-10-09 13:29:52 -07001300 int err;
1301 u32 min_spi, max_spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 u32 reqid;
1303 u8 proto;
1304 unsigned short family;
1305 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1306
1307 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1308 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1309 return -EINVAL;
1310
1311 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1312 if (proto == 0)
1313 return -EINVAL;
1314
1315 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001316 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1317 if (mode < 0)
1318 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 reqid = sa2->sadb_x_sa2_reqid;
1320 } else {
1321 mode = 0;
1322 reqid = 0;
1323 }
1324
1325 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1326 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1327
1328 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1329 switch (family) {
1330 case AF_INET:
1331 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1332 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1333 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001334#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 case AF_INET6:
1336 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1337 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1338 break;
1339#endif
1340 }
1341
1342 if (hdr->sadb_msg_seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001343 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001344 if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 xfrm_state_put(x);
1346 x = NULL;
1347 }
1348 }
1349
1350 if (!x)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001351 x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (x == NULL)
1354 return -ENOENT;
1355
Herbert Xu658b2192007-10-09 13:29:52 -07001356 min_spi = 0x100;
1357 max_spi = 0x0fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Herbert Xu658b2192007-10-09 13:29:52 -07001359 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1360 if (range) {
1361 min_spi = range->sadb_spirange_min;
1362 max_spi = range->sadb_spirange_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
Herbert Xu658b2192007-10-09 13:29:52 -07001364
Fan Du776e9dd2013-12-16 18:47:49 +08001365 err = verify_spi_info(x->id.proto, min_spi, max_spi);
1366 if (err) {
1367 xfrm_state_put(x);
1368 return err;
1369 }
1370
Herbert Xu658b2192007-10-09 13:29:52 -07001371 err = xfrm_alloc_spi(x, min_spi, max_spi);
Herbert Xu050f0092007-10-09 13:31:47 -07001372 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 if (IS_ERR(resp_skb)) {
1375 xfrm_state_put(x);
1376 return PTR_ERR(resp_skb);
1377 }
1378
1379 out_hdr = (struct sadb_msg *) resp_skb->data;
1380 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1381 out_hdr->sadb_msg_type = SADB_GETSPI;
1382 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1383 out_hdr->sadb_msg_errno = 0;
1384 out_hdr->sadb_msg_reserved = 0;
1385 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1386 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1387
1388 xfrm_state_put(x);
1389
David Ahernba51b6b2015-08-24 15:17:17 -06001390 pfkey_broadcast(resp_skb, BROADCAST_ONE, sk, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 return 0;
1393}
1394
David S. Miller4c93fbb2011-02-25 18:07:06 -08001395static 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 -07001396{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001397 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 struct xfrm_state *x;
1399
1400 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1401 return -EOPNOTSUPP;
1402
1403 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1404 return 0;
1405
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001406 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (x == NULL)
1408 return 0;
1409
1410 spin_lock_bh(&x->lock);
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001411 if (x->km.state == XFRM_STATE_ACQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 x->km.state = XFRM_STATE_ERROR;
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 spin_unlock_bh(&x->lock);
1415 xfrm_state_put(x);
1416 return 0;
1417}
1418
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001419static inline int event2poltype(int event)
1420{
1421 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001422 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001423 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001424 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001425 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001426 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001427 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001428 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001429 // return SADB_X_SPDEXPIRE;
1430 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001431 pr_err("pfkey: Unknown policy event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001432 break;
1433 }
1434
1435 return 0;
1436}
1437
1438static inline int event2keytype(int event)
1439{
1440 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001441 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001442 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001443 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001444 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001445 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001446 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001447 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001448 return SADB_EXPIRE;
1449 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001450 pr_err("pfkey: Unknown SA event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001451 break;
1452 }
1453
1454 return 0;
1455}
1456
1457/* ADD/UPD/DEL */
David S. Miller214e0052011-02-24 00:02:38 -05001458static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001459{
1460 struct sk_buff *skb;
1461 struct sadb_msg *hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001462
Herbert Xu050f0092007-10-09 13:31:47 -07001463 skb = pfkey_xfrm_state2msg(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001464
1465 if (IS_ERR(skb))
1466 return PTR_ERR(skb);
1467
1468 hdr = (struct sadb_msg *) skb->data;
1469 hdr->sadb_msg_version = PF_KEY_V2;
1470 hdr->sadb_msg_type = event2keytype(c->event);
1471 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1472 hdr->sadb_msg_errno = 0;
1473 hdr->sadb_msg_reserved = 0;
1474 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001475 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001476
David Ahernba51b6b2015-08-24 15:17:17 -06001477 pfkey_broadcast(skb, BROADCAST_ALL, NULL, xs_net(x));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001478
1479 return 0;
1480}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
David S. Miller4c93fbb2011-02-25 18:07:06 -08001482static 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 -07001483{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001484 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 struct xfrm_state *x;
1486 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001487 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001489 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (IS_ERR(x))
1491 return PTR_ERR(x);
1492
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001493 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (hdr->sadb_msg_type == SADB_ADD)
1495 err = xfrm_state_add(x);
1496 else
1497 err = xfrm_state_update(x);
1498
Tetsuo Handa2e710292014-04-22 21:48:30 +09001499 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (err < 0) {
1502 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001503 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001504 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001507 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001508 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001509 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001510 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001511 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001512 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001513 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001514out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001515 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001516 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
David S. Miller4c93fbb2011-02-25 18:07:06 -08001519static 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 -07001520{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001521 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001523 struct km_event c;
1524 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 if (!ext_hdrs[SADB_EXT_SA-1] ||
1527 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1528 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1529 return -EINVAL;
1530
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001531 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (x == NULL)
1533 return -ESRCH;
1534
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001535 if ((err = security_xfrm_state_delete(x)))
1536 goto out;
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001539 err = -EPERM;
1540 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
Joy Latten161a09e2006-11-27 13:11:54 -06001542
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001543 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001544
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001545 if (err < 0)
1546 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001547
1548 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001549 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001550 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001551 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001552out:
Tetsuo Handa2e710292014-04-22 21:48:30 +09001553 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 xfrm_state_put(x);
1555
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001556 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
David S. Miller4c93fbb2011-02-25 18:07:06 -08001559static 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 -07001560{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001561 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 __u8 proto;
1563 struct sk_buff *out_skb;
1564 struct sadb_msg *out_hdr;
1565 struct xfrm_state *x;
1566
1567 if (!ext_hdrs[SADB_EXT_SA-1] ||
1568 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1569 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1570 return -EINVAL;
1571
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001572 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (x == NULL)
1574 return -ESRCH;
1575
Herbert Xu050f0092007-10-09 13:31:47 -07001576 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 proto = x->id.proto;
1578 xfrm_state_put(x);
1579 if (IS_ERR(out_skb))
1580 return PTR_ERR(out_skb);
1581
1582 out_hdr = (struct sadb_msg *) out_skb->data;
1583 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Charles Hardin435000b2007-11-22 19:35:15 +08001584 out_hdr->sadb_msg_type = SADB_GET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1586 out_hdr->sadb_msg_errno = 0;
1587 out_hdr->sadb_msg_reserved = 0;
1588 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1589 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
David Ahernba51b6b2015-08-24 15:17:17 -06001590 pfkey_broadcast(out_skb, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 return 0;
1593}
1594
David S. Miller4c93fbb2011-02-25 18:07:06 -08001595static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001596 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 struct sk_buff *skb;
1599 struct sadb_msg *hdr;
1600 int len, auth_len, enc_len, i;
1601
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001602 auth_len = xfrm_count_pfkey_auth_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (auth_len) {
1604 auth_len *= sizeof(struct sadb_alg);
1605 auth_len += sizeof(struct sadb_supported);
1606 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001607
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001608 enc_len = xfrm_count_pfkey_enc_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (enc_len) {
1610 enc_len *= sizeof(struct sadb_alg);
1611 enc_len += sizeof(struct sadb_supported);
1612 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 len = enc_len + auth_len + sizeof(struct sadb_msg);
1615
1616 skb = alloc_skb(len + 16, allocation);
1617 if (!skb)
1618 goto out_put_algs;
1619
1620 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1621 pfkey_hdr_dup(hdr, orig);
1622 hdr->sadb_msg_errno = 0;
1623 hdr->sadb_msg_len = len / sizeof(uint64_t);
1624
1625 if (auth_len) {
1626 struct sadb_supported *sp;
1627 struct sadb_alg *ap;
1628
1629 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1630 ap = (struct sadb_alg *) (sp + 1);
1631
1632 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1633 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1634
1635 for (i = 0; ; i++) {
1636 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1637 if (!aalg)
1638 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001639 if (!aalg->pfkey_supported)
1640 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (aalg->available)
1642 *ap++ = aalg->desc;
1643 }
1644 }
1645
1646 if (enc_len) {
1647 struct sadb_supported *sp;
1648 struct sadb_alg *ap;
1649
1650 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1651 ap = (struct sadb_alg *) (sp + 1);
1652
1653 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1654 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1655
1656 for (i = 0; ; i++) {
1657 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1658 if (!ealg)
1659 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001660 if (!ealg->pfkey_supported)
1661 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (ealg->available)
1663 *ap++ = ealg->desc;
1664 }
1665 }
1666
1667out_put_algs:
1668 return skb;
1669}
1670
David S. Miller4c93fbb2011-02-25 18:07:06 -08001671static 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 -07001672{
1673 struct pfkey_sock *pfk = pfkey_sk(sk);
1674 struct sk_buff *supp_skb;
1675
1676 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1677 return -EINVAL;
1678
1679 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1680 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1681 return -EEXIST;
1682 pfk->registered |= (1<<hdr->sadb_msg_satype);
1683 }
1684
1685 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1688 if (!supp_skb) {
1689 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1690 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1691
1692 return -ENOBUFS;
1693 }
1694
David Ahernba51b6b2015-08-24 15:17:17 -06001695 pfkey_broadcast(supp_skb, BROADCAST_REGISTERED, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 return 0;
1698}
1699
David S. Miller4c93fbb2011-02-25 18:07:06 -08001700static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001701{
1702 struct sk_buff *skb;
1703 struct sadb_msg *hdr;
1704
1705 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1706 if (!skb)
1707 return -ENOBUFS;
1708
1709 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1710 memcpy(hdr, ihdr, sizeof(struct sadb_msg));
1711 hdr->sadb_msg_errno = (uint8_t) 0;
1712 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1713
David Ahernba51b6b2015-08-24 15:17:17 -06001714 return pfkey_broadcast(skb, BROADCAST_ONE, sk, sock_net(sk));
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001715}
1716
David S. Miller214e0052011-02-24 00:02:38 -05001717static int key_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001718{
1719 struct sk_buff *skb;
1720 struct sadb_msg *hdr;
1721
1722 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1723 if (!skb)
1724 return -ENOBUFS;
1725 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf088672005-06-18 22:44:00 -07001726 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001727 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001728 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001729 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001730 hdr->sadb_msg_version = PF_KEY_V2;
1731 hdr->sadb_msg_errno = (uint8_t) 0;
1732 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02001733 hdr->sadb_msg_reserved = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001734
David Ahernba51b6b2015-08-24 15:17:17 -06001735 pfkey_broadcast(skb, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001736
1737 return 0;
1738}
1739
David S. Miller4c93fbb2011-02-25 18:07:06 -08001740static 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 -07001741{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001742 struct net *net = sock_net(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +00001743 unsigned int proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001744 struct km_event c;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001745 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1748 if (proto == 0)
1749 return -EINVAL;
1750
Tetsuo Handa2e710292014-04-22 21:48:30 +09001751 err = xfrm_state_flush(net, proto, true);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001752 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001753 if (err || err2) {
1754 if (err == -ESRCH) /* empty table - go quietly */
1755 err = 0;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001756 return err ? err : err2;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001757 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001758
Herbert Xubf088672005-06-18 22:44:00 -07001759 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001760 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001761 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001762 c.event = XFRM_MSG_FLUSHSA;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001763 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001764 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 return 0;
1767}
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1770{
Timo Teras83321d62008-03-03 23:40:12 -08001771 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 struct sk_buff *out_skb;
1773 struct sadb_msg *out_hdr;
1774
Timo Teras83321d62008-03-03 23:40:12 -08001775 if (!pfkey_can_dump(&pfk->sk))
1776 return -ENOBUFS;
1777
Herbert Xu050f0092007-10-09 13:31:47 -07001778 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (IS_ERR(out_skb))
1780 return PTR_ERR(out_skb);
1781
1782 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08001783 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 out_hdr->sadb_msg_type = SADB_DUMP;
1785 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1786 out_hdr->sadb_msg_errno = 0;
1787 out_hdr->sadb_msg_reserved = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07001788 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001789 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07001790
1791 if (pfk->dump.skb)
David Ahernba51b6b2015-08-24 15:17:17 -06001792 pfkey_broadcast(pfk->dump.skb, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001793 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07001794 pfk->dump.skb = out_skb;
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return 0;
1797}
1798
Timo Teras83321d62008-03-03 23:40:12 -08001799static int pfkey_dump_sa(struct pfkey_sock *pfk)
1800{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001801 struct net *net = sock_net(&pfk->sk);
1802 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08001803}
1804
1805static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1806{
Fan Du283bc9f2013-11-07 17:47:50 +08001807 struct net *net = sock_net(&pfk->sk);
1808
1809 xfrm_state_walk_done(&pfk->dump.u.state, net);
Timo Teras83321d62008-03-03 23:40:12 -08001810}
1811
David S. Miller4c93fbb2011-02-25 18:07:06 -08001812static 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 -07001813{
1814 u8 proto;
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001815 struct xfrm_address_filter *filter = NULL;
Timo Teras83321d62008-03-03 23:40:12 -08001816 struct pfkey_sock *pfk = pfkey_sk(sk);
1817
Yuejie Shi89e357d2017-03-31 15:10:20 +08001818 mutex_lock(&pfk->dump_lock);
1819 if (pfk->dump.dump != NULL) {
1820 mutex_unlock(&pfk->dump_lock);
Timo Teras83321d62008-03-03 23:40:12 -08001821 return -EBUSY;
Yuejie Shi89e357d2017-03-31 15:10:20 +08001822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
Yuejie Shi89e357d2017-03-31 15:10:20 +08001825 if (proto == 0) {
1826 mutex_unlock(&pfk->dump_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return -EINVAL;
Yuejie Shi89e357d2017-03-31 15:10:20 +08001828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Nicolas Dichteld3623092014-02-14 15:30:36 +01001830 if (ext_hdrs[SADB_X_EXT_FILTER - 1]) {
1831 struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1];
1832
1833 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
Yuejie Shi89e357d2017-03-31 15:10:20 +08001834 if (filter == NULL) {
1835 mutex_unlock(&pfk->dump_lock);
Nicolas Dichteld3623092014-02-14 15:30:36 +01001836 return -ENOMEM;
Yuejie Shi89e357d2017-03-31 15:10:20 +08001837 }
Nicolas Dichteld3623092014-02-14 15:30:36 +01001838
1839 memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr,
1840 sizeof(xfrm_address_t));
1841 memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr,
1842 sizeof(xfrm_address_t));
1843 filter->family = xfilter->sadb_x_filter_family;
1844 filter->splen = xfilter->sadb_x_filter_splen;
1845 filter->dplen = xfilter->sadb_x_filter_dplen;
1846 }
1847
Timo Teras83321d62008-03-03 23:40:12 -08001848 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001849 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08001850 pfk->dump.dump = pfkey_dump_sa;
1851 pfk->dump.done = pfkey_dump_sa_done;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001852 xfrm_state_walk_init(&pfk->dump.u.state, proto, filter);
Yuejie Shi89e357d2017-03-31 15:10:20 +08001853 mutex_unlock(&pfk->dump_lock);
Timo Teras4c563f72008-02-28 21:31:08 -08001854
Timo Teras83321d62008-03-03 23:40:12 -08001855 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
David S. Miller4c93fbb2011-02-25 18:07:06 -08001858static 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 -07001859{
1860 struct pfkey_sock *pfk = pfkey_sk(sk);
1861 int satype = hdr->sadb_msg_satype;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001862 bool reset_errno = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001865 reset_errno = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (satype != 0 && satype != 1)
1867 return -EINVAL;
1868 pfk->promisc = satype;
1869 }
David S. Miller4c93fbb2011-02-25 18:07:06 -08001870 if (reset_errno && skb_cloned(skb))
1871 skb = skb_copy(skb, GFP_KERNEL);
1872 else
1873 skb = skb_clone(skb, GFP_KERNEL);
1874
1875 if (reset_errno && skb) {
1876 struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
1877 new_hdr->sadb_msg_errno = 0;
1878 }
1879
David Ahernba51b6b2015-08-24 15:17:17 -06001880 pfkey_broadcast(skb, BROADCAST_ALL, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return 0;
1882}
1883
1884static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1885{
1886 int i;
1887 u32 reqid = *(u32*)ptr;
1888
1889 for (i=0; i<xp->xfrm_nr; i++) {
1890 if (xp->xfrm_vec[i].reqid == reqid)
1891 return -EEXIST;
1892 }
1893 return 0;
1894}
1895
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001896static u32 gen_reqid(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Timo Teras4c563f72008-02-28 21:31:08 -08001898 struct xfrm_policy_walk walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 u32 start;
Timo Teras4c563f72008-02-28 21:31:08 -08001900 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1902
1903 start = reqid;
1904 do {
1905 ++reqid;
1906 if (reqid == 0)
1907 reqid = IPSEC_MANUAL_REQID_MAX+1;
Timo Teras4c563f72008-02-28 21:31:08 -08001908 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001909 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
Fan Du283bc9f2013-11-07 17:47:50 +08001910 xfrm_policy_walk_done(&walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001911 if (rc != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return reqid;
1913 } while (reqid != start);
1914 return 0;
1915}
1916
1917static int
1918parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1919{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001920 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001922 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1925 return -ELOOP;
1926
1927 if (rq->sadb_x_ipsecrequest_mode == 0)
1928 return -EINVAL;
1929
1930 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001931 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1932 return -EINVAL;
1933 t->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1935 t->optional = 1;
1936 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1937 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1938 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1939 t->reqid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001940 if (!t->reqid && !(t->reqid = gen_reqid(net)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return -ENOBUFS;
1942 }
1943
1944 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001945 if (t->mode == XFRM_MODE_TUNNEL) {
Herbert Xu096f41d2017-04-13 18:35:59 +08001946 int err;
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001947
Herbert Xu096f41d2017-04-13 18:35:59 +08001948 err = parse_sockaddr_pair(
1949 (struct sockaddr *)(rq + 1),
1950 rq->sadb_x_ipsecrequest_len - sizeof(*rq),
1951 &t->saddr, &t->id.daddr, &t->encap_family);
1952 if (err)
1953 return err;
Miika Komu2718aa72006-11-30 16:41:50 -08001954 } else
1955 t->encap_family = xp->family;
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 /* No way to set this via kame pfkey */
Herbert Xuc5d18e92008-04-22 00:46:42 -07001958 t->allalgs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 xp->xfrm_nr++;
1960 return 0;
1961}
1962
1963static int
1964parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1965{
1966 int err;
1967 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1968 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1969
Dan Carpenterf674e722012-09-27 22:21:19 +00001970 if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
1971 return -EINVAL;
1972
Herbert Xu096f41d2017-04-13 18:35:59 +08001973 while (len >= sizeof(*rq)) {
1974 if (len < rq->sadb_x_ipsecrequest_len ||
1975 rq->sadb_x_ipsecrequest_len < sizeof(*rq))
1976 return -EINVAL;
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1979 return err;
1980 len -= rq->sadb_x_ipsecrequest_len;
1981 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1982 }
1983 return 0;
1984}
1985
David S. Miller4c93fbb2011-02-25 18:07:06 -08001986static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
Trent Jaegerdf718372005-12-13 23:12:27 -08001987{
1988 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1989
1990 if (xfrm_ctx) {
1991 int len = sizeof(struct sadb_x_sec_ctx);
1992 len += xfrm_ctx->ctx_len;
1993 return PFKEY_ALIGN8(len);
1994 }
1995 return 0;
1996}
1997
David S. Miller4c93fbb2011-02-25 18:07:06 -08001998static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
David S. Miller4c93fbb2011-02-25 18:07:06 -08002000 const struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08002002 int socklen = 0;
2003 int i;
2004
2005 for (i=0; i<xp->xfrm_nr; i++) {
2006 t = xp->xfrm_vec + i;
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002007 socklen += pfkey_sockaddr_len(t->encap_family);
Miika Komu2718aa72006-11-30 16:41:50 -08002008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 return sizeof(struct sadb_msg) +
2011 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002012 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 (sockaddr_size * 2) +
2014 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08002015 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
2016 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08002017 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019
David S. Miller4c93fbb2011-02-25 18:07:06 -08002020static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
2022 struct sk_buff *skb;
2023 int size;
2024
2025 size = pfkey_xfrm_policy2msg_size(xp);
2026
2027 skb = alloc_skb(size + 16, GFP_ATOMIC);
2028 if (skb == NULL)
2029 return ERR_PTR(-ENOBUFS);
2030
2031 return skb;
2032}
2033
David S. Miller4c93fbb2011-02-25 18:07:06 -08002034static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
2036 struct sadb_msg *hdr;
2037 struct sadb_address *addr;
2038 struct sadb_lifetime *lifetime;
2039 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08002040 struct sadb_x_sec_ctx *sec_ctx;
2041 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 int i;
2043 int size;
2044 int sockaddr_size = pfkey_sockaddr_size(xp->family);
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002045 int socklen = pfkey_sockaddr_len(xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 size = pfkey_xfrm_policy2msg_size(xp);
2048
2049 /* call should fill header later */
2050 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2051 memset(hdr, 0, size); /* XXX do we need this ? */
2052
2053 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002054 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002056 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 (sizeof(struct sadb_address)+sockaddr_size)/
2058 sizeof(uint64_t);
2059 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2060 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2061 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2062 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002063 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2064 xp->selector.sport,
2065 (struct sockaddr *) (addr + 1),
2066 xp->family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 BUG();
2068
2069 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002070 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 sizeof(struct sadb_address)+sockaddr_size);
2072 addr->sadb_address_len =
2073 (sizeof(struct sadb_address)+sockaddr_size)/
2074 sizeof(uint64_t);
2075 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2076 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002077 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002079
2080 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2081 (struct sockaddr *) (addr + 1),
2082 xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 /* hard time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002085 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 sizeof(struct sadb_lifetime));
2087 lifetime->sadb_lifetime_len =
2088 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2089 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2090 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2091 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2092 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2093 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2094 /* soft time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002095 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 sizeof(struct sadb_lifetime));
2097 lifetime->sadb_lifetime_len =
2098 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2099 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2100 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2101 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2102 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2103 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2104 /* current time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002105 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 sizeof(struct sadb_lifetime));
2107 lifetime->sadb_lifetime_len =
2108 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2109 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2110 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2111 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2112 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2113 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2114
2115 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2116 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2117 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2118 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2119 if (xp->action == XFRM_POLICY_ALLOW) {
2120 if (xp->xfrm_nr)
2121 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2122 else
2123 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2124 }
2125 pol->sadb_x_policy_dir = dir+1;
Dan Carpenterff862a42013-07-28 23:04:45 +03002126 pol->sadb_x_policy_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 pol->sadb_x_policy_id = xp->index;
2128 pol->sadb_x_policy_priority = xp->priority;
2129
2130 for (i=0; i<xp->xfrm_nr; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002131 const struct xfrm_tmpl *t = xp->xfrm_vec + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 struct sadb_x_ipsecrequest *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 int req_size;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002134 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 req_size = sizeof(struct sadb_x_ipsecrequest);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002137 if (t->mode == XFRM_MODE_TUNNEL) {
2138 socklen = pfkey_sockaddr_len(t->encap_family);
2139 req_size += socklen * 2;
2140 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 size -= 2*socklen;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 rq = (void*)skb_put(skb, req_size);
2144 pol->sadb_x_policy_len += req_size/8;
2145 memset(rq, 0, sizeof(*rq));
2146 rq->sadb_x_ipsecrequest_len = req_size;
2147 rq->sadb_x_ipsecrequest_proto = t->id.proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002148 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2149 return -EINVAL;
David S. Millerfefaa752007-04-17 21:48:10 -07002150 rq->sadb_x_ipsecrequest_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2152 if (t->reqid)
2153 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2154 if (t->optional)
2155 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2156 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002158 if (t->mode == XFRM_MODE_TUNNEL) {
2159 u8 *sa = (void *)(rq + 1);
2160 pfkey_sockaddr_fill(&t->saddr, 0,
2161 (struct sockaddr *)sa,
2162 t->encap_family);
2163 pfkey_sockaddr_fill(&t->id.daddr, 0,
2164 (struct sockaddr *) (sa + socklen),
2165 t->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
2167 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002168
2169 /* security context */
2170 if ((xfrm_ctx = xp->security)) {
2171 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2172
2173 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2174 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2175 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2176 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2177 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2178 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2179 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2180 xfrm_ctx->ctx_len);
2181 }
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 hdr->sadb_msg_len = size / sizeof(uint64_t);
2184 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002185
2186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
David S. Miller214e0052011-02-24 00:02:38 -05002189static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002190{
2191 struct sk_buff *out_skb;
2192 struct sadb_msg *out_hdr;
2193 int err;
2194
2195 out_skb = pfkey_xfrm_policy2msg_prep(xp);
Dan Carpenter9a127aa2010-03-24 01:47:00 +00002196 if (IS_ERR(out_skb))
2197 return PTR_ERR(out_skb);
2198
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002199 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2200 if (err < 0)
2201 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002202
2203 out_hdr = (struct sadb_msg *) out_skb->data;
2204 out_hdr->sadb_msg_version = PF_KEY_V2;
2205
Herbert Xuf60f6b82005-06-18 22:44:37 -07002206 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002207 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2208 else
2209 out_hdr->sadb_msg_type = event2poltype(c->event);
2210 out_hdr->sadb_msg_errno = 0;
2211 out_hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002212 out_hdr->sadb_msg_pid = c->portid;
David Ahernba51b6b2015-08-24 15:17:17 -06002213 pfkey_broadcast(out_skb, BROADCAST_ALL, NULL, xp_net(xp));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002214 return 0;
2215
2216}
2217
David S. Miller4c93fbb2011-02-25 18:07:06 -08002218static 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 -07002219{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002220 struct net *net = sock_net(sk);
Trent Jaegerdf718372005-12-13 23:12:27 -08002221 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 struct sadb_lifetime *lifetime;
2223 struct sadb_address *sa;
2224 struct sadb_x_policy *pol;
2225 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002226 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002227 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2230 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2231 !ext_hdrs[SADB_X_EXT_POLICY-1])
2232 return -EINVAL;
2233
2234 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2235 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2236 return -EINVAL;
2237 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2238 return -EINVAL;
2239
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002240 xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (xp == NULL)
2242 return -ENOBUFS;
2243
2244 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2245 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2246 xp->priority = pol->sadb_x_policy_priority;
2247
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002248 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 xp->selector.family = xp->family;
2251 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2252 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2253 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2254 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002255 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002257 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2259 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2260
2261 /* Amusing, we set this twice. KAME apps appear to set same value
2262 * in both addresses.
2263 */
2264 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2265
2266 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2267 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002268 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Joe Perchesea110732011-06-13 16:21:26 +00002270 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002271 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01002272 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002273
2274 if (!uctx) {
2275 err = -ENOBUFS;
2276 goto out;
2277 }
2278
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002279 err = security_xfrm_policy_alloc(&xp->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002280 kfree(uctx);
2281
2282 if (err)
2283 goto out;
2284 }
2285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 xp->lft.soft_byte_limit = XFRM_INF;
2287 xp->lft.hard_byte_limit = XFRM_INF;
2288 xp->lft.soft_packet_limit = XFRM_INF;
2289 xp->lft.hard_packet_limit = XFRM_INF;
2290 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2291 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2292 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2293 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2294 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2295 }
2296 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2297 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2298 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2299 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2300 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2301 }
2302 xp->xfrm_nr = 0;
2303 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2304 (err = parse_ipsecrequests(xp, pol)) < 0)
2305 goto out;
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2308 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002309
Tetsuo Handa2e710292014-04-22 21:48:30 +09002310 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002311
Trent Jaegerdf718372005-12-13 23:12:27 -08002312 if (err)
2313 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002315 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002316 c.event = XFRM_MSG_UPDPOLICY;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002317 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07002318 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002320 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002321 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002322
2323 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return 0;
2326
2327out:
Herbert Xu12a169e2008-10-01 07:03:24 -07002328 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08002329 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return err;
2331}
2332
David S. Miller4c93fbb2011-02-25 18:07:06 -08002333static 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 -07002334{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002335 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 int err;
2337 struct sadb_address *sa;
2338 struct sadb_x_policy *pol;
Paul Moore03e1ad72008-04-12 19:07:52 -07002339 struct xfrm_policy *xp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002341 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002342 struct sadb_x_sec_ctx *sec_ctx;
Brian Haley2db3e472008-04-24 20:38:31 -07002343 struct xfrm_sec_ctx *pol_ctx = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2346 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2347 !ext_hdrs[SADB_X_EXT_POLICY-1])
2348 return -EINVAL;
2349
2350 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2351 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2352 return -EINVAL;
2353
2354 memset(&sel, 0, sizeof(sel));
2355
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002356 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2358 sel.prefixlen_s = sa->sadb_address_prefixlen;
2359 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2360 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2361 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002362 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002364 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2366 sel.prefixlen_d = sa->sadb_address_prefixlen;
2367 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2368 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2369 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002370 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Joe Perchesea110732011-06-13 16:21:26 +00002372 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002373 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01002374 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002375
2376 if (!uctx)
2377 return -ENOMEM;
2378
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002379 err = security_xfrm_policy_alloc(&pol_ctx, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002380 kfree(uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002381 if (err)
2382 return err;
Brian Haley2db3e472008-04-24 20:38:31 -07002383 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002384
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002385 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Paul Moore03e1ad72008-04-12 19:07:52 -07002386 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2387 1, &err);
2388 security_xfrm_policy_free(pol_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (xp == NULL)
2390 return -ENOENT;
2391
Tetsuo Handa2e710292014-04-22 21:48:30 +09002392 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08002393
2394 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002395 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08002396
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002397 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002398 c.portid = hdr->sadb_msg_pid;
Tobias Brunner1839faa2008-10-10 14:07:03 -07002399 c.data.byid = 0;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002400 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002401 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2402
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002403out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002404 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00002405 if (err == 0)
2406 xfrm_garbage_collect(net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002407 return err;
2408}
2409
David S. Miller4c93fbb2011-02-25 18:07:06 -08002410static 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 -07002411{
2412 int err;
2413 struct sk_buff *out_skb;
2414 struct sadb_msg *out_hdr;
2415 err = 0;
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2418 if (IS_ERR(out_skb)) {
2419 err = PTR_ERR(out_skb);
2420 goto out;
2421 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002422 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2423 if (err < 0)
2424 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 out_hdr = (struct sadb_msg *) out_skb->data;
2427 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002428 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 out_hdr->sadb_msg_satype = 0;
2430 out_hdr->sadb_msg_errno = 0;
2431 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2432 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
David Ahernba51b6b2015-08-24 15:17:17 -06002433 pfkey_broadcast(out_skb, BROADCAST_ONE, sk, xp_net(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 err = 0;
2435
2436out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return err;
2438}
2439
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002440static int pfkey_sockaddr_pair_size(sa_family_t family)
2441{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002442 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002443}
2444
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002445static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002446 xfrm_address_t *saddr, xfrm_address_t *daddr,
2447 u16 *family)
2448{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002449 int af, socklen;
2450
Herbert Xu096f41d2017-04-13 18:35:59 +08002451 if (ext_len < 2 || ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002452 return -EINVAL;
2453
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002454 af = pfkey_sockaddr_extract(sa, saddr);
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002455 if (!af)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002456 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002457
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002458 socklen = pfkey_sockaddr_len(af);
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002459 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002460 daddr) != af)
2461 return -EINVAL;
2462
2463 *family = af;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002464 return 0;
2465}
2466
Herbert Xu096f41d2017-04-13 18:35:59 +08002467#ifdef CONFIG_NET_KEY_MIGRATE
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002468static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2469 struct xfrm_migrate *m)
2470{
2471 int err;
2472 struct sadb_x_ipsecrequest *rq2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002473 int mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002474
Herbert Xu096f41d2017-04-13 18:35:59 +08002475 if (len < sizeof(*rq1) ||
2476 len < rq1->sadb_x_ipsecrequest_len ||
2477 rq1->sadb_x_ipsecrequest_len < sizeof(*rq1))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002478 return -EINVAL;
2479
2480 /* old endoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002481 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
Herbert Xu096f41d2017-04-13 18:35:59 +08002482 rq1->sadb_x_ipsecrequest_len - sizeof(*rq1),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002483 &m->old_saddr, &m->old_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002484 &m->old_family);
2485 if (err)
2486 return err;
2487
2488 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2489 len -= rq1->sadb_x_ipsecrequest_len;
2490
Herbert Xu096f41d2017-04-13 18:35:59 +08002491 if (len <= sizeof(*rq2) ||
2492 len < rq2->sadb_x_ipsecrequest_len ||
2493 rq2->sadb_x_ipsecrequest_len < sizeof(*rq2))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002494 return -EINVAL;
2495
2496 /* new endpoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002497 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
Herbert Xu096f41d2017-04-13 18:35:59 +08002498 rq2->sadb_x_ipsecrequest_len - sizeof(*rq2),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002499 &m->new_saddr, &m->new_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002500 &m->new_family);
2501 if (err)
2502 return err;
2503
2504 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2505 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2506 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2507 return -EINVAL;
2508
2509 m->proto = rq1->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002510 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2511 return -EINVAL;
2512 m->mode = mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002513 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2514
2515 return ((int)(rq1->sadb_x_ipsecrequest_len +
2516 rq2->sadb_x_ipsecrequest_len));
2517}
2518
2519static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002520 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002521{
2522 int i, len, ret, err = -EINVAL;
2523 u8 dir;
2524 struct sadb_address *sa;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002525 struct sadb_x_kmaddress *kma;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002526 struct sadb_x_policy *pol;
2527 struct sadb_x_ipsecrequest *rq;
2528 struct xfrm_selector sel;
2529 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002530 struct xfrm_kmaddress k;
Fan Du8d549c42013-11-07 17:47:49 +08002531 struct net *net = sock_net(sk);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002532
2533 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002534 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002535 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2536 err = -EINVAL;
2537 goto out;
2538 }
2539
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002540 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002541 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002542
2543 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2544 err = -EINVAL;
2545 goto out;
2546 }
2547
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002548 if (kma) {
2549 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2550 k.reserved = kma->sadb_x_kmaddress_reserved;
2551 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2552 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2553 &k.local, &k.remote, &k.family);
2554 if (ret < 0) {
2555 err = ret;
2556 goto out;
2557 }
2558 }
2559
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002560 dir = pol->sadb_x_policy_dir - 1;
2561 memset(&sel, 0, sizeof(sel));
2562
2563 /* set source address info of selector */
2564 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2565 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2566 sel.prefixlen_s = sa->sadb_address_prefixlen;
2567 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2568 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2569 if (sel.sport)
Al Viro582ee432007-07-26 17:33:39 +01002570 sel.sport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002571
2572 /* set destination address info of selector */
Himangi Saraogi47162c02014-05-30 21:18:12 +05302573 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002574 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2575 sel.prefixlen_d = sa->sadb_address_prefixlen;
2576 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2577 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2578 if (sel.dport)
Al Viro582ee432007-07-26 17:33:39 +01002579 sel.dport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002580
2581 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2582
2583 /* extract ipsecrequests */
2584 i = 0;
2585 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2586
2587 while (len > 0 && i < XFRM_MAX_DEPTH) {
2588 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2589 if (ret < 0) {
2590 err = ret;
2591 goto out;
2592 } else {
2593 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2594 len -= ret;
2595 i++;
2596 }
2597 }
2598
2599 if (!i || len > 0) {
2600 err = -EINVAL;
2601 goto out;
2602 }
2603
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002604 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
Fan Du8d549c42013-11-07 17:47:49 +08002605 kma ? &k : NULL, net);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002606
2607 out:
2608 return err;
2609}
2610#else
2611static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
Stephen Hemminger7f6daa62011-03-01 22:51:52 -08002612 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002613{
2614 return -ENOPROTOOPT;
2615}
2616#endif
2617
2618
David S. Miller4c93fbb2011-02-25 18:07:06 -08002619static 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 -07002620{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002621 struct net *net = sock_net(sk);
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002622 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002623 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 struct sadb_x_policy *pol;
2625 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002626 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
2628 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2629 return -EINVAL;
2630
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002631 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2632 if (dir >= XFRM_POLICY_MAX)
2633 return -EINVAL;
2634
Eric Paris215a2dd2007-03-07 16:01:45 -08002635 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002636 xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002637 dir, pol->sadb_x_policy_id, delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if (xp == NULL)
2639 return -ENOENT;
2640
Eric Paris215a2dd2007-03-07 16:01:45 -08002641 if (delete) {
Tetsuo Handa2e710292014-04-22 21:48:30 +09002642 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Eric Paris215a2dd2007-03-07 16:01:45 -08002644 if (err)
2645 goto out;
2646 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002647 c.portid = hdr->sadb_msg_pid;
Herbert Xubf088672005-06-18 22:44:00 -07002648 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002649 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002650 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002651 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002652 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Eric Paris215a2dd2007-03-07 16:01:45 -08002655out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00002657 if (delete && err == 0)
2658 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 return err;
2660}
2661
2662static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2663{
Timo Teras83321d62008-03-03 23:40:12 -08002664 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 struct sk_buff *out_skb;
2666 struct sadb_msg *out_hdr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002667 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Timo Teras83321d62008-03-03 23:40:12 -08002669 if (!pfkey_can_dump(&pfk->sk))
2670 return -ENOBUFS;
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2673 if (IS_ERR(out_skb))
2674 return PTR_ERR(out_skb);
2675
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002676 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2677 if (err < 0)
2678 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08002681 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2683 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2684 out_hdr->sadb_msg_errno = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07002685 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002686 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07002687
2688 if (pfk->dump.skb)
David Ahernba51b6b2015-08-24 15:17:17 -06002689 pfkey_broadcast(pfk->dump.skb, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002690 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07002691 pfk->dump.skb = out_skb;
2692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 return 0;
2694}
2695
Timo Teras83321d62008-03-03 23:40:12 -08002696static int pfkey_dump_sp(struct pfkey_sock *pfk)
2697{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002698 struct net *net = sock_net(&pfk->sk);
2699 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08002700}
2701
2702static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2703{
Fan Du283bc9f2013-11-07 17:47:50 +08002704 struct net *net = sock_net((struct sock *)pfk);
2705
2706 xfrm_policy_walk_done(&pfk->dump.u.policy, net);
Timo Teras83321d62008-03-03 23:40:12 -08002707}
2708
David S. Miller4c93fbb2011-02-25 18:07:06 -08002709static 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 -07002710{
Timo Teras83321d62008-03-03 23:40:12 -08002711 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Yuejie Shi89e357d2017-03-31 15:10:20 +08002713 mutex_lock(&pfk->dump_lock);
2714 if (pfk->dump.dump != NULL) {
2715 mutex_unlock(&pfk->dump_lock);
Timo Teras83321d62008-03-03 23:40:12 -08002716 return -EBUSY;
Yuejie Shi89e357d2017-03-31 15:10:20 +08002717 }
Timo Teras4c563f72008-02-28 21:31:08 -08002718
Timo Teras83321d62008-03-03 23:40:12 -08002719 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002720 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08002721 pfk->dump.dump = pfkey_dump_sp;
2722 pfk->dump.done = pfkey_dump_sp_done;
2723 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
Yuejie Shi89e357d2017-03-31 15:10:20 +08002724 mutex_unlock(&pfk->dump_lock);
Timo Teras83321d62008-03-03 23:40:12 -08002725
2726 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
David S. Miller214e0052011-02-24 00:02:38 -05002729static int key_notify_policy_flush(const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
2731 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002732 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002734 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 if (!skb_out)
2736 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002737 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002738 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002739 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002740 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002741 hdr->sadb_msg_version = PF_KEY_V2;
2742 hdr->sadb_msg_errno = (uint8_t) 0;
Nicolas Dichtel85dfb742013-02-18 16:24:20 +01002743 hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002744 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02002745 hdr->sadb_msg_reserved = 0;
David Ahernba51b6b2015-08-24 15:17:17 -06002746 pfkey_broadcast(skb_out, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002747 return 0;
2748
2749}
2750
David S. Miller4c93fbb2011-02-25 18:07:06 -08002751static 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 -07002752{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002753 struct net *net = sock_net(sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002754 struct km_event c;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002755 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Tetsuo Handa2e710292014-04-22 21:48:30 +09002757 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002758 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002759 if (err || err2) {
2760 if (err == -ESRCH) /* empty table - old silent behavior */
2761 return 0;
2762 return err;
2763 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002764
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002765 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002766 c.event = XFRM_MSG_FLUSHPOLICY;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002767 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002768 c.seq = hdr->sadb_msg_seq;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002769 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002770 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 return 0;
2773}
2774
2775typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002776 const struct sadb_msg *hdr, void * const *ext_hdrs);
Mathias Krause8603b9552013-06-26 23:56:59 +02002777static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 [SADB_RESERVED] = pfkey_reserved,
2779 [SADB_GETSPI] = pfkey_getspi,
2780 [SADB_UPDATE] = pfkey_add,
2781 [SADB_ADD] = pfkey_add,
2782 [SADB_DELETE] = pfkey_delete,
2783 [SADB_GET] = pfkey_get,
2784 [SADB_ACQUIRE] = pfkey_acquire,
2785 [SADB_REGISTER] = pfkey_register,
2786 [SADB_EXPIRE] = NULL,
2787 [SADB_FLUSH] = pfkey_flush,
2788 [SADB_DUMP] = pfkey_dump,
2789 [SADB_X_PROMISC] = pfkey_promisc,
2790 [SADB_X_PCHANGE] = NULL,
2791 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2792 [SADB_X_SPDADD] = pfkey_spdadd,
2793 [SADB_X_SPDDELETE] = pfkey_spddelete,
2794 [SADB_X_SPDGET] = pfkey_spdget,
2795 [SADB_X_SPDACQUIRE] = NULL,
2796 [SADB_X_SPDDUMP] = pfkey_spddump,
2797 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2798 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2799 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002800 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801};
2802
David S. Miller4c93fbb2011-02-25 18:07:06 -08002803static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
2805 void *ext_hdrs[SADB_EXT_MAX];
2806 int err;
2807
David Ahernba51b6b2015-08-24 15:17:17 -06002808 pfkey_broadcast(skb_clone(skb, GFP_KERNEL),
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002809 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2812 err = parse_exthdrs(skb, hdr, ext_hdrs);
2813 if (!err) {
2814 err = -EOPNOTSUPP;
2815 if (pfkey_funcs[hdr->sadb_msg_type])
2816 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2817 }
2818 return err;
2819}
2820
2821static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2822{
2823 struct sadb_msg *hdr = NULL;
2824
2825 if (skb->len < sizeof(*hdr)) {
2826 *errp = -EMSGSIZE;
2827 } else {
2828 hdr = (struct sadb_msg *) skb->data;
2829 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2830 hdr->sadb_msg_reserved != 0 ||
2831 (hdr->sadb_msg_type <= SADB_RESERVED ||
2832 hdr->sadb_msg_type > SADB_MAX)) {
2833 hdr = NULL;
2834 *errp = -EINVAL;
2835 } else if (hdr->sadb_msg_len != (skb->len /
2836 sizeof(uint64_t)) ||
2837 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2838 sizeof(uint64_t))) {
2839 hdr = NULL;
2840 *errp = -EMSGSIZE;
2841 } else {
2842 *errp = 0;
2843 }
2844 }
2845 return hdr;
2846}
2847
David S. Miller4c93fbb2011-02-25 18:07:06 -08002848static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
2849 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Herbert Xuf3980352007-12-19 23:44:29 -08002851 unsigned int id = d->desc.sadb_alg_id;
2852
2853 if (id >= sizeof(t->aalgos) * 8)
2854 return 0;
2855
2856 return (t->aalgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857}
2858
David S. Miller4c93fbb2011-02-25 18:07:06 -08002859static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
2860 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
Herbert Xuf3980352007-12-19 23:44:29 -08002862 unsigned int id = d->desc.sadb_alg_id;
2863
2864 if (id >= sizeof(t->ealgos) * 8)
2865 return 0;
2866
2867 return (t->ealgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868}
2869
David S. Miller4c93fbb2011-02-25 18:07:06 -08002870static int count_ah_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
2872 int i, sz = 0;
2873
2874 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002875 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 if (!aalg)
2877 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002878 if (!aalg->pfkey_supported)
2879 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (aalg_tmpl_set(t, aalg) && aalg->available)
2881 sz += sizeof(struct sadb_comb);
2882 }
2883 return sz + sizeof(struct sadb_prop);
2884}
2885
David S. Miller4c93fbb2011-02-25 18:07:06 -08002886static int count_esp_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
2888 int i, k, sz = 0;
2889
2890 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002891 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (!ealg)
2893 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002894
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002895 if (!ealg->pfkey_supported)
2896 continue;
2897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2899 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 for (k = 1; ; k++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002902 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 if (!aalg)
2904 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002905
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002906 if (!aalg->pfkey_supported)
2907 continue;
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 if (aalg_tmpl_set(t, aalg) && aalg->available)
2910 sz += sizeof(struct sadb_comb);
2911 }
2912 }
2913 return sz + sizeof(struct sadb_prop);
2914}
2915
David S. Miller4c93fbb2011-02-25 18:07:06 -08002916static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
2918 struct sadb_prop *p;
2919 int i;
2920
2921 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2922 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2923 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2924 p->sadb_prop_replay = 32;
2925 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2926
2927 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002928 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 if (!aalg)
2930 break;
2931
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002932 if (!aalg->pfkey_supported)
2933 continue;
2934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2936 struct sadb_comb *c;
Johannes Bergb080db52017-06-16 14:29:19 +02002937 c = skb_put_zero(skb, sizeof(struct sadb_comb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2939 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2940 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2941 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2942 c->sadb_comb_hard_addtime = 24*60*60;
2943 c->sadb_comb_soft_addtime = 20*60*60;
2944 c->sadb_comb_hard_usetime = 8*60*60;
2945 c->sadb_comb_soft_usetime = 7*60*60;
2946 }
2947 }
2948}
2949
David S. Miller4c93fbb2011-02-25 18:07:06 -08002950static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
2952 struct sadb_prop *p;
2953 int i, k;
2954
2955 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2956 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2957 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2958 p->sadb_prop_replay = 32;
2959 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2960
2961 for (i=0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002962 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (!ealg)
2964 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002965
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002966 if (!ealg->pfkey_supported)
2967 continue;
2968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2970 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 for (k = 1; ; k++) {
2973 struct sadb_comb *c;
David S. Miller4c93fbb2011-02-25 18:07:06 -08002974 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 if (!aalg)
2976 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002977 if (!aalg->pfkey_supported)
2978 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2980 continue;
2981 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2982 memset(c, 0, sizeof(*c));
2983 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2984 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2985 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2986 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2987 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2988 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2989 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2990 c->sadb_comb_hard_addtime = 24*60*60;
2991 c->sadb_comb_soft_addtime = 20*60*60;
2992 c->sadb_comb_hard_usetime = 8*60*60;
2993 c->sadb_comb_soft_usetime = 7*60*60;
2994 }
2995 }
2996}
2997
David S. Miller214e0052011-02-24 00:02:38 -05002998static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002999{
3000 return 0;
3001}
3002
David S. Miller214e0052011-02-24 00:02:38 -05003003static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
3005 struct sk_buff *out_skb;
3006 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003007 int hard;
3008 int hsc;
3009
Herbert Xubf088672005-06-18 22:44:00 -07003010 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003011 if (hard)
3012 hsc = 2;
3013 else
3014 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Herbert Xu050f0092007-10-09 13:31:47 -07003016 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if (IS_ERR(out_skb))
3018 return PTR_ERR(out_skb);
3019
3020 out_hdr = (struct sadb_msg *) out_skb->data;
3021 out_hdr->sadb_msg_version = PF_KEY_V2;
3022 out_hdr->sadb_msg_type = SADB_EXPIRE;
3023 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3024 out_hdr->sadb_msg_errno = 0;
3025 out_hdr->sadb_msg_reserved = 0;
3026 out_hdr->sadb_msg_seq = 0;
3027 out_hdr->sadb_msg_pid = 0;
3028
David Ahernba51b6b2015-08-24 15:17:17 -06003029 pfkey_broadcast(out_skb, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return 0;
3031}
3032
David S. Miller214e0052011-02-24 00:02:38 -05003033static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003034{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003035 struct net *net = x ? xs_net(x) : c->net;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003036 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3037
3038 if (atomic_read(&net_pfkey->socks_nr) == 0)
Jamal Hadi Salim99c6f602008-06-10 14:25:34 -07003039 return 0;
3040
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003041 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003042 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003043 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003044 case XFRM_MSG_DELSA:
3045 case XFRM_MSG_NEWSA:
3046 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003047 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003048 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003049 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003050 case XFRM_MSG_NEWAE: /* not yet supported */
3051 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003052 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003053 pr_err("pfkey: Unknown SA event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003054 break;
3055 }
3056
3057 return 0;
3058}
3059
David S. Miller214e0052011-02-24 00:02:38 -05003060static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003061{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003062 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3063 return 0;
3064
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003065 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003066 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003067 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003068 case XFRM_MSG_DELPOLICY:
3069 case XFRM_MSG_NEWPOLICY:
3070 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003071 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003072 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003073 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3074 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003075 return key_notify_policy_flush(c);
3076 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003077 pr_err("pfkey: Unknown policy event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003078 break;
3079 }
3080
3081 return 0;
3082}
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084static u32 get_acqseq(void)
3085{
3086 u32 res;
Eric Dumazet28aecb92010-01-29 04:05:52 +00003087 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Eric Dumazet28aecb92010-01-29 04:05:52 +00003089 do {
3090 res = atomic_inc_return(&acqseq);
3091 } while (!res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return res;
3093}
3094
Horia Geanta0f245582014-02-12 16:20:06 +02003095static bool pfkey_is_alive(const struct km_event *c)
3096{
3097 struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id);
3098 struct sock *sk;
3099 bool is_alive = false;
3100
3101 rcu_read_lock();
3102 sk_for_each_rcu(sk, &net_pfkey->table) {
3103 if (pfkey_sk(sk)->registered) {
3104 is_alive = true;
3105 break;
3106 }
3107 }
3108 rcu_read_unlock();
3109
3110 return is_alive;
3111}
3112
Fan Du65e07362012-08-15 10:13:47 +08003113static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114{
3115 struct sk_buff *skb;
3116 struct sadb_msg *hdr;
3117 struct sadb_address *addr;
3118 struct sadb_x_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 int sockaddr_size;
3120 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003121 struct sadb_x_sec_ctx *sec_ctx;
3122 struct xfrm_sec_ctx *xfrm_ctx;
3123 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3126 if (!sockaddr_size)
3127 return -EINVAL;
3128
3129 size = sizeof(struct sadb_msg) +
3130 (sizeof(struct sadb_address) * 2) +
3131 (sockaddr_size * 2) +
3132 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 if (x->id.proto == IPPROTO_AH)
3135 size += count_ah_combs(t);
3136 else if (x->id.proto == IPPROTO_ESP)
3137 size += count_esp_combs(t);
3138
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003139 if ((xfrm_ctx = x->security)) {
3140 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3141 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3142 }
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 skb = alloc_skb(size + 16, GFP_ATOMIC);
3145 if (skb == NULL)
3146 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3149 hdr->sadb_msg_version = PF_KEY_V2;
3150 hdr->sadb_msg_type = SADB_ACQUIRE;
3151 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3152 hdr->sadb_msg_len = size / sizeof(uint64_t);
3153 hdr->sadb_msg_errno = 0;
3154 hdr->sadb_msg_reserved = 0;
3155 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3156 hdr->sadb_msg_pid = 0;
3157
3158 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003159 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003161 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 (sizeof(struct sadb_address)+sockaddr_size)/
3163 sizeof(uint64_t);
3164 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3165 addr->sadb_address_proto = 0;
3166 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003167 addr->sadb_address_prefixlen =
3168 pfkey_sockaddr_fill(&x->props.saddr, 0,
3169 (struct sockaddr *) (addr + 1),
3170 x->props.family);
3171 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003173
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003175 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 sizeof(struct sadb_address)+sockaddr_size);
3177 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
3190 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3191 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) {
3207 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3208 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3209 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
David Ahernba51b6b2015-08-24 15:17:17 -06003219 return pfkey_broadcast(skb, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003222static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003223 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003225 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 struct xfrm_policy *xp;
3227 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003228 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003230 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 case AF_INET:
3232 if (opt != IP_IPSEC_POLICY) {
3233 *dir = -EOPNOTSUPP;
3234 return NULL;
3235 }
3236 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00003237#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 case AF_INET6:
3239 if (opt != IPV6_IPSEC_POLICY) {
3240 *dir = -EOPNOTSUPP;
3241 return NULL;
3242 }
3243 break;
3244#endif
3245 default:
3246 *dir = -EINVAL;
3247 return NULL;
3248 }
3249
3250 *dir = -EINVAL;
3251
3252 if (len < sizeof(struct sadb_x_policy) ||
3253 pol->sadb_x_policy_len*8 > len ||
3254 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3255 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3256 return NULL;
3257
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003258 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 if (xp == NULL) {
3260 *dir = -ENOBUFS;
3261 return NULL;
3262 }
3263
3264 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3265 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3266
3267 xp->lft.soft_byte_limit = XFRM_INF;
3268 xp->lft.hard_byte_limit = XFRM_INF;
3269 xp->lft.soft_packet_limit = XFRM_INF;
3270 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003271 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 xp->xfrm_nr = 0;
3274 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3275 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3276 goto out;
3277
Trent Jaegerdf718372005-12-13 23:12:27 -08003278 /* security context too */
3279 if (len >= (pol->sadb_x_policy_len*8 +
3280 sizeof(struct sadb_x_sec_ctx))) {
3281 char *p = (char *)pol;
3282 struct xfrm_user_sec_ctx *uctx;
3283
3284 p += pol->sadb_x_policy_len*8;
3285 sec_ctx = (struct sadb_x_sec_ctx *)p;
3286 if (len < pol->sadb_x_policy_len*8 +
Steffen Klassertd90c9022017-05-05 07:40:42 +02003287 sec_ctx->sadb_x_sec_len*8) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003288 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003289 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003290 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003291 if ((*dir = verify_sec_ctx_len(p)))
3292 goto out;
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01003293 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_ATOMIC);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01003294 *dir = security_xfrm_policy_alloc(&xp->security, uctx, GFP_ATOMIC);
Trent Jaegerdf718372005-12-13 23:12:27 -08003295 kfree(uctx);
3296
3297 if (*dir)
3298 goto out;
3299 }
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 *dir = pol->sadb_x_policy_dir-1;
3302 return xp;
3303
3304out:
Alexey Dobriyan70e90672008-11-06 23:08:37 -08003305 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08003306 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 return NULL;
3308}
3309
Al Viro5d36b182006-11-08 00:24:06 -08003310static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
3312 struct sk_buff *skb;
3313 struct sadb_msg *hdr;
3314 struct sadb_sa *sa;
3315 struct sadb_address *addr;
3316 struct sadb_x_nat_t_port *n_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 int sockaddr_size;
3318 int size;
3319 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3320 struct xfrm_encap_tmpl *natt = NULL;
3321
3322 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3323 if (!sockaddr_size)
3324 return -EINVAL;
3325
3326 if (!satype)
3327 return -EINVAL;
3328
3329 if (!x->encap)
3330 return -EINVAL;
3331
3332 natt = x->encap;
3333
3334 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3335 *
3336 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3337 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3338 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 size = sizeof(struct sadb_msg) +
3341 sizeof(struct sadb_sa) +
3342 (sizeof(struct sadb_address) * 2) +
3343 (sockaddr_size * 2) +
3344 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 skb = alloc_skb(size + 16, GFP_ATOMIC);
3347 if (skb == NULL)
3348 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3351 hdr->sadb_msg_version = PF_KEY_V2;
3352 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3353 hdr->sadb_msg_satype = satype;
3354 hdr->sadb_msg_len = size / sizeof(uint64_t);
3355 hdr->sadb_msg_errno = 0;
3356 hdr->sadb_msg_reserved = 0;
3357 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3358 hdr->sadb_msg_pid = 0;
3359
3360 /* SA */
3361 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3362 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3363 sa->sadb_sa_exttype = SADB_EXT_SA;
3364 sa->sadb_sa_spi = x->id.spi;
3365 sa->sadb_sa_replay = 0;
3366 sa->sadb_sa_state = 0;
3367 sa->sadb_sa_auth = 0;
3368 sa->sadb_sa_encrypt = 0;
3369 sa->sadb_sa_flags = 0;
3370
3371 /* ADDRESS_SRC (old addr) */
3372 addr = (struct sadb_address*)
3373 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) */
3388 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3389 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) */
3395 addr = (struct sadb_address*)
3396 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003397 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 (sizeof(struct sadb_address)+sockaddr_size)/
3399 sizeof(uint64_t);
3400 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3401 addr->sadb_address_proto = 0;
3402 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003403 addr->sadb_address_prefixlen =
3404 pfkey_sockaddr_fill(ipaddr, 0,
3405 (struct sockaddr *) (addr + 1),
3406 x->props.family);
3407 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 BUG();
3409
3410 /* NAT_T_DPORT (new port) */
3411 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3412 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3413 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3414 n_port->sadb_x_nat_t_port_port = sport;
3415 n_port->sadb_x_nat_t_port_reserved = 0;
3416
David Ahernba51b6b2015-08-24 15:17:17 -06003417 return pfkey_broadcast(skb, BROADCAST_REGISTERED, NULL, 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;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003425 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3426 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,
3508 const struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003509{
3510 int i;
3511 int sasize_sel;
3512 int size = 0;
3513 int size_pol = 0;
3514 struct sk_buff *skb;
3515 struct sadb_msg *hdr;
3516 struct sadb_x_policy *pol;
David S. Miller183cad12011-02-24 00:28:01 -05003517 const struct xfrm_migrate *mp;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003518
3519 if (type != XFRM_POLICY_TYPE_MAIN)
3520 return 0;
3521
3522 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3523 return -EINVAL;
3524
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003525 if (k != NULL) {
3526 /* addresses for KM */
3527 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3528 pfkey_sockaddr_pair_size(k->family));
3529 }
3530
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003531 /* selector */
3532 sasize_sel = pfkey_sockaddr_size(sel->family);
3533 if (!sasize_sel)
3534 return -EINVAL;
3535 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3536
3537 /* policy info */
3538 size_pol += sizeof(struct sadb_x_policy);
3539
3540 /* ipsecrequests */
3541 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3542 /* old locator pair */
3543 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3544 pfkey_sockaddr_pair_size(mp->old_family);
3545 /* new locator pair */
3546 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3547 pfkey_sockaddr_pair_size(mp->new_family);
3548 }
3549
3550 size += sizeof(struct sadb_msg) + size_pol;
3551
3552 /* alloc buffer */
3553 skb = alloc_skb(size, GFP_ATOMIC);
3554 if (skb == NULL)
3555 return -ENOMEM;
3556
3557 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3558 hdr->sadb_msg_version = PF_KEY_V2;
3559 hdr->sadb_msg_type = SADB_X_MIGRATE;
3560 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3561 hdr->sadb_msg_len = size / 8;
3562 hdr->sadb_msg_errno = 0;
3563 hdr->sadb_msg_reserved = 0;
3564 hdr->sadb_msg_seq = 0;
3565 hdr->sadb_msg_pid = 0;
3566
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003567 /* Addresses to be used by KM for negotiation, if ext is available */
3568 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
Julia Lawall89eb06f2012-04-08 22:41:10 +00003569 goto err;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003570
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003571 /* selector src */
3572 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3573
3574 /* selector dst */
3575 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3576
3577 /* policy information */
3578 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3579 pol->sadb_x_policy_len = size_pol / 8;
3580 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3581 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3582 pol->sadb_x_policy_dir = dir + 1;
Dan Carpenterff862a42013-07-28 23:04:45 +03003583 pol->sadb_x_policy_reserved = 0;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003584 pol->sadb_x_policy_id = 0;
3585 pol->sadb_x_policy_priority = 0;
3586
3587 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3588 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003589 int mode = pfkey_mode_from_xfrm(mp->mode);
3590 if (mode < 0)
Patrick McHardyd4782c32008-01-20 17:24:29 -08003591 goto err;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003592 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003593 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3594 mp->reqid, mp->old_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003595 &mp->old_saddr, &mp->old_daddr) < 0)
3596 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003597
3598 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003599 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003600 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3601 mp->reqid, mp->new_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003602 &mp->new_saddr, &mp->new_daddr) < 0)
3603 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003604 }
3605
3606 /* broadcast migrate message to sockets */
David Ahernba51b6b2015-08-24 15:17:17 -06003607 pfkey_broadcast(skb, BROADCAST_ALL, NULL, &init_net);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003608
3609 return 0;
Patrick McHardyd4782c32008-01-20 17:24:29 -08003610
3611err:
3612 kfree_skb(skb);
3613 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003614}
3615#else
David S. Miller183cad12011-02-24 00:28:01 -05003616static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3617 const struct xfrm_migrate *m, int num_bundles,
3618 const struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003619{
3620 return -ENOPROTOOPT;
3621}
3622#endif
3623
Ying Xue1b784142015-03-02 15:37:48 +08003624static int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
3626 struct sock *sk = sock->sk;
3627 struct sk_buff *skb = NULL;
3628 struct sadb_msg *hdr = NULL;
3629 int err;
Fan Du283bc9f2013-11-07 17:47:50 +08003630 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
3632 err = -EOPNOTSUPP;
3633 if (msg->msg_flags & MSG_OOB)
3634 goto out;
3635
3636 err = -EMSGSIZE;
Eric Dumazet95c96172012-04-15 05:58:06 +00003637 if ((unsigned int)len > sk->sk_sndbuf - 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 goto out;
3639
3640 err = -ENOBUFS;
3641 skb = alloc_skb(len, GFP_KERNEL);
3642 if (skb == NULL)
3643 goto out;
3644
3645 err = -EFAULT;
Al Viro6ce8e9c2014-04-06 21:25:44 -04003646 if (memcpy_from_msg(skb_put(skb,len), msg, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 goto out;
3648
3649 hdr = pfkey_get_base_msg(skb, &err);
3650 if (!hdr)
3651 goto out;
3652
Fan Du283bc9f2013-11-07 17:47:50 +08003653 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 err = pfkey_process(sk, skb, hdr);
Fan Du283bc9f2013-11-07 17:47:50 +08003655 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657out:
3658 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3659 err = 0;
Wei Yongjun6f961062009-02-25 00:31:04 +00003660 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
3662 return err ? : len;
3663}
3664
Ying Xue1b784142015-03-02 15:37:48 +08003665static int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 int flags)
3667{
3668 struct sock *sk = sock->sk;
Timo Teras83321d62008-03-03 23:40:12 -08003669 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 struct sk_buff *skb;
3671 int copied, err;
3672
3673 err = -EINVAL;
3674 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3675 goto out;
3676
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3678 if (skb == NULL)
3679 goto out;
3680
3681 copied = skb->len;
3682 if (copied > len) {
3683 msg->msg_flags |= MSG_TRUNC;
3684 copied = len;
3685 }
3686
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003687 skb_reset_transport_header(skb);
David S. Miller51f3d022014-11-05 16:46:40 -05003688 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 if (err)
3690 goto out_free;
3691
Neil Horman3b885782009-10-12 13:26:31 -07003692 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
3694 err = (flags & MSG_TRUNC) ? skb->len : copied;
3695
Timo Teras83321d62008-03-03 23:40:12 -08003696 if (pfk->dump.dump != NULL &&
3697 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3698 pfkey_do_dump(pfk);
3699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700out_free:
3701 skb_free_datagram(sk, skb);
3702out:
3703 return err;
3704}
3705
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003706static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 .family = PF_KEY,
3708 .owner = THIS_MODULE,
3709 /* Operations that make no sense on pfkey sockets. */
3710 .bind = sock_no_bind,
3711 .connect = sock_no_connect,
3712 .socketpair = sock_no_socketpair,
3713 .accept = sock_no_accept,
3714 .getname = sock_no_getname,
3715 .ioctl = sock_no_ioctl,
3716 .listen = sock_no_listen,
3717 .shutdown = sock_no_shutdown,
3718 .setsockopt = sock_no_setsockopt,
3719 .getsockopt = sock_no_getsockopt,
3720 .mmap = sock_no_mmap,
3721 .sendpage = sock_no_sendpage,
3722
3723 /* Now the operations that really occur. */
3724 .release = pfkey_release,
3725 .poll = datagram_poll,
3726 .sendmsg = pfkey_sendmsg,
3727 .recvmsg = pfkey_recvmsg,
3728};
3729
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003730static const struct net_proto_family pfkey_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 .family = PF_KEY,
3732 .create = pfkey_create,
3733 .owner = THIS_MODULE,
3734};
3735
3736#ifdef CONFIG_PROC_FS
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003737static int pfkey_seq_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738{
Li Zefan27b5b862010-02-08 23:20:29 +00003739 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003741 if (v == SEQ_START_TOKEN)
3742 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3743 else
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003744 seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 s,
3746 atomic_read(&s->sk_refcnt),
Eric Dumazet31e6d362009-06-17 19:05:41 -07003747 sk_rmem_alloc_get(s),
3748 sk_wmem_alloc_get(s),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06003749 from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 sock_i_ino(s)
3751 );
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753}
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003754
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003755static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
stephen hemmingerada440e2011-02-23 09:06:50 +00003756 __acquires(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003757{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003758 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003759 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003760
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003761 rcu_read_lock();
3762 return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003763}
3764
3765static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3766{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003767 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003768 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3769
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003770 return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003771}
3772
3773static void pfkey_seq_stop(struct seq_file *f, void *v)
stephen hemmingerada440e2011-02-23 09:06:50 +00003774 __releases(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003775{
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003776 rcu_read_unlock();
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003777}
3778
Stephen Hemminger98147d52009-09-01 19:25:02 +00003779static const struct seq_operations pfkey_seq_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003780 .start = pfkey_seq_start,
3781 .next = pfkey_seq_next,
3782 .stop = pfkey_seq_stop,
3783 .show = pfkey_seq_show,
3784};
3785
3786static int pfkey_seq_open(struct inode *inode, struct file *file)
3787{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003788 return seq_open_net(inode, file, &pfkey_seq_ops,
3789 sizeof(struct seq_net_private));
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003790}
3791
Stephen Hemminger5ca1b992009-09-01 19:25:05 +00003792static const struct file_operations pfkey_proc_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003793 .open = pfkey_seq_open,
3794 .read = seq_read,
3795 .llseek = seq_lseek,
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003796 .release = seq_release_net,
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003797};
3798
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003799static int __net_init pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003800{
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003801 struct proc_dir_entry *e;
3802
Gao fengd4beaa62013-02-18 01:34:54 +00003803 e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003804 if (e == NULL)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003805 return -ENOMEM;
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003806
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003807 return 0;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003808}
3809
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003810static void __net_exit pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003811{
Gao fengece31ff2013-02-18 01:34:56 +00003812 remove_proc_entry("pfkey", net->proc_net);
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003813}
3814#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003815static inline int pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003816{
3817 return 0;
3818}
3819
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003820static inline void pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003821{
3822}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823#endif
3824
3825static struct xfrm_mgr pfkeyv2_mgr =
3826{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 .notify = pfkey_send_notify,
3828 .acquire = pfkey_send_acquire,
3829 .compile_policy = pfkey_compile_policy,
3830 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003831 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003832 .migrate = pfkey_send_migrate,
Horia Geanta0f245582014-02-12 16:20:06 +02003833 .is_alive = pfkey_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834};
3835
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003836static int __net_init pfkey_net_init(struct net *net)
3837{
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003838 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003839 int rv;
3840
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003841 INIT_HLIST_HEAD(&net_pfkey->table);
3842 atomic_set(&net_pfkey->socks_nr, 0);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003843
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003844 rv = pfkey_init_proc(net);
3845
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003846 return rv;
3847}
3848
3849static void __net_exit pfkey_net_exit(struct net *net)
3850{
3851 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3852
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003853 pfkey_exit_proc(net);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003854 BUG_ON(!hlist_empty(&net_pfkey->table));
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003855}
3856
3857static struct pernet_operations pfkey_net_ops = {
3858 .init = pfkey_net_init,
3859 .exit = pfkey_net_exit,
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003860 .id = &pfkey_net_id,
3861 .size = sizeof(struct netns_pfkey),
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003862};
3863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864static void __exit ipsec_pfkey_exit(void)
3865{
3866 xfrm_unregister_km(&pfkeyv2_mgr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003868 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 proto_unregister(&key_proto);
3870}
3871
3872static int __init ipsec_pfkey_init(void)
3873{
3874 int err = proto_register(&key_proto, 0);
3875
3876 if (err != 0)
3877 goto out;
3878
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003879 err = register_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 if (err != 0)
3881 goto out_unregister_key_proto;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003882 err = sock_register(&pfkey_family_ops);
3883 if (err != 0)
3884 goto out_unregister_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 err = xfrm_register_km(&pfkeyv2_mgr);
3886 if (err != 0)
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003887 goto out_sock_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888out:
3889 return err;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891out_sock_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003893out_unregister_pernet:
3894 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895out_unregister_key_proto:
3896 proto_unregister(&key_proto);
3897 goto out;
3898}
3899
3900module_init(ipsec_pfkey_init);
3901module_exit(ipsec_pfkey_exit);
3902MODULE_LICENSE("GPL");
3903MODULE_ALIAS_NETPROTO(PF_KEY);