blob: 3f565e495ac68cea83e1d52cf7db7e820fe777ad [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +000029#include <net/ah.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070032#include <linux/in6.h>
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Herbert Xu1a6509d2008-01-28 19:37:29 -080035static inline int aead_len(struct xfrm_algo_aead *alg)
36{
37 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
38}
39
Thomas Graf5424f322007-08-22 14:01:33 -070040static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Thomas Graf5424f322007-08-22 14:01:33 -070042 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct xfrm_algo *algp;
44
45 if (!rt)
46 return 0;
47
Thomas Graf5424f322007-08-22 14:01:33 -070048 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080049 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070050 return -EINVAL;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 switch (type) {
53 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 break;
57
58 default:
59 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
63 return 0;
64}
65
Martin Willi4447bb32009-11-25 00:29:52 +000066static int verify_auth_trunc(struct nlattr **attrs)
67{
68 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
69 struct xfrm_algo_auth *algp;
70
71 if (!rt)
72 return 0;
73
74 algp = nla_data(rt);
75 if (nla_len(rt) < xfrm_alg_auth_len(algp))
76 return -EINVAL;
77
78 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
79 return 0;
80}
81
Herbert Xu1a6509d2008-01-28 19:37:29 -080082static int verify_aead(struct nlattr **attrs)
83{
84 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
85 struct xfrm_algo_aead *algp;
86
87 if (!rt)
88 return 0;
89
90 algp = nla_data(rt);
91 if (nla_len(rt) < aead_len(algp))
92 return -EINVAL;
93
94 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
95 return 0;
96}
97
Thomas Graf5424f322007-08-22 14:01:33 -070098static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070099 xfrm_address_t **addrp)
100{
Thomas Graf5424f322007-08-22 14:01:33 -0700101 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700102
Thomas Grafcf5cb792007-08-22 13:59:04 -0700103 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700104 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700105}
Trent Jaegerdf718372005-12-13 23:12:27 -0800106
Thomas Graf5424f322007-08-22 14:01:33 -0700107static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800108{
Thomas Graf5424f322007-08-22 14:01:33 -0700109 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800110 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800111
112 if (!rt)
113 return 0;
114
Thomas Graf5424f322007-08-22 14:01:33 -0700115 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700116 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800117 return -EINVAL;
118
119 return 0;
120}
121
Steffen Klassertd8647b72011-03-08 00:10:27 +0000122static inline int verify_replay(struct xfrm_usersa_info *p,
123 struct nlattr **attrs)
124{
125 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000126 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000127
Mathias Krauseecd79182012-09-20 10:01:49 +0000128 if (p->flags & XFRM_STATE_ESN) {
129 if (!rt)
130 return -EINVAL;
131
132 rs = nla_data(rt);
133
134 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
135 return -EINVAL;
136
137 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
138 nla_len(rt) != sizeof(*rs))
139 return -EINVAL;
140 }
Steffen Klassert7833aa02011-04-25 19:41:21 +0000141
Steffen Klassertd8647b72011-03-08 00:10:27 +0000142 if (!rt)
143 return 0;
144
Steffen Klassert02aadf72011-03-28 19:48:09 +0000145 if (p->id.proto != IPPROTO_ESP)
146 return -EINVAL;
147
Steffen Klassertd8647b72011-03-08 00:10:27 +0000148 if (p->replay_window != 0)
149 return -EINVAL;
150
151 return 0;
152}
Trent Jaegerdf718372005-12-13 23:12:27 -0800153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700155 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 int err;
158
159 err = -EINVAL;
160 switch (p->family) {
161 case AF_INET:
162 break;
163
164 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000165#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 break;
167#else
168 err = -EAFNOSUPPORT;
169 goto out;
170#endif
171
172 default:
173 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 err = -EINVAL;
177 switch (p->id.proto) {
178 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000179 if ((!attrs[XFRMA_ALG_AUTH] &&
180 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800181 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700182 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000183 attrs[XFRMA_ALG_COMP] ||
184 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 goto out;
186 break;
187
188 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800189 if (attrs[XFRMA_ALG_COMP])
190 goto out;
191 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000192 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800193 !attrs[XFRMA_ALG_CRYPT] &&
194 !attrs[XFRMA_ALG_AEAD])
195 goto out;
196 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000197 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800198 attrs[XFRMA_ALG_CRYPT]) &&
199 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000201 if (attrs[XFRMA_TFCPAD] &&
202 p->mode != XFRM_MODE_TUNNEL)
203 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205
206 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700207 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800208 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700209 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000210 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000211 attrs[XFRMA_ALG_CRYPT] ||
212 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 goto out;
214 break;
215
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000216#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700217 case IPPROTO_DSTOPTS:
218 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700219 if (attrs[XFRMA_ALG_COMP] ||
220 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000221 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800222 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700223 attrs[XFRMA_ALG_CRYPT] ||
224 attrs[XFRMA_ENCAP] ||
225 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000226 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700227 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700228 goto out;
229 break;
230#endif
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 default:
233 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Herbert Xu1a6509d2008-01-28 19:37:29 -0800236 if ((err = verify_aead(attrs)))
237 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000238 if ((err = verify_auth_trunc(attrs)))
239 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700240 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700242 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700244 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700246 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800247 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000248 if ((err = verify_replay(p, attrs)))
249 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 err = -EINVAL;
252 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700253 case XFRM_MODE_TRANSPORT:
254 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700255 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700256 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 break;
258
259 default:
260 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 err = 0;
264
265out:
266 return err;
267}
268
269static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800270 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700271 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct xfrm_algo *p, *ualg;
274 struct xfrm_algo_desc *algo;
275
276 if (!rta)
277 return 0;
278
Thomas Graf5424f322007-08-22 14:01:33 -0700279 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 algo = get_byname(ualg->alg_name, 1);
282 if (!algo)
283 return -ENOSYS;
284 *props = algo->desc.sadb_alg_id;
285
Eric Dumazet0f99be02008-01-08 23:39:06 -0800286 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!p)
288 return -ENOMEM;
289
Herbert Xu04ff1262006-08-13 08:50:00 +1000290 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *algpp = p;
292 return 0;
293}
294
Martin Willi4447bb32009-11-25 00:29:52 +0000295static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
296 struct nlattr *rta)
297{
298 struct xfrm_algo *ualg;
299 struct xfrm_algo_auth *p;
300 struct xfrm_algo_desc *algo;
301
302 if (!rta)
303 return 0;
304
305 ualg = nla_data(rta);
306
307 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
308 if (!algo)
309 return -ENOSYS;
310 *props = algo->desc.sadb_alg_id;
311
312 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
313 if (!p)
314 return -ENOMEM;
315
316 strcpy(p->alg_name, algo->name);
317 p->alg_key_len = ualg->alg_key_len;
318 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
319 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
320
321 *algpp = p;
322 return 0;
323}
324
325static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
326 struct nlattr *rta)
327{
328 struct xfrm_algo_auth *p, *ualg;
329 struct xfrm_algo_desc *algo;
330
331 if (!rta)
332 return 0;
333
334 ualg = nla_data(rta);
335
336 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
337 if (!algo)
338 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000339 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
340 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000341 return -EINVAL;
342 *props = algo->desc.sadb_alg_id;
343
344 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
345 if (!p)
346 return -ENOMEM;
347
348 strcpy(p->alg_name, algo->name);
349 if (!p->alg_trunc_len)
350 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
351
352 *algpp = p;
353 return 0;
354}
355
Herbert Xu1a6509d2008-01-28 19:37:29 -0800356static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
357 struct nlattr *rta)
358{
359 struct xfrm_algo_aead *p, *ualg;
360 struct xfrm_algo_desc *algo;
361
362 if (!rta)
363 return 0;
364
365 ualg = nla_data(rta);
366
367 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
368 if (!algo)
369 return -ENOSYS;
370 *props = algo->desc.sadb_alg_id;
371
372 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
373 if (!p)
374 return -ENOMEM;
375
376 strcpy(p->alg_name, algo->name);
377 *algpp = p;
378 return 0;
379}
380
Steffen Klasserte2b19122011-03-28 19:47:30 +0000381static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
382 struct nlattr *rp)
383{
384 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000385 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000386
387 if (!replay_esn || !rp)
388 return 0;
389
390 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000391 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000392
Mathias Krauseecd79182012-09-20 10:01:49 +0000393 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000394 return -EINVAL;
395
396 return 0;
397}
398
Steffen Klassertd8647b72011-03-08 00:10:27 +0000399static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
400 struct xfrm_replay_state_esn **preplay_esn,
401 struct nlattr *rta)
402{
403 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000404 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000405
406 if (!rta)
407 return 0;
408
409 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000410 klen = xfrm_replay_state_esn_len(up);
411 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000412
Mathias Krauseecd79182012-09-20 10:01:49 +0000413 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000414 if (!p)
415 return -ENOMEM;
416
Mathias Krauseecd79182012-09-20 10:01:49 +0000417 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000418 if (!pp) {
419 kfree(p);
420 return -ENOMEM;
421 }
422
Mathias Krauseecd79182012-09-20 10:01:49 +0000423 memcpy(p, up, ulen);
424 memcpy(pp, up, ulen);
425
Steffen Klassertd8647b72011-03-08 00:10:27 +0000426 *replay_esn = p;
427 *preplay_esn = pp;
428
429 return 0;
430}
431
Joy Latten661697f2007-04-13 16:14:35 -0700432static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800433{
Trent Jaegerdf718372005-12-13 23:12:27 -0800434 int len = 0;
435
436 if (xfrm_ctx) {
437 len += sizeof(struct xfrm_user_sec_ctx);
438 len += xfrm_ctx->ctx_len;
439 }
440 return len;
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
444{
445 memcpy(&x->id, &p->id, sizeof(x->id));
446 memcpy(&x->sel, &p->sel, sizeof(x->sel));
447 memcpy(&x->lft, &p->lft, sizeof(x->lft));
448 x->props.mode = p->mode;
449 x->props.replay_window = p->replay_window;
450 x->props.reqid = p->reqid;
451 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700452 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700454
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700455 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700456 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800459/*
460 * someday when pfkey also has support, we could have the code
461 * somehow made shareable and move it to xfrm_state.c - JHS
462 *
463*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000464static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
465 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800466{
Thomas Graf5424f322007-08-22 14:01:33 -0700467 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000468 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700469 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
470 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
471 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800472
Steffen Klassertd8647b72011-03-08 00:10:27 +0000473 if (re) {
474 struct xfrm_replay_state_esn *replay_esn;
475 replay_esn = nla_data(re);
476 memcpy(x->replay_esn, replay_esn,
477 xfrm_replay_state_esn_len(replay_esn));
478 memcpy(x->preplay_esn, replay_esn,
479 xfrm_replay_state_esn_len(replay_esn));
480 }
481
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800482 if (rp) {
483 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700484 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800485 memcpy(&x->replay, replay, sizeof(*replay));
486 memcpy(&x->preplay, replay, sizeof(*replay));
487 }
488
489 if (lt) {
490 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700491 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800492 x->curlft.bytes = ltime->bytes;
493 x->curlft.packets = ltime->packets;
494 x->curlft.add_time = ltime->add_time;
495 x->curlft.use_time = ltime->use_time;
496 }
497
Thomas Grafcf5cb792007-08-22 13:59:04 -0700498 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700499 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800500
Thomas Grafcf5cb792007-08-22 13:59:04 -0700501 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700502 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800503}
504
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800505static struct xfrm_state *xfrm_state_construct(struct net *net,
506 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700507 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 int *errp)
509{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800510 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 int err = -ENOMEM;
512
513 if (!x)
514 goto error_no_put;
515
516 copy_from_user_state(x, p);
517
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100518 if (attrs[XFRMA_SA_EXTRA_FLAGS])
519 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
520
Herbert Xu1a6509d2008-01-28 19:37:29 -0800521 if ((err = attach_aead(&x->aead, &x->props.ealgo,
522 attrs[XFRMA_ALG_AEAD])))
523 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000524 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
525 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000527 if (!x->props.aalgo) {
528 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
529 attrs[XFRMA_ALG_AUTH])))
530 goto error;
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
533 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700534 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto error;
536 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
537 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700538 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700540
541 if (attrs[XFRMA_ENCAP]) {
542 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
543 sizeof(*x->encap), GFP_KERNEL);
544 if (x->encap == NULL)
545 goto error;
546 }
547
Martin Willi35d28562010-12-08 04:37:49 +0000548 if (attrs[XFRMA_TFCPAD])
549 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
550
Thomas Graffd211502007-09-06 03:28:08 -0700551 if (attrs[XFRMA_COADDR]) {
552 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
553 sizeof(*x->coaddr), GFP_KERNEL);
554 if (x->coaddr == NULL)
555 goto error;
556 }
557
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000558 xfrm_mark_get(attrs, &x->mark);
559
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700560 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (err)
562 goto error;
563
Thomas Graffd211502007-09-06 03:28:08 -0700564 if (attrs[XFRMA_SEC_CTX] &&
565 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800566 goto error;
567
Steffen Klassertd8647b72011-03-08 00:10:27 +0000568 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
569 attrs[XFRMA_REPLAY_ESN_VAL])))
570 goto error;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800573 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800574 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800575 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800576
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000577 if ((err = xfrm_init_replay(x)))
578 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800579
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000580 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000581 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 return x;
584
585error:
586 x->km.state = XFRM_STATE_DEAD;
587 xfrm_state_put(x);
588error_no_put:
589 *errp = err;
590 return NULL;
591}
592
Christoph Hellwig22e70052007-01-02 15:22:30 -0800593static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700594 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800596 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700597 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct xfrm_state *x;
599 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700600 struct km_event c;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700601 kuid_t loginuid = audit_get_loginuid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800602 u32 sessionid = audit_get_sessionid(current);
603 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Thomas Graf35a7aa02007-08-22 14:00:40 -0700605 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (err)
607 return err;
608
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800609 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (!x)
611 return err;
612
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700613 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
615 err = xfrm_state_add(x);
616 else
617 err = xfrm_state_update(x);
618
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800619 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400620 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (err < 0) {
623 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800624 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700625 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700628 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000629 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700630 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700631
632 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700633out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700634 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return err;
636}
637
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800638static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
639 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700640 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700641 int *errp)
642{
643 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000644 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700645 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000646 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700647
648 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
649 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000650 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700651 } else {
652 xfrm_address_t *saddr = NULL;
653
Thomas Graf35a7aa02007-08-22 14:00:40 -0700654 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700655 if (!saddr) {
656 err = -EINVAL;
657 goto out;
658 }
659
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800660 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000661 x = xfrm_state_lookup_byaddr(net, mark,
662 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800663 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700664 }
665
666 out:
667 if (!x && errp)
668 *errp = err;
669 return x;
670}
671
Christoph Hellwig22e70052007-01-02 15:22:30 -0800672static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700673 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800675 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700677 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700678 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700679 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700680 kuid_t loginuid = audit_get_loginuid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800681 u32 sessionid = audit_get_sessionid(current);
682 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800684 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700686 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
David S. Miller6f68dc32006-06-08 23:58:52 -0700688 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700689 goto out;
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700692 err = -EPERM;
693 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700696 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600697
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700698 if (err < 0)
699 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700700
701 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000702 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700703 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700704 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700706out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800707 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400708 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700709 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700710 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
714{
Mathias Krausef778a632012-09-19 11:33:39 +0000715 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 memcpy(&p->id, &x->id, sizeof(p->id));
717 memcpy(&p->sel, &x->sel, sizeof(p->sel));
718 memcpy(&p->lft, &x->lft, sizeof(p->lft));
719 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
720 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700721 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 p->mode = x->props.mode;
723 p->replay_window = x->props.replay_window;
724 p->reqid = x->props.reqid;
725 p->family = x->props.family;
726 p->flags = x->props.flags;
727 p->seq = x->km.seq;
728}
729
730struct xfrm_dump_info {
731 struct sk_buff *in_skb;
732 struct sk_buff *out_skb;
733 u32 nlmsg_seq;
734 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735};
736
Thomas Grafc0144be2007-08-22 13:55:43 -0700737static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
738{
Thomas Grafc0144be2007-08-22 13:55:43 -0700739 struct xfrm_user_sec_ctx *uctx;
740 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700741 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700742
743 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
744 if (attr == NULL)
745 return -EMSGSIZE;
746
747 uctx = nla_data(attr);
748 uctx->exttype = XFRMA_SEC_CTX;
749 uctx->len = ctx_size;
750 uctx->ctx_doi = s->ctx_doi;
751 uctx->ctx_alg = s->ctx_alg;
752 uctx->ctx_len = s->ctx_len;
753 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
754
755 return 0;
756}
757
Martin Willi4447bb32009-11-25 00:29:52 +0000758static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
759{
760 struct xfrm_algo *algo;
761 struct nlattr *nla;
762
763 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
764 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
765 if (!nla)
766 return -EMSGSIZE;
767
768 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000769 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000770 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
771 algo->alg_key_len = auth->alg_key_len;
772
773 return 0;
774}
775
Herbert Xu68325d32007-10-09 13:30:57 -0700776/* Don't change this without updating xfrm_sa_len! */
777static int copy_to_user_state_extra(struct xfrm_state *x,
778 struct xfrm_usersa_info *p,
779 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700781 int ret = 0;
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 copy_to_user_state(x, p);
784
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100785 if (x->props.extra_flags) {
786 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
787 x->props.extra_flags);
788 if (ret)
789 goto out;
790 }
791
David S. Miller1d1e34d2012-06-27 21:57:03 -0700792 if (x->coaddr) {
793 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
794 if (ret)
795 goto out;
796 }
797 if (x->lastused) {
798 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
799 if (ret)
800 goto out;
801 }
802 if (x->aead) {
803 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
804 if (ret)
805 goto out;
806 }
807 if (x->aalg) {
808 ret = copy_to_user_auth(x->aalg, skb);
809 if (!ret)
810 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
811 xfrm_alg_auth_len(x->aalg), x->aalg);
812 if (ret)
813 goto out;
814 }
815 if (x->ealg) {
816 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
817 if (ret)
818 goto out;
819 }
820 if (x->calg) {
821 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
822 if (ret)
823 goto out;
824 }
825 if (x->encap) {
826 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
827 if (ret)
828 goto out;
829 }
830 if (x->tfcpad) {
831 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
832 if (ret)
833 goto out;
834 }
835 ret = xfrm_mark_put(skb, &x->mark);
836 if (ret)
837 goto out;
838 if (x->replay_esn) {
839 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
840 xfrm_replay_state_esn_len(x->replay_esn),
841 x->replay_esn);
842 if (ret)
843 goto out;
844 }
845 if (x->security)
846 ret = copy_sec_ctx(x->security, skb);
847out:
848 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700849}
850
851static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
852{
853 struct xfrm_dump_info *sp = ptr;
854 struct sk_buff *in_skb = sp->in_skb;
855 struct sk_buff *skb = sp->out_skb;
856 struct xfrm_usersa_info *p;
857 struct nlmsghdr *nlh;
858 int err;
859
Eric W. Biederman15e47302012-09-07 20:12:54 +0000860 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700861 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
862 if (nlh == NULL)
863 return -EMSGSIZE;
864
865 p = nlmsg_data(nlh);
866
867 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700868 if (err) {
869 nlmsg_cancel(skb, nlh);
870 return err;
871 }
Thomas Graf98250692007-08-22 12:47:26 -0700872 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Timo Teras4c563f72008-02-28 21:31:08 -0800876static int xfrm_dump_sa_done(struct netlink_callback *cb)
877{
878 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
879 xfrm_state_walk_done(walk);
880 return 0;
881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
884{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800885 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800886 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 struct xfrm_dump_info info;
888
Timo Teras4c563f72008-02-28 21:31:08 -0800889 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
890 sizeof(cb->args) - sizeof(cb->args[0]));
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 info.in_skb = cb->skb;
893 info.out_skb = skb;
894 info.nlmsg_seq = cb->nlh->nlmsg_seq;
895 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800896
897 if (!cb->args[0]) {
898 cb->args[0] = 1;
899 xfrm_state_walk_init(walk, 0);
900 }
901
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800902 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 return skb->len;
905}
906
907static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
908 struct xfrm_state *x, u32 seq)
909{
910 struct xfrm_dump_info info;
911 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000912 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Thomas Graf7deb2262007-08-22 13:57:39 -0700914 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (!skb)
916 return ERR_PTR(-ENOMEM);
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 info.in_skb = in_skb;
919 info.out_skb = skb;
920 info.nlmsg_seq = seq;
921 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Mathias Krause864745d2012-09-13 11:41:26 +0000923 err = dump_one_state(x, 0, &info);
924 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000926 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
929 return skb;
930}
931
Thomas Graf7deb2262007-08-22 13:57:39 -0700932static inline size_t xfrm_spdinfo_msgsize(void)
933{
934 return NLMSG_ALIGN(4)
935 + nla_total_size(sizeof(struct xfrmu_spdinfo))
936 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
937}
938
Alexey Dobriyane0710412010-01-23 13:37:10 +0000939static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000940 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700941{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700942 struct xfrmk_spdinfo si;
943 struct xfrmu_spdinfo spc;
944 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700945 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700946 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700947 u32 *f;
948
Eric W. Biederman15e47302012-09-07 20:12:54 +0000949 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300950 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700951 return -EMSGSIZE;
952
953 f = nlmsg_data(nlh);
954 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000955 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700956 spc.incnt = si.incnt;
957 spc.outcnt = si.outcnt;
958 spc.fwdcnt = si.fwdcnt;
959 spc.inscnt = si.inscnt;
960 spc.outscnt = si.outscnt;
961 spc.fwdscnt = si.fwdscnt;
962 sph.spdhcnt = si.spdhcnt;
963 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700964
David S. Miller1d1e34d2012-06-27 21:57:03 -0700965 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
966 if (!err)
967 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
968 if (err) {
969 nlmsg_cancel(skb, nlh);
970 return err;
971 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700972
973 return nlmsg_end(skb, nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700974}
975
976static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700977 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700978{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800979 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700980 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700981 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000982 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700983 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700984
Thomas Graf7deb2262007-08-22 13:57:39 -0700985 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700986 if (r_skb == NULL)
987 return -ENOMEM;
988
Eric W. Biederman15e47302012-09-07 20:12:54 +0000989 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700990 BUG();
991
Eric W. Biederman15e47302012-09-07 20:12:54 +0000992 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700993}
994
Thomas Graf7deb2262007-08-22 13:57:39 -0700995static inline size_t xfrm_sadinfo_msgsize(void)
996{
997 return NLMSG_ALIGN(4)
998 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
999 + nla_total_size(4); /* XFRMA_SAD_CNT */
1000}
1001
Alexey Dobriyane0710412010-01-23 13:37:10 +00001002static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001003 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001004{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001005 struct xfrmk_sadinfo si;
1006 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001007 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001008 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001009 u32 *f;
1010
Eric W. Biederman15e47302012-09-07 20:12:54 +00001011 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001012 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001013 return -EMSGSIZE;
1014
1015 f = nlmsg_data(nlh);
1016 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001017 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001018
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001019 sh.sadhmcnt = si.sadhmcnt;
1020 sh.sadhcnt = si.sadhcnt;
1021
David S. Miller1d1e34d2012-06-27 21:57:03 -07001022 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1023 if (!err)
1024 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1025 if (err) {
1026 nlmsg_cancel(skb, nlh);
1027 return err;
1028 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001029
1030 return nlmsg_end(skb, nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001031}
1032
1033static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001034 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001035{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001036 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001037 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001038 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001039 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001040 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001041
Thomas Graf7deb2262007-08-22 13:57:39 -07001042 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001043 if (r_skb == NULL)
1044 return -ENOMEM;
1045
Eric W. Biederman15e47302012-09-07 20:12:54 +00001046 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001047 BUG();
1048
Eric W. Biederman15e47302012-09-07 20:12:54 +00001049 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001050}
1051
Christoph Hellwig22e70052007-01-02 15:22:30 -08001052static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001053 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001055 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001056 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct xfrm_state *x;
1058 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001059 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001061 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (x == NULL)
1063 goto out_noput;
1064
1065 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1066 if (IS_ERR(resp_skb)) {
1067 err = PTR_ERR(resp_skb);
1068 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001069 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071 xfrm_state_put(x);
1072out_noput:
1073 return err;
1074}
1075
1076static int verify_userspi_info(struct xfrm_userspi_info *p)
1077{
1078 switch (p->info.id.proto) {
1079 case IPPROTO_AH:
1080 case IPPROTO_ESP:
1081 break;
1082
1083 case IPPROTO_COMP:
1084 /* IPCOMP spi is 16-bits. */
1085 if (p->max >= 0x10000)
1086 return -EINVAL;
1087 break;
1088
1089 default:
1090 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (p->min > p->max)
1094 return -EINVAL;
1095
1096 return 0;
1097}
1098
Christoph Hellwig22e70052007-01-02 15:22:30 -08001099static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001100 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001102 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 struct xfrm_state *x;
1104 struct xfrm_userspi_info *p;
1105 struct sk_buff *resp_skb;
1106 xfrm_address_t *daddr;
1107 int family;
1108 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001109 u32 mark;
1110 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Thomas Graf7b67c852007-08-22 13:53:52 -07001112 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 err = verify_userspi_info(p);
1114 if (err)
1115 goto out_noput;
1116
1117 family = p->info.family;
1118 daddr = &p->info.id.daddr;
1119
1120 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001121
1122 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001124 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001125 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 xfrm_state_put(x);
1127 x = NULL;
1128 }
1129 }
1130
1131 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001132 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 p->info.id.proto, daddr,
1134 &p->info.saddr, 1,
1135 family);
1136 err = -ENOENT;
1137 if (x == NULL)
1138 goto out_noput;
1139
Herbert Xu658b2192007-10-09 13:29:52 -07001140 err = xfrm_alloc_spi(x, p->min, p->max);
1141 if (err)
1142 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Herbert Xu658b2192007-10-09 13:29:52 -07001144 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (IS_ERR(resp_skb)) {
1146 err = PTR_ERR(resp_skb);
1147 goto out;
1148 }
1149
Eric W. Biederman15e47302012-09-07 20:12:54 +00001150 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152out:
1153 xfrm_state_put(x);
1154out_noput:
1155 return err;
1156}
1157
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001158static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 switch (dir) {
1161 case XFRM_POLICY_IN:
1162 case XFRM_POLICY_OUT:
1163 case XFRM_POLICY_FWD:
1164 break;
1165
1166 default:
1167 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 return 0;
1171}
1172
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001173static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001174{
1175 switch (type) {
1176 case XFRM_POLICY_TYPE_MAIN:
1177#ifdef CONFIG_XFRM_SUB_POLICY
1178 case XFRM_POLICY_TYPE_SUB:
1179#endif
1180 break;
1181
1182 default:
1183 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001184 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001185
1186 return 0;
1187}
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1190{
1191 switch (p->share) {
1192 case XFRM_SHARE_ANY:
1193 case XFRM_SHARE_SESSION:
1194 case XFRM_SHARE_USER:
1195 case XFRM_SHARE_UNIQUE:
1196 break;
1197
1198 default:
1199 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 switch (p->action) {
1203 case XFRM_POLICY_ALLOW:
1204 case XFRM_POLICY_BLOCK:
1205 break;
1206
1207 default:
1208 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 switch (p->sel.family) {
1212 case AF_INET:
1213 break;
1214
1215 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001216#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 break;
1218#else
1219 return -EAFNOSUPPORT;
1220#endif
1221
1222 default:
1223 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 return verify_policy_dir(p->dir);
1227}
1228
Thomas Graf5424f322007-08-22 14:01:33 -07001229static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001230{
Thomas Graf5424f322007-08-22 14:01:33 -07001231 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001232 struct xfrm_user_sec_ctx *uctx;
1233
1234 if (!rt)
1235 return 0;
1236
Thomas Graf5424f322007-08-22 14:01:33 -07001237 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001238 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1242 int nr)
1243{
1244 int i;
1245
1246 xp->xfrm_nr = nr;
1247 for (i = 0; i < nr; i++, ut++) {
1248 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1249
1250 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1251 memcpy(&t->saddr, &ut->saddr,
1252 sizeof(xfrm_address_t));
1253 t->reqid = ut->reqid;
1254 t->mode = ut->mode;
1255 t->share = ut->share;
1256 t->optional = ut->optional;
1257 t->aalgos = ut->aalgos;
1258 t->ealgos = ut->ealgos;
1259 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001260 /* If all masks are ~0, then we allow all algorithms. */
1261 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001262 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264}
1265
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001266static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1267{
1268 int i;
1269
1270 if (nr > XFRM_MAX_DEPTH)
1271 return -EINVAL;
1272
1273 for (i = 0; i < nr; i++) {
1274 /* We never validated the ut->family value, so many
1275 * applications simply leave it at zero. The check was
1276 * never made and ut->family was ignored because all
1277 * templates could be assumed to have the same family as
1278 * the policy itself. Now that we will have ipv4-in-ipv6
1279 * and ipv6-in-ipv4 tunnels, this is no longer true.
1280 */
1281 if (!ut[i].family)
1282 ut[i].family = family;
1283
1284 switch (ut[i].family) {
1285 case AF_INET:
1286 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001287#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001288 case AF_INET6:
1289 break;
1290#endif
1291 default:
1292 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001293 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001294 }
1295
1296 return 0;
1297}
1298
Thomas Graf5424f322007-08-22 14:01:33 -07001299static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Thomas Graf5424f322007-08-22 14:01:33 -07001301 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 if (!rt) {
1304 pol->xfrm_nr = 0;
1305 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001306 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1307 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001308 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001310 err = validate_tmpl(nr, utmpl, pol->family);
1311 if (err)
1312 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Thomas Graf5424f322007-08-22 14:01:33 -07001314 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316 return 0;
1317}
1318
Thomas Graf5424f322007-08-22 14:01:33 -07001319static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001320{
Thomas Graf5424f322007-08-22 14:01:33 -07001321 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001322 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001323 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001324 int err;
1325
1326 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001327 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001328 type = upt->type;
1329 }
1330
1331 err = verify_policy_type(type);
1332 if (err)
1333 return err;
1334
1335 *tp = type;
1336 return 0;
1337}
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1340{
1341 xp->priority = p->priority;
1342 xp->index = p->index;
1343 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1344 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1345 xp->action = p->action;
1346 xp->flags = p->flags;
1347 xp->family = p->sel.family;
1348 /* XXX xp->share = p->share; */
1349}
1350
1351static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1352{
Mathias Krause7b789832012-09-19 11:33:40 +00001353 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1355 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1356 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1357 p->priority = xp->priority;
1358 p->index = xp->index;
1359 p->sel.family = xp->family;
1360 p->dir = dir;
1361 p->action = xp->action;
1362 p->flags = xp->flags;
1363 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1364}
1365
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001366static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001368 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 int err;
1370
1371 if (!xp) {
1372 *errp = -ENOMEM;
1373 return NULL;
1374 }
1375
1376 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001377
Thomas Graf35a7aa02007-08-22 14:00:40 -07001378 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001379 if (err)
1380 goto error;
1381
Thomas Graf35a7aa02007-08-22 14:00:40 -07001382 if (!(err = copy_from_user_tmpl(xp, attrs)))
1383 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001384 if (err)
1385 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001387 xfrm_mark_get(attrs, &xp->mark);
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001390 error:
1391 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001392 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001393 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001394 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Christoph Hellwig22e70052007-01-02 15:22:30 -08001397static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001398 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001400 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001401 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001403 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 int err;
1405 int excl;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001406 kuid_t loginuid = audit_get_loginuid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001407 u32 sessionid = audit_get_sessionid(current);
1408 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 err = verify_newpolicy_info(p);
1411 if (err)
1412 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001413 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001414 if (err)
1415 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001417 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (!xp)
1419 return err;
1420
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001421 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001422 * Aha! this is anti-netlink really i.e more pfkey derived
1423 * in netlink excl is a flag and you wouldnt need
1424 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1426 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001427 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001428 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001431 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 kfree(xp);
1433 return err;
1434 }
1435
Herbert Xuf60f6b82005-06-18 22:44:37 -07001436 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001437 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001438 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001439 km_policy_notify(xp, p->dir, &c);
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 xfrm_pol_put(xp);
1442
1443 return 0;
1444}
1445
1446static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1447{
1448 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1449 int i;
1450
1451 if (xp->xfrm_nr == 0)
1452 return 0;
1453
1454 for (i = 0; i < xp->xfrm_nr; i++) {
1455 struct xfrm_user_tmpl *up = &vec[i];
1456 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1457
Mathias Krause1f868402012-09-19 11:33:41 +00001458 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001460 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1462 up->reqid = kp->reqid;
1463 up->mode = kp->mode;
1464 up->share = kp->share;
1465 up->optional = kp->optional;
1466 up->aalgos = kp->aalgos;
1467 up->ealgos = kp->ealgos;
1468 up->calgos = kp->calgos;
1469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Thomas Grafc0144be2007-08-22 13:55:43 -07001471 return nla_put(skb, XFRMA_TMPL,
1472 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001473}
1474
Serge Hallyn0d681622006-07-24 23:30:44 -07001475static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1476{
1477 if (x->security) {
1478 return copy_sec_ctx(x->security, skb);
1479 }
1480 return 0;
1481}
1482
1483static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1484{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001485 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001486 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001487 return 0;
1488}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001489static inline size_t userpolicy_type_attrsize(void)
1490{
1491#ifdef CONFIG_XFRM_SUB_POLICY
1492 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1493#else
1494 return 0;
1495#endif
1496}
Serge Hallyn0d681622006-07-24 23:30:44 -07001497
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001498#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001499static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001500{
Thomas Grafc0144be2007-08-22 13:55:43 -07001501 struct xfrm_userpolicy_type upt = {
1502 .type = type,
1503 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001504
Thomas Grafc0144be2007-08-22 13:55:43 -07001505 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001506}
1507
1508#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001509static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001510{
1511 return 0;
1512}
1513#endif
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1516{
1517 struct xfrm_dump_info *sp = ptr;
1518 struct xfrm_userpolicy_info *p;
1519 struct sk_buff *in_skb = sp->in_skb;
1520 struct sk_buff *skb = sp->out_skb;
1521 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001522 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Eric W. Biederman15e47302012-09-07 20:12:54 +00001524 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001525 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1526 if (nlh == NULL)
1527 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Thomas Graf7b67c852007-08-22 13:53:52 -07001529 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001531 err = copy_to_user_tmpl(xp, skb);
1532 if (!err)
1533 err = copy_to_user_sec_ctx(xp, skb);
1534 if (!err)
1535 err = copy_to_user_policy_type(xp->type, skb);
1536 if (!err)
1537 err = xfrm_mark_put(skb, &xp->mark);
1538 if (err) {
1539 nlmsg_cancel(skb, nlh);
1540 return err;
1541 }
Thomas Graf98250692007-08-22 12:47:26 -07001542 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
Timo Teras4c563f72008-02-28 21:31:08 -08001546static int xfrm_dump_policy_done(struct netlink_callback *cb)
1547{
1548 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1549
1550 xfrm_policy_walk_done(walk);
1551 return 0;
1552}
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1555{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001556 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001557 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 struct xfrm_dump_info info;
1559
Timo Teras4c563f72008-02-28 21:31:08 -08001560 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1561 sizeof(cb->args) - sizeof(cb->args[0]));
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 info.in_skb = cb->skb;
1564 info.out_skb = skb;
1565 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1566 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001567
1568 if (!cb->args[0]) {
1569 cb->args[0] = 1;
1570 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1571 }
1572
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001573 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 return skb->len;
1576}
1577
1578static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1579 struct xfrm_policy *xp,
1580 int dir, u32 seq)
1581{
1582 struct xfrm_dump_info info;
1583 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001584 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Thomas Graf7deb2262007-08-22 13:57:39 -07001586 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (!skb)
1588 return ERR_PTR(-ENOMEM);
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 info.in_skb = in_skb;
1591 info.out_skb = skb;
1592 info.nlmsg_seq = seq;
1593 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Mathias Krausec2546372012-09-14 09:58:32 +00001595 err = dump_one_policy(xp, dir, 0, &info);
1596 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001598 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
1600
1601 return skb;
1602}
1603
Christoph Hellwig22e70052007-01-02 15:22:30 -08001604static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001605 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001607 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 struct xfrm_policy *xp;
1609 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001610 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001612 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001614 struct xfrm_mark m;
1615 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Thomas Graf7b67c852007-08-22 13:53:52 -07001617 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1619
Thomas Graf35a7aa02007-08-22 14:00:40 -07001620 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001621 if (err)
1622 return err;
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 err = verify_policy_dir(p->dir);
1625 if (err)
1626 return err;
1627
1628 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001629 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001630 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001631 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001632 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001633
Thomas Graf35a7aa02007-08-22 14:00:40 -07001634 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001635 if (err)
1636 return err;
1637
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001638 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001639 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001640 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001641
Paul Moore03e1ad72008-04-12 19:07:52 -07001642 err = security_xfrm_policy_alloc(&ctx, uctx);
1643 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001644 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001645 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001646 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001647 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001648 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (xp == NULL)
1651 return -ENOENT;
1652
1653 if (!delete) {
1654 struct sk_buff *resp_skb;
1655
1656 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1657 if (IS_ERR(resp_skb)) {
1658 err = PTR_ERR(resp_skb);
1659 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001660 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001661 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001663 } else {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001664 kuid_t loginuid = audit_get_loginuid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001665 u32 sessionid = audit_get_sessionid(current);
1666 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001667
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001668 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001669 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1670 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001671
1672 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001673 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001674
Herbert Xue7443892005-06-18 22:44:18 -07001675 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001676 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001677 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001678 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001679 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001682out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001683 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001684 if (delete && err == 0)
1685 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return err;
1687}
1688
Christoph Hellwig22e70052007-01-02 15:22:30 -08001689static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001690 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001692 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001693 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001694 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001695 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001696 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001698 audit_info.loginuid = audit_get_loginuid(current);
1699 audit_info.sessionid = audit_get_sessionid(current);
1700 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001701 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001702 if (err) {
1703 if (err == -ESRCH) /* empty table */
1704 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001705 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001706 }
Herbert Xubf088672005-06-18 22:44:00 -07001707 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001708 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001709 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001710 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001711 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001712 km_state_notify(NULL, &c);
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return 0;
1715}
1716
Steffen Klassertd8647b72011-03-08 00:10:27 +00001717static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001718{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001719 size_t replay_size = x->replay_esn ?
1720 xfrm_replay_state_esn_len(x->replay_esn) :
1721 sizeof(struct xfrm_replay_state);
1722
Thomas Graf7deb2262007-08-22 13:57:39 -07001723 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001724 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001725 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001726 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001727 + nla_total_size(4) /* XFRM_AE_RTHR */
1728 + nla_total_size(4); /* XFRM_AE_ETHR */
1729}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001730
David S. Miller214e0052011-02-24 00:02:38 -05001731static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001732{
1733 struct xfrm_aevent_id *id;
1734 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001735 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001736
Eric W. Biederman15e47302012-09-07 20:12:54 +00001737 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001738 if (nlh == NULL)
1739 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001740
Thomas Graf7b67c852007-08-22 13:53:52 -07001741 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001742 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001743 id->sa_id.spi = x->id.spi;
1744 id->sa_id.family = x->props.family;
1745 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001746 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1747 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001748 id->flags = c->data.aevent;
1749
David S. Millerd0fde792012-03-29 04:02:26 -04001750 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001751 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1752 xfrm_replay_state_esn_len(x->replay_esn),
1753 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001754 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001755 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1756 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001757 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001758 if (err)
1759 goto out_cancel;
1760 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1761 if (err)
1762 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001763
David S. Miller1d1e34d2012-06-27 21:57:03 -07001764 if (id->flags & XFRM_AE_RTHR) {
1765 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1766 if (err)
1767 goto out_cancel;
1768 }
1769 if (id->flags & XFRM_AE_ETHR) {
1770 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1771 x->replay_maxage * 10 / HZ);
1772 if (err)
1773 goto out_cancel;
1774 }
1775 err = xfrm_mark_put(skb, &x->mark);
1776 if (err)
1777 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001778
Thomas Graf98250692007-08-22 12:47:26 -07001779 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001780
David S. Miller1d1e34d2012-06-27 21:57:03 -07001781out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001782 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001783 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001784}
1785
Christoph Hellwig22e70052007-01-02 15:22:30 -08001786static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001787 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001788{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001789 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001790 struct xfrm_state *x;
1791 struct sk_buff *r_skb;
1792 int err;
1793 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001794 u32 mark;
1795 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001796 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001797 struct xfrm_usersa_id *id = &p->sa_id;
1798
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001799 mark = xfrm_mark_get(attrs, &m);
1800
1801 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001802 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001803 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001804
1805 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1806 if (r_skb == NULL) {
1807 xfrm_state_put(x);
1808 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001809 }
1810
1811 /*
1812 * XXX: is this lock really needed - none of the other
1813 * gets lock (the concern is things getting updated
1814 * while we are still reading) - jhs
1815 */
1816 spin_lock_bh(&x->lock);
1817 c.data.aevent = p->flags;
1818 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001819 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001820
1821 if (build_aevent(r_skb, x, &c) < 0)
1822 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001823 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001824 spin_unlock_bh(&x->lock);
1825 xfrm_state_put(x);
1826 return err;
1827}
1828
Christoph Hellwig22e70052007-01-02 15:22:30 -08001829static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001830 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001831{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001832 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001833 struct xfrm_state *x;
1834 struct km_event c;
1835 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001836 u32 mark = 0;
1837 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001838 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001839 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001840 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001841 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001842
Steffen Klassertd8647b72011-03-08 00:10:27 +00001843 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001844 return err;
1845
1846 /* pedantic mode - thou shalt sayeth replaceth */
1847 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1848 return err;
1849
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001850 mark = xfrm_mark_get(attrs, &m);
1851
1852 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001853 if (x == NULL)
1854 return -ESRCH;
1855
1856 if (x->km.state != XFRM_STATE_VALID)
1857 goto out;
1858
Steffen Klasserte2b19122011-03-28 19:47:30 +00001859 err = xfrm_replay_verify_len(x->replay_esn, rp);
1860 if (err)
1861 goto out;
1862
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001863 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001864 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001865 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001866
1867 c.event = nlh->nlmsg_type;
1868 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001869 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001870 c.data.aevent = XFRM_AE_CU;
1871 km_state_notify(x, &c);
1872 err = 0;
1873out:
1874 xfrm_state_put(x);
1875 return err;
1876}
1877
Christoph Hellwig22e70052007-01-02 15:22:30 -08001878static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001879 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001881 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001882 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001883 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001884 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001885 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001886
Thomas Graf35a7aa02007-08-22 14:00:40 -07001887 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001888 if (err)
1889 return err;
1890
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001891 audit_info.loginuid = audit_get_loginuid(current);
1892 audit_info.sessionid = audit_get_sessionid(current);
1893 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001894 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001895 if (err) {
1896 if (err == -ESRCH) /* empty table */
1897 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001898 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001899 }
1900
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001901 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001902 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001903 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001904 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001905 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001906 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return 0;
1908}
1909
Christoph Hellwig22e70052007-01-02 15:22:30 -08001910static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001911 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001912{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001913 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001914 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001915 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001916 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001917 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001918 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001919 struct xfrm_mark m;
1920 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001921
Thomas Graf35a7aa02007-08-22 14:00:40 -07001922 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001923 if (err)
1924 return err;
1925
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001926 err = verify_policy_dir(p->dir);
1927 if (err)
1928 return err;
1929
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001930 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001931 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001932 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001933 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001934 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001935
Thomas Graf35a7aa02007-08-22 14:00:40 -07001936 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001937 if (err)
1938 return err;
1939
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001940 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001941 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001942 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001943
Paul Moore03e1ad72008-04-12 19:07:52 -07001944 err = security_xfrm_policy_alloc(&ctx, uctx);
1945 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001946 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001947 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001948 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001949 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001950 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001951 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001952 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001953 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001954
Timo Teräsea2dea92010-03-31 00:17:05 +00001955 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001956 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001957
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001958 err = 0;
1959 if (up->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001960 kuid_t loginuid = audit_get_loginuid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001961 u32 sessionid = audit_get_sessionid(current);
1962 u32 sid;
1963
1964 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001965 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001966 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001967
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001968 } else {
1969 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001970 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001971 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001972 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001973
1974out:
1975 xfrm_pol_put(xp);
1976 return err;
1977}
1978
Christoph Hellwig22e70052007-01-02 15:22:30 -08001979static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001980 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001981{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001982 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001983 struct xfrm_state *x;
1984 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001985 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001986 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001987 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001988 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001989
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001990 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001991
David S. Miller3a765aa2007-02-26 14:52:21 -08001992 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001993 if (x == NULL)
1994 return err;
1995
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001996 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001997 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001998 if (x->km.state != XFRM_STATE_VALID)
1999 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002000 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002001
Joy Latten161a09e2006-11-27 13:11:54 -06002002 if (ue->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002003 kuid_t loginuid = audit_get_loginuid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08002004 u32 sessionid = audit_get_sessionid(current);
2005 u32 sid;
2006
2007 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002008 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04002009 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06002010 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002011 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002012out:
2013 spin_unlock_bh(&x->lock);
2014 xfrm_state_put(x);
2015 return err;
2016}
2017
Christoph Hellwig22e70052007-01-02 15:22:30 -08002018static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002019 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002020{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002021 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002022 struct xfrm_policy *xp;
2023 struct xfrm_user_tmpl *ut;
2024 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002025 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002026 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002027
Thomas Graf7b67c852007-08-22 13:53:52 -07002028 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002029 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002030 int err = -ENOMEM;
2031
2032 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002033 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002034
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002035 xfrm_mark_get(attrs, &mark);
2036
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002037 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002038 if (err)
2039 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002040
2041 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002042 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002043 if (!xp)
2044 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002045
2046 memcpy(&x->id, &ua->id, sizeof(ua->id));
2047 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2048 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002049 xp->mark.m = x->mark.m = mark.m;
2050 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002051 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002052 /* extract the templates and for each call km_key */
2053 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2054 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2055 memcpy(&x->id, &t->id, sizeof(x->id));
2056 x->props.mode = t->mode;
2057 x->props.reqid = t->reqid;
2058 x->props.family = ut->family;
2059 t->aalgos = ua->aalgos;
2060 t->ealgos = ua->ealgos;
2061 t->calgos = ua->calgos;
2062 err = km_query(x, t, xp);
2063
2064 }
2065
2066 kfree(x);
2067 kfree(xp);
2068
2069 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002070
2071bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002072 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002073free_state:
2074 kfree(x);
2075nomem:
2076 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002077}
2078
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002079#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002080static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002081 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002082 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002083{
Thomas Graf5424f322007-08-22 14:01:33 -07002084 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002085 struct xfrm_user_migrate *um;
2086 int i, num_migrate;
2087
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002088 if (k != NULL) {
2089 struct xfrm_user_kmaddress *uk;
2090
2091 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2092 memcpy(&k->local, &uk->local, sizeof(k->local));
2093 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2094 k->family = uk->family;
2095 k->reserved = uk->reserved;
2096 }
2097
Thomas Graf5424f322007-08-22 14:01:33 -07002098 um = nla_data(rt);
2099 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002100
2101 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2102 return -EINVAL;
2103
2104 for (i = 0; i < num_migrate; i++, um++, ma++) {
2105 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2106 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2107 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2108 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2109
2110 ma->proto = um->proto;
2111 ma->mode = um->mode;
2112 ma->reqid = um->reqid;
2113
2114 ma->old_family = um->old_family;
2115 ma->new_family = um->new_family;
2116 }
2117
2118 *num = i;
2119 return 0;
2120}
2121
2122static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002123 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002124{
Thomas Graf7b67c852007-08-22 13:53:52 -07002125 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002126 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002127 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002128 u8 type;
2129 int err;
2130 int n = 0;
2131
Thomas Graf35a7aa02007-08-22 14:00:40 -07002132 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002133 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002134
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002135 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2136
Thomas Graf5424f322007-08-22 14:01:33 -07002137 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002138 if (err)
2139 return err;
2140
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002141 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002142 if (err)
2143 return err;
2144
2145 if (!n)
2146 return 0;
2147
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002148 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002149
2150 return 0;
2151}
2152#else
2153static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002154 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002155{
2156 return -ENOPROTOOPT;
2157}
2158#endif
2159
2160#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002161static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002162{
2163 struct xfrm_user_migrate um;
2164
2165 memset(&um, 0, sizeof(um));
2166 um.proto = m->proto;
2167 um.mode = m->mode;
2168 um.reqid = m->reqid;
2169 um.old_family = m->old_family;
2170 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2171 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2172 um.new_family = m->new_family;
2173 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2174 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2175
Thomas Grafc0144be2007-08-22 13:55:43 -07002176 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002177}
2178
David S. Miller183cad12011-02-24 00:28:01 -05002179static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002180{
2181 struct xfrm_user_kmaddress uk;
2182
2183 memset(&uk, 0, sizeof(uk));
2184 uk.family = k->family;
2185 uk.reserved = k->reserved;
2186 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002187 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002188
2189 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2190}
2191
2192static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002193{
2194 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002195 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2196 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2197 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002198}
2199
David S. Miller183cad12011-02-24 00:28:01 -05002200static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2201 int num_migrate, const struct xfrm_kmaddress *k,
2202 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002203{
David S. Miller183cad12011-02-24 00:28:01 -05002204 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002205 struct xfrm_userpolicy_id *pol_id;
2206 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002207 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002208
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002209 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2210 if (nlh == NULL)
2211 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002212
Thomas Graf7b67c852007-08-22 13:53:52 -07002213 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002214 /* copy data from selector, dir, and type to the pol_id */
2215 memset(pol_id, 0, sizeof(*pol_id));
2216 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2217 pol_id->dir = dir;
2218
David S. Miller1d1e34d2012-06-27 21:57:03 -07002219 if (k != NULL) {
2220 err = copy_to_user_kmaddress(k, skb);
2221 if (err)
2222 goto out_cancel;
2223 }
2224 err = copy_to_user_policy_type(type, skb);
2225 if (err)
2226 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002227 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002228 err = copy_to_user_migrate(mp, skb);
2229 if (err)
2230 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002231 }
2232
Thomas Graf98250692007-08-22 12:47:26 -07002233 return nlmsg_end(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002234
2235out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002236 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002237 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002238}
2239
David S. Miller183cad12011-02-24 00:28:01 -05002240static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2241 const struct xfrm_migrate *m, int num_migrate,
2242 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002243{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002244 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002245 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002246
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002247 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002248 if (skb == NULL)
2249 return -ENOMEM;
2250
2251 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002252 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002253 BUG();
2254
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002255 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002256}
2257#else
David S. Miller183cad12011-02-24 00:28:01 -05002258static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2259 const struct xfrm_migrate *m, int num_migrate,
2260 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002261{
2262 return -ENOPROTOOPT;
2263}
2264#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002265
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002266#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002267
2268static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002269 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002270 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2271 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2272 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2273 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2274 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2275 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002276 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002277 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002278 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002279 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002280 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002281 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002282 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002283 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2284 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002285 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002286 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002287 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2288 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289};
2290
Thomas Graf492b5582005-05-03 14:26:40 -07002291#undef XMSGSIZE
2292
Thomas Grafcf5cb792007-08-22 13:59:04 -07002293static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002294 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2295 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2296 [XFRMA_LASTUSED] = { .type = NLA_U64},
2297 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002298 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002299 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2300 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2301 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2302 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2303 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2304 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2305 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2306 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2307 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2308 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2309 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2310 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2311 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2312 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002313 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002314 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002315 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002316 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002317 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002318};
2319
Mathias Krause05600a72013-02-24 14:10:27 +01002320static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002321 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002323 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002324} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2325 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2326 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2327 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002328 .dump = xfrm_dump_sa,
2329 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002330 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2331 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2332 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002333 .dump = xfrm_dump_policy,
2334 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002335 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002336 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002337 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002338 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2339 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002340 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002341 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2342 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002343 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2344 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002345 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002346 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002347 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348};
2349
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002350static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002352 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002353 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002354 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002355 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 type -= XFRM_MSG_BASE;
2362 link = &xfrm_dispatch[type];
2363
2364 /* All operations require privileges, even GET */
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002365 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002366 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Thomas Graf492b5582005-05-03 14:26:40 -07002368 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2369 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002370 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002372 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002374 {
2375 struct netlink_dump_control c = {
2376 .dump = link->dump,
2377 .done = link->done,
2378 };
2379 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382
Thomas Graf35a7aa02007-08-22 14:00:40 -07002383 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002384 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002385 if (err < 0)
2386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002389 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Thomas Graf5424f322007-08-22 14:01:33 -07002391 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002394static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002396 mutex_lock(&xfrm_cfg_mutex);
2397 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2398 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
2400
Thomas Graf7deb2262007-08-22 13:57:39 -07002401static inline size_t xfrm_expire_msgsize(void)
2402{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002403 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2404 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002405}
2406
David S. Miller214e0052011-02-24 00:02:38 -05002407static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
2409 struct xfrm_user_expire *ue;
2410 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002411 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Eric W. Biederman15e47302012-09-07 20:12:54 +00002413 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002414 if (nlh == NULL)
2415 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Thomas Graf7b67c852007-08-22 13:53:52 -07002417 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002419 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
David S. Miller1d1e34d2012-06-27 21:57:03 -07002421 err = xfrm_mark_put(skb, &x->mark);
2422 if (err)
2423 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002424
Thomas Graf98250692007-08-22 12:47:26 -07002425 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
David S. Miller214e0052011-02-24 00:02:38 -05002428static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002430 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 struct sk_buff *skb;
2432
Thomas Graf7deb2262007-08-22 13:57:39 -07002433 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 if (skb == NULL)
2435 return -ENOMEM;
2436
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002437 if (build_expire(skb, x, c) < 0) {
2438 kfree_skb(skb);
2439 return -EMSGSIZE;
2440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002442 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
David S. Miller214e0052011-02-24 00:02:38 -05002445static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002446{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002447 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002448 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002449
Steffen Klassertd8647b72011-03-08 00:10:27 +00002450 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002451 if (skb == NULL)
2452 return -ENOMEM;
2453
2454 if (build_aevent(skb, x, c) < 0)
2455 BUG();
2456
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002457 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002458}
2459
David S. Miller214e0052011-02-24 00:02:38 -05002460static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002461{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002462 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002463 struct xfrm_usersa_flush *p;
2464 struct nlmsghdr *nlh;
2465 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002466 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002467
Thomas Graf7deb2262007-08-22 13:57:39 -07002468 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002469 if (skb == NULL)
2470 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002471
Eric W. Biederman15e47302012-09-07 20:12:54 +00002472 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002473 if (nlh == NULL) {
2474 kfree_skb(skb);
2475 return -EMSGSIZE;
2476 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002477
Thomas Graf7b67c852007-08-22 13:53:52 -07002478 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002479 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002480
Thomas Graf98250692007-08-22 12:47:26 -07002481 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002482
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002483 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002484}
2485
Thomas Graf7deb2262007-08-22 13:57:39 -07002486static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002487{
Thomas Graf7deb2262007-08-22 13:57:39 -07002488 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002489 if (x->aead)
2490 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002491 if (x->aalg) {
2492 l += nla_total_size(sizeof(struct xfrm_algo) +
2493 (x->aalg->alg_key_len + 7) / 8);
2494 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2495 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002496 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002497 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002498 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002499 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002500 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002501 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002502 if (x->tfcpad)
2503 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002504 if (x->replay_esn)
2505 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002506 if (x->security)
2507 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2508 x->security->ctx_len);
2509 if (x->coaddr)
2510 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002511 if (x->props.extra_flags)
2512 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002513
Herbert Xud26f3982007-11-13 21:47:08 -08002514 /* Must count x->lastused as it may become non-zero behind our back. */
2515 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002516
2517 return l;
2518}
2519
David S. Miller214e0052011-02-24 00:02:38 -05002520static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002521{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002522 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002523 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002524 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002525 struct nlmsghdr *nlh;
2526 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002527 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002528 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002529
2530 headlen = sizeof(*p);
2531 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002532 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002533 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002534 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002535 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002536 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002537
Thomas Graf7deb2262007-08-22 13:57:39 -07002538 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002539 if (skb == NULL)
2540 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002541
Eric W. Biederman15e47302012-09-07 20:12:54 +00002542 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002543 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002544 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002545 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002546
Thomas Graf7b67c852007-08-22 13:53:52 -07002547 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002548 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002549 struct nlattr *attr;
2550
Thomas Graf7b67c852007-08-22 13:53:52 -07002551 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002552 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2553 id->spi = x->id.spi;
2554 id->family = x->props.family;
2555 id->proto = x->id.proto;
2556
Thomas Grafc0144be2007-08-22 13:55:43 -07002557 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002558 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002559 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002560 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002561
2562 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002563 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002564 err = copy_to_user_state_extra(x, p, skb);
2565 if (err)
2566 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002567
Thomas Graf98250692007-08-22 12:47:26 -07002568 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002569
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002570 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002571
David S. Miller1d1e34d2012-06-27 21:57:03 -07002572out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002573 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002574 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002575}
2576
David S. Miller214e0052011-02-24 00:02:38 -05002577static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002578{
2579
2580 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002581 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002582 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002583 case XFRM_MSG_NEWAE:
2584 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002585 case XFRM_MSG_DELSA:
2586 case XFRM_MSG_UPDSA:
2587 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002588 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002589 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002590 return xfrm_notify_sa_flush(c);
2591 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002592 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2593 c->event);
2594 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002595 }
2596
2597 return 0;
2598
2599}
2600
Thomas Graf7deb2262007-08-22 13:57:39 -07002601static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2602 struct xfrm_policy *xp)
2603{
2604 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2605 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002606 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002607 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2608 + userpolicy_type_attrsize();
2609}
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002612 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002614 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 struct xfrm_user_acquire *ua;
2616 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002617 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002619 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2620 if (nlh == NULL)
2621 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Thomas Graf7b67c852007-08-22 13:53:52 -07002623 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 memcpy(&ua->id, &x->id, sizeof(ua->id));
2625 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2626 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002627 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 ua->aalgos = xt->aalgos;
2629 ua->ealgos = xt->ealgos;
2630 ua->calgos = xt->calgos;
2631 ua->seq = x->km.seq = seq;
2632
David S. Miller1d1e34d2012-06-27 21:57:03 -07002633 err = copy_to_user_tmpl(xp, skb);
2634 if (!err)
2635 err = copy_to_user_state_sec_ctx(x, skb);
2636 if (!err)
2637 err = copy_to_user_policy_type(xp->type, skb);
2638 if (!err)
2639 err = xfrm_mark_put(skb, &xp->mark);
2640 if (err) {
2641 nlmsg_cancel(skb, nlh);
2642 return err;
2643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Thomas Graf98250692007-08-22 12:47:26 -07002645 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647
2648static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002649 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002651 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Thomas Graf7deb2262007-08-22 13:57:39 -07002654 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 if (skb == NULL)
2656 return -ENOMEM;
2657
Fan Du65e07362012-08-15 10:13:47 +08002658 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 BUG();
2660
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002661 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
2664/* User gives us xfrm_user_policy_info followed by an array of 0
2665 * or more templates.
2666 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002667static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 u8 *data, int len, int *dir)
2669{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002670 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2672 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2673 struct xfrm_policy *xp;
2674 int nr;
2675
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002676 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 case AF_INET:
2678 if (opt != IP_XFRM_POLICY) {
2679 *dir = -EOPNOTSUPP;
2680 return NULL;
2681 }
2682 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002683#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 case AF_INET6:
2685 if (opt != IPV6_XFRM_POLICY) {
2686 *dir = -EOPNOTSUPP;
2687 return NULL;
2688 }
2689 break;
2690#endif
2691 default:
2692 *dir = -EINVAL;
2693 return NULL;
2694 }
2695
2696 *dir = -EINVAL;
2697
2698 if (len < sizeof(*p) ||
2699 verify_newpolicy_info(p))
2700 return NULL;
2701
2702 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002703 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return NULL;
2705
Herbert Xua4f1bac2005-07-26 15:43:17 -07002706 if (p->dir > XFRM_POLICY_OUT)
2707 return NULL;
2708
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002709 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 if (xp == NULL) {
2711 *dir = -ENOBUFS;
2712 return NULL;
2713 }
2714
2715 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002716 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 copy_templates(xp, ut, nr);
2718
2719 *dir = p->dir;
2720
2721 return xp;
2722}
2723
Thomas Graf7deb2262007-08-22 13:57:39 -07002724static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2725{
2726 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2727 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2728 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002729 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002730 + userpolicy_type_attrsize();
2731}
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002734 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
2736 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002737 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002738 struct nlmsghdr *nlh;
2739 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Eric W. Biederman15e47302012-09-07 20:12:54 +00002741 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002742 if (nlh == NULL)
2743 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Thomas Graf7b67c852007-08-22 13:53:52 -07002745 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002747 err = copy_to_user_tmpl(xp, skb);
2748 if (!err)
2749 err = copy_to_user_sec_ctx(xp, skb);
2750 if (!err)
2751 err = copy_to_user_policy_type(xp->type, skb);
2752 if (!err)
2753 err = xfrm_mark_put(skb, &xp->mark);
2754 if (err) {
2755 nlmsg_cancel(skb, nlh);
2756 return err;
2757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 upe->hard = !!hard;
2759
Thomas Graf98250692007-08-22 12:47:26 -07002760 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
David S. Miller214e0052011-02-24 00:02:38 -05002763static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002765 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Thomas Graf7deb2262007-08-22 13:57:39 -07002768 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (skb == NULL)
2770 return -ENOMEM;
2771
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002772 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 BUG();
2774
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002775 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
David S. Miller214e0052011-02-24 00:02:38 -05002778static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002779{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002780 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002781 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002782 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002783 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002784 struct nlmsghdr *nlh;
2785 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002786 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002787
2788 headlen = sizeof(*p);
2789 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002790 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002791 headlen = sizeof(*id);
2792 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002793 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002794 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002795 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002796
Thomas Graf7deb2262007-08-22 13:57:39 -07002797 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002798 if (skb == NULL)
2799 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002800
Eric W. Biederman15e47302012-09-07 20:12:54 +00002801 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002802 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002803 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002804 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002805
Thomas Graf7b67c852007-08-22 13:53:52 -07002806 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002807 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002808 struct nlattr *attr;
2809
Thomas Graf7b67c852007-08-22 13:53:52 -07002810 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002811 memset(id, 0, sizeof(*id));
2812 id->dir = dir;
2813 if (c->data.byid)
2814 id->index = xp->index;
2815 else
2816 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2817
Thomas Grafc0144be2007-08-22 13:55:43 -07002818 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002819 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002820 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002821 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002822
2823 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002824 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002825
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002826 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002827 err = copy_to_user_tmpl(xp, skb);
2828 if (!err)
2829 err = copy_to_user_policy_type(xp->type, skb);
2830 if (!err)
2831 err = xfrm_mark_put(skb, &xp->mark);
2832 if (err)
2833 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002834
Thomas Graf98250692007-08-22 12:47:26 -07002835 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002836
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002837 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002838
David S. Miller1d1e34d2012-06-27 21:57:03 -07002839out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002840 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002841 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002842}
2843
David S. Miller214e0052011-02-24 00:02:38 -05002844static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002845{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002846 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002847 struct nlmsghdr *nlh;
2848 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002849 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002850
Thomas Graf7deb2262007-08-22 13:57:39 -07002851 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002852 if (skb == NULL)
2853 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002854
Eric W. Biederman15e47302012-09-07 20:12:54 +00002855 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002856 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002857 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002858 goto out_free_skb;
2859 err = copy_to_user_policy_type(c->data.type, skb);
2860 if (err)
2861 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002862
Thomas Graf98250692007-08-22 12:47:26 -07002863 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002864
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002865 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002866
David S. Miller1d1e34d2012-06-27 21:57:03 -07002867out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002868 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002869 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002870}
2871
David S. Miller214e0052011-02-24 00:02:38 -05002872static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002873{
2874
2875 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002876 case XFRM_MSG_NEWPOLICY:
2877 case XFRM_MSG_UPDPOLICY:
2878 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002879 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002880 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002881 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002882 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002883 return xfrm_exp_policy_notify(xp, dir, c);
2884 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002885 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2886 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002887 }
2888
2889 return 0;
2890
2891}
2892
Thomas Graf7deb2262007-08-22 13:57:39 -07002893static inline size_t xfrm_report_msgsize(void)
2894{
2895 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2896}
2897
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002898static int build_report(struct sk_buff *skb, u8 proto,
2899 struct xfrm_selector *sel, xfrm_address_t *addr)
2900{
2901 struct xfrm_user_report *ur;
2902 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002903
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002904 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2905 if (nlh == NULL)
2906 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002907
Thomas Graf7b67c852007-08-22 13:53:52 -07002908 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002909 ur->proto = proto;
2910 memcpy(&ur->sel, sel, sizeof(ur->sel));
2911
David S. Miller1d1e34d2012-06-27 21:57:03 -07002912 if (addr) {
2913 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2914 if (err) {
2915 nlmsg_cancel(skb, nlh);
2916 return err;
2917 }
2918 }
Thomas Graf98250692007-08-22 12:47:26 -07002919 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002920}
2921
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002922static int xfrm_send_report(struct net *net, u8 proto,
2923 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002924{
2925 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002926
Thomas Graf7deb2262007-08-22 13:57:39 -07002927 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002928 if (skb == NULL)
2929 return -ENOMEM;
2930
2931 if (build_report(skb, proto, sel, addr) < 0)
2932 BUG();
2933
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002934 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002935}
2936
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002937static inline size_t xfrm_mapping_msgsize(void)
2938{
2939 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2940}
2941
2942static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2943 xfrm_address_t *new_saddr, __be16 new_sport)
2944{
2945 struct xfrm_user_mapping *um;
2946 struct nlmsghdr *nlh;
2947
2948 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2949 if (nlh == NULL)
2950 return -EMSGSIZE;
2951
2952 um = nlmsg_data(nlh);
2953
2954 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2955 um->id.spi = x->id.spi;
2956 um->id.family = x->props.family;
2957 um->id.proto = x->id.proto;
2958 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2959 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2960 um->new_sport = new_sport;
2961 um->old_sport = x->encap->encap_sport;
2962 um->reqid = x->props.reqid;
2963
2964 return nlmsg_end(skb, nlh);
2965}
2966
2967static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2968 __be16 sport)
2969{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002970 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002971 struct sk_buff *skb;
2972
2973 if (x->id.proto != IPPROTO_ESP)
2974 return -EINVAL;
2975
2976 if (!x->encap)
2977 return -EINVAL;
2978
2979 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2980 if (skb == NULL)
2981 return -ENOMEM;
2982
2983 if (build_mapping(skb, x, ipaddr, sport) < 0)
2984 BUG();
2985
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002986 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002987}
2988
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989static struct xfrm_mgr netlink_mgr = {
2990 .id = "netlink",
2991 .notify = xfrm_send_state_notify,
2992 .acquire = xfrm_send_acquire,
2993 .compile_policy = xfrm_compile_policy,
2994 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002995 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002996 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002997 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998};
2999
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003000static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
Patrick McHardybe336902006-03-20 22:40:54 -08003002 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003003 struct netlink_kernel_cfg cfg = {
3004 .groups = XFRMNLGRP_MAX,
3005 .input = xfrm_netlink_rcv,
3006 };
Patrick McHardybe336902006-03-20 22:40:54 -08003007
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003008 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003009 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003011 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003012 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 return 0;
3014}
3015
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003016static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003017{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003018 struct net *net;
3019 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003020 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003021 synchronize_net();
3022 list_for_each_entry(net, net_exit_list, exit_list)
3023 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003024}
3025
3026static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003027 .init = xfrm_user_net_init,
3028 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003029};
3030
3031static int __init xfrm_user_init(void)
3032{
3033 int rv;
3034
3035 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3036
3037 rv = register_pernet_subsys(&xfrm_user_net_ops);
3038 if (rv < 0)
3039 return rv;
3040 rv = xfrm_register_km(&netlink_mgr);
3041 if (rv < 0)
3042 unregister_pernet_subsys(&xfrm_user_net_ops);
3043 return rv;
3044}
3045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046static void __exit xfrm_user_exit(void)
3047{
3048 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003049 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
3052module_init(xfrm_user_init);
3053module_exit(xfrm_user_exit);
3054MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003055MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003056