blob: e72589a8400dbf7e35a68468149bc7ce601b48be [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;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700152 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto);
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);
208 sk->sk_data_ready(sk, (*skb2)->len);
209 *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
Al Virodd0fc662005-10-07 07:46:04 +0100222static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
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)
247 pfkey_broadcast_one(skb, &skb2, allocation, sk);
248
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
262 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
263
264 /* Error is cleare after succecful sending to at least one
265 * 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)
272 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
273
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;
295 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, 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
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -0800336 pfkey_broadcast(skb, GFP_KERNEL, 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;
408 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 return 0;
412}
413
David S. Miller4c93fbb2011-02-25 18:07:06 -0800414static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800415{
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700416 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
417 sec_ctx->sadb_x_ctx_len,
418 sizeof(uint64_t));
Trent Jaegerdf718372005-12-13 23:12:27 -0800419}
420
David S. Miller4c93fbb2011-02-25 18:07:06 -0800421static inline int verify_sec_ctx_len(const void *p)
Trent Jaegerdf718372005-12-13 23:12:27 -0800422{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800423 const struct sadb_x_sec_ctx *sec_ctx = p;
Stephen Rothwell298bb622007-10-30 23:57:05 -0700424 int len = sec_ctx->sadb_x_ctx_len;
Trent Jaegerdf718372005-12-13 23:12:27 -0800425
Stephen Rothwell298bb622007-10-30 23:57:05 -0700426 if (len > PAGE_SIZE)
Trent Jaegerdf718372005-12-13 23:12:27 -0800427 return -EINVAL;
428
429 len = pfkey_sec_ctx_len(sec_ctx);
430
431 if (sec_ctx->sadb_x_sec_len != len)
432 return -EINVAL;
433
434 return 0;
435}
436
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +0100437static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx,
438 gfp_t gfp)
Trent Jaegerdf718372005-12-13 23:12:27 -0800439{
440 struct xfrm_user_sec_ctx *uctx = NULL;
441 int ctx_size = sec_ctx->sadb_x_ctx_len;
442
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +0100443 uctx = kmalloc((sizeof(*uctx)+ctx_size), gfp);
Trent Jaegerdf718372005-12-13 23:12:27 -0800444
445 if (!uctx)
446 return NULL;
447
448 uctx->len = pfkey_sec_ctx_len(sec_ctx);
449 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
450 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
451 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
452 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
453 memcpy(uctx + 1, sec_ctx + 1,
454 uctx->ctx_len);
455
456 return uctx;
457}
458
David S. Miller4c93fbb2011-02-25 18:07:06 -0800459static int present_and_same_family(const struct sadb_address *src,
460 const struct sadb_address *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800462 const struct sockaddr *s_addr, *d_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 if (!src || !dst)
465 return 0;
466
David S. Miller4c93fbb2011-02-25 18:07:06 -0800467 s_addr = (const struct sockaddr *)(src + 1);
468 d_addr = (const struct sockaddr *)(dst + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (s_addr->sa_family != d_addr->sa_family)
470 return 0;
471 if (s_addr->sa_family != AF_INET
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000472#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 && s_addr->sa_family != AF_INET6
474#endif
475 )
476 return 0;
477
478 return 1;
479}
480
David S. Miller4c93fbb2011-02-25 18:07:06 -0800481static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800483 const char *p = (char *) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int len = skb->len;
485
486 len -= sizeof(*hdr);
487 p += sizeof(*hdr);
488 while (len > 0) {
David S. Miller4c93fbb2011-02-25 18:07:06 -0800489 const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 uint16_t ext_type;
491 int ext_len;
492
493 ext_len = ehdr->sadb_ext_len;
494 ext_len *= sizeof(uint64_t);
495 ext_type = ehdr->sadb_ext_type;
496 if (ext_len < sizeof(uint64_t) ||
497 ext_len > len ||
498 ext_type == SADB_EXT_RESERVED)
499 return -EINVAL;
500
501 if (ext_type <= SADB_EXT_MAX) {
502 int min = (int) sadb_ext_min_len[ext_type];
503 if (ext_len < min)
504 return -EINVAL;
505 if (ext_hdrs[ext_type-1] != NULL)
506 return -EINVAL;
507 if (ext_type == SADB_EXT_ADDRESS_SRC ||
508 ext_type == SADB_EXT_ADDRESS_DST ||
509 ext_type == SADB_EXT_ADDRESS_PROXY ||
510 ext_type == SADB_X_EXT_NAT_T_OA) {
511 if (verify_address_len(p))
512 return -EINVAL;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900513 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800514 if (ext_type == SADB_X_EXT_SEC_CTX) {
515 if (verify_sec_ctx_len(p))
516 return -EINVAL;
517 }
David S. Miller4c93fbb2011-02-25 18:07:06 -0800518 ext_hdrs[ext_type-1] = (void *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 p += ext_len;
521 len -= ext_len;
522 }
523
524 return 0;
525}
526
527static uint16_t
528pfkey_satype2proto(uint8_t satype)
529{
530 switch (satype) {
531 case SADB_SATYPE_UNSPEC:
532 return IPSEC_PROTO_ANY;
533 case SADB_SATYPE_AH:
534 return IPPROTO_AH;
535 case SADB_SATYPE_ESP:
536 return IPPROTO_ESP;
537 case SADB_X_SATYPE_IPCOMP:
538 return IPPROTO_COMP;
539 break;
540 default:
541 return 0;
542 }
543 /* NOTREACHED */
544}
545
546static uint8_t
547pfkey_proto2satype(uint16_t proto)
548{
549 switch (proto) {
550 case IPPROTO_AH:
551 return SADB_SATYPE_AH;
552 case IPPROTO_ESP:
553 return SADB_SATYPE_ESP;
554 case IPPROTO_COMP:
555 return SADB_X_SATYPE_IPCOMP;
556 break;
557 default:
558 return 0;
559 }
560 /* NOTREACHED */
561}
562
563/* BTW, this scheme means that there is no way with PFKEY2 sockets to
564 * say specifically 'just raw sockets' as we encode them as 255.
565 */
566
567static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
568{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000569 return proto == IPSEC_PROTO_ANY ? 0 : proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
573{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000574 return proto ? proto : IPSEC_PROTO_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900577static inline int pfkey_sockaddr_len(sa_family_t family)
578{
579 switch (family) {
580 case AF_INET:
581 return sizeof(struct sockaddr_in);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000582#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900583 case AF_INET6:
584 return sizeof(struct sockaddr_in6);
585#endif
586 }
587 return 0;
588}
589
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900590static
591int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900593 switch (sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 case AF_INET:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900595 xaddr->a4 =
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900596 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return AF_INET;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000598#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 case AF_INET6:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900600 memcpy(xaddr->a6,
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900601 &((struct sockaddr_in6 *)sa)->sin6_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 sizeof(struct in6_addr));
603 return AF_INET6;
604#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900606 return 0;
607}
608
609static
David S. Miller4c93fbb2011-02-25 18:07:06 -0800610int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900611{
612 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
613 xaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
David S. Miller4c93fbb2011-02-25 18:07:06 -0800616static 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 -0700617{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800618 const struct sadb_sa *sa;
619 const struct sadb_address *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 uint16_t proto;
621 unsigned short family;
622 xfrm_address_t *xaddr;
623
Joe Perchesea110732011-06-13 16:21:26 +0000624 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (sa == NULL)
626 return NULL;
627
628 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
629 if (proto == 0)
630 return NULL;
631
632 /* sadb_address_len should be checked by caller */
Joe Perchesea110732011-06-13 16:21:26 +0000633 addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (addr == NULL)
635 return NULL;
636
David S. Miller4c93fbb2011-02-25 18:07:06 -0800637 family = ((const struct sockaddr *)(addr + 1))->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 switch (family) {
639 case AF_INET:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800640 xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000642#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 case AF_INET6:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800644 xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646#endif
647 default:
648 xaddr = NULL;
649 }
650
651 if (!xaddr)
652 return NULL;
653
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800654 return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659static int
660pfkey_sockaddr_size(sa_family_t family)
661{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900662 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700665static inline int pfkey_mode_from_xfrm(int mode)
666{
667 switch(mode) {
668 case XFRM_MODE_TRANSPORT:
669 return IPSEC_MODE_TRANSPORT;
670 case XFRM_MODE_TUNNEL:
671 return IPSEC_MODE_TUNNEL;
672 case XFRM_MODE_BEET:
673 return IPSEC_MODE_BEET;
674 default:
675 return -1;
676 }
677}
678
679static inline int pfkey_mode_to_xfrm(int mode)
680{
681 switch(mode) {
682 case IPSEC_MODE_ANY: /*XXX*/
683 case IPSEC_MODE_TRANSPORT:
684 return XFRM_MODE_TRANSPORT;
685 case IPSEC_MODE_TUNNEL:
686 return XFRM_MODE_TUNNEL;
687 case IPSEC_MODE_BEET:
688 return XFRM_MODE_BEET;
689 default:
690 return -1;
691 }
692}
693
David S. Miller183cad12011-02-24 00:28:01 -0500694static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
David S. Miller4c93fbb2011-02-25 18:07:06 -0800695 struct sockaddr *sa,
696 unsigned short family)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900697{
698 switch (family) {
699 case AF_INET:
700 {
701 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
702 sin->sin_family = AF_INET;
703 sin->sin_port = port;
704 sin->sin_addr.s_addr = xaddr->a4;
705 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
706 return 32;
707 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000708#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900709 case AF_INET6:
710 {
711 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
712 sin6->sin6_family = AF_INET6;
713 sin6->sin6_port = port;
714 sin6->sin6_flowinfo = 0;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000715 sin6->sin6_addr = *(struct in6_addr *)xaddr->a6;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900716 sin6->sin6_scope_id = 0;
717 return 128;
718 }
719#endif
720 }
721 return 0;
722}
723
David S. Miller4c93fbb2011-02-25 18:07:06 -0800724static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -0700725 int add_keys, int hsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 struct sk_buff *skb;
728 struct sadb_msg *hdr;
729 struct sadb_sa *sa;
730 struct sadb_lifetime *lifetime;
731 struct sadb_address *addr;
732 struct sadb_key *key;
733 struct sadb_x_sa2 *sa2;
Trent Jaegerdf718372005-12-13 23:12:27 -0800734 struct sadb_x_sec_ctx *sec_ctx;
735 struct xfrm_sec_ctx *xfrm_ctx;
736 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 int size;
738 int auth_key_size = 0;
739 int encrypt_key_size = 0;
740 int sockaddr_size;
741 struct xfrm_encap_tmpl *natt = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700742 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 /* address family check */
745 sockaddr_size = pfkey_sockaddr_size(x->props.family);
746 if (!sockaddr_size)
747 return ERR_PTR(-EINVAL);
748
749 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
750 key(AE), (identity(SD),) (sensitivity)> */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900751 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 sizeof(struct sadb_lifetime) +
753 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
754 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900755 sizeof(struct sadb_address)*2 +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 sockaddr_size*2 +
757 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800758
759 if ((xfrm_ctx = x->security)) {
760 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
761 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
762 }
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /* identity & sensitivity */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000765 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 size += sizeof(struct sadb_address) + sockaddr_size;
767
768 if (add_keys) {
769 if (x->aalg && x->aalg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900770 auth_key_size =
771 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 size += sizeof(struct sadb_key) + auth_key_size;
773 }
774 if (x->ealg && x->ealg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900775 encrypt_key_size =
776 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 size += sizeof(struct sadb_key) + encrypt_key_size;
778 }
779 }
780 if (x->encap)
781 natt = x->encap;
782
783 if (natt && natt->encap_type) {
784 size += sizeof(struct sadb_x_nat_t_type);
785 size += sizeof(struct sadb_x_nat_t_port);
786 size += sizeof(struct sadb_x_nat_t_port);
787 }
788
789 skb = alloc_skb(size + 16, GFP_ATOMIC);
790 if (skb == NULL)
791 return ERR_PTR(-ENOBUFS);
792
793 /* call should fill header later */
794 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
795 memset(hdr, 0, size); /* XXX do we need this ? */
796 hdr->sadb_msg_len = size / sizeof(uint64_t);
797
798 /* sa */
799 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
800 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
801 sa->sadb_sa_exttype = SADB_EXT_SA;
802 sa->sadb_sa_spi = x->id.spi;
803 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700804 switch (x->km.state) {
805 case XFRM_STATE_VALID:
806 sa->sadb_sa_state = x->km.dying ?
807 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
808 break;
809 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700811 break;
812 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700814 break;
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 sa->sadb_sa_auth = 0;
817 if (x->aalg) {
818 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200819 sa->sadb_sa_auth = (a && a->pfkey_supported) ?
820 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822 sa->sadb_sa_encrypt = 0;
823 BUG_ON(x->ealg && x->calg);
824 if (x->ealg) {
825 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200826 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
827 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
830 if (x->calg) {
831 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200832 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
833 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
836 sa->sadb_sa_flags = 0;
837 if (x->props.flags & XFRM_STATE_NOECN)
838 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
839 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
840 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700841 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
842 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /* hard time */
845 if (hsc & 2) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900846 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 sizeof(struct sadb_lifetime));
848 lifetime->sadb_lifetime_len =
849 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
850 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
851 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
852 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
853 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
854 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
855 }
856 /* soft time */
857 if (hsc & 1) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900858 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 sizeof(struct sadb_lifetime));
860 lifetime->sadb_lifetime_len =
861 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
862 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
863 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
864 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
865 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
866 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
867 }
868 /* current time */
869 lifetime = (struct sadb_lifetime *) skb_put(skb,
870 sizeof(struct sadb_lifetime));
871 lifetime->sadb_lifetime_len =
872 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
873 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
874 lifetime->sadb_lifetime_allocations = x->curlft.packets;
875 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
876 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
877 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
878 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900879 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900881 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 (sizeof(struct sadb_address)+sockaddr_size)/
883 sizeof(uint64_t);
884 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900885 /* "if the ports are non-zero, then the sadb_address_proto field,
886 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900888 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900891 addr->sadb_address_prefixlen =
892 pfkey_sockaddr_fill(&x->props.saddr, 0,
893 (struct sockaddr *) (addr + 1),
894 x->props.family);
895 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 BUG();
897
898 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900899 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900901 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 (sizeof(struct sadb_address)+sockaddr_size)/
903 sizeof(uint64_t);
904 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900905 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900908 addr->sadb_address_prefixlen =
909 pfkey_sockaddr_fill(&x->id.daddr, 0,
910 (struct sockaddr *) (addr + 1),
911 x->props.family);
912 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 BUG();
914
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000915 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
916 x->props.family)) {
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900917 addr = (struct sadb_address*) skb_put(skb,
918 sizeof(struct sadb_address)+sockaddr_size);
919 addr->sadb_address_len =
920 (sizeof(struct sadb_address)+sockaddr_size)/
921 sizeof(uint64_t);
922 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
923 addr->sadb_address_proto =
924 pfkey_proto_from_xfrm(x->sel.proto);
925 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
926 addr->sadb_address_reserved = 0;
927
928 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
929 (struct sockaddr *) (addr + 1),
930 x->props.family);
931 }
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* auth key */
934 if (add_keys && auth_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900935 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 sizeof(struct sadb_key)+auth_key_size);
937 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
938 sizeof(uint64_t);
939 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
940 key->sadb_key_bits = x->aalg->alg_key_len;
941 key->sadb_key_reserved = 0;
942 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
943 }
944 /* encrypt key */
945 if (add_keys && encrypt_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900946 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 sizeof(struct sadb_key)+encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900948 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 encrypt_key_size) / sizeof(uint64_t);
950 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
951 key->sadb_key_bits = x->ealg->alg_key_len;
952 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900953 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 (x->ealg->alg_key_len+7)/8);
955 }
956
957 /* sa */
958 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
959 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
960 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700961 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
962 kfree_skb(skb);
963 return ERR_PTR(-EINVAL);
964 }
965 sa2->sadb_x_sa2_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 sa2->sadb_x_sa2_reserved1 = 0;
967 sa2->sadb_x_sa2_reserved2 = 0;
968 sa2->sadb_x_sa2_sequence = 0;
969 sa2->sadb_x_sa2_reqid = x->props.reqid;
970
971 if (natt && natt->encap_type) {
972 struct sadb_x_nat_t_type *n_type;
973 struct sadb_x_nat_t_port *n_port;
974
975 /* type */
976 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
977 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
978 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
979 n_type->sadb_x_nat_t_type_type = natt->encap_type;
980 n_type->sadb_x_nat_t_type_reserved[0] = 0;
981 n_type->sadb_x_nat_t_type_reserved[1] = 0;
982 n_type->sadb_x_nat_t_type_reserved[2] = 0;
983
984 /* source port */
985 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
986 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
987 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
988 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
989 n_port->sadb_x_nat_t_port_reserved = 0;
990
991 /* dest port */
992 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
993 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
994 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
995 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
996 n_port->sadb_x_nat_t_port_reserved = 0;
997 }
998
Trent Jaegerdf718372005-12-13 23:12:27 -0800999 /* security context */
1000 if (xfrm_ctx) {
1001 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1002 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1003 sec_ctx->sadb_x_sec_len =
1004 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1005 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1006 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1007 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1008 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1009 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1010 xfrm_ctx->ctx_len);
1011 }
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return skb;
1014}
1015
Herbert Xu050f0092007-10-09 13:31:47 -07001016
David S. Miller4c93fbb2011-02-25 18:07:06 -08001017static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
Herbert Xu050f0092007-10-09 13:31:47 -07001018{
1019 struct sk_buff *skb;
1020
Herbert Xu050f0092007-10-09 13:31:47 -07001021 skb = __pfkey_xfrm_state2msg(x, 1, 3);
Herbert Xu050f0092007-10-09 13:31:47 -07001022
1023 return skb;
1024}
1025
David S. Miller4c93fbb2011-02-25 18:07:06 -08001026static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -07001027 int hsc)
1028{
1029 return __pfkey_xfrm_state2msg(x, 0, hsc);
1030}
1031
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001032static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
David S. Miller4c93fbb2011-02-25 18:07:06 -08001033 const struct sadb_msg *hdr,
1034 void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001036 struct xfrm_state *x;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001037 const struct sadb_lifetime *lifetime;
1038 const struct sadb_sa *sa;
1039 const struct sadb_key *key;
1040 const struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 uint16_t proto;
1042 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Joe Perchesea110732011-06-13 16:21:26 +00001045 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (!sa ||
1047 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1048 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1049 return ERR_PTR(-EINVAL);
1050 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1051 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1052 return ERR_PTR(-EINVAL);
1053 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1054 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1055 return ERR_PTR(-EINVAL);
1056 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1057 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1058 return ERR_PTR(-EINVAL);
1059
1060 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1061 if (proto == 0)
1062 return ERR_PTR(-EINVAL);
1063
1064 /* default error is no buffer space */
1065 err = -ENOBUFS;
1066
1067 /* RFC2367:
1068
1069 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1070 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1071 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1072 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1073 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1074 not true.
1075
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001076 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1078 */
1079 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1080 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1081 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1082 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1083 return ERR_PTR(-EINVAL);
Joe Perchesea110732011-06-13 16:21:26 +00001084 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (key != NULL &&
1086 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1087 ((key->sadb_key_bits+7) / 8 == 0 ||
1088 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1089 return ERR_PTR(-EINVAL);
1090 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1091 if (key != NULL &&
1092 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1093 ((key->sadb_key_bits+7) / 8 == 0 ||
1094 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1095 return ERR_PTR(-EINVAL);
1096
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001097 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (x == NULL)
1099 return ERR_PTR(-ENOBUFS);
1100
1101 x->id.proto = proto;
1102 x->id.spi = sa->sadb_sa_spi;
Fan Du33fce602013-09-17 15:14:13 +08001103 x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay,
1104 (sizeof(x->replay.bitmap) * 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1106 x->props.flags |= XFRM_STATE_NOECN;
1107 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1108 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001109 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1110 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Joe Perchesea110732011-06-13 16:21:26 +00001112 lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (lifetime != NULL) {
1114 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1115 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1116 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1117 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1118 }
Joe Perchesea110732011-06-13 16:21:26 +00001119 lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (lifetime != NULL) {
1121 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1122 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1123 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1124 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1125 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001126
Joe Perchesea110732011-06-13 16:21:26 +00001127 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001128 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01001129 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001130
1131 if (!uctx)
1132 goto out;
1133
1134 err = security_xfrm_state_alloc(x, uctx);
1135 kfree(uctx);
1136
1137 if (err)
1138 goto out;
1139 }
1140
Joe Perchesea110732011-06-13 16:21:26 +00001141 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (sa->sadb_sa_auth) {
1143 int keysize = 0;
1144 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001145 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 err = -ENOSYS;
1147 goto out;
1148 }
1149 if (key)
1150 keysize = (key->sadb_key_bits + 7) / 8;
1151 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1152 if (!x->aalg)
1153 goto out;
1154 strcpy(x->aalg->alg_name, a->name);
1155 x->aalg->alg_key_len = 0;
1156 if (key) {
1157 x->aalg->alg_key_len = key->sadb_key_bits;
1158 memcpy(x->aalg->alg_key, key+1, keysize);
1159 }
Martin Willic20a66f2009-12-09 06:11:15 +00001160 x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 x->props.aalgo = sa->sadb_sa_auth;
1162 /* x->algo.flags = sa->sadb_sa_flags; */
1163 }
1164 if (sa->sadb_sa_encrypt) {
1165 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1166 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001167 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 err = -ENOSYS;
1169 goto out;
1170 }
1171 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1172 if (!x->calg)
1173 goto out;
1174 strcpy(x->calg->alg_name, a->name);
1175 x->props.calgo = sa->sadb_sa_encrypt;
1176 } else {
1177 int keysize = 0;
1178 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001179 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 err = -ENOSYS;
1181 goto out;
1182 }
1183 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1184 if (key)
1185 keysize = (key->sadb_key_bits + 7) / 8;
1186 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1187 if (!x->ealg)
1188 goto out;
1189 strcpy(x->ealg->alg_name, a->name);
1190 x->ealg->alg_key_len = 0;
1191 if (key) {
1192 x->ealg->alg_key_len = key->sadb_key_bits;
1193 memcpy(x->ealg->alg_key, key+1, keysize);
1194 }
1195 x->props.ealgo = sa->sadb_sa_encrypt;
1196 }
1197 }
1198 /* x->algo.flags = sa->sadb_sa_flags; */
1199
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001200 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 &x->props.saddr);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001202 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 &x->id.daddr);
1204
1205 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001206 const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001207 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1208 if (mode < 0) {
1209 err = -EINVAL;
1210 goto out;
1211 }
1212 x->props.mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 x->props.reqid = sa2->sadb_x_sa2_reqid;
1214 }
1215
1216 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001217 const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /* Nobody uses this, but we try. */
1220 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1221 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1222 }
1223
Kazunori MIYAZAWA4da51052008-05-21 13:26:11 -07001224 if (!x->sel.family)
Joy Latten4a4b6272007-08-02 19:25:43 -07001225 x->sel.family = x->props.family;
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001228 const struct sadb_x_nat_t_type* n_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 struct xfrm_encap_tmpl *natt;
1230
1231 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1232 if (!x->encap)
1233 goto out;
1234
1235 natt = x->encap;
1236 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1237 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1238
1239 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001240 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1242 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1243 }
1244 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001245 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1247 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1248 }
Timo Terasa8d694c2009-01-25 20:49:14 -08001249 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251
Herbert Xu72cb6962005-06-20 13:18:08 -07001252 err = xfrm_init_state(x);
1253 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return x;
1258
1259out:
1260 x->km.state = XFRM_STATE_DEAD;
1261 xfrm_state_put(x);
1262 return ERR_PTR(err);
1263}
1264
David S. Miller4c93fbb2011-02-25 18:07:06 -08001265static 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 -07001266{
1267 return -EOPNOTSUPP;
1268}
1269
David S. Miller4c93fbb2011-02-25 18:07:06 -08001270static 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 -07001271{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001272 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct sk_buff *resp_skb;
1274 struct sadb_x_sa2 *sa2;
1275 struct sadb_address *saddr, *daddr;
1276 struct sadb_msg *out_hdr;
Herbert Xu658b2192007-10-09 13:29:52 -07001277 struct sadb_spirange *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 struct xfrm_state *x = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001279 int mode;
Herbert Xu658b2192007-10-09 13:29:52 -07001280 int err;
1281 u32 min_spi, max_spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 u32 reqid;
1283 u8 proto;
1284 unsigned short family;
1285 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1286
1287 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1288 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1289 return -EINVAL;
1290
1291 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1292 if (proto == 0)
1293 return -EINVAL;
1294
1295 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001296 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1297 if (mode < 0)
1298 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 reqid = sa2->sadb_x_sa2_reqid;
1300 } else {
1301 mode = 0;
1302 reqid = 0;
1303 }
1304
1305 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1306 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1307
1308 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1309 switch (family) {
1310 case AF_INET:
1311 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1312 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1313 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001314#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 case AF_INET6:
1316 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1317 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1318 break;
1319#endif
1320 }
1321
1322 if (hdr->sadb_msg_seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001323 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001324 if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 xfrm_state_put(x);
1326 x = NULL;
1327 }
1328 }
1329
1330 if (!x)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001331 x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 if (x == NULL)
1334 return -ENOENT;
1335
Herbert Xu658b2192007-10-09 13:29:52 -07001336 min_spi = 0x100;
1337 max_spi = 0x0fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Herbert Xu658b2192007-10-09 13:29:52 -07001339 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1340 if (range) {
1341 min_spi = range->sadb_spirange_min;
1342 max_spi = range->sadb_spirange_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
Herbert Xu658b2192007-10-09 13:29:52 -07001344
Fan Du776e9dd2013-12-16 18:47:49 +08001345 err = verify_spi_info(x->id.proto, min_spi, max_spi);
1346 if (err) {
1347 xfrm_state_put(x);
1348 return err;
1349 }
1350
Herbert Xu658b2192007-10-09 13:29:52 -07001351 err = xfrm_alloc_spi(x, min_spi, max_spi);
Herbert Xu050f0092007-10-09 13:31:47 -07001352 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 if (IS_ERR(resp_skb)) {
1355 xfrm_state_put(x);
1356 return PTR_ERR(resp_skb);
1357 }
1358
1359 out_hdr = (struct sadb_msg *) resp_skb->data;
1360 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1361 out_hdr->sadb_msg_type = SADB_GETSPI;
1362 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1363 out_hdr->sadb_msg_errno = 0;
1364 out_hdr->sadb_msg_reserved = 0;
1365 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1366 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1367
1368 xfrm_state_put(x);
1369
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001370 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 return 0;
1373}
1374
David S. Miller4c93fbb2011-02-25 18:07:06 -08001375static 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 -07001376{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001377 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 struct xfrm_state *x;
1379
1380 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1381 return -EOPNOTSUPP;
1382
1383 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1384 return 0;
1385
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001386 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (x == NULL)
1388 return 0;
1389
1390 spin_lock_bh(&x->lock);
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001391 if (x->km.state == XFRM_STATE_ACQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 x->km.state = XFRM_STATE_ERROR;
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 spin_unlock_bh(&x->lock);
1395 xfrm_state_put(x);
1396 return 0;
1397}
1398
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001399static inline int event2poltype(int event)
1400{
1401 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001402 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001403 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001404 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001405 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001406 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001407 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001408 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001409 // return SADB_X_SPDEXPIRE;
1410 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001411 pr_err("pfkey: Unknown policy event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001412 break;
1413 }
1414
1415 return 0;
1416}
1417
1418static inline int event2keytype(int event)
1419{
1420 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001421 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001422 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001423 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001424 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001425 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001426 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001427 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001428 return SADB_EXPIRE;
1429 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001430 pr_err("pfkey: Unknown SA event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001431 break;
1432 }
1433
1434 return 0;
1435}
1436
1437/* ADD/UPD/DEL */
David S. Miller214e0052011-02-24 00:02:38 -05001438static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001439{
1440 struct sk_buff *skb;
1441 struct sadb_msg *hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001442
Herbert Xu050f0092007-10-09 13:31:47 -07001443 skb = pfkey_xfrm_state2msg(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001444
1445 if (IS_ERR(skb))
1446 return PTR_ERR(skb);
1447
1448 hdr = (struct sadb_msg *) skb->data;
1449 hdr->sadb_msg_version = PF_KEY_V2;
1450 hdr->sadb_msg_type = event2keytype(c->event);
1451 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1452 hdr->sadb_msg_errno = 0;
1453 hdr->sadb_msg_reserved = 0;
1454 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001455 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001456
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001457 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001458
1459 return 0;
1460}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
David S. Miller4c93fbb2011-02-25 18:07:06 -08001462static 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 -07001463{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001464 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 struct xfrm_state *x;
1466 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001467 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001469 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (IS_ERR(x))
1471 return PTR_ERR(x);
1472
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001473 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (hdr->sadb_msg_type == SADB_ADD)
1475 err = xfrm_state_add(x);
1476 else
1477 err = xfrm_state_update(x);
1478
Joy Lattenab5f5e82007-09-17 11:51:22 -07001479 xfrm_audit_state_add(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001480 audit_get_loginuid(current),
1481 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (err < 0) {
1484 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001485 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001486 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001489 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001490 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001491 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001492 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001493 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001494 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001495 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001496out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001497 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001498 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
David S. Miller4c93fbb2011-02-25 18:07:06 -08001501static 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 -07001502{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001503 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001505 struct km_event c;
1506 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 if (!ext_hdrs[SADB_EXT_SA-1] ||
1509 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1510 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1511 return -EINVAL;
1512
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001513 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (x == NULL)
1515 return -ESRCH;
1516
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001517 if ((err = security_xfrm_state_delete(x)))
1518 goto out;
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001521 err = -EPERM;
1522 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
Joy Latten161a09e2006-11-27 13:11:54 -06001524
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001525 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001526
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001527 if (err < 0)
1528 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001529
1530 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001531 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001532 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001533 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001534out:
Joy Lattenab5f5e82007-09-17 11:51:22 -07001535 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001536 audit_get_loginuid(current),
1537 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 xfrm_state_put(x);
1539
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001540 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
David S. Miller4c93fbb2011-02-25 18:07:06 -08001543static 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 -07001544{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001545 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 __u8 proto;
1547 struct sk_buff *out_skb;
1548 struct sadb_msg *out_hdr;
1549 struct xfrm_state *x;
1550
1551 if (!ext_hdrs[SADB_EXT_SA-1] ||
1552 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1553 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1554 return -EINVAL;
1555
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001556 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (x == NULL)
1558 return -ESRCH;
1559
Herbert Xu050f0092007-10-09 13:31:47 -07001560 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 proto = x->id.proto;
1562 xfrm_state_put(x);
1563 if (IS_ERR(out_skb))
1564 return PTR_ERR(out_skb);
1565
1566 out_hdr = (struct sadb_msg *) out_skb->data;
1567 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Charles Hardin435000b2007-11-22 19:35:15 +08001568 out_hdr->sadb_msg_type = SADB_GET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1570 out_hdr->sadb_msg_errno = 0;
1571 out_hdr->sadb_msg_reserved = 0;
1572 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1573 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001574 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 return 0;
1577}
1578
David S. Miller4c93fbb2011-02-25 18:07:06 -08001579static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001580 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 struct sk_buff *skb;
1583 struct sadb_msg *hdr;
1584 int len, auth_len, enc_len, i;
1585
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001586 auth_len = xfrm_count_pfkey_auth_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (auth_len) {
1588 auth_len *= sizeof(struct sadb_alg);
1589 auth_len += sizeof(struct sadb_supported);
1590 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001591
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001592 enc_len = xfrm_count_pfkey_enc_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (enc_len) {
1594 enc_len *= sizeof(struct sadb_alg);
1595 enc_len += sizeof(struct sadb_supported);
1596 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 len = enc_len + auth_len + sizeof(struct sadb_msg);
1599
1600 skb = alloc_skb(len + 16, allocation);
1601 if (!skb)
1602 goto out_put_algs;
1603
1604 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1605 pfkey_hdr_dup(hdr, orig);
1606 hdr->sadb_msg_errno = 0;
1607 hdr->sadb_msg_len = len / sizeof(uint64_t);
1608
1609 if (auth_len) {
1610 struct sadb_supported *sp;
1611 struct sadb_alg *ap;
1612
1613 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1614 ap = (struct sadb_alg *) (sp + 1);
1615
1616 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1617 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1618
1619 for (i = 0; ; i++) {
1620 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1621 if (!aalg)
1622 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001623 if (!aalg->pfkey_supported)
1624 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 if (aalg->available)
1626 *ap++ = aalg->desc;
1627 }
1628 }
1629
1630 if (enc_len) {
1631 struct sadb_supported *sp;
1632 struct sadb_alg *ap;
1633
1634 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1635 ap = (struct sadb_alg *) (sp + 1);
1636
1637 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1638 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1639
1640 for (i = 0; ; i++) {
1641 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1642 if (!ealg)
1643 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001644 if (!ealg->pfkey_supported)
1645 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if (ealg->available)
1647 *ap++ = ealg->desc;
1648 }
1649 }
1650
1651out_put_algs:
1652 return skb;
1653}
1654
David S. Miller4c93fbb2011-02-25 18:07:06 -08001655static 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 -07001656{
1657 struct pfkey_sock *pfk = pfkey_sk(sk);
1658 struct sk_buff *supp_skb;
1659
1660 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1661 return -EINVAL;
1662
1663 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1664 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1665 return -EEXIST;
1666 pfk->registered |= (1<<hdr->sadb_msg_satype);
1667 }
1668
1669 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1672 if (!supp_skb) {
1673 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1674 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1675
1676 return -ENOBUFS;
1677 }
1678
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001679 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 return 0;
1682}
1683
David S. Miller4c93fbb2011-02-25 18:07:06 -08001684static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001685{
1686 struct sk_buff *skb;
1687 struct sadb_msg *hdr;
1688
1689 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1690 if (!skb)
1691 return -ENOBUFS;
1692
1693 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1694 memcpy(hdr, ihdr, sizeof(struct sadb_msg));
1695 hdr->sadb_msg_errno = (uint8_t) 0;
1696 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1697
1698 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
1699}
1700
David S. Miller214e0052011-02-24 00:02:38 -05001701static int key_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001702{
1703 struct sk_buff *skb;
1704 struct sadb_msg *hdr;
1705
1706 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1707 if (!skb)
1708 return -ENOBUFS;
1709 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf088672005-06-18 22:44:00 -07001710 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001711 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001712 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001713 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001714 hdr->sadb_msg_version = PF_KEY_V2;
1715 hdr->sadb_msg_errno = (uint8_t) 0;
1716 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02001717 hdr->sadb_msg_reserved = 0;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001718
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001719 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001720
1721 return 0;
1722}
1723
David S. Miller4c93fbb2011-02-25 18:07:06 -08001724static 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 -07001725{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001726 struct net *net = sock_net(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +00001727 unsigned int proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001728 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06001729 struct xfrm_audit audit_info;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001730 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1733 if (proto == 0)
1734 return -EINVAL;
1735
Al Viro0c11b942008-01-10 04:20:52 -05001736 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04001737 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06001738 audit_info.secid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001739 err = xfrm_state_flush(net, proto, &audit_info);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001740 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001741 if (err || err2) {
1742 if (err == -ESRCH) /* empty table - go quietly */
1743 err = 0;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001744 return err ? err : err2;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001745 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001746
Herbert Xubf088672005-06-18 22:44:00 -07001747 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001748 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001749 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001750 c.event = XFRM_MSG_FLUSHSA;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001751 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001752 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 return 0;
1755}
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1758{
Timo Teras83321d62008-03-03 23:40:12 -08001759 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 struct sk_buff *out_skb;
1761 struct sadb_msg *out_hdr;
1762
Timo Teras83321d62008-03-03 23:40:12 -08001763 if (!pfkey_can_dump(&pfk->sk))
1764 return -ENOBUFS;
1765
Herbert Xu050f0092007-10-09 13:31:47 -07001766 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (IS_ERR(out_skb))
1768 return PTR_ERR(out_skb);
1769
1770 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08001771 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 out_hdr->sadb_msg_type = SADB_DUMP;
1773 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1774 out_hdr->sadb_msg_errno = 0;
1775 out_hdr->sadb_msg_reserved = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07001776 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001777 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07001778
1779 if (pfk->dump.skb)
1780 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001781 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07001782 pfk->dump.skb = out_skb;
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return 0;
1785}
1786
Timo Teras83321d62008-03-03 23:40:12 -08001787static int pfkey_dump_sa(struct pfkey_sock *pfk)
1788{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001789 struct net *net = sock_net(&pfk->sk);
1790 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08001791}
1792
1793static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1794{
Fan Du283bc9f2013-11-07 17:47:50 +08001795 struct net *net = sock_net(&pfk->sk);
1796
1797 xfrm_state_walk_done(&pfk->dump.u.state, net);
Timo Teras83321d62008-03-03 23:40:12 -08001798}
1799
David S. Miller4c93fbb2011-02-25 18:07:06 -08001800static 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 -07001801{
1802 u8 proto;
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001803 struct xfrm_address_filter *filter = NULL;
Timo Teras83321d62008-03-03 23:40:12 -08001804 struct pfkey_sock *pfk = pfkey_sk(sk);
1805
1806 if (pfk->dump.dump != NULL)
1807 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1810 if (proto == 0)
1811 return -EINVAL;
1812
Nicolas Dichteld3623092014-02-14 15:30:36 +01001813 if (ext_hdrs[SADB_X_EXT_FILTER - 1]) {
1814 struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1];
1815
1816 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
1817 if (filter == NULL)
1818 return -ENOMEM;
1819
1820 memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr,
1821 sizeof(xfrm_address_t));
1822 memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr,
1823 sizeof(xfrm_address_t));
1824 filter->family = xfilter->sadb_x_filter_family;
1825 filter->splen = xfilter->sadb_x_filter_splen;
1826 filter->dplen = xfilter->sadb_x_filter_dplen;
1827 }
1828
Timo Teras83321d62008-03-03 23:40:12 -08001829 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001830 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08001831 pfk->dump.dump = pfkey_dump_sa;
1832 pfk->dump.done = pfkey_dump_sa_done;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001833 xfrm_state_walk_init(&pfk->dump.u.state, proto, filter);
Timo Teras4c563f72008-02-28 21:31:08 -08001834
Timo Teras83321d62008-03-03 23:40:12 -08001835 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
David S. Miller4c93fbb2011-02-25 18:07:06 -08001838static 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 -07001839{
1840 struct pfkey_sock *pfk = pfkey_sk(sk);
1841 int satype = hdr->sadb_msg_satype;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001842 bool reset_errno = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001845 reset_errno = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (satype != 0 && satype != 1)
1847 return -EINVAL;
1848 pfk->promisc = satype;
1849 }
David S. Miller4c93fbb2011-02-25 18:07:06 -08001850 if (reset_errno && skb_cloned(skb))
1851 skb = skb_copy(skb, GFP_KERNEL);
1852 else
1853 skb = skb_clone(skb, GFP_KERNEL);
1854
1855 if (reset_errno && skb) {
1856 struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
1857 new_hdr->sadb_msg_errno = 0;
1858 }
1859
1860 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return 0;
1862}
1863
1864static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1865{
1866 int i;
1867 u32 reqid = *(u32*)ptr;
1868
1869 for (i=0; i<xp->xfrm_nr; i++) {
1870 if (xp->xfrm_vec[i].reqid == reqid)
1871 return -EEXIST;
1872 }
1873 return 0;
1874}
1875
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001876static u32 gen_reqid(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Timo Teras4c563f72008-02-28 21:31:08 -08001878 struct xfrm_policy_walk walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 u32 start;
Timo Teras4c563f72008-02-28 21:31:08 -08001880 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1882
1883 start = reqid;
1884 do {
1885 ++reqid;
1886 if (reqid == 0)
1887 reqid = IPSEC_MANUAL_REQID_MAX+1;
Timo Teras4c563f72008-02-28 21:31:08 -08001888 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001889 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
Fan Du283bc9f2013-11-07 17:47:50 +08001890 xfrm_policy_walk_done(&walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001891 if (rc != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return reqid;
1893 } while (reqid != start);
1894 return 0;
1895}
1896
1897static int
1898parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1899{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001900 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001902 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1905 return -ELOOP;
1906
1907 if (rq->sadb_x_ipsecrequest_mode == 0)
1908 return -EINVAL;
1909
1910 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001911 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1912 return -EINVAL;
1913 t->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1915 t->optional = 1;
1916 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1917 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1918 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1919 t->reqid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001920 if (!t->reqid && !(t->reqid = gen_reqid(net)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return -ENOBUFS;
1922 }
1923
1924 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001925 if (t->mode == XFRM_MODE_TUNNEL) {
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001926 u8 *sa = (u8 *) (rq + 1);
1927 int family, socklen;
1928
1929 family = pfkey_sockaddr_extract((struct sockaddr *)sa,
1930 &t->saddr);
1931 if (!family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return -EINVAL;
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001933
1934 socklen = pfkey_sockaddr_len(family);
1935 if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen),
1936 &t->id.daddr) != family)
1937 return -EINVAL;
1938 t->encap_family = family;
Miika Komu2718aa72006-11-30 16:41:50 -08001939 } else
1940 t->encap_family = xp->family;
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 /* No way to set this via kame pfkey */
Herbert Xuc5d18e92008-04-22 00:46:42 -07001943 t->allalgs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 xp->xfrm_nr++;
1945 return 0;
1946}
1947
1948static int
1949parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1950{
1951 int err;
1952 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1953 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1954
Dan Carpenterf674e722012-09-27 22:21:19 +00001955 if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
1956 return -EINVAL;
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1959 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1960 return err;
1961 len -= rq->sadb_x_ipsecrequest_len;
1962 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1963 }
1964 return 0;
1965}
1966
David S. Miller4c93fbb2011-02-25 18:07:06 -08001967static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
Trent Jaegerdf718372005-12-13 23:12:27 -08001968{
1969 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1970
1971 if (xfrm_ctx) {
1972 int len = sizeof(struct sadb_x_sec_ctx);
1973 len += xfrm_ctx->ctx_len;
1974 return PFKEY_ALIGN8(len);
1975 }
1976 return 0;
1977}
1978
David S. Miller4c93fbb2011-02-25 18:07:06 -08001979static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
David S. Miller4c93fbb2011-02-25 18:07:06 -08001981 const struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08001983 int socklen = 0;
1984 int i;
1985
1986 for (i=0; i<xp->xfrm_nr; i++) {
1987 t = xp->xfrm_vec + i;
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09001988 socklen += pfkey_sockaddr_len(t->encap_family);
Miika Komu2718aa72006-11-30 16:41:50 -08001989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 return sizeof(struct sadb_msg) +
1992 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001993 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 (sockaddr_size * 2) +
1995 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08001996 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1997 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08001998 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
David S. Miller4c93fbb2011-02-25 18:07:06 -08002001static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 struct sk_buff *skb;
2004 int size;
2005
2006 size = pfkey_xfrm_policy2msg_size(xp);
2007
2008 skb = alloc_skb(size + 16, GFP_ATOMIC);
2009 if (skb == NULL)
2010 return ERR_PTR(-ENOBUFS);
2011
2012 return skb;
2013}
2014
David S. Miller4c93fbb2011-02-25 18:07:06 -08002015static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 struct sadb_msg *hdr;
2018 struct sadb_address *addr;
2019 struct sadb_lifetime *lifetime;
2020 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08002021 struct sadb_x_sec_ctx *sec_ctx;
2022 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 int i;
2024 int size;
2025 int sockaddr_size = pfkey_sockaddr_size(xp->family);
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002026 int socklen = pfkey_sockaddr_len(xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 size = pfkey_xfrm_policy2msg_size(xp);
2029
2030 /* call should fill header later */
2031 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2032 memset(hdr, 0, size); /* XXX do we need this ? */
2033
2034 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002035 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002037 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 (sizeof(struct sadb_address)+sockaddr_size)/
2039 sizeof(uint64_t);
2040 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2041 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2042 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2043 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002044 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2045 xp->selector.sport,
2046 (struct sockaddr *) (addr + 1),
2047 xp->family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 BUG();
2049
2050 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002051 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 sizeof(struct sadb_address)+sockaddr_size);
2053 addr->sadb_address_len =
2054 (sizeof(struct sadb_address)+sockaddr_size)/
2055 sizeof(uint64_t);
2056 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2057 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002058 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002060
2061 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2062 (struct sockaddr *) (addr + 1),
2063 xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 /* hard 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_HARD;
2071 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2072 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2073 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2074 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2075 /* soft 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_SOFT;
2081 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2082 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2083 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2084 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2085 /* current time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002086 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 sizeof(struct sadb_lifetime));
2088 lifetime->sadb_lifetime_len =
2089 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2090 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2091 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2092 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2093 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2094 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2095
2096 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2097 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2098 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2099 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2100 if (xp->action == XFRM_POLICY_ALLOW) {
2101 if (xp->xfrm_nr)
2102 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2103 else
2104 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2105 }
2106 pol->sadb_x_policy_dir = dir+1;
Dan Carpenterff862a42013-07-28 23:04:45 +03002107 pol->sadb_x_policy_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 pol->sadb_x_policy_id = xp->index;
2109 pol->sadb_x_policy_priority = xp->priority;
2110
2111 for (i=0; i<xp->xfrm_nr; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002112 const struct xfrm_tmpl *t = xp->xfrm_vec + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 struct sadb_x_ipsecrequest *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 int req_size;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002115 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 req_size = sizeof(struct sadb_x_ipsecrequest);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002118 if (t->mode == XFRM_MODE_TUNNEL) {
2119 socklen = pfkey_sockaddr_len(t->encap_family);
2120 req_size += socklen * 2;
2121 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 size -= 2*socklen;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 rq = (void*)skb_put(skb, req_size);
2125 pol->sadb_x_policy_len += req_size/8;
2126 memset(rq, 0, sizeof(*rq));
2127 rq->sadb_x_ipsecrequest_len = req_size;
2128 rq->sadb_x_ipsecrequest_proto = t->id.proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002129 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2130 return -EINVAL;
David S. Millerfefaa752007-04-17 21:48:10 -07002131 rq->sadb_x_ipsecrequest_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2133 if (t->reqid)
2134 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2135 if (t->optional)
2136 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2137 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002139 if (t->mode == XFRM_MODE_TUNNEL) {
2140 u8 *sa = (void *)(rq + 1);
2141 pfkey_sockaddr_fill(&t->saddr, 0,
2142 (struct sockaddr *)sa,
2143 t->encap_family);
2144 pfkey_sockaddr_fill(&t->id.daddr, 0,
2145 (struct sockaddr *) (sa + socklen),
2146 t->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002149
2150 /* security context */
2151 if ((xfrm_ctx = xp->security)) {
2152 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2153
2154 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2155 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2156 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2157 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2158 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2159 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2160 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2161 xfrm_ctx->ctx_len);
2162 }
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 hdr->sadb_msg_len = size / sizeof(uint64_t);
2165 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002166
2167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
David S. Miller214e0052011-02-24 00:02:38 -05002170static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002171{
2172 struct sk_buff *out_skb;
2173 struct sadb_msg *out_hdr;
2174 int err;
2175
2176 out_skb = pfkey_xfrm_policy2msg_prep(xp);
Dan Carpenter9a127aa2010-03-24 01:47:00 +00002177 if (IS_ERR(out_skb))
2178 return PTR_ERR(out_skb);
2179
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002180 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2181 if (err < 0)
2182 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002183
2184 out_hdr = (struct sadb_msg *) out_skb->data;
2185 out_hdr->sadb_msg_version = PF_KEY_V2;
2186
Herbert Xuf60f6b82005-06-18 22:44:37 -07002187 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002188 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2189 else
2190 out_hdr->sadb_msg_type = event2poltype(c->event);
2191 out_hdr->sadb_msg_errno = 0;
2192 out_hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002193 out_hdr->sadb_msg_pid = c->portid;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002194 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002195 return 0;
2196
2197}
2198
David S. Miller4c93fbb2011-02-25 18:07:06 -08002199static 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 -07002200{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002201 struct net *net = sock_net(sk);
Trent Jaegerdf718372005-12-13 23:12:27 -08002202 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 struct sadb_lifetime *lifetime;
2204 struct sadb_address *sa;
2205 struct sadb_x_policy *pol;
2206 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002207 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002208 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2211 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2212 !ext_hdrs[SADB_X_EXT_POLICY-1])
2213 return -EINVAL;
2214
2215 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2216 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2217 return -EINVAL;
2218 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2219 return -EINVAL;
2220
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002221 xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (xp == NULL)
2223 return -ENOBUFS;
2224
2225 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2226 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2227 xp->priority = pol->sadb_x_policy_priority;
2228
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002229 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 xp->selector.family = xp->family;
2232 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2233 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2234 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2235 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002236 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002238 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2240 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2241
2242 /* Amusing, we set this twice. KAME apps appear to set same value
2243 * in both addresses.
2244 */
2245 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2246
2247 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2248 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002249 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Joe Perchesea110732011-06-13 16:21:26 +00002251 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002252 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01002253 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002254
2255 if (!uctx) {
2256 err = -ENOBUFS;
2257 goto out;
2258 }
2259
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002260 err = security_xfrm_policy_alloc(&xp->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002261 kfree(uctx);
2262
2263 if (err)
2264 goto out;
2265 }
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 xp->lft.soft_byte_limit = XFRM_INF;
2268 xp->lft.hard_byte_limit = XFRM_INF;
2269 xp->lft.soft_packet_limit = XFRM_INF;
2270 xp->lft.hard_packet_limit = XFRM_INF;
2271 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2272 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2273 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2274 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2275 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2276 }
2277 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2278 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2279 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2280 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2281 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2282 }
2283 xp->xfrm_nr = 0;
2284 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2285 (err = parse_ipsecrequests(xp, pol)) < 0)
2286 goto out;
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2289 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002290
Joy Lattenab5f5e82007-09-17 11:51:22 -07002291 xfrm_audit_policy_add(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002292 audit_get_loginuid(current),
2293 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06002294
Trent Jaegerdf718372005-12-13 23:12:27 -08002295 if (err)
2296 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002298 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002299 c.event = XFRM_MSG_UPDPOLICY;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002300 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07002301 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002303 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002304 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002305
2306 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return 0;
2309
2310out:
Herbert Xu12a169e2008-10-01 07:03:24 -07002311 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08002312 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return err;
2314}
2315
David S. Miller4c93fbb2011-02-25 18:07:06 -08002316static 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 -07002317{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002318 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 int err;
2320 struct sadb_address *sa;
2321 struct sadb_x_policy *pol;
Paul Moore03e1ad72008-04-12 19:07:52 -07002322 struct xfrm_policy *xp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002324 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002325 struct sadb_x_sec_ctx *sec_ctx;
Brian Haley2db3e472008-04-24 20:38:31 -07002326 struct xfrm_sec_ctx *pol_ctx = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2329 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2330 !ext_hdrs[SADB_X_EXT_POLICY-1])
2331 return -EINVAL;
2332
2333 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2334 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2335 return -EINVAL;
2336
2337 memset(&sel, 0, sizeof(sel));
2338
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002339 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2341 sel.prefixlen_s = sa->sadb_address_prefixlen;
2342 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2343 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2344 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002345 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002347 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2349 sel.prefixlen_d = sa->sadb_address_prefixlen;
2350 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2351 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2352 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002353 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Joe Perchesea110732011-06-13 16:21:26 +00002355 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002356 if (sec_ctx != NULL) {
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01002357 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002358
2359 if (!uctx)
2360 return -ENOMEM;
2361
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002362 err = security_xfrm_policy_alloc(&pol_ctx, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08002363 kfree(uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002364 if (err)
2365 return err;
Brian Haley2db3e472008-04-24 20:38:31 -07002366 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002367
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002368 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Paul Moore03e1ad72008-04-12 19:07:52 -07002369 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2370 1, &err);
2371 security_xfrm_policy_free(pol_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (xp == NULL)
2373 return -ENOENT;
2374
Joy Lattenab5f5e82007-09-17 11:51:22 -07002375 xfrm_audit_policy_delete(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002376 audit_get_loginuid(current),
2377 audit_get_sessionid(current), 0);
David S. Miller13fcfbb02007-02-12 13:53:54 -08002378
2379 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002380 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08002381
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002382 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002383 c.portid = hdr->sadb_msg_pid;
Tobias Brunner1839faa2008-10-10 14:07:03 -07002384 c.data.byid = 0;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002385 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002386 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2387
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002388out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002389 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00002390 if (err == 0)
2391 xfrm_garbage_collect(net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002392 return err;
2393}
2394
David S. Miller4c93fbb2011-02-25 18:07:06 -08002395static 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 -07002396{
2397 int err;
2398 struct sk_buff *out_skb;
2399 struct sadb_msg *out_hdr;
2400 err = 0;
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2403 if (IS_ERR(out_skb)) {
2404 err = PTR_ERR(out_skb);
2405 goto out;
2406 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002407 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2408 if (err < 0)
2409 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411 out_hdr = (struct sadb_msg *) out_skb->data;
2412 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002413 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 out_hdr->sadb_msg_satype = 0;
2415 out_hdr->sadb_msg_errno = 0;
2416 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2417 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002418 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 err = 0;
2420
2421out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return err;
2423}
2424
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002425#ifdef CONFIG_NET_KEY_MIGRATE
2426static int pfkey_sockaddr_pair_size(sa_family_t family)
2427{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002428 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002429}
2430
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002431static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002432 xfrm_address_t *saddr, xfrm_address_t *daddr,
2433 u16 *family)
2434{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002435 int af, socklen;
2436
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002437 if (ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002438 return -EINVAL;
2439
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002440 af = pfkey_sockaddr_extract(sa, saddr);
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002441 if (!af)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002442 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002443
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002444 socklen = pfkey_sockaddr_len(af);
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002445 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002446 daddr) != af)
2447 return -EINVAL;
2448
2449 *family = af;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002450 return 0;
2451}
2452
2453static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2454 struct xfrm_migrate *m)
2455{
2456 int err;
2457 struct sadb_x_ipsecrequest *rq2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002458 int mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002459
2460 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2461 len < rq1->sadb_x_ipsecrequest_len)
2462 return -EINVAL;
2463
2464 /* old endoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002465 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
2466 rq1->sadb_x_ipsecrequest_len,
2467 &m->old_saddr, &m->old_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002468 &m->old_family);
2469 if (err)
2470 return err;
2471
2472 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2473 len -= rq1->sadb_x_ipsecrequest_len;
2474
2475 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2476 len < rq2->sadb_x_ipsecrequest_len)
2477 return -EINVAL;
2478
2479 /* new endpoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002480 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
2481 rq2->sadb_x_ipsecrequest_len,
2482 &m->new_saddr, &m->new_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002483 &m->new_family);
2484 if (err)
2485 return err;
2486
2487 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2488 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2489 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2490 return -EINVAL;
2491
2492 m->proto = rq1->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002493 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2494 return -EINVAL;
2495 m->mode = mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002496 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2497
2498 return ((int)(rq1->sadb_x_ipsecrequest_len +
2499 rq2->sadb_x_ipsecrequest_len));
2500}
2501
2502static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002503 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002504{
2505 int i, len, ret, err = -EINVAL;
2506 u8 dir;
2507 struct sadb_address *sa;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002508 struct sadb_x_kmaddress *kma;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002509 struct sadb_x_policy *pol;
2510 struct sadb_x_ipsecrequest *rq;
2511 struct xfrm_selector sel;
2512 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002513 struct xfrm_kmaddress k;
Fan Du8d549c42013-11-07 17:47:49 +08002514 struct net *net = sock_net(sk);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002515
2516 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002517 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002518 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2519 err = -EINVAL;
2520 goto out;
2521 }
2522
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002523 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002524 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002525
2526 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2527 err = -EINVAL;
2528 goto out;
2529 }
2530
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002531 if (kma) {
2532 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2533 k.reserved = kma->sadb_x_kmaddress_reserved;
2534 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2535 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2536 &k.local, &k.remote, &k.family);
2537 if (ret < 0) {
2538 err = ret;
2539 goto out;
2540 }
2541 }
2542
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002543 dir = pol->sadb_x_policy_dir - 1;
2544 memset(&sel, 0, sizeof(sel));
2545
2546 /* set source address info of selector */
2547 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2548 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2549 sel.prefixlen_s = sa->sadb_address_prefixlen;
2550 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2551 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2552 if (sel.sport)
Al Viro582ee432007-07-26 17:33:39 +01002553 sel.sport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002554
2555 /* set destination address info of selector */
2556 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2557 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2558 sel.prefixlen_d = sa->sadb_address_prefixlen;
2559 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2560 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2561 if (sel.dport)
Al Viro582ee432007-07-26 17:33:39 +01002562 sel.dport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002563
2564 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2565
2566 /* extract ipsecrequests */
2567 i = 0;
2568 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2569
2570 while (len > 0 && i < XFRM_MAX_DEPTH) {
2571 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2572 if (ret < 0) {
2573 err = ret;
2574 goto out;
2575 } else {
2576 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2577 len -= ret;
2578 i++;
2579 }
2580 }
2581
2582 if (!i || len > 0) {
2583 err = -EINVAL;
2584 goto out;
2585 }
2586
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002587 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
Fan Du8d549c42013-11-07 17:47:49 +08002588 kma ? &k : NULL, net);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002589
2590 out:
2591 return err;
2592}
2593#else
2594static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
Stephen Hemminger7f6daa62011-03-01 22:51:52 -08002595 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002596{
2597 return -ENOPROTOOPT;
2598}
2599#endif
2600
2601
David S. Miller4c93fbb2011-02-25 18:07:06 -08002602static 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 -07002603{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002604 struct net *net = sock_net(sk);
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002605 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002606 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 struct sadb_x_policy *pol;
2608 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002609 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
2611 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2612 return -EINVAL;
2613
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002614 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2615 if (dir >= XFRM_POLICY_MAX)
2616 return -EINVAL;
2617
Eric Paris215a2dd2007-03-07 16:01:45 -08002618 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002619 xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002620 dir, pol->sadb_x_policy_id, delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (xp == NULL)
2622 return -ENOENT;
2623
Eric Paris215a2dd2007-03-07 16:01:45 -08002624 if (delete) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002625 xfrm_audit_policy_delete(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002626 audit_get_loginuid(current),
2627 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Eric Paris215a2dd2007-03-07 16:01:45 -08002629 if (err)
2630 goto out;
2631 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002632 c.portid = hdr->sadb_msg_pid;
Herbert Xubf088672005-06-18 22:44:00 -07002633 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002634 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002635 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002636 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002637 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Eric Paris215a2dd2007-03-07 16:01:45 -08002640out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00002642 if (delete && err == 0)
2643 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 return err;
2645}
2646
2647static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2648{
Timo Teras83321d62008-03-03 23:40:12 -08002649 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 struct sk_buff *out_skb;
2651 struct sadb_msg *out_hdr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002652 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Timo Teras83321d62008-03-03 23:40:12 -08002654 if (!pfkey_can_dump(&pfk->sk))
2655 return -ENOBUFS;
2656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2658 if (IS_ERR(out_skb))
2659 return PTR_ERR(out_skb);
2660
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002661 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2662 if (err < 0)
2663 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08002666 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2668 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2669 out_hdr->sadb_msg_errno = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07002670 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002671 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07002672
2673 if (pfk->dump.skb)
2674 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002675 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07002676 pfk->dump.skb = out_skb;
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return 0;
2679}
2680
Timo Teras83321d62008-03-03 23:40:12 -08002681static int pfkey_dump_sp(struct pfkey_sock *pfk)
2682{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002683 struct net *net = sock_net(&pfk->sk);
2684 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08002685}
2686
2687static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2688{
Fan Du283bc9f2013-11-07 17:47:50 +08002689 struct net *net = sock_net((struct sock *)pfk);
2690
2691 xfrm_policy_walk_done(&pfk->dump.u.policy, net);
Timo Teras83321d62008-03-03 23:40:12 -08002692}
2693
David S. Miller4c93fbb2011-02-25 18:07:06 -08002694static 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 -07002695{
Timo Teras83321d62008-03-03 23:40:12 -08002696 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Timo Teras83321d62008-03-03 23:40:12 -08002698 if (pfk->dump.dump != NULL)
2699 return -EBUSY;
Timo Teras4c563f72008-02-28 21:31:08 -08002700
Timo Teras83321d62008-03-03 23:40:12 -08002701 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002702 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08002703 pfk->dump.dump = pfkey_dump_sp;
2704 pfk->dump.done = pfkey_dump_sp_done;
2705 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2706
2707 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
David S. Miller214e0052011-02-24 00:02:38 -05002710static int key_notify_policy_flush(const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
2712 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002713 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002715 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 if (!skb_out)
2717 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002718 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002719 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002720 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002721 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002722 hdr->sadb_msg_version = PF_KEY_V2;
2723 hdr->sadb_msg_errno = (uint8_t) 0;
Nicolas Dichtel85dfb742013-02-18 16:24:20 +01002724 hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002725 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Mathias Krausea5cc68f2013-06-26 23:52:30 +02002726 hdr->sadb_msg_reserved = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002727 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002728 return 0;
2729
2730}
2731
David S. Miller4c93fbb2011-02-25 18:07:06 -08002732static 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 -07002733{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002734 struct net *net = sock_net(sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002735 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06002736 struct xfrm_audit audit_info;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002737 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Al Viro0c11b942008-01-10 04:20:52 -05002739 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04002740 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06002741 audit_info.secid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002742 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002743 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002744 if (err || err2) {
2745 if (err == -ESRCH) /* empty table - old silent behavior */
2746 return 0;
2747 return err;
2748 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002749
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002750 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002751 c.event = XFRM_MSG_FLUSHPOLICY;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002752 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002753 c.seq = hdr->sadb_msg_seq;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002754 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002755 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 return 0;
2758}
2759
2760typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002761 const struct sadb_msg *hdr, void * const *ext_hdrs);
Mathias Krause8603b9552013-06-26 23:56:59 +02002762static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 [SADB_RESERVED] = pfkey_reserved,
2764 [SADB_GETSPI] = pfkey_getspi,
2765 [SADB_UPDATE] = pfkey_add,
2766 [SADB_ADD] = pfkey_add,
2767 [SADB_DELETE] = pfkey_delete,
2768 [SADB_GET] = pfkey_get,
2769 [SADB_ACQUIRE] = pfkey_acquire,
2770 [SADB_REGISTER] = pfkey_register,
2771 [SADB_EXPIRE] = NULL,
2772 [SADB_FLUSH] = pfkey_flush,
2773 [SADB_DUMP] = pfkey_dump,
2774 [SADB_X_PROMISC] = pfkey_promisc,
2775 [SADB_X_PCHANGE] = NULL,
2776 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2777 [SADB_X_SPDADD] = pfkey_spdadd,
2778 [SADB_X_SPDDELETE] = pfkey_spddelete,
2779 [SADB_X_SPDGET] = pfkey_spdget,
2780 [SADB_X_SPDACQUIRE] = NULL,
2781 [SADB_X_SPDDUMP] = pfkey_spddump,
2782 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2783 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2784 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002785 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786};
2787
David S. Miller4c93fbb2011-02-25 18:07:06 -08002788static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
2790 void *ext_hdrs[SADB_EXT_MAX];
2791 int err;
2792
2793 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002794 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2797 err = parse_exthdrs(skb, hdr, ext_hdrs);
2798 if (!err) {
2799 err = -EOPNOTSUPP;
2800 if (pfkey_funcs[hdr->sadb_msg_type])
2801 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2802 }
2803 return err;
2804}
2805
2806static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2807{
2808 struct sadb_msg *hdr = NULL;
2809
2810 if (skb->len < sizeof(*hdr)) {
2811 *errp = -EMSGSIZE;
2812 } else {
2813 hdr = (struct sadb_msg *) skb->data;
2814 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2815 hdr->sadb_msg_reserved != 0 ||
2816 (hdr->sadb_msg_type <= SADB_RESERVED ||
2817 hdr->sadb_msg_type > SADB_MAX)) {
2818 hdr = NULL;
2819 *errp = -EINVAL;
2820 } else if (hdr->sadb_msg_len != (skb->len /
2821 sizeof(uint64_t)) ||
2822 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2823 sizeof(uint64_t))) {
2824 hdr = NULL;
2825 *errp = -EMSGSIZE;
2826 } else {
2827 *errp = 0;
2828 }
2829 }
2830 return hdr;
2831}
2832
David S. Miller4c93fbb2011-02-25 18:07:06 -08002833static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
2834 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
Herbert Xuf3980352007-12-19 23:44:29 -08002836 unsigned int id = d->desc.sadb_alg_id;
2837
2838 if (id >= sizeof(t->aalgos) * 8)
2839 return 0;
2840
2841 return (t->aalgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
David S. Miller4c93fbb2011-02-25 18:07:06 -08002844static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
2845 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
Herbert Xuf3980352007-12-19 23:44:29 -08002847 unsigned int id = d->desc.sadb_alg_id;
2848
2849 if (id >= sizeof(t->ealgos) * 8)
2850 return 0;
2851
2852 return (t->ealgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
2854
David S. Miller4c93fbb2011-02-25 18:07:06 -08002855static int count_ah_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
2857 int i, sz = 0;
2858
2859 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002860 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if (!aalg)
2862 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002863 if (!aalg->pfkey_supported)
2864 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 if (aalg_tmpl_set(t, aalg) && aalg->available)
2866 sz += sizeof(struct sadb_comb);
2867 }
2868 return sz + sizeof(struct sadb_prop);
2869}
2870
David S. Miller4c93fbb2011-02-25 18:07:06 -08002871static int count_esp_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
2873 int i, k, sz = 0;
2874
2875 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002876 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (!ealg)
2878 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002879
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002880 if (!ealg->pfkey_supported)
2881 continue;
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2884 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 for (k = 1; ; k++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002887 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 if (!aalg)
2889 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002890
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002891 if (!aalg->pfkey_supported)
2892 continue;
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (aalg_tmpl_set(t, aalg) && aalg->available)
2895 sz += sizeof(struct sadb_comb);
2896 }
2897 }
2898 return sz + sizeof(struct sadb_prop);
2899}
2900
David S. Miller4c93fbb2011-02-25 18:07:06 -08002901static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
2903 struct sadb_prop *p;
2904 int i;
2905
2906 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2907 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2908 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2909 p->sadb_prop_replay = 32;
2910 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2911
2912 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002913 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 if (!aalg)
2915 break;
2916
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002917 if (!aalg->pfkey_supported)
2918 continue;
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2921 struct sadb_comb *c;
2922 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2923 memset(c, 0, sizeof(*c));
2924 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2925 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2926 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2927 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2928 c->sadb_comb_hard_addtime = 24*60*60;
2929 c->sadb_comb_soft_addtime = 20*60*60;
2930 c->sadb_comb_hard_usetime = 8*60*60;
2931 c->sadb_comb_soft_usetime = 7*60*60;
2932 }
2933 }
2934}
2935
David S. Miller4c93fbb2011-02-25 18:07:06 -08002936static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
2938 struct sadb_prop *p;
2939 int i, k;
2940
2941 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2942 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2943 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2944 p->sadb_prop_replay = 32;
2945 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2946
2947 for (i=0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002948 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 if (!ealg)
2950 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002951
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002952 if (!ealg->pfkey_supported)
2953 continue;
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2956 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 for (k = 1; ; k++) {
2959 struct sadb_comb *c;
David S. Miller4c93fbb2011-02-25 18:07:06 -08002960 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 if (!aalg)
2962 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002963 if (!aalg->pfkey_supported)
2964 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2966 continue;
2967 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2968 memset(c, 0, sizeof(*c));
2969 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2970 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2971 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2972 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2973 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2974 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2975 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2976 c->sadb_comb_hard_addtime = 24*60*60;
2977 c->sadb_comb_soft_addtime = 20*60*60;
2978 c->sadb_comb_hard_usetime = 8*60*60;
2979 c->sadb_comb_soft_usetime = 7*60*60;
2980 }
2981 }
2982}
2983
David S. Miller214e0052011-02-24 00:02:38 -05002984static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002985{
2986 return 0;
2987}
2988
David S. Miller214e0052011-02-24 00:02:38 -05002989static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
2991 struct sk_buff *out_skb;
2992 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002993 int hard;
2994 int hsc;
2995
Herbert Xubf088672005-06-18 22:44:00 -07002996 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002997 if (hard)
2998 hsc = 2;
2999 else
3000 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Herbert Xu050f0092007-10-09 13:31:47 -07003002 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 if (IS_ERR(out_skb))
3004 return PTR_ERR(out_skb);
3005
3006 out_hdr = (struct sadb_msg *) out_skb->data;
3007 out_hdr->sadb_msg_version = PF_KEY_V2;
3008 out_hdr->sadb_msg_type = SADB_EXPIRE;
3009 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3010 out_hdr->sadb_msg_errno = 0;
3011 out_hdr->sadb_msg_reserved = 0;
3012 out_hdr->sadb_msg_seq = 0;
3013 out_hdr->sadb_msg_pid = 0;
3014
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003015 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 return 0;
3017}
3018
David S. Miller214e0052011-02-24 00:02:38 -05003019static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003020{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003021 struct net *net = x ? xs_net(x) : c->net;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003022 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3023
3024 if (atomic_read(&net_pfkey->socks_nr) == 0)
Jamal Hadi Salim99c6f602008-06-10 14:25:34 -07003025 return 0;
3026
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003027 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003028 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003029 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003030 case XFRM_MSG_DELSA:
3031 case XFRM_MSG_NEWSA:
3032 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003033 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003034 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003035 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003036 case XFRM_MSG_NEWAE: /* not yet supported */
3037 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003038 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003039 pr_err("pfkey: Unknown SA event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003040 break;
3041 }
3042
3043 return 0;
3044}
3045
David S. Miller214e0052011-02-24 00:02:38 -05003046static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003047{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003048 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3049 return 0;
3050
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003051 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003052 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003053 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003054 case XFRM_MSG_DELPOLICY:
3055 case XFRM_MSG_NEWPOLICY:
3056 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003057 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003058 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003059 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3060 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003061 return key_notify_policy_flush(c);
3062 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003063 pr_err("pfkey: Unknown policy event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003064 break;
3065 }
3066
3067 return 0;
3068}
3069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070static u32 get_acqseq(void)
3071{
3072 u32 res;
Eric Dumazet28aecb92010-01-29 04:05:52 +00003073 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Eric Dumazet28aecb92010-01-29 04:05:52 +00003075 do {
3076 res = atomic_inc_return(&acqseq);
3077 } while (!res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return res;
3079}
3080
Horia Geanta0f245582014-02-12 16:20:06 +02003081static bool pfkey_is_alive(const struct km_event *c)
3082{
3083 struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id);
3084 struct sock *sk;
3085 bool is_alive = false;
3086
3087 rcu_read_lock();
3088 sk_for_each_rcu(sk, &net_pfkey->table) {
3089 if (pfkey_sk(sk)->registered) {
3090 is_alive = true;
3091 break;
3092 }
3093 }
3094 rcu_read_unlock();
3095
3096 return is_alive;
3097}
3098
Fan Du65e07362012-08-15 10:13:47 +08003099static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
3101 struct sk_buff *skb;
3102 struct sadb_msg *hdr;
3103 struct sadb_address *addr;
3104 struct sadb_x_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 int sockaddr_size;
3106 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003107 struct sadb_x_sec_ctx *sec_ctx;
3108 struct xfrm_sec_ctx *xfrm_ctx;
3109 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003110
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3112 if (!sockaddr_size)
3113 return -EINVAL;
3114
3115 size = sizeof(struct sadb_msg) +
3116 (sizeof(struct sadb_address) * 2) +
3117 (sockaddr_size * 2) +
3118 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if (x->id.proto == IPPROTO_AH)
3121 size += count_ah_combs(t);
3122 else if (x->id.proto == IPPROTO_ESP)
3123 size += count_esp_combs(t);
3124
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003125 if ((xfrm_ctx = x->security)) {
3126 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3127 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3128 }
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 skb = alloc_skb(size + 16, GFP_ATOMIC);
3131 if (skb == NULL)
3132 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3135 hdr->sadb_msg_version = PF_KEY_V2;
3136 hdr->sadb_msg_type = SADB_ACQUIRE;
3137 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3138 hdr->sadb_msg_len = size / sizeof(uint64_t);
3139 hdr->sadb_msg_errno = 0;
3140 hdr->sadb_msg_reserved = 0;
3141 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3142 hdr->sadb_msg_pid = 0;
3143
3144 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003145 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003147 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 (sizeof(struct sadb_address)+sockaddr_size)/
3149 sizeof(uint64_t);
3150 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3151 addr->sadb_address_proto = 0;
3152 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003153 addr->sadb_address_prefixlen =
3154 pfkey_sockaddr_fill(&x->props.saddr, 0,
3155 (struct sockaddr *) (addr + 1),
3156 x->props.family);
3157 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003161 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 sizeof(struct sadb_address)+sockaddr_size);
3163 addr->sadb_address_len =
3164 (sizeof(struct sadb_address)+sockaddr_size)/
3165 sizeof(uint64_t);
3166 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3167 addr->sadb_address_proto = 0;
3168 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003169 addr->sadb_address_prefixlen =
3170 pfkey_sockaddr_fill(&x->id.daddr, 0,
3171 (struct sockaddr *) (addr + 1),
3172 x->props.family);
3173 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 BUG();
3175
3176 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3177 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3178 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3179 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
Fan Du65e07362012-08-15 10:13:47 +08003180 pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1;
Dan Carpenterff862a42013-07-28 23:04:45 +03003181 pol->sadb_x_policy_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 pol->sadb_x_policy_id = xp->index;
Dan Carpenterff862a42013-07-28 23:04:45 +03003183 pol->sadb_x_policy_priority = xp->priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185 /* Set sadb_comb's. */
3186 if (x->id.proto == IPPROTO_AH)
3187 dump_ah_combs(skb, t);
3188 else if (x->id.proto == IPPROTO_ESP)
3189 dump_esp_combs(skb, t);
3190
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003191 /* security context */
3192 if (xfrm_ctx) {
3193 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3194 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3195 sec_ctx->sadb_x_sec_len =
3196 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3197 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3198 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3199 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3200 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3201 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3202 xfrm_ctx->ctx_len);
3203 }
3204
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003205 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206}
3207
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003208static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003209 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003211 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 struct xfrm_policy *xp;
3213 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003214 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003216 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 case AF_INET:
3218 if (opt != IP_IPSEC_POLICY) {
3219 *dir = -EOPNOTSUPP;
3220 return NULL;
3221 }
3222 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00003223#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 case AF_INET6:
3225 if (opt != IPV6_IPSEC_POLICY) {
3226 *dir = -EOPNOTSUPP;
3227 return NULL;
3228 }
3229 break;
3230#endif
3231 default:
3232 *dir = -EINVAL;
3233 return NULL;
3234 }
3235
3236 *dir = -EINVAL;
3237
3238 if (len < sizeof(struct sadb_x_policy) ||
3239 pol->sadb_x_policy_len*8 > len ||
3240 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3241 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3242 return NULL;
3243
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003244 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 if (xp == NULL) {
3246 *dir = -ENOBUFS;
3247 return NULL;
3248 }
3249
3250 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3251 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3252
3253 xp->lft.soft_byte_limit = XFRM_INF;
3254 xp->lft.hard_byte_limit = XFRM_INF;
3255 xp->lft.soft_packet_limit = XFRM_INF;
3256 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003257 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
3259 xp->xfrm_nr = 0;
3260 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3261 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3262 goto out;
3263
Trent Jaegerdf718372005-12-13 23:12:27 -08003264 /* security context too */
3265 if (len >= (pol->sadb_x_policy_len*8 +
3266 sizeof(struct sadb_x_sec_ctx))) {
3267 char *p = (char *)pol;
3268 struct xfrm_user_sec_ctx *uctx;
3269
3270 p += pol->sadb_x_policy_len*8;
3271 sec_ctx = (struct sadb_x_sec_ctx *)p;
3272 if (len < pol->sadb_x_policy_len*8 +
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003273 sec_ctx->sadb_x_sec_len) {
3274 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003275 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003276 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003277 if ((*dir = verify_sec_ctx_len(p)))
3278 goto out;
Nikolay Aleksandrov87536a82014-03-07 12:44:18 +01003279 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_ATOMIC);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01003280 *dir = security_xfrm_policy_alloc(&xp->security, uctx, GFP_ATOMIC);
Trent Jaegerdf718372005-12-13 23:12:27 -08003281 kfree(uctx);
3282
3283 if (*dir)
3284 goto out;
3285 }
3286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 *dir = pol->sadb_x_policy_dir-1;
3288 return xp;
3289
3290out:
Alexey Dobriyan70e90672008-11-06 23:08:37 -08003291 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08003292 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 return NULL;
3294}
3295
Al Viro5d36b182006-11-08 00:24:06 -08003296static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
3298 struct sk_buff *skb;
3299 struct sadb_msg *hdr;
3300 struct sadb_sa *sa;
3301 struct sadb_address *addr;
3302 struct sadb_x_nat_t_port *n_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 int sockaddr_size;
3304 int size;
3305 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3306 struct xfrm_encap_tmpl *natt = NULL;
3307
3308 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3309 if (!sockaddr_size)
3310 return -EINVAL;
3311
3312 if (!satype)
3313 return -EINVAL;
3314
3315 if (!x->encap)
3316 return -EINVAL;
3317
3318 natt = x->encap;
3319
3320 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3321 *
3322 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3323 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3324 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003325
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 size = sizeof(struct sadb_msg) +
3327 sizeof(struct sadb_sa) +
3328 (sizeof(struct sadb_address) * 2) +
3329 (sockaddr_size * 2) +
3330 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 skb = alloc_skb(size + 16, GFP_ATOMIC);
3333 if (skb == NULL)
3334 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3337 hdr->sadb_msg_version = PF_KEY_V2;
3338 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3339 hdr->sadb_msg_satype = satype;
3340 hdr->sadb_msg_len = size / sizeof(uint64_t);
3341 hdr->sadb_msg_errno = 0;
3342 hdr->sadb_msg_reserved = 0;
3343 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3344 hdr->sadb_msg_pid = 0;
3345
3346 /* SA */
3347 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3348 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3349 sa->sadb_sa_exttype = SADB_EXT_SA;
3350 sa->sadb_sa_spi = x->id.spi;
3351 sa->sadb_sa_replay = 0;
3352 sa->sadb_sa_state = 0;
3353 sa->sadb_sa_auth = 0;
3354 sa->sadb_sa_encrypt = 0;
3355 sa->sadb_sa_flags = 0;
3356
3357 /* ADDRESS_SRC (old addr) */
3358 addr = (struct sadb_address*)
3359 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003360 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 (sizeof(struct sadb_address)+sockaddr_size)/
3362 sizeof(uint64_t);
3363 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3364 addr->sadb_address_proto = 0;
3365 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003366 addr->sadb_address_prefixlen =
3367 pfkey_sockaddr_fill(&x->props.saddr, 0,
3368 (struct sockaddr *) (addr + 1),
3369 x->props.family);
3370 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 BUG();
3372
3373 /* NAT_T_SPORT (old port) */
3374 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3375 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3376 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3377 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3378 n_port->sadb_x_nat_t_port_reserved = 0;
3379
3380 /* ADDRESS_DST (new addr) */
3381 addr = (struct sadb_address*)
3382 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003383 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 (sizeof(struct sadb_address)+sockaddr_size)/
3385 sizeof(uint64_t);
3386 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3387 addr->sadb_address_proto = 0;
3388 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003389 addr->sadb_address_prefixlen =
3390 pfkey_sockaddr_fill(ipaddr, 0,
3391 (struct sockaddr *) (addr + 1),
3392 x->props.family);
3393 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 BUG();
3395
3396 /* NAT_T_DPORT (new port) */
3397 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3398 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3399 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3400 n_port->sadb_x_nat_t_port_port = sport;
3401 n_port->sadb_x_nat_t_port_reserved = 0;
3402
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003403 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003406#ifdef CONFIG_NET_KEY_MIGRATE
3407static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
David S. Miller183cad12011-02-24 00:28:01 -05003408 const struct xfrm_selector *sel)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003409{
3410 struct sadb_address *addr;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003411 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3412 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3413 addr->sadb_address_exttype = type;
3414 addr->sadb_address_proto = sel->proto;
3415 addr->sadb_address_reserved = 0;
3416
3417 switch (type) {
3418 case SADB_EXT_ADDRESS_SRC:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003419 addr->sadb_address_prefixlen = sel->prefixlen_s;
3420 pfkey_sockaddr_fill(&sel->saddr, 0,
3421 (struct sockaddr *)(addr + 1),
3422 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003423 break;
3424 case SADB_EXT_ADDRESS_DST:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003425 addr->sadb_address_prefixlen = sel->prefixlen_d;
3426 pfkey_sockaddr_fill(&sel->daddr, 0,
3427 (struct sockaddr *)(addr + 1),
3428 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003429 break;
3430 default:
3431 return -EINVAL;
3432 }
3433
3434 return 0;
3435}
3436
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003437
David S. Miller183cad12011-02-24 00:28:01 -05003438static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003439{
3440 struct sadb_x_kmaddress *kma;
3441 u8 *sa;
3442 int family = k->family;
3443 int socklen = pfkey_sockaddr_len(family);
3444 int size_req;
3445
3446 size_req = (sizeof(struct sadb_x_kmaddress) +
3447 pfkey_sockaddr_pair_size(family));
3448
3449 kma = (struct sadb_x_kmaddress *)skb_put(skb, size_req);
3450 memset(kma, 0, size_req);
3451 kma->sadb_x_kmaddress_len = size_req / 8;
3452 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3453 kma->sadb_x_kmaddress_reserved = k->reserved;
3454
3455 sa = (u8 *)(kma + 1);
3456 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3457 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3458 return -EINVAL;
3459
3460 return 0;
3461}
3462
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003463static int set_ipsecrequest(struct sk_buff *skb,
3464 uint8_t proto, uint8_t mode, int level,
3465 uint32_t reqid, uint8_t family,
David S. Miller183cad12011-02-24 00:28:01 -05003466 const xfrm_address_t *src, const xfrm_address_t *dst)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003467{
3468 struct sadb_x_ipsecrequest *rq;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003469 u8 *sa;
3470 int socklen = pfkey_sockaddr_len(family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003471 int size_req;
3472
3473 size_req = sizeof(struct sadb_x_ipsecrequest) +
3474 pfkey_sockaddr_pair_size(family);
3475
3476 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3477 memset(rq, 0, size_req);
3478 rq->sadb_x_ipsecrequest_len = size_req;
3479 rq->sadb_x_ipsecrequest_proto = proto;
3480 rq->sadb_x_ipsecrequest_mode = mode;
3481 rq->sadb_x_ipsecrequest_level = level;
3482 rq->sadb_x_ipsecrequest_reqid = reqid;
3483
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003484 sa = (u8 *) (rq + 1);
3485 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3486 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003487 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003488
3489 return 0;
3490}
3491#endif
3492
3493#ifdef CONFIG_NET_KEY_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05003494static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3495 const struct xfrm_migrate *m, int num_bundles,
3496 const struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003497{
3498 int i;
3499 int sasize_sel;
3500 int size = 0;
3501 int size_pol = 0;
3502 struct sk_buff *skb;
3503 struct sadb_msg *hdr;
3504 struct sadb_x_policy *pol;
David S. Miller183cad12011-02-24 00:28:01 -05003505 const struct xfrm_migrate *mp;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003506
3507 if (type != XFRM_POLICY_TYPE_MAIN)
3508 return 0;
3509
3510 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3511 return -EINVAL;
3512
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003513 if (k != NULL) {
3514 /* addresses for KM */
3515 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3516 pfkey_sockaddr_pair_size(k->family));
3517 }
3518
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003519 /* selector */
3520 sasize_sel = pfkey_sockaddr_size(sel->family);
3521 if (!sasize_sel)
3522 return -EINVAL;
3523 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3524
3525 /* policy info */
3526 size_pol += sizeof(struct sadb_x_policy);
3527
3528 /* ipsecrequests */
3529 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3530 /* old locator pair */
3531 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3532 pfkey_sockaddr_pair_size(mp->old_family);
3533 /* new locator pair */
3534 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3535 pfkey_sockaddr_pair_size(mp->new_family);
3536 }
3537
3538 size += sizeof(struct sadb_msg) + size_pol;
3539
3540 /* alloc buffer */
3541 skb = alloc_skb(size, GFP_ATOMIC);
3542 if (skb == NULL)
3543 return -ENOMEM;
3544
3545 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3546 hdr->sadb_msg_version = PF_KEY_V2;
3547 hdr->sadb_msg_type = SADB_X_MIGRATE;
3548 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3549 hdr->sadb_msg_len = size / 8;
3550 hdr->sadb_msg_errno = 0;
3551 hdr->sadb_msg_reserved = 0;
3552 hdr->sadb_msg_seq = 0;
3553 hdr->sadb_msg_pid = 0;
3554
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003555 /* Addresses to be used by KM for negotiation, if ext is available */
3556 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
Julia Lawall89eb06f2012-04-08 22:41:10 +00003557 goto err;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003558
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003559 /* selector src */
3560 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3561
3562 /* selector dst */
3563 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3564
3565 /* policy information */
3566 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3567 pol->sadb_x_policy_len = size_pol / 8;
3568 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3569 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3570 pol->sadb_x_policy_dir = dir + 1;
Dan Carpenterff862a42013-07-28 23:04:45 +03003571 pol->sadb_x_policy_reserved = 0;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003572 pol->sadb_x_policy_id = 0;
3573 pol->sadb_x_policy_priority = 0;
3574
3575 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3576 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003577 int mode = pfkey_mode_from_xfrm(mp->mode);
3578 if (mode < 0)
Patrick McHardyd4782c32008-01-20 17:24:29 -08003579 goto err;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003580 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003581 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3582 mp->reqid, mp->old_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003583 &mp->old_saddr, &mp->old_daddr) < 0)
3584 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003585
3586 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003587 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003588 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3589 mp->reqid, mp->new_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003590 &mp->new_saddr, &mp->new_daddr) < 0)
3591 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003592 }
3593
3594 /* broadcast migrate message to sockets */
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003595 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003596
3597 return 0;
Patrick McHardyd4782c32008-01-20 17:24:29 -08003598
3599err:
3600 kfree_skb(skb);
3601 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003602}
3603#else
David S. Miller183cad12011-02-24 00:28:01 -05003604static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3605 const struct xfrm_migrate *m, int num_bundles,
3606 const struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003607{
3608 return -ENOPROTOOPT;
3609}
3610#endif
3611
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612static int pfkey_sendmsg(struct kiocb *kiocb,
3613 struct socket *sock, struct msghdr *msg, size_t len)
3614{
3615 struct sock *sk = sock->sk;
3616 struct sk_buff *skb = NULL;
3617 struct sadb_msg *hdr = NULL;
3618 int err;
Fan Du283bc9f2013-11-07 17:47:50 +08003619 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
3621 err = -EOPNOTSUPP;
3622 if (msg->msg_flags & MSG_OOB)
3623 goto out;
3624
3625 err = -EMSGSIZE;
Eric Dumazet95c96172012-04-15 05:58:06 +00003626 if ((unsigned int)len > sk->sk_sndbuf - 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 goto out;
3628
3629 err = -ENOBUFS;
3630 skb = alloc_skb(len, GFP_KERNEL);
3631 if (skb == NULL)
3632 goto out;
3633
3634 err = -EFAULT;
3635 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3636 goto out;
3637
3638 hdr = pfkey_get_base_msg(skb, &err);
3639 if (!hdr)
3640 goto out;
3641
Fan Du283bc9f2013-11-07 17:47:50 +08003642 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 err = pfkey_process(sk, skb, hdr);
Fan Du283bc9f2013-11-07 17:47:50 +08003644 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
3646out:
3647 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3648 err = 0;
Wei Yongjun6f961062009-02-25 00:31:04 +00003649 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
3651 return err ? : len;
3652}
3653
3654static int pfkey_recvmsg(struct kiocb *kiocb,
3655 struct socket *sock, struct msghdr *msg, size_t len,
3656 int flags)
3657{
3658 struct sock *sk = sock->sk;
Timo Teras83321d62008-03-03 23:40:12 -08003659 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 struct sk_buff *skb;
3661 int copied, err;
3662
3663 err = -EINVAL;
3664 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3665 goto out;
3666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3668 if (skb == NULL)
3669 goto out;
3670
3671 copied = skb->len;
3672 if (copied > len) {
3673 msg->msg_flags |= MSG_TRUNC;
3674 copied = len;
3675 }
3676
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003677 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3679 if (err)
3680 goto out_free;
3681
Neil Horman3b885782009-10-12 13:26:31 -07003682 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684 err = (flags & MSG_TRUNC) ? skb->len : copied;
3685
Timo Teras83321d62008-03-03 23:40:12 -08003686 if (pfk->dump.dump != NULL &&
3687 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3688 pfkey_do_dump(pfk);
3689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690out_free:
3691 skb_free_datagram(sk, skb);
3692out:
3693 return err;
3694}
3695
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003696static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 .family = PF_KEY,
3698 .owner = THIS_MODULE,
3699 /* Operations that make no sense on pfkey sockets. */
3700 .bind = sock_no_bind,
3701 .connect = sock_no_connect,
3702 .socketpair = sock_no_socketpair,
3703 .accept = sock_no_accept,
3704 .getname = sock_no_getname,
3705 .ioctl = sock_no_ioctl,
3706 .listen = sock_no_listen,
3707 .shutdown = sock_no_shutdown,
3708 .setsockopt = sock_no_setsockopt,
3709 .getsockopt = sock_no_getsockopt,
3710 .mmap = sock_no_mmap,
3711 .sendpage = sock_no_sendpage,
3712
3713 /* Now the operations that really occur. */
3714 .release = pfkey_release,
3715 .poll = datagram_poll,
3716 .sendmsg = pfkey_sendmsg,
3717 .recvmsg = pfkey_recvmsg,
3718};
3719
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003720static const struct net_proto_family pfkey_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 .family = PF_KEY,
3722 .create = pfkey_create,
3723 .owner = THIS_MODULE,
3724};
3725
3726#ifdef CONFIG_PROC_FS
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003727static int pfkey_seq_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{
Li Zefan27b5b862010-02-08 23:20:29 +00003729 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003731 if (v == SEQ_START_TOKEN)
3732 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3733 else
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003734 seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 s,
3736 atomic_read(&s->sk_refcnt),
Eric Dumazet31e6d362009-06-17 19:05:41 -07003737 sk_rmem_alloc_get(s),
3738 sk_wmem_alloc_get(s),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06003739 from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 sock_i_ino(s)
3741 );
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003742 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743}
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003744
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003745static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
stephen hemmingerada440e2011-02-23 09:06:50 +00003746 __acquires(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003747{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003748 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003749 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003750
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003751 rcu_read_lock();
3752 return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003753}
3754
3755static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3756{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003757 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003758 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3759
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003760 return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003761}
3762
3763static void pfkey_seq_stop(struct seq_file *f, void *v)
stephen hemmingerada440e2011-02-23 09:06:50 +00003764 __releases(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003765{
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003766 rcu_read_unlock();
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003767}
3768
Stephen Hemminger98147d52009-09-01 19:25:02 +00003769static const struct seq_operations pfkey_seq_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003770 .start = pfkey_seq_start,
3771 .next = pfkey_seq_next,
3772 .stop = pfkey_seq_stop,
3773 .show = pfkey_seq_show,
3774};
3775
3776static int pfkey_seq_open(struct inode *inode, struct file *file)
3777{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003778 return seq_open_net(inode, file, &pfkey_seq_ops,
3779 sizeof(struct seq_net_private));
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003780}
3781
Stephen Hemminger5ca1b992009-09-01 19:25:05 +00003782static const struct file_operations pfkey_proc_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003783 .open = pfkey_seq_open,
3784 .read = seq_read,
3785 .llseek = seq_lseek,
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003786 .release = seq_release_net,
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003787};
3788
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003789static int __net_init pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003790{
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003791 struct proc_dir_entry *e;
3792
Gao fengd4beaa62013-02-18 01:34:54 +00003793 e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003794 if (e == NULL)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003795 return -ENOMEM;
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003796
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003797 return 0;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003798}
3799
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003800static void __net_exit pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003801{
Gao fengece31ff2013-02-18 01:34:56 +00003802 remove_proc_entry("pfkey", net->proc_net);
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003803}
3804#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003805static inline int pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003806{
3807 return 0;
3808}
3809
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003810static inline void pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003811{
3812}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813#endif
3814
3815static struct xfrm_mgr pfkeyv2_mgr =
3816{
3817 .id = "pfkeyv2",
3818 .notify = pfkey_send_notify,
3819 .acquire = pfkey_send_acquire,
3820 .compile_policy = pfkey_compile_policy,
3821 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003822 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003823 .migrate = pfkey_send_migrate,
Horia Geanta0f245582014-02-12 16:20:06 +02003824 .is_alive = pfkey_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825};
3826
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003827static int __net_init pfkey_net_init(struct net *net)
3828{
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003829 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003830 int rv;
3831
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003832 INIT_HLIST_HEAD(&net_pfkey->table);
3833 atomic_set(&net_pfkey->socks_nr, 0);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003834
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003835 rv = pfkey_init_proc(net);
3836
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003837 return rv;
3838}
3839
3840static void __net_exit pfkey_net_exit(struct net *net)
3841{
3842 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3843
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003844 pfkey_exit_proc(net);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003845 BUG_ON(!hlist_empty(&net_pfkey->table));
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003846}
3847
3848static struct pernet_operations pfkey_net_ops = {
3849 .init = pfkey_net_init,
3850 .exit = pfkey_net_exit,
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003851 .id = &pfkey_net_id,
3852 .size = sizeof(struct netns_pfkey),
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003853};
3854
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855static void __exit ipsec_pfkey_exit(void)
3856{
3857 xfrm_unregister_km(&pfkeyv2_mgr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003859 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 proto_unregister(&key_proto);
3861}
3862
3863static int __init ipsec_pfkey_init(void)
3864{
3865 int err = proto_register(&key_proto, 0);
3866
3867 if (err != 0)
3868 goto out;
3869
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003870 err = register_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 if (err != 0)
3872 goto out_unregister_key_proto;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003873 err = sock_register(&pfkey_family_ops);
3874 if (err != 0)
3875 goto out_unregister_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 err = xfrm_register_km(&pfkeyv2_mgr);
3877 if (err != 0)
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003878 goto out_sock_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879out:
3880 return err;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003881
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882out_sock_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003884out_unregister_pernet:
3885 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886out_unregister_key_proto:
3887 proto_unregister(&key_proto);
3888 goto out;
3889}
3890
3891module_init(ipsec_pfkey_init);
3892module_exit(ipsec_pfkey_exit);
3893MODULE_LICENSE("GPL");
3894MODULE_ALIAS_NETPROTO(PF_KEY);