blob: f9c9ecb0cdd3b3eea618538fda2e884583f9bc09 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Maxim Giryaev <gem@asplinux.ru>
10 * David S. Miller <davem@redhat.com>
11 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
12 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
14 * Derek Atkins <derek@ihtfp.com>
15 */
16
Randy Dunlap4fc268d2006-01-11 12:17:47 -080017#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/socket.h>
21#include <linux/pfkeyv2.h>
22#include <linux/ipsec.h>
23#include <linux/skbuff.h>
24#include <linux/rtnetlink.h>
25#include <linux/in.h>
26#include <linux/in6.h>
27#include <linux/proc_fs.h>
28#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020030#include <net/net_namespace.h>
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080031#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/xfrm.h>
33
34#include <net/sock.h>
35
36#define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
37#define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
38
Eric Dumazetf99189b2009-11-17 10:42:49 +000039static int pfkey_net_id __read_mostly;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080040struct netns_pfkey {
41 /* List of all pfkey sockets. */
42 struct hlist_head table;
43 atomic_t socks_nr;
44};
stephen hemminger7f6b9db2010-02-22 07:57:19 +000045static DEFINE_MUTEX(pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jamal Hadi Salimbd557752010-02-22 16:20:22 -080047#define DUMMY_MARK 0
Mathias Krausee473fcb2013-06-26 23:56:58 +020048static const struct xfrm_mark dummy_mark = {0, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049struct pfkey_sock {
50 /* struct sock must be the first member of struct pfkey_sock */
51 struct sock sk;
52 int registered;
53 int promisc;
Timo Teras83321d62008-03-03 23:40:12 -080054
55 struct {
56 uint8_t msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +000057 uint32_t msg_portid;
Timo Teras83321d62008-03-03 23:40:12 -080058 int (*dump)(struct pfkey_sock *sk);
59 void (*done)(struct pfkey_sock *sk);
60 union {
61 struct xfrm_policy_walk policy;
62 struct xfrm_state_walk state;
63 } u;
Herbert Xu12a169e2008-10-01 07:03:24 -070064 struct sk_buff *skb;
Timo Teras83321d62008-03-03 23:40:12 -080065 } dump;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
68static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
69{
70 return (struct pfkey_sock *)sk;
71}
72
David S. Miller4c93fbb2011-02-25 18:07:06 -080073static int pfkey_can_dump(const struct sock *sk)
Timo Teras83321d62008-03-03 23:40:12 -080074{
75 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
76 return 1;
77 return 0;
78}
79
Timo Teras05238202008-10-01 05:17:54 -070080static void pfkey_terminate_dump(struct pfkey_sock *pfk)
Timo Teras83321d62008-03-03 23:40:12 -080081{
Timo Teras05238202008-10-01 05:17:54 -070082 if (pfk->dump.dump) {
Herbert Xu12a169e2008-10-01 07:03:24 -070083 if (pfk->dump.skb) {
84 kfree_skb(pfk->dump.skb);
85 pfk->dump.skb = NULL;
86 }
Timo Teras05238202008-10-01 05:17:54 -070087 pfk->dump.done(pfk);
88 pfk->dump.dump = NULL;
89 pfk->dump.done = NULL;
90 }
Timo Teras83321d62008-03-03 23:40:12 -080091}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static void pfkey_sock_destruct(struct sock *sk)
94{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -080095 struct net *net = sock_net(sk);
96 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
97
Timo Teras05238202008-10-01 05:17:54 -070098 pfkey_terminate_dump(pfkey_sk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 skb_queue_purge(&sk->sk_receive_queue);
100
101 if (!sock_flag(sk, SOCK_DEAD)) {
stephen hemminger207024b2010-05-12 06:37:07 +0000102 pr_err("Attempt to release alive pfkey socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return;
104 }
105
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700106 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
107 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800109 atomic_dec(&net_pfkey->socks_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800112static const struct proto_ops pfkey_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114static void pfkey_insert(struct sock *sk)
115{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800116 struct net *net = sock_net(sk);
117 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
118
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000119 mutex_lock(&pfkey_mutex);
120 sk_add_node_rcu(sk, &net_pfkey->table);
121 mutex_unlock(&pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static void pfkey_remove(struct sock *sk)
125{
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000126 mutex_lock(&pfkey_mutex);
127 sk_del_node_init_rcu(sk);
128 mutex_unlock(&pfkey_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131static struct proto key_proto = {
132 .name = "KEY",
133 .owner = THIS_MODULE,
134 .obj_size = sizeof(struct pfkey_sock),
135};
136
Eric Paris3f378b62009-11-05 22:18:14 -0800137static int pfkey_create(struct net *net, struct socket *sock, int protocol,
138 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800140 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct sock *sk;
142 int err;
143
Eric W. Biedermandf008c92012-11-16 03:03:07 +0000144 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -EPERM;
146 if (sock->type != SOCK_RAW)
147 return -ESOCKTNOSUPPORT;
148 if (protocol != PF_KEY_V2)
149 return -EPROTONOSUPPORT;
150
151 err = -ENOMEM;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500152 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (sk == NULL)
154 goto out;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 sock->ops = &pfkey_ops;
157 sock_init_data(sock, sk);
158
159 sk->sk_family = PF_KEY;
160 sk->sk_destruct = pfkey_sock_destruct;
161
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800162 atomic_inc(&net_pfkey->socks_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 pfkey_insert(sk);
165
166 return 0;
167out:
168 return err;
169}
170
171static int pfkey_release(struct socket *sock)
172{
173 struct sock *sk = sock->sk;
174
175 if (!sk)
176 return 0;
177
178 pfkey_remove(sk);
179
180 sock_orphan(sk);
181 sock->sk = NULL;
182 skb_queue_purge(&sk->sk_write_queue);
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000183
184 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 sock_put(sk);
186
187 return 0;
188}
189
190static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
Al Virodd0fc662005-10-07 07:46:04 +0100191 gfp_t allocation, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int err = -ENOBUFS;
194
195 sock_hold(sk);
196 if (*skb2 == NULL) {
197 if (atomic_read(&skb->users) != 1) {
198 *skb2 = skb_clone(skb, allocation);
199 } else {
200 *skb2 = skb;
201 atomic_inc(&skb->users);
202 }
203 }
204 if (*skb2 != NULL) {
205 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 skb_set_owner_r(*skb2, sk);
207 skb_queue_tail(&sk->sk_receive_queue, *skb2);
David S. Miller676d2362014-04-11 16:15:36 -0400208 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *skb2 = NULL;
210 err = 0;
211 }
212 }
213 sock_put(sk);
214 return err;
215}
216
217/* Send SKB to all pfkey sockets matching selected criteria. */
218#define BROADCAST_ALL 0
219#define BROADCAST_ONE 1
220#define BROADCAST_REGISTERED 2
221#define BROADCAST_PROMISC_ONLY 4
David Ahernba51b6b2015-08-24 15:17:17 -0600222static int pfkey_broadcast(struct sk_buff *skb,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800223 int broadcast_flags, struct sock *one_sk,
224 struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -0800226 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct sk_buff *skb2 = NULL;
229 int err = -ESRCH;
230
231 /* XXX Do we need something like netlink_overrun? I think
232 * XXX PF_KEY socket apps will not mind current behavior.
233 */
234 if (!skb)
235 return -ENOMEM;
236
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000237 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800238 sk_for_each_rcu(sk, &net_pfkey->table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 struct pfkey_sock *pfk = pfkey_sk(sk);
240 int err2;
241
242 /* Yes, it means that if you are meant to receive this
243 * pfkey message you receive it twice as promiscuous
244 * socket.
245 */
246 if (pfk->promisc)
David Ahernba51b6b2015-08-24 15:17:17 -0600247 pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* the exact target will be processed later */
250 if (sk == one_sk)
251 continue;
252 if (broadcast_flags != BROADCAST_ALL) {
253 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
254 continue;
255 if ((broadcast_flags & BROADCAST_REGISTERED) &&
256 !pfk->registered)
257 continue;
258 if (broadcast_flags & BROADCAST_ONE)
259 continue;
260 }
261
David Ahernba51b6b2015-08-24 15:17:17 -0600262 err2 = pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Li RongQingf6b8dec92015-10-22 11:35:05 +0800264 /* Error is cleared after successful sending to at least one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 * registered KM */
266 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
267 err = err2;
268 }
stephen hemminger7f6b9db2010-02-22 07:57:19 +0000269 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (one_sk != NULL)
David Ahernba51b6b2015-08-24 15:17:17 -0600272 err = pfkey_broadcast_one(skb, &skb2, GFP_KERNEL, one_sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Wei Yongjun6f961062009-02-25 00:31:04 +0000274 kfree_skb(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 kfree_skb(skb);
276 return err;
277}
278
Timo Teras05238202008-10-01 05:17:54 -0700279static int pfkey_do_dump(struct pfkey_sock *pfk)
280{
Herbert Xu12a169e2008-10-01 07:03:24 -0700281 struct sadb_msg *hdr;
Timo Teras05238202008-10-01 05:17:54 -0700282 int rc;
283
284 rc = pfk->dump.dump(pfk);
285 if (rc == -ENOBUFS)
286 return 0;
287
Herbert Xu12a169e2008-10-01 07:03:24 -0700288 if (pfk->dump.skb) {
289 if (!pfkey_can_dump(&pfk->sk))
290 return 0;
291
292 hdr = (struct sadb_msg *) pfk->dump.skb->data;
293 hdr->sadb_msg_seq = 0;
294 hdr->sadb_msg_errno = rc;
David Ahernba51b6b2015-08-24 15:17:17 -0600295 pfkey_broadcast(pfk->dump.skb, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800296 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -0700297 pfk->dump.skb = NULL;
298 }
299
Timo Teras05238202008-10-01 05:17:54 -0700300 pfkey_terminate_dump(pfk);
301 return rc;
302}
303
David S. Miller4c93fbb2011-02-25 18:07:06 -0800304static inline void pfkey_hdr_dup(struct sadb_msg *new,
305 const struct sadb_msg *orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 *new = *orig;
308}
309
David S. Miller4c93fbb2011-02-25 18:07:06 -0800310static int pfkey_error(const struct sadb_msg *orig, int err, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
313 struct sadb_msg *hdr;
314
315 if (!skb)
316 return -ENOBUFS;
317
318 /* Woe be to the platform trying to support PFKEY yet
319 * having normal errnos outside the 1-255 range, inclusive.
320 */
321 err = -err;
322 if (err == ERESTARTSYS ||
323 err == ERESTARTNOHAND ||
324 err == ERESTARTNOINTR)
325 err = EINTR;
326 if (err >= 512)
327 err = EINVAL;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800328 BUG_ON(err <= 0 || err >= 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
331 pfkey_hdr_dup(hdr, orig);
332 hdr->sadb_msg_errno = (uint8_t) err;
333 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
334 sizeof(uint64_t));
335
David Ahernba51b6b2015-08-24 15:17:17 -0600336 pfkey_broadcast(skb, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 return 0;
339}
340
Mathias Krause8603b9552013-06-26 23:56:59 +0200341static const u8 sadb_ext_min_len[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 [SADB_EXT_RESERVED] = (u8) 0,
343 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
344 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
345 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
346 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
347 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
348 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
349 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
350 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
351 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
352 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
353 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
354 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
355 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
356 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
357 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
358 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
359 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
360 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
361 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
362 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
363 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
364 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
365 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
Trent Jaegerdf718372005-12-13 23:12:27 -0800366 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
Arnaud Ebalard13c1d182008-10-05 13:33:42 -0700367 [SADB_X_EXT_KMADDRESS] = (u8) sizeof(struct sadb_x_kmaddress),
Nicolas Dichteld2c5f652014-02-20 14:52:41 +0100368 [SADB_X_EXT_FILTER] = (u8) sizeof(struct sadb_x_filter),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369};
370
371/* Verify sadb_address_{len,prefixlen} against sa_family. */
David S. Miller4c93fbb2011-02-25 18:07:06 -0800372static int verify_address_len(const void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800374 const struct sadb_address *sp = p;
375 const struct sockaddr *addr = (const struct sockaddr *)(sp + 1);
376 const struct sockaddr_in *sin;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000377#if IS_ENABLED(CONFIG_IPV6)
David S. Miller4c93fbb2011-02-25 18:07:06 -0800378 const struct sockaddr_in6 *sin6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379#endif
380 int len;
381
382 switch (addr->sa_family) {
383 case AF_INET:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700384 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (sp->sadb_address_len != len ||
386 sp->sadb_address_prefixlen > 32)
387 return -EINVAL;
388 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000389#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 case AF_INET6:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700391 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (sp->sadb_address_len != len ||
393 sp->sadb_address_prefixlen > 128)
394 return -EINVAL;
395 break;
396#endif
397 default:
398 /* It is user using kernel to keep track of security
399 * associations for another protocol, such as
400 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
401 * lengths.
402 *
403 * XXX Actually, association/policy database is not yet
404 * XXX able to cope with arbitrary sockaddr families.
405 * XXX When it can, remove this -EINVAL. -DaveM
406 */
407 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 return 0;
411}
412
David S. Miller4c93fbb2011-02-25 18:07:06 -0800413static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800414{
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700415 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
416 sec_ctx->sadb_x_ctx_len,
417 sizeof(uint64_t));
Trent Jaegerdf718372005-12-13 23:12:27 -0800418}
419
David S. Miller4c93fbb2011-02-25 18:07:06 -0800420static inline int verify_sec_ctx_len(const void *p)
Trent Jaegerdf718372005-12-13 23:12:27 -0800421{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800422 const struct sadb_x_sec_ctx *sec_ctx = p;
Stephen Rothwell298bb622007-10-30 23:57:05 -0700423 int len = sec_ctx->sadb_x_ctx_len;
Trent Jaegerdf718372005-12-13 23:12:27 -0800424
Stephen Rothwell298bb622007-10-30 23:57:05 -0700425 if (len > PAGE_SIZE)
Trent Jaegerdf718372005-12-13 23:12:27 -0800426 return -EINVAL;
427
428 len = pfkey_sec_ctx_len(sec_ctx);
429
430 if (sec_ctx->sadb_x_sec_len != len)
431 return -EINVAL;
432
433 return 0;
434}
435
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +0100436static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx,
437 gfp_t gfp)
Trent Jaegerdf718372005-12-13 23:12:27 -0800438{
439 struct xfrm_user_sec_ctx *uctx = NULL;
440 int ctx_size = sec_ctx->sadb_x_ctx_len;
441
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +0100442 uctx = kmalloc((sizeof(*uctx)+ctx_size), gfp);
Trent Jaegerdf718372005-12-13 23:12:27 -0800443
444 if (!uctx)
445 return NULL;
446
447 uctx->len = pfkey_sec_ctx_len(sec_ctx);
448 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
449 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
450 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
451 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
452 memcpy(uctx + 1, sec_ctx + 1,
453 uctx->ctx_len);
454
455 return uctx;
456}
457
David S. Miller4c93fbb2011-02-25 18:07:06 -0800458static int present_and_same_family(const struct sadb_address *src,
459 const struct sadb_address *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800461 const struct sockaddr *s_addr, *d_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (!src || !dst)
464 return 0;
465
David S. Miller4c93fbb2011-02-25 18:07:06 -0800466 s_addr = (const struct sockaddr *)(src + 1);
467 d_addr = (const struct sockaddr *)(dst + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (s_addr->sa_family != d_addr->sa_family)
469 return 0;
470 if (s_addr->sa_family != AF_INET
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000471#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 && s_addr->sa_family != AF_INET6
473#endif
474 )
475 return 0;
476
477 return 1;
478}
479
David S. Miller4c93fbb2011-02-25 18:07:06 -0800480static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800482 const char *p = (char *) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int len = skb->len;
484
485 len -= sizeof(*hdr);
486 p += sizeof(*hdr);
487 while (len > 0) {
David S. Miller4c93fbb2011-02-25 18:07:06 -0800488 const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 uint16_t ext_type;
490 int ext_len;
491
492 ext_len = ehdr->sadb_ext_len;
493 ext_len *= sizeof(uint64_t);
494 ext_type = ehdr->sadb_ext_type;
495 if (ext_len < sizeof(uint64_t) ||
496 ext_len > len ||
497 ext_type == SADB_EXT_RESERVED)
498 return -EINVAL;
499
500 if (ext_type <= SADB_EXT_MAX) {
501 int min = (int) sadb_ext_min_len[ext_type];
502 if (ext_len < min)
503 return -EINVAL;
504 if (ext_hdrs[ext_type-1] != NULL)
505 return -EINVAL;
506 if (ext_type == SADB_EXT_ADDRESS_SRC ||
507 ext_type == SADB_EXT_ADDRESS_DST ||
508 ext_type == SADB_EXT_ADDRESS_PROXY ||
509 ext_type == SADB_X_EXT_NAT_T_OA) {
510 if (verify_address_len(p))
511 return -EINVAL;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900512 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800513 if (ext_type == SADB_X_EXT_SEC_CTX) {
514 if (verify_sec_ctx_len(p))
515 return -EINVAL;
516 }
David S. Miller4c93fbb2011-02-25 18:07:06 -0800517 ext_hdrs[ext_type-1] = (void *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 p += ext_len;
520 len -= ext_len;
521 }
522
523 return 0;
524}
525
526static uint16_t
527pfkey_satype2proto(uint8_t satype)
528{
529 switch (satype) {
530 case SADB_SATYPE_UNSPEC:
531 return IPSEC_PROTO_ANY;
532 case SADB_SATYPE_AH:
533 return IPPROTO_AH;
534 case SADB_SATYPE_ESP:
535 return IPPROTO_ESP;
536 case SADB_X_SATYPE_IPCOMP:
537 return IPPROTO_COMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 default:
539 return 0;
540 }
541 /* NOTREACHED */
542}
543
544static uint8_t
545pfkey_proto2satype(uint16_t proto)
546{
547 switch (proto) {
548 case IPPROTO_AH:
549 return SADB_SATYPE_AH;
550 case IPPROTO_ESP:
551 return SADB_SATYPE_ESP;
552 case IPPROTO_COMP:
553 return SADB_X_SATYPE_IPCOMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 default:
555 return 0;
556 }
557 /* NOTREACHED */
558}
559
560/* BTW, this scheme means that there is no way with PFKEY2 sockets to
561 * say specifically 'just raw sockets' as we encode them as 255.
562 */
563
564static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
565{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000566 return proto == IPSEC_PROTO_ANY ? 0 : proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
570{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000571 return proto ? proto : IPSEC_PROTO_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900574static inline int pfkey_sockaddr_len(sa_family_t family)
575{
576 switch (family) {
577 case AF_INET:
578 return sizeof(struct sockaddr_in);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000579#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900580 case AF_INET6:
581 return sizeof(struct sockaddr_in6);
582#endif
583 }
584 return 0;
585}
586
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900587static
588int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900590 switch (sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 case AF_INET:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900592 xaddr->a4 =
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900593 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return AF_INET;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000595#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 case AF_INET6:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900597 memcpy(xaddr->a6,
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900598 &((struct sockaddr_in6 *)sa)->sin6_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 sizeof(struct in6_addr));
600 return AF_INET6;
601#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900603 return 0;
604}
605
606static
David S. Miller4c93fbb2011-02-25 18:07:06 -0800607int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900608{
609 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
610 xaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
David S. Miller4c93fbb2011-02-25 18:07:06 -0800613static 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 -0700614{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800615 const struct sadb_sa *sa;
616 const struct sadb_address *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 uint16_t proto;
618 unsigned short family;
619 xfrm_address_t *xaddr;
620
Joe Perchesea110732011-06-13 16:21:26 +0000621 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (sa == NULL)
623 return NULL;
624
625 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
626 if (proto == 0)
627 return NULL;
628
629 /* sadb_address_len should be checked by caller */
Joe Perchesea110732011-06-13 16:21:26 +0000630 addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (addr == NULL)
632 return NULL;
633
David S. Miller4c93fbb2011-02-25 18:07:06 -0800634 family = ((const struct sockaddr *)(addr + 1))->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 switch (family) {
636 case AF_INET:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800637 xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000639#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 case AF_INET6:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800641 xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 break;
643#endif
644 default:
645 xaddr = NULL;
646 }
647
648 if (!xaddr)
649 return NULL;
650
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800651 return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656static int
657pfkey_sockaddr_size(sa_family_t family)
658{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900659 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700662static inline int pfkey_mode_from_xfrm(int mode)
663{
664 switch(mode) {
665 case XFRM_MODE_TRANSPORT:
666 return IPSEC_MODE_TRANSPORT;
667 case XFRM_MODE_TUNNEL:
668 return IPSEC_MODE_TUNNEL;
669 case XFRM_MODE_BEET:
670 return IPSEC_MODE_BEET;
671 default:
672 return -1;
673 }
674}
675
676static inline int pfkey_mode_to_xfrm(int mode)
677{
678 switch(mode) {
679 case IPSEC_MODE_ANY: /*XXX*/
680 case IPSEC_MODE_TRANSPORT:
681 return XFRM_MODE_TRANSPORT;
682 case IPSEC_MODE_TUNNEL:
683 return XFRM_MODE_TUNNEL;
684 case IPSEC_MODE_BEET:
685 return XFRM_MODE_BEET;
686 default:
687 return -1;
688 }
689}
690
David S. Miller183cad12011-02-24 00:28:01 -0500691static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
David S. Miller4c93fbb2011-02-25 18:07:06 -0800692 struct sockaddr *sa,
693 unsigned short family)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900694{
695 switch (family) {
696 case AF_INET:
697 {
698 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
699 sin->sin_family = AF_INET;
700 sin->sin_port = port;
701 sin->sin_addr.s_addr = xaddr->a4;
702 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
703 return 32;
704 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000705#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900706 case AF_INET6:
707 {
708 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
709 sin6->sin6_family = AF_INET6;
710 sin6->sin6_port = port;
711 sin6->sin6_flowinfo = 0;
Jiri Benc15e318b2015-03-29 16:59:24 +0200712 sin6->sin6_addr = xaddr->in6;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900713 sin6->sin6_scope_id = 0;
714 return 128;
715 }
716#endif
717 }
718 return 0;
719}
720
David S. Miller4c93fbb2011-02-25 18:07:06 -0800721static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -0700722 int add_keys, int hsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 struct sk_buff *skb;
725 struct sadb_msg *hdr;
726 struct sadb_sa *sa;
727 struct sadb_lifetime *lifetime;
728 struct sadb_address *addr;
729 struct sadb_key *key;
730 struct sadb_x_sa2 *sa2;
Trent Jaegerdf718372005-12-13 23:12:27 -0800731 struct sadb_x_sec_ctx *sec_ctx;
732 struct xfrm_sec_ctx *xfrm_ctx;
733 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int size;
735 int auth_key_size = 0;
736 int encrypt_key_size = 0;
737 int sockaddr_size;
738 struct xfrm_encap_tmpl *natt = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700739 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /* address family check */
742 sockaddr_size = pfkey_sockaddr_size(x->props.family);
743 if (!sockaddr_size)
744 return ERR_PTR(-EINVAL);
745
746 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
747 key(AE), (identity(SD),) (sensitivity)> */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900748 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 sizeof(struct sadb_lifetime) +
750 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
751 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900752 sizeof(struct sadb_address)*2 +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 sockaddr_size*2 +
754 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800755
756 if ((xfrm_ctx = x->security)) {
757 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
758 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
759 }
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* identity & sensitivity */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000762 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 size += sizeof(struct sadb_address) + sockaddr_size;
764
765 if (add_keys) {
766 if (x->aalg && x->aalg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900767 auth_key_size =
768 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 size += sizeof(struct sadb_key) + auth_key_size;
770 }
771 if (x->ealg && x->ealg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900772 encrypt_key_size =
773 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 size += sizeof(struct sadb_key) + encrypt_key_size;
775 }
776 }
777 if (x->encap)
778 natt = x->encap;
779
780 if (natt && natt->encap_type) {
781 size += sizeof(struct sadb_x_nat_t_type);
782 size += sizeof(struct sadb_x_nat_t_port);
783 size += sizeof(struct sadb_x_nat_t_port);
784 }
785
786 skb = alloc_skb(size + 16, GFP_ATOMIC);
787 if (skb == NULL)
788 return ERR_PTR(-ENOBUFS);
789
790 /* call should fill header later */
791 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
792 memset(hdr, 0, size); /* XXX do we need this ? */
793 hdr->sadb_msg_len = size / sizeof(uint64_t);
794
795 /* sa */
796 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
797 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
798 sa->sadb_sa_exttype = SADB_EXT_SA;
799 sa->sadb_sa_spi = x->id.spi;
800 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700801 switch (x->km.state) {
802 case XFRM_STATE_VALID:
803 sa->sadb_sa_state = x->km.dying ?
804 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
805 break;
806 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700808 break;
809 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700811 break;
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 sa->sadb_sa_auth = 0;
814 if (x->aalg) {
815 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200816 sa->sadb_sa_auth = (a && a->pfkey_supported) ?
817 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819 sa->sadb_sa_encrypt = 0;
820 BUG_ON(x->ealg && x->calg);
821 if (x->ealg) {
822 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200823 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
824 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
827 if (x->calg) {
828 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200829 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
830 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
833 sa->sadb_sa_flags = 0;
834 if (x->props.flags & XFRM_STATE_NOECN)
835 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
836 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
837 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700838 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
839 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* hard time */
842 if (hsc & 2) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900843 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 sizeof(struct sadb_lifetime));
845 lifetime->sadb_lifetime_len =
846 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
847 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
848 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
849 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
850 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
851 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
852 }
853 /* soft time */
854 if (hsc & 1) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900855 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 sizeof(struct sadb_lifetime));
857 lifetime->sadb_lifetime_len =
858 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
859 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
860 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
861 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
862 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
863 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
864 }
865 /* current time */
866 lifetime = (struct sadb_lifetime *) skb_put(skb,
867 sizeof(struct sadb_lifetime));
868 lifetime->sadb_lifetime_len =
869 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
870 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
871 lifetime->sadb_lifetime_allocations = x->curlft.packets;
872 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
873 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
874 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
875 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900876 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900878 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 (sizeof(struct sadb_address)+sockaddr_size)/
880 sizeof(uint64_t);
881 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900882 /* "if the ports are non-zero, then the sadb_address_proto field,
883 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900885 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900888 addr->sadb_address_prefixlen =
889 pfkey_sockaddr_fill(&x->props.saddr, 0,
890 (struct sockaddr *) (addr + 1),
891 x->props.family);
892 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 BUG();
894
895 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900896 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900898 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 (sizeof(struct sadb_address)+sockaddr_size)/
900 sizeof(uint64_t);
901 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900902 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900905 addr->sadb_address_prefixlen =
906 pfkey_sockaddr_fill(&x->id.daddr, 0,
907 (struct sockaddr *) (addr + 1),
908 x->props.family);
909 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 BUG();
911
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000912 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
913 x->props.family)) {
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900914 addr = (struct sadb_address*) skb_put(skb,
915 sizeof(struct sadb_address)+sockaddr_size);
916 addr->sadb_address_len =
917 (sizeof(struct sadb_address)+sockaddr_size)/
918 sizeof(uint64_t);
919 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
920 addr->sadb_address_proto =
921 pfkey_proto_from_xfrm(x->sel.proto);
922 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
923 addr->sadb_address_reserved = 0;
924
925 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
926 (struct sockaddr *) (addr + 1),
927 x->props.family);
928 }
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /* auth key */
931 if (add_keys && auth_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900932 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 sizeof(struct sadb_key)+auth_key_size);
934 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
935 sizeof(uint64_t);
936 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
937 key->sadb_key_bits = x->aalg->alg_key_len;
938 key->sadb_key_reserved = 0;
939 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
940 }
941 /* encrypt key */
942 if (add_keys && encrypt_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900943 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 sizeof(struct sadb_key)+encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900945 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 encrypt_key_size) / sizeof(uint64_t);
947 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
948 key->sadb_key_bits = x->ealg->alg_key_len;
949 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900950 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 (x->ealg->alg_key_len+7)/8);
952 }
953
954 /* sa */
955 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
956 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
957 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700958 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
959 kfree_skb(skb);
960 return ERR_PTR(-EINVAL);
961 }
962 sa2->sadb_x_sa2_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 sa2->sadb_x_sa2_reserved1 = 0;
964 sa2->sadb_x_sa2_reserved2 = 0;
965 sa2->sadb_x_sa2_sequence = 0;
966 sa2->sadb_x_sa2_reqid = x->props.reqid;
967
968 if (natt && natt->encap_type) {
969 struct sadb_x_nat_t_type *n_type;
970 struct sadb_x_nat_t_port *n_port;
971
972 /* type */
973 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
974 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
975 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
976 n_type->sadb_x_nat_t_type_type = natt->encap_type;
977 n_type->sadb_x_nat_t_type_reserved[0] = 0;
978 n_type->sadb_x_nat_t_type_reserved[1] = 0;
979 n_type->sadb_x_nat_t_type_reserved[2] = 0;
980
981 /* source port */
982 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
983 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
984 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
985 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
986 n_port->sadb_x_nat_t_port_reserved = 0;
987
988 /* dest port */
989 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
990 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
991 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
992 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
993 n_port->sadb_x_nat_t_port_reserved = 0;
994 }
995
Trent Jaegerdf718372005-12-13 23:12:27 -0800996 /* security context */
997 if (xfrm_ctx) {
998 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
999 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1000 sec_ctx->sadb_x_sec_len =
1001 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1002 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1003 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1004 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1005 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1006 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1007 xfrm_ctx->ctx_len);
1008 }
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return skb;
1011}
1012
Herbert Xu050f0092007-10-09 13:31:47 -07001013
David S. Miller4c93fbb2011-02-25 18:07:06 -08001014static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
Herbert Xu050f0092007-10-09 13:31:47 -07001015{
1016 struct sk_buff *skb;
1017
Herbert Xu050f0092007-10-09 13:31:47 -07001018 skb = __pfkey_xfrm_state2msg(x, 1, 3);
Herbert Xu050f0092007-10-09 13:31:47 -07001019
1020 return skb;
1021}
1022
David S. Miller4c93fbb2011-02-25 18:07:06 -08001023static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -07001024 int hsc)
1025{
1026 return __pfkey_xfrm_state2msg(x, 0, hsc);
1027}
1028
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001029static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
David S. Miller4c93fbb2011-02-25 18:07:06 -08001030 const struct sadb_msg *hdr,
1031 void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001033 struct xfrm_state *x;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001034 const struct sadb_lifetime *lifetime;
1035 const struct sadb_sa *sa;
1036 const struct sadb_key *key;
1037 const struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 uint16_t proto;
1039 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Joe Perchesea110732011-06-13 16:21:26 +00001042 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (!sa ||
1044 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1045 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1046 return ERR_PTR(-EINVAL);
1047 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1048 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1049 return ERR_PTR(-EINVAL);
1050 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1051 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1052 return ERR_PTR(-EINVAL);
1053 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1054 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1055 return ERR_PTR(-EINVAL);
1056
1057 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1058 if (proto == 0)
1059 return ERR_PTR(-EINVAL);
1060
1061 /* default error is no buffer space */
1062 err = -ENOBUFS;
1063
1064 /* RFC2367:
1065
1066 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1067 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1068 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1069 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1070 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1071 not true.
1072
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001073 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1075 */
1076 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1077 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1078 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1079 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1080 return ERR_PTR(-EINVAL);
Joe Perchesea110732011-06-13 16:21:26 +00001081 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (key != NULL &&
1083 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1084 ((key->sadb_key_bits+7) / 8 == 0 ||
1085 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1086 return ERR_PTR(-EINVAL);
1087 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1088 if (key != NULL &&
1089 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1090 ((key->sadb_key_bits+7) / 8 == 0 ||
1091 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1092 return ERR_PTR(-EINVAL);
1093
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001094 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (x == NULL)
1096 return ERR_PTR(-ENOBUFS);
1097
1098 x->id.proto = proto;
1099 x->id.spi = sa->sadb_sa_spi;
Fan Du33fce602013-09-17 15:14:13 +08001100 x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay,
1101 (sizeof(x->replay.bitmap) * 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1103 x->props.flags |= XFRM_STATE_NOECN;
1104 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1105 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001106 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1107 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Joe Perchesea110732011-06-13 16:21:26 +00001109 lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (lifetime != NULL) {
1111 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1112 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1113 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1114 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1115 }
Joe Perchesea110732011-06-13 16:21:26 +00001116 lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (lifetime != NULL) {
1118 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1119 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1120 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1121 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1122 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001123
Joe Perchesea110732011-06-13 16:21:26 +00001124 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001125 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01001126 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001127
1128 if (!uctx)
1129 goto out;
1130
1131 err = security_xfrm_state_alloc(x, uctx);
1132 kfree(uctx);
1133
1134 if (err)
1135 goto out;
1136 }
1137
Joe Perchesea110732011-06-13 16:21:26 +00001138 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (sa->sadb_sa_auth) {
1140 int keysize = 0;
1141 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001142 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 err = -ENOSYS;
1144 goto out;
1145 }
1146 if (key)
1147 keysize = (key->sadb_key_bits + 7) / 8;
1148 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1149 if (!x->aalg)
1150 goto out;
1151 strcpy(x->aalg->alg_name, a->name);
1152 x->aalg->alg_key_len = 0;
1153 if (key) {
1154 x->aalg->alg_key_len = key->sadb_key_bits;
1155 memcpy(x->aalg->alg_key, key+1, keysize);
1156 }
Martin Willic20a66f2009-12-09 06:11:15 +00001157 x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 x->props.aalgo = sa->sadb_sa_auth;
1159 /* x->algo.flags = sa->sadb_sa_flags; */
1160 }
1161 if (sa->sadb_sa_encrypt) {
1162 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1163 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001164 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 err = -ENOSYS;
1166 goto out;
1167 }
1168 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1169 if (!x->calg)
1170 goto out;
1171 strcpy(x->calg->alg_name, a->name);
1172 x->props.calgo = sa->sadb_sa_encrypt;
1173 } else {
1174 int keysize = 0;
1175 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001176 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 err = -ENOSYS;
1178 goto out;
1179 }
1180 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1181 if (key)
1182 keysize = (key->sadb_key_bits + 7) / 8;
1183 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1184 if (!x->ealg)
1185 goto out;
1186 strcpy(x->ealg->alg_name, a->name);
1187 x->ealg->alg_key_len = 0;
1188 if (key) {
1189 x->ealg->alg_key_len = key->sadb_key_bits;
1190 memcpy(x->ealg->alg_key, key+1, keysize);
1191 }
1192 x->props.ealgo = sa->sadb_sa_encrypt;
Herbert Xu69b01372015-05-27 16:03:45 +08001193 x->geniv = a->uinfo.encr.geniv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195 }
1196 /* x->algo.flags = sa->sadb_sa_flags; */
1197
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001198 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 &x->props.saddr);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001200 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 &x->id.daddr);
1202
1203 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001204 const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001205 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1206 if (mode < 0) {
1207 err = -EINVAL;
1208 goto out;
1209 }
1210 x->props.mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 x->props.reqid = sa2->sadb_x_sa2_reqid;
1212 }
1213
1214 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001215 const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 /* Nobody uses this, but we try. */
1218 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1219 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1220 }
1221
Kazunori MIYAZAWA4da51052008-05-21 13:26:11 -07001222 if (!x->sel.family)
Joy Latten4a4b6272007-08-02 19:25:43 -07001223 x->sel.family = x->props.family;
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001226 const struct sadb_x_nat_t_type* n_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 struct xfrm_encap_tmpl *natt;
1228
1229 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1230 if (!x->encap)
1231 goto out;
1232
1233 natt = x->encap;
1234 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1235 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1236
1237 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001238 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1240 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1241 }
1242 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001243 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1245 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1246 }
Timo Terasa8d694c2009-01-25 20:49:14 -08001247 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249
Herbert Xu72cb6962005-06-20 13:18:08 -07001250 err = xfrm_init_state(x);
1251 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return x;
1256
1257out:
1258 x->km.state = XFRM_STATE_DEAD;
1259 xfrm_state_put(x);
1260 return ERR_PTR(err);
1261}
1262
David S. Miller4c93fbb2011-02-25 18:07:06 -08001263static 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 -07001264{
1265 return -EOPNOTSUPP;
1266}
1267
David S. Miller4c93fbb2011-02-25 18:07:06 -08001268static 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 -07001269{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001270 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct sk_buff *resp_skb;
1272 struct sadb_x_sa2 *sa2;
1273 struct sadb_address *saddr, *daddr;
1274 struct sadb_msg *out_hdr;
Herbert Xu658b2192007-10-09 13:29:52 -07001275 struct sadb_spirange *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 struct xfrm_state *x = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001277 int mode;
Herbert Xu658b2192007-10-09 13:29:52 -07001278 int err;
1279 u32 min_spi, max_spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 u32 reqid;
1281 u8 proto;
1282 unsigned short family;
1283 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1284
1285 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1286 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1287 return -EINVAL;
1288
1289 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1290 if (proto == 0)
1291 return -EINVAL;
1292
1293 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001294 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1295 if (mode < 0)
1296 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 reqid = sa2->sadb_x_sa2_reqid;
1298 } else {
1299 mode = 0;
1300 reqid = 0;
1301 }
1302
1303 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1304 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1305
1306 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1307 switch (family) {
1308 case AF_INET:
1309 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1310 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1311 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001312#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 case AF_INET6:
1314 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1315 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1316 break;
1317#endif
1318 }
1319
1320 if (hdr->sadb_msg_seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001321 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001322 if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 xfrm_state_put(x);
1324 x = NULL;
1325 }
1326 }
1327
1328 if (!x)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001329 x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 if (x == NULL)
1332 return -ENOENT;
1333
Herbert Xu658b2192007-10-09 13:29:52 -07001334 min_spi = 0x100;
1335 max_spi = 0x0fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Herbert Xu658b2192007-10-09 13:29:52 -07001337 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1338 if (range) {
1339 min_spi = range->sadb_spirange_min;
1340 max_spi = range->sadb_spirange_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
Herbert Xu658b2192007-10-09 13:29:52 -07001342
Fan Du776e9dd2013-12-16 18:47:49 +08001343 err = verify_spi_info(x->id.proto, min_spi, max_spi);
1344 if (err) {
1345 xfrm_state_put(x);
1346 return err;
1347 }
1348
Herbert Xu658b2192007-10-09 13:29:52 -07001349 err = xfrm_alloc_spi(x, min_spi, max_spi);
Herbert Xu050f0092007-10-09 13:31:47 -07001350 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 if (IS_ERR(resp_skb)) {
1353 xfrm_state_put(x);
1354 return PTR_ERR(resp_skb);
1355 }
1356
1357 out_hdr = (struct sadb_msg *) resp_skb->data;
1358 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1359 out_hdr->sadb_msg_type = SADB_GETSPI;
1360 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1361 out_hdr->sadb_msg_errno = 0;
1362 out_hdr->sadb_msg_reserved = 0;
1363 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1364 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1365
1366 xfrm_state_put(x);
1367
David Ahernba51b6b2015-08-24 15:17:17 -06001368 pfkey_broadcast(resp_skb, BROADCAST_ONE, sk, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 return 0;
1371}
1372
David S. Miller4c93fbb2011-02-25 18:07:06 -08001373static 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 -07001374{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001375 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 struct xfrm_state *x;
1377
1378 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1379 return -EOPNOTSUPP;
1380
1381 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1382 return 0;
1383
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001384 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (x == NULL)
1386 return 0;
1387
1388 spin_lock_bh(&x->lock);
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001389 if (x->km.state == XFRM_STATE_ACQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 x->km.state = XFRM_STATE_ERROR;
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 spin_unlock_bh(&x->lock);
1393 xfrm_state_put(x);
1394 return 0;
1395}
1396
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001397static inline int event2poltype(int event)
1398{
1399 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001400 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001401 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001402 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001403 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001404 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001405 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001406 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001407 // return SADB_X_SPDEXPIRE;
1408 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001409 pr_err("pfkey: Unknown policy event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001410 break;
1411 }
1412
1413 return 0;
1414}
1415
1416static inline int event2keytype(int event)
1417{
1418 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001419 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001420 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001421 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001422 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001423 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001424 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001425 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001426 return SADB_EXPIRE;
1427 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001428 pr_err("pfkey: Unknown SA event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001429 break;
1430 }
1431
1432 return 0;
1433}
1434
1435/* ADD/UPD/DEL */
David S. Miller214e0052011-02-24 00:02:38 -05001436static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001437{
1438 struct sk_buff *skb;
1439 struct sadb_msg *hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001440
Herbert Xu050f0092007-10-09 13:31:47 -07001441 skb = pfkey_xfrm_state2msg(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001442
1443 if (IS_ERR(skb))
1444 return PTR_ERR(skb);
1445
1446 hdr = (struct sadb_msg *) skb->data;
1447 hdr->sadb_msg_version = PF_KEY_V2;
1448 hdr->sadb_msg_type = event2keytype(c->event);
1449 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1450 hdr->sadb_msg_errno = 0;
1451 hdr->sadb_msg_reserved = 0;
1452 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001453 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001454
David Ahernba51b6b2015-08-24 15:17:17 -06001455 pfkey_broadcast(skb, BROADCAST_ALL, NULL, xs_net(x));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001456
1457 return 0;
1458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
David S. Miller4c93fbb2011-02-25 18:07:06 -08001460static 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 -07001461{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001462 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 struct xfrm_state *x;
1464 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001465 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001467 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (IS_ERR(x))
1469 return PTR_ERR(x);
1470
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001471 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (hdr->sadb_msg_type == SADB_ADD)
1473 err = xfrm_state_add(x);
1474 else
1475 err = xfrm_state_update(x);
1476
Tetsuo Handa2e710292014-04-22 21:48:30 +09001477 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (err < 0) {
1480 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001481 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001482 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001485 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001486 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001487 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001488 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001489 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001490 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001491 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001492out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001493 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001494 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
David S. Miller4c93fbb2011-02-25 18:07:06 -08001497static 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 -07001498{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001499 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001501 struct km_event c;
1502 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 if (!ext_hdrs[SADB_EXT_SA-1] ||
1505 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1506 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1507 return -EINVAL;
1508
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001509 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (x == NULL)
1511 return -ESRCH;
1512
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001513 if ((err = security_xfrm_state_delete(x)))
1514 goto out;
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001517 err = -EPERM;
1518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
Joy Latten161a09e2006-11-27 13:11:54 -06001520
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001521 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001522
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001523 if (err < 0)
1524 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001525
1526 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001527 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001528 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001529 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001530out:
Tetsuo Handa2e710292014-04-22 21:48:30 +09001531 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 xfrm_state_put(x);
1533
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001534 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
David S. Miller4c93fbb2011-02-25 18:07:06 -08001537static 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 -07001538{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001539 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 __u8 proto;
1541 struct sk_buff *out_skb;
1542 struct sadb_msg *out_hdr;
1543 struct xfrm_state *x;
1544
1545 if (!ext_hdrs[SADB_EXT_SA-1] ||
1546 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1547 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1548 return -EINVAL;
1549
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001550 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (x == NULL)
1552 return -ESRCH;
1553
Herbert Xu050f0092007-10-09 13:31:47 -07001554 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 proto = x->id.proto;
1556 xfrm_state_put(x);
1557 if (IS_ERR(out_skb))
1558 return PTR_ERR(out_skb);
1559
1560 out_hdr = (struct sadb_msg *) out_skb->data;
1561 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Charles Hardin435000b2007-11-22 19:35:15 +08001562 out_hdr->sadb_msg_type = SADB_GET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1564 out_hdr->sadb_msg_errno = 0;
1565 out_hdr->sadb_msg_reserved = 0;
1566 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1567 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
David Ahernba51b6b2015-08-24 15:17:17 -06001568 pfkey_broadcast(out_skb, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 return 0;
1571}
1572
David S. Miller4c93fbb2011-02-25 18:07:06 -08001573static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001574 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 struct sk_buff *skb;
1577 struct sadb_msg *hdr;
1578 int len, auth_len, enc_len, i;
1579
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001580 auth_len = xfrm_count_pfkey_auth_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (auth_len) {
1582 auth_len *= sizeof(struct sadb_alg);
1583 auth_len += sizeof(struct sadb_supported);
1584 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001585
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001586 enc_len = xfrm_count_pfkey_enc_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (enc_len) {
1588 enc_len *= sizeof(struct sadb_alg);
1589 enc_len += sizeof(struct sadb_supported);
1590 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 len = enc_len + auth_len + sizeof(struct sadb_msg);
1593
1594 skb = alloc_skb(len + 16, allocation);
1595 if (!skb)
1596 goto out_put_algs;
1597
1598 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1599 pfkey_hdr_dup(hdr, orig);
1600 hdr->sadb_msg_errno = 0;
1601 hdr->sadb_msg_len = len / sizeof(uint64_t);
1602
1603 if (auth_len) {
1604 struct sadb_supported *sp;
1605 struct sadb_alg *ap;
1606
1607 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1608 ap = (struct sadb_alg *) (sp + 1);
1609
1610 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1611 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1612
1613 for (i = 0; ; i++) {
1614 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1615 if (!aalg)
1616 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001617 if (!aalg->pfkey_supported)
1618 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (aalg->available)
1620 *ap++ = aalg->desc;
1621 }
1622 }
1623
1624 if (enc_len) {
1625 struct sadb_supported *sp;
1626 struct sadb_alg *ap;
1627
1628 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1629 ap = (struct sadb_alg *) (sp + 1);
1630
1631 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1632 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1633
1634 for (i = 0; ; i++) {
1635 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1636 if (!ealg)
1637 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001638 if (!ealg->pfkey_supported)
1639 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (ealg->available)
1641 *ap++ = ealg->desc;
1642 }
1643 }
1644
1645out_put_algs:
1646 return skb;
1647}
1648
David S. Miller4c93fbb2011-02-25 18:07:06 -08001649static 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 -07001650{
1651 struct pfkey_sock *pfk = pfkey_sk(sk);
1652 struct sk_buff *supp_skb;
1653
1654 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1655 return -EINVAL;
1656
1657 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1658 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1659 return -EEXIST;
1660 pfk->registered |= (1<<hdr->sadb_msg_satype);
1661 }
1662
1663 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1666 if (!supp_skb) {
1667 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1668 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1669
1670 return -ENOBUFS;
1671 }
1672
David Ahernba51b6b2015-08-24 15:17:17 -06001673 pfkey_broadcast(supp_skb, BROADCAST_REGISTERED, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 return 0;
1676}
1677
David S. Miller4c93fbb2011-02-25 18:07:06 -08001678static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001679{
1680 struct sk_buff *skb;
1681 struct sadb_msg *hdr;
1682
1683 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1684 if (!skb)
1685 return -ENOBUFS;
1686
1687 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1688 memcpy(hdr, ihdr, sizeof(struct sadb_msg));
1689 hdr->sadb_msg_errno = (uint8_t) 0;
1690 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1691
David Ahernba51b6b2015-08-24 15:17:17 -06001692 return pfkey_broadcast(skb, BROADCAST_ONE, sk, sock_net(sk));
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001693}
1694
David S. Miller214e0052011-02-24 00:02:38 -05001695static int key_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001696{
1697 struct sk_buff *skb;
1698 struct sadb_msg *hdr;
1699
1700 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1701 if (!skb)
1702 return -ENOBUFS;
1703 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf088672005-06-18 22:44:00 -07001704 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001705 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001706 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001707 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001708 hdr->sadb_msg_version = PF_KEY_V2;
1709 hdr->sadb_msg_errno = (uint8_t) 0;
1710 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02001711 hdr->sadb_msg_reserved = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001712
David Ahernba51b6b2015-08-24 15:17:17 -06001713 pfkey_broadcast(skb, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001714
1715 return 0;
1716}
1717
David S. Miller4c93fbb2011-02-25 18:07:06 -08001718static 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 -07001719{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001720 struct net *net = sock_net(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +00001721 unsigned int proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001722 struct km_event c;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001723 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1726 if (proto == 0)
1727 return -EINVAL;
1728
Tetsuo Handa2e710292014-04-22 21:48:30 +09001729 err = xfrm_state_flush(net, proto, true);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001730 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001731 if (err || err2) {
1732 if (err == -ESRCH) /* empty table - go quietly */
1733 err = 0;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001734 return err ? err : err2;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001735 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001736
Herbert Xubf088672005-06-18 22:44:00 -07001737 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001738 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001739 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001740 c.event = XFRM_MSG_FLUSHSA;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001741 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001742 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 return 0;
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1748{
Timo Teras83321d62008-03-03 23:40:12 -08001749 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 struct sk_buff *out_skb;
1751 struct sadb_msg *out_hdr;
1752
Timo Teras83321d62008-03-03 23:40:12 -08001753 if (!pfkey_can_dump(&pfk->sk))
1754 return -ENOBUFS;
1755
Herbert Xu050f0092007-10-09 13:31:47 -07001756 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (IS_ERR(out_skb))
1758 return PTR_ERR(out_skb);
1759
1760 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08001761 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 out_hdr->sadb_msg_type = SADB_DUMP;
1763 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1764 out_hdr->sadb_msg_errno = 0;
1765 out_hdr->sadb_msg_reserved = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07001766 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001767 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07001768
1769 if (pfk->dump.skb)
David Ahernba51b6b2015-08-24 15:17:17 -06001770 pfkey_broadcast(pfk->dump.skb, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001771 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07001772 pfk->dump.skb = out_skb;
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return 0;
1775}
1776
Timo Teras83321d62008-03-03 23:40:12 -08001777static int pfkey_dump_sa(struct pfkey_sock *pfk)
1778{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001779 struct net *net = sock_net(&pfk->sk);
1780 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08001781}
1782
1783static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1784{
Fan Du283bc9f2013-11-07 17:47:50 +08001785 struct net *net = sock_net(&pfk->sk);
1786
1787 xfrm_state_walk_done(&pfk->dump.u.state, net);
Timo Teras83321d62008-03-03 23:40:12 -08001788}
1789
David S. Miller4c93fbb2011-02-25 18:07:06 -08001790static 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 -07001791{
1792 u8 proto;
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001793 struct xfrm_address_filter *filter = NULL;
Timo Teras83321d62008-03-03 23:40:12 -08001794 struct pfkey_sock *pfk = pfkey_sk(sk);
1795
1796 if (pfk->dump.dump != NULL)
1797 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1800 if (proto == 0)
1801 return -EINVAL;
1802
Nicolas Dichteld3623092014-02-14 15:30:36 +01001803 if (ext_hdrs[SADB_X_EXT_FILTER - 1]) {
1804 struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1];
1805
1806 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
1807 if (filter == NULL)
1808 return -ENOMEM;
1809
1810 memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr,
1811 sizeof(xfrm_address_t));
1812 memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr,
1813 sizeof(xfrm_address_t));
1814 filter->family = xfilter->sadb_x_filter_family;
1815 filter->splen = xfilter->sadb_x_filter_splen;
1816 filter->dplen = xfilter->sadb_x_filter_dplen;
1817 }
1818
Timo Teras83321d62008-03-03 23:40:12 -08001819 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001820 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08001821 pfk->dump.dump = pfkey_dump_sa;
1822 pfk->dump.done = pfkey_dump_sa_done;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001823 xfrm_state_walk_init(&pfk->dump.u.state, proto, filter);
Timo Teras4c563f72008-02-28 21:31:08 -08001824
Timo Teras83321d62008-03-03 23:40:12 -08001825 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
David S. Miller4c93fbb2011-02-25 18:07:06 -08001828static 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 -07001829{
1830 struct pfkey_sock *pfk = pfkey_sk(sk);
1831 int satype = hdr->sadb_msg_satype;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001832 bool reset_errno = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001835 reset_errno = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (satype != 0 && satype != 1)
1837 return -EINVAL;
1838 pfk->promisc = satype;
1839 }
David S. Miller4c93fbb2011-02-25 18:07:06 -08001840 if (reset_errno && skb_cloned(skb))
1841 skb = skb_copy(skb, GFP_KERNEL);
1842 else
1843 skb = skb_clone(skb, GFP_KERNEL);
1844
1845 if (reset_errno && skb) {
1846 struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
1847 new_hdr->sadb_msg_errno = 0;
1848 }
1849
David Ahernba51b6b2015-08-24 15:17:17 -06001850 pfkey_broadcast(skb, BROADCAST_ALL, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return 0;
1852}
1853
1854static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1855{
1856 int i;
1857 u32 reqid = *(u32*)ptr;
1858
1859 for (i=0; i<xp->xfrm_nr; i++) {
1860 if (xp->xfrm_vec[i].reqid == reqid)
1861 return -EEXIST;
1862 }
1863 return 0;
1864}
1865
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001866static u32 gen_reqid(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Timo Teras4c563f72008-02-28 21:31:08 -08001868 struct xfrm_policy_walk walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 u32 start;
Timo Teras4c563f72008-02-28 21:31:08 -08001870 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1872
1873 start = reqid;
1874 do {
1875 ++reqid;
1876 if (reqid == 0)
1877 reqid = IPSEC_MANUAL_REQID_MAX+1;
Timo Teras4c563f72008-02-28 21:31:08 -08001878 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001879 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
Fan Du283bc9f2013-11-07 17:47:50 +08001880 xfrm_policy_walk_done(&walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001881 if (rc != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return reqid;
1883 } while (reqid != start);
1884 return 0;
1885}
1886
1887static int
1888parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1889{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001890 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001892 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1895 return -ELOOP;
1896
1897 if (rq->sadb_x_ipsecrequest_mode == 0)
1898 return -EINVAL;
1899
1900 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001901 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1902 return -EINVAL;
1903 t->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1905 t->optional = 1;
1906 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1907 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1908 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1909 t->reqid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001910 if (!t->reqid && !(t->reqid = gen_reqid(net)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return -ENOBUFS;
1912 }
1913
1914 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001915 if (t->mode == XFRM_MODE_TUNNEL) {
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001916 u8 *sa = (u8 *) (rq + 1);
1917 int family, socklen;
1918
1919 family = pfkey_sockaddr_extract((struct sockaddr *)sa,
1920 &t->saddr);
1921 if (!family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return -EINVAL;
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001923
1924 socklen = pfkey_sockaddr_len(family);
1925 if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen),
1926 &t->id.daddr) != family)
1927 return -EINVAL;
1928 t->encap_family = family;
Miika Komu2718aa72006-11-30 16:41:50 -08001929 } else
1930 t->encap_family = xp->family;
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 /* No way to set this via kame pfkey */
Herbert Xuc5d18e92008-04-22 00:46:42 -07001933 t->allalgs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 xp->xfrm_nr++;
1935 return 0;
1936}
1937
1938static int
1939parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1940{
1941 int err;
1942 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1943 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1944
Dan Carpenterf674e722012-09-27 22:21:19 +00001945 if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
1946 return -EINVAL;
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1949 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1950 return err;
1951 len -= rq->sadb_x_ipsecrequest_len;
1952 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1953 }
1954 return 0;
1955}
1956
David S. Miller4c93fbb2011-02-25 18:07:06 -08001957static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
Trent Jaegerdf718372005-12-13 23:12:27 -08001958{
1959 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1960
1961 if (xfrm_ctx) {
1962 int len = sizeof(struct sadb_x_sec_ctx);
1963 len += xfrm_ctx->ctx_len;
1964 return PFKEY_ALIGN8(len);
1965 }
1966 return 0;
1967}
1968
David S. Miller4c93fbb2011-02-25 18:07:06 -08001969static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
David S. Miller4c93fbb2011-02-25 18:07:06 -08001971 const struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08001973 int socklen = 0;
1974 int i;
1975
1976 for (i=0; i<xp->xfrm_nr; i++) {
1977 t = xp->xfrm_vec + i;
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09001978 socklen += pfkey_sockaddr_len(t->encap_family);
Miika Komu2718aa72006-11-30 16:41:50 -08001979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 return sizeof(struct sadb_msg) +
1982 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001983 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 (sockaddr_size * 2) +
1985 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08001986 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1987 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08001988 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
David S. Miller4c93fbb2011-02-25 18:07:06 -08001991static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 struct sk_buff *skb;
1994 int size;
1995
1996 size = pfkey_xfrm_policy2msg_size(xp);
1997
1998 skb = alloc_skb(size + 16, GFP_ATOMIC);
1999 if (skb == NULL)
2000 return ERR_PTR(-ENOBUFS);
2001
2002 return skb;
2003}
2004
David S. Miller4c93fbb2011-02-25 18:07:06 -08002005static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
2007 struct sadb_msg *hdr;
2008 struct sadb_address *addr;
2009 struct sadb_lifetime *lifetime;
2010 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08002011 struct sadb_x_sec_ctx *sec_ctx;
2012 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 int i;
2014 int size;
2015 int sockaddr_size = pfkey_sockaddr_size(xp->family);
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002016 int socklen = pfkey_sockaddr_len(xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 size = pfkey_xfrm_policy2msg_size(xp);
2019
2020 /* call should fill header later */
2021 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2022 memset(hdr, 0, size); /* XXX do we need this ? */
2023
2024 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002025 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002027 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 (sizeof(struct sadb_address)+sockaddr_size)/
2029 sizeof(uint64_t);
2030 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2031 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2032 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2033 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002034 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2035 xp->selector.sport,
2036 (struct sockaddr *) (addr + 1),
2037 xp->family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 BUG();
2039
2040 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002041 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 sizeof(struct sadb_address)+sockaddr_size);
2043 addr->sadb_address_len =
2044 (sizeof(struct sadb_address)+sockaddr_size)/
2045 sizeof(uint64_t);
2046 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2047 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002048 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002050
2051 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2052 (struct sockaddr *) (addr + 1),
2053 xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 /* hard time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002056 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 sizeof(struct sadb_lifetime));
2058 lifetime->sadb_lifetime_len =
2059 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2060 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2061 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2062 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2063 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2064 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2065 /* soft time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002066 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 sizeof(struct sadb_lifetime));
2068 lifetime->sadb_lifetime_len =
2069 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2070 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2071 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2072 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2073 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2074 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2075 /* current time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002076 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 sizeof(struct sadb_lifetime));
2078 lifetime->sadb_lifetime_len =
2079 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2080 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2081 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2082 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2083 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2084 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2085
2086 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2087 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2088 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2089 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2090 if (xp->action == XFRM_POLICY_ALLOW) {
2091 if (xp->xfrm_nr)
2092 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2093 else
2094 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2095 }
2096 pol->sadb_x_policy_dir = dir+1;
Dan Carpenterff862a42013-07-28 23:04:45 +03002097 pol->sadb_x_policy_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 pol->sadb_x_policy_id = xp->index;
2099 pol->sadb_x_policy_priority = xp->priority;
2100
2101 for (i=0; i<xp->xfrm_nr; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002102 const struct xfrm_tmpl *t = xp->xfrm_vec + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 struct sadb_x_ipsecrequest *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 int req_size;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002105 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 req_size = sizeof(struct sadb_x_ipsecrequest);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002108 if (t->mode == XFRM_MODE_TUNNEL) {
2109 socklen = pfkey_sockaddr_len(t->encap_family);
2110 req_size += socklen * 2;
2111 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 size -= 2*socklen;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 rq = (void*)skb_put(skb, req_size);
2115 pol->sadb_x_policy_len += req_size/8;
2116 memset(rq, 0, sizeof(*rq));
2117 rq->sadb_x_ipsecrequest_len = req_size;
2118 rq->sadb_x_ipsecrequest_proto = t->id.proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002119 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2120 return -EINVAL;
David S. Millerfefaa752007-04-17 21:48:10 -07002121 rq->sadb_x_ipsecrequest_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2123 if (t->reqid)
2124 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2125 if (t->optional)
2126 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2127 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002129 if (t->mode == XFRM_MODE_TUNNEL) {
2130 u8 *sa = (void *)(rq + 1);
2131 pfkey_sockaddr_fill(&t->saddr, 0,
2132 (struct sockaddr *)sa,
2133 t->encap_family);
2134 pfkey_sockaddr_fill(&t->id.daddr, 0,
2135 (struct sockaddr *) (sa + socklen),
2136 t->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002139
2140 /* security context */
2141 if ((xfrm_ctx = xp->security)) {
2142 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2143
2144 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2145 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2146 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2147 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2148 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2149 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2150 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2151 xfrm_ctx->ctx_len);
2152 }
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 hdr->sadb_msg_len = size / sizeof(uint64_t);
2155 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002156
2157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
David S. Miller214e0052011-02-24 00:02:38 -05002160static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002161{
2162 struct sk_buff *out_skb;
2163 struct sadb_msg *out_hdr;
2164 int err;
2165
2166 out_skb = pfkey_xfrm_policy2msg_prep(xp);
Dan Carpenter9a127aa2010-03-24 01:47:00 +00002167 if (IS_ERR(out_skb))
2168 return PTR_ERR(out_skb);
2169
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002170 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2171 if (err < 0)
2172 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002173
2174 out_hdr = (struct sadb_msg *) out_skb->data;
2175 out_hdr->sadb_msg_version = PF_KEY_V2;
2176
Herbert Xuf60f6b82005-06-18 22:44:37 -07002177 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002178 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2179 else
2180 out_hdr->sadb_msg_type = event2poltype(c->event);
2181 out_hdr->sadb_msg_errno = 0;
2182 out_hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002183 out_hdr->sadb_msg_pid = c->portid;
David Ahernba51b6b2015-08-24 15:17:17 -06002184 pfkey_broadcast(out_skb, BROADCAST_ALL, NULL, xp_net(xp));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002185 return 0;
2186
2187}
2188
David S. Miller4c93fbb2011-02-25 18:07:06 -08002189static 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 -07002190{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002191 struct net *net = sock_net(sk);
Trent Jaegerdf718372005-12-13 23:12:27 -08002192 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 struct sadb_lifetime *lifetime;
2194 struct sadb_address *sa;
2195 struct sadb_x_policy *pol;
2196 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002197 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002198 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2201 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2202 !ext_hdrs[SADB_X_EXT_POLICY-1])
2203 return -EINVAL;
2204
2205 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2206 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2207 return -EINVAL;
2208 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2209 return -EINVAL;
2210
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002211 xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (xp == NULL)
2213 return -ENOBUFS;
2214
2215 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2216 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2217 xp->priority = pol->sadb_x_policy_priority;
2218
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002219 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 xp->selector.family = xp->family;
2222 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2223 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2224 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2225 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002226 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002228 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2230 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2231
2232 /* Amusing, we set this twice. KAME apps appear to set same value
2233 * in both addresses.
2234 */
2235 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2236
2237 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2238 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002239 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Joe Perchesea110732011-06-13 16:21:26 +00002241 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002242 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01002243 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002244
2245 if (!uctx) {
2246 err = -ENOBUFS;
2247 goto out;
2248 }
2249
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002250 err = security_xfrm_policy_alloc(&xp->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002251 kfree(uctx);
2252
2253 if (err)
2254 goto out;
2255 }
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 xp->lft.soft_byte_limit = XFRM_INF;
2258 xp->lft.hard_byte_limit = XFRM_INF;
2259 xp->lft.soft_packet_limit = XFRM_INF;
2260 xp->lft.hard_packet_limit = XFRM_INF;
2261 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2262 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2263 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2264 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2265 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2266 }
2267 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2268 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2269 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2270 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2271 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2272 }
2273 xp->xfrm_nr = 0;
2274 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2275 (err = parse_ipsecrequests(xp, pol)) < 0)
2276 goto out;
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2279 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002280
Tetsuo Handa2e710292014-04-22 21:48:30 +09002281 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002282
Trent Jaegerdf718372005-12-13 23:12:27 -08002283 if (err)
2284 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002286 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002287 c.event = XFRM_MSG_UPDPOLICY;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002288 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07002289 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002291 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002292 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002293
2294 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return 0;
2297
2298out:
Herbert Xu12a169e2008-10-01 07:03:24 -07002299 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08002300 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return err;
2302}
2303
David S. Miller4c93fbb2011-02-25 18:07:06 -08002304static 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 -07002305{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002306 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 int err;
2308 struct sadb_address *sa;
2309 struct sadb_x_policy *pol;
Paul Moore03e1ad72008-04-12 19:07:52 -07002310 struct xfrm_policy *xp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002312 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002313 struct sadb_x_sec_ctx *sec_ctx;
Brian Haley2db3e472008-04-24 20:38:31 -07002314 struct xfrm_sec_ctx *pol_ctx = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2317 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2318 !ext_hdrs[SADB_X_EXT_POLICY-1])
2319 return -EINVAL;
2320
2321 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2322 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2323 return -EINVAL;
2324
2325 memset(&sel, 0, sizeof(sel));
2326
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002327 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2329 sel.prefixlen_s = sa->sadb_address_prefixlen;
2330 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2331 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2332 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002333 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002335 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2337 sel.prefixlen_d = sa->sadb_address_prefixlen;
2338 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2339 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2340 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002341 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Joe Perchesea110732011-06-13 16:21:26 +00002343 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002344 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01002345 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002346
2347 if (!uctx)
2348 return -ENOMEM;
2349
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002350 err = security_xfrm_policy_alloc(&pol_ctx, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002351 kfree(uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002352 if (err)
2353 return err;
Brian Haley2db3e472008-04-24 20:38:31 -07002354 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002355
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002356 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Paul Moore03e1ad72008-04-12 19:07:52 -07002357 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2358 1, &err);
2359 security_xfrm_policy_free(pol_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (xp == NULL)
2361 return -ENOENT;
2362
Tetsuo Handa2e710292014-04-22 21:48:30 +09002363 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08002364
2365 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002366 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08002367
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002368 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002369 c.portid = hdr->sadb_msg_pid;
Tobias Brunner1839faa2008-10-10 14:07:03 -07002370 c.data.byid = 0;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002371 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002372 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2373
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002374out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002375 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00002376 if (err == 0)
2377 xfrm_garbage_collect(net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002378 return err;
2379}
2380
David S. Miller4c93fbb2011-02-25 18:07:06 -08002381static 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 -07002382{
2383 int err;
2384 struct sk_buff *out_skb;
2385 struct sadb_msg *out_hdr;
2386 err = 0;
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2389 if (IS_ERR(out_skb)) {
2390 err = PTR_ERR(out_skb);
2391 goto out;
2392 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002393 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2394 if (err < 0)
2395 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 out_hdr = (struct sadb_msg *) out_skb->data;
2398 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002399 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 out_hdr->sadb_msg_satype = 0;
2401 out_hdr->sadb_msg_errno = 0;
2402 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2403 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
David Ahernba51b6b2015-08-24 15:17:17 -06002404 pfkey_broadcast(out_skb, BROADCAST_ONE, sk, xp_net(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 err = 0;
2406
2407out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 return err;
2409}
2410
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002411#ifdef CONFIG_NET_KEY_MIGRATE
2412static int pfkey_sockaddr_pair_size(sa_family_t family)
2413{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002414 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002415}
2416
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002417static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002418 xfrm_address_t *saddr, xfrm_address_t *daddr,
2419 u16 *family)
2420{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002421 int af, socklen;
2422
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002423 if (ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002424 return -EINVAL;
2425
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002426 af = pfkey_sockaddr_extract(sa, saddr);
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002427 if (!af)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002428 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002429
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002430 socklen = pfkey_sockaddr_len(af);
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002431 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002432 daddr) != af)
2433 return -EINVAL;
2434
2435 *family = af;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002436 return 0;
2437}
2438
2439static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2440 struct xfrm_migrate *m)
2441{
2442 int err;
2443 struct sadb_x_ipsecrequest *rq2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002444 int mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002445
2446 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2447 len < rq1->sadb_x_ipsecrequest_len)
2448 return -EINVAL;
2449
2450 /* old endoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002451 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
2452 rq1->sadb_x_ipsecrequest_len,
2453 &m->old_saddr, &m->old_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002454 &m->old_family);
2455 if (err)
2456 return err;
2457
2458 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2459 len -= rq1->sadb_x_ipsecrequest_len;
2460
2461 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2462 len < rq2->sadb_x_ipsecrequest_len)
2463 return -EINVAL;
2464
2465 /* new endpoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002466 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
2467 rq2->sadb_x_ipsecrequest_len,
2468 &m->new_saddr, &m->new_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002469 &m->new_family);
2470 if (err)
2471 return err;
2472
2473 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2474 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2475 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2476 return -EINVAL;
2477
2478 m->proto = rq1->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002479 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2480 return -EINVAL;
2481 m->mode = mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002482 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2483
2484 return ((int)(rq1->sadb_x_ipsecrequest_len +
2485 rq2->sadb_x_ipsecrequest_len));
2486}
2487
2488static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002489 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002490{
2491 int i, len, ret, err = -EINVAL;
2492 u8 dir;
2493 struct sadb_address *sa;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002494 struct sadb_x_kmaddress *kma;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002495 struct sadb_x_policy *pol;
2496 struct sadb_x_ipsecrequest *rq;
2497 struct xfrm_selector sel;
2498 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002499 struct xfrm_kmaddress k;
Fan Du8d549c42013-11-07 17:47:49 +08002500 struct net *net = sock_net(sk);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002501
2502 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002503 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002504 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2505 err = -EINVAL;
2506 goto out;
2507 }
2508
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002509 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002510 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002511
2512 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2513 err = -EINVAL;
2514 goto out;
2515 }
2516
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002517 if (kma) {
2518 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2519 k.reserved = kma->sadb_x_kmaddress_reserved;
2520 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2521 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2522 &k.local, &k.remote, &k.family);
2523 if (ret < 0) {
2524 err = ret;
2525 goto out;
2526 }
2527 }
2528
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002529 dir = pol->sadb_x_policy_dir - 1;
2530 memset(&sel, 0, sizeof(sel));
2531
2532 /* set source address info of selector */
2533 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2534 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2535 sel.prefixlen_s = sa->sadb_address_prefixlen;
2536 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2537 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2538 if (sel.sport)
Al Viro582ee432007-07-26 17:33:39 +01002539 sel.sport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002540
2541 /* set destination address info of selector */
Himangi Saraogi47162c02014-05-30 21:18:12 +05302542 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002543 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2544 sel.prefixlen_d = sa->sadb_address_prefixlen;
2545 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2546 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2547 if (sel.dport)
Al Viro582ee432007-07-26 17:33:39 +01002548 sel.dport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002549
2550 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2551
2552 /* extract ipsecrequests */
2553 i = 0;
2554 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2555
2556 while (len > 0 && i < XFRM_MAX_DEPTH) {
2557 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2558 if (ret < 0) {
2559 err = ret;
2560 goto out;
2561 } else {
2562 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2563 len -= ret;
2564 i++;
2565 }
2566 }
2567
2568 if (!i || len > 0) {
2569 err = -EINVAL;
2570 goto out;
2571 }
2572
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002573 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
Fan Du8d549c42013-11-07 17:47:49 +08002574 kma ? &k : NULL, net);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002575
2576 out:
2577 return err;
2578}
2579#else
2580static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
Stephen Hemminger7f6daa62011-03-01 22:51:52 -08002581 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002582{
2583 return -ENOPROTOOPT;
2584}
2585#endif
2586
2587
David S. Miller4c93fbb2011-02-25 18:07:06 -08002588static 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 -07002589{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002590 struct net *net = sock_net(sk);
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002591 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002592 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 struct sadb_x_policy *pol;
2594 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002595 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2598 return -EINVAL;
2599
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002600 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2601 if (dir >= XFRM_POLICY_MAX)
2602 return -EINVAL;
2603
Eric Paris215a2dd2007-03-07 16:01:45 -08002604 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002605 xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002606 dir, pol->sadb_x_policy_id, delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (xp == NULL)
2608 return -ENOENT;
2609
Eric Paris215a2dd2007-03-07 16:01:45 -08002610 if (delete) {
Tetsuo Handa2e710292014-04-22 21:48:30 +09002611 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Eric Paris215a2dd2007-03-07 16:01:45 -08002613 if (err)
2614 goto out;
2615 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002616 c.portid = hdr->sadb_msg_pid;
Herbert Xubf088672005-06-18 22:44:00 -07002617 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002618 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002619 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002620 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002621 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Eric Paris215a2dd2007-03-07 16:01:45 -08002624out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00002626 if (delete && err == 0)
2627 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 return err;
2629}
2630
2631static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2632{
Timo Teras83321d62008-03-03 23:40:12 -08002633 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 struct sk_buff *out_skb;
2635 struct sadb_msg *out_hdr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002636 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Timo Teras83321d62008-03-03 23:40:12 -08002638 if (!pfkey_can_dump(&pfk->sk))
2639 return -ENOBUFS;
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2642 if (IS_ERR(out_skb))
2643 return PTR_ERR(out_skb);
2644
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002645 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2646 if (err < 0)
2647 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08002650 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2652 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2653 out_hdr->sadb_msg_errno = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07002654 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002655 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07002656
2657 if (pfk->dump.skb)
David Ahernba51b6b2015-08-24 15:17:17 -06002658 pfkey_broadcast(pfk->dump.skb, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002659 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07002660 pfk->dump.skb = out_skb;
2661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return 0;
2663}
2664
Timo Teras83321d62008-03-03 23:40:12 -08002665static int pfkey_dump_sp(struct pfkey_sock *pfk)
2666{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002667 struct net *net = sock_net(&pfk->sk);
2668 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08002669}
2670
2671static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2672{
Fan Du283bc9f2013-11-07 17:47:50 +08002673 struct net *net = sock_net((struct sock *)pfk);
2674
2675 xfrm_policy_walk_done(&pfk->dump.u.policy, net);
Timo Teras83321d62008-03-03 23:40:12 -08002676}
2677
David S. Miller4c93fbb2011-02-25 18:07:06 -08002678static 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 -07002679{
Timo Teras83321d62008-03-03 23:40:12 -08002680 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Timo Teras83321d62008-03-03 23:40:12 -08002682 if (pfk->dump.dump != NULL)
2683 return -EBUSY;
Timo Teras4c563f72008-02-28 21:31:08 -08002684
Timo Teras83321d62008-03-03 23:40:12 -08002685 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002686 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08002687 pfk->dump.dump = pfkey_dump_sp;
2688 pfk->dump.done = pfkey_dump_sp_done;
2689 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2690
2691 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}
2693
David S. Miller214e0052011-02-24 00:02:38 -05002694static int key_notify_policy_flush(const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
2696 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002697 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002699 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 if (!skb_out)
2701 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002702 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002703 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002704 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002705 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002706 hdr->sadb_msg_version = PF_KEY_V2;
2707 hdr->sadb_msg_errno = (uint8_t) 0;
Nicolas Dichtel85dfb742013-02-18 16:24:20 +01002708 hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002709 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02002710 hdr->sadb_msg_reserved = 0;
David Ahernba51b6b2015-08-24 15:17:17 -06002711 pfkey_broadcast(skb_out, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002712 return 0;
2713
2714}
2715
David S. Miller4c93fbb2011-02-25 18:07:06 -08002716static 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 -07002717{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002718 struct net *net = sock_net(sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002719 struct km_event c;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002720 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Tetsuo Handa2e710292014-04-22 21:48:30 +09002722 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002723 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002724 if (err || err2) {
2725 if (err == -ESRCH) /* empty table - old silent behavior */
2726 return 0;
2727 return err;
2728 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002729
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002730 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002731 c.event = XFRM_MSG_FLUSHPOLICY;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002732 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002733 c.seq = hdr->sadb_msg_seq;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002734 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002735 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737 return 0;
2738}
2739
2740typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002741 const struct sadb_msg *hdr, void * const *ext_hdrs);
Mathias Krause8603b9552013-06-26 23:56:59 +02002742static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 [SADB_RESERVED] = pfkey_reserved,
2744 [SADB_GETSPI] = pfkey_getspi,
2745 [SADB_UPDATE] = pfkey_add,
2746 [SADB_ADD] = pfkey_add,
2747 [SADB_DELETE] = pfkey_delete,
2748 [SADB_GET] = pfkey_get,
2749 [SADB_ACQUIRE] = pfkey_acquire,
2750 [SADB_REGISTER] = pfkey_register,
2751 [SADB_EXPIRE] = NULL,
2752 [SADB_FLUSH] = pfkey_flush,
2753 [SADB_DUMP] = pfkey_dump,
2754 [SADB_X_PROMISC] = pfkey_promisc,
2755 [SADB_X_PCHANGE] = NULL,
2756 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2757 [SADB_X_SPDADD] = pfkey_spdadd,
2758 [SADB_X_SPDDELETE] = pfkey_spddelete,
2759 [SADB_X_SPDGET] = pfkey_spdget,
2760 [SADB_X_SPDACQUIRE] = NULL,
2761 [SADB_X_SPDDUMP] = pfkey_spddump,
2762 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2763 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2764 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002765 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766};
2767
David S. Miller4c93fbb2011-02-25 18:07:06 -08002768static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
2770 void *ext_hdrs[SADB_EXT_MAX];
2771 int err;
2772
David Ahernba51b6b2015-08-24 15:17:17 -06002773 pfkey_broadcast(skb_clone(skb, GFP_KERNEL),
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002774 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2777 err = parse_exthdrs(skb, hdr, ext_hdrs);
2778 if (!err) {
2779 err = -EOPNOTSUPP;
2780 if (pfkey_funcs[hdr->sadb_msg_type])
2781 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2782 }
2783 return err;
2784}
2785
2786static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2787{
2788 struct sadb_msg *hdr = NULL;
2789
2790 if (skb->len < sizeof(*hdr)) {
2791 *errp = -EMSGSIZE;
2792 } else {
2793 hdr = (struct sadb_msg *) skb->data;
2794 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2795 hdr->sadb_msg_reserved != 0 ||
2796 (hdr->sadb_msg_type <= SADB_RESERVED ||
2797 hdr->sadb_msg_type > SADB_MAX)) {
2798 hdr = NULL;
2799 *errp = -EINVAL;
2800 } else if (hdr->sadb_msg_len != (skb->len /
2801 sizeof(uint64_t)) ||
2802 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2803 sizeof(uint64_t))) {
2804 hdr = NULL;
2805 *errp = -EMSGSIZE;
2806 } else {
2807 *errp = 0;
2808 }
2809 }
2810 return hdr;
2811}
2812
David S. Miller4c93fbb2011-02-25 18:07:06 -08002813static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
2814 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Herbert Xuf3980352007-12-19 23:44:29 -08002816 unsigned int id = d->desc.sadb_alg_id;
2817
2818 if (id >= sizeof(t->aalgos) * 8)
2819 return 0;
2820
2821 return (t->aalgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
2823
David S. Miller4c93fbb2011-02-25 18:07:06 -08002824static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
2825 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
Herbert Xuf3980352007-12-19 23:44:29 -08002827 unsigned int id = d->desc.sadb_alg_id;
2828
2829 if (id >= sizeof(t->ealgos) * 8)
2830 return 0;
2831
2832 return (t->ealgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833}
2834
David S. Miller4c93fbb2011-02-25 18:07:06 -08002835static int count_ah_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
2837 int i, sz = 0;
2838
2839 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002840 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (!aalg)
2842 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002843 if (!aalg->pfkey_supported)
2844 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 if (aalg_tmpl_set(t, aalg) && aalg->available)
2846 sz += sizeof(struct sadb_comb);
2847 }
2848 return sz + sizeof(struct sadb_prop);
2849}
2850
David S. Miller4c93fbb2011-02-25 18:07:06 -08002851static int count_esp_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
2853 int i, k, sz = 0;
2854
2855 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002856 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (!ealg)
2858 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002859
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002860 if (!ealg->pfkey_supported)
2861 continue;
2862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2864 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 for (k = 1; ; k++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002867 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (!aalg)
2869 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002870
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002871 if (!aalg->pfkey_supported)
2872 continue;
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 if (aalg_tmpl_set(t, aalg) && aalg->available)
2875 sz += sizeof(struct sadb_comb);
2876 }
2877 }
2878 return sz + sizeof(struct sadb_prop);
2879}
2880
David S. Miller4c93fbb2011-02-25 18:07:06 -08002881static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
2883 struct sadb_prop *p;
2884 int i;
2885
2886 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2887 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2888 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2889 p->sadb_prop_replay = 32;
2890 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2891
2892 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002893 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (!aalg)
2895 break;
2896
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002897 if (!aalg->pfkey_supported)
2898 continue;
2899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2901 struct sadb_comb *c;
2902 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2903 memset(c, 0, sizeof(*c));
2904 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2905 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2906 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2907 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2908 c->sadb_comb_hard_addtime = 24*60*60;
2909 c->sadb_comb_soft_addtime = 20*60*60;
2910 c->sadb_comb_hard_usetime = 8*60*60;
2911 c->sadb_comb_soft_usetime = 7*60*60;
2912 }
2913 }
2914}
2915
David S. Miller4c93fbb2011-02-25 18:07:06 -08002916static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
2918 struct sadb_prop *p;
2919 int i, k;
2920
2921 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2922 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2923 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2924 p->sadb_prop_replay = 32;
2925 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2926
2927 for (i=0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002928 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 if (!ealg)
2930 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002931
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002932 if (!ealg->pfkey_supported)
2933 continue;
2934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2936 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 for (k = 1; ; k++) {
2939 struct sadb_comb *c;
David S. Miller4c93fbb2011-02-25 18:07:06 -08002940 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 if (!aalg)
2942 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002943 if (!aalg->pfkey_supported)
2944 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2946 continue;
2947 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2948 memset(c, 0, sizeof(*c));
2949 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2950 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2951 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2952 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2953 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2954 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2955 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2956 c->sadb_comb_hard_addtime = 24*60*60;
2957 c->sadb_comb_soft_addtime = 20*60*60;
2958 c->sadb_comb_hard_usetime = 8*60*60;
2959 c->sadb_comb_soft_usetime = 7*60*60;
2960 }
2961 }
2962}
2963
David S. Miller214e0052011-02-24 00:02:38 -05002964static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002965{
2966 return 0;
2967}
2968
David S. Miller214e0052011-02-24 00:02:38 -05002969static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
2971 struct sk_buff *out_skb;
2972 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002973 int hard;
2974 int hsc;
2975
Herbert Xubf088672005-06-18 22:44:00 -07002976 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002977 if (hard)
2978 hsc = 2;
2979 else
2980 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Herbert Xu050f0092007-10-09 13:31:47 -07002982 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 if (IS_ERR(out_skb))
2984 return PTR_ERR(out_skb);
2985
2986 out_hdr = (struct sadb_msg *) out_skb->data;
2987 out_hdr->sadb_msg_version = PF_KEY_V2;
2988 out_hdr->sadb_msg_type = SADB_EXPIRE;
2989 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2990 out_hdr->sadb_msg_errno = 0;
2991 out_hdr->sadb_msg_reserved = 0;
2992 out_hdr->sadb_msg_seq = 0;
2993 out_hdr->sadb_msg_pid = 0;
2994
David Ahernba51b6b2015-08-24 15:17:17 -06002995 pfkey_broadcast(out_skb, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return 0;
2997}
2998
David S. Miller214e0052011-02-24 00:02:38 -05002999static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003000{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003001 struct net *net = x ? xs_net(x) : c->net;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003002 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3003
3004 if (atomic_read(&net_pfkey->socks_nr) == 0)
Jamal Hadi Salim99c6f602008-06-10 14:25:34 -07003005 return 0;
3006
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003007 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003008 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003009 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003010 case XFRM_MSG_DELSA:
3011 case XFRM_MSG_NEWSA:
3012 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003013 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003014 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003015 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003016 case XFRM_MSG_NEWAE: /* not yet supported */
3017 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003018 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003019 pr_err("pfkey: Unknown SA event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003020 break;
3021 }
3022
3023 return 0;
3024}
3025
David S. Miller214e0052011-02-24 00:02:38 -05003026static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003027{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003028 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3029 return 0;
3030
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003031 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003032 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003033 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003034 case XFRM_MSG_DELPOLICY:
3035 case XFRM_MSG_NEWPOLICY:
3036 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003037 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003038 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003039 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3040 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003041 return key_notify_policy_flush(c);
3042 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003043 pr_err("pfkey: Unknown policy event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003044 break;
3045 }
3046
3047 return 0;
3048}
3049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050static u32 get_acqseq(void)
3051{
3052 u32 res;
Eric Dumazet28aecb92010-01-29 04:05:52 +00003053 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Eric Dumazet28aecb92010-01-29 04:05:52 +00003055 do {
3056 res = atomic_inc_return(&acqseq);
3057 } while (!res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return res;
3059}
3060
Horia Geanta0f245582014-02-12 16:20:06 +02003061static bool pfkey_is_alive(const struct km_event *c)
3062{
3063 struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id);
3064 struct sock *sk;
3065 bool is_alive = false;
3066
3067 rcu_read_lock();
3068 sk_for_each_rcu(sk, &net_pfkey->table) {
3069 if (pfkey_sk(sk)->registered) {
3070 is_alive = true;
3071 break;
3072 }
3073 }
3074 rcu_read_unlock();
3075
3076 return is_alive;
3077}
3078
Fan Du65e07362012-08-15 10:13:47 +08003079static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
3081 struct sk_buff *skb;
3082 struct sadb_msg *hdr;
3083 struct sadb_address *addr;
3084 struct sadb_x_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 int sockaddr_size;
3086 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003087 struct sadb_x_sec_ctx *sec_ctx;
3088 struct xfrm_sec_ctx *xfrm_ctx;
3089 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3092 if (!sockaddr_size)
3093 return -EINVAL;
3094
3095 size = sizeof(struct sadb_msg) +
3096 (sizeof(struct sadb_address) * 2) +
3097 (sockaddr_size * 2) +
3098 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003099
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 if (x->id.proto == IPPROTO_AH)
3101 size += count_ah_combs(t);
3102 else if (x->id.proto == IPPROTO_ESP)
3103 size += count_esp_combs(t);
3104
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003105 if ((xfrm_ctx = x->security)) {
3106 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3107 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3108 }
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 skb = alloc_skb(size + 16, GFP_ATOMIC);
3111 if (skb == NULL)
3112 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3115 hdr->sadb_msg_version = PF_KEY_V2;
3116 hdr->sadb_msg_type = SADB_ACQUIRE;
3117 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3118 hdr->sadb_msg_len = size / sizeof(uint64_t);
3119 hdr->sadb_msg_errno = 0;
3120 hdr->sadb_msg_reserved = 0;
3121 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3122 hdr->sadb_msg_pid = 0;
3123
3124 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003125 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003127 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 (sizeof(struct sadb_address)+sockaddr_size)/
3129 sizeof(uint64_t);
3130 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3131 addr->sadb_address_proto = 0;
3132 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003133 addr->sadb_address_prefixlen =
3134 pfkey_sockaddr_fill(&x->props.saddr, 0,
3135 (struct sockaddr *) (addr + 1),
3136 x->props.family);
3137 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003141 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 sizeof(struct sadb_address)+sockaddr_size);
3143 addr->sadb_address_len =
3144 (sizeof(struct sadb_address)+sockaddr_size)/
3145 sizeof(uint64_t);
3146 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3147 addr->sadb_address_proto = 0;
3148 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003149 addr->sadb_address_prefixlen =
3150 pfkey_sockaddr_fill(&x->id.daddr, 0,
3151 (struct sockaddr *) (addr + 1),
3152 x->props.family);
3153 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 BUG();
3155
3156 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3157 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3158 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3159 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
Fan Du65e07362012-08-15 10:13:47 +08003160 pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1;
Dan Carpenterff862a42013-07-28 23:04:45 +03003161 pol->sadb_x_policy_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 pol->sadb_x_policy_id = xp->index;
Dan Carpenterff862a42013-07-28 23:04:45 +03003163 pol->sadb_x_policy_priority = xp->priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
3165 /* Set sadb_comb's. */
3166 if (x->id.proto == IPPROTO_AH)
3167 dump_ah_combs(skb, t);
3168 else if (x->id.proto == IPPROTO_ESP)
3169 dump_esp_combs(skb, t);
3170
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003171 /* security context */
3172 if (xfrm_ctx) {
3173 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3174 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3175 sec_ctx->sadb_x_sec_len =
3176 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3177 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3178 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3179 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3180 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3181 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3182 xfrm_ctx->ctx_len);
3183 }
3184
David Ahernba51b6b2015-08-24 15:17:17 -06003185 return pfkey_broadcast(skb, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186}
3187
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003188static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003189 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003191 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 struct xfrm_policy *xp;
3193 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003194 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003196 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 case AF_INET:
3198 if (opt != IP_IPSEC_POLICY) {
3199 *dir = -EOPNOTSUPP;
3200 return NULL;
3201 }
3202 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00003203#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 case AF_INET6:
3205 if (opt != IPV6_IPSEC_POLICY) {
3206 *dir = -EOPNOTSUPP;
3207 return NULL;
3208 }
3209 break;
3210#endif
3211 default:
3212 *dir = -EINVAL;
3213 return NULL;
3214 }
3215
3216 *dir = -EINVAL;
3217
3218 if (len < sizeof(struct sadb_x_policy) ||
3219 pol->sadb_x_policy_len*8 > len ||
3220 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3221 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3222 return NULL;
3223
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003224 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 if (xp == NULL) {
3226 *dir = -ENOBUFS;
3227 return NULL;
3228 }
3229
3230 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3231 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3232
3233 xp->lft.soft_byte_limit = XFRM_INF;
3234 xp->lft.hard_byte_limit = XFRM_INF;
3235 xp->lft.soft_packet_limit = XFRM_INF;
3236 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003237 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239 xp->xfrm_nr = 0;
3240 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3241 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3242 goto out;
3243
Trent Jaegerdf718372005-12-13 23:12:27 -08003244 /* security context too */
3245 if (len >= (pol->sadb_x_policy_len*8 +
3246 sizeof(struct sadb_x_sec_ctx))) {
3247 char *p = (char *)pol;
3248 struct xfrm_user_sec_ctx *uctx;
3249
3250 p += pol->sadb_x_policy_len*8;
3251 sec_ctx = (struct sadb_x_sec_ctx *)p;
3252 if (len < pol->sadb_x_policy_len*8 +
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003253 sec_ctx->sadb_x_sec_len) {
3254 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003255 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003256 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003257 if ((*dir = verify_sec_ctx_len(p)))
3258 goto out;
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01003259 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_ATOMIC);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01003260 *dir = security_xfrm_policy_alloc(&xp->security, uctx, GFP_ATOMIC);
Trent Jaegerdf718372005-12-13 23:12:27 -08003261 kfree(uctx);
3262
3263 if (*dir)
3264 goto out;
3265 }
3266
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 *dir = pol->sadb_x_policy_dir-1;
3268 return xp;
3269
3270out:
Alexey Dobriyan70e90672008-11-06 23:08:37 -08003271 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08003272 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 return NULL;
3274}
3275
Al Viro5d36b182006-11-08 00:24:06 -08003276static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
3278 struct sk_buff *skb;
3279 struct sadb_msg *hdr;
3280 struct sadb_sa *sa;
3281 struct sadb_address *addr;
3282 struct sadb_x_nat_t_port *n_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 int sockaddr_size;
3284 int size;
3285 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3286 struct xfrm_encap_tmpl *natt = NULL;
3287
3288 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3289 if (!sockaddr_size)
3290 return -EINVAL;
3291
3292 if (!satype)
3293 return -EINVAL;
3294
3295 if (!x->encap)
3296 return -EINVAL;
3297
3298 natt = x->encap;
3299
3300 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3301 *
3302 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3303 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3304 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 size = sizeof(struct sadb_msg) +
3307 sizeof(struct sadb_sa) +
3308 (sizeof(struct sadb_address) * 2) +
3309 (sockaddr_size * 2) +
3310 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003311
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 skb = alloc_skb(size + 16, GFP_ATOMIC);
3313 if (skb == NULL)
3314 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3317 hdr->sadb_msg_version = PF_KEY_V2;
3318 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3319 hdr->sadb_msg_satype = satype;
3320 hdr->sadb_msg_len = size / sizeof(uint64_t);
3321 hdr->sadb_msg_errno = 0;
3322 hdr->sadb_msg_reserved = 0;
3323 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3324 hdr->sadb_msg_pid = 0;
3325
3326 /* SA */
3327 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3328 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3329 sa->sadb_sa_exttype = SADB_EXT_SA;
3330 sa->sadb_sa_spi = x->id.spi;
3331 sa->sadb_sa_replay = 0;
3332 sa->sadb_sa_state = 0;
3333 sa->sadb_sa_auth = 0;
3334 sa->sadb_sa_encrypt = 0;
3335 sa->sadb_sa_flags = 0;
3336
3337 /* ADDRESS_SRC (old addr) */
3338 addr = (struct sadb_address*)
3339 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003340 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 (sizeof(struct sadb_address)+sockaddr_size)/
3342 sizeof(uint64_t);
3343 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3344 addr->sadb_address_proto = 0;
3345 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003346 addr->sadb_address_prefixlen =
3347 pfkey_sockaddr_fill(&x->props.saddr, 0,
3348 (struct sockaddr *) (addr + 1),
3349 x->props.family);
3350 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 BUG();
3352
3353 /* NAT_T_SPORT (old port) */
3354 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3355 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3356 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3357 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3358 n_port->sadb_x_nat_t_port_reserved = 0;
3359
3360 /* ADDRESS_DST (new addr) */
3361 addr = (struct sadb_address*)
3362 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003363 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 (sizeof(struct sadb_address)+sockaddr_size)/
3365 sizeof(uint64_t);
3366 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3367 addr->sadb_address_proto = 0;
3368 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003369 addr->sadb_address_prefixlen =
3370 pfkey_sockaddr_fill(ipaddr, 0,
3371 (struct sockaddr *) (addr + 1),
3372 x->props.family);
3373 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 BUG();
3375
3376 /* NAT_T_DPORT (new port) */
3377 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3378 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3379 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3380 n_port->sadb_x_nat_t_port_port = sport;
3381 n_port->sadb_x_nat_t_port_reserved = 0;
3382
David Ahernba51b6b2015-08-24 15:17:17 -06003383 return pfkey_broadcast(skb, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384}
3385
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003386#ifdef CONFIG_NET_KEY_MIGRATE
3387static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
David S. Miller183cad12011-02-24 00:28:01 -05003388 const struct xfrm_selector *sel)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003389{
3390 struct sadb_address *addr;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003391 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3392 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3393 addr->sadb_address_exttype = type;
3394 addr->sadb_address_proto = sel->proto;
3395 addr->sadb_address_reserved = 0;
3396
3397 switch (type) {
3398 case SADB_EXT_ADDRESS_SRC:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003399 addr->sadb_address_prefixlen = sel->prefixlen_s;
3400 pfkey_sockaddr_fill(&sel->saddr, 0,
3401 (struct sockaddr *)(addr + 1),
3402 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003403 break;
3404 case SADB_EXT_ADDRESS_DST:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003405 addr->sadb_address_prefixlen = sel->prefixlen_d;
3406 pfkey_sockaddr_fill(&sel->daddr, 0,
3407 (struct sockaddr *)(addr + 1),
3408 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003409 break;
3410 default:
3411 return -EINVAL;
3412 }
3413
3414 return 0;
3415}
3416
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003417
David S. Miller183cad12011-02-24 00:28:01 -05003418static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003419{
3420 struct sadb_x_kmaddress *kma;
3421 u8 *sa;
3422 int family = k->family;
3423 int socklen = pfkey_sockaddr_len(family);
3424 int size_req;
3425
3426 size_req = (sizeof(struct sadb_x_kmaddress) +
3427 pfkey_sockaddr_pair_size(family));
3428
3429 kma = (struct sadb_x_kmaddress *)skb_put(skb, size_req);
3430 memset(kma, 0, size_req);
3431 kma->sadb_x_kmaddress_len = size_req / 8;
3432 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3433 kma->sadb_x_kmaddress_reserved = k->reserved;
3434
3435 sa = (u8 *)(kma + 1);
3436 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3437 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3438 return -EINVAL;
3439
3440 return 0;
3441}
3442
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003443static int set_ipsecrequest(struct sk_buff *skb,
3444 uint8_t proto, uint8_t mode, int level,
3445 uint32_t reqid, uint8_t family,
David S. Miller183cad12011-02-24 00:28:01 -05003446 const xfrm_address_t *src, const xfrm_address_t *dst)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003447{
3448 struct sadb_x_ipsecrequest *rq;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003449 u8 *sa;
3450 int socklen = pfkey_sockaddr_len(family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003451 int size_req;
3452
3453 size_req = sizeof(struct sadb_x_ipsecrequest) +
3454 pfkey_sockaddr_pair_size(family);
3455
3456 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3457 memset(rq, 0, size_req);
3458 rq->sadb_x_ipsecrequest_len = size_req;
3459 rq->sadb_x_ipsecrequest_proto = proto;
3460 rq->sadb_x_ipsecrequest_mode = mode;
3461 rq->sadb_x_ipsecrequest_level = level;
3462 rq->sadb_x_ipsecrequest_reqid = reqid;
3463
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003464 sa = (u8 *) (rq + 1);
3465 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3466 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003467 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003468
3469 return 0;
3470}
3471#endif
3472
3473#ifdef CONFIG_NET_KEY_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05003474static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3475 const struct xfrm_migrate *m, int num_bundles,
3476 const struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003477{
3478 int i;
3479 int sasize_sel;
3480 int size = 0;
3481 int size_pol = 0;
3482 struct sk_buff *skb;
3483 struct sadb_msg *hdr;
3484 struct sadb_x_policy *pol;
David S. Miller183cad12011-02-24 00:28:01 -05003485 const struct xfrm_migrate *mp;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003486
3487 if (type != XFRM_POLICY_TYPE_MAIN)
3488 return 0;
3489
3490 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3491 return -EINVAL;
3492
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003493 if (k != NULL) {
3494 /* addresses for KM */
3495 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3496 pfkey_sockaddr_pair_size(k->family));
3497 }
3498
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003499 /* selector */
3500 sasize_sel = pfkey_sockaddr_size(sel->family);
3501 if (!sasize_sel)
3502 return -EINVAL;
3503 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3504
3505 /* policy info */
3506 size_pol += sizeof(struct sadb_x_policy);
3507
3508 /* ipsecrequests */
3509 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3510 /* old locator pair */
3511 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3512 pfkey_sockaddr_pair_size(mp->old_family);
3513 /* new locator pair */
3514 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3515 pfkey_sockaddr_pair_size(mp->new_family);
3516 }
3517
3518 size += sizeof(struct sadb_msg) + size_pol;
3519
3520 /* alloc buffer */
3521 skb = alloc_skb(size, GFP_ATOMIC);
3522 if (skb == NULL)
3523 return -ENOMEM;
3524
3525 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3526 hdr->sadb_msg_version = PF_KEY_V2;
3527 hdr->sadb_msg_type = SADB_X_MIGRATE;
3528 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3529 hdr->sadb_msg_len = size / 8;
3530 hdr->sadb_msg_errno = 0;
3531 hdr->sadb_msg_reserved = 0;
3532 hdr->sadb_msg_seq = 0;
3533 hdr->sadb_msg_pid = 0;
3534
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003535 /* Addresses to be used by KM for negotiation, if ext is available */
3536 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
Julia Lawall89eb06f2012-04-08 22:41:10 +00003537 goto err;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003538
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003539 /* selector src */
3540 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3541
3542 /* selector dst */
3543 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3544
3545 /* policy information */
3546 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3547 pol->sadb_x_policy_len = size_pol / 8;
3548 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3549 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3550 pol->sadb_x_policy_dir = dir + 1;
Dan Carpenterff862a42013-07-28 23:04:45 +03003551 pol->sadb_x_policy_reserved = 0;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003552 pol->sadb_x_policy_id = 0;
3553 pol->sadb_x_policy_priority = 0;
3554
3555 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3556 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003557 int mode = pfkey_mode_from_xfrm(mp->mode);
3558 if (mode < 0)
Patrick McHardyd4782c32008-01-20 17:24:29 -08003559 goto err;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003560 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003561 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3562 mp->reqid, mp->old_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003563 &mp->old_saddr, &mp->old_daddr) < 0)
3564 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003565
3566 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003567 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003568 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3569 mp->reqid, mp->new_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003570 &mp->new_saddr, &mp->new_daddr) < 0)
3571 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003572 }
3573
3574 /* broadcast migrate message to sockets */
David Ahernba51b6b2015-08-24 15:17:17 -06003575 pfkey_broadcast(skb, BROADCAST_ALL, NULL, &init_net);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003576
3577 return 0;
Patrick McHardyd4782c32008-01-20 17:24:29 -08003578
3579err:
3580 kfree_skb(skb);
3581 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003582}
3583#else
David S. Miller183cad12011-02-24 00:28:01 -05003584static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3585 const struct xfrm_migrate *m, int num_bundles,
3586 const struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003587{
3588 return -ENOPROTOOPT;
3589}
3590#endif
3591
Ying Xue1b784142015-03-02 15:37:48 +08003592static int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
3594 struct sock *sk = sock->sk;
3595 struct sk_buff *skb = NULL;
3596 struct sadb_msg *hdr = NULL;
3597 int err;
Fan Du283bc9f2013-11-07 17:47:50 +08003598 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
3600 err = -EOPNOTSUPP;
3601 if (msg->msg_flags & MSG_OOB)
3602 goto out;
3603
3604 err = -EMSGSIZE;
Eric Dumazet95c96172012-04-15 05:58:06 +00003605 if ((unsigned int)len > sk->sk_sndbuf - 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 goto out;
3607
3608 err = -ENOBUFS;
3609 skb = alloc_skb(len, GFP_KERNEL);
3610 if (skb == NULL)
3611 goto out;
3612
3613 err = -EFAULT;
Al Viro6ce8e9c2014-04-06 21:25:44 -04003614 if (memcpy_from_msg(skb_put(skb,len), msg, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 goto out;
3616
3617 hdr = pfkey_get_base_msg(skb, &err);
3618 if (!hdr)
3619 goto out;
3620
Fan Du283bc9f2013-11-07 17:47:50 +08003621 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 err = pfkey_process(sk, skb, hdr);
Fan Du283bc9f2013-11-07 17:47:50 +08003623 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
3625out:
3626 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3627 err = 0;
Wei Yongjun6f961062009-02-25 00:31:04 +00003628 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
3630 return err ? : len;
3631}
3632
Ying Xue1b784142015-03-02 15:37:48 +08003633static int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 int flags)
3635{
3636 struct sock *sk = sock->sk;
Timo Teras83321d62008-03-03 23:40:12 -08003637 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 struct sk_buff *skb;
3639 int copied, err;
3640
3641 err = -EINVAL;
3642 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3643 goto out;
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3646 if (skb == NULL)
3647 goto out;
3648
3649 copied = skb->len;
3650 if (copied > len) {
3651 msg->msg_flags |= MSG_TRUNC;
3652 copied = len;
3653 }
3654
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003655 skb_reset_transport_header(skb);
David S. Miller51f3d022014-11-05 16:46:40 -05003656 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 if (err)
3658 goto out_free;
3659
Neil Horman3b885782009-10-12 13:26:31 -07003660 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
3662 err = (flags & MSG_TRUNC) ? skb->len : copied;
3663
Timo Teras83321d62008-03-03 23:40:12 -08003664 if (pfk->dump.dump != NULL &&
3665 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3666 pfkey_do_dump(pfk);
3667
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668out_free:
3669 skb_free_datagram(sk, skb);
3670out:
3671 return err;
3672}
3673
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003674static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 .family = PF_KEY,
3676 .owner = THIS_MODULE,
3677 /* Operations that make no sense on pfkey sockets. */
3678 .bind = sock_no_bind,
3679 .connect = sock_no_connect,
3680 .socketpair = sock_no_socketpair,
3681 .accept = sock_no_accept,
3682 .getname = sock_no_getname,
3683 .ioctl = sock_no_ioctl,
3684 .listen = sock_no_listen,
3685 .shutdown = sock_no_shutdown,
3686 .setsockopt = sock_no_setsockopt,
3687 .getsockopt = sock_no_getsockopt,
3688 .mmap = sock_no_mmap,
3689 .sendpage = sock_no_sendpage,
3690
3691 /* Now the operations that really occur. */
3692 .release = pfkey_release,
3693 .poll = datagram_poll,
3694 .sendmsg = pfkey_sendmsg,
3695 .recvmsg = pfkey_recvmsg,
3696};
3697
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003698static const struct net_proto_family pfkey_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 .family = PF_KEY,
3700 .create = pfkey_create,
3701 .owner = THIS_MODULE,
3702};
3703
3704#ifdef CONFIG_PROC_FS
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003705static int pfkey_seq_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706{
Li Zefan27b5b862010-02-08 23:20:29 +00003707 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003709 if (v == SEQ_START_TOKEN)
3710 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3711 else
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003712 seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 s,
3714 atomic_read(&s->sk_refcnt),
Eric Dumazet31e6d362009-06-17 19:05:41 -07003715 sk_rmem_alloc_get(s),
3716 sk_wmem_alloc_get(s),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06003717 from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 sock_i_ino(s)
3719 );
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721}
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003722
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003723static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
stephen hemmingerada440e2011-02-23 09:06:50 +00003724 __acquires(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003725{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003726 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003727 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003728
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003729 rcu_read_lock();
3730 return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003731}
3732
3733static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3734{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003735 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003736 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3737
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003738 return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003739}
3740
3741static void pfkey_seq_stop(struct seq_file *f, void *v)
stephen hemmingerada440e2011-02-23 09:06:50 +00003742 __releases(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003743{
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003744 rcu_read_unlock();
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003745}
3746
Stephen Hemminger98147d52009-09-01 19:25:02 +00003747static const struct seq_operations pfkey_seq_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003748 .start = pfkey_seq_start,
3749 .next = pfkey_seq_next,
3750 .stop = pfkey_seq_stop,
3751 .show = pfkey_seq_show,
3752};
3753
3754static int pfkey_seq_open(struct inode *inode, struct file *file)
3755{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003756 return seq_open_net(inode, file, &pfkey_seq_ops,
3757 sizeof(struct seq_net_private));
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003758}
3759
Stephen Hemminger5ca1b992009-09-01 19:25:05 +00003760static const struct file_operations pfkey_proc_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003761 .open = pfkey_seq_open,
3762 .read = seq_read,
3763 .llseek = seq_lseek,
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003764 .release = seq_release_net,
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003765};
3766
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003767static int __net_init pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003768{
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003769 struct proc_dir_entry *e;
3770
Gao fengd4beaa62013-02-18 01:34:54 +00003771 e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003772 if (e == NULL)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003773 return -ENOMEM;
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003774
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003775 return 0;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003776}
3777
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003778static void __net_exit pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003779{
Gao fengece31ff2013-02-18 01:34:56 +00003780 remove_proc_entry("pfkey", net->proc_net);
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003781}
3782#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003783static inline int pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003784{
3785 return 0;
3786}
3787
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003788static inline void pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003789{
3790}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791#endif
3792
3793static struct xfrm_mgr pfkeyv2_mgr =
3794{
3795 .id = "pfkeyv2",
3796 .notify = pfkey_send_notify,
3797 .acquire = pfkey_send_acquire,
3798 .compile_policy = pfkey_compile_policy,
3799 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003800 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003801 .migrate = pfkey_send_migrate,
Horia Geanta0f245582014-02-12 16:20:06 +02003802 .is_alive = pfkey_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803};
3804
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003805static int __net_init pfkey_net_init(struct net *net)
3806{
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003807 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003808 int rv;
3809
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003810 INIT_HLIST_HEAD(&net_pfkey->table);
3811 atomic_set(&net_pfkey->socks_nr, 0);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003812
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003813 rv = pfkey_init_proc(net);
3814
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003815 return rv;
3816}
3817
3818static void __net_exit pfkey_net_exit(struct net *net)
3819{
3820 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3821
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003822 pfkey_exit_proc(net);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003823 BUG_ON(!hlist_empty(&net_pfkey->table));
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003824}
3825
3826static struct pernet_operations pfkey_net_ops = {
3827 .init = pfkey_net_init,
3828 .exit = pfkey_net_exit,
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003829 .id = &pfkey_net_id,
3830 .size = sizeof(struct netns_pfkey),
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003831};
3832
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833static void __exit ipsec_pfkey_exit(void)
3834{
3835 xfrm_unregister_km(&pfkeyv2_mgr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003837 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 proto_unregister(&key_proto);
3839}
3840
3841static int __init ipsec_pfkey_init(void)
3842{
3843 int err = proto_register(&key_proto, 0);
3844
3845 if (err != 0)
3846 goto out;
3847
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003848 err = register_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 if (err != 0)
3850 goto out_unregister_key_proto;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003851 err = sock_register(&pfkey_family_ops);
3852 if (err != 0)
3853 goto out_unregister_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 err = xfrm_register_km(&pfkeyv2_mgr);
3855 if (err != 0)
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003856 goto out_sock_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857out:
3858 return err;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003859
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860out_sock_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003862out_unregister_pernet:
3863 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864out_unregister_key_proto:
3865 proto_unregister(&key_proto);
3866 goto out;
3867}
3868
3869module_init(ipsec_pfkey_init);
3870module_exit(ipsec_pfkey_exit);
3871MODULE_LICENSE("GPL");
3872MODULE_ALIAS_NETPROTO(PF_KEY);