blob: ca9d3ae665e76ea847a4ce03b4d275f80d7705bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Maxim Giryaev <gem@asplinux.ru>
10 * David S. Miller <davem@redhat.com>
11 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
12 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
14 * Derek Atkins <derek@ihtfp.com>
15 */
16
Randy Dunlap4fc268d2006-01-11 12:17:47 -080017#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/socket.h>
21#include <linux/pfkeyv2.h>
22#include <linux/ipsec.h>
23#include <linux/skbuff.h>
24#include <linux/rtnetlink.h>
25#include <linux/in.h>
26#include <linux/in6.h>
27#include <linux/proc_fs.h>
28#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020030#include <net/net_namespace.h>
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080031#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/xfrm.h>
33
34#include <net/sock.h>
35
36#define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
37#define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
38
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030039static unsigned int pfkey_net_id __read_mostly;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080040struct netns_pfkey {
41 /* List of all pfkey sockets. */
42 struct hlist_head table;
43 atomic_t socks_nr;
44};
stephen hemminger7f6b9db2010-02-22 07:57:19 +000045static DEFINE_MUTEX(pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jamal Hadi Salimbd557752010-02-22 16:20:22 -080047#define DUMMY_MARK 0
Mathias Krausee473fcb2013-06-26 23:56:58 +020048static const struct xfrm_mark dummy_mark = {0, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049struct pfkey_sock {
50 /* struct sock must be the first member of struct pfkey_sock */
51 struct sock sk;
52 int registered;
53 int promisc;
Timo Teras83321d62008-03-03 23:40:12 -080054
55 struct {
56 uint8_t msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +000057 uint32_t msg_portid;
Timo Teras83321d62008-03-03 23:40:12 -080058 int (*dump)(struct pfkey_sock *sk);
59 void (*done)(struct pfkey_sock *sk);
60 union {
61 struct xfrm_policy_walk policy;
62 struct xfrm_state_walk state;
63 } u;
Herbert Xu12a169e2008-10-01 07:03:24 -070064 struct sk_buff *skb;
Timo Teras83321d62008-03-03 23:40:12 -080065 } dump;
Yuejie Shi89e357d2017-03-31 15:10:20 +080066 struct mutex dump_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Herbert Xu096f41d2017-04-13 18:35:59 +080069static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
70 xfrm_address_t *saddr, xfrm_address_t *daddr,
71 u16 *family);
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
74{
75 return (struct pfkey_sock *)sk;
76}
77
David S. Miller4c93fbb2011-02-25 18:07:06 -080078static int pfkey_can_dump(const struct sock *sk)
Timo Teras83321d62008-03-03 23:40:12 -080079{
80 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
81 return 1;
82 return 0;
83}
84
Timo Teras05238202008-10-01 05:17:54 -070085static void pfkey_terminate_dump(struct pfkey_sock *pfk)
Timo Teras83321d62008-03-03 23:40:12 -080086{
Timo Teras05238202008-10-01 05:17:54 -070087 if (pfk->dump.dump) {
Herbert Xu12a169e2008-10-01 07:03:24 -070088 if (pfk->dump.skb) {
89 kfree_skb(pfk->dump.skb);
90 pfk->dump.skb = NULL;
91 }
Timo Teras05238202008-10-01 05:17:54 -070092 pfk->dump.done(pfk);
93 pfk->dump.dump = NULL;
94 pfk->dump.done = NULL;
95 }
Timo Teras83321d62008-03-03 23:40:12 -080096}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static void pfkey_sock_destruct(struct sock *sk)
99{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800100 struct net *net = sock_net(sk);
101 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
102
Timo Teras05238202008-10-01 05:17:54 -0700103 pfkey_terminate_dump(pfkey_sk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 skb_queue_purge(&sk->sk_receive_queue);
105
106 if (!sock_flag(sk, SOCK_DEAD)) {
stephen hemminger207024b2010-05-12 06:37:07 +0000107 pr_err("Attempt to release alive pfkey socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return;
109 }
110
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700111 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
Reshetova, Elena14afee42017-06-30 13:08:00 +0300112 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800114 atomic_dec(&net_pfkey->socks_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800117static const struct proto_ops pfkey_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119static void pfkey_insert(struct sock *sk)
120{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800121 struct net *net = sock_net(sk);
122 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
123
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000124 mutex_lock(&pfkey_mutex);
125 sk_add_node_rcu(sk, &net_pfkey->table);
126 mutex_unlock(&pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static void pfkey_remove(struct sock *sk)
130{
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000131 mutex_lock(&pfkey_mutex);
132 sk_del_node_init_rcu(sk);
133 mutex_unlock(&pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static struct proto key_proto = {
137 .name = "KEY",
138 .owner = THIS_MODULE,
139 .obj_size = sizeof(struct pfkey_sock),
140};
141
Eric Paris3f378b62009-11-05 22:18:14 -0800142static int pfkey_create(struct net *net, struct socket *sock, int protocol,
143 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800145 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct sock *sk;
Yuejie Shi89e357d2017-03-31 15:10:20 +0800147 struct pfkey_sock *pfk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int err;
149
Eric W. Biedermandf008c92012-11-16 03:03:07 +0000150 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return -EPERM;
152 if (sock->type != SOCK_RAW)
153 return -ESOCKTNOSUPPORT;
154 if (protocol != PF_KEY_V2)
155 return -EPROTONOSUPPORT;
156
157 err = -ENOMEM;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500158 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (sk == NULL)
160 goto out;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900161
Yuejie Shi89e357d2017-03-31 15:10:20 +0800162 pfk = pfkey_sk(sk);
163 mutex_init(&pfk->dump_lock);
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 sock->ops = &pfkey_ops;
166 sock_init_data(sock, sk);
167
168 sk->sk_family = PF_KEY;
169 sk->sk_destruct = pfkey_sock_destruct;
170
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800171 atomic_inc(&net_pfkey->socks_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 pfkey_insert(sk);
174
175 return 0;
176out:
177 return err;
178}
179
180static int pfkey_release(struct socket *sock)
181{
182 struct sock *sk = sock->sk;
183
184 if (!sk)
185 return 0;
186
187 pfkey_remove(sk);
188
189 sock_orphan(sk);
190 sock->sk = NULL;
191 skb_queue_purge(&sk->sk_write_queue);
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000192
193 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 sock_put(sk);
195
196 return 0;
197}
198
199static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
Al Virodd0fc662005-10-07 07:46:04 +0100200 gfp_t allocation, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 int err = -ENOBUFS;
203
204 sock_hold(sk);
205 if (*skb2 == NULL) {
Reshetova, Elena63354792017-06-30 13:07:58 +0300206 if (refcount_read(&skb->users) != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *skb2 = skb_clone(skb, allocation);
208 } else {
209 *skb2 = skb;
Reshetova, Elena63354792017-06-30 13:07:58 +0300210 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212 }
213 if (*skb2 != NULL) {
214 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 skb_set_owner_r(*skb2, sk);
216 skb_queue_tail(&sk->sk_receive_queue, *skb2);
David S. Miller676d2362014-04-11 16:15:36 -0400217 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 *skb2 = NULL;
219 err = 0;
220 }
221 }
222 sock_put(sk);
223 return err;
224}
225
226/* Send SKB to all pfkey sockets matching selected criteria. */
227#define BROADCAST_ALL 0
228#define BROADCAST_ONE 1
229#define BROADCAST_REGISTERED 2
230#define BROADCAST_PROMISC_ONLY 4
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
Johannes Berg4df864c2017-06-16 14:29:21 +0200352 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 pfkey_hdr_dup(hdr, orig);
354 hdr->sadb_msg_errno = (uint8_t) err;
355 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
356 sizeof(uint64_t));
357
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 */
Johannes Berg4df864c2017-06-16 14:29:21 +0200813 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 memset(hdr, 0, size); /* XXX do we need this ? */
815 hdr->sadb_msg_len = size / sizeof(uint64_t);
816
817 /* sa */
Johannes Berg4df864c2017-06-16 14:29:21 +0200818 sa = skb_put(skb, sizeof(struct sadb_sa));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 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) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200865 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 lifetime->sadb_lifetime_len =
867 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
868 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
869 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
870 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
871 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
872 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
873 }
874 /* soft time */
875 if (hsc & 1) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200876 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 lifetime->sadb_lifetime_len =
878 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
879 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
880 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
881 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
882 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
883 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
884 }
885 /* current time */
Johannes Berg4df864c2017-06-16 14:29:21 +0200886 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 lifetime->sadb_lifetime_len =
888 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
889 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
890 lifetime->sadb_lifetime_allocations = x->curlft.packets;
891 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
892 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
893 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
894 /* src address */
Johannes Berg4df864c2017-06-16 14:29:21 +0200895 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900896 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 (sizeof(struct sadb_address)+sockaddr_size)/
898 sizeof(uint64_t);
899 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900900 /* "if the ports are non-zero, then the sadb_address_proto field,
901 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900903 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900906 addr->sadb_address_prefixlen =
907 pfkey_sockaddr_fill(&x->props.saddr, 0,
908 (struct sockaddr *) (addr + 1),
909 x->props.family);
910 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 BUG();
912
913 /* dst address */
Johannes Berg4df864c2017-06-16 14:29:21 +0200914 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900915 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 (sizeof(struct sadb_address)+sockaddr_size)/
917 sizeof(uint64_t);
918 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900919 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900922 addr->sadb_address_prefixlen =
923 pfkey_sockaddr_fill(&x->id.daddr, 0,
924 (struct sockaddr *) (addr + 1),
925 x->props.family);
926 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 BUG();
928
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000929 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
930 x->props.family)) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200931 addr = skb_put(skb,
932 sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900933 addr->sadb_address_len =
934 (sizeof(struct sadb_address)+sockaddr_size)/
935 sizeof(uint64_t);
936 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
937 addr->sadb_address_proto =
938 pfkey_proto_from_xfrm(x->sel.proto);
939 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
940 addr->sadb_address_reserved = 0;
941
942 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
943 (struct sockaddr *) (addr + 1),
944 x->props.family);
945 }
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /* auth key */
948 if (add_keys && auth_key_size) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200949 key = skb_put(skb, sizeof(struct sadb_key) + auth_key_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
951 sizeof(uint64_t);
952 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
953 key->sadb_key_bits = x->aalg->alg_key_len;
954 key->sadb_key_reserved = 0;
955 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
956 }
957 /* encrypt key */
958 if (add_keys && encrypt_key_size) {
Johannes Berg4df864c2017-06-16 14:29:21 +0200959 key = skb_put(skb, sizeof(struct sadb_key) + encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900960 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 encrypt_key_size) / sizeof(uint64_t);
962 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
963 key->sadb_key_bits = x->ealg->alg_key_len;
964 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900965 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 (x->ealg->alg_key_len+7)/8);
967 }
968
969 /* sa */
Johannes Berg4df864c2017-06-16 14:29:21 +0200970 sa2 = skb_put(skb, sizeof(struct sadb_x_sa2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
972 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700973 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
974 kfree_skb(skb);
975 return ERR_PTR(-EINVAL);
976 }
977 sa2->sadb_x_sa2_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 sa2->sadb_x_sa2_reserved1 = 0;
979 sa2->sadb_x_sa2_reserved2 = 0;
980 sa2->sadb_x_sa2_sequence = 0;
981 sa2->sadb_x_sa2_reqid = x->props.reqid;
982
983 if (natt && natt->encap_type) {
984 struct sadb_x_nat_t_type *n_type;
985 struct sadb_x_nat_t_port *n_port;
986
987 /* type */
Johannes Berg4df864c2017-06-16 14:29:21 +0200988 n_type = skb_put(skb, sizeof(*n_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
990 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
991 n_type->sadb_x_nat_t_type_type = natt->encap_type;
992 n_type->sadb_x_nat_t_type_reserved[0] = 0;
993 n_type->sadb_x_nat_t_type_reserved[1] = 0;
994 n_type->sadb_x_nat_t_type_reserved[2] = 0;
995
996 /* source port */
Johannes Berg4df864c2017-06-16 14:29:21 +0200997 n_port = skb_put(skb, sizeof(*n_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
999 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
1000 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
1001 n_port->sadb_x_nat_t_port_reserved = 0;
1002
1003 /* dest port */
Johannes Berg4df864c2017-06-16 14:29:21 +02001004 n_port = skb_put(skb, sizeof(*n_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 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_DPORT;
1007 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
1008 n_port->sadb_x_nat_t_port_reserved = 0;
1009 }
1010
Trent Jaegerdf718372005-12-13 23:12:27 -08001011 /* security context */
1012 if (xfrm_ctx) {
Johannes Berg4df864c2017-06-16 14:29:21 +02001013 sec_ctx = skb_put(skb,
1014 sizeof(struct sadb_x_sec_ctx) + ctx_size);
Trent Jaegerdf718372005-12-13 23:12:27 -08001015 sec_ctx->sadb_x_sec_len =
1016 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1017 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1018 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1019 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1020 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1021 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1022 xfrm_ctx->ctx_len);
1023 }
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return skb;
1026}
1027
Herbert Xu050f0092007-10-09 13:31:47 -07001028
David S. Miller4c93fbb2011-02-25 18:07:06 -08001029static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
Herbert Xu050f0092007-10-09 13:31:47 -07001030{
1031 struct sk_buff *skb;
1032
Herbert Xu050f0092007-10-09 13:31:47 -07001033 skb = __pfkey_xfrm_state2msg(x, 1, 3);
Herbert Xu050f0092007-10-09 13:31:47 -07001034
1035 return skb;
1036}
1037
David S. Miller4c93fbb2011-02-25 18:07:06 -08001038static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -07001039 int hsc)
1040{
1041 return __pfkey_xfrm_state2msg(x, 0, hsc);
1042}
1043
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001044static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
David S. Miller4c93fbb2011-02-25 18:07:06 -08001045 const struct sadb_msg *hdr,
1046 void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001048 struct xfrm_state *x;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001049 const struct sadb_lifetime *lifetime;
1050 const struct sadb_sa *sa;
1051 const struct sadb_key *key;
1052 const struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 uint16_t proto;
1054 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Joe Perchesea110732011-06-13 16:21:26 +00001057 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (!sa ||
1059 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1060 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1061 return ERR_PTR(-EINVAL);
1062 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1063 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1064 return ERR_PTR(-EINVAL);
1065 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1066 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1067 return ERR_PTR(-EINVAL);
1068 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1069 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1070 return ERR_PTR(-EINVAL);
1071
1072 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1073 if (proto == 0)
1074 return ERR_PTR(-EINVAL);
1075
1076 /* default error is no buffer space */
1077 err = -ENOBUFS;
1078
1079 /* RFC2367:
1080
1081 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1082 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1083 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1084 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1085 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1086 not true.
1087
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001088 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1090 */
1091 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1092 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1093 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1094 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1095 return ERR_PTR(-EINVAL);
Joe Perchesea110732011-06-13 16:21:26 +00001096 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (key != NULL &&
1098 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1099 ((key->sadb_key_bits+7) / 8 == 0 ||
1100 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1101 return ERR_PTR(-EINVAL);
1102 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1103 if (key != NULL &&
1104 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1105 ((key->sadb_key_bits+7) / 8 == 0 ||
1106 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1107 return ERR_PTR(-EINVAL);
1108
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001109 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (x == NULL)
1111 return ERR_PTR(-ENOBUFS);
1112
1113 x->id.proto = proto;
1114 x->id.spi = sa->sadb_sa_spi;
Fan Du33fce602013-09-17 15:14:13 +08001115 x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay,
1116 (sizeof(x->replay.bitmap) * 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1118 x->props.flags |= XFRM_STATE_NOECN;
1119 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1120 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001121 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1122 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Joe Perchesea110732011-06-13 16:21:26 +00001124 lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (lifetime != NULL) {
1126 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1127 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1128 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1129 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1130 }
Joe Perchesea110732011-06-13 16:21:26 +00001131 lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (lifetime != NULL) {
1133 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1134 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1135 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1136 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1137 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001138
Joe Perchesea110732011-06-13 16:21:26 +00001139 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001140 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01001141 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001142
1143 if (!uctx)
1144 goto out;
1145
1146 err = security_xfrm_state_alloc(x, uctx);
1147 kfree(uctx);
1148
1149 if (err)
1150 goto out;
1151 }
1152
Dan Carpentere747f642017-06-14 13:35:37 +03001153 err = -ENOBUFS;
Joe Perchesea110732011-06-13 16:21:26 +00001154 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (sa->sadb_sa_auth) {
1156 int keysize = 0;
1157 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001158 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 err = -ENOSYS;
1160 goto out;
1161 }
1162 if (key)
1163 keysize = (key->sadb_key_bits + 7) / 8;
1164 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001165 if (!x->aalg) {
1166 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 goto out;
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 strcpy(x->aalg->alg_name, a->name);
1170 x->aalg->alg_key_len = 0;
1171 if (key) {
1172 x->aalg->alg_key_len = key->sadb_key_bits;
1173 memcpy(x->aalg->alg_key, key+1, keysize);
1174 }
Martin Willic20a66f2009-12-09 06:11:15 +00001175 x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 x->props.aalgo = sa->sadb_sa_auth;
1177 /* x->algo.flags = sa->sadb_sa_flags; */
1178 }
1179 if (sa->sadb_sa_encrypt) {
1180 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1181 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001182 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 err = -ENOSYS;
1184 goto out;
1185 }
1186 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001187 if (!x->calg) {
1188 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 goto out;
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 strcpy(x->calg->alg_name, a->name);
1192 x->props.calgo = sa->sadb_sa_encrypt;
1193 } else {
1194 int keysize = 0;
1195 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001196 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 err = -ENOSYS;
1198 goto out;
1199 }
1200 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1201 if (key)
1202 keysize = (key->sadb_key_bits + 7) / 8;
1203 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001204 if (!x->ealg) {
1205 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 goto out;
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 strcpy(x->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);
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001252 if (!x->encap) {
1253 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto out;
Dan Carpenter1e3d0c22017-06-14 13:34:05 +03001255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 natt = x->encap;
1258 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1259 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1260
1261 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001262 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1264 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1265 }
1266 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001267 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1269 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1270 }
Timo Terasa8d694c2009-01-25 20:49:14 -08001271 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273
Herbert Xu72cb6962005-06-20 13:18:08 -07001274 err = xfrm_init_state(x);
1275 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return x;
1280
1281out:
1282 x->km.state = XFRM_STATE_DEAD;
1283 xfrm_state_put(x);
1284 return ERR_PTR(err);
1285}
1286
David S. Miller4c93fbb2011-02-25 18:07:06 -08001287static 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 -07001288{
1289 return -EOPNOTSUPP;
1290}
1291
David S. Miller4c93fbb2011-02-25 18:07:06 -08001292static 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 -07001293{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001294 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 struct sk_buff *resp_skb;
1296 struct sadb_x_sa2 *sa2;
1297 struct sadb_address *saddr, *daddr;
1298 struct sadb_msg *out_hdr;
Herbert Xu658b2192007-10-09 13:29:52 -07001299 struct sadb_spirange *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 struct xfrm_state *x = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001301 int mode;
Herbert Xu658b2192007-10-09 13:29:52 -07001302 int err;
1303 u32 min_spi, max_spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 u32 reqid;
1305 u8 proto;
1306 unsigned short family;
1307 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1308
1309 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1310 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1311 return -EINVAL;
1312
1313 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1314 if (proto == 0)
1315 return -EINVAL;
1316
1317 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001318 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1319 if (mode < 0)
1320 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 reqid = sa2->sadb_x_sa2_reqid;
1322 } else {
1323 mode = 0;
1324 reqid = 0;
1325 }
1326
1327 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1328 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1329
1330 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1331 switch (family) {
1332 case AF_INET:
1333 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1334 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1335 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001336#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 case AF_INET6:
1338 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1339 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1340 break;
1341#endif
1342 }
1343
1344 if (hdr->sadb_msg_seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001345 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001346 if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 xfrm_state_put(x);
1348 x = NULL;
1349 }
1350 }
1351
1352 if (!x)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001353 x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 if (x == NULL)
1356 return -ENOENT;
1357
Herbert Xu658b2192007-10-09 13:29:52 -07001358 min_spi = 0x100;
1359 max_spi = 0x0fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Herbert Xu658b2192007-10-09 13:29:52 -07001361 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1362 if (range) {
1363 min_spi = range->sadb_spirange_min;
1364 max_spi = range->sadb_spirange_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Herbert Xu658b2192007-10-09 13:29:52 -07001366
Fan Du776e9dd2013-12-16 18:47:49 +08001367 err = verify_spi_info(x->id.proto, min_spi, max_spi);
1368 if (err) {
1369 xfrm_state_put(x);
1370 return err;
1371 }
1372
Herbert Xu658b2192007-10-09 13:29:52 -07001373 err = xfrm_alloc_spi(x, min_spi, max_spi);
Herbert Xu050f0092007-10-09 13:31:47 -07001374 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 if (IS_ERR(resp_skb)) {
1377 xfrm_state_put(x);
1378 return PTR_ERR(resp_skb);
1379 }
1380
1381 out_hdr = (struct sadb_msg *) resp_skb->data;
1382 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1383 out_hdr->sadb_msg_type = SADB_GETSPI;
1384 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1385 out_hdr->sadb_msg_errno = 0;
1386 out_hdr->sadb_msg_reserved = 0;
1387 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1388 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1389
1390 xfrm_state_put(x);
1391
David Ahernba51b6b2015-08-24 15:17:17 -06001392 pfkey_broadcast(resp_skb, BROADCAST_ONE, sk, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 return 0;
1395}
1396
David S. Miller4c93fbb2011-02-25 18:07:06 -08001397static 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 -07001398{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001399 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 struct xfrm_state *x;
1401
1402 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1403 return -EOPNOTSUPP;
1404
1405 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1406 return 0;
1407
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001408 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (x == NULL)
1410 return 0;
1411
1412 spin_lock_bh(&x->lock);
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001413 if (x->km.state == XFRM_STATE_ACQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 x->km.state = XFRM_STATE_ERROR;
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 spin_unlock_bh(&x->lock);
1417 xfrm_state_put(x);
1418 return 0;
1419}
1420
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001421static inline int event2poltype(int event)
1422{
1423 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001424 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001425 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001426 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001427 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001428 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001429 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001430 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001431 // return SADB_X_SPDEXPIRE;
1432 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001433 pr_err("pfkey: Unknown policy event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001434 break;
1435 }
1436
1437 return 0;
1438}
1439
1440static inline int event2keytype(int event)
1441{
1442 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001443 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001444 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001445 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001446 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001447 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001448 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001449 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001450 return SADB_EXPIRE;
1451 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001452 pr_err("pfkey: Unknown SA event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001453 break;
1454 }
1455
1456 return 0;
1457}
1458
1459/* ADD/UPD/DEL */
David S. Miller214e0052011-02-24 00:02:38 -05001460static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001461{
1462 struct sk_buff *skb;
1463 struct sadb_msg *hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001464
Herbert Xu050f0092007-10-09 13:31:47 -07001465 skb = pfkey_xfrm_state2msg(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001466
1467 if (IS_ERR(skb))
1468 return PTR_ERR(skb);
1469
1470 hdr = (struct sadb_msg *) skb->data;
1471 hdr->sadb_msg_version = PF_KEY_V2;
1472 hdr->sadb_msg_type = event2keytype(c->event);
1473 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1474 hdr->sadb_msg_errno = 0;
1475 hdr->sadb_msg_reserved = 0;
1476 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001477 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001478
David Ahernba51b6b2015-08-24 15:17:17 -06001479 pfkey_broadcast(skb, BROADCAST_ALL, NULL, xs_net(x));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001480
1481 return 0;
1482}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
David S. Miller4c93fbb2011-02-25 18:07:06 -08001484static 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 -07001485{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001486 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 struct xfrm_state *x;
1488 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001489 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001491 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (IS_ERR(x))
1493 return PTR_ERR(x);
1494
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001495 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (hdr->sadb_msg_type == SADB_ADD)
1497 err = xfrm_state_add(x);
1498 else
1499 err = xfrm_state_update(x);
1500
Tetsuo Handa2e710292014-04-22 21:48:30 +09001501 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (err < 0) {
1504 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001505 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
1508
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001509 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001510 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001511 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001512 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001513 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001514 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001515 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001516out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001517 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001518 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
David S. Miller4c93fbb2011-02-25 18:07:06 -08001521static 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 -07001522{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001523 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001525 struct km_event c;
1526 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 if (!ext_hdrs[SADB_EXT_SA-1] ||
1529 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1530 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1531 return -EINVAL;
1532
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001533 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (x == NULL)
1535 return -ESRCH;
1536
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001537 if ((err = security_xfrm_state_delete(x)))
1538 goto out;
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001541 err = -EPERM;
1542 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
Joy Latten161a09e2006-11-27 13:11:54 -06001544
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001545 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001546
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001547 if (err < 0)
1548 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001549
1550 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001551 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001552 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001553 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001554out:
Tetsuo Handa2e710292014-04-22 21:48:30 +09001555 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 xfrm_state_put(x);
1557
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001558 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
David S. Miller4c93fbb2011-02-25 18:07:06 -08001561static 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 -07001562{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001563 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 __u8 proto;
1565 struct sk_buff *out_skb;
1566 struct sadb_msg *out_hdr;
1567 struct xfrm_state *x;
1568
1569 if (!ext_hdrs[SADB_EXT_SA-1] ||
1570 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1571 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1572 return -EINVAL;
1573
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001574 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (x == NULL)
1576 return -ESRCH;
1577
Herbert Xu050f0092007-10-09 13:31:47 -07001578 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 proto = x->id.proto;
1580 xfrm_state_put(x);
1581 if (IS_ERR(out_skb))
1582 return PTR_ERR(out_skb);
1583
1584 out_hdr = (struct sadb_msg *) out_skb->data;
1585 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Charles Hardin435000b2007-11-22 19:35:15 +08001586 out_hdr->sadb_msg_type = SADB_GET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1588 out_hdr->sadb_msg_errno = 0;
1589 out_hdr->sadb_msg_reserved = 0;
1590 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1591 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
David Ahernba51b6b2015-08-24 15:17:17 -06001592 pfkey_broadcast(out_skb, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 return 0;
1595}
1596
David S. Miller4c93fbb2011-02-25 18:07:06 -08001597static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001598 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 struct sk_buff *skb;
1601 struct sadb_msg *hdr;
1602 int len, auth_len, enc_len, i;
1603
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001604 auth_len = xfrm_count_pfkey_auth_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (auth_len) {
1606 auth_len *= sizeof(struct sadb_alg);
1607 auth_len += sizeof(struct sadb_supported);
1608 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001609
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001610 enc_len = xfrm_count_pfkey_enc_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (enc_len) {
1612 enc_len *= sizeof(struct sadb_alg);
1613 enc_len += sizeof(struct sadb_supported);
1614 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 len = enc_len + auth_len + sizeof(struct sadb_msg);
1617
1618 skb = alloc_skb(len + 16, allocation);
1619 if (!skb)
1620 goto out_put_algs;
1621
Johannes Berg4df864c2017-06-16 14:29:21 +02001622 hdr = skb_put(skb, sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 pfkey_hdr_dup(hdr, orig);
1624 hdr->sadb_msg_errno = 0;
1625 hdr->sadb_msg_len = len / sizeof(uint64_t);
1626
1627 if (auth_len) {
1628 struct sadb_supported *sp;
1629 struct sadb_alg *ap;
1630
Johannes Berg4df864c2017-06-16 14:29:21 +02001631 sp = skb_put(skb, auth_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 ap = (struct sadb_alg *) (sp + 1);
1633
1634 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1635 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1636
1637 for (i = 0; ; i++) {
1638 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1639 if (!aalg)
1640 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001641 if (!aalg->pfkey_supported)
1642 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (aalg->available)
1644 *ap++ = aalg->desc;
1645 }
1646 }
1647
1648 if (enc_len) {
1649 struct sadb_supported *sp;
1650 struct sadb_alg *ap;
1651
Johannes Berg4df864c2017-06-16 14:29:21 +02001652 sp = skb_put(skb, enc_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 ap = (struct sadb_alg *) (sp + 1);
1654
1655 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1656 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1657
1658 for (i = 0; ; i++) {
1659 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1660 if (!ealg)
1661 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001662 if (!ealg->pfkey_supported)
1663 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (ealg->available)
1665 *ap++ = ealg->desc;
1666 }
1667 }
1668
1669out_put_algs:
1670 return skb;
1671}
1672
David S. Miller4c93fbb2011-02-25 18:07:06 -08001673static 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 -07001674{
1675 struct pfkey_sock *pfk = pfkey_sk(sk);
1676 struct sk_buff *supp_skb;
1677
1678 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1679 return -EINVAL;
1680
1681 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1682 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1683 return -EEXIST;
1684 pfk->registered |= (1<<hdr->sadb_msg_satype);
1685 }
1686
1687 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1690 if (!supp_skb) {
1691 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1692 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1693
1694 return -ENOBUFS;
1695 }
1696
David Ahernba51b6b2015-08-24 15:17:17 -06001697 pfkey_broadcast(supp_skb, BROADCAST_REGISTERED, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 return 0;
1700}
1701
David S. Miller4c93fbb2011-02-25 18:07:06 -08001702static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001703{
1704 struct sk_buff *skb;
1705 struct sadb_msg *hdr;
1706
1707 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1708 if (!skb)
1709 return -ENOBUFS;
1710
Johannes Berg59ae1d12017-06-16 14:29:20 +02001711 hdr = skb_put_data(skb, ihdr, sizeof(struct sadb_msg));
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001712 hdr->sadb_msg_errno = (uint8_t) 0;
1713 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1714
David Ahernba51b6b2015-08-24 15:17:17 -06001715 return pfkey_broadcast(skb, BROADCAST_ONE, sk, sock_net(sk));
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001716}
1717
David S. Miller214e0052011-02-24 00:02:38 -05001718static int key_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001719{
1720 struct sk_buff *skb;
1721 struct sadb_msg *hdr;
1722
1723 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1724 if (!skb)
1725 return -ENOBUFS;
Johannes Berg4df864c2017-06-16 14:29:21 +02001726 hdr = skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf088672005-06-18 22:44:00 -07001727 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001728 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001729 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001730 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001731 hdr->sadb_msg_version = PF_KEY_V2;
1732 hdr->sadb_msg_errno = (uint8_t) 0;
1733 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02001734 hdr->sadb_msg_reserved = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001735
David Ahernba51b6b2015-08-24 15:17:17 -06001736 pfkey_broadcast(skb, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001737
1738 return 0;
1739}
1740
David S. Miller4c93fbb2011-02-25 18:07:06 -08001741static 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 -07001742{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001743 struct net *net = sock_net(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +00001744 unsigned int proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001745 struct km_event c;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001746 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1749 if (proto == 0)
1750 return -EINVAL;
1751
Tetsuo Handa2e710292014-04-22 21:48:30 +09001752 err = xfrm_state_flush(net, proto, true);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001753 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001754 if (err || err2) {
1755 if (err == -ESRCH) /* empty table - go quietly */
1756 err = 0;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001757 return err ? err : err2;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001758 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001759
Herbert Xubf088672005-06-18 22:44:00 -07001760 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001761 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001762 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001763 c.event = XFRM_MSG_FLUSHSA;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001764 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001765 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 return 0;
1768}
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1771{
Timo Teras83321d62008-03-03 23:40:12 -08001772 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 struct sk_buff *out_skb;
1774 struct sadb_msg *out_hdr;
1775
Timo Teras83321d62008-03-03 23:40:12 -08001776 if (!pfkey_can_dump(&pfk->sk))
1777 return -ENOBUFS;
1778
Herbert Xu050f0092007-10-09 13:31:47 -07001779 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (IS_ERR(out_skb))
1781 return PTR_ERR(out_skb);
1782
1783 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08001784 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 out_hdr->sadb_msg_type = SADB_DUMP;
1786 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1787 out_hdr->sadb_msg_errno = 0;
1788 out_hdr->sadb_msg_reserved = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07001789 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001790 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07001791
1792 if (pfk->dump.skb)
David Ahernba51b6b2015-08-24 15:17:17 -06001793 pfkey_broadcast(pfk->dump.skb, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001794 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07001795 pfk->dump.skb = out_skb;
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return 0;
1798}
1799
Timo Teras83321d62008-03-03 23:40:12 -08001800static int pfkey_dump_sa(struct pfkey_sock *pfk)
1801{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001802 struct net *net = sock_net(&pfk->sk);
1803 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08001804}
1805
1806static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1807{
Fan Du283bc9f2013-11-07 17:47:50 +08001808 struct net *net = sock_net(&pfk->sk);
1809
1810 xfrm_state_walk_done(&pfk->dump.u.state, net);
Timo Teras83321d62008-03-03 23:40:12 -08001811}
1812
David S. Miller4c93fbb2011-02-25 18:07:06 -08001813static 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 -07001814{
1815 u8 proto;
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001816 struct xfrm_address_filter *filter = NULL;
Timo Teras83321d62008-03-03 23:40:12 -08001817 struct pfkey_sock *pfk = pfkey_sk(sk);
1818
Yuejie Shi89e357d2017-03-31 15:10:20 +08001819 mutex_lock(&pfk->dump_lock);
1820 if (pfk->dump.dump != NULL) {
1821 mutex_unlock(&pfk->dump_lock);
Timo Teras83321d62008-03-03 23:40:12 -08001822 return -EBUSY;
Yuejie Shi89e357d2017-03-31 15:10:20 +08001823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
Yuejie Shi89e357d2017-03-31 15:10:20 +08001826 if (proto == 0) {
1827 mutex_unlock(&pfk->dump_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return -EINVAL;
Yuejie Shi89e357d2017-03-31 15:10:20 +08001829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Nicolas Dichteld3623092014-02-14 15:30:36 +01001831 if (ext_hdrs[SADB_X_EXT_FILTER - 1]) {
1832 struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1];
1833
1834 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
Yuejie Shi89e357d2017-03-31 15:10:20 +08001835 if (filter == NULL) {
1836 mutex_unlock(&pfk->dump_lock);
Nicolas Dichteld3623092014-02-14 15:30:36 +01001837 return -ENOMEM;
Yuejie Shi89e357d2017-03-31 15:10:20 +08001838 }
Nicolas Dichteld3623092014-02-14 15:30:36 +01001839
1840 memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr,
1841 sizeof(xfrm_address_t));
1842 memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr,
1843 sizeof(xfrm_address_t));
1844 filter->family = xfilter->sadb_x_filter_family;
1845 filter->splen = xfilter->sadb_x_filter_splen;
1846 filter->dplen = xfilter->sadb_x_filter_dplen;
1847 }
1848
Timo Teras83321d62008-03-03 23:40:12 -08001849 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001850 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08001851 pfk->dump.dump = pfkey_dump_sa;
1852 pfk->dump.done = pfkey_dump_sa_done;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001853 xfrm_state_walk_init(&pfk->dump.u.state, proto, filter);
Yuejie Shi89e357d2017-03-31 15:10:20 +08001854 mutex_unlock(&pfk->dump_lock);
Timo Teras4c563f72008-02-28 21:31:08 -08001855
Timo Teras83321d62008-03-03 23:40:12 -08001856 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
David S. Miller4c93fbb2011-02-25 18:07:06 -08001859static 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 -07001860{
1861 struct pfkey_sock *pfk = pfkey_sk(sk);
1862 int satype = hdr->sadb_msg_satype;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001863 bool reset_errno = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001866 reset_errno = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (satype != 0 && satype != 1)
1868 return -EINVAL;
1869 pfk->promisc = satype;
1870 }
David S. Miller4c93fbb2011-02-25 18:07:06 -08001871 if (reset_errno && skb_cloned(skb))
1872 skb = skb_copy(skb, GFP_KERNEL);
1873 else
1874 skb = skb_clone(skb, GFP_KERNEL);
1875
1876 if (reset_errno && skb) {
1877 struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
1878 new_hdr->sadb_msg_errno = 0;
1879 }
1880
David Ahernba51b6b2015-08-24 15:17:17 -06001881 pfkey_broadcast(skb, BROADCAST_ALL, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return 0;
1883}
1884
1885static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1886{
1887 int i;
1888 u32 reqid = *(u32*)ptr;
1889
1890 for (i=0; i<xp->xfrm_nr; i++) {
1891 if (xp->xfrm_vec[i].reqid == reqid)
1892 return -EEXIST;
1893 }
1894 return 0;
1895}
1896
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001897static u32 gen_reqid(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Timo Teras4c563f72008-02-28 21:31:08 -08001899 struct xfrm_policy_walk walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 u32 start;
Timo Teras4c563f72008-02-28 21:31:08 -08001901 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1903
1904 start = reqid;
1905 do {
1906 ++reqid;
1907 if (reqid == 0)
1908 reqid = IPSEC_MANUAL_REQID_MAX+1;
Timo Teras4c563f72008-02-28 21:31:08 -08001909 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001910 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
Fan Du283bc9f2013-11-07 17:47:50 +08001911 xfrm_policy_walk_done(&walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001912 if (rc != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return reqid;
1914 } while (reqid != start);
1915 return 0;
1916}
1917
1918static int
1919parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1920{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001921 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001923 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1926 return -ELOOP;
1927
1928 if (rq->sadb_x_ipsecrequest_mode == 0)
1929 return -EINVAL;
1930
1931 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001932 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1933 return -EINVAL;
1934 t->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1936 t->optional = 1;
1937 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1938 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1939 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1940 t->reqid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001941 if (!t->reqid && !(t->reqid = gen_reqid(net)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return -ENOBUFS;
1943 }
1944
1945 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001946 if (t->mode == XFRM_MODE_TUNNEL) {
Herbert Xu096f41d2017-04-13 18:35:59 +08001947 int err;
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001948
Herbert Xu096f41d2017-04-13 18:35:59 +08001949 err = parse_sockaddr_pair(
1950 (struct sockaddr *)(rq + 1),
1951 rq->sadb_x_ipsecrequest_len - sizeof(*rq),
1952 &t->saddr, &t->id.daddr, &t->encap_family);
1953 if (err)
1954 return err;
Miika Komu2718aa72006-11-30 16:41:50 -08001955 } else
1956 t->encap_family = xp->family;
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 /* No way to set this via kame pfkey */
Herbert Xuc5d18e92008-04-22 00:46:42 -07001959 t->allalgs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 xp->xfrm_nr++;
1961 return 0;
1962}
1963
1964static int
1965parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1966{
1967 int err;
1968 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1969 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1970
Dan Carpenterf674e722012-09-27 22:21:19 +00001971 if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
1972 return -EINVAL;
1973
Herbert Xu096f41d2017-04-13 18:35:59 +08001974 while (len >= sizeof(*rq)) {
1975 if (len < rq->sadb_x_ipsecrequest_len ||
1976 rq->sadb_x_ipsecrequest_len < sizeof(*rq))
1977 return -EINVAL;
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1980 return err;
1981 len -= rq->sadb_x_ipsecrequest_len;
1982 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1983 }
1984 return 0;
1985}
1986
David S. Miller4c93fbb2011-02-25 18:07:06 -08001987static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
Trent Jaegerdf718372005-12-13 23:12:27 -08001988{
1989 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1990
1991 if (xfrm_ctx) {
1992 int len = sizeof(struct sadb_x_sec_ctx);
1993 len += xfrm_ctx->ctx_len;
1994 return PFKEY_ALIGN8(len);
1995 }
1996 return 0;
1997}
1998
David S. Miller4c93fbb2011-02-25 18:07:06 -08001999static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
David S. Miller4c93fbb2011-02-25 18:07:06 -08002001 const struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08002003 int socklen = 0;
2004 int i;
2005
2006 for (i=0; i<xp->xfrm_nr; i++) {
2007 t = xp->xfrm_vec + i;
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002008 socklen += pfkey_sockaddr_len(t->encap_family);
Miika Komu2718aa72006-11-30 16:41:50 -08002009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 return sizeof(struct sadb_msg) +
2012 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002013 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 (sockaddr_size * 2) +
2015 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08002016 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
2017 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08002018 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
David S. Miller4c93fbb2011-02-25 18:07:06 -08002021static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 struct sk_buff *skb;
2024 int size;
2025
2026 size = pfkey_xfrm_policy2msg_size(xp);
2027
2028 skb = alloc_skb(size + 16, GFP_ATOMIC);
2029 if (skb == NULL)
2030 return ERR_PTR(-ENOBUFS);
2031
2032 return skb;
2033}
2034
David S. Miller4c93fbb2011-02-25 18:07:06 -08002035static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
2037 struct sadb_msg *hdr;
2038 struct sadb_address *addr;
2039 struct sadb_lifetime *lifetime;
2040 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08002041 struct sadb_x_sec_ctx *sec_ctx;
2042 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 int i;
2044 int size;
2045 int sockaddr_size = pfkey_sockaddr_size(xp->family);
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002046 int socklen = pfkey_sockaddr_len(xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 size = pfkey_xfrm_policy2msg_size(xp);
2049
2050 /* call should fill header later */
Johannes Berg4df864c2017-06-16 14:29:21 +02002051 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 memset(hdr, 0, size); /* XXX do we need this ? */
2053
2054 /* src address */
Johannes Berg4df864c2017-06-16 14:29:21 +02002055 addr = skb_put(skb, 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 */
Johannes Berg4df864c2017-06-16 14:29:21 +02002070 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 addr->sadb_address_len =
2072 (sizeof(struct sadb_address)+sockaddr_size)/
2073 sizeof(uint64_t);
2074 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2075 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002076 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002078
2079 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2080 (struct sockaddr *) (addr + 1),
2081 xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 /* hard time */
Johannes Berg4df864c2017-06-16 14:29:21 +02002084 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 lifetime->sadb_lifetime_len =
2086 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2087 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2088 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2089 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2090 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2091 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2092 /* soft time */
Johannes Berg4df864c2017-06-16 14:29:21 +02002093 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 lifetime->sadb_lifetime_len =
2095 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2096 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2097 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2098 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2099 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2100 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2101 /* current time */
Johannes Berg4df864c2017-06-16 14:29:21 +02002102 lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 lifetime->sadb_lifetime_len =
2104 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2105 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2106 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2107 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2108 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2109 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2110
Johannes Berg4df864c2017-06-16 14:29:21 +02002111 pol = skb_put(skb, sizeof(struct sadb_x_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2113 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2114 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2115 if (xp->action == XFRM_POLICY_ALLOW) {
2116 if (xp->xfrm_nr)
2117 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2118 else
2119 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2120 }
2121 pol->sadb_x_policy_dir = dir+1;
Dan Carpenterff862a42013-07-28 23:04:45 +03002122 pol->sadb_x_policy_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 pol->sadb_x_policy_id = xp->index;
2124 pol->sadb_x_policy_priority = xp->priority;
2125
2126 for (i=0; i<xp->xfrm_nr; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002127 const struct xfrm_tmpl *t = xp->xfrm_vec + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 struct sadb_x_ipsecrequest *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 int req_size;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002130 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 req_size = sizeof(struct sadb_x_ipsecrequest);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002133 if (t->mode == XFRM_MODE_TUNNEL) {
2134 socklen = pfkey_sockaddr_len(t->encap_family);
2135 req_size += socklen * 2;
2136 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 size -= 2*socklen;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002138 }
Johannes Berg4df864c2017-06-16 14:29:21 +02002139 rq = skb_put(skb, req_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 pol->sadb_x_policy_len += req_size/8;
2141 memset(rq, 0, sizeof(*rq));
2142 rq->sadb_x_ipsecrequest_len = req_size;
2143 rq->sadb_x_ipsecrequest_proto = t->id.proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002144 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2145 return -EINVAL;
David S. Millerfefaa752007-04-17 21:48:10 -07002146 rq->sadb_x_ipsecrequest_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2148 if (t->reqid)
2149 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2150 if (t->optional)
2151 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2152 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002154 if (t->mode == XFRM_MODE_TUNNEL) {
2155 u8 *sa = (void *)(rq + 1);
2156 pfkey_sockaddr_fill(&t->saddr, 0,
2157 (struct sockaddr *)sa,
2158 t->encap_family);
2159 pfkey_sockaddr_fill(&t->id.daddr, 0,
2160 (struct sockaddr *) (sa + socklen),
2161 t->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002164
2165 /* security context */
2166 if ((xfrm_ctx = xp->security)) {
2167 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2168
Johannes Berg4df864c2017-06-16 14:29:21 +02002169 sec_ctx = skb_put(skb, ctx_size);
Trent Jaegerdf718372005-12-13 23:12:27 -08002170 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2171 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2172 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2173 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2174 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2175 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2176 xfrm_ctx->ctx_len);
2177 }
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 hdr->sadb_msg_len = size / sizeof(uint64_t);
Reshetova, Elena850a6212017-07-04 15:53:22 +03002180 hdr->sadb_msg_reserved = refcount_read(&xp->refcnt);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002181
2182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
David S. Miller214e0052011-02-24 00:02:38 -05002185static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002186{
2187 struct sk_buff *out_skb;
2188 struct sadb_msg *out_hdr;
2189 int err;
2190
2191 out_skb = pfkey_xfrm_policy2msg_prep(xp);
Dan Carpenter9a127aa2010-03-24 01:47:00 +00002192 if (IS_ERR(out_skb))
2193 return PTR_ERR(out_skb);
2194
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002195 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2196 if (err < 0)
2197 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002198
2199 out_hdr = (struct sadb_msg *) out_skb->data;
2200 out_hdr->sadb_msg_version = PF_KEY_V2;
2201
Herbert Xuf60f6b82005-06-18 22:44:37 -07002202 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002203 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2204 else
2205 out_hdr->sadb_msg_type = event2poltype(c->event);
2206 out_hdr->sadb_msg_errno = 0;
2207 out_hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002208 out_hdr->sadb_msg_pid = c->portid;
David Ahernba51b6b2015-08-24 15:17:17 -06002209 pfkey_broadcast(out_skb, BROADCAST_ALL, NULL, xp_net(xp));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002210 return 0;
2211
2212}
2213
David S. Miller4c93fbb2011-02-25 18:07:06 -08002214static 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 -07002215{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002216 struct net *net = sock_net(sk);
Trent Jaegerdf718372005-12-13 23:12:27 -08002217 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct sadb_lifetime *lifetime;
2219 struct sadb_address *sa;
2220 struct sadb_x_policy *pol;
2221 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002222 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002223 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2226 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2227 !ext_hdrs[SADB_X_EXT_POLICY-1])
2228 return -EINVAL;
2229
2230 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2231 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2232 return -EINVAL;
2233 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2234 return -EINVAL;
2235
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002236 xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (xp == NULL)
2238 return -ENOBUFS;
2239
2240 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2241 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2242 xp->priority = pol->sadb_x_policy_priority;
2243
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002244 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 xp->selector.family = xp->family;
2247 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2248 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2249 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2250 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002251 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002253 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2255 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2256
2257 /* Amusing, we set this twice. KAME apps appear to set same value
2258 * in both addresses.
2259 */
2260 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2261
2262 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2263 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002264 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Joe Perchesea110732011-06-13 16:21:26 +00002266 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002267 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01002268 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002269
2270 if (!uctx) {
2271 err = -ENOBUFS;
2272 goto out;
2273 }
2274
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002275 err = security_xfrm_policy_alloc(&xp->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002276 kfree(uctx);
2277
2278 if (err)
2279 goto out;
2280 }
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 xp->lft.soft_byte_limit = XFRM_INF;
2283 xp->lft.hard_byte_limit = XFRM_INF;
2284 xp->lft.soft_packet_limit = XFRM_INF;
2285 xp->lft.hard_packet_limit = XFRM_INF;
2286 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2287 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2288 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2289 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2290 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2291 }
2292 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2293 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2294 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2295 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2296 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2297 }
2298 xp->xfrm_nr = 0;
2299 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2300 (err = parse_ipsecrequests(xp, pol)) < 0)
2301 goto out;
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2304 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002305
Tetsuo Handa2e710292014-04-22 21:48:30 +09002306 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002307
Trent Jaegerdf718372005-12-13 23:12:27 -08002308 if (err)
2309 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002311 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002312 c.event = XFRM_MSG_UPDPOLICY;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002313 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07002314 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002316 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002317 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002318
2319 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 return 0;
2322
2323out:
Herbert Xu12a169e2008-10-01 07:03:24 -07002324 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08002325 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return err;
2327}
2328
David S. Miller4c93fbb2011-02-25 18:07:06 -08002329static 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 -07002330{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002331 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 int err;
2333 struct sadb_address *sa;
2334 struct sadb_x_policy *pol;
Paul Moore03e1ad72008-04-12 19:07:52 -07002335 struct xfrm_policy *xp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002337 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002338 struct sadb_x_sec_ctx *sec_ctx;
Brian Haley2db3e472008-04-24 20:38:31 -07002339 struct xfrm_sec_ctx *pol_ctx = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2342 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2343 !ext_hdrs[SADB_X_EXT_POLICY-1])
2344 return -EINVAL;
2345
2346 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2347 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2348 return -EINVAL;
2349
2350 memset(&sel, 0, sizeof(sel));
2351
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002352 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2354 sel.prefixlen_s = sa->sadb_address_prefixlen;
2355 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2356 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2357 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002358 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002360 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2362 sel.prefixlen_d = sa->sadb_address_prefixlen;
2363 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2364 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2365 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002366 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Joe Perchesea110732011-06-13 16:21:26 +00002368 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002369 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01002370 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002371
2372 if (!uctx)
2373 return -ENOMEM;
2374
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002375 err = security_xfrm_policy_alloc(&pol_ctx, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002376 kfree(uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002377 if (err)
2378 return err;
Brian Haley2db3e472008-04-24 20:38:31 -07002379 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002380
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002381 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Paul Moore03e1ad72008-04-12 19:07:52 -07002382 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2383 1, &err);
2384 security_xfrm_policy_free(pol_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (xp == NULL)
2386 return -ENOENT;
2387
Tetsuo Handa2e710292014-04-22 21:48:30 +09002388 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08002389
2390 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002391 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08002392
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002393 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002394 c.portid = hdr->sadb_msg_pid;
Tobias Brunner1839faa2008-10-10 14:07:03 -07002395 c.data.byid = 0;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002396 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002397 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2398
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002399out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002400 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00002401 if (err == 0)
2402 xfrm_garbage_collect(net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002403 return err;
2404}
2405
David S. Miller4c93fbb2011-02-25 18:07:06 -08002406static 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 -07002407{
2408 int err;
2409 struct sk_buff *out_skb;
2410 struct sadb_msg *out_hdr;
2411 err = 0;
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2414 if (IS_ERR(out_skb)) {
2415 err = PTR_ERR(out_skb);
2416 goto out;
2417 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002418 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2419 if (err < 0)
2420 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 out_hdr = (struct sadb_msg *) out_skb->data;
2423 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002424 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 out_hdr->sadb_msg_satype = 0;
2426 out_hdr->sadb_msg_errno = 0;
2427 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2428 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
David Ahernba51b6b2015-08-24 15:17:17 -06002429 pfkey_broadcast(out_skb, BROADCAST_ONE, sk, xp_net(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 err = 0;
2431
2432out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return err;
2434}
2435
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002436static int pfkey_sockaddr_pair_size(sa_family_t family)
2437{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002438 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002439}
2440
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002441static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002442 xfrm_address_t *saddr, xfrm_address_t *daddr,
2443 u16 *family)
2444{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002445 int af, socklen;
2446
Herbert Xu096f41d2017-04-13 18:35:59 +08002447 if (ext_len < 2 || ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002448 return -EINVAL;
2449
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002450 af = pfkey_sockaddr_extract(sa, saddr);
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002451 if (!af)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002452 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002453
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002454 socklen = pfkey_sockaddr_len(af);
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002455 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002456 daddr) != af)
2457 return -EINVAL;
2458
2459 *family = af;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002460 return 0;
2461}
2462
Herbert Xu096f41d2017-04-13 18:35:59 +08002463#ifdef CONFIG_NET_KEY_MIGRATE
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002464static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2465 struct xfrm_migrate *m)
2466{
2467 int err;
2468 struct sadb_x_ipsecrequest *rq2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002469 int mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002470
Herbert Xu096f41d2017-04-13 18:35:59 +08002471 if (len < sizeof(*rq1) ||
2472 len < rq1->sadb_x_ipsecrequest_len ||
2473 rq1->sadb_x_ipsecrequest_len < sizeof(*rq1))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002474 return -EINVAL;
2475
2476 /* old endoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002477 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
Herbert Xu096f41d2017-04-13 18:35:59 +08002478 rq1->sadb_x_ipsecrequest_len - sizeof(*rq1),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002479 &m->old_saddr, &m->old_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002480 &m->old_family);
2481 if (err)
2482 return err;
2483
2484 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2485 len -= rq1->sadb_x_ipsecrequest_len;
2486
Herbert Xu096f41d2017-04-13 18:35:59 +08002487 if (len <= sizeof(*rq2) ||
2488 len < rq2->sadb_x_ipsecrequest_len ||
2489 rq2->sadb_x_ipsecrequest_len < sizeof(*rq2))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002490 return -EINVAL;
2491
2492 /* new endpoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002493 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
Herbert Xu096f41d2017-04-13 18:35:59 +08002494 rq2->sadb_x_ipsecrequest_len - sizeof(*rq2),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002495 &m->new_saddr, &m->new_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002496 &m->new_family);
2497 if (err)
2498 return err;
2499
2500 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2501 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2502 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2503 return -EINVAL;
2504
2505 m->proto = rq1->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002506 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2507 return -EINVAL;
2508 m->mode = mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002509 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2510
2511 return ((int)(rq1->sadb_x_ipsecrequest_len +
2512 rq2->sadb_x_ipsecrequest_len));
2513}
2514
2515static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002516 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002517{
2518 int i, len, ret, err = -EINVAL;
2519 u8 dir;
2520 struct sadb_address *sa;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002521 struct sadb_x_kmaddress *kma;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002522 struct sadb_x_policy *pol;
2523 struct sadb_x_ipsecrequest *rq;
2524 struct xfrm_selector sel;
2525 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002526 struct xfrm_kmaddress k;
Fan Du8d549c42013-11-07 17:47:49 +08002527 struct net *net = sock_net(sk);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002528
2529 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002530 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002531 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2532 err = -EINVAL;
2533 goto out;
2534 }
2535
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002536 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002537 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002538
2539 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2540 err = -EINVAL;
2541 goto out;
2542 }
2543
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002544 if (kma) {
2545 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2546 k.reserved = kma->sadb_x_kmaddress_reserved;
2547 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2548 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2549 &k.local, &k.remote, &k.family);
2550 if (ret < 0) {
2551 err = ret;
2552 goto out;
2553 }
2554 }
2555
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002556 dir = pol->sadb_x_policy_dir - 1;
2557 memset(&sel, 0, sizeof(sel));
2558
2559 /* set source address info of selector */
2560 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2561 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2562 sel.prefixlen_s = sa->sadb_address_prefixlen;
2563 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2564 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2565 if (sel.sport)
Al Viro582ee432007-07-26 17:33:39 +01002566 sel.sport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002567
2568 /* set destination address info of selector */
Himangi Saraogi47162c02014-05-30 21:18:12 +05302569 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002570 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2571 sel.prefixlen_d = sa->sadb_address_prefixlen;
2572 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2573 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2574 if (sel.dport)
Al Viro582ee432007-07-26 17:33:39 +01002575 sel.dport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002576
2577 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2578
2579 /* extract ipsecrequests */
2580 i = 0;
2581 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2582
2583 while (len > 0 && i < XFRM_MAX_DEPTH) {
2584 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2585 if (ret < 0) {
2586 err = ret;
2587 goto out;
2588 } else {
2589 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2590 len -= ret;
2591 i++;
2592 }
2593 }
2594
2595 if (!i || len > 0) {
2596 err = -EINVAL;
2597 goto out;
2598 }
2599
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002600 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
Antony Antony4ab47d42017-06-06 12:12:13 +02002601 kma ? &k : NULL, net, NULL);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002602
2603 out:
2604 return err;
2605}
2606#else
2607static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
Stephen Hemminger7f6daa62011-03-01 22:51:52 -08002608 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002609{
2610 return -ENOPROTOOPT;
2611}
2612#endif
2613
2614
David S. Miller4c93fbb2011-02-25 18:07:06 -08002615static 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 -07002616{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002617 struct net *net = sock_net(sk);
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002618 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002619 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 struct sadb_x_policy *pol;
2621 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002622 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2625 return -EINVAL;
2626
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002627 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2628 if (dir >= XFRM_POLICY_MAX)
2629 return -EINVAL;
2630
Eric Paris215a2dd2007-03-07 16:01:45 -08002631 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002632 xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002633 dir, pol->sadb_x_policy_id, delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (xp == NULL)
2635 return -ENOENT;
2636
Eric Paris215a2dd2007-03-07 16:01:45 -08002637 if (delete) {
Tetsuo Handa2e710292014-04-22 21:48:30 +09002638 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Eric Paris215a2dd2007-03-07 16:01:45 -08002640 if (err)
2641 goto out;
2642 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002643 c.portid = hdr->sadb_msg_pid;
Herbert Xubf088672005-06-18 22:44:00 -07002644 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002645 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002646 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002647 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002648 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Eric Paris215a2dd2007-03-07 16:01:45 -08002651out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00002653 if (delete && err == 0)
2654 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 return err;
2656}
2657
2658static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2659{
Timo Teras83321d62008-03-03 23:40:12 -08002660 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 struct sk_buff *out_skb;
2662 struct sadb_msg *out_hdr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002663 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Timo Teras83321d62008-03-03 23:40:12 -08002665 if (!pfkey_can_dump(&pfk->sk))
2666 return -ENOBUFS;
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2669 if (IS_ERR(out_skb))
2670 return PTR_ERR(out_skb);
2671
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002672 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2673 if (err < 0)
2674 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
2676 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08002677 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2679 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2680 out_hdr->sadb_msg_errno = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07002681 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002682 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07002683
2684 if (pfk->dump.skb)
David Ahernba51b6b2015-08-24 15:17:17 -06002685 pfkey_broadcast(pfk->dump.skb, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002686 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07002687 pfk->dump.skb = out_skb;
2688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return 0;
2690}
2691
Timo Teras83321d62008-03-03 23:40:12 -08002692static int pfkey_dump_sp(struct pfkey_sock *pfk)
2693{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002694 struct net *net = sock_net(&pfk->sk);
2695 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08002696}
2697
2698static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2699{
Fan Du283bc9f2013-11-07 17:47:50 +08002700 struct net *net = sock_net((struct sock *)pfk);
2701
2702 xfrm_policy_walk_done(&pfk->dump.u.policy, net);
Timo Teras83321d62008-03-03 23:40:12 -08002703}
2704
David S. Miller4c93fbb2011-02-25 18:07:06 -08002705static 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 -07002706{
Timo Teras83321d62008-03-03 23:40:12 -08002707 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Yuejie Shi89e357d2017-03-31 15:10:20 +08002709 mutex_lock(&pfk->dump_lock);
2710 if (pfk->dump.dump != NULL) {
2711 mutex_unlock(&pfk->dump_lock);
Timo Teras83321d62008-03-03 23:40:12 -08002712 return -EBUSY;
Yuejie Shi89e357d2017-03-31 15:10:20 +08002713 }
Timo Teras4c563f72008-02-28 21:31:08 -08002714
Timo Teras83321d62008-03-03 23:40:12 -08002715 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002716 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08002717 pfk->dump.dump = pfkey_dump_sp;
2718 pfk->dump.done = pfkey_dump_sp_done;
2719 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
Yuejie Shi89e357d2017-03-31 15:10:20 +08002720 mutex_unlock(&pfk->dump_lock);
Timo Teras83321d62008-03-03 23:40:12 -08002721
2722 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
David S. Miller214e0052011-02-24 00:02:38 -05002725static int key_notify_policy_flush(const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
2727 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002728 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002730 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 if (!skb_out)
2732 return -ENOBUFS;
Johannes Berg4df864c2017-06-16 14:29:21 +02002733 hdr = skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002734 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002735 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002736 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002737 hdr->sadb_msg_version = PF_KEY_V2;
2738 hdr->sadb_msg_errno = (uint8_t) 0;
Nicolas Dichtel85dfb742013-02-18 16:24:20 +01002739 hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002740 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02002741 hdr->sadb_msg_reserved = 0;
David Ahernba51b6b2015-08-24 15:17:17 -06002742 pfkey_broadcast(skb_out, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002743 return 0;
2744
2745}
2746
David S. Miller4c93fbb2011-02-25 18:07:06 -08002747static 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 -07002748{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002749 struct net *net = sock_net(sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002750 struct km_event c;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002751 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Tetsuo Handa2e710292014-04-22 21:48:30 +09002753 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true);
Hangbin Liu138437f2017-06-11 09:44:20 +08002754 if (!err)
2755 xfrm_garbage_collect(net);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002756 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002757 if (err || err2) {
2758 if (err == -ESRCH) /* empty table - old silent behavior */
2759 return 0;
2760 return err;
2761 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002762
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002763 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002764 c.event = XFRM_MSG_FLUSHPOLICY;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002765 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002766 c.seq = hdr->sadb_msg_seq;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002767 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002768 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770 return 0;
2771}
2772
2773typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002774 const struct sadb_msg *hdr, void * const *ext_hdrs);
Mathias Krause8603b9552013-06-26 23:56:59 +02002775static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 [SADB_RESERVED] = pfkey_reserved,
2777 [SADB_GETSPI] = pfkey_getspi,
2778 [SADB_UPDATE] = pfkey_add,
2779 [SADB_ADD] = pfkey_add,
2780 [SADB_DELETE] = pfkey_delete,
2781 [SADB_GET] = pfkey_get,
2782 [SADB_ACQUIRE] = pfkey_acquire,
2783 [SADB_REGISTER] = pfkey_register,
2784 [SADB_EXPIRE] = NULL,
2785 [SADB_FLUSH] = pfkey_flush,
2786 [SADB_DUMP] = pfkey_dump,
2787 [SADB_X_PROMISC] = pfkey_promisc,
2788 [SADB_X_PCHANGE] = NULL,
2789 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2790 [SADB_X_SPDADD] = pfkey_spdadd,
2791 [SADB_X_SPDDELETE] = pfkey_spddelete,
2792 [SADB_X_SPDGET] = pfkey_spdget,
2793 [SADB_X_SPDACQUIRE] = NULL,
2794 [SADB_X_SPDDUMP] = pfkey_spddump,
2795 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2796 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2797 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002798 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799};
2800
David S. Miller4c93fbb2011-02-25 18:07:06 -08002801static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
2803 void *ext_hdrs[SADB_EXT_MAX];
2804 int err;
2805
David Ahernba51b6b2015-08-24 15:17:17 -06002806 pfkey_broadcast(skb_clone(skb, GFP_KERNEL),
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002807 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2810 err = parse_exthdrs(skb, hdr, ext_hdrs);
2811 if (!err) {
2812 err = -EOPNOTSUPP;
2813 if (pfkey_funcs[hdr->sadb_msg_type])
2814 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2815 }
2816 return err;
2817}
2818
2819static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2820{
2821 struct sadb_msg *hdr = NULL;
2822
2823 if (skb->len < sizeof(*hdr)) {
2824 *errp = -EMSGSIZE;
2825 } else {
2826 hdr = (struct sadb_msg *) skb->data;
2827 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2828 hdr->sadb_msg_reserved != 0 ||
2829 (hdr->sadb_msg_type <= SADB_RESERVED ||
2830 hdr->sadb_msg_type > SADB_MAX)) {
2831 hdr = NULL;
2832 *errp = -EINVAL;
2833 } else if (hdr->sadb_msg_len != (skb->len /
2834 sizeof(uint64_t)) ||
2835 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2836 sizeof(uint64_t))) {
2837 hdr = NULL;
2838 *errp = -EMSGSIZE;
2839 } else {
2840 *errp = 0;
2841 }
2842 }
2843 return hdr;
2844}
2845
David S. Miller4c93fbb2011-02-25 18:07:06 -08002846static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
2847 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Herbert Xuf3980352007-12-19 23:44:29 -08002849 unsigned int id = d->desc.sadb_alg_id;
2850
2851 if (id >= sizeof(t->aalgos) * 8)
2852 return 0;
2853
2854 return (t->aalgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855}
2856
David S. Miller4c93fbb2011-02-25 18:07:06 -08002857static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
2858 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
Herbert Xuf3980352007-12-19 23:44:29 -08002860 unsigned int id = d->desc.sadb_alg_id;
2861
2862 if (id >= sizeof(t->ealgos) * 8)
2863 return 0;
2864
2865 return (t->ealgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866}
2867
David S. Miller4c93fbb2011-02-25 18:07:06 -08002868static int count_ah_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
2870 int i, sz = 0;
2871
2872 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002873 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 if (!aalg)
2875 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002876 if (!aalg->pfkey_supported)
2877 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (aalg_tmpl_set(t, aalg) && aalg->available)
2879 sz += sizeof(struct sadb_comb);
2880 }
2881 return sz + sizeof(struct sadb_prop);
2882}
2883
David S. Miller4c93fbb2011-02-25 18:07:06 -08002884static int count_esp_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
2886 int i, k, sz = 0;
2887
2888 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002889 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 if (!ealg)
2891 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002892
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002893 if (!ealg->pfkey_supported)
2894 continue;
2895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2897 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 for (k = 1; ; k++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002900 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (!aalg)
2902 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002903
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002904 if (!aalg->pfkey_supported)
2905 continue;
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 if (aalg_tmpl_set(t, aalg) && aalg->available)
2908 sz += sizeof(struct sadb_comb);
2909 }
2910 }
2911 return sz + sizeof(struct sadb_prop);
2912}
2913
David S. Miller4c93fbb2011-02-25 18:07:06 -08002914static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
2916 struct sadb_prop *p;
2917 int i;
2918
Johannes Berg4df864c2017-06-16 14:29:21 +02002919 p = skb_put(skb, sizeof(struct sadb_prop));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2921 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2922 p->sadb_prop_replay = 32;
2923 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2924
2925 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002926 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 if (!aalg)
2928 break;
2929
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002930 if (!aalg->pfkey_supported)
2931 continue;
2932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2934 struct sadb_comb *c;
Johannes Bergb080db52017-06-16 14:29:19 +02002935 c = skb_put_zero(skb, sizeof(struct sadb_comb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2937 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2938 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2939 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2940 c->sadb_comb_hard_addtime = 24*60*60;
2941 c->sadb_comb_soft_addtime = 20*60*60;
2942 c->sadb_comb_hard_usetime = 8*60*60;
2943 c->sadb_comb_soft_usetime = 7*60*60;
2944 }
2945 }
2946}
2947
David S. Miller4c93fbb2011-02-25 18:07:06 -08002948static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
2950 struct sadb_prop *p;
2951 int i, k;
2952
Johannes Berg4df864c2017-06-16 14:29:21 +02002953 p = skb_put(skb, sizeof(struct sadb_prop));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2955 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2956 p->sadb_prop_replay = 32;
2957 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2958
2959 for (i=0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002960 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 if (!ealg)
2962 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002963
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002964 if (!ealg->pfkey_supported)
2965 continue;
2966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2968 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 for (k = 1; ; k++) {
2971 struct sadb_comb *c;
David S. Miller4c93fbb2011-02-25 18:07:06 -08002972 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (!aalg)
2974 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002975 if (!aalg->pfkey_supported)
2976 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2978 continue;
Johannes Berg4df864c2017-06-16 14:29:21 +02002979 c = skb_put(skb, sizeof(struct sadb_comb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 memset(c, 0, sizeof(*c));
2981 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2982 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2983 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2984 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2985 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2986 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2987 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2988 c->sadb_comb_hard_addtime = 24*60*60;
2989 c->sadb_comb_soft_addtime = 20*60*60;
2990 c->sadb_comb_hard_usetime = 8*60*60;
2991 c->sadb_comb_soft_usetime = 7*60*60;
2992 }
2993 }
2994}
2995
David S. Miller214e0052011-02-24 00:02:38 -05002996static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002997{
2998 return 0;
2999}
3000
David S. Miller214e0052011-02-24 00:02:38 -05003001static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
3003 struct sk_buff *out_skb;
3004 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003005 int hard;
3006 int hsc;
3007
Herbert Xubf088672005-06-18 22:44:00 -07003008 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003009 if (hard)
3010 hsc = 2;
3011 else
3012 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Herbert Xu050f0092007-10-09 13:31:47 -07003014 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 if (IS_ERR(out_skb))
3016 return PTR_ERR(out_skb);
3017
3018 out_hdr = (struct sadb_msg *) out_skb->data;
3019 out_hdr->sadb_msg_version = PF_KEY_V2;
3020 out_hdr->sadb_msg_type = SADB_EXPIRE;
3021 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3022 out_hdr->sadb_msg_errno = 0;
3023 out_hdr->sadb_msg_reserved = 0;
3024 out_hdr->sadb_msg_seq = 0;
3025 out_hdr->sadb_msg_pid = 0;
3026
David Ahernba51b6b2015-08-24 15:17:17 -06003027 pfkey_broadcast(out_skb, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return 0;
3029}
3030
David S. Miller214e0052011-02-24 00:02:38 -05003031static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003032{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003033 struct net *net = x ? xs_net(x) : c->net;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003034 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3035
3036 if (atomic_read(&net_pfkey->socks_nr) == 0)
Jamal Hadi Salim99c6f602008-06-10 14:25:34 -07003037 return 0;
3038
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003039 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003040 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003041 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003042 case XFRM_MSG_DELSA:
3043 case XFRM_MSG_NEWSA:
3044 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003045 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003046 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003047 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003048 case XFRM_MSG_NEWAE: /* not yet supported */
3049 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003050 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003051 pr_err("pfkey: Unknown SA event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003052 break;
3053 }
3054
3055 return 0;
3056}
3057
David S. Miller214e0052011-02-24 00:02:38 -05003058static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003059{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003060 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3061 return 0;
3062
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003063 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003064 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003065 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003066 case XFRM_MSG_DELPOLICY:
3067 case XFRM_MSG_NEWPOLICY:
3068 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003069 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003070 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003071 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3072 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003073 return key_notify_policy_flush(c);
3074 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003075 pr_err("pfkey: Unknown policy event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003076 break;
3077 }
3078
3079 return 0;
3080}
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082static u32 get_acqseq(void)
3083{
3084 u32 res;
Eric Dumazet28aecb92010-01-29 04:05:52 +00003085 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Eric Dumazet28aecb92010-01-29 04:05:52 +00003087 do {
3088 res = atomic_inc_return(&acqseq);
3089 } while (!res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return res;
3091}
3092
Horia Geanta0f245582014-02-12 16:20:06 +02003093static bool pfkey_is_alive(const struct km_event *c)
3094{
3095 struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id);
3096 struct sock *sk;
3097 bool is_alive = false;
3098
3099 rcu_read_lock();
3100 sk_for_each_rcu(sk, &net_pfkey->table) {
3101 if (pfkey_sk(sk)->registered) {
3102 is_alive = true;
3103 break;
3104 }
3105 }
3106 rcu_read_unlock();
3107
3108 return is_alive;
3109}
3110
Fan Du65e07362012-08-15 10:13:47 +08003111static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
3113 struct sk_buff *skb;
3114 struct sadb_msg *hdr;
3115 struct sadb_address *addr;
3116 struct sadb_x_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 int sockaddr_size;
3118 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003119 struct sadb_x_sec_ctx *sec_ctx;
3120 struct xfrm_sec_ctx *xfrm_ctx;
3121 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3124 if (!sockaddr_size)
3125 return -EINVAL;
3126
3127 size = sizeof(struct sadb_msg) +
3128 (sizeof(struct sadb_address) * 2) +
3129 (sockaddr_size * 2) +
3130 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 if (x->id.proto == IPPROTO_AH)
3133 size += count_ah_combs(t);
3134 else if (x->id.proto == IPPROTO_ESP)
3135 size += count_esp_combs(t);
3136
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003137 if ((xfrm_ctx = x->security)) {
3138 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3139 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3140 }
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 skb = alloc_skb(size + 16, GFP_ATOMIC);
3143 if (skb == NULL)
3144 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003145
Johannes Berg4df864c2017-06-16 14:29:21 +02003146 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 hdr->sadb_msg_version = PF_KEY_V2;
3148 hdr->sadb_msg_type = SADB_ACQUIRE;
3149 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3150 hdr->sadb_msg_len = size / sizeof(uint64_t);
3151 hdr->sadb_msg_errno = 0;
3152 hdr->sadb_msg_reserved = 0;
3153 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3154 hdr->sadb_msg_pid = 0;
3155
3156 /* src address */
Johannes Berg4df864c2017-06-16 14:29:21 +02003157 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003158 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 (sizeof(struct sadb_address)+sockaddr_size)/
3160 sizeof(uint64_t);
3161 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3162 addr->sadb_address_proto = 0;
3163 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003164 addr->sadb_address_prefixlen =
3165 pfkey_sockaddr_fill(&x->props.saddr, 0,
3166 (struct sockaddr *) (addr + 1),
3167 x->props.family);
3168 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 /* dst address */
Johannes Berg4df864c2017-06-16 14:29:21 +02003172 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 addr->sadb_address_len =
3174 (sizeof(struct sadb_address)+sockaddr_size)/
3175 sizeof(uint64_t);
3176 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3177 addr->sadb_address_proto = 0;
3178 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003179 addr->sadb_address_prefixlen =
3180 pfkey_sockaddr_fill(&x->id.daddr, 0,
3181 (struct sockaddr *) (addr + 1),
3182 x->props.family);
3183 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 BUG();
3185
Johannes Berg4df864c2017-06-16 14:29:21 +02003186 pol = skb_put(skb, sizeof(struct sadb_x_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3188 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3189 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
Fan Du65e07362012-08-15 10:13:47 +08003190 pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1;
Dan Carpenterff862a42013-07-28 23:04:45 +03003191 pol->sadb_x_policy_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 pol->sadb_x_policy_id = xp->index;
Dan Carpenterff862a42013-07-28 23:04:45 +03003193 pol->sadb_x_policy_priority = xp->priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
3195 /* Set sadb_comb's. */
3196 if (x->id.proto == IPPROTO_AH)
3197 dump_ah_combs(skb, t);
3198 else if (x->id.proto == IPPROTO_ESP)
3199 dump_esp_combs(skb, t);
3200
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003201 /* security context */
3202 if (xfrm_ctx) {
Johannes Berg4df864c2017-06-16 14:29:21 +02003203 sec_ctx = skb_put(skb,
3204 sizeof(struct sadb_x_sec_ctx) + ctx_size);
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003205 sec_ctx->sadb_x_sec_len =
3206 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3207 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3208 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3209 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3210 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3211 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3212 xfrm_ctx->ctx_len);
3213 }
3214
David Ahernba51b6b2015-08-24 15:17:17 -06003215 return pfkey_broadcast(skb, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216}
3217
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003218static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003219 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003221 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 struct xfrm_policy *xp;
3223 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003224 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003226 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 case AF_INET:
3228 if (opt != IP_IPSEC_POLICY) {
3229 *dir = -EOPNOTSUPP;
3230 return NULL;
3231 }
3232 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00003233#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 case AF_INET6:
3235 if (opt != IPV6_IPSEC_POLICY) {
3236 *dir = -EOPNOTSUPP;
3237 return NULL;
3238 }
3239 break;
3240#endif
3241 default:
3242 *dir = -EINVAL;
3243 return NULL;
3244 }
3245
3246 *dir = -EINVAL;
3247
3248 if (len < sizeof(struct sadb_x_policy) ||
3249 pol->sadb_x_policy_len*8 > len ||
3250 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3251 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3252 return NULL;
3253
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003254 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 if (xp == NULL) {
3256 *dir = -ENOBUFS;
3257 return NULL;
3258 }
3259
3260 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3261 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3262
3263 xp->lft.soft_byte_limit = XFRM_INF;
3264 xp->lft.hard_byte_limit = XFRM_INF;
3265 xp->lft.soft_packet_limit = XFRM_INF;
3266 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003267 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269 xp->xfrm_nr = 0;
3270 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3271 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3272 goto out;
3273
Trent Jaegerdf718372005-12-13 23:12:27 -08003274 /* security context too */
3275 if (len >= (pol->sadb_x_policy_len*8 +
3276 sizeof(struct sadb_x_sec_ctx))) {
3277 char *p = (char *)pol;
3278 struct xfrm_user_sec_ctx *uctx;
3279
3280 p += pol->sadb_x_policy_len*8;
3281 sec_ctx = (struct sadb_x_sec_ctx *)p;
3282 if (len < pol->sadb_x_policy_len*8 +
Steffen Klassertd90c9022017-05-05 07:40:42 +02003283 sec_ctx->sadb_x_sec_len*8) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003284 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003285 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003286 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003287 if ((*dir = verify_sec_ctx_len(p)))
3288 goto out;
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01003289 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_ATOMIC);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01003290 *dir = security_xfrm_policy_alloc(&xp->security, uctx, GFP_ATOMIC);
Trent Jaegerdf718372005-12-13 23:12:27 -08003291 kfree(uctx);
3292
3293 if (*dir)
3294 goto out;
3295 }
3296
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 *dir = pol->sadb_x_policy_dir-1;
3298 return xp;
3299
3300out:
Alexey Dobriyan70e90672008-11-06 23:08:37 -08003301 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08003302 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return NULL;
3304}
3305
Al Viro5d36b182006-11-08 00:24:06 -08003306static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
3308 struct sk_buff *skb;
3309 struct sadb_msg *hdr;
3310 struct sadb_sa *sa;
3311 struct sadb_address *addr;
3312 struct sadb_x_nat_t_port *n_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 int sockaddr_size;
3314 int size;
3315 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3316 struct xfrm_encap_tmpl *natt = NULL;
3317
3318 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3319 if (!sockaddr_size)
3320 return -EINVAL;
3321
3322 if (!satype)
3323 return -EINVAL;
3324
3325 if (!x->encap)
3326 return -EINVAL;
3327
3328 natt = x->encap;
3329
3330 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3331 *
3332 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3333 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3334 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 size = sizeof(struct sadb_msg) +
3337 sizeof(struct sadb_sa) +
3338 (sizeof(struct sadb_address) * 2) +
3339 (sockaddr_size * 2) +
3340 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003341
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 skb = alloc_skb(size + 16, GFP_ATOMIC);
3343 if (skb == NULL)
3344 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003345
Johannes Berg4df864c2017-06-16 14:29:21 +02003346 hdr = skb_put(skb, sizeof(struct sadb_msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 hdr->sadb_msg_version = PF_KEY_V2;
3348 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3349 hdr->sadb_msg_satype = satype;
3350 hdr->sadb_msg_len = size / sizeof(uint64_t);
3351 hdr->sadb_msg_errno = 0;
3352 hdr->sadb_msg_reserved = 0;
3353 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3354 hdr->sadb_msg_pid = 0;
3355
3356 /* SA */
Johannes Berg4df864c2017-06-16 14:29:21 +02003357 sa = skb_put(skb, sizeof(struct sadb_sa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3359 sa->sadb_sa_exttype = SADB_EXT_SA;
3360 sa->sadb_sa_spi = x->id.spi;
3361 sa->sadb_sa_replay = 0;
3362 sa->sadb_sa_state = 0;
3363 sa->sadb_sa_auth = 0;
3364 sa->sadb_sa_encrypt = 0;
3365 sa->sadb_sa_flags = 0;
3366
3367 /* ADDRESS_SRC (old addr) */
Johannes Berg4df864c2017-06-16 14:29:21 +02003368 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003369 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 (sizeof(struct sadb_address)+sockaddr_size)/
3371 sizeof(uint64_t);
3372 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3373 addr->sadb_address_proto = 0;
3374 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003375 addr->sadb_address_prefixlen =
3376 pfkey_sockaddr_fill(&x->props.saddr, 0,
3377 (struct sockaddr *) (addr + 1),
3378 x->props.family);
3379 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 BUG();
3381
3382 /* NAT_T_SPORT (old port) */
Johannes Berg4df864c2017-06-16 14:29:21 +02003383 n_port = skb_put(skb, sizeof(*n_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3385 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3386 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3387 n_port->sadb_x_nat_t_port_reserved = 0;
3388
3389 /* ADDRESS_DST (new addr) */
Johannes Berg4df864c2017-06-16 14:29:21 +02003390 addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003391 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 (sizeof(struct sadb_address)+sockaddr_size)/
3393 sizeof(uint64_t);
3394 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3395 addr->sadb_address_proto = 0;
3396 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003397 addr->sadb_address_prefixlen =
3398 pfkey_sockaddr_fill(ipaddr, 0,
3399 (struct sockaddr *) (addr + 1),
3400 x->props.family);
3401 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 BUG();
3403
3404 /* NAT_T_DPORT (new port) */
Johannes Berg4df864c2017-06-16 14:29:21 +02003405 n_port = skb_put(skb, sizeof(*n_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3407 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3408 n_port->sadb_x_nat_t_port_port = sport;
3409 n_port->sadb_x_nat_t_port_reserved = 0;
3410
David Ahernba51b6b2015-08-24 15:17:17 -06003411 return pfkey_broadcast(skb, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412}
3413
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003414#ifdef CONFIG_NET_KEY_MIGRATE
3415static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
David S. Miller183cad12011-02-24 00:28:01 -05003416 const struct xfrm_selector *sel)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003417{
3418 struct sadb_address *addr;
Johannes Berg4df864c2017-06-16 14:29:21 +02003419 addr = skb_put(skb, sizeof(struct sadb_address) + sasize);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003420 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3421 addr->sadb_address_exttype = type;
3422 addr->sadb_address_proto = sel->proto;
3423 addr->sadb_address_reserved = 0;
3424
3425 switch (type) {
3426 case SADB_EXT_ADDRESS_SRC:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003427 addr->sadb_address_prefixlen = sel->prefixlen_s;
3428 pfkey_sockaddr_fill(&sel->saddr, 0,
3429 (struct sockaddr *)(addr + 1),
3430 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003431 break;
3432 case SADB_EXT_ADDRESS_DST:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003433 addr->sadb_address_prefixlen = sel->prefixlen_d;
3434 pfkey_sockaddr_fill(&sel->daddr, 0,
3435 (struct sockaddr *)(addr + 1),
3436 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003437 break;
3438 default:
3439 return -EINVAL;
3440 }
3441
3442 return 0;
3443}
3444
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003445
David S. Miller183cad12011-02-24 00:28:01 -05003446static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003447{
3448 struct sadb_x_kmaddress *kma;
3449 u8 *sa;
3450 int family = k->family;
3451 int socklen = pfkey_sockaddr_len(family);
3452 int size_req;
3453
3454 size_req = (sizeof(struct sadb_x_kmaddress) +
3455 pfkey_sockaddr_pair_size(family));
3456
Johannes Bergb080db52017-06-16 14:29:19 +02003457 kma = skb_put_zero(skb, size_req);
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003458 kma->sadb_x_kmaddress_len = size_req / 8;
3459 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3460 kma->sadb_x_kmaddress_reserved = k->reserved;
3461
3462 sa = (u8 *)(kma + 1);
3463 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3464 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3465 return -EINVAL;
3466
3467 return 0;
3468}
3469
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003470static int set_ipsecrequest(struct sk_buff *skb,
3471 uint8_t proto, uint8_t mode, int level,
3472 uint32_t reqid, uint8_t family,
David S. Miller183cad12011-02-24 00:28:01 -05003473 const xfrm_address_t *src, const xfrm_address_t *dst)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003474{
3475 struct sadb_x_ipsecrequest *rq;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003476 u8 *sa;
3477 int socklen = pfkey_sockaddr_len(family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003478 int size_req;
3479
3480 size_req = sizeof(struct sadb_x_ipsecrequest) +
3481 pfkey_sockaddr_pair_size(family);
3482
Johannes Bergb080db52017-06-16 14:29:19 +02003483 rq = skb_put_zero(skb, size_req);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003484 rq->sadb_x_ipsecrequest_len = size_req;
3485 rq->sadb_x_ipsecrequest_proto = proto;
3486 rq->sadb_x_ipsecrequest_mode = mode;
3487 rq->sadb_x_ipsecrequest_level = level;
3488 rq->sadb_x_ipsecrequest_reqid = reqid;
3489
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003490 sa = (u8 *) (rq + 1);
3491 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3492 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003493 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003494
3495 return 0;
3496}
3497#endif
3498
3499#ifdef CONFIG_NET_KEY_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05003500static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3501 const struct xfrm_migrate *m, int num_bundles,
Antony Antony8bafd732017-06-06 12:12:14 +02003502 const struct xfrm_kmaddress *k,
3503 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003504{
3505 int i;
3506 int sasize_sel;
3507 int size = 0;
3508 int size_pol = 0;
3509 struct sk_buff *skb;
3510 struct sadb_msg *hdr;
3511 struct sadb_x_policy *pol;
David S. Miller183cad12011-02-24 00:28:01 -05003512 const struct xfrm_migrate *mp;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003513
3514 if (type != XFRM_POLICY_TYPE_MAIN)
3515 return 0;
3516
3517 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3518 return -EINVAL;
3519
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003520 if (k != NULL) {
3521 /* addresses for KM */
3522 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3523 pfkey_sockaddr_pair_size(k->family));
3524 }
3525
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003526 /* selector */
3527 sasize_sel = pfkey_sockaddr_size(sel->family);
3528 if (!sasize_sel)
3529 return -EINVAL;
3530 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3531
3532 /* policy info */
3533 size_pol += sizeof(struct sadb_x_policy);
3534
3535 /* ipsecrequests */
3536 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3537 /* old locator pair */
3538 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3539 pfkey_sockaddr_pair_size(mp->old_family);
3540 /* new locator pair */
3541 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3542 pfkey_sockaddr_pair_size(mp->new_family);
3543 }
3544
3545 size += sizeof(struct sadb_msg) + size_pol;
3546
3547 /* alloc buffer */
3548 skb = alloc_skb(size, GFP_ATOMIC);
3549 if (skb == NULL)
3550 return -ENOMEM;
3551
Johannes Berg4df864c2017-06-16 14:29:21 +02003552 hdr = skb_put(skb, sizeof(struct sadb_msg));
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003553 hdr->sadb_msg_version = PF_KEY_V2;
3554 hdr->sadb_msg_type = SADB_X_MIGRATE;
3555 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3556 hdr->sadb_msg_len = size / 8;
3557 hdr->sadb_msg_errno = 0;
3558 hdr->sadb_msg_reserved = 0;
3559 hdr->sadb_msg_seq = 0;
3560 hdr->sadb_msg_pid = 0;
3561
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003562 /* Addresses to be used by KM for negotiation, if ext is available */
3563 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
Julia Lawall89eb06f2012-04-08 22:41:10 +00003564 goto err;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003565
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003566 /* selector src */
3567 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3568
3569 /* selector dst */
3570 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3571
3572 /* policy information */
Johannes Berg4df864c2017-06-16 14:29:21 +02003573 pol = skb_put(skb, sizeof(struct sadb_x_policy));
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003574 pol->sadb_x_policy_len = size_pol / 8;
3575 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3576 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3577 pol->sadb_x_policy_dir = dir + 1;
Dan Carpenterff862a42013-07-28 23:04:45 +03003578 pol->sadb_x_policy_reserved = 0;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003579 pol->sadb_x_policy_id = 0;
3580 pol->sadb_x_policy_priority = 0;
3581
3582 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3583 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003584 int mode = pfkey_mode_from_xfrm(mp->mode);
3585 if (mode < 0)
Patrick McHardyd4782c32008-01-20 17:24:29 -08003586 goto err;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003587 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003588 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3589 mp->reqid, mp->old_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003590 &mp->old_saddr, &mp->old_daddr) < 0)
3591 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003592
3593 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003594 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003595 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3596 mp->reqid, mp->new_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003597 &mp->new_saddr, &mp->new_daddr) < 0)
3598 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003599 }
3600
3601 /* broadcast migrate message to sockets */
David Ahernba51b6b2015-08-24 15:17:17 -06003602 pfkey_broadcast(skb, BROADCAST_ALL, NULL, &init_net);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003603
3604 return 0;
Patrick McHardyd4782c32008-01-20 17:24:29 -08003605
3606err:
3607 kfree_skb(skb);
3608 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003609}
3610#else
David S. Miller183cad12011-02-24 00:28:01 -05003611static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3612 const struct xfrm_migrate *m, int num_bundles,
Antony Antony8bafd732017-06-06 12:12:14 +02003613 const struct xfrm_kmaddress *k,
3614 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003615{
3616 return -ENOPROTOOPT;
3617}
3618#endif
3619
Ying Xue1b784142015-03-02 15:37:48 +08003620static int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
3622 struct sock *sk = sock->sk;
3623 struct sk_buff *skb = NULL;
3624 struct sadb_msg *hdr = NULL;
3625 int err;
Fan Du283bc9f2013-11-07 17:47:50 +08003626 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
3628 err = -EOPNOTSUPP;
3629 if (msg->msg_flags & MSG_OOB)
3630 goto out;
3631
3632 err = -EMSGSIZE;
Eric Dumazet95c96172012-04-15 05:58:06 +00003633 if ((unsigned int)len > sk->sk_sndbuf - 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 goto out;
3635
3636 err = -ENOBUFS;
3637 skb = alloc_skb(len, GFP_KERNEL);
3638 if (skb == NULL)
3639 goto out;
3640
3641 err = -EFAULT;
Al Viro6ce8e9c2014-04-06 21:25:44 -04003642 if (memcpy_from_msg(skb_put(skb,len), msg, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 goto out;
3644
3645 hdr = pfkey_get_base_msg(skb, &err);
3646 if (!hdr)
3647 goto out;
3648
Fan Du283bc9f2013-11-07 17:47:50 +08003649 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 err = pfkey_process(sk, skb, hdr);
Fan Du283bc9f2013-11-07 17:47:50 +08003651 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
3653out:
3654 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3655 err = 0;
Wei Yongjun6f961062009-02-25 00:31:04 +00003656 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
3658 return err ? : len;
3659}
3660
Ying Xue1b784142015-03-02 15:37:48 +08003661static int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 int flags)
3663{
3664 struct sock *sk = sock->sk;
Timo Teras83321d62008-03-03 23:40:12 -08003665 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 struct sk_buff *skb;
3667 int copied, err;
3668
3669 err = -EINVAL;
3670 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3671 goto out;
3672
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3674 if (skb == NULL)
3675 goto out;
3676
3677 copied = skb->len;
3678 if (copied > len) {
3679 msg->msg_flags |= MSG_TRUNC;
3680 copied = len;
3681 }
3682
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003683 skb_reset_transport_header(skb);
David S. Miller51f3d022014-11-05 16:46:40 -05003684 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 if (err)
3686 goto out_free;
3687
Neil Horman3b885782009-10-12 13:26:31 -07003688 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
3690 err = (flags & MSG_TRUNC) ? skb->len : copied;
3691
Timo Teras83321d62008-03-03 23:40:12 -08003692 if (pfk->dump.dump != NULL &&
3693 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3694 pfkey_do_dump(pfk);
3695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696out_free:
3697 skb_free_datagram(sk, skb);
3698out:
3699 return err;
3700}
3701
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003702static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 .family = PF_KEY,
3704 .owner = THIS_MODULE,
3705 /* Operations that make no sense on pfkey sockets. */
3706 .bind = sock_no_bind,
3707 .connect = sock_no_connect,
3708 .socketpair = sock_no_socketpair,
3709 .accept = sock_no_accept,
3710 .getname = sock_no_getname,
3711 .ioctl = sock_no_ioctl,
3712 .listen = sock_no_listen,
3713 .shutdown = sock_no_shutdown,
3714 .setsockopt = sock_no_setsockopt,
3715 .getsockopt = sock_no_getsockopt,
3716 .mmap = sock_no_mmap,
3717 .sendpage = sock_no_sendpage,
3718
3719 /* Now the operations that really occur. */
3720 .release = pfkey_release,
3721 .poll = datagram_poll,
3722 .sendmsg = pfkey_sendmsg,
3723 .recvmsg = pfkey_recvmsg,
3724};
3725
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003726static const struct net_proto_family pfkey_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 .family = PF_KEY,
3728 .create = pfkey_create,
3729 .owner = THIS_MODULE,
3730};
3731
3732#ifdef CONFIG_PROC_FS
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003733static int pfkey_seq_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734{
Li Zefan27b5b862010-02-08 23:20:29 +00003735 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003737 if (v == SEQ_START_TOKEN)
3738 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3739 else
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003740 seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 s,
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003742 refcount_read(&s->sk_refcnt),
Eric Dumazet31e6d362009-06-17 19:05:41 -07003743 sk_rmem_alloc_get(s),
3744 sk_wmem_alloc_get(s),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06003745 from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 sock_i_ino(s)
3747 );
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749}
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003750
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003751static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
stephen hemmingerada440e2011-02-23 09:06:50 +00003752 __acquires(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003753{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003754 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003755 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003756
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003757 rcu_read_lock();
3758 return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003759}
3760
3761static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3762{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003763 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003764 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3765
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003766 return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003767}
3768
3769static void pfkey_seq_stop(struct seq_file *f, void *v)
stephen hemmingerada440e2011-02-23 09:06:50 +00003770 __releases(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003771{
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003772 rcu_read_unlock();
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003773}
3774
Stephen Hemminger98147d52009-09-01 19:25:02 +00003775static const struct seq_operations pfkey_seq_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003776 .start = pfkey_seq_start,
3777 .next = pfkey_seq_next,
3778 .stop = pfkey_seq_stop,
3779 .show = pfkey_seq_show,
3780};
3781
3782static int pfkey_seq_open(struct inode *inode, struct file *file)
3783{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003784 return seq_open_net(inode, file, &pfkey_seq_ops,
3785 sizeof(struct seq_net_private));
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003786}
3787
Stephen Hemminger5ca1b992009-09-01 19:25:05 +00003788static const struct file_operations pfkey_proc_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003789 .open = pfkey_seq_open,
3790 .read = seq_read,
3791 .llseek = seq_lseek,
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003792 .release = seq_release_net,
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003793};
3794
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003795static int __net_init pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003796{
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003797 struct proc_dir_entry *e;
3798
Gao fengd4beaa62013-02-18 01:34:54 +00003799 e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003800 if (e == NULL)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003801 return -ENOMEM;
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003802
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003803 return 0;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003804}
3805
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003806static void __net_exit pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003807{
Gao fengece31ff2013-02-18 01:34:56 +00003808 remove_proc_entry("pfkey", net->proc_net);
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003809}
3810#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003811static inline int pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003812{
3813 return 0;
3814}
3815
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003816static inline void pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003817{
3818}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819#endif
3820
3821static struct xfrm_mgr pfkeyv2_mgr =
3822{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 .notify = pfkey_send_notify,
3824 .acquire = pfkey_send_acquire,
3825 .compile_policy = pfkey_compile_policy,
3826 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003827 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003828 .migrate = pfkey_send_migrate,
Horia Geanta0f245582014-02-12 16:20:06 +02003829 .is_alive = pfkey_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830};
3831
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003832static int __net_init pfkey_net_init(struct net *net)
3833{
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003834 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003835 int rv;
3836
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003837 INIT_HLIST_HEAD(&net_pfkey->table);
3838 atomic_set(&net_pfkey->socks_nr, 0);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003839
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003840 rv = pfkey_init_proc(net);
3841
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003842 return rv;
3843}
3844
3845static void __net_exit pfkey_net_exit(struct net *net)
3846{
3847 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3848
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003849 pfkey_exit_proc(net);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003850 BUG_ON(!hlist_empty(&net_pfkey->table));
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003851}
3852
3853static struct pernet_operations pfkey_net_ops = {
3854 .init = pfkey_net_init,
3855 .exit = pfkey_net_exit,
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003856 .id = &pfkey_net_id,
3857 .size = sizeof(struct netns_pfkey),
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003858};
3859
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860static void __exit ipsec_pfkey_exit(void)
3861{
3862 xfrm_unregister_km(&pfkeyv2_mgr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003864 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 proto_unregister(&key_proto);
3866}
3867
3868static int __init ipsec_pfkey_init(void)
3869{
3870 int err = proto_register(&key_proto, 0);
3871
3872 if (err != 0)
3873 goto out;
3874
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003875 err = register_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 if (err != 0)
3877 goto out_unregister_key_proto;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003878 err = sock_register(&pfkey_family_ops);
3879 if (err != 0)
3880 goto out_unregister_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 err = xfrm_register_km(&pfkeyv2_mgr);
3882 if (err != 0)
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003883 goto out_sock_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884out:
3885 return err;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003886
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887out_sock_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003889out_unregister_pernet:
3890 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891out_unregister_key_proto:
3892 proto_unregister(&key_proto);
3893 goto out;
3894}
3895
3896module_init(ipsec_pfkey_init);
3897module_exit(ipsec_pfkey_exit);
3898MODULE_LICENSE("GPL");
3899MODULE_ALIAS_NETPROTO(PF_KEY);