blob: c5fbd7589681cf984220325fff463696163340d0 [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
48static 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
341static u8 sadb_ext_min_len[] = {
342 [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),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368};
369
370/* Verify sadb_address_{len,prefixlen} against sa_family. */
David S. Miller4c93fbb2011-02-25 18:07:06 -0800371static int verify_address_len(const void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800373 const struct sadb_address *sp = p;
374 const struct sockaddr *addr = (const struct sockaddr *)(sp + 1);
375 const struct sockaddr_in *sin;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000376#if IS_ENABLED(CONFIG_IPV6)
David S. Miller4c93fbb2011-02-25 18:07:06 -0800377 const struct sockaddr_in6 *sin6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378#endif
379 int len;
380
381 switch (addr->sa_family) {
382 case AF_INET:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700383 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (sp->sadb_address_len != len ||
385 sp->sadb_address_prefixlen > 32)
386 return -EINVAL;
387 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000388#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 case AF_INET6:
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700390 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (sp->sadb_address_len != len ||
392 sp->sadb_address_prefixlen > 128)
393 return -EINVAL;
394 break;
395#endif
396 default:
397 /* It is user using kernel to keep track of security
398 * associations for another protocol, such as
399 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
400 * lengths.
401 *
402 * XXX Actually, association/policy database is not yet
403 * XXX able to cope with arbitrary sockaddr families.
404 * XXX When it can, remove this -EINVAL. -DaveM
405 */
406 return -EINVAL;
407 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 return 0;
411}
412
David S. Miller4c93fbb2011-02-25 18:07:06 -0800413static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800414{
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700415 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
416 sec_ctx->sadb_x_ctx_len,
417 sizeof(uint64_t));
Trent Jaegerdf718372005-12-13 23:12:27 -0800418}
419
David S. Miller4c93fbb2011-02-25 18:07:06 -0800420static inline int verify_sec_ctx_len(const void *p)
Trent Jaegerdf718372005-12-13 23:12:27 -0800421{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800422 const struct sadb_x_sec_ctx *sec_ctx = p;
Stephen Rothwell298bb622007-10-30 23:57:05 -0700423 int len = sec_ctx->sadb_x_ctx_len;
Trent Jaegerdf718372005-12-13 23:12:27 -0800424
Stephen Rothwell298bb622007-10-30 23:57:05 -0700425 if (len > PAGE_SIZE)
Trent Jaegerdf718372005-12-13 23:12:27 -0800426 return -EINVAL;
427
428 len = pfkey_sec_ctx_len(sec_ctx);
429
430 if (sec_ctx->sadb_x_sec_len != len)
431 return -EINVAL;
432
433 return 0;
434}
435
David S. Miller4c93fbb2011-02-25 18:07:06 -0800436static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800437{
438 struct xfrm_user_sec_ctx *uctx = NULL;
439 int ctx_size = sec_ctx->sadb_x_ctx_len;
440
441 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
442
443 if (!uctx)
444 return NULL;
445
446 uctx->len = pfkey_sec_ctx_len(sec_ctx);
447 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
448 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
449 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
450 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
451 memcpy(uctx + 1, sec_ctx + 1,
452 uctx->ctx_len);
453
454 return uctx;
455}
456
David S. Miller4c93fbb2011-02-25 18:07:06 -0800457static int present_and_same_family(const struct sadb_address *src,
458 const struct sadb_address *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800460 const struct sockaddr *s_addr, *d_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 if (!src || !dst)
463 return 0;
464
David S. Miller4c93fbb2011-02-25 18:07:06 -0800465 s_addr = (const struct sockaddr *)(src + 1);
466 d_addr = (const struct sockaddr *)(dst + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (s_addr->sa_family != d_addr->sa_family)
468 return 0;
469 if (s_addr->sa_family != AF_INET
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000470#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 && s_addr->sa_family != AF_INET6
472#endif
473 )
474 return 0;
475
476 return 1;
477}
478
David S. Miller4c93fbb2011-02-25 18:07:06 -0800479static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800481 const char *p = (char *) hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int len = skb->len;
483
484 len -= sizeof(*hdr);
485 p += sizeof(*hdr);
486 while (len > 0) {
David S. Miller4c93fbb2011-02-25 18:07:06 -0800487 const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 uint16_t ext_type;
489 int ext_len;
490
491 ext_len = ehdr->sadb_ext_len;
492 ext_len *= sizeof(uint64_t);
493 ext_type = ehdr->sadb_ext_type;
494 if (ext_len < sizeof(uint64_t) ||
495 ext_len > len ||
496 ext_type == SADB_EXT_RESERVED)
497 return -EINVAL;
498
499 if (ext_type <= SADB_EXT_MAX) {
500 int min = (int) sadb_ext_min_len[ext_type];
501 if (ext_len < min)
502 return -EINVAL;
503 if (ext_hdrs[ext_type-1] != NULL)
504 return -EINVAL;
505 if (ext_type == SADB_EXT_ADDRESS_SRC ||
506 ext_type == SADB_EXT_ADDRESS_DST ||
507 ext_type == SADB_EXT_ADDRESS_PROXY ||
508 ext_type == SADB_X_EXT_NAT_T_OA) {
509 if (verify_address_len(p))
510 return -EINVAL;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900511 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800512 if (ext_type == SADB_X_EXT_SEC_CTX) {
513 if (verify_sec_ctx_len(p))
514 return -EINVAL;
515 }
David S. Miller4c93fbb2011-02-25 18:07:06 -0800516 ext_hdrs[ext_type-1] = (void *) p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 p += ext_len;
519 len -= ext_len;
520 }
521
522 return 0;
523}
524
525static uint16_t
526pfkey_satype2proto(uint8_t satype)
527{
528 switch (satype) {
529 case SADB_SATYPE_UNSPEC:
530 return IPSEC_PROTO_ANY;
531 case SADB_SATYPE_AH:
532 return IPPROTO_AH;
533 case SADB_SATYPE_ESP:
534 return IPPROTO_ESP;
535 case SADB_X_SATYPE_IPCOMP:
536 return IPPROTO_COMP;
537 break;
538 default:
539 return 0;
540 }
541 /* NOTREACHED */
542}
543
544static uint8_t
545pfkey_proto2satype(uint16_t proto)
546{
547 switch (proto) {
548 case IPPROTO_AH:
549 return SADB_SATYPE_AH;
550 case IPPROTO_ESP:
551 return SADB_SATYPE_ESP;
552 case IPPROTO_COMP:
553 return SADB_X_SATYPE_IPCOMP;
554 break;
555 default:
556 return 0;
557 }
558 /* NOTREACHED */
559}
560
561/* BTW, this scheme means that there is no way with PFKEY2 sockets to
562 * say specifically 'just raw sockets' as we encode them as 255.
563 */
564
565static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
566{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000567 return proto == IPSEC_PROTO_ANY ? 0 : proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
571{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000572 return proto ? proto : IPSEC_PROTO_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900575static inline int pfkey_sockaddr_len(sa_family_t family)
576{
577 switch (family) {
578 case AF_INET:
579 return sizeof(struct sockaddr_in);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000580#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900581 case AF_INET6:
582 return sizeof(struct sockaddr_in6);
583#endif
584 }
585 return 0;
586}
587
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900588static
589int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900591 switch (sa->sa_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 case AF_INET:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900593 xaddr->a4 =
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900594 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return AF_INET;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000596#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 case AF_INET6:
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900598 memcpy(xaddr->a6,
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900599 &((struct sockaddr_in6 *)sa)->sin6_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 sizeof(struct in6_addr));
601 return AF_INET6;
602#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900604 return 0;
605}
606
607static
David S. Miller4c93fbb2011-02-25 18:07:06 -0800608int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +0900609{
610 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
611 xaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
David S. Miller4c93fbb2011-02-25 18:07:06 -0800614static 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 -0700615{
David S. Miller4c93fbb2011-02-25 18:07:06 -0800616 const struct sadb_sa *sa;
617 const struct sadb_address *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 uint16_t proto;
619 unsigned short family;
620 xfrm_address_t *xaddr;
621
Joe Perchesea110732011-06-13 16:21:26 +0000622 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (sa == NULL)
624 return NULL;
625
626 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
627 if (proto == 0)
628 return NULL;
629
630 /* sadb_address_len should be checked by caller */
Joe Perchesea110732011-06-13 16:21:26 +0000631 addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (addr == NULL)
633 return NULL;
634
David S. Miller4c93fbb2011-02-25 18:07:06 -0800635 family = ((const struct sockaddr *)(addr + 1))->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 switch (family) {
637 case AF_INET:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800638 xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000640#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 case AF_INET6:
David S. Miller4c93fbb2011-02-25 18:07:06 -0800642 xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 break;
644#endif
645 default:
646 xaddr = NULL;
647 }
648
649 if (!xaddr)
650 return NULL;
651
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800652 return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657static int
658pfkey_sockaddr_size(sa_family_t family)
659{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +0900660 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700663static inline int pfkey_mode_from_xfrm(int mode)
664{
665 switch(mode) {
666 case XFRM_MODE_TRANSPORT:
667 return IPSEC_MODE_TRANSPORT;
668 case XFRM_MODE_TUNNEL:
669 return IPSEC_MODE_TUNNEL;
670 case XFRM_MODE_BEET:
671 return IPSEC_MODE_BEET;
672 default:
673 return -1;
674 }
675}
676
677static inline int pfkey_mode_to_xfrm(int mode)
678{
679 switch(mode) {
680 case IPSEC_MODE_ANY: /*XXX*/
681 case IPSEC_MODE_TRANSPORT:
682 return XFRM_MODE_TRANSPORT;
683 case IPSEC_MODE_TUNNEL:
684 return XFRM_MODE_TUNNEL;
685 case IPSEC_MODE_BEET:
686 return XFRM_MODE_BEET;
687 default:
688 return -1;
689 }
690}
691
David S. Miller183cad12011-02-24 00:28:01 -0500692static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
David S. Miller4c93fbb2011-02-25 18:07:06 -0800693 struct sockaddr *sa,
694 unsigned short family)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900695{
696 switch (family) {
697 case AF_INET:
698 {
699 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
700 sin->sin_family = AF_INET;
701 sin->sin_port = port;
702 sin->sin_addr.s_addr = xaddr->a4;
703 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
704 return 32;
705 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000706#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900707 case AF_INET6:
708 {
709 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
710 sin6->sin6_family = AF_INET6;
711 sin6->sin6_port = port;
712 sin6->sin6_flowinfo = 0;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000713 sin6->sin6_addr = *(struct in6_addr *)xaddr->a6;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900714 sin6->sin6_scope_id = 0;
715 return 128;
716 }
717#endif
718 }
719 return 0;
720}
721
David S. Miller4c93fbb2011-02-25 18:07:06 -0800722static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -0700723 int add_keys, int hsc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 struct sk_buff *skb;
726 struct sadb_msg *hdr;
727 struct sadb_sa *sa;
728 struct sadb_lifetime *lifetime;
729 struct sadb_address *addr;
730 struct sadb_key *key;
731 struct sadb_x_sa2 *sa2;
Trent Jaegerdf718372005-12-13 23:12:27 -0800732 struct sadb_x_sec_ctx *sec_ctx;
733 struct xfrm_sec_ctx *xfrm_ctx;
734 int ctx_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int size;
736 int auth_key_size = 0;
737 int encrypt_key_size = 0;
738 int sockaddr_size;
739 struct xfrm_encap_tmpl *natt = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700740 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 /* address family check */
743 sockaddr_size = pfkey_sockaddr_size(x->props.family);
744 if (!sockaddr_size)
745 return ERR_PTR(-EINVAL);
746
747 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
748 key(AE), (identity(SD),) (sensitivity)> */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900749 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 sizeof(struct sadb_lifetime) +
751 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
752 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900753 sizeof(struct sadb_address)*2 +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 sockaddr_size*2 +
755 sizeof(struct sadb_x_sa2);
Trent Jaegerdf718372005-12-13 23:12:27 -0800756
757 if ((xfrm_ctx = x->security)) {
758 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
759 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
760 }
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* identity & sensitivity */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000763 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 size += sizeof(struct sadb_address) + sockaddr_size;
765
766 if (add_keys) {
767 if (x->aalg && x->aalg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900768 auth_key_size =
769 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 size += sizeof(struct sadb_key) + auth_key_size;
771 }
772 if (x->ealg && x->ealg->alg_key_len) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900773 encrypt_key_size =
774 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 size += sizeof(struct sadb_key) + encrypt_key_size;
776 }
777 }
778 if (x->encap)
779 natt = x->encap;
780
781 if (natt && natt->encap_type) {
782 size += sizeof(struct sadb_x_nat_t_type);
783 size += sizeof(struct sadb_x_nat_t_port);
784 size += sizeof(struct sadb_x_nat_t_port);
785 }
786
787 skb = alloc_skb(size + 16, GFP_ATOMIC);
788 if (skb == NULL)
789 return ERR_PTR(-ENOBUFS);
790
791 /* call should fill header later */
792 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
793 memset(hdr, 0, size); /* XXX do we need this ? */
794 hdr->sadb_msg_len = size / sizeof(uint64_t);
795
796 /* sa */
797 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
798 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
799 sa->sadb_sa_exttype = SADB_EXT_SA;
800 sa->sadb_sa_spi = x->id.spi;
801 sa->sadb_sa_replay = x->props.replay_window;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700802 switch (x->km.state) {
803 case XFRM_STATE_VALID:
804 sa->sadb_sa_state = x->km.dying ?
805 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
806 break;
807 case XFRM_STATE_ACQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700809 break;
810 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 sa->sadb_sa_state = SADB_SASTATE_DEAD;
Herbert Xu4f09f0b2005-06-18 22:43:43 -0700812 break;
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 sa->sadb_sa_auth = 0;
815 if (x->aalg) {
816 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200817 sa->sadb_sa_auth = (a && a->pfkey_supported) ?
818 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820 sa->sadb_sa_encrypt = 0;
821 BUG_ON(x->ealg && x->calg);
822 if (x->ealg) {
823 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200824 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
825 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
828 if (x->calg) {
829 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +0200830 sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
831 a->desc.sadb_alg_id : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
834 sa->sadb_sa_flags = 0;
835 if (x->props.flags & XFRM_STATE_NOECN)
836 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
837 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
838 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -0700839 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
840 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* hard time */
843 if (hsc & 2) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900844 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 sizeof(struct sadb_lifetime));
846 lifetime->sadb_lifetime_len =
847 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
848 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
849 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
850 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
851 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
852 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
853 }
854 /* soft time */
855 if (hsc & 1) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900856 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 sizeof(struct sadb_lifetime));
858 lifetime->sadb_lifetime_len =
859 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
860 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
861 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
862 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
863 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
864 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
865 }
866 /* current time */
867 lifetime = (struct sadb_lifetime *) skb_put(skb,
868 sizeof(struct sadb_lifetime));
869 lifetime->sadb_lifetime_len =
870 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
871 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
872 lifetime->sadb_lifetime_allocations = x->curlft.packets;
873 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
874 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
875 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
876 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900877 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900879 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 (sizeof(struct sadb_address)+sockaddr_size)/
881 sizeof(uint64_t);
882 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900883 /* "if the ports are non-zero, then the sadb_address_proto field,
884 normally zero, MUST be filled in with the transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 protocol's number." - RFC2367 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900886 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900889 addr->sadb_address_prefixlen =
890 pfkey_sockaddr_fill(&x->props.saddr, 0,
891 (struct sockaddr *) (addr + 1),
892 x->props.family);
893 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 BUG();
895
896 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900897 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900899 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 (sizeof(struct sadb_address)+sockaddr_size)/
901 sizeof(uint64_t);
902 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900903 addr->sadb_address_proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 addr->sadb_address_reserved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900906 addr->sadb_address_prefixlen =
907 pfkey_sockaddr_fill(&x->id.daddr, 0,
908 (struct sockaddr *) (addr + 1),
909 x->props.family);
910 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 BUG();
912
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000913 if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
914 x->props.family)) {
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +0900915 addr = (struct sadb_address*) skb_put(skb,
916 sizeof(struct sadb_address)+sockaddr_size);
917 addr->sadb_address_len =
918 (sizeof(struct sadb_address)+sockaddr_size)/
919 sizeof(uint64_t);
920 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
921 addr->sadb_address_proto =
922 pfkey_proto_from_xfrm(x->sel.proto);
923 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
924 addr->sadb_address_reserved = 0;
925
926 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
927 (struct sockaddr *) (addr + 1),
928 x->props.family);
929 }
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* auth key */
932 if (add_keys && auth_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900933 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 sizeof(struct sadb_key)+auth_key_size);
935 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
936 sizeof(uint64_t);
937 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
938 key->sadb_key_bits = x->aalg->alg_key_len;
939 key->sadb_key_reserved = 0;
940 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
941 }
942 /* encrypt key */
943 if (add_keys && encrypt_key_size) {
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900944 key = (struct sadb_key *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 sizeof(struct sadb_key)+encrypt_key_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900946 key->sadb_key_len = (sizeof(struct sadb_key) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 encrypt_key_size) / sizeof(uint64_t);
948 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
949 key->sadb_key_bits = x->ealg->alg_key_len;
950 key->sadb_key_reserved = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +0900951 memcpy(key + 1, x->ealg->alg_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 (x->ealg->alg_key_len+7)/8);
953 }
954
955 /* sa */
956 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
957 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
958 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -0700959 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
960 kfree_skb(skb);
961 return ERR_PTR(-EINVAL);
962 }
963 sa2->sadb_x_sa2_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 sa2->sadb_x_sa2_reserved1 = 0;
965 sa2->sadb_x_sa2_reserved2 = 0;
966 sa2->sadb_x_sa2_sequence = 0;
967 sa2->sadb_x_sa2_reqid = x->props.reqid;
968
969 if (natt && natt->encap_type) {
970 struct sadb_x_nat_t_type *n_type;
971 struct sadb_x_nat_t_port *n_port;
972
973 /* type */
974 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
975 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
976 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
977 n_type->sadb_x_nat_t_type_type = natt->encap_type;
978 n_type->sadb_x_nat_t_type_reserved[0] = 0;
979 n_type->sadb_x_nat_t_type_reserved[1] = 0;
980 n_type->sadb_x_nat_t_type_reserved[2] = 0;
981
982 /* source port */
983 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
984 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
985 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
986 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
987 n_port->sadb_x_nat_t_port_reserved = 0;
988
989 /* dest port */
990 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
991 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
992 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
993 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
994 n_port->sadb_x_nat_t_port_reserved = 0;
995 }
996
Trent Jaegerdf718372005-12-13 23:12:27 -0800997 /* security context */
998 if (xfrm_ctx) {
999 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1000 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1001 sec_ctx->sadb_x_sec_len =
1002 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1003 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1004 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1005 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1006 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1007 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1008 xfrm_ctx->ctx_len);
1009 }
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return skb;
1012}
1013
Herbert Xu050f0092007-10-09 13:31:47 -07001014
David S. Miller4c93fbb2011-02-25 18:07:06 -08001015static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
Herbert Xu050f0092007-10-09 13:31:47 -07001016{
1017 struct sk_buff *skb;
1018
Herbert Xu050f0092007-10-09 13:31:47 -07001019 skb = __pfkey_xfrm_state2msg(x, 1, 3);
Herbert Xu050f0092007-10-09 13:31:47 -07001020
1021 return skb;
1022}
1023
David S. Miller4c93fbb2011-02-25 18:07:06 -08001024static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
Herbert Xu050f0092007-10-09 13:31:47 -07001025 int hsc)
1026{
1027 return __pfkey_xfrm_state2msg(x, 0, hsc);
1028}
1029
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001030static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
David S. Miller4c93fbb2011-02-25 18:07:06 -08001031 const struct sadb_msg *hdr,
1032 void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001034 struct xfrm_state *x;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001035 const struct sadb_lifetime *lifetime;
1036 const struct sadb_sa *sa;
1037 const struct sadb_key *key;
1038 const struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 uint16_t proto;
1040 int err;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Joe Perchesea110732011-06-13 16:21:26 +00001043 sa = ext_hdrs[SADB_EXT_SA - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (!sa ||
1045 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1046 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1047 return ERR_PTR(-EINVAL);
1048 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1049 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1050 return ERR_PTR(-EINVAL);
1051 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1052 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1053 return ERR_PTR(-EINVAL);
1054 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1055 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1056 return ERR_PTR(-EINVAL);
1057
1058 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1059 if (proto == 0)
1060 return ERR_PTR(-EINVAL);
1061
1062 /* default error is no buffer space */
1063 err = -ENOBUFS;
1064
1065 /* RFC2367:
1066
1067 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1068 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1069 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1070 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1071 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1072 not true.
1073
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001074 However, KAME setkey always uses SADB_SASTATE_LARVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1076 */
1077 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1078 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1079 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1080 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1081 return ERR_PTR(-EINVAL);
Joe Perchesea110732011-06-13 16:21:26 +00001082 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (key != NULL &&
1084 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1085 ((key->sadb_key_bits+7) / 8 == 0 ||
1086 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1087 return ERR_PTR(-EINVAL);
1088 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1089 if (key != NULL &&
1090 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1091 ((key->sadb_key_bits+7) / 8 == 0 ||
1092 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1093 return ERR_PTR(-EINVAL);
1094
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001095 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (x == NULL)
1097 return ERR_PTR(-ENOBUFS);
1098
1099 x->id.proto = proto;
1100 x->id.spi = sa->sadb_sa_spi;
1101 x->props.replay_window = sa->sadb_sa_replay;
1102 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1103 x->props.flags |= XFRM_STATE_NOECN;
1104 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1105 x->props.flags |= XFRM_STATE_DECAP_DSCP;
Herbert Xudd871472005-06-20 13:21:43 -07001106 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1107 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Joe Perchesea110732011-06-13 16:21:26 +00001109 lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (lifetime != NULL) {
1111 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1112 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1113 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1114 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1115 }
Joe Perchesea110732011-06-13 16:21:26 +00001116 lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (lifetime != NULL) {
1118 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1119 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1120 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1121 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1122 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001123
Joe Perchesea110732011-06-13 16:21:26 +00001124 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08001125 if (sec_ctx != NULL) {
1126 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1127
1128 if (!uctx)
1129 goto out;
1130
1131 err = security_xfrm_state_alloc(x, uctx);
1132 kfree(uctx);
1133
1134 if (err)
1135 goto out;
1136 }
1137
Joe Perchesea110732011-06-13 16:21:26 +00001138 key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (sa->sadb_sa_auth) {
1140 int keysize = 0;
1141 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001142 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 err = -ENOSYS;
1144 goto out;
1145 }
1146 if (key)
1147 keysize = (key->sadb_key_bits + 7) / 8;
1148 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1149 if (!x->aalg)
1150 goto out;
1151 strcpy(x->aalg->alg_name, a->name);
1152 x->aalg->alg_key_len = 0;
1153 if (key) {
1154 x->aalg->alg_key_len = key->sadb_key_bits;
1155 memcpy(x->aalg->alg_key, key+1, keysize);
1156 }
Martin Willic20a66f2009-12-09 06:11:15 +00001157 x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 x->props.aalgo = sa->sadb_sa_auth;
1159 /* x->algo.flags = sa->sadb_sa_flags; */
1160 }
1161 if (sa->sadb_sa_encrypt) {
1162 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1163 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001164 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 err = -ENOSYS;
1166 goto out;
1167 }
1168 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1169 if (!x->calg)
1170 goto out;
1171 strcpy(x->calg->alg_name, a->name);
1172 x->props.calgo = sa->sadb_sa_encrypt;
1173 } else {
1174 int keysize = 0;
1175 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001176 if (!a || !a->pfkey_supported) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 err = -ENOSYS;
1178 goto out;
1179 }
1180 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1181 if (key)
1182 keysize = (key->sadb_key_bits + 7) / 8;
1183 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1184 if (!x->ealg)
1185 goto out;
1186 strcpy(x->ealg->alg_name, a->name);
1187 x->ealg->alg_key_len = 0;
1188 if (key) {
1189 x->ealg->alg_key_len = key->sadb_key_bits;
1190 memcpy(x->ealg->alg_key, key+1, keysize);
1191 }
1192 x->props.ealgo = sa->sadb_sa_encrypt;
1193 }
1194 }
1195 /* x->algo.flags = sa->sadb_sa_flags; */
1196
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001197 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 &x->props.saddr);
1199 if (!x->props.family) {
1200 err = -EAFNOSUPPORT;
1201 goto out;
1202 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001203 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 &x->id.daddr);
1205
1206 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001207 const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001208 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1209 if (mode < 0) {
1210 err = -EINVAL;
1211 goto out;
1212 }
1213 x->props.mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 x->props.reqid = sa2->sadb_x_sa2_reqid;
1215 }
1216
1217 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001218 const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /* Nobody uses this, but we try. */
1221 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1222 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1223 }
1224
Kazunori MIYAZAWA4da51052008-05-21 13:26:11 -07001225 if (!x->sel.family)
Joy Latten4a4b6272007-08-02 19:25:43 -07001226 x->sel.family = x->props.family;
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001229 const struct sadb_x_nat_t_type* n_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 struct xfrm_encap_tmpl *natt;
1231
1232 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1233 if (!x->encap)
1234 goto out;
1235
1236 natt = x->encap;
1237 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1238 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1239
1240 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001241 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1243 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1244 }
1245 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001246 const struct sadb_x_nat_t_port *n_port =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1248 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1249 }
Timo Terasa8d694c2009-01-25 20:49:14 -08001250 memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252
Herbert Xu72cb6962005-06-20 13:18:08 -07001253 err = xfrm_init_state(x);
1254 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 goto out;
Herbert Xu72cb6962005-06-20 13:18:08 -07001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 x->km.seq = hdr->sadb_msg_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return x;
1259
1260out:
1261 x->km.state = XFRM_STATE_DEAD;
1262 xfrm_state_put(x);
1263 return ERR_PTR(err);
1264}
1265
David S. Miller4c93fbb2011-02-25 18:07:06 -08001266static 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 -07001267{
1268 return -EOPNOTSUPP;
1269}
1270
David S. Miller4c93fbb2011-02-25 18:07:06 -08001271static 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 -07001272{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001273 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct sk_buff *resp_skb;
1275 struct sadb_x_sa2 *sa2;
1276 struct sadb_address *saddr, *daddr;
1277 struct sadb_msg *out_hdr;
Herbert Xu658b2192007-10-09 13:29:52 -07001278 struct sadb_spirange *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 struct xfrm_state *x = NULL;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001280 int mode;
Herbert Xu658b2192007-10-09 13:29:52 -07001281 int err;
1282 u32 min_spi, max_spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 u32 reqid;
1284 u8 proto;
1285 unsigned short family;
1286 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1287
1288 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1289 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1290 return -EINVAL;
1291
1292 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1293 if (proto == 0)
1294 return -EINVAL;
1295
1296 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001297 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1298 if (mode < 0)
1299 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 reqid = sa2->sadb_x_sa2_reqid;
1301 } else {
1302 mode = 0;
1303 reqid = 0;
1304 }
1305
1306 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1307 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1308
1309 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1310 switch (family) {
1311 case AF_INET:
1312 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1313 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1314 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001315#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 case AF_INET6:
1317 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1318 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1319 break;
1320#endif
1321 }
1322
1323 if (hdr->sadb_msg_seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001324 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001325 if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 xfrm_state_put(x);
1327 x = NULL;
1328 }
1329 }
1330
1331 if (!x)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001332 x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 if (x == NULL)
1335 return -ENOENT;
1336
Herbert Xu658b2192007-10-09 13:29:52 -07001337 min_spi = 0x100;
1338 max_spi = 0x0fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Herbert Xu658b2192007-10-09 13:29:52 -07001340 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1341 if (range) {
1342 min_spi = range->sadb_spirange_min;
1343 max_spi = range->sadb_spirange_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
Herbert Xu658b2192007-10-09 13:29:52 -07001345
1346 err = xfrm_alloc_spi(x, min_spi, max_spi);
Herbert Xu050f0092007-10-09 13:31:47 -07001347 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 if (IS_ERR(resp_skb)) {
1350 xfrm_state_put(x);
1351 return PTR_ERR(resp_skb);
1352 }
1353
1354 out_hdr = (struct sadb_msg *) resp_skb->data;
1355 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1356 out_hdr->sadb_msg_type = SADB_GETSPI;
1357 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1358 out_hdr->sadb_msg_errno = 0;
1359 out_hdr->sadb_msg_reserved = 0;
1360 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1361 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1362
1363 xfrm_state_put(x);
1364
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001365 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 return 0;
1368}
1369
David S. Miller4c93fbb2011-02-25 18:07:06 -08001370static 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 -07001371{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001372 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct xfrm_state *x;
1374
1375 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1376 return -EOPNOTSUPP;
1377
1378 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1379 return 0;
1380
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001381 x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (x == NULL)
1383 return 0;
1384
1385 spin_lock_bh(&x->lock);
1386 if (x->km.state == XFRM_STATE_ACQ) {
1387 x->km.state = XFRM_STATE_ERROR;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001388 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390 spin_unlock_bh(&x->lock);
1391 xfrm_state_put(x);
1392 return 0;
1393}
1394
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001395static inline int event2poltype(int event)
1396{
1397 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001398 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001399 return SADB_X_SPDDELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001400 case XFRM_MSG_NEWPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001401 return SADB_X_SPDADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001402 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001403 return SADB_X_SPDUPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001404 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001405 // return SADB_X_SPDEXPIRE;
1406 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001407 pr_err("pfkey: Unknown policy event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001408 break;
1409 }
1410
1411 return 0;
1412}
1413
1414static inline int event2keytype(int event)
1415{
1416 switch (event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07001417 case XFRM_MSG_DELSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001418 return SADB_DELETE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001419 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001420 return SADB_ADD;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001421 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001422 return SADB_UPDATE;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001423 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001424 return SADB_EXPIRE;
1425 default:
stephen hemminger207024b2010-05-12 06:37:07 +00001426 pr_err("pfkey: Unknown SA event %d\n", event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001427 break;
1428 }
1429
1430 return 0;
1431}
1432
1433/* ADD/UPD/DEL */
David S. Miller214e0052011-02-24 00:02:38 -05001434static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001435{
1436 struct sk_buff *skb;
1437 struct sadb_msg *hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001438
Herbert Xu050f0092007-10-09 13:31:47 -07001439 skb = pfkey_xfrm_state2msg(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001440
1441 if (IS_ERR(skb))
1442 return PTR_ERR(skb);
1443
1444 hdr = (struct sadb_msg *) skb->data;
1445 hdr->sadb_msg_version = PF_KEY_V2;
1446 hdr->sadb_msg_type = event2keytype(c->event);
1447 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1448 hdr->sadb_msg_errno = 0;
1449 hdr->sadb_msg_reserved = 0;
1450 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001451 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001452
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001453 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001454
1455 return 0;
1456}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
David S. Miller4c93fbb2011-02-25 18:07:06 -08001458static 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 -07001459{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001460 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 struct xfrm_state *x;
1462 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001463 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001465 x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (IS_ERR(x))
1467 return PTR_ERR(x);
1468
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001469 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (hdr->sadb_msg_type == SADB_ADD)
1471 err = xfrm_state_add(x);
1472 else
1473 err = xfrm_state_update(x);
1474
Joy Lattenab5f5e82007-09-17 11:51:22 -07001475 xfrm_audit_state_add(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001476 audit_get_loginuid(current),
1477 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (err < 0) {
1480 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -08001481 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001482 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001485 if (hdr->sadb_msg_type == SADB_ADD)
Herbert Xuf60f6b82005-06-18 22:44:37 -07001486 c.event = XFRM_MSG_NEWSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001487 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07001488 c.event = XFRM_MSG_UPDSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001489 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001490 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001491 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -07001492out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001493 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001494 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
David S. Miller4c93fbb2011-02-25 18:07:06 -08001497static int pfkey_delete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001499 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 struct xfrm_state *x;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001501 struct km_event c;
1502 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 if (!ext_hdrs[SADB_EXT_SA-1] ||
1505 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1506 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1507 return -EINVAL;
1508
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001509 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (x == NULL)
1511 return -ESRCH;
1512
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001513 if ((err = security_xfrm_state_delete(x)))
1514 goto out;
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001517 err = -EPERM;
1518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
Joy Latten161a09e2006-11-27 13:11:54 -06001520
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001521 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -06001522
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001523 if (err < 0)
1524 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001525
1526 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001527 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001528 c.event = XFRM_MSG_DELSA;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001529 km_state_notify(x, &c);
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001530out:
Joy Lattenab5f5e82007-09-17 11:51:22 -07001531 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04001532 audit_get_loginuid(current),
1533 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 xfrm_state_put(x);
1535
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001536 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
David S. Miller4c93fbb2011-02-25 18:07:06 -08001539static 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 -07001540{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001541 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 __u8 proto;
1543 struct sk_buff *out_skb;
1544 struct sadb_msg *out_hdr;
1545 struct xfrm_state *x;
1546
1547 if (!ext_hdrs[SADB_EXT_SA-1] ||
1548 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1549 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1550 return -EINVAL;
1551
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001552 x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (x == NULL)
1554 return -ESRCH;
1555
Herbert Xu050f0092007-10-09 13:31:47 -07001556 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 proto = x->id.proto;
1558 xfrm_state_put(x);
1559 if (IS_ERR(out_skb))
1560 return PTR_ERR(out_skb);
1561
1562 out_hdr = (struct sadb_msg *) out_skb->data;
1563 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Charles Hardin435000b2007-11-22 19:35:15 +08001564 out_hdr->sadb_msg_type = SADB_GET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1566 out_hdr->sadb_msg_errno = 0;
1567 out_hdr->sadb_msg_reserved = 0;
1568 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1569 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001570 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 return 0;
1573}
1574
David S. Miller4c93fbb2011-02-25 18:07:06 -08001575static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
Al Virodd0fc662005-10-07 07:46:04 +01001576 gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 struct sk_buff *skb;
1579 struct sadb_msg *hdr;
1580 int len, auth_len, enc_len, i;
1581
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001582 auth_len = xfrm_count_pfkey_auth_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (auth_len) {
1584 auth_len *= sizeof(struct sadb_alg);
1585 auth_len += sizeof(struct sadb_supported);
1586 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001587
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001588 enc_len = xfrm_count_pfkey_enc_supported();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (enc_len) {
1590 enc_len *= sizeof(struct sadb_alg);
1591 enc_len += sizeof(struct sadb_supported);
1592 }
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 len = enc_len + auth_len + sizeof(struct sadb_msg);
1595
1596 skb = alloc_skb(len + 16, allocation);
1597 if (!skb)
1598 goto out_put_algs;
1599
1600 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1601 pfkey_hdr_dup(hdr, orig);
1602 hdr->sadb_msg_errno = 0;
1603 hdr->sadb_msg_len = len / sizeof(uint64_t);
1604
1605 if (auth_len) {
1606 struct sadb_supported *sp;
1607 struct sadb_alg *ap;
1608
1609 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1610 ap = (struct sadb_alg *) (sp + 1);
1611
1612 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1613 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1614
1615 for (i = 0; ; i++) {
1616 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1617 if (!aalg)
1618 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001619 if (!aalg->pfkey_supported)
1620 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (aalg->available)
1622 *ap++ = aalg->desc;
1623 }
1624 }
1625
1626 if (enc_len) {
1627 struct sadb_supported *sp;
1628 struct sadb_alg *ap;
1629
1630 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1631 ap = (struct sadb_alg *) (sp + 1);
1632
1633 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1634 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1635
1636 for (i = 0; ; i++) {
1637 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1638 if (!ealg)
1639 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02001640 if (!ealg->pfkey_supported)
1641 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (ealg->available)
1643 *ap++ = ealg->desc;
1644 }
1645 }
1646
1647out_put_algs:
1648 return skb;
1649}
1650
David S. Miller4c93fbb2011-02-25 18:07:06 -08001651static 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 -07001652{
1653 struct pfkey_sock *pfk = pfkey_sk(sk);
1654 struct sk_buff *supp_skb;
1655
1656 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1657 return -EINVAL;
1658
1659 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1660 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1661 return -EEXIST;
1662 pfk->registered |= (1<<hdr->sadb_msg_satype);
1663 }
1664
1665 xfrm_probe_algs();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1668 if (!supp_skb) {
1669 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1670 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1671
1672 return -ENOBUFS;
1673 }
1674
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001675 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 return 0;
1678}
1679
David S. Miller4c93fbb2011-02-25 18:07:06 -08001680static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001681{
1682 struct sk_buff *skb;
1683 struct sadb_msg *hdr;
1684
1685 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1686 if (!skb)
1687 return -ENOBUFS;
1688
1689 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1690 memcpy(hdr, ihdr, sizeof(struct sadb_msg));
1691 hdr->sadb_msg_errno = (uint8_t) 0;
1692 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1693
1694 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
1695}
1696
David S. Miller214e0052011-02-24 00:02:38 -05001697static int key_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001698{
1699 struct sk_buff *skb;
1700 struct sadb_msg *hdr;
1701
1702 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1703 if (!skb)
1704 return -ENOBUFS;
1705 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
Herbert Xubf088672005-06-18 22:44:00 -07001706 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08001707 hdr->sadb_msg_type = SADB_FLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001708 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001709 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001710 hdr->sadb_msg_version = PF_KEY_V2;
1711 hdr->sadb_msg_errno = (uint8_t) 0;
1712 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1713
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001714 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001715
1716 return 0;
1717}
1718
David S. Miller4c93fbb2011-02-25 18:07:06 -08001719static 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 -07001720{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001721 struct net *net = sock_net(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +00001722 unsigned int proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001723 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06001724 struct xfrm_audit audit_info;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001725 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1728 if (proto == 0)
1729 return -EINVAL;
1730
Al Viro0c11b942008-01-10 04:20:52 -05001731 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04001732 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06001733 audit_info.secid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001734 err = xfrm_state_flush(net, proto, &audit_info);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001735 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001736 if (err || err2) {
1737 if (err == -ESRCH) /* empty table - go quietly */
1738 err = 0;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001739 return err ? err : err2;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001740 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00001741
Herbert Xubf088672005-06-18 22:44:00 -07001742 c.data.proto = proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001743 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001744 c.portid = hdr->sadb_msg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001745 c.event = XFRM_MSG_FLUSHSA;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001746 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001747 km_state_notify(NULL, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 return 0;
1750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1753{
Timo Teras83321d62008-03-03 23:40:12 -08001754 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 struct sk_buff *out_skb;
1756 struct sadb_msg *out_hdr;
1757
Timo Teras83321d62008-03-03 23:40:12 -08001758 if (!pfkey_can_dump(&pfk->sk))
1759 return -ENOBUFS;
1760
Herbert Xu050f0092007-10-09 13:31:47 -07001761 out_skb = pfkey_xfrm_state2msg(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (IS_ERR(out_skb))
1763 return PTR_ERR(out_skb);
1764
1765 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08001766 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 out_hdr->sadb_msg_type = SADB_DUMP;
1768 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1769 out_hdr->sadb_msg_errno = 0;
1770 out_hdr->sadb_msg_reserved = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07001771 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001772 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07001773
1774 if (pfk->dump.skb)
1775 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001776 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07001777 pfk->dump.skb = out_skb;
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return 0;
1780}
1781
Timo Teras83321d62008-03-03 23:40:12 -08001782static int pfkey_dump_sa(struct pfkey_sock *pfk)
1783{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001784 struct net *net = sock_net(&pfk->sk);
1785 return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08001786}
1787
1788static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1789{
1790 xfrm_state_walk_done(&pfk->dump.u.state);
1791}
1792
David S. Miller4c93fbb2011-02-25 18:07:06 -08001793static 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 -07001794{
1795 u8 proto;
Timo Teras83321d62008-03-03 23:40:12 -08001796 struct pfkey_sock *pfk = pfkey_sk(sk);
1797
1798 if (pfk->dump.dump != NULL)
1799 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1802 if (proto == 0)
1803 return -EINVAL;
1804
Timo Teras83321d62008-03-03 23:40:12 -08001805 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001806 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08001807 pfk->dump.dump = pfkey_dump_sa;
1808 pfk->dump.done = pfkey_dump_sa_done;
1809 xfrm_state_walk_init(&pfk->dump.u.state, proto);
Timo Teras4c563f72008-02-28 21:31:08 -08001810
Timo Teras83321d62008-03-03 23:40:12 -08001811 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
David S. Miller4c93fbb2011-02-25 18:07:06 -08001814static 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 -07001815{
1816 struct pfkey_sock *pfk = pfkey_sk(sk);
1817 int satype = hdr->sadb_msg_satype;
David S. Miller4c93fbb2011-02-25 18:07:06 -08001818 bool reset_errno = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08001821 reset_errno = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (satype != 0 && satype != 1)
1823 return -EINVAL;
1824 pfk->promisc = satype;
1825 }
David S. Miller4c93fbb2011-02-25 18:07:06 -08001826 if (reset_errno && skb_cloned(skb))
1827 skb = skb_copy(skb, GFP_KERNEL);
1828 else
1829 skb = skb_clone(skb, GFP_KERNEL);
1830
1831 if (reset_errno && skb) {
1832 struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
1833 new_hdr->sadb_msg_errno = 0;
1834 }
1835
1836 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return 0;
1838}
1839
1840static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1841{
1842 int i;
1843 u32 reqid = *(u32*)ptr;
1844
1845 for (i=0; i<xp->xfrm_nr; i++) {
1846 if (xp->xfrm_vec[i].reqid == reqid)
1847 return -EEXIST;
1848 }
1849 return 0;
1850}
1851
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001852static u32 gen_reqid(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Timo Teras4c563f72008-02-28 21:31:08 -08001854 struct xfrm_policy_walk walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 u32 start;
Timo Teras4c563f72008-02-28 21:31:08 -08001856 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1858
1859 start = reqid;
1860 do {
1861 ++reqid;
1862 if (reqid == 0)
1863 reqid = IPSEC_MANUAL_REQID_MAX+1;
Timo Teras4c563f72008-02-28 21:31:08 -08001864 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001865 rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
Timo Teras4c563f72008-02-28 21:31:08 -08001866 xfrm_policy_walk_done(&walk);
1867 if (rc != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return reqid;
1869 } while (reqid != start);
1870 return 0;
1871}
1872
1873static int
1874parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1875{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001876 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001878 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1881 return -ELOOP;
1882
1883 if (rq->sadb_x_ipsecrequest_mode == 0)
1884 return -EINVAL;
1885
1886 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07001887 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1888 return -EINVAL;
1889 t->mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1891 t->optional = 1;
1892 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1893 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1894 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1895 t->reqid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08001896 if (!t->reqid && !(t->reqid = gen_reqid(net)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return -ENOBUFS;
1898 }
1899
1900 /* addresses present only in tunnel mode */
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001901 if (t->mode == XFRM_MODE_TUNNEL) {
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001902 u8 *sa = (u8 *) (rq + 1);
1903 int family, socklen;
1904
1905 family = pfkey_sockaddr_extract((struct sockaddr *)sa,
1906 &t->saddr);
1907 if (!family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return -EINVAL;
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09001909
1910 socklen = pfkey_sockaddr_len(family);
1911 if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen),
1912 &t->id.daddr) != family)
1913 return -EINVAL;
1914 t->encap_family = family;
Miika Komu2718aa72006-11-30 16:41:50 -08001915 } else
1916 t->encap_family = xp->family;
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 /* No way to set this via kame pfkey */
Herbert Xuc5d18e92008-04-22 00:46:42 -07001919 t->allalgs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 xp->xfrm_nr++;
1921 return 0;
1922}
1923
1924static int
1925parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1926{
1927 int err;
1928 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1929 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1930
Dan Carpenterf674e722012-09-27 22:21:19 +00001931 if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
1932 return -EINVAL;
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1935 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1936 return err;
1937 len -= rq->sadb_x_ipsecrequest_len;
1938 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1939 }
1940 return 0;
1941}
1942
David S. Miller4c93fbb2011-02-25 18:07:06 -08001943static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
Trent Jaegerdf718372005-12-13 23:12:27 -08001944{
1945 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1946
1947 if (xfrm_ctx) {
1948 int len = sizeof(struct sadb_x_sec_ctx);
1949 len += xfrm_ctx->ctx_len;
1950 return PFKEY_ALIGN8(len);
1951 }
1952 return 0;
1953}
1954
David S. Miller4c93fbb2011-02-25 18:07:06 -08001955static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
David S. Miller4c93fbb2011-02-25 18:07:06 -08001957 const struct xfrm_tmpl *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 int sockaddr_size = pfkey_sockaddr_size(xp->family);
Miika Komu2718aa72006-11-30 16:41:50 -08001959 int socklen = 0;
1960 int i;
1961
1962 for (i=0; i<xp->xfrm_nr; i++) {
1963 t = xp->xfrm_vec + i;
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09001964 socklen += pfkey_sockaddr_len(t->encap_family);
Miika Komu2718aa72006-11-30 16:41:50 -08001965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 return sizeof(struct sadb_msg) +
1968 (sizeof(struct sadb_lifetime) * 3) +
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09001969 (sizeof(struct sadb_address) * 2) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 (sockaddr_size * 2) +
1971 sizeof(struct sadb_x_policy) +
Miika Komu2718aa72006-11-30 16:41:50 -08001972 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1973 (socklen * 2) +
Trent Jaegerdf718372005-12-13 23:12:27 -08001974 pfkey_xfrm_policy2sec_ctx_size(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
David S. Miller4c93fbb2011-02-25 18:07:06 -08001977static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 struct sk_buff *skb;
1980 int size;
1981
1982 size = pfkey_xfrm_policy2msg_size(xp);
1983
1984 skb = alloc_skb(size + 16, GFP_ATOMIC);
1985 if (skb == NULL)
1986 return ERR_PTR(-ENOBUFS);
1987
1988 return skb;
1989}
1990
David S. Miller4c93fbb2011-02-25 18:07:06 -08001991static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 struct sadb_msg *hdr;
1994 struct sadb_address *addr;
1995 struct sadb_lifetime *lifetime;
1996 struct sadb_x_policy *pol;
Trent Jaegerdf718372005-12-13 23:12:27 -08001997 struct sadb_x_sec_ctx *sec_ctx;
1998 struct xfrm_sec_ctx *xfrm_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 int i;
2000 int size;
2001 int sockaddr_size = pfkey_sockaddr_size(xp->family);
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002002 int socklen = pfkey_sockaddr_len(xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 size = pfkey_xfrm_policy2msg_size(xp);
2005
2006 /* call should fill header later */
2007 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2008 memset(hdr, 0, size); /* XXX do we need this ? */
2009
2010 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002011 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002013 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 (sizeof(struct sadb_address)+sockaddr_size)/
2015 sizeof(uint64_t);
2016 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2017 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2018 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2019 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002020 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2021 xp->selector.sport,
2022 (struct sockaddr *) (addr + 1),
2023 xp->family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 BUG();
2025
2026 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002027 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 sizeof(struct sadb_address)+sockaddr_size);
2029 addr->sadb_address_len =
2030 (sizeof(struct sadb_address)+sockaddr_size)/
2031 sizeof(uint64_t);
2032 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2033 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002034 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002036
2037 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2038 (struct sockaddr *) (addr + 1),
2039 xp->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 /* hard time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002042 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 sizeof(struct sadb_lifetime));
2044 lifetime->sadb_lifetime_len =
2045 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2046 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2047 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2048 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2049 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2050 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2051 /* soft time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002052 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 sizeof(struct sadb_lifetime));
2054 lifetime->sadb_lifetime_len =
2055 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2056 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2057 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2058 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2059 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2060 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2061 /* current time */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002062 lifetime = (struct sadb_lifetime *) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 sizeof(struct sadb_lifetime));
2064 lifetime->sadb_lifetime_len =
2065 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2066 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2067 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2068 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2069 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2070 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2071
2072 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2073 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2074 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2075 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2076 if (xp->action == XFRM_POLICY_ALLOW) {
2077 if (xp->xfrm_nr)
2078 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2079 else
2080 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2081 }
2082 pol->sadb_x_policy_dir = dir+1;
2083 pol->sadb_x_policy_id = xp->index;
2084 pol->sadb_x_policy_priority = xp->priority;
2085
2086 for (i=0; i<xp->xfrm_nr; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002087 const struct xfrm_tmpl *t = xp->xfrm_vec + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 struct sadb_x_ipsecrequest *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 int req_size;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002090 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 req_size = sizeof(struct sadb_x_ipsecrequest);
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002093 if (t->mode == XFRM_MODE_TUNNEL) {
2094 socklen = pfkey_sockaddr_len(t->encap_family);
2095 req_size += socklen * 2;
2096 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 size -= 2*socklen;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 rq = (void*)skb_put(skb, req_size);
2100 pol->sadb_x_policy_len += req_size/8;
2101 memset(rq, 0, sizeof(*rq));
2102 rq->sadb_x_ipsecrequest_len = req_size;
2103 rq->sadb_x_ipsecrequest_proto = t->id.proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002104 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2105 return -EINVAL;
David S. Millerfefaa752007-04-17 21:48:10 -07002106 rq->sadb_x_ipsecrequest_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2108 if (t->reqid)
2109 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2110 if (t->optional)
2111 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2112 rq->sadb_x_ipsecrequest_reqid = t->reqid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09002114 if (t->mode == XFRM_MODE_TUNNEL) {
2115 u8 *sa = (void *)(rq + 1);
2116 pfkey_sockaddr_fill(&t->saddr, 0,
2117 (struct sockaddr *)sa,
2118 t->encap_family);
2119 pfkey_sockaddr_fill(&t->id.daddr, 0,
2120 (struct sockaddr *) (sa + socklen),
2121 t->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
2123 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002124
2125 /* security context */
2126 if ((xfrm_ctx = xp->security)) {
2127 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2128
2129 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2130 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2131 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2132 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2133 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2134 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2135 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2136 xfrm_ctx->ctx_len);
2137 }
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 hdr->sadb_msg_len = size / sizeof(uint64_t);
2140 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002141
2142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
David S. Miller214e0052011-02-24 00:02:38 -05002145static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002146{
2147 struct sk_buff *out_skb;
2148 struct sadb_msg *out_hdr;
2149 int err;
2150
2151 out_skb = pfkey_xfrm_policy2msg_prep(xp);
Dan Carpenter9a127aa2010-03-24 01:47:00 +00002152 if (IS_ERR(out_skb))
2153 return PTR_ERR(out_skb);
2154
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002155 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2156 if (err < 0)
2157 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002158
2159 out_hdr = (struct sadb_msg *) out_skb->data;
2160 out_hdr->sadb_msg_version = PF_KEY_V2;
2161
Herbert Xuf60f6b82005-06-18 22:44:37 -07002162 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002163 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2164 else
2165 out_hdr->sadb_msg_type = event2poltype(c->event);
2166 out_hdr->sadb_msg_errno = 0;
2167 out_hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002168 out_hdr->sadb_msg_pid = c->portid;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002169 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002170 return 0;
2171
2172}
2173
David S. Miller4c93fbb2011-02-25 18:07:06 -08002174static 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 -07002175{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002176 struct net *net = sock_net(sk);
Trent Jaegerdf718372005-12-13 23:12:27 -08002177 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 struct sadb_lifetime *lifetime;
2179 struct sadb_address *sa;
2180 struct sadb_x_policy *pol;
2181 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002182 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002183 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2186 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2187 !ext_hdrs[SADB_X_EXT_POLICY-1])
2188 return -EINVAL;
2189
2190 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2191 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2192 return -EINVAL;
2193 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2194 return -EINVAL;
2195
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002196 xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (xp == NULL)
2198 return -ENOBUFS;
2199
2200 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2201 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2202 xp->priority = pol->sadb_x_policy_priority;
2203
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002204 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2206 if (!xp->family) {
2207 err = -EINVAL;
2208 goto out;
2209 }
2210 xp->selector.family = xp->family;
2211 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2212 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2213 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2214 if (xp->selector.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002215 xp->selector.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002217 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2219 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2220
2221 /* Amusing, we set this twice. KAME apps appear to set same value
2222 * in both addresses.
2223 */
2224 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2225
2226 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2227 if (xp->selector.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002228 xp->selector.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Joe Perchesea110732011-06-13 16:21:26 +00002230 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002231 if (sec_ctx != NULL) {
2232 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2233
2234 if (!uctx) {
2235 err = -ENOBUFS;
2236 goto out;
2237 }
2238
Paul Moore03e1ad72008-04-12 19:07:52 -07002239 err = security_xfrm_policy_alloc(&xp->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002240 kfree(uctx);
2241
2242 if (err)
2243 goto out;
2244 }
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 xp->lft.soft_byte_limit = XFRM_INF;
2247 xp->lft.hard_byte_limit = XFRM_INF;
2248 xp->lft.soft_packet_limit = XFRM_INF;
2249 xp->lft.hard_packet_limit = XFRM_INF;
2250 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2251 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2252 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2253 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2254 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2255 }
2256 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2257 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2258 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2259 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2260 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2261 }
2262 xp->xfrm_nr = 0;
2263 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2264 (err = parse_ipsecrequests(xp, pol)) < 0)
2265 goto out;
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2268 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
Trent Jaegerdf718372005-12-13 23:12:27 -08002269
Joy Lattenab5f5e82007-09-17 11:51:22 -07002270 xfrm_audit_policy_add(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002271 audit_get_loginuid(current),
2272 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -06002273
Trent Jaegerdf718372005-12-13 23:12:27 -08002274 if (err)
2275 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002277 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
Herbert Xuf60f6b82005-06-18 22:44:37 -07002278 c.event = XFRM_MSG_UPDPOLICY;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002279 else
Herbert Xuf60f6b82005-06-18 22:44:37 -07002280 c.event = XFRM_MSG_NEWPOLICY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002282 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002283 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002284
2285 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return 0;
2288
2289out:
Herbert Xu12a169e2008-10-01 07:03:24 -07002290 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08002291 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return err;
2293}
2294
David S. Miller4c93fbb2011-02-25 18:07:06 -08002295static 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 -07002296{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002297 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 int err;
2299 struct sadb_address *sa;
2300 struct sadb_x_policy *pol;
Paul Moore03e1ad72008-04-12 19:07:52 -07002301 struct xfrm_policy *xp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 struct xfrm_selector sel;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002303 struct km_event c;
Trent Jaegerdf718372005-12-13 23:12:27 -08002304 struct sadb_x_sec_ctx *sec_ctx;
Brian Haley2db3e472008-04-24 20:38:31 -07002305 struct xfrm_sec_ctx *pol_ctx = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2308 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2309 !ext_hdrs[SADB_X_EXT_POLICY-1])
2310 return -EINVAL;
2311
2312 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2313 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2314 return -EINVAL;
2315
2316 memset(&sel, 0, sizeof(sel));
2317
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002318 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2320 sel.prefixlen_s = sa->sadb_address_prefixlen;
2321 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2322 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2323 if (sel.sport)
Al Viro8f83f232006-09-27 18:46:11 -07002324 sel.sport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Junwei Zhangd0d79c32013-03-06 20:48:47 +00002326 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2328 sel.prefixlen_d = sa->sadb_address_prefixlen;
2329 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2330 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2331 if (sel.dport)
Al Viro8f83f232006-09-27 18:46:11 -07002332 sel.dport_mask = htons(0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Joe Perchesea110732011-06-13 16:21:26 +00002334 sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
Trent Jaegerdf718372005-12-13 23:12:27 -08002335 if (sec_ctx != NULL) {
2336 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2337
2338 if (!uctx)
2339 return -ENOMEM;
2340
Paul Moore03e1ad72008-04-12 19:07:52 -07002341 err = security_xfrm_policy_alloc(&pol_ctx, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002342 kfree(uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08002343 if (err)
2344 return err;
Brian Haley2db3e472008-04-24 20:38:31 -07002345 }
Trent Jaegerdf718372005-12-13 23:12:27 -08002346
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002347 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Paul Moore03e1ad72008-04-12 19:07:52 -07002348 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2349 1, &err);
2350 security_xfrm_policy_free(pol_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (xp == NULL)
2352 return -ENOENT;
2353
Joy Lattenab5f5e82007-09-17 11:51:22 -07002354 xfrm_audit_policy_delete(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002355 audit_get_loginuid(current),
2356 audit_get_sessionid(current), 0);
David S. Miller13fcfbb02007-02-12 13:53:54 -08002357
2358 if (err)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002359 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08002360
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002361 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002362 c.portid = hdr->sadb_msg_pid;
Tobias Brunner1839faa2008-10-10 14:07:03 -07002363 c.data.byid = 0;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002364 c.event = XFRM_MSG_DELPOLICY;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002365 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2366
Catherine Zhangc8c05a82006-06-08 23:39:49 -07002367out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002368 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00002369 if (err == 0)
2370 xfrm_garbage_collect(net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002371 return err;
2372}
2373
David S. Miller4c93fbb2011-02-25 18:07:06 -08002374static 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 -07002375{
2376 int err;
2377 struct sk_buff *out_skb;
2378 struct sadb_msg *out_hdr;
2379 err = 0;
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2382 if (IS_ERR(out_skb)) {
2383 err = PTR_ERR(out_skb);
2384 goto out;
2385 }
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002386 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2387 if (err < 0)
2388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 out_hdr = (struct sadb_msg *) out_skb->data;
2391 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002392 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 out_hdr->sadb_msg_satype = 0;
2394 out_hdr->sadb_msg_errno = 0;
2395 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2396 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002397 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 err = 0;
2399
2400out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return err;
2402}
2403
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002404#ifdef CONFIG_NET_KEY_MIGRATE
2405static int pfkey_sockaddr_pair_size(sa_family_t family)
2406{
YOSHIFUJI Hideaki9e8b4ed2008-04-28 00:50:45 +09002407 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002408}
2409
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002410static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002411 xfrm_address_t *saddr, xfrm_address_t *daddr,
2412 u16 *family)
2413{
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002414 int af, socklen;
2415
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002416 if (ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002417 return -EINVAL;
2418
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002419 af = pfkey_sockaddr_extract(sa, saddr);
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002420 if (!af)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002421 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002422
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002423 socklen = pfkey_sockaddr_len(af);
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002424 if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
YOSHIFUJI Hideaki5f95ac92008-04-28 02:46:24 +09002425 daddr) != af)
2426 return -EINVAL;
2427
2428 *family = af;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002429 return 0;
2430}
2431
2432static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2433 struct xfrm_migrate *m)
2434{
2435 int err;
2436 struct sadb_x_ipsecrequest *rq2;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002437 int mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002438
2439 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2440 len < rq1->sadb_x_ipsecrequest_len)
2441 return -EINVAL;
2442
2443 /* old endoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002444 err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
2445 rq1->sadb_x_ipsecrequest_len,
2446 &m->old_saddr, &m->old_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002447 &m->old_family);
2448 if (err)
2449 return err;
2450
2451 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2452 len -= rq1->sadb_x_ipsecrequest_len;
2453
2454 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2455 len < rq2->sadb_x_ipsecrequest_len)
2456 return -EINVAL;
2457
2458 /* new endpoints */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002459 err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
2460 rq2->sadb_x_ipsecrequest_len,
2461 &m->new_saddr, &m->new_daddr,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002462 &m->new_family);
2463 if (err)
2464 return err;
2465
2466 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2467 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2468 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2469 return -EINVAL;
2470
2471 m->proto = rq1->sadb_x_ipsecrequest_proto;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002472 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2473 return -EINVAL;
2474 m->mode = mode;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002475 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2476
2477 return ((int)(rq1->sadb_x_ipsecrequest_len +
2478 rq2->sadb_x_ipsecrequest_len));
2479}
2480
2481static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002482 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002483{
2484 int i, len, ret, err = -EINVAL;
2485 u8 dir;
2486 struct sadb_address *sa;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002487 struct sadb_x_kmaddress *kma;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002488 struct sadb_x_policy *pol;
2489 struct sadb_x_ipsecrequest *rq;
2490 struct xfrm_selector sel;
2491 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002492 struct xfrm_kmaddress k;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002493
2494 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002495 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002496 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2497 err = -EINVAL;
2498 goto out;
2499 }
2500
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002501 kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002502 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002503
2504 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2505 err = -EINVAL;
2506 goto out;
2507 }
2508
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002509 if (kma) {
2510 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2511 k.reserved = kma->sadb_x_kmaddress_reserved;
2512 ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2513 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2514 &k.local, &k.remote, &k.family);
2515 if (ret < 0) {
2516 err = ret;
2517 goto out;
2518 }
2519 }
2520
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002521 dir = pol->sadb_x_policy_dir - 1;
2522 memset(&sel, 0, sizeof(sel));
2523
2524 /* set source address info of selector */
2525 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2526 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2527 sel.prefixlen_s = sa->sadb_address_prefixlen;
2528 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2529 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2530 if (sel.sport)
Al Viro582ee432007-07-26 17:33:39 +01002531 sel.sport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002532
2533 /* set destination address info of selector */
2534 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2535 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2536 sel.prefixlen_d = sa->sadb_address_prefixlen;
2537 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2538 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2539 if (sel.dport)
Al Viro582ee432007-07-26 17:33:39 +01002540 sel.dport_mask = htons(0xffff);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002541
2542 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2543
2544 /* extract ipsecrequests */
2545 i = 0;
2546 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2547
2548 while (len > 0 && i < XFRM_MAX_DEPTH) {
2549 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2550 if (ret < 0) {
2551 err = ret;
2552 goto out;
2553 } else {
2554 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2555 len -= ret;
2556 i++;
2557 }
2558 }
2559
2560 if (!i || len > 0) {
2561 err = -EINVAL;
2562 goto out;
2563 }
2564
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002565 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
2566 kma ? &k : NULL);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002567
2568 out:
2569 return err;
2570}
2571#else
2572static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
Stephen Hemminger7f6daa62011-03-01 22:51:52 -08002573 const struct sadb_msg *hdr, void * const *ext_hdrs)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002574{
2575 return -ENOPROTOOPT;
2576}
2577#endif
2578
2579
David S. Miller4c93fbb2011-02-25 18:07:06 -08002580static 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 -07002581{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002582 struct net *net = sock_net(sk);
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002583 unsigned int dir;
Eric Paris215a2dd2007-03-07 16:01:45 -08002584 int err = 0, delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 struct sadb_x_policy *pol;
2586 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002587 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2590 return -EINVAL;
2591
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002592 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2593 if (dir >= XFRM_POLICY_MAX)
2594 return -EINVAL;
2595
Eric Paris215a2dd2007-03-07 16:01:45 -08002596 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00002597 xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002598 dir, pol->sadb_x_policy_id, delete, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 if (xp == NULL)
2600 return -ENOENT;
2601
Eric Paris215a2dd2007-03-07 16:01:45 -08002602 if (delete) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002603 xfrm_audit_policy_delete(xp, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -04002604 audit_get_loginuid(current),
2605 audit_get_sessionid(current), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Eric Paris215a2dd2007-03-07 16:01:45 -08002607 if (err)
2608 goto out;
2609 c.seq = hdr->sadb_msg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002610 c.portid = hdr->sadb_msg_pid;
Herbert Xubf088672005-06-18 22:44:00 -07002611 c.data.byid = 1;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002612 c.event = XFRM_MSG_DELPOLICY;
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002613 km_policy_notify(xp, dir, &c);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002614 } else {
Herbert Xu77d8d7a2005-10-05 12:15:12 -07002615 err = key_pol_get_resp(sk, xp, hdr, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Eric Paris215a2dd2007-03-07 16:01:45 -08002618out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00002620 if (delete && err == 0)
2621 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 return err;
2623}
2624
2625static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2626{
Timo Teras83321d62008-03-03 23:40:12 -08002627 struct pfkey_sock *pfk = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 struct sk_buff *out_skb;
2629 struct sadb_msg *out_hdr;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002630 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Timo Teras83321d62008-03-03 23:40:12 -08002632 if (!pfkey_can_dump(&pfk->sk))
2633 return -ENOBUFS;
2634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2636 if (IS_ERR(out_skb))
2637 return PTR_ERR(out_skb);
2638
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07002639 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2640 if (err < 0)
2641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 out_hdr = (struct sadb_msg *) out_skb->data;
Timo Teras83321d62008-03-03 23:40:12 -08002644 out_hdr->sadb_msg_version = pfk->dump.msg_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2646 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2647 out_hdr->sadb_msg_errno = 0;
Herbert Xu12a169e2008-10-01 07:03:24 -07002648 out_hdr->sadb_msg_seq = count + 1;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002649 out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
Herbert Xu12a169e2008-10-01 07:03:24 -07002650
2651 if (pfk->dump.skb)
2652 pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002653 &pfk->sk, sock_net(&pfk->sk));
Herbert Xu12a169e2008-10-01 07:03:24 -07002654 pfk->dump.skb = out_skb;
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return 0;
2657}
2658
Timo Teras83321d62008-03-03 23:40:12 -08002659static int pfkey_dump_sp(struct pfkey_sock *pfk)
2660{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002661 struct net *net = sock_net(&pfk->sk);
2662 return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
Timo Teras83321d62008-03-03 23:40:12 -08002663}
2664
2665static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2666{
2667 xfrm_policy_walk_done(&pfk->dump.u.policy);
2668}
2669
David S. Miller4c93fbb2011-02-25 18:07:06 -08002670static 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 -07002671{
Timo Teras83321d62008-03-03 23:40:12 -08002672 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Timo Teras83321d62008-03-03 23:40:12 -08002674 if (pfk->dump.dump != NULL)
2675 return -EBUSY;
Timo Teras4c563f72008-02-28 21:31:08 -08002676
Timo Teras83321d62008-03-03 23:40:12 -08002677 pfk->dump.msg_version = hdr->sadb_msg_version;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002678 pfk->dump.msg_portid = hdr->sadb_msg_pid;
Timo Teras83321d62008-03-03 23:40:12 -08002679 pfk->dump.dump = pfkey_dump_sp;
2680 pfk->dump.done = pfkey_dump_sp_done;
2681 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2682
2683 return pfkey_do_dump(pfk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
David S. Miller214e0052011-02-24 00:02:38 -05002686static int key_notify_policy_flush(const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
2688 struct sk_buff *skb_out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002689 struct sadb_msg *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002691 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (!skb_out)
2693 return -ENOBUFS;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002694 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
Jerome Borsboom151bb0f2006-01-24 12:57:19 -08002695 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002696 hdr->sadb_msg_seq = c->seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002697 hdr->sadb_msg_pid = c->portid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002698 hdr->sadb_msg_version = PF_KEY_V2;
2699 hdr->sadb_msg_errno = (uint8_t) 0;
Nicolas Dichtel85dfb742013-02-18 16:24:20 +01002700 hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002701 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002702 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002703 return 0;
2704
2705}
2706
David S. Miller4c93fbb2011-02-25 18:07:06 -08002707static 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 -07002708{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002709 struct net *net = sock_net(sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002710 struct km_event c;
Joy Latten161a09e2006-11-27 13:11:54 -06002711 struct xfrm_audit audit_info;
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002712 int err, err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Al Viro0c11b942008-01-10 04:20:52 -05002714 audit_info.loginuid = audit_get_loginuid(current);
Eric Paris25323862008-04-18 10:09:25 -04002715 audit_info.sessionid = audit_get_sessionid(current);
Joy Latten161a09e2006-11-27 13:11:54 -06002716 audit_info.secid = 0;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002717 err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002718 err2 = unicast_flush_resp(sk, hdr);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002719 if (err || err2) {
2720 if (err == -ESRCH) /* empty table - old silent behavior */
2721 return 0;
2722 return err;
2723 }
Jamal Hadi Salim8be987d2010-02-19 02:00:40 +00002724
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002725 c.data.type = XFRM_POLICY_TYPE_MAIN;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002726 c.event = XFRM_MSG_FLUSHPOLICY;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002727 c.portid = hdr->sadb_msg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002728 c.seq = hdr->sadb_msg_seq;
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002729 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002730 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
2732 return 0;
2733}
2734
2735typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
David S. Miller4c93fbb2011-02-25 18:07:06 -08002736 const struct sadb_msg *hdr, void * const *ext_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2738 [SADB_RESERVED] = pfkey_reserved,
2739 [SADB_GETSPI] = pfkey_getspi,
2740 [SADB_UPDATE] = pfkey_add,
2741 [SADB_ADD] = pfkey_add,
2742 [SADB_DELETE] = pfkey_delete,
2743 [SADB_GET] = pfkey_get,
2744 [SADB_ACQUIRE] = pfkey_acquire,
2745 [SADB_REGISTER] = pfkey_register,
2746 [SADB_EXPIRE] = NULL,
2747 [SADB_FLUSH] = pfkey_flush,
2748 [SADB_DUMP] = pfkey_dump,
2749 [SADB_X_PROMISC] = pfkey_promisc,
2750 [SADB_X_PCHANGE] = NULL,
2751 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2752 [SADB_X_SPDADD] = pfkey_spdadd,
2753 [SADB_X_SPDDELETE] = pfkey_spddelete,
2754 [SADB_X_SPDGET] = pfkey_spdget,
2755 [SADB_X_SPDACQUIRE] = NULL,
2756 [SADB_X_SPDDUMP] = pfkey_spddump,
2757 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2758 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2759 [SADB_X_SPDDELETE2] = pfkey_spdget,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08002760 [SADB_X_MIGRATE] = pfkey_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761};
2762
David S. Miller4c93fbb2011-02-25 18:07:06 -08002763static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
2765 void *ext_hdrs[SADB_EXT_MAX];
2766 int err;
2767
2768 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002769 BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2772 err = parse_exthdrs(skb, hdr, ext_hdrs);
2773 if (!err) {
2774 err = -EOPNOTSUPP;
2775 if (pfkey_funcs[hdr->sadb_msg_type])
2776 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2777 }
2778 return err;
2779}
2780
2781static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2782{
2783 struct sadb_msg *hdr = NULL;
2784
2785 if (skb->len < sizeof(*hdr)) {
2786 *errp = -EMSGSIZE;
2787 } else {
2788 hdr = (struct sadb_msg *) skb->data;
2789 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2790 hdr->sadb_msg_reserved != 0 ||
2791 (hdr->sadb_msg_type <= SADB_RESERVED ||
2792 hdr->sadb_msg_type > SADB_MAX)) {
2793 hdr = NULL;
2794 *errp = -EINVAL;
2795 } else if (hdr->sadb_msg_len != (skb->len /
2796 sizeof(uint64_t)) ||
2797 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2798 sizeof(uint64_t))) {
2799 hdr = NULL;
2800 *errp = -EMSGSIZE;
2801 } else {
2802 *errp = 0;
2803 }
2804 }
2805 return hdr;
2806}
2807
David S. Miller4c93fbb2011-02-25 18:07:06 -08002808static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
2809 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Herbert Xuf3980352007-12-19 23:44:29 -08002811 unsigned int id = d->desc.sadb_alg_id;
2812
2813 if (id >= sizeof(t->aalgos) * 8)
2814 return 0;
2815
2816 return (t->aalgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817}
2818
David S. Miller4c93fbb2011-02-25 18:07:06 -08002819static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
2820 const struct xfrm_algo_desc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
Herbert Xuf3980352007-12-19 23:44:29 -08002822 unsigned int id = d->desc.sadb_alg_id;
2823
2824 if (id >= sizeof(t->ealgos) * 8)
2825 return 0;
2826
2827 return (t->ealgos >> id) & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828}
2829
David S. Miller4c93fbb2011-02-25 18:07:06 -08002830static int count_ah_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
2832 int i, sz = 0;
2833
2834 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002835 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 if (!aalg)
2837 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002838 if (!aalg->pfkey_supported)
2839 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 if (aalg_tmpl_set(t, aalg) && aalg->available)
2841 sz += sizeof(struct sadb_comb);
2842 }
2843 return sz + sizeof(struct sadb_prop);
2844}
2845
David S. Miller4c93fbb2011-02-25 18:07:06 -08002846static int count_esp_combs(const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 int i, k, sz = 0;
2849
2850 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002851 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (!ealg)
2853 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002854
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002855 if (!ealg->pfkey_supported)
2856 continue;
2857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2859 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 for (k = 1; ; k++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002862 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (!aalg)
2864 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002865
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002866 if (!aalg->pfkey_supported)
2867 continue;
2868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (aalg_tmpl_set(t, aalg) && aalg->available)
2870 sz += sizeof(struct sadb_comb);
2871 }
2872 }
2873 return sz + sizeof(struct sadb_prop);
2874}
2875
David S. Miller4c93fbb2011-02-25 18:07:06 -08002876static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
2878 struct sadb_prop *p;
2879 int i;
2880
2881 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2882 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2883 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2884 p->sadb_prop_replay = 32;
2885 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2886
2887 for (i = 0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002888 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (!aalg)
2890 break;
2891
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002892 if (!aalg->pfkey_supported)
2893 continue;
2894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2896 struct sadb_comb *c;
2897 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2898 memset(c, 0, sizeof(*c));
2899 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2900 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2901 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2902 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2903 c->sadb_comb_hard_addtime = 24*60*60;
2904 c->sadb_comb_soft_addtime = 20*60*60;
2905 c->sadb_comb_hard_usetime = 8*60*60;
2906 c->sadb_comb_soft_usetime = 7*60*60;
2907 }
2908 }
2909}
2910
David S. Miller4c93fbb2011-02-25 18:07:06 -08002911static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
2913 struct sadb_prop *p;
2914 int i, k;
2915
2916 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2917 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2918 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2919 p->sadb_prop_replay = 32;
2920 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2921
2922 for (i=0; ; i++) {
David S. Miller4c93fbb2011-02-25 18:07:06 -08002923 const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 if (!ealg)
2925 break;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002926
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002927 if (!ealg->pfkey_supported)
2928 continue;
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2931 continue;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09002932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 for (k = 1; ; k++) {
2934 struct sadb_comb *c;
David S. Miller4c93fbb2011-02-25 18:07:06 -08002935 const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (!aalg)
2937 break;
Jussi Kivilinna7e50f842013-01-31 12:40:38 +02002938 if (!aalg->pfkey_supported)
2939 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2941 continue;
2942 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2943 memset(c, 0, sizeof(*c));
2944 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2945 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2946 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2947 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2948 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2949 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2950 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2951 c->sadb_comb_hard_addtime = 24*60*60;
2952 c->sadb_comb_soft_addtime = 20*60*60;
2953 c->sadb_comb_hard_usetime = 8*60*60;
2954 c->sadb_comb_soft_usetime = 7*60*60;
2955 }
2956 }
2957}
2958
David S. Miller214e0052011-02-24 00:02:38 -05002959static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002960{
2961 return 0;
2962}
2963
David S. Miller214e0052011-02-24 00:02:38 -05002964static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
2966 struct sk_buff *out_skb;
2967 struct sadb_msg *out_hdr;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002968 int hard;
2969 int hsc;
2970
Herbert Xubf088672005-06-18 22:44:00 -07002971 hard = c->data.hard;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002972 if (hard)
2973 hsc = 2;
2974 else
2975 hsc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Herbert Xu050f0092007-10-09 13:31:47 -07002977 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (IS_ERR(out_skb))
2979 return PTR_ERR(out_skb);
2980
2981 out_hdr = (struct sadb_msg *) out_skb->data;
2982 out_hdr->sadb_msg_version = PF_KEY_V2;
2983 out_hdr->sadb_msg_type = SADB_EXPIRE;
2984 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2985 out_hdr->sadb_msg_errno = 0;
2986 out_hdr->sadb_msg_reserved = 0;
2987 out_hdr->sadb_msg_seq = 0;
2988 out_hdr->sadb_msg_pid = 0;
2989
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002990 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 return 0;
2992}
2993
David S. Miller214e0052011-02-24 00:02:38 -05002994static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002995{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08002996 struct net *net = x ? xs_net(x) : c->net;
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08002997 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
2998
2999 if (atomic_read(&net_pfkey->socks_nr) == 0)
Jamal Hadi Salim99c6f602008-06-10 14:25:34 -07003000 return 0;
3001
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003002 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003003 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003004 return key_notify_sa_expire(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003005 case XFRM_MSG_DELSA:
3006 case XFRM_MSG_NEWSA:
3007 case XFRM_MSG_UPDSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003008 return key_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003009 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003010 return key_notify_sa_flush(c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08003011 case XFRM_MSG_NEWAE: /* not yet supported */
3012 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003013 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003014 pr_err("pfkey: Unknown SA event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003015 break;
3016 }
3017
3018 return 0;
3019}
3020
David S. Miller214e0052011-02-24 00:02:38 -05003021static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003022{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003023 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3024 return 0;
3025
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003026 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003027 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003028 return key_notify_policy_expire(xp, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003029 case XFRM_MSG_DELPOLICY:
3030 case XFRM_MSG_NEWPOLICY:
3031 case XFRM_MSG_UPDPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003032 return key_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003033 case XFRM_MSG_FLUSHPOLICY:
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07003034 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3035 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003036 return key_notify_policy_flush(c);
3037 default:
stephen hemminger207024b2010-05-12 06:37:07 +00003038 pr_err("pfkey: Unknown policy event %d\n", c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003039 break;
3040 }
3041
3042 return 0;
3043}
3044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045static u32 get_acqseq(void)
3046{
3047 u32 res;
Eric Dumazet28aecb92010-01-29 04:05:52 +00003048 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Eric Dumazet28aecb92010-01-29 04:05:52 +00003050 do {
3051 res = atomic_inc_return(&acqseq);
3052 } while (!res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 return res;
3054}
3055
Fan Du65e07362012-08-15 10:13:47 +08003056static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 struct sk_buff *skb;
3059 struct sadb_msg *hdr;
3060 struct sadb_address *addr;
3061 struct sadb_x_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 int sockaddr_size;
3063 int size;
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003064 struct sadb_x_sec_ctx *sec_ctx;
3065 struct xfrm_sec_ctx *xfrm_ctx;
3066 int ctx_size = 0;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3069 if (!sockaddr_size)
3070 return -EINVAL;
3071
3072 size = sizeof(struct sadb_msg) +
3073 (sizeof(struct sadb_address) * 2) +
3074 (sockaddr_size * 2) +
3075 sizeof(struct sadb_x_policy);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 if (x->id.proto == IPPROTO_AH)
3078 size += count_ah_combs(t);
3079 else if (x->id.proto == IPPROTO_ESP)
3080 size += count_esp_combs(t);
3081
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003082 if ((xfrm_ctx = x->security)) {
3083 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3084 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3085 }
3086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 skb = alloc_skb(size + 16, GFP_ATOMIC);
3088 if (skb == NULL)
3089 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3092 hdr->sadb_msg_version = PF_KEY_V2;
3093 hdr->sadb_msg_type = SADB_ACQUIRE;
3094 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3095 hdr->sadb_msg_len = size / sizeof(uint64_t);
3096 hdr->sadb_msg_errno = 0;
3097 hdr->sadb_msg_reserved = 0;
3098 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3099 hdr->sadb_msg_pid = 0;
3100
3101 /* src address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003102 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003104 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 (sizeof(struct sadb_address)+sockaddr_size)/
3106 sizeof(uint64_t);
3107 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3108 addr->sadb_address_proto = 0;
3109 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003110 addr->sadb_address_prefixlen =
3111 pfkey_sockaddr_fill(&x->props.saddr, 0,
3112 (struct sockaddr *) (addr + 1),
3113 x->props.family);
3114 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 BUG();
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 /* dst address */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003118 addr = (struct sadb_address*) skb_put(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 sizeof(struct sadb_address)+sockaddr_size);
3120 addr->sadb_address_len =
3121 (sizeof(struct sadb_address)+sockaddr_size)/
3122 sizeof(uint64_t);
3123 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3124 addr->sadb_address_proto = 0;
3125 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003126 addr->sadb_address_prefixlen =
3127 pfkey_sockaddr_fill(&x->id.daddr, 0,
3128 (struct sockaddr *) (addr + 1),
3129 x->props.family);
3130 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 BUG();
3132
3133 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3134 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3135 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3136 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
Fan Du65e07362012-08-15 10:13:47 +08003137 pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 pol->sadb_x_policy_id = xp->index;
3139
3140 /* Set sadb_comb's. */
3141 if (x->id.proto == IPPROTO_AH)
3142 dump_ah_combs(skb, t);
3143 else if (x->id.proto == IPPROTO_ESP)
3144 dump_esp_combs(skb, t);
3145
Venkat Yekkirala4e2ba182006-07-24 23:31:14 -07003146 /* security context */
3147 if (xfrm_ctx) {
3148 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3149 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3150 sec_ctx->sadb_x_sec_len =
3151 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3152 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3153 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3154 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3155 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3156 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3157 xfrm_ctx->ctx_len);
3158 }
3159
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003160 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
3162
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003163static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003164 u8 *data, int len, int *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003166 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 struct xfrm_policy *xp;
3168 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
Trent Jaegerdf718372005-12-13 23:12:27 -08003169 struct sadb_x_sec_ctx *sec_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003171 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 case AF_INET:
3173 if (opt != IP_IPSEC_POLICY) {
3174 *dir = -EOPNOTSUPP;
3175 return NULL;
3176 }
3177 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00003178#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 case AF_INET6:
3180 if (opt != IPV6_IPSEC_POLICY) {
3181 *dir = -EOPNOTSUPP;
3182 return NULL;
3183 }
3184 break;
3185#endif
3186 default:
3187 *dir = -EINVAL;
3188 return NULL;
3189 }
3190
3191 *dir = -EINVAL;
3192
3193 if (len < sizeof(struct sadb_x_policy) ||
3194 pol->sadb_x_policy_len*8 > len ||
3195 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3196 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3197 return NULL;
3198
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003199 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 if (xp == NULL) {
3201 *dir = -ENOBUFS;
3202 return NULL;
3203 }
3204
3205 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3206 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3207
3208 xp->lft.soft_byte_limit = XFRM_INF;
3209 xp->lft.hard_byte_limit = XFRM_INF;
3210 xp->lft.soft_packet_limit = XFRM_INF;
3211 xp->lft.hard_packet_limit = XFRM_INF;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003212 xp->family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
3214 xp->xfrm_nr = 0;
3215 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3216 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3217 goto out;
3218
Trent Jaegerdf718372005-12-13 23:12:27 -08003219 /* security context too */
3220 if (len >= (pol->sadb_x_policy_len*8 +
3221 sizeof(struct sadb_x_sec_ctx))) {
3222 char *p = (char *)pol;
3223 struct xfrm_user_sec_ctx *uctx;
3224
3225 p += pol->sadb_x_policy_len*8;
3226 sec_ctx = (struct sadb_x_sec_ctx *)p;
3227 if (len < pol->sadb_x_policy_len*8 +
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003228 sec_ctx->sadb_x_sec_len) {
3229 *dir = -EINVAL;
Trent Jaegerdf718372005-12-13 23:12:27 -08003230 goto out;
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07003231 }
Trent Jaegerdf718372005-12-13 23:12:27 -08003232 if ((*dir = verify_sec_ctx_len(p)))
3233 goto out;
3234 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
Paul Moore03e1ad72008-04-12 19:07:52 -07003235 *dir = security_xfrm_policy_alloc(&xp->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08003236 kfree(uctx);
3237
3238 if (*dir)
3239 goto out;
3240 }
3241
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 *dir = pol->sadb_x_policy_dir-1;
3243 return xp;
3244
3245out:
Alexey Dobriyan70e90672008-11-06 23:08:37 -08003246 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08003247 xfrm_policy_destroy(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 return NULL;
3249}
3250
Al Viro5d36b182006-11-08 00:24:06 -08003251static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
3253 struct sk_buff *skb;
3254 struct sadb_msg *hdr;
3255 struct sadb_sa *sa;
3256 struct sadb_address *addr;
3257 struct sadb_x_nat_t_port *n_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 int sockaddr_size;
3259 int size;
3260 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3261 struct xfrm_encap_tmpl *natt = NULL;
3262
3263 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3264 if (!sockaddr_size)
3265 return -EINVAL;
3266
3267 if (!satype)
3268 return -EINVAL;
3269
3270 if (!x->encap)
3271 return -EINVAL;
3272
3273 natt = x->encap;
3274
3275 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3276 *
3277 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3278 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3279 */
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 size = sizeof(struct sadb_msg) +
3282 sizeof(struct sadb_sa) +
3283 (sizeof(struct sadb_address) * 2) +
3284 (sockaddr_size * 2) +
3285 (sizeof(struct sadb_x_nat_t_port) * 2);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 skb = alloc_skb(size + 16, GFP_ATOMIC);
3288 if (skb == NULL)
3289 return -ENOMEM;
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3292 hdr->sadb_msg_version = PF_KEY_V2;
3293 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3294 hdr->sadb_msg_satype = satype;
3295 hdr->sadb_msg_len = size / sizeof(uint64_t);
3296 hdr->sadb_msg_errno = 0;
3297 hdr->sadb_msg_reserved = 0;
3298 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3299 hdr->sadb_msg_pid = 0;
3300
3301 /* SA */
3302 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3303 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3304 sa->sadb_sa_exttype = SADB_EXT_SA;
3305 sa->sadb_sa_spi = x->id.spi;
3306 sa->sadb_sa_replay = 0;
3307 sa->sadb_sa_state = 0;
3308 sa->sadb_sa_auth = 0;
3309 sa->sadb_sa_encrypt = 0;
3310 sa->sadb_sa_flags = 0;
3311
3312 /* ADDRESS_SRC (old addr) */
3313 addr = (struct sadb_address*)
3314 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003315 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 (sizeof(struct sadb_address)+sockaddr_size)/
3317 sizeof(uint64_t);
3318 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3319 addr->sadb_address_proto = 0;
3320 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003321 addr->sadb_address_prefixlen =
3322 pfkey_sockaddr_fill(&x->props.saddr, 0,
3323 (struct sockaddr *) (addr + 1),
3324 x->props.family);
3325 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 BUG();
3327
3328 /* NAT_T_SPORT (old port) */
3329 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3330 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3331 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3332 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3333 n_port->sadb_x_nat_t_port_reserved = 0;
3334
3335 /* ADDRESS_DST (new addr) */
3336 addr = (struct sadb_address*)
3337 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
YOSHIFUJI Hideaki8ff24542007-02-09 23:24:58 +09003338 addr->sadb_address_len =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 (sizeof(struct sadb_address)+sockaddr_size)/
3340 sizeof(uint64_t);
3341 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3342 addr->sadb_address_proto = 0;
3343 addr->sadb_address_reserved = 0;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003344 addr->sadb_address_prefixlen =
3345 pfkey_sockaddr_fill(ipaddr, 0,
3346 (struct sockaddr *) (addr + 1),
3347 x->props.family);
3348 if (!addr->sadb_address_prefixlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 BUG();
3350
3351 /* NAT_T_DPORT (new port) */
3352 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3353 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3354 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3355 n_port->sadb_x_nat_t_port_port = sport;
3356 n_port->sadb_x_nat_t_port_reserved = 0;
3357
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003358 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359}
3360
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003361#ifdef CONFIG_NET_KEY_MIGRATE
3362static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
David S. Miller183cad12011-02-24 00:28:01 -05003363 const struct xfrm_selector *sel)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003364{
3365 struct sadb_address *addr;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003366 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3367 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3368 addr->sadb_address_exttype = type;
3369 addr->sadb_address_proto = sel->proto;
3370 addr->sadb_address_reserved = 0;
3371
3372 switch (type) {
3373 case SADB_EXT_ADDRESS_SRC:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003374 addr->sadb_address_prefixlen = sel->prefixlen_s;
3375 pfkey_sockaddr_fill(&sel->saddr, 0,
3376 (struct sockaddr *)(addr + 1),
3377 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003378 break;
3379 case SADB_EXT_ADDRESS_DST:
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003380 addr->sadb_address_prefixlen = sel->prefixlen_d;
3381 pfkey_sockaddr_fill(&sel->daddr, 0,
3382 (struct sockaddr *)(addr + 1),
3383 sel->family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003384 break;
3385 default:
3386 return -EINVAL;
3387 }
3388
3389 return 0;
3390}
3391
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003392
David S. Miller183cad12011-02-24 00:28:01 -05003393static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003394{
3395 struct sadb_x_kmaddress *kma;
3396 u8 *sa;
3397 int family = k->family;
3398 int socklen = pfkey_sockaddr_len(family);
3399 int size_req;
3400
3401 size_req = (sizeof(struct sadb_x_kmaddress) +
3402 pfkey_sockaddr_pair_size(family));
3403
3404 kma = (struct sadb_x_kmaddress *)skb_put(skb, size_req);
3405 memset(kma, 0, size_req);
3406 kma->sadb_x_kmaddress_len = size_req / 8;
3407 kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3408 kma->sadb_x_kmaddress_reserved = k->reserved;
3409
3410 sa = (u8 *)(kma + 1);
3411 if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3412 !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3413 return -EINVAL;
3414
3415 return 0;
3416}
3417
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003418static int set_ipsecrequest(struct sk_buff *skb,
3419 uint8_t proto, uint8_t mode, int level,
3420 uint32_t reqid, uint8_t family,
David S. Miller183cad12011-02-24 00:28:01 -05003421 const xfrm_address_t *src, const xfrm_address_t *dst)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003422{
3423 struct sadb_x_ipsecrequest *rq;
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003424 u8 *sa;
3425 int socklen = pfkey_sockaddr_len(family);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003426 int size_req;
3427
3428 size_req = sizeof(struct sadb_x_ipsecrequest) +
3429 pfkey_sockaddr_pair_size(family);
3430
3431 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3432 memset(rq, 0, size_req);
3433 rq->sadb_x_ipsecrequest_len = size_req;
3434 rq->sadb_x_ipsecrequest_proto = proto;
3435 rq->sadb_x_ipsecrequest_mode = mode;
3436 rq->sadb_x_ipsecrequest_level = level;
3437 rq->sadb_x_ipsecrequest_reqid = reqid;
3438
YOSHIFUJI Hideakie5b56652008-04-28 01:46:41 +09003439 sa = (u8 *) (rq + 1);
3440 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3441 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003442 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003443
3444 return 0;
3445}
3446#endif
3447
3448#ifdef CONFIG_NET_KEY_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05003449static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3450 const struct xfrm_migrate *m, int num_bundles,
3451 const struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003452{
3453 int i;
3454 int sasize_sel;
3455 int size = 0;
3456 int size_pol = 0;
3457 struct sk_buff *skb;
3458 struct sadb_msg *hdr;
3459 struct sadb_x_policy *pol;
David S. Miller183cad12011-02-24 00:28:01 -05003460 const struct xfrm_migrate *mp;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003461
3462 if (type != XFRM_POLICY_TYPE_MAIN)
3463 return 0;
3464
3465 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3466 return -EINVAL;
3467
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003468 if (k != NULL) {
3469 /* addresses for KM */
3470 size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3471 pfkey_sockaddr_pair_size(k->family));
3472 }
3473
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003474 /* selector */
3475 sasize_sel = pfkey_sockaddr_size(sel->family);
3476 if (!sasize_sel)
3477 return -EINVAL;
3478 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3479
3480 /* policy info */
3481 size_pol += sizeof(struct sadb_x_policy);
3482
3483 /* ipsecrequests */
3484 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3485 /* old locator pair */
3486 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3487 pfkey_sockaddr_pair_size(mp->old_family);
3488 /* new locator pair */
3489 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3490 pfkey_sockaddr_pair_size(mp->new_family);
3491 }
3492
3493 size += sizeof(struct sadb_msg) + size_pol;
3494
3495 /* alloc buffer */
3496 skb = alloc_skb(size, GFP_ATOMIC);
3497 if (skb == NULL)
3498 return -ENOMEM;
3499
3500 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3501 hdr->sadb_msg_version = PF_KEY_V2;
3502 hdr->sadb_msg_type = SADB_X_MIGRATE;
3503 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3504 hdr->sadb_msg_len = size / 8;
3505 hdr->sadb_msg_errno = 0;
3506 hdr->sadb_msg_reserved = 0;
3507 hdr->sadb_msg_seq = 0;
3508 hdr->sadb_msg_pid = 0;
3509
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003510 /* Addresses to be used by KM for negotiation, if ext is available */
3511 if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
Julia Lawall89eb06f2012-04-08 22:41:10 +00003512 goto err;
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003513
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003514 /* selector src */
3515 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3516
3517 /* selector dst */
3518 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3519
3520 /* policy information */
3521 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3522 pol->sadb_x_policy_len = size_pol / 8;
3523 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3524 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3525 pol->sadb_x_policy_dir = dir + 1;
3526 pol->sadb_x_policy_id = 0;
3527 pol->sadb_x_policy_priority = 0;
3528
3529 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3530 /* old ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003531 int mode = pfkey_mode_from_xfrm(mp->mode);
3532 if (mode < 0)
Patrick McHardyd4782c32008-01-20 17:24:29 -08003533 goto err;
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003534 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003535 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3536 mp->reqid, mp->old_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003537 &mp->old_saddr, &mp->old_daddr) < 0)
3538 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003539
3540 /* new ipsecrequest */
Kazunori MIYAZAWA55569ce2007-04-17 12:32:20 -07003541 if (set_ipsecrequest(skb, mp->proto, mode,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003542 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3543 mp->reqid, mp->new_family,
Patrick McHardyd4782c32008-01-20 17:24:29 -08003544 &mp->new_saddr, &mp->new_daddr) < 0)
3545 goto err;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003546 }
3547
3548 /* broadcast migrate message to sockets */
Alexey Dobriyan07fb0f12008-11-25 17:58:31 -08003549 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net);
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003550
3551 return 0;
Patrick McHardyd4782c32008-01-20 17:24:29 -08003552
3553err:
3554 kfree_skb(skb);
3555 return -EINVAL;
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003556}
3557#else
David S. Miller183cad12011-02-24 00:28:01 -05003558static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3559 const struct xfrm_migrate *m, int num_bundles,
3560 const struct xfrm_kmaddress *k)
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003561{
3562 return -ENOPROTOOPT;
3563}
3564#endif
3565
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566static int pfkey_sendmsg(struct kiocb *kiocb,
3567 struct socket *sock, struct msghdr *msg, size_t len)
3568{
3569 struct sock *sk = sock->sk;
3570 struct sk_buff *skb = NULL;
3571 struct sadb_msg *hdr = NULL;
3572 int err;
3573
3574 err = -EOPNOTSUPP;
3575 if (msg->msg_flags & MSG_OOB)
3576 goto out;
3577
3578 err = -EMSGSIZE;
Eric Dumazet95c96172012-04-15 05:58:06 +00003579 if ((unsigned int)len > sk->sk_sndbuf - 32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 goto out;
3581
3582 err = -ENOBUFS;
3583 skb = alloc_skb(len, GFP_KERNEL);
3584 if (skb == NULL)
3585 goto out;
3586
3587 err = -EFAULT;
3588 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3589 goto out;
3590
3591 hdr = pfkey_get_base_msg(skb, &err);
3592 if (!hdr)
3593 goto out;
3594
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003595 mutex_lock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 err = pfkey_process(sk, skb, hdr);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08003597 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
3599out:
3600 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3601 err = 0;
Wei Yongjun6f961062009-02-25 00:31:04 +00003602 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
3604 return err ? : len;
3605}
3606
3607static int pfkey_recvmsg(struct kiocb *kiocb,
3608 struct socket *sock, struct msghdr *msg, size_t len,
3609 int flags)
3610{
3611 struct sock *sk = sock->sk;
Timo Teras83321d62008-03-03 23:40:12 -08003612 struct pfkey_sock *pfk = pfkey_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 struct sk_buff *skb;
3614 int copied, err;
3615
3616 err = -EINVAL;
3617 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3618 goto out;
3619
3620 msg->msg_namelen = 0;
3621 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3622 if (skb == NULL)
3623 goto out;
3624
3625 copied = skb->len;
3626 if (copied > len) {
3627 msg->msg_flags |= MSG_TRUNC;
3628 copied = len;
3629 }
3630
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03003631 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3633 if (err)
3634 goto out_free;
3635
Neil Horman3b885782009-10-12 13:26:31 -07003636 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
3638 err = (flags & MSG_TRUNC) ? skb->len : copied;
3639
Timo Teras83321d62008-03-03 23:40:12 -08003640 if (pfk->dump.dump != NULL &&
3641 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3642 pfkey_do_dump(pfk);
3643
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644out_free:
3645 skb_free_datagram(sk, skb);
3646out:
3647 return err;
3648}
3649
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003650static const struct proto_ops pfkey_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 .family = PF_KEY,
3652 .owner = THIS_MODULE,
3653 /* Operations that make no sense on pfkey sockets. */
3654 .bind = sock_no_bind,
3655 .connect = sock_no_connect,
3656 .socketpair = sock_no_socketpair,
3657 .accept = sock_no_accept,
3658 .getname = sock_no_getname,
3659 .ioctl = sock_no_ioctl,
3660 .listen = sock_no_listen,
3661 .shutdown = sock_no_shutdown,
3662 .setsockopt = sock_no_setsockopt,
3663 .getsockopt = sock_no_getsockopt,
3664 .mmap = sock_no_mmap,
3665 .sendpage = sock_no_sendpage,
3666
3667 /* Now the operations that really occur. */
3668 .release = pfkey_release,
3669 .poll = datagram_poll,
3670 .sendmsg = pfkey_sendmsg,
3671 .recvmsg = pfkey_recvmsg,
3672};
3673
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003674static const struct net_proto_family pfkey_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 .family = PF_KEY,
3676 .create = pfkey_create,
3677 .owner = THIS_MODULE,
3678};
3679
3680#ifdef CONFIG_PROC_FS
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003681static int pfkey_seq_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682{
Li Zefan27b5b862010-02-08 23:20:29 +00003683 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003685 if (v == SEQ_START_TOKEN)
3686 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3687 else
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003688 seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 s,
3690 atomic_read(&s->sk_refcnt),
Eric Dumazet31e6d362009-06-17 19:05:41 -07003691 sk_rmem_alloc_get(s),
3692 sk_wmem_alloc_get(s),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06003693 from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 sock_i_ino(s)
3695 );
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697}
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003698
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003699static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
stephen hemmingerada440e2011-02-23 09:06:50 +00003700 __acquires(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003701{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003702 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003703 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003704
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003705 rcu_read_lock();
3706 return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003707}
3708
3709static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3710{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003711 struct net *net = seq_file_net(f);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003712 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3713
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003714 return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003715}
3716
3717static void pfkey_seq_stop(struct seq_file *f, void *v)
stephen hemmingerada440e2011-02-23 09:06:50 +00003718 __releases(rcu)
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003719{
stephen hemminger7f6b9db2010-02-22 07:57:19 +00003720 rcu_read_unlock();
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003721}
3722
Stephen Hemminger98147d52009-09-01 19:25:02 +00003723static const struct seq_operations pfkey_seq_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003724 .start = pfkey_seq_start,
3725 .next = pfkey_seq_next,
3726 .stop = pfkey_seq_stop,
3727 .show = pfkey_seq_show,
3728};
3729
3730static int pfkey_seq_open(struct inode *inode, struct file *file)
3731{
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003732 return seq_open_net(inode, file, &pfkey_seq_ops,
3733 sizeof(struct seq_net_private));
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003734}
3735
Stephen Hemminger5ca1b992009-09-01 19:25:05 +00003736static const struct file_operations pfkey_proc_ops = {
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003737 .open = pfkey_seq_open,
3738 .read = seq_read,
3739 .llseek = seq_lseek,
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003740 .release = seq_release_net,
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003741};
3742
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003743static int __net_init pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003744{
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003745 struct proc_dir_entry *e;
3746
Gao fengd4beaa62013-02-18 01:34:54 +00003747 e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops);
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003748 if (e == NULL)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003749 return -ENOMEM;
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003750
Pavel Emelyanovbd2f7472008-02-09 23:20:06 -08003751 return 0;
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003752}
3753
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003754static void __net_exit pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003755{
Gao fengece31ff2013-02-18 01:34:56 +00003756 remove_proc_entry("pfkey", net->proc_net);
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003757}
3758#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003759static inline int pfkey_init_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003760{
3761 return 0;
3762}
3763
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003764static inline void pfkey_exit_proc(struct net *net)
Pavel Emelyanov61145aa2008-02-09 23:19:14 -08003765{
3766}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767#endif
3768
3769static struct xfrm_mgr pfkeyv2_mgr =
3770{
3771 .id = "pfkeyv2",
3772 .notify = pfkey_send_notify,
3773 .acquire = pfkey_send_acquire,
3774 .compile_policy = pfkey_compile_policy,
3775 .new_mapping = pfkey_send_new_mapping,
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003776 .notify_policy = pfkey_send_policy_notify,
Shinta Sugimoto08de61b2007-02-08 13:14:33 -08003777 .migrate = pfkey_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778};
3779
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003780static int __net_init pfkey_net_init(struct net *net)
3781{
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003782 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003783 int rv;
3784
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003785 INIT_HLIST_HEAD(&net_pfkey->table);
3786 atomic_set(&net_pfkey->socks_nr, 0);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003787
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003788 rv = pfkey_init_proc(net);
3789
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003790 return rv;
3791}
3792
3793static void __net_exit pfkey_net_exit(struct net *net)
3794{
3795 struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3796
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003797 pfkey_exit_proc(net);
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003798 BUG_ON(!hlist_empty(&net_pfkey->table));
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003799}
3800
3801static struct pernet_operations pfkey_net_ops = {
3802 .init = pfkey_net_init,
3803 .exit = pfkey_net_exit,
Eric W. Biederman23c049c2009-11-29 15:46:06 +00003804 .id = &pfkey_net_id,
3805 .size = sizeof(struct netns_pfkey),
Alexey Dobriyan3fa87a32008-11-25 17:58:07 -08003806};
3807
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808static void __exit ipsec_pfkey_exit(void)
3809{
3810 xfrm_unregister_km(&pfkeyv2_mgr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003812 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 proto_unregister(&key_proto);
3814}
3815
3816static int __init ipsec_pfkey_init(void)
3817{
3818 int err = proto_register(&key_proto, 0);
3819
3820 if (err != 0)
3821 goto out;
3822
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003823 err = register_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 if (err != 0)
3825 goto out_unregister_key_proto;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003826 err = sock_register(&pfkey_family_ops);
3827 if (err != 0)
3828 goto out_unregister_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 err = xfrm_register_km(&pfkeyv2_mgr);
3830 if (err != 0)
Alexey Dobriyan7013ec32008-11-25 17:59:00 -08003831 goto out_sock_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832out:
3833 return err;
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003834
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835out_sock_unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 sock_unregister(PF_KEY);
Alexey Dobriyan180211b2010-01-30 02:53:27 +00003837out_unregister_pernet:
3838 unregister_pernet_subsys(&pfkey_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839out_unregister_key_proto:
3840 proto_unregister(&key_proto);
3841 goto out;
3842}
3843
3844module_init(ipsec_pfkey_init);
3845module_exit(ipsec_pfkey_exit);
3846MODULE_LICENSE("GPL");
3847MODULE_ALIAS_NETPROTO(PF_KEY);