blob: 3d1cd36befa73e34a08ca5ce906ac998da21b0a7 [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
Sowmini Varadhane33d4f12015-10-21 11:48:25 -040034#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Thomas Graf5424f322007-08-22 14:01:33 -070036static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Thomas Graf5424f322007-08-22 14:01:33 -070038 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct xfrm_algo *algp;
40
41 if (!rt)
42 return 0;
43
Thomas Graf5424f322007-08-22 14:01:33 -070044 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080045 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070046 return -EINVAL;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 switch (type) {
49 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 break;
53
54 default:
55 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
59 return 0;
60}
61
Martin Willi4447bb32009-11-25 00:29:52 +000062static int verify_auth_trunc(struct nlattr **attrs)
63{
64 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
65 struct xfrm_algo_auth *algp;
66
67 if (!rt)
68 return 0;
69
70 algp = nla_data(rt);
71 if (nla_len(rt) < xfrm_alg_auth_len(algp))
72 return -EINVAL;
73
74 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
75 return 0;
76}
77
Herbert Xu1a6509d2008-01-28 19:37:29 -080078static int verify_aead(struct nlattr **attrs)
79{
80 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
81 struct xfrm_algo_aead *algp;
82
83 if (!rt)
84 return 0;
85
86 algp = nla_data(rt);
87 if (nla_len(rt) < aead_len(algp))
88 return -EINVAL;
89
90 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
91 return 0;
92}
93
Thomas Graf5424f322007-08-22 14:01:33 -070094static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070095 xfrm_address_t **addrp)
96{
Thomas Graf5424f322007-08-22 14:01:33 -070097 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070098
Thomas Grafcf5cb792007-08-22 13:59:04 -070099 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700100 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700101}
Trent Jaegerdf718372005-12-13 23:12:27 -0800102
Thomas Graf5424f322007-08-22 14:01:33 -0700103static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800104{
Thomas Graf5424f322007-08-22 14:01:33 -0700105 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800106 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800107
108 if (!rt)
109 return 0;
110
Thomas Graf5424f322007-08-22 14:01:33 -0700111 uctx = nla_data(rt);
Xin Longfa40d122020-02-09 21:15:29 +0800112 if (uctx->len > nla_len(rt) ||
113 uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800114 return -EINVAL;
115
116 return 0;
117}
118
Steffen Klassertd8647b72011-03-08 00:10:27 +0000119static inline int verify_replay(struct xfrm_usersa_info *p,
120 struct nlattr **attrs)
121{
122 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000123 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000124
125 if (!rt)
Florian Westphal0355a9f2018-02-12 14:42:01 +0100126 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
127
128 rs = nla_data(rt);
129
130 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
131 return -EINVAL;
132
133 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
134 nla_len(rt) != sizeof(*rs))
135 return -EINVAL;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000136
Fan Du01714102014-01-18 09:54:28 +0800137 /* As only ESP and AH support ESN feature. */
138 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
Steffen Klassert02aadf72011-03-28 19:48:09 +0000139 return -EINVAL;
140
Steffen Klassertd8647b72011-03-08 00:10:27 +0000141 if (p->replay_window != 0)
142 return -EINVAL;
143
144 return 0;
145}
Trent Jaegerdf718372005-12-13 23:12:27 -0800146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700148 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 int err;
151
152 err = -EINVAL;
153 switch (p->family) {
154 case AF_INET:
Anirudh Gupta92a63c22019-05-21 20:59:47 +0530155 break;
156
157 case AF_INET6:
158#if IS_ENABLED(CONFIG_IPV6)
159 break;
160#else
161 err = -EAFNOSUPPORT;
162 goto out;
163#endif
164
165 default:
166 goto out;
167 }
168
169 switch (p->sel.family) {
Nicolas Dichtel2d0dbd02019-06-14 11:13:55 +0200170 case AF_UNSPEC:
171 break;
172
Anirudh Gupta92a63c22019-05-21 20:59:47 +0530173 case AF_INET:
Steffen Klassert89cb86e2018-08-01 13:45:11 +0200174 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
175 goto out;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 break;
178
179 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000180#if IS_ENABLED(CONFIG_IPV6)
Steffen Klassert89cb86e2018-08-01 13:45:11 +0200181 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
182 goto out;
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185#else
186 err = -EAFNOSUPPORT;
187 goto out;
188#endif
189
190 default:
191 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 err = -EINVAL;
195 switch (p->id.proto) {
196 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000197 if ((!attrs[XFRMA_ALG_AUTH] &&
198 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800199 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700200 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000201 attrs[XFRMA_ALG_COMP] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200202 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 goto out;
204 break;
205
206 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800207 if (attrs[XFRMA_ALG_COMP])
208 goto out;
209 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000210 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800211 !attrs[XFRMA_ALG_CRYPT] &&
212 !attrs[XFRMA_ALG_AEAD])
213 goto out;
214 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000215 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800216 attrs[XFRMA_ALG_CRYPT]) &&
217 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000219 if (attrs[XFRMA_TFCPAD] &&
220 p->mode != XFRM_MODE_TUNNEL)
221 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 break;
223
224 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700225 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800226 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700227 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000228 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000229 attrs[XFRMA_ALG_CRYPT] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200230 attrs[XFRMA_TFCPAD] ||
231 (ntohl(p->id.spi) >= 0x10000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 goto out;
233 break;
234
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000235#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700236 case IPPROTO_DSTOPTS:
237 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700238 if (attrs[XFRMA_ALG_COMP] ||
239 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000240 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800241 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700242 attrs[XFRMA_ALG_CRYPT] ||
243 attrs[XFRMA_ENCAP] ||
244 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000245 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700246 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700247 goto out;
248 break;
249#endif
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 default:
252 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Herbert Xu1a6509d2008-01-28 19:37:29 -0800255 if ((err = verify_aead(attrs)))
256 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000257 if ((err = verify_auth_trunc(attrs)))
258 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700259 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700261 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700263 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700265 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800266 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000267 if ((err = verify_replay(p, attrs)))
268 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 err = -EINVAL;
271 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700272 case XFRM_MODE_TRANSPORT:
273 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700274 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700275 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277
278 default:
279 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 err = 0;
283
284out:
285 return err;
286}
287
288static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800289 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700290 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct xfrm_algo *p, *ualg;
293 struct xfrm_algo_desc *algo;
294
295 if (!rta)
296 return 0;
297
Thomas Graf5424f322007-08-22 14:01:33 -0700298 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 algo = get_byname(ualg->alg_name, 1);
301 if (!algo)
302 return -ENOSYS;
303 *props = algo->desc.sadb_alg_id;
304
Eric Dumazet0f99be02008-01-08 23:39:06 -0800305 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!p)
307 return -ENOMEM;
308
Herbert Xu04ff1262006-08-13 08:50:00 +1000309 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 *algpp = p;
311 return 0;
312}
313
Herbert Xu69b01372015-05-27 16:03:45 +0800314static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
315{
316 struct xfrm_algo *p, *ualg;
317 struct xfrm_algo_desc *algo;
318
319 if (!rta)
320 return 0;
321
322 ualg = nla_data(rta);
323
324 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
325 if (!algo)
326 return -ENOSYS;
327 x->props.ealgo = algo->desc.sadb_alg_id;
328
329 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
330 if (!p)
331 return -ENOMEM;
332
333 strcpy(p->alg_name, algo->name);
334 x->ealg = p;
335 x->geniv = algo->uinfo.encr.geniv;
336 return 0;
337}
338
Martin Willi4447bb32009-11-25 00:29:52 +0000339static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
340 struct nlattr *rta)
341{
342 struct xfrm_algo *ualg;
343 struct xfrm_algo_auth *p;
344 struct xfrm_algo_desc *algo;
345
346 if (!rta)
347 return 0;
348
349 ualg = nla_data(rta);
350
351 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
352 if (!algo)
353 return -ENOSYS;
354 *props = algo->desc.sadb_alg_id;
355
356 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
357 if (!p)
358 return -ENOMEM;
359
360 strcpy(p->alg_name, algo->name);
361 p->alg_key_len = ualg->alg_key_len;
362 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
363 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
364
365 *algpp = p;
366 return 0;
367}
368
369static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
370 struct nlattr *rta)
371{
372 struct xfrm_algo_auth *p, *ualg;
373 struct xfrm_algo_desc *algo;
374
375 if (!rta)
376 return 0;
377
378 ualg = nla_data(rta);
379
380 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
381 if (!algo)
382 return -ENOSYS;
Herbert Xu689f1c92014-09-18 16:38:18 +0800383 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000384 return -EINVAL;
385 *props = algo->desc.sadb_alg_id;
386
387 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
388 if (!p)
389 return -ENOMEM;
390
391 strcpy(p->alg_name, algo->name);
392 if (!p->alg_trunc_len)
393 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
394
395 *algpp = p;
396 return 0;
397}
398
Herbert Xu69b01372015-05-27 16:03:45 +0800399static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800400{
401 struct xfrm_algo_aead *p, *ualg;
402 struct xfrm_algo_desc *algo;
403
404 if (!rta)
405 return 0;
406
407 ualg = nla_data(rta);
408
409 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
410 if (!algo)
411 return -ENOSYS;
Herbert Xu69b01372015-05-27 16:03:45 +0800412 x->props.ealgo = algo->desc.sadb_alg_id;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800413
414 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
415 if (!p)
416 return -ENOMEM;
417
418 strcpy(p->alg_name, algo->name);
Herbert Xu69b01372015-05-27 16:03:45 +0800419 x->aead = p;
420 x->geniv = algo->uinfo.aead.geniv;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800421 return 0;
422}
423
Steffen Klasserte2b19122011-03-28 19:47:30 +0000424static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
425 struct nlattr *rp)
426{
427 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000428 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000429
430 if (!replay_esn || !rp)
431 return 0;
432
433 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000434 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000435
Andy Whitcroft79191ea2017-03-23 07:45:44 +0000436 /* Check the overall length and the internal bitmap length to avoid
437 * potential overflow. */
438 if (nla_len(rp) < ulen ||
439 xfrm_replay_state_esn_len(replay_esn) != ulen ||
440 replay_esn->bmp_len != up->bmp_len)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000441 return -EINVAL;
442
Andy Whitcroft64a54652017-03-22 07:29:31 +0000443 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
444 return -EINVAL;
445
Steffen Klasserte2b19122011-03-28 19:47:30 +0000446 return 0;
447}
448
Steffen Klassertd8647b72011-03-08 00:10:27 +0000449static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
450 struct xfrm_replay_state_esn **preplay_esn,
451 struct nlattr *rta)
452{
453 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000454 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000455
456 if (!rta)
457 return 0;
458
459 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000460 klen = xfrm_replay_state_esn_len(up);
461 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000462
Mathias Krauseecd79182012-09-20 10:01:49 +0000463 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000464 if (!p)
465 return -ENOMEM;
466
Mathias Krauseecd79182012-09-20 10:01:49 +0000467 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000468 if (!pp) {
469 kfree(p);
470 return -ENOMEM;
471 }
472
Mathias Krauseecd79182012-09-20 10:01:49 +0000473 memcpy(p, up, ulen);
474 memcpy(pp, up, ulen);
475
Steffen Klassertd8647b72011-03-08 00:10:27 +0000476 *replay_esn = p;
477 *preplay_esn = pp;
478
479 return 0;
480}
481
Joy Latten661697f2007-04-13 16:14:35 -0700482static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800483{
Trent Jaegerdf718372005-12-13 23:12:27 -0800484 int len = 0;
485
486 if (xfrm_ctx) {
487 len += sizeof(struct xfrm_user_sec_ctx);
488 len += xfrm_ctx->ctx_len;
489 }
490 return len;
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
494{
495 memcpy(&x->id, &p->id, sizeof(x->id));
496 memcpy(&x->sel, &p->sel, sizeof(x->sel));
497 memcpy(&x->lft, &p->lft, sizeof(x->lft));
498 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800499 x->props.replay_window = min_t(unsigned int, p->replay_window,
500 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 x->props.reqid = p->reqid;
502 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700503 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700505
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700506 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700507 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800510/*
511 * someday when pfkey also has support, we could have the code
512 * somehow made shareable and move it to xfrm_state.c - JHS
513 *
514*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000515static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
516 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800517{
Thomas Graf5424f322007-08-22 14:01:33 -0700518 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000519 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700520 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
521 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
522 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800523
Steffen Klassertd8647b72011-03-08 00:10:27 +0000524 if (re) {
525 struct xfrm_replay_state_esn *replay_esn;
526 replay_esn = nla_data(re);
527 memcpy(x->replay_esn, replay_esn,
528 xfrm_replay_state_esn_len(replay_esn));
529 memcpy(x->preplay_esn, replay_esn,
530 xfrm_replay_state_esn_len(replay_esn));
531 }
532
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800533 if (rp) {
534 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700535 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800536 memcpy(&x->replay, replay, sizeof(*replay));
537 memcpy(&x->preplay, replay, sizeof(*replay));
538 }
539
540 if (lt) {
541 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700542 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800543 x->curlft.bytes = ltime->bytes;
544 x->curlft.packets = ltime->packets;
545 x->curlft.add_time = ltime->add_time;
546 x->curlft.use_time = ltime->use_time;
547 }
548
Thomas Grafcf5cb792007-08-22 13:59:04 -0700549 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700550 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800551
Thomas Grafcf5cb792007-08-22 13:59:04 -0700552 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700553 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800554}
555
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800556static struct xfrm_state *xfrm_state_construct(struct net *net,
557 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700558 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int *errp)
560{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800561 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int err = -ENOMEM;
563
564 if (!x)
565 goto error_no_put;
566
567 copy_from_user_state(x, p);
568
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100569 if (attrs[XFRMA_SA_EXTRA_FLAGS])
570 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
571
Herbert Xu69b01372015-05-27 16:03:45 +0800572 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
Herbert Xu1a6509d2008-01-28 19:37:29 -0800573 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000574 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
575 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000577 if (!x->props.aalgo) {
578 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
579 attrs[XFRMA_ALG_AUTH])))
580 goto error;
581 }
Herbert Xu69b01372015-05-27 16:03:45 +0800582 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 goto error;
584 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
585 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700586 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700588
589 if (attrs[XFRMA_ENCAP]) {
590 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
591 sizeof(*x->encap), GFP_KERNEL);
592 if (x->encap == NULL)
593 goto error;
594 }
595
Martin Willi35d28562010-12-08 04:37:49 +0000596 if (attrs[XFRMA_TFCPAD])
597 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
598
Thomas Graffd211502007-09-06 03:28:08 -0700599 if (attrs[XFRMA_COADDR]) {
600 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
601 sizeof(*x->coaddr), GFP_KERNEL);
602 if (x->coaddr == NULL)
603 goto error;
604 }
605
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000606 xfrm_mark_get(attrs, &x->mark);
607
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700608 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (err)
610 goto error;
611
Mathias Krause2f30ea52016-09-08 18:09:57 +0200612 if (attrs[XFRMA_SEC_CTX]) {
613 err = security_xfrm_state_alloc(x,
614 nla_data(attrs[XFRMA_SEC_CTX]));
615 if (err)
616 goto error;
617 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800618
Steffen Klassertd8647b72011-03-08 00:10:27 +0000619 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
620 attrs[XFRMA_REPLAY_ESN_VAL])))
621 goto error;
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800624 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800625 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800626 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800627
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000628 if ((err = xfrm_init_replay(x)))
629 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800630
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000631 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000632 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 return x;
635
636error:
637 x->km.state = XFRM_STATE_DEAD;
638 xfrm_state_put(x);
639error_no_put:
640 *errp = err;
641 return NULL;
642}
643
Christoph Hellwig22e70052007-01-02 15:22:30 -0800644static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700645 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800647 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700648 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct xfrm_state *x;
650 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700651 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Thomas Graf35a7aa02007-08-22 14:00:40 -0700653 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (err)
655 return err;
656
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800657 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!x)
659 return err;
660
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700661 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
663 err = xfrm_state_add(x);
664 else
665 err = xfrm_state_update(x);
666
Tetsuo Handa2e710292014-04-22 21:48:30 +0900667 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (err < 0) {
670 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800671 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700672 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700675 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000676 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700677 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700678
679 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700680out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700681 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return err;
683}
684
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800685static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
686 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700687 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700688 int *errp)
689{
690 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000691 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700692 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000693 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700694
695 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
696 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000697 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700698 } else {
699 xfrm_address_t *saddr = NULL;
700
Thomas Graf35a7aa02007-08-22 14:00:40 -0700701 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700702 if (!saddr) {
703 err = -EINVAL;
704 goto out;
705 }
706
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800707 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000708 x = xfrm_state_lookup_byaddr(net, mark,
709 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800710 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700711 }
712
713 out:
714 if (!x && errp)
715 *errp = err;
716 return x;
717}
718
Christoph Hellwig22e70052007-01-02 15:22:30 -0800719static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700720 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800722 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700724 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700725 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700726 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800728 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700730 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
David S. Miller6f68dc32006-06-08 23:58:52 -0700732 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700733 goto out;
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700736 err = -EPERM;
737 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700740 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600741
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700742 if (err < 0)
743 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700744
745 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000746 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700747 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700748 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700750out:
Tetsuo Handa2e710292014-04-22 21:48:30 +0900751 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700752 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700753 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
757{
Mathias Krausef778a632012-09-19 11:33:39 +0000758 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 memcpy(&p->id, &x->id, sizeof(p->id));
760 memcpy(&p->sel, &x->sel, sizeof(p->sel));
761 memcpy(&p->lft, &x->lft, sizeof(p->lft));
762 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
Sowmini Varadhane33d4f12015-10-21 11:48:25 -0400763 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
764 put_unaligned(x->stats.replay, &p->stats.replay);
765 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
David S. Miller54489c142006-10-27 15:29:47 -0700766 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 p->mode = x->props.mode;
768 p->replay_window = x->props.replay_window;
769 p->reqid = x->props.reqid;
770 p->family = x->props.family;
771 p->flags = x->props.flags;
772 p->seq = x->km.seq;
773}
774
775struct xfrm_dump_info {
776 struct sk_buff *in_skb;
777 struct sk_buff *out_skb;
778 u32 nlmsg_seq;
779 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780};
781
Thomas Grafc0144be2007-08-22 13:55:43 -0700782static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
783{
Thomas Grafc0144be2007-08-22 13:55:43 -0700784 struct xfrm_user_sec_ctx *uctx;
785 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700786 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700787
788 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
789 if (attr == NULL)
790 return -EMSGSIZE;
791
792 uctx = nla_data(attr);
793 uctx->exttype = XFRMA_SEC_CTX;
794 uctx->len = ctx_size;
795 uctx->ctx_doi = s->ctx_doi;
796 uctx->ctx_alg = s->ctx_alg;
797 uctx->ctx_len = s->ctx_len;
798 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
799
800 return 0;
801}
802
Martin Willi4447bb32009-11-25 00:29:52 +0000803static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
804{
805 struct xfrm_algo *algo;
806 struct nlattr *nla;
807
808 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
809 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
810 if (!nla)
811 return -EMSGSIZE;
812
813 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000814 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000815 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
816 algo->alg_key_len = auth->alg_key_len;
817
818 return 0;
819}
820
Herbert Xu68325d32007-10-09 13:30:57 -0700821/* Don't change this without updating xfrm_sa_len! */
822static int copy_to_user_state_extra(struct xfrm_state *x,
823 struct xfrm_usersa_info *p,
824 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700826 int ret = 0;
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 copy_to_user_state(x, p);
829
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100830 if (x->props.extra_flags) {
831 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
832 x->props.extra_flags);
833 if (ret)
834 goto out;
835 }
836
David S. Miller1d1e34d2012-06-27 21:57:03 -0700837 if (x->coaddr) {
838 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
839 if (ret)
840 goto out;
841 }
842 if (x->lastused) {
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +0200843 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
844 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700845 if (ret)
846 goto out;
847 }
848 if (x->aead) {
849 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
850 if (ret)
851 goto out;
852 }
853 if (x->aalg) {
854 ret = copy_to_user_auth(x->aalg, skb);
855 if (!ret)
856 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
857 xfrm_alg_auth_len(x->aalg), x->aalg);
858 if (ret)
859 goto out;
860 }
861 if (x->ealg) {
862 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
863 if (ret)
864 goto out;
865 }
866 if (x->calg) {
867 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
868 if (ret)
869 goto out;
870 }
871 if (x->encap) {
872 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
873 if (ret)
874 goto out;
875 }
876 if (x->tfcpad) {
877 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
878 if (ret)
879 goto out;
880 }
881 ret = xfrm_mark_put(skb, &x->mark);
882 if (ret)
883 goto out;
dingzhif293a5e2014-10-30 09:39:36 +0100884 if (x->replay_esn)
David S. Miller1d1e34d2012-06-27 21:57:03 -0700885 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
886 xfrm_replay_state_esn_len(x->replay_esn),
887 x->replay_esn);
dingzhif293a5e2014-10-30 09:39:36 +0100888 else
889 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
890 &x->replay);
891 if (ret)
892 goto out;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700893 if (x->security)
894 ret = copy_sec_ctx(x->security, skb);
895out:
896 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700897}
898
899static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
900{
901 struct xfrm_dump_info *sp = ptr;
902 struct sk_buff *in_skb = sp->in_skb;
903 struct sk_buff *skb = sp->out_skb;
904 struct xfrm_usersa_info *p;
905 struct nlmsghdr *nlh;
906 int err;
907
Eric W. Biederman15e47302012-09-07 20:12:54 +0000908 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700909 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
910 if (nlh == NULL)
911 return -EMSGSIZE;
912
913 p = nlmsg_data(nlh);
914
915 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700916 if (err) {
917 nlmsg_cancel(skb, nlh);
918 return err;
919 }
Thomas Graf98250692007-08-22 12:47:26 -0700920 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Timo Teras4c563f72008-02-28 21:31:08 -0800924static int xfrm_dump_sa_done(struct netlink_callback *cb)
925{
926 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800927 struct sock *sk = cb->skb->sk;
928 struct net *net = sock_net(sk);
929
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200930 if (cb->args[0])
931 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800932 return 0;
933}
934
Nicolas Dichteld3623092014-02-14 15:30:36 +0100935static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
937{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800938 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800939 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 struct xfrm_dump_info info;
941
Timo Teras4c563f72008-02-28 21:31:08 -0800942 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
943 sizeof(cb->args) - sizeof(cb->args[0]));
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 info.in_skb = cb->skb;
946 info.out_skb = skb;
947 info.nlmsg_seq = cb->nlh->nlmsg_seq;
948 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800949
950 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100951 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100952 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100953 u8 proto = 0;
954 int err;
955
Nicolas Dichteld3623092014-02-14 15:30:36 +0100956 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
957 xfrma_policy);
958 if (err < 0)
959 return err;
960
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100961 if (attrs[XFRMA_ADDRESS_FILTER]) {
Andrzej Hajdadf367562015-08-07 09:59:34 +0200962 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
963 sizeof(*filter), GFP_KERNEL);
Nicolas Dichteld3623092014-02-14 15:30:36 +0100964 if (filter == NULL)
965 return -ENOMEM;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100966 }
967
968 if (attrs[XFRMA_PROTO])
969 proto = nla_get_u8(attrs[XFRMA_PROTO]);
970
971 xfrm_state_walk_init(walk, proto, filter);
Vegard Nossum1ba5bf92016-07-05 10:18:08 +0200972 cb->args[0] = 1;
Timo Teras4c563f72008-02-28 21:31:08 -0800973 }
974
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800975 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 return skb->len;
978}
979
980static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
981 struct xfrm_state *x, u32 seq)
982{
983 struct xfrm_dump_info info;
984 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000985 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Thomas Graf7deb2262007-08-22 13:57:39 -0700987 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (!skb)
989 return ERR_PTR(-ENOMEM);
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 info.in_skb = in_skb;
992 info.out_skb = skb;
993 info.nlmsg_seq = seq;
994 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Mathias Krause864745d2012-09-13 11:41:26 +0000996 err = dump_one_state(x, 0, &info);
997 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000999 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
1002 return skb;
1003}
1004
Michal Kubecek21ee5432014-06-03 10:26:06 +02001005/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1006 * Must be called with RCU read lock.
1007 */
1008static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1009 u32 pid, unsigned int group)
1010{
1011 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1012
Florian Westphal301a6da2018-06-25 14:00:07 +02001013 if (!nlsk) {
1014 kfree_skb(skb);
1015 return -EPIPE;
1016 }
1017
1018 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
Michal Kubecek21ee5432014-06-03 10:26:06 +02001019}
1020
Thomas Graf7deb2262007-08-22 13:57:39 -07001021static inline size_t xfrm_spdinfo_msgsize(void)
1022{
1023 return NLMSG_ALIGN(4)
1024 + nla_total_size(sizeof(struct xfrmu_spdinfo))
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001025 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1026 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1027 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
Thomas Graf7deb2262007-08-22 13:57:39 -07001028}
1029
Alexey Dobriyane0710412010-01-23 13:37:10 +00001030static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001031 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001032{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001033 struct xfrmk_spdinfo si;
1034 struct xfrmu_spdinfo spc;
1035 struct xfrmu_spdhinfo sph;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001036 struct xfrmu_spdhthresh spt4, spt6;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001037 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001038 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001039 u32 *f;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001040 unsigned lseq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001041
Eric W. Biederman15e47302012-09-07 20:12:54 +00001042 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001043 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001044 return -EMSGSIZE;
1045
1046 f = nlmsg_data(nlh);
1047 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001048 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001049 spc.incnt = si.incnt;
1050 spc.outcnt = si.outcnt;
1051 spc.fwdcnt = si.fwdcnt;
1052 spc.inscnt = si.inscnt;
1053 spc.outscnt = si.outscnt;
1054 spc.fwdscnt = si.fwdscnt;
1055 sph.spdhcnt = si.spdhcnt;
1056 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001057
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001058 do {
1059 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1060
1061 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1062 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1063 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1064 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1065 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1066
David S. Miller1d1e34d2012-06-27 21:57:03 -07001067 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1068 if (!err)
1069 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001070 if (!err)
1071 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1072 if (!err)
1073 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001074 if (err) {
1075 nlmsg_cancel(skb, nlh);
1076 return err;
1077 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001078
Johannes Berg053c0952015-01-16 22:09:00 +01001079 nlmsg_end(skb, nlh);
1080 return 0;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001081}
1082
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001083static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1084 struct nlattr **attrs)
1085{
1086 struct net *net = sock_net(skb->sk);
1087 struct xfrmu_spdhthresh *thresh4 = NULL;
1088 struct xfrmu_spdhthresh *thresh6 = NULL;
1089
1090 /* selector prefixlen thresholds to hash policies */
1091 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1092 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1093
1094 if (nla_len(rta) < sizeof(*thresh4))
1095 return -EINVAL;
1096 thresh4 = nla_data(rta);
1097 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1098 return -EINVAL;
1099 }
1100 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1101 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1102
1103 if (nla_len(rta) < sizeof(*thresh6))
1104 return -EINVAL;
1105 thresh6 = nla_data(rta);
1106 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1107 return -EINVAL;
1108 }
1109
1110 if (thresh4 || thresh6) {
1111 write_seqlock(&net->xfrm.policy_hthresh.lock);
1112 if (thresh4) {
1113 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1114 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1115 }
1116 if (thresh6) {
1117 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1118 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1119 }
1120 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1121
1122 xfrm_policy_hash_rebuild(net);
1123 }
1124
1125 return 0;
1126}
1127
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001128static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001129 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001130{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001131 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001132 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001133 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001134 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001135 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001136
Thomas Graf7deb2262007-08-22 13:57:39 -07001137 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001138 if (r_skb == NULL)
1139 return -ENOMEM;
1140
Eric W. Biederman15e47302012-09-07 20:12:54 +00001141 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001142 BUG();
1143
Eric W. Biederman15e47302012-09-07 20:12:54 +00001144 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001145}
1146
Thomas Graf7deb2262007-08-22 13:57:39 -07001147static inline size_t xfrm_sadinfo_msgsize(void)
1148{
1149 return NLMSG_ALIGN(4)
1150 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1151 + nla_total_size(4); /* XFRMA_SAD_CNT */
1152}
1153
Alexey Dobriyane0710412010-01-23 13:37:10 +00001154static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001155 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001156{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001157 struct xfrmk_sadinfo si;
1158 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001159 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001160 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001161 u32 *f;
1162
Eric W. Biederman15e47302012-09-07 20:12:54 +00001163 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001164 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001165 return -EMSGSIZE;
1166
1167 f = nlmsg_data(nlh);
1168 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001169 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001170
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001171 sh.sadhmcnt = si.sadhmcnt;
1172 sh.sadhcnt = si.sadhcnt;
1173
David S. Miller1d1e34d2012-06-27 21:57:03 -07001174 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1175 if (!err)
1176 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1177 if (err) {
1178 nlmsg_cancel(skb, nlh);
1179 return err;
1180 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001181
Johannes Berg053c0952015-01-16 22:09:00 +01001182 nlmsg_end(skb, nlh);
1183 return 0;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001184}
1185
1186static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001187 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001188{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001189 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001190 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001191 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001192 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001193 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001194
Thomas Graf7deb2262007-08-22 13:57:39 -07001195 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001196 if (r_skb == NULL)
1197 return -ENOMEM;
1198
Eric W. Biederman15e47302012-09-07 20:12:54 +00001199 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001200 BUG();
1201
Eric W. Biederman15e47302012-09-07 20:12:54 +00001202 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001203}
1204
Christoph Hellwig22e70052007-01-02 15:22:30 -08001205static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001206 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001208 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001209 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 struct xfrm_state *x;
1211 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001212 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001214 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (x == NULL)
1216 goto out_noput;
1217
1218 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1219 if (IS_ERR(resp_skb)) {
1220 err = PTR_ERR(resp_skb);
1221 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001222 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 xfrm_state_put(x);
1225out_noput:
1226 return err;
1227}
1228
Christoph Hellwig22e70052007-01-02 15:22:30 -08001229static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001230 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001232 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 struct xfrm_state *x;
1234 struct xfrm_userspi_info *p;
1235 struct sk_buff *resp_skb;
1236 xfrm_address_t *daddr;
1237 int family;
1238 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001239 u32 mark;
1240 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Thomas Graf7b67c852007-08-22 13:53:52 -07001242 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001243 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (err)
1245 goto out_noput;
1246
1247 family = p->info.family;
1248 daddr = &p->info.id.daddr;
1249
1250 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001251
1252 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001254 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001255 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 xfrm_state_put(x);
1257 x = NULL;
1258 }
1259 }
1260
1261 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001262 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 p->info.id.proto, daddr,
1264 &p->info.saddr, 1,
1265 family);
1266 err = -ENOENT;
1267 if (x == NULL)
1268 goto out_noput;
1269
Herbert Xu658b2192007-10-09 13:29:52 -07001270 err = xfrm_alloc_spi(x, p->min, p->max);
1271 if (err)
1272 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Herbert Xu658b2192007-10-09 13:29:52 -07001274 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (IS_ERR(resp_skb)) {
1276 err = PTR_ERR(resp_skb);
1277 goto out;
1278 }
1279
Eric W. Biederman15e47302012-09-07 20:12:54 +00001280 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282out:
1283 xfrm_state_put(x);
1284out_noput:
1285 return err;
1286}
1287
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001288static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 switch (dir) {
1291 case XFRM_POLICY_IN:
1292 case XFRM_POLICY_OUT:
1293 case XFRM_POLICY_FWD:
1294 break;
1295
1296 default:
1297 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 return 0;
1301}
1302
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001303static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001304{
1305 switch (type) {
1306 case XFRM_POLICY_TYPE_MAIN:
1307#ifdef CONFIG_XFRM_SUB_POLICY
1308 case XFRM_POLICY_TYPE_SUB:
1309#endif
1310 break;
1311
1312 default:
1313 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001314 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001315
1316 return 0;
1317}
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1320{
Fan Due682adf2013-11-07 17:47:48 +08001321 int ret;
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 switch (p->share) {
1324 case XFRM_SHARE_ANY:
1325 case XFRM_SHARE_SESSION:
1326 case XFRM_SHARE_USER:
1327 case XFRM_SHARE_UNIQUE:
1328 break;
1329
1330 default:
1331 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 switch (p->action) {
1335 case XFRM_POLICY_ALLOW:
1336 case XFRM_POLICY_BLOCK:
1337 break;
1338
1339 default:
1340 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 switch (p->sel.family) {
1344 case AF_INET:
Steffen Klassert89cb86e2018-08-01 13:45:11 +02001345 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1346 return -EINVAL;
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 break;
1349
1350 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001351#if IS_ENABLED(CONFIG_IPV6)
Steffen Klassert89cb86e2018-08-01 13:45:11 +02001352 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1353 return -EINVAL;
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 break;
1356#else
1357 return -EAFNOSUPPORT;
1358#endif
1359
1360 default:
1361 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Fan Due682adf2013-11-07 17:47:48 +08001364 ret = verify_policy_dir(p->dir);
1365 if (ret)
1366 return ret;
YueHaibing7c967212019-02-28 15:18:59 +08001367 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
Fan Due682adf2013-11-07 17:47:48 +08001368 return -EINVAL;
1369
1370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Thomas Graf5424f322007-08-22 14:01:33 -07001373static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001374{
Thomas Graf5424f322007-08-22 14:01:33 -07001375 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001376 struct xfrm_user_sec_ctx *uctx;
1377
1378 if (!rt)
1379 return 0;
1380
Thomas Graf5424f322007-08-22 14:01:33 -07001381 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001382 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001383}
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1386 int nr)
1387{
1388 int i;
1389
1390 xp->xfrm_nr = nr;
1391 for (i = 0; i < nr; i++, ut++) {
1392 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1393
1394 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1395 memcpy(&t->saddr, &ut->saddr,
1396 sizeof(xfrm_address_t));
1397 t->reqid = ut->reqid;
1398 t->mode = ut->mode;
1399 t->share = ut->share;
1400 t->optional = ut->optional;
1401 t->aalgos = ut->aalgos;
1402 t->ealgos = ut->ealgos;
1403 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001404 /* If all masks are ~0, then we allow all algorithms. */
1405 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001406 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408}
1409
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001410static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1411{
Steffen Klassert9a54c512017-12-08 08:07:25 +01001412 u16 prev_family;
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001413 int i;
1414
1415 if (nr > XFRM_MAX_DEPTH)
1416 return -EINVAL;
1417
Steffen Klassert9a54c512017-12-08 08:07:25 +01001418 prev_family = family;
1419
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001420 for (i = 0; i < nr; i++) {
1421 /* We never validated the ut->family value, so many
1422 * applications simply leave it at zero. The check was
1423 * never made and ut->family was ignored because all
1424 * templates could be assumed to have the same family as
1425 * the policy itself. Now that we will have ipv4-in-ipv6
1426 * and ipv6-in-ipv4 tunnels, this is no longer true.
1427 */
1428 if (!ut[i].family)
1429 ut[i].family = family;
1430
Florian Westphala19fd852019-01-09 14:37:34 +01001431 switch (ut[i].mode) {
1432 case XFRM_MODE_TUNNEL:
1433 case XFRM_MODE_BEET:
1434 break;
1435 default:
1436 if (ut[i].family != prev_family)
1437 return -EINVAL;
1438 break;
1439 }
Sean Tranchettide500b92018-09-19 13:54:56 -06001440 if (ut[i].mode >= XFRM_MODE_MAX)
1441 return -EINVAL;
1442
Steffen Klassert9a54c512017-12-08 08:07:25 +01001443 prev_family = ut[i].family;
1444
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001445 switch (ut[i].family) {
1446 case AF_INET:
1447 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001448#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001449 case AF_INET6:
1450 break;
1451#endif
1452 default:
1453 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001454 }
Cong Wang85552882017-11-27 11:15:16 -08001455
Cong Wang88d08312019-03-22 16:26:19 -07001456 if (!xfrm_id_proto_valid(ut[i].id.proto))
Cong Wang85552882017-11-27 11:15:16 -08001457 return -EINVAL;
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001458 }
1459
1460 return 0;
1461}
1462
Thomas Graf5424f322007-08-22 14:01:33 -07001463static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Thomas Graf5424f322007-08-22 14:01:33 -07001465 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 if (!rt) {
1468 pol->xfrm_nr = 0;
1469 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001470 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1471 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001472 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001474 err = validate_tmpl(nr, utmpl, pol->family);
1475 if (err)
1476 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Thomas Graf5424f322007-08-22 14:01:33 -07001478 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480 return 0;
1481}
1482
Thomas Graf5424f322007-08-22 14:01:33 -07001483static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001484{
Thomas Graf5424f322007-08-22 14:01:33 -07001485 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001486 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001487 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001488 int err;
1489
1490 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001491 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001492 type = upt->type;
1493 }
1494
1495 err = verify_policy_type(type);
1496 if (err)
1497 return err;
1498
1499 *tp = type;
1500 return 0;
1501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1504{
1505 xp->priority = p->priority;
1506 xp->index = p->index;
1507 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1508 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1509 xp->action = p->action;
1510 xp->flags = p->flags;
1511 xp->family = p->sel.family;
1512 /* XXX xp->share = p->share; */
1513}
1514
1515static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1516{
Mathias Krause7b789832012-09-19 11:33:40 +00001517 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1519 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1520 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1521 p->priority = xp->priority;
1522 p->index = xp->index;
1523 p->sel.family = xp->family;
1524 p->dir = dir;
1525 p->action = xp->action;
1526 p->flags = xp->flags;
1527 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1528}
1529
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001530static 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 -07001531{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001532 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 int err;
1534
1535 if (!xp) {
1536 *errp = -ENOMEM;
1537 return NULL;
1538 }
1539
1540 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001541
Thomas Graf35a7aa02007-08-22 14:00:40 -07001542 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001543 if (err)
1544 goto error;
1545
Thomas Graf35a7aa02007-08-22 14:00:40 -07001546 if (!(err = copy_from_user_tmpl(xp, attrs)))
1547 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001548 if (err)
1549 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001551 xfrm_mark_get(attrs, &xp->mark);
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001554 error:
1555 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001556 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001557 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001558 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Christoph Hellwig22e70052007-01-02 15:22:30 -08001561static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001562 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001564 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001565 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001567 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 int err;
1569 int excl;
1570
1571 err = verify_newpolicy_info(p);
1572 if (err)
1573 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001574 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001575 if (err)
1576 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001578 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (!xp)
1580 return err;
1581
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001582 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001583 * Aha! this is anti-netlink really i.e more pfkey derived
1584 * in netlink excl is a flag and you wouldnt need
1585 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1587 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001588 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001591 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 kfree(xp);
1593 return err;
1594 }
1595
Herbert Xuf60f6b82005-06-18 22:44:37 -07001596 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001597 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001598 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001599 km_policy_notify(xp, p->dir, &c);
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 xfrm_pol_put(xp);
1602
1603 return 0;
1604}
1605
1606static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1607{
1608 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1609 int i;
1610
1611 if (xp->xfrm_nr == 0)
1612 return 0;
1613
1614 for (i = 0; i < xp->xfrm_nr; i++) {
1615 struct xfrm_user_tmpl *up = &vec[i];
1616 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1617
Mathias Krause1f868402012-09-19 11:33:41 +00001618 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001620 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1622 up->reqid = kp->reqid;
1623 up->mode = kp->mode;
1624 up->share = kp->share;
1625 up->optional = kp->optional;
1626 up->aalgos = kp->aalgos;
1627 up->ealgos = kp->ealgos;
1628 up->calgos = kp->calgos;
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Thomas Grafc0144be2007-08-22 13:55:43 -07001631 return nla_put(skb, XFRMA_TMPL,
1632 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001633}
1634
Serge Hallyn0d681622006-07-24 23:30:44 -07001635static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1636{
1637 if (x->security) {
1638 return copy_sec_ctx(x->security, skb);
1639 }
1640 return 0;
1641}
1642
1643static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1644{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001645 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001646 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001647 return 0;
1648}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001649static inline size_t userpolicy_type_attrsize(void)
1650{
1651#ifdef CONFIG_XFRM_SUB_POLICY
1652 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1653#else
1654 return 0;
1655#endif
1656}
Serge Hallyn0d681622006-07-24 23:30:44 -07001657
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001658#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001659static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001660{
Eric Dumazet2038a9e2018-06-18 21:35:07 -07001661 struct xfrm_userpolicy_type upt;
1662
1663 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1664 memset(&upt, 0, sizeof(upt));
1665 upt.type = type;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001666
Thomas Grafc0144be2007-08-22 13:55:43 -07001667 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001668}
1669
1670#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001671static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001672{
1673 return 0;
1674}
1675#endif
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1678{
1679 struct xfrm_dump_info *sp = ptr;
1680 struct xfrm_userpolicy_info *p;
1681 struct sk_buff *in_skb = sp->in_skb;
1682 struct sk_buff *skb = sp->out_skb;
1683 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001684 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Eric W. Biederman15e47302012-09-07 20:12:54 +00001686 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001687 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1688 if (nlh == NULL)
1689 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Thomas Graf7b67c852007-08-22 13:53:52 -07001691 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001693 err = copy_to_user_tmpl(xp, skb);
1694 if (!err)
1695 err = copy_to_user_sec_ctx(xp, skb);
1696 if (!err)
1697 err = copy_to_user_policy_type(xp->type, skb);
1698 if (!err)
1699 err = xfrm_mark_put(skb, &xp->mark);
1700 if (err) {
1701 nlmsg_cancel(skb, nlh);
1702 return err;
1703 }
Thomas Graf98250692007-08-22 12:47:26 -07001704 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Timo Teras4c563f72008-02-28 21:31:08 -08001708static int xfrm_dump_policy_done(struct netlink_callback *cb)
1709{
Herbert Xu543aabb2017-10-19 20:51:10 +08001710 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Fan Du283bc9f2013-11-07 17:47:50 +08001711 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001712
Fan Du283bc9f2013-11-07 17:47:50 +08001713 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001714 return 0;
1715}
1716
Herbert Xu543aabb2017-10-19 20:51:10 +08001717static int xfrm_dump_policy_start(struct netlink_callback *cb)
1718{
1719 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1720
1721 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1722
1723 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1724 return 0;
1725}
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1728{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001729 struct net *net = sock_net(skb->sk);
Herbert Xu543aabb2017-10-19 20:51:10 +08001730 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct xfrm_dump_info info;
1732
1733 info.in_skb = cb->skb;
1734 info.out_skb = skb;
1735 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1736 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001737
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001738 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 return skb->len;
1741}
1742
1743static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1744 struct xfrm_policy *xp,
1745 int dir, u32 seq)
1746{
1747 struct xfrm_dump_info info;
1748 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001749 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Thomas Graf7deb2262007-08-22 13:57:39 -07001751 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (!skb)
1753 return ERR_PTR(-ENOMEM);
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 info.in_skb = in_skb;
1756 info.out_skb = skb;
1757 info.nlmsg_seq = seq;
1758 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Mathias Krausec2546372012-09-14 09:58:32 +00001760 err = dump_one_policy(xp, dir, 0, &info);
1761 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001763 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765
1766 return skb;
1767}
1768
Christoph Hellwig22e70052007-01-02 15:22:30 -08001769static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001770 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001772 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 struct xfrm_policy *xp;
1774 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001775 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001777 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001779 struct xfrm_mark m;
1780 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Thomas Graf7b67c852007-08-22 13:53:52 -07001782 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1784
Thomas Graf35a7aa02007-08-22 14:00:40 -07001785 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001786 if (err)
1787 return err;
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 err = verify_policy_dir(p->dir);
1790 if (err)
1791 return err;
1792
1793 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001794 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001795 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001796 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001797 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001798
Thomas Graf35a7aa02007-08-22 14:00:40 -07001799 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001800 if (err)
1801 return err;
1802
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001803 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001804 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001805 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001806
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001807 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001808 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001809 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001810 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001811 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001812 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001813 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (xp == NULL)
1816 return -ENOENT;
1817
1818 if (!delete) {
1819 struct sk_buff *resp_skb;
1820
1821 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1822 if (IS_ERR(resp_skb)) {
1823 err = PTR_ERR(resp_skb);
1824 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001825 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001826 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001828 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09001829 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001830
1831 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001832 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001833
Herbert Xue7443892005-06-18 22:44:18 -07001834 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001835 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001836 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001837 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001838 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
1840
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001841out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001842 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001843 if (delete && err == 0)
1844 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return err;
1846}
1847
Christoph Hellwig22e70052007-01-02 15:22:30 -08001848static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001849 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001851 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001852 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001853 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04001854 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Tetsuo Handa2e710292014-04-22 21:48:30 +09001856 err = xfrm_state_flush(net, p->proto, true);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001857 if (err) {
1858 if (err == -ESRCH) /* empty table */
1859 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001860 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001861 }
Herbert Xubf088672005-06-18 22:44:00 -07001862 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001863 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001864 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001865 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001866 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001867 km_state_notify(NULL, &c);
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return 0;
1870}
1871
Steffen Klassertd8647b72011-03-08 00:10:27 +00001872static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001873{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001874 size_t replay_size = x->replay_esn ?
1875 xfrm_replay_state_esn_len(x->replay_esn) :
1876 sizeof(struct xfrm_replay_state);
1877
Thomas Graf7deb2262007-08-22 13:57:39 -07001878 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001879 + nla_total_size(replay_size)
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001880 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001881 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001882 + nla_total_size(4) /* XFRM_AE_RTHR */
1883 + nla_total_size(4); /* XFRM_AE_ETHR */
1884}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001885
David S. Miller214e0052011-02-24 00:02:38 -05001886static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001887{
1888 struct xfrm_aevent_id *id;
1889 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001890 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001891
Eric W. Biederman15e47302012-09-07 20:12:54 +00001892 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001893 if (nlh == NULL)
1894 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001895
Thomas Graf7b67c852007-08-22 13:53:52 -07001896 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001897 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001898 id->sa_id.spi = x->id.spi;
1899 id->sa_id.family = x->props.family;
1900 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001901 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001902 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001903 id->flags = c->data.aevent;
1904
David S. Millerd0fde792012-03-29 04:02:26 -04001905 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001906 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1907 xfrm_replay_state_esn_len(x->replay_esn),
1908 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001909 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001910 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1911 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001912 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001913 if (err)
1914 goto out_cancel;
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02001915 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1916 XFRMA_PAD);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001917 if (err)
1918 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001919
David S. Miller1d1e34d2012-06-27 21:57:03 -07001920 if (id->flags & XFRM_AE_RTHR) {
1921 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1922 if (err)
1923 goto out_cancel;
1924 }
1925 if (id->flags & XFRM_AE_ETHR) {
1926 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1927 x->replay_maxage * 10 / HZ);
1928 if (err)
1929 goto out_cancel;
1930 }
1931 err = xfrm_mark_put(skb, &x->mark);
1932 if (err)
1933 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001934
Johannes Berg053c0952015-01-16 22:09:00 +01001935 nlmsg_end(skb, nlh);
1936 return 0;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001937
David S. Miller1d1e34d2012-06-27 21:57:03 -07001938out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001939 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001940 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001941}
1942
Christoph Hellwig22e70052007-01-02 15:22:30 -08001943static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001944 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001945{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001946 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001947 struct xfrm_state *x;
1948 struct sk_buff *r_skb;
1949 int err;
1950 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001951 u32 mark;
1952 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001953 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001954 struct xfrm_usersa_id *id = &p->sa_id;
1955
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001956 mark = xfrm_mark_get(attrs, &m);
1957
1958 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001959 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001960 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001961
1962 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1963 if (r_skb == NULL) {
1964 xfrm_state_put(x);
1965 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001966 }
1967
1968 /*
1969 * XXX: is this lock really needed - none of the other
1970 * gets lock (the concern is things getting updated
1971 * while we are still reading) - jhs
1972 */
1973 spin_lock_bh(&x->lock);
1974 c.data.aevent = p->flags;
1975 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001976 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001977
1978 if (build_aevent(r_skb, x, &c) < 0)
1979 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001980 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001981 spin_unlock_bh(&x->lock);
1982 xfrm_state_put(x);
1983 return err;
1984}
1985
Christoph Hellwig22e70052007-01-02 15:22:30 -08001986static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001987 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001988{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001989 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001990 struct xfrm_state *x;
1991 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001992 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001993 u32 mark = 0;
1994 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001995 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001996 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001997 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001998 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Michael Rossberg4e077232015-09-29 11:25:08 +02001999 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2000 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002001
Michael Rossberg4e077232015-09-29 11:25:08 +02002002 if (!lt && !rp && !re && !et && !rt)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002003 return err;
2004
2005 /* pedantic mode - thou shalt sayeth replaceth */
2006 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2007 return err;
2008
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002009 mark = xfrm_mark_get(attrs, &m);
2010
2011 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 -08002012 if (x == NULL)
2013 return -ESRCH;
2014
2015 if (x->km.state != XFRM_STATE_VALID)
2016 goto out;
2017
Steffen Klassert4479ff72013-09-09 09:39:01 +02002018 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00002019 if (err)
2020 goto out;
2021
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002022 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00002023 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002024 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002025
2026 c.event = nlh->nlmsg_type;
2027 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002028 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002029 c.data.aevent = XFRM_AE_CU;
2030 km_state_notify(x, &c);
2031 err = 0;
2032out:
2033 xfrm_state_put(x);
2034 return err;
2035}
2036
Christoph Hellwig22e70052007-01-02 15:22:30 -08002037static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002038 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002040 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002041 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002042 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002043 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002044
Thomas Graf35a7aa02007-08-22 14:00:40 -07002045 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002046 if (err)
2047 return err;
2048
Tetsuo Handa2e710292014-04-22 21:48:30 +09002049 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002050 if (err) {
2051 if (err == -ESRCH) /* empty table */
2052 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08002053 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00002054 }
2055
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002056 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002057 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002058 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002059 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08002060 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002061 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return 0;
2063}
2064
Christoph Hellwig22e70052007-01-02 15:22:30 -08002065static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002066 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002067{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002068 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002069 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07002070 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002071 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002072 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002073 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002074 struct xfrm_mark m;
2075 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002076
Thomas Graf35a7aa02007-08-22 14:00:40 -07002077 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002078 if (err)
2079 return err;
2080
Timo Teräsc8bf4d02010-03-31 00:17:04 +00002081 err = verify_policy_dir(p->dir);
2082 if (err)
2083 return err;
2084
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002085 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002086 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002087 else {
Thomas Graf5424f322007-08-22 14:01:33 -07002088 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07002089 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002090
Thomas Graf35a7aa02007-08-22 14:00:40 -07002091 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002092 if (err)
2093 return err;
2094
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002095 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002096 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07002097 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002098
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002099 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07002100 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002101 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002102 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002103 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002104 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07002105 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002106 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002107 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08002108 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07002109
Timo Teräsea2dea92010-03-31 00:17:05 +00002110 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002111 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002112
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002113 err = 0;
2114 if (up->hard) {
2115 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002116 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002117 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002118 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002119
2120out:
2121 xfrm_pol_put(xp);
2122 return err;
2123}
2124
Christoph Hellwig22e70052007-01-02 15:22:30 -08002125static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002126 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002127{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002128 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002129 struct xfrm_state *x;
2130 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002131 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002132 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002133 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002134 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002135
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002136 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 -08002137
David S. Miller3a765aa2007-02-26 14:52:21 -08002138 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002139 if (x == NULL)
2140 return err;
2141
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002142 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002143 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002144 if (x->km.state != XFRM_STATE_VALID)
2145 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002146 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002147
Joy Latten161a09e2006-11-27 13:11:54 -06002148 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002149 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002150 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002151 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002152 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002153out:
2154 spin_unlock_bh(&x->lock);
2155 xfrm_state_put(x);
2156 return err;
2157}
2158
Christoph Hellwig22e70052007-01-02 15:22:30 -08002159static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002160 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002161{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002162 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002163 struct xfrm_policy *xp;
2164 struct xfrm_user_tmpl *ut;
2165 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002166 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002167 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002168
Thomas Graf7b67c852007-08-22 13:53:52 -07002169 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002170 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002171 int err = -ENOMEM;
2172
2173 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002174 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002175
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002176 xfrm_mark_get(attrs, &mark);
2177
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002178 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002179 if (err)
Vegard Nossum73efc322016-07-27 08:03:18 +02002180 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002181
2182 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002183 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002184 if (!xp)
2185 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002186
2187 memcpy(&x->id, &ua->id, sizeof(ua->id));
2188 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2189 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002190 xp->mark.m = x->mark.m = mark.m;
2191 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002192 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002193 /* extract the templates and for each call km_key */
2194 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2195 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2196 memcpy(&x->id, &t->id, sizeof(x->id));
2197 x->props.mode = t->mode;
2198 x->props.reqid = t->reqid;
2199 x->props.family = ut->family;
2200 t->aalgos = ua->aalgos;
2201 t->ealgos = ua->ealgos;
2202 t->calgos = ua->calgos;
2203 err = km_query(x, t, xp);
2204
2205 }
2206
2207 kfree(x);
2208 kfree(xp);
2209
2210 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002211
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002212free_state:
2213 kfree(x);
2214nomem:
2215 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002216}
2217
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002218#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002219static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002220 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002221 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002222{
Thomas Graf5424f322007-08-22 14:01:33 -07002223 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002224 struct xfrm_user_migrate *um;
2225 int i, num_migrate;
2226
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002227 if (k != NULL) {
2228 struct xfrm_user_kmaddress *uk;
2229
2230 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2231 memcpy(&k->local, &uk->local, sizeof(k->local));
2232 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2233 k->family = uk->family;
2234 k->reserved = uk->reserved;
2235 }
2236
Thomas Graf5424f322007-08-22 14:01:33 -07002237 um = nla_data(rt);
2238 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002239
2240 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2241 return -EINVAL;
2242
2243 for (i = 0; i < num_migrate; i++, um++, ma++) {
2244 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2245 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2246 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2247 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2248
2249 ma->proto = um->proto;
2250 ma->mode = um->mode;
2251 ma->reqid = um->reqid;
2252
2253 ma->old_family = um->old_family;
2254 ma->new_family = um->new_family;
2255 }
2256
2257 *num = i;
2258 return 0;
2259}
2260
2261static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002262 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002263{
Thomas Graf7b67c852007-08-22 13:53:52 -07002264 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002265 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002266 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002267 u8 type;
2268 int err;
2269 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002270 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002271
Thomas Graf35a7aa02007-08-22 14:00:40 -07002272 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002273 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002274
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002275 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2276
Thomas Graf5424f322007-08-22 14:01:33 -07002277 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002278 if (err)
2279 return err;
2280
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002281 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002282 if (err)
2283 return err;
2284
2285 if (!n)
2286 return 0;
2287
Fan Du8d549c42013-11-07 17:47:49 +08002288 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002289
2290 return 0;
2291}
2292#else
2293static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002294 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002295{
2296 return -ENOPROTOOPT;
2297}
2298#endif
2299
2300#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002301static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002302{
2303 struct xfrm_user_migrate um;
2304
2305 memset(&um, 0, sizeof(um));
2306 um.proto = m->proto;
2307 um.mode = m->mode;
2308 um.reqid = m->reqid;
2309 um.old_family = m->old_family;
2310 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2311 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2312 um.new_family = m->new_family;
2313 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2314 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2315
Thomas Grafc0144be2007-08-22 13:55:43 -07002316 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002317}
2318
David S. Miller183cad12011-02-24 00:28:01 -05002319static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002320{
2321 struct xfrm_user_kmaddress uk;
2322
2323 memset(&uk, 0, sizeof(uk));
2324 uk.family = k->family;
2325 uk.reserved = k->reserved;
2326 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002327 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002328
2329 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2330}
2331
2332static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002333{
2334 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002335 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2336 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2337 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002338}
2339
David S. Miller183cad12011-02-24 00:28:01 -05002340static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2341 int num_migrate, const struct xfrm_kmaddress *k,
2342 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002343{
David S. Miller183cad12011-02-24 00:28:01 -05002344 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002345 struct xfrm_userpolicy_id *pol_id;
2346 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002347 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002348
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002349 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2350 if (nlh == NULL)
2351 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002352
Thomas Graf7b67c852007-08-22 13:53:52 -07002353 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002354 /* copy data from selector, dir, and type to the pol_id */
2355 memset(pol_id, 0, sizeof(*pol_id));
2356 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2357 pol_id->dir = dir;
2358
David S. Miller1d1e34d2012-06-27 21:57:03 -07002359 if (k != NULL) {
2360 err = copy_to_user_kmaddress(k, skb);
2361 if (err)
2362 goto out_cancel;
2363 }
2364 err = copy_to_user_policy_type(type, skb);
2365 if (err)
2366 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002367 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002368 err = copy_to_user_migrate(mp, skb);
2369 if (err)
2370 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002371 }
2372
Johannes Berg053c0952015-01-16 22:09:00 +01002373 nlmsg_end(skb, nlh);
2374 return 0;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002375
2376out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002377 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002378 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002379}
2380
David S. Miller183cad12011-02-24 00:28:01 -05002381static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2382 const struct xfrm_migrate *m, int num_migrate,
2383 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002384{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002385 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002386 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002387
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002388 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002389 if (skb == NULL)
2390 return -ENOMEM;
2391
2392 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002393 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002394 BUG();
2395
Michal Kubecek21ee5432014-06-03 10:26:06 +02002396 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002397}
2398#else
David S. Miller183cad12011-02-24 00:28:01 -05002399static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2400 const struct xfrm_migrate *m, int num_migrate,
2401 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002402{
2403 return -ENOPROTOOPT;
2404}
2405#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002406
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002407#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002408
2409static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002410 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002411 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2412 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2413 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2414 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2415 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2416 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002417 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002418 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002419 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002420 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002421 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002422 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002423 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002424 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2425 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002426 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002427 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002428 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002429 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002430 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431};
2432
Thomas Graf492b5582005-05-03 14:26:40 -07002433#undef XMSGSIZE
2434
Thomas Grafcf5cb792007-08-22 13:59:04 -07002435static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002436 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2437 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2438 [XFRMA_LASTUSED] = { .type = NLA_U64},
2439 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002440 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002441 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2442 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2443 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2444 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2445 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2446 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2447 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2448 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2449 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2450 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2451 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2452 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2453 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2454 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002455 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002456 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002457 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002458 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002459 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002460 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002461 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002462};
2463
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002464static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2465 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2466 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2467};
2468
Mathias Krause05600a72013-02-24 14:10:27 +01002469static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002470 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Herbert Xu543aabb2017-10-19 20:51:10 +08002471 int (*start)(struct netlink_callback *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002473 int (*done)(struct netlink_callback *);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002474 const struct nla_policy *nla_pol;
2475 int nla_max;
Thomas Graf492b5582005-05-03 14:26:40 -07002476} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2477 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2478 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2479 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002480 .dump = xfrm_dump_sa,
2481 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002482 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2483 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2484 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Herbert Xu543aabb2017-10-19 20:51:10 +08002485 .start = xfrm_dump_policy_start,
Timo Teras4c563f72008-02-28 21:31:08 -08002486 .dump = xfrm_dump_policy,
2487 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002488 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002489 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002490 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002491 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2492 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002493 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002494 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2495 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002496 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2497 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002498 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002499 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002500 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2501 .nla_pol = xfrma_spd_policy,
2502 .nla_max = XFRMA_SPD_MAX },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002503 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504};
2505
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002506static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002508 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002509 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002510 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002511 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Fan Du74005992015-01-27 17:00:29 +08002513#ifdef CONFIG_COMPAT
Andy Lutomirski2bf8c472016-03-22 14:25:10 -07002514 if (in_compat_syscall())
Yi Zhao83e2d052016-11-29 18:09:01 +08002515 return -EOPNOTSUPP;
Fan Du74005992015-01-27 17:00:29 +08002516#endif
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002520 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522 type -= XFRM_MSG_BASE;
2523 link = &xfrm_dispatch[type];
2524
2525 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002526 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002527 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Thomas Graf492b5582005-05-03 14:26:40 -07002529 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2530 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002531 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002533 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002535 {
2536 struct netlink_dump_control c = {
Herbert Xu543aabb2017-10-19 20:51:10 +08002537 .start = link->start,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002538 .dump = link->dump,
2539 .done = link->done,
2540 };
2541 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002545 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2546 link->nla_max ? : XFRMA_MAX,
2547 link->nla_pol ? : xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002548 if (err < 0)
2549 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002552 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Thomas Graf5424f322007-08-22 14:01:33 -07002554 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002557static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
Fan Du283bc9f2013-11-07 17:47:50 +08002559 struct net *net = sock_net(skb->sk);
2560
2561 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002562 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002563 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
Thomas Graf7deb2262007-08-22 13:57:39 -07002566static inline size_t xfrm_expire_msgsize(void)
2567{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002568 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2569 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002570}
2571
David S. Miller214e0052011-02-24 00:02:38 -05002572static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
2574 struct xfrm_user_expire *ue;
2575 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002576 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Eric W. Biederman15e47302012-09-07 20:12:54 +00002578 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002579 if (nlh == NULL)
2580 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Thomas Graf7b67c852007-08-22 13:53:52 -07002582 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002584 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
David S. Miller1d1e34d2012-06-27 21:57:03 -07002586 err = xfrm_mark_put(skb, &x->mark);
2587 if (err)
2588 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002589
Johannes Berg053c0952015-01-16 22:09:00 +01002590 nlmsg_end(skb, nlh);
2591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
David S. Miller214e0052011-02-24 00:02:38 -05002594static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002596 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 struct sk_buff *skb;
2598
Thomas Graf7deb2262007-08-22 13:57:39 -07002599 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 if (skb == NULL)
2601 return -ENOMEM;
2602
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002603 if (build_expire(skb, x, c) < 0) {
2604 kfree_skb(skb);
2605 return -EMSGSIZE;
2606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Michal Kubecek21ee5432014-06-03 10:26:06 +02002608 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}
2610
David S. Miller214e0052011-02-24 00:02:38 -05002611static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002612{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002613 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002614 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002615
Steffen Klassertd8647b72011-03-08 00:10:27 +00002616 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002617 if (skb == NULL)
2618 return -ENOMEM;
2619
2620 if (build_aevent(skb, x, c) < 0)
2621 BUG();
2622
Michal Kubecek21ee5432014-06-03 10:26:06 +02002623 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002624}
2625
David S. Miller214e0052011-02-24 00:02:38 -05002626static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002627{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002628 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002629 struct xfrm_usersa_flush *p;
2630 struct nlmsghdr *nlh;
2631 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002632 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002633
Thomas Graf7deb2262007-08-22 13:57:39 -07002634 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002635 if (skb == NULL)
2636 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002637
Eric W. Biederman15e47302012-09-07 20:12:54 +00002638 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002639 if (nlh == NULL) {
2640 kfree_skb(skb);
2641 return -EMSGSIZE;
2642 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002643
Thomas Graf7b67c852007-08-22 13:53:52 -07002644 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002645 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002646
Thomas Graf98250692007-08-22 12:47:26 -07002647 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002648
Michal Kubecek21ee5432014-06-03 10:26:06 +02002649 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002650}
2651
Thomas Graf7deb2262007-08-22 13:57:39 -07002652static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002653{
Thomas Graf7deb2262007-08-22 13:57:39 -07002654 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002655 if (x->aead)
2656 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002657 if (x->aalg) {
2658 l += nla_total_size(sizeof(struct xfrm_algo) +
2659 (x->aalg->alg_key_len + 7) / 8);
2660 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2661 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002662 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002663 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002664 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002665 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002666 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002667 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002668 if (x->tfcpad)
2669 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002670 if (x->replay_esn)
2671 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
dingzhif293a5e2014-10-30 09:39:36 +01002672 else
2673 l += nla_total_size(sizeof(struct xfrm_replay_state));
Herbert Xu68325d32007-10-09 13:30:57 -07002674 if (x->security)
2675 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2676 x->security->ctx_len);
2677 if (x->coaddr)
2678 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002679 if (x->props.extra_flags)
2680 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002681
Herbert Xud26f3982007-11-13 21:47:08 -08002682 /* Must count x->lastused as it may become non-zero behind our back. */
Nicolas Dichtelde95c4a2016-04-22 17:31:23 +02002683 l += nla_total_size_64bit(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002684
2685 return l;
2686}
2687
David S. Miller214e0052011-02-24 00:02:38 -05002688static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002689{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002690 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002691 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002692 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002693 struct nlmsghdr *nlh;
2694 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002695 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002696 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002697
2698 headlen = sizeof(*p);
2699 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002700 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002701 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002702 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002703 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002704 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002705
Thomas Graf7deb2262007-08-22 13:57:39 -07002706 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002707 if (skb == NULL)
2708 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002709
Eric W. Biederman15e47302012-09-07 20:12:54 +00002710 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002711 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002712 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002713 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002714
Thomas Graf7b67c852007-08-22 13:53:52 -07002715 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002716 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002717 struct nlattr *attr;
2718
Thomas Graf7b67c852007-08-22 13:53:52 -07002719 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002720 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2721 id->spi = x->id.spi;
2722 id->family = x->props.family;
2723 id->proto = x->id.proto;
2724
Thomas Grafc0144be2007-08-22 13:55:43 -07002725 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002726 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002727 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002728 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002729
2730 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002731 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002732 err = copy_to_user_state_extra(x, p, skb);
2733 if (err)
2734 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002735
Thomas Graf98250692007-08-22 12:47:26 -07002736 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002737
Michal Kubecek21ee5432014-06-03 10:26:06 +02002738 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002739
David S. Miller1d1e34d2012-06-27 21:57:03 -07002740out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002741 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002742 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002743}
2744
David S. Miller214e0052011-02-24 00:02:38 -05002745static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002746{
2747
2748 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002749 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002750 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002751 case XFRM_MSG_NEWAE:
2752 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002753 case XFRM_MSG_DELSA:
2754 case XFRM_MSG_UPDSA:
2755 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002756 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002757 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002758 return xfrm_notify_sa_flush(c);
2759 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002760 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2761 c->event);
2762 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002763 }
2764
2765 return 0;
2766
2767}
2768
Thomas Graf7deb2262007-08-22 13:57:39 -07002769static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2770 struct xfrm_policy *xp)
2771{
2772 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2773 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002774 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002775 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2776 + userpolicy_type_attrsize();
2777}
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002780 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002782 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 struct xfrm_user_acquire *ua;
2784 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002785 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002787 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2788 if (nlh == NULL)
2789 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Thomas Graf7b67c852007-08-22 13:53:52 -07002791 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 memcpy(&ua->id, &x->id, sizeof(ua->id));
2793 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2794 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002795 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 ua->aalgos = xt->aalgos;
2797 ua->ealgos = xt->ealgos;
2798 ua->calgos = xt->calgos;
2799 ua->seq = x->km.seq = seq;
2800
David S. Miller1d1e34d2012-06-27 21:57:03 -07002801 err = copy_to_user_tmpl(xp, skb);
2802 if (!err)
2803 err = copy_to_user_state_sec_ctx(x, skb);
2804 if (!err)
2805 err = copy_to_user_policy_type(xp->type, skb);
2806 if (!err)
2807 err = xfrm_mark_put(skb, &xp->mark);
2808 if (err) {
2809 nlmsg_cancel(skb, nlh);
2810 return err;
2811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Johannes Berg053c0952015-01-16 22:09:00 +01002813 nlmsg_end(skb, nlh);
2814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815}
2816
2817static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002818 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002820 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Thomas Graf7deb2262007-08-22 13:57:39 -07002823 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 if (skb == NULL)
2825 return -ENOMEM;
2826
Fan Du65e07362012-08-15 10:13:47 +08002827 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 BUG();
2829
Michal Kubecek21ee5432014-06-03 10:26:06 +02002830 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831}
2832
2833/* User gives us xfrm_user_policy_info followed by an array of 0
2834 * or more templates.
2835 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002836static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 u8 *data, int len, int *dir)
2838{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002839 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2841 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2842 struct xfrm_policy *xp;
2843 int nr;
2844
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002845 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 case AF_INET:
2847 if (opt != IP_XFRM_POLICY) {
2848 *dir = -EOPNOTSUPP;
2849 return NULL;
2850 }
2851 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002852#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 case AF_INET6:
2854 if (opt != IPV6_XFRM_POLICY) {
2855 *dir = -EOPNOTSUPP;
2856 return NULL;
2857 }
2858 break;
2859#endif
2860 default:
2861 *dir = -EINVAL;
2862 return NULL;
2863 }
2864
2865 *dir = -EINVAL;
2866
2867 if (len < sizeof(*p) ||
2868 verify_newpolicy_info(p))
2869 return NULL;
2870
2871 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002872 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 return NULL;
2874
Herbert Xua4f1bac2005-07-26 15:43:17 -07002875 if (p->dir > XFRM_POLICY_OUT)
2876 return NULL;
2877
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002878 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 if (xp == NULL) {
2880 *dir = -ENOBUFS;
2881 return NULL;
2882 }
2883
2884 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002885 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 copy_templates(xp, ut, nr);
2887
2888 *dir = p->dir;
2889
2890 return xp;
2891}
2892
Thomas Graf7deb2262007-08-22 13:57:39 -07002893static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2894{
2895 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2896 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2897 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002898 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002899 + userpolicy_type_attrsize();
2900}
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002903 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
2905 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002906 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002907 struct nlmsghdr *nlh;
2908 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Eric W. Biederman15e47302012-09-07 20:12:54 +00002910 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002911 if (nlh == NULL)
2912 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Thomas Graf7b67c852007-08-22 13:53:52 -07002914 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002916 err = copy_to_user_tmpl(xp, skb);
2917 if (!err)
2918 err = copy_to_user_sec_ctx(xp, skb);
2919 if (!err)
2920 err = copy_to_user_policy_type(xp->type, skb);
2921 if (!err)
2922 err = xfrm_mark_put(skb, &xp->mark);
2923 if (err) {
2924 nlmsg_cancel(skb, nlh);
2925 return err;
2926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 upe->hard = !!hard;
2928
Johannes Berg053c0952015-01-16 22:09:00 +01002929 nlmsg_end(skb, nlh);
2930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
David S. Miller214e0052011-02-24 00:02:38 -05002933static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002935 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Thomas Graf7deb2262007-08-22 13:57:39 -07002938 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 if (skb == NULL)
2940 return -ENOMEM;
2941
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002942 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 BUG();
2944
Michal Kubecek21ee5432014-06-03 10:26:06 +02002945 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
David S. Miller214e0052011-02-24 00:02:38 -05002948static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002949{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002950 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002951 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002952 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002953 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002954 struct nlmsghdr *nlh;
2955 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002956 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002957
2958 headlen = sizeof(*p);
2959 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002960 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002961 headlen = sizeof(*id);
2962 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002963 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002964 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002965 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002966
Thomas Graf7deb2262007-08-22 13:57:39 -07002967 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002968 if (skb == NULL)
2969 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002970
Eric W. Biederman15e47302012-09-07 20:12:54 +00002971 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002972 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002973 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002974 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002975
Thomas Graf7b67c852007-08-22 13:53:52 -07002976 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002977 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002978 struct nlattr *attr;
2979
Thomas Graf7b67c852007-08-22 13:53:52 -07002980 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002981 memset(id, 0, sizeof(*id));
2982 id->dir = dir;
2983 if (c->data.byid)
2984 id->index = xp->index;
2985 else
2986 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2987
Thomas Grafc0144be2007-08-22 13:55:43 -07002988 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002989 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002990 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002991 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002992
2993 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002994 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002995
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002996 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002997 err = copy_to_user_tmpl(xp, skb);
2998 if (!err)
2999 err = copy_to_user_policy_type(xp->type, skb);
3000 if (!err)
3001 err = xfrm_mark_put(skb, &xp->mark);
3002 if (err)
3003 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00003004
Thomas Graf98250692007-08-22 12:47:26 -07003005 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003006
Michal Kubecek21ee5432014-06-03 10:26:06 +02003007 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003008
David S. Miller1d1e34d2012-06-27 21:57:03 -07003009out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003010 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003011 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003012}
3013
David S. Miller214e0052011-02-24 00:02:38 -05003014static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003015{
Alexey Dobriyan70678022008-11-25 17:50:36 -08003016 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003017 struct nlmsghdr *nlh;
3018 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07003019 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003020
Thomas Graf7deb2262007-08-22 13:57:39 -07003021 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003022 if (skb == NULL)
3023 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003024
Eric W. Biederman15e47302012-09-07 20:12:54 +00003025 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003026 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003027 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07003028 goto out_free_skb;
3029 err = copy_to_user_policy_type(c->data.type, skb);
3030 if (err)
3031 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003032
Thomas Graf98250692007-08-22 12:47:26 -07003033 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003034
Michal Kubecek21ee5432014-06-03 10:26:06 +02003035 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003036
David S. Miller1d1e34d2012-06-27 21:57:03 -07003037out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003038 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07003039 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003040}
3041
David S. Miller214e0052011-02-24 00:02:38 -05003042static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003043{
3044
3045 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07003046 case XFRM_MSG_NEWPOLICY:
3047 case XFRM_MSG_UPDPOLICY:
3048 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003049 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003050 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003051 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07003052 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003053 return xfrm_exp_policy_notify(xp, dir, c);
3054 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00003055 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3056 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07003057 }
3058
3059 return 0;
3060
3061}
3062
Thomas Graf7deb2262007-08-22 13:57:39 -07003063static inline size_t xfrm_report_msgsize(void)
3064{
3065 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3066}
3067
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003068static int build_report(struct sk_buff *skb, u8 proto,
3069 struct xfrm_selector *sel, xfrm_address_t *addr)
3070{
3071 struct xfrm_user_report *ur;
3072 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003073
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003074 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3075 if (nlh == NULL)
3076 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003077
Thomas Graf7b67c852007-08-22 13:53:52 -07003078 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003079 ur->proto = proto;
3080 memcpy(&ur->sel, sel, sizeof(ur->sel));
3081
David S. Miller1d1e34d2012-06-27 21:57:03 -07003082 if (addr) {
3083 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3084 if (err) {
3085 nlmsg_cancel(skb, nlh);
3086 return err;
3087 }
3088 }
Johannes Berg053c0952015-01-16 22:09:00 +01003089 nlmsg_end(skb, nlh);
3090 return 0;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003091}
3092
Alexey Dobriyandb983c12008-11-25 17:51:01 -08003093static int xfrm_send_report(struct net *net, u8 proto,
3094 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003095{
3096 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003097
Thomas Graf7deb2262007-08-22 13:57:39 -07003098 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003099 if (skb == NULL)
3100 return -ENOMEM;
3101
3102 if (build_report(skb, proto, sel, addr) < 0)
3103 BUG();
3104
Michal Kubecek21ee5432014-06-03 10:26:06 +02003105 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003106}
3107
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003108static inline size_t xfrm_mapping_msgsize(void)
3109{
3110 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3111}
3112
3113static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3114 xfrm_address_t *new_saddr, __be16 new_sport)
3115{
3116 struct xfrm_user_mapping *um;
3117 struct nlmsghdr *nlh;
3118
3119 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3120 if (nlh == NULL)
3121 return -EMSGSIZE;
3122
3123 um = nlmsg_data(nlh);
3124
3125 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3126 um->id.spi = x->id.spi;
3127 um->id.family = x->props.family;
3128 um->id.proto = x->id.proto;
3129 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3130 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3131 um->new_sport = new_sport;
3132 um->old_sport = x->encap->encap_sport;
3133 um->reqid = x->props.reqid;
3134
Johannes Berg053c0952015-01-16 22:09:00 +01003135 nlmsg_end(skb, nlh);
3136 return 0;
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003137}
3138
3139static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3140 __be16 sport)
3141{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003142 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003143 struct sk_buff *skb;
3144
3145 if (x->id.proto != IPPROTO_ESP)
3146 return -EINVAL;
3147
3148 if (!x->encap)
3149 return -EINVAL;
3150
3151 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3152 if (skb == NULL)
3153 return -ENOMEM;
3154
3155 if (build_mapping(skb, x, ipaddr, sport) < 0)
3156 BUG();
3157
Michal Kubecek21ee5432014-06-03 10:26:06 +02003158 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003159}
3160
Horia Geanta0f245582014-02-12 16:20:06 +02003161static bool xfrm_is_alive(const struct km_event *c)
3162{
3163 return (bool)xfrm_acquire_is_on(c->net);
3164}
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166static struct xfrm_mgr netlink_mgr = {
3167 .id = "netlink",
3168 .notify = xfrm_send_state_notify,
3169 .acquire = xfrm_send_acquire,
3170 .compile_policy = xfrm_compile_policy,
3171 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003172 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003173 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003174 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003175 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176};
3177
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003178static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179{
Patrick McHardybe336902006-03-20 22:40:54 -08003180 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003181 struct netlink_kernel_cfg cfg = {
3182 .groups = XFRMNLGRP_MAX,
3183 .input = xfrm_netlink_rcv,
3184 };
Patrick McHardybe336902006-03-20 22:40:54 -08003185
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003186 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003187 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003189 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003190 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 return 0;
3192}
3193
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003194static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003195{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003196 struct net *net;
3197 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003198 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003199 synchronize_net();
3200 list_for_each_entry(net, net_exit_list, exit_list)
3201 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003202}
3203
3204static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003205 .init = xfrm_user_net_init,
3206 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003207};
3208
3209static int __init xfrm_user_init(void)
3210{
3211 int rv;
3212
3213 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3214
3215 rv = register_pernet_subsys(&xfrm_user_net_ops);
3216 if (rv < 0)
3217 return rv;
3218 rv = xfrm_register_km(&netlink_mgr);
3219 if (rv < 0)
3220 unregister_pernet_subsys(&xfrm_user_net_ops);
3221 return rv;
3222}
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224static void __exit xfrm_user_exit(void)
3225{
3226 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003227 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
3230module_init(xfrm_user_init);
3231module_exit(xfrm_user_exit);
3232MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003233MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003234