blob: 0cebf1fc37a2743ba096747056fab6c927922b23 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +000029#include <net/ah.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070032#include <linux/in6.h>
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Thomas Graf5424f322007-08-22 14:01:33 -070035static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Thomas Graf5424f322007-08-22 14:01:33 -070037 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct xfrm_algo *algp;
39
40 if (!rt)
41 return 0;
42
Thomas Graf5424f322007-08-22 14:01:33 -070043 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080044 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070045 return -EINVAL;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 switch (type) {
48 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 break;
52
53 default:
54 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
58 return 0;
59}
60
Martin Willi4447bb32009-11-25 00:29:52 +000061static int verify_auth_trunc(struct nlattr **attrs)
62{
63 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
64 struct xfrm_algo_auth *algp;
65
66 if (!rt)
67 return 0;
68
69 algp = nla_data(rt);
70 if (nla_len(rt) < xfrm_alg_auth_len(algp))
71 return -EINVAL;
72
73 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
74 return 0;
75}
76
Herbert Xu1a6509d2008-01-28 19:37:29 -080077static int verify_aead(struct nlattr **attrs)
78{
79 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
80 struct xfrm_algo_aead *algp;
81
82 if (!rt)
83 return 0;
84
85 algp = nla_data(rt);
86 if (nla_len(rt) < aead_len(algp))
87 return -EINVAL;
88
89 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
90 return 0;
91}
92
Thomas Graf5424f322007-08-22 14:01:33 -070093static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070094 xfrm_address_t **addrp)
95{
Thomas Graf5424f322007-08-22 14:01:33 -070096 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070097
Thomas Grafcf5cb792007-08-22 13:59:04 -070098 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -070099 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700100}
Trent Jaegerdf718372005-12-13 23:12:27 -0800101
Thomas Graf5424f322007-08-22 14:01:33 -0700102static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800103{
Thomas Graf5424f322007-08-22 14:01:33 -0700104 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800105 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800106
107 if (!rt)
108 return 0;
109
Thomas Graf5424f322007-08-22 14:01:33 -0700110 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700111 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800112 return -EINVAL;
113
114 return 0;
115}
116
Steffen Klassertd8647b72011-03-08 00:10:27 +0000117static inline int verify_replay(struct xfrm_usersa_info *p,
118 struct nlattr **attrs)
119{
120 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000121 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000122
Mathias Krauseecd79182012-09-20 10:01:49 +0000123 if (p->flags & XFRM_STATE_ESN) {
124 if (!rt)
125 return -EINVAL;
126
127 rs = nla_data(rt);
128
129 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
130 return -EINVAL;
131
132 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
133 nla_len(rt) != sizeof(*rs))
134 return -EINVAL;
135 }
Steffen Klassert7833aa02011-04-25 19:41:21 +0000136
Steffen Klassertd8647b72011-03-08 00:10:27 +0000137 if (!rt)
138 return 0;
139
Fan Du01714102014-01-18 09:54:28 +0800140 /* As only ESP and AH support ESN feature. */
141 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
Steffen Klassert02aadf72011-03-28 19:48:09 +0000142 return -EINVAL;
143
Steffen Klassertd8647b72011-03-08 00:10:27 +0000144 if (p->replay_window != 0)
145 return -EINVAL;
146
147 return 0;
148}
Trent Jaegerdf718372005-12-13 23:12:27 -0800149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700151 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 int err;
154
155 err = -EINVAL;
156 switch (p->family) {
157 case AF_INET:
158 break;
159
160 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000161#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 break;
163#else
164 err = -EAFNOSUPPORT;
165 goto out;
166#endif
167
168 default:
169 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 err = -EINVAL;
173 switch (p->id.proto) {
174 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000175 if ((!attrs[XFRMA_ALG_AUTH] &&
176 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800177 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700178 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000179 attrs[XFRMA_ALG_COMP] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200180 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto out;
182 break;
183
184 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800185 if (attrs[XFRMA_ALG_COMP])
186 goto out;
187 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000188 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800189 !attrs[XFRMA_ALG_CRYPT] &&
190 !attrs[XFRMA_ALG_AEAD])
191 goto out;
192 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000193 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800194 attrs[XFRMA_ALG_CRYPT]) &&
195 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000197 if (attrs[XFRMA_TFCPAD] &&
198 p->mode != XFRM_MODE_TUNNEL)
199 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
201
202 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700203 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800204 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700205 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000206 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000207 attrs[XFRMA_ALG_CRYPT] ||
Tobias Brunnera0e5ef52014-06-26 15:12:45 +0200208 attrs[XFRMA_TFCPAD] ||
209 (ntohl(p->id.spi) >= 0x10000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 goto out;
211 break;
212
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000213#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700214 case IPPROTO_DSTOPTS:
215 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700216 if (attrs[XFRMA_ALG_COMP] ||
217 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000218 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800219 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700220 attrs[XFRMA_ALG_CRYPT] ||
221 attrs[XFRMA_ENCAP] ||
222 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000223 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700224 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700225 goto out;
226 break;
227#endif
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 default:
230 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Herbert Xu1a6509d2008-01-28 19:37:29 -0800233 if ((err = verify_aead(attrs)))
234 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000235 if ((err = verify_auth_trunc(attrs)))
236 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700237 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700239 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700241 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700243 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800244 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000245 if ((err = verify_replay(p, attrs)))
246 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 err = -EINVAL;
249 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700250 case XFRM_MODE_TRANSPORT:
251 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700252 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700253 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 break;
255
256 default:
257 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 err = 0;
261
262out:
263 return err;
264}
265
266static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800267 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700268 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct xfrm_algo *p, *ualg;
271 struct xfrm_algo_desc *algo;
272
273 if (!rta)
274 return 0;
275
Thomas Graf5424f322007-08-22 14:01:33 -0700276 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 algo = get_byname(ualg->alg_name, 1);
279 if (!algo)
280 return -ENOSYS;
281 *props = algo->desc.sadb_alg_id;
282
Eric Dumazet0f99be02008-01-08 23:39:06 -0800283 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (!p)
285 return -ENOMEM;
286
Herbert Xu04ff1262006-08-13 08:50:00 +1000287 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 *algpp = p;
289 return 0;
290}
291
Herbert Xu69b01372015-05-27 16:03:45 +0800292static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
293{
294 struct xfrm_algo *p, *ualg;
295 struct xfrm_algo_desc *algo;
296
297 if (!rta)
298 return 0;
299
300 ualg = nla_data(rta);
301
302 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
303 if (!algo)
304 return -ENOSYS;
305 x->props.ealgo = algo->desc.sadb_alg_id;
306
307 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
308 if (!p)
309 return -ENOMEM;
310
311 strcpy(p->alg_name, algo->name);
312 x->ealg = p;
313 x->geniv = algo->uinfo.encr.geniv;
314 return 0;
315}
316
Martin Willi4447bb32009-11-25 00:29:52 +0000317static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
318 struct nlattr *rta)
319{
320 struct xfrm_algo *ualg;
321 struct xfrm_algo_auth *p;
322 struct xfrm_algo_desc *algo;
323
324 if (!rta)
325 return 0;
326
327 ualg = nla_data(rta);
328
329 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
330 if (!algo)
331 return -ENOSYS;
332 *props = algo->desc.sadb_alg_id;
333
334 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
335 if (!p)
336 return -ENOMEM;
337
338 strcpy(p->alg_name, algo->name);
339 p->alg_key_len = ualg->alg_key_len;
340 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
341 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
342
343 *algpp = p;
344 return 0;
345}
346
347static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
348 struct nlattr *rta)
349{
350 struct xfrm_algo_auth *p, *ualg;
351 struct xfrm_algo_desc *algo;
352
353 if (!rta)
354 return 0;
355
356 ualg = nla_data(rta);
357
358 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
359 if (!algo)
360 return -ENOSYS;
Herbert Xu689f1c92014-09-18 16:38:18 +0800361 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000362 return -EINVAL;
363 *props = algo->desc.sadb_alg_id;
364
365 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
366 if (!p)
367 return -ENOMEM;
368
369 strcpy(p->alg_name, algo->name);
370 if (!p->alg_trunc_len)
371 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
372
373 *algpp = p;
374 return 0;
375}
376
Herbert Xu69b01372015-05-27 16:03:45 +0800377static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
Herbert Xu1a6509d2008-01-28 19:37:29 -0800378{
379 struct xfrm_algo_aead *p, *ualg;
380 struct xfrm_algo_desc *algo;
381
382 if (!rta)
383 return 0;
384
385 ualg = nla_data(rta);
386
387 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
388 if (!algo)
389 return -ENOSYS;
Herbert Xu69b01372015-05-27 16:03:45 +0800390 x->props.ealgo = algo->desc.sadb_alg_id;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800391
392 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
393 if (!p)
394 return -ENOMEM;
395
396 strcpy(p->alg_name, algo->name);
Herbert Xu69b01372015-05-27 16:03:45 +0800397 x->aead = p;
398 x->geniv = algo->uinfo.aead.geniv;
Herbert Xu1a6509d2008-01-28 19:37:29 -0800399 return 0;
400}
401
Steffen Klasserte2b19122011-03-28 19:47:30 +0000402static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
403 struct nlattr *rp)
404{
405 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000406 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000407
408 if (!replay_esn || !rp)
409 return 0;
410
411 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000412 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000413
Mathias Krauseecd79182012-09-20 10:01:49 +0000414 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000415 return -EINVAL;
416
417 return 0;
418}
419
Steffen Klassertd8647b72011-03-08 00:10:27 +0000420static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
421 struct xfrm_replay_state_esn **preplay_esn,
422 struct nlattr *rta)
423{
424 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000425 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000426
427 if (!rta)
428 return 0;
429
430 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000431 klen = xfrm_replay_state_esn_len(up);
432 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000433
Mathias Krauseecd79182012-09-20 10:01:49 +0000434 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000435 if (!p)
436 return -ENOMEM;
437
Mathias Krauseecd79182012-09-20 10:01:49 +0000438 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000439 if (!pp) {
440 kfree(p);
441 return -ENOMEM;
442 }
443
Mathias Krauseecd79182012-09-20 10:01:49 +0000444 memcpy(p, up, ulen);
445 memcpy(pp, up, ulen);
446
Steffen Klassertd8647b72011-03-08 00:10:27 +0000447 *replay_esn = p;
448 *preplay_esn = pp;
449
450 return 0;
451}
452
Joy Latten661697f2007-04-13 16:14:35 -0700453static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800454{
Trent Jaegerdf718372005-12-13 23:12:27 -0800455 int len = 0;
456
457 if (xfrm_ctx) {
458 len += sizeof(struct xfrm_user_sec_ctx);
459 len += xfrm_ctx->ctx_len;
460 }
461 return len;
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
465{
466 memcpy(&x->id, &p->id, sizeof(x->id));
467 memcpy(&x->sel, &p->sel, sizeof(x->sel));
468 memcpy(&x->lft, &p->lft, sizeof(x->lft));
469 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800470 x->props.replay_window = min_t(unsigned int, p->replay_window,
471 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 x->props.reqid = p->reqid;
473 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700474 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700476
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700477 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700478 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800481/*
482 * someday when pfkey also has support, we could have the code
483 * somehow made shareable and move it to xfrm_state.c - JHS
484 *
485*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000486static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
487 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800488{
Thomas Graf5424f322007-08-22 14:01:33 -0700489 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000490 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700491 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
492 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
493 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800494
Steffen Klassertd8647b72011-03-08 00:10:27 +0000495 if (re) {
496 struct xfrm_replay_state_esn *replay_esn;
497 replay_esn = nla_data(re);
498 memcpy(x->replay_esn, replay_esn,
499 xfrm_replay_state_esn_len(replay_esn));
500 memcpy(x->preplay_esn, replay_esn,
501 xfrm_replay_state_esn_len(replay_esn));
502 }
503
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800504 if (rp) {
505 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700506 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800507 memcpy(&x->replay, replay, sizeof(*replay));
508 memcpy(&x->preplay, replay, sizeof(*replay));
509 }
510
511 if (lt) {
512 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700513 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800514 x->curlft.bytes = ltime->bytes;
515 x->curlft.packets = ltime->packets;
516 x->curlft.add_time = ltime->add_time;
517 x->curlft.use_time = ltime->use_time;
518 }
519
Thomas Grafcf5cb792007-08-22 13:59:04 -0700520 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700521 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800522
Thomas Grafcf5cb792007-08-22 13:59:04 -0700523 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700524 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800525}
526
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800527static struct xfrm_state *xfrm_state_construct(struct net *net,
528 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700529 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 int *errp)
531{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800532 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int err = -ENOMEM;
534
535 if (!x)
536 goto error_no_put;
537
538 copy_from_user_state(x, p);
539
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100540 if (attrs[XFRMA_SA_EXTRA_FLAGS])
541 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
542
Herbert Xu69b01372015-05-27 16:03:45 +0800543 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
Herbert Xu1a6509d2008-01-28 19:37:29 -0800544 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000545 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
546 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000548 if (!x->props.aalgo) {
549 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
550 attrs[XFRMA_ALG_AUTH])))
551 goto error;
552 }
Herbert Xu69b01372015-05-27 16:03:45 +0800553 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 goto error;
555 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
556 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700557 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700559
560 if (attrs[XFRMA_ENCAP]) {
561 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
562 sizeof(*x->encap), GFP_KERNEL);
563 if (x->encap == NULL)
564 goto error;
565 }
566
Martin Willi35d28562010-12-08 04:37:49 +0000567 if (attrs[XFRMA_TFCPAD])
568 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
569
Thomas Graffd211502007-09-06 03:28:08 -0700570 if (attrs[XFRMA_COADDR]) {
571 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
572 sizeof(*x->coaddr), GFP_KERNEL);
573 if (x->coaddr == NULL)
574 goto error;
575 }
576
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000577 xfrm_mark_get(attrs, &x->mark);
578
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700579 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (err)
581 goto error;
582
Thomas Graffd211502007-09-06 03:28:08 -0700583 if (attrs[XFRMA_SEC_CTX] &&
584 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800585 goto error;
586
Steffen Klassertd8647b72011-03-08 00:10:27 +0000587 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
588 attrs[XFRMA_REPLAY_ESN_VAL])))
589 goto error;
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800592 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800593 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800594 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800595
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000596 if ((err = xfrm_init_replay(x)))
597 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800598
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000599 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000600 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 return x;
603
604error:
605 x->km.state = XFRM_STATE_DEAD;
606 xfrm_state_put(x);
607error_no_put:
608 *errp = err;
609 return NULL;
610}
611
Christoph Hellwig22e70052007-01-02 15:22:30 -0800612static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700613 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800615 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700616 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct xfrm_state *x;
618 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700619 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Thomas Graf35a7aa02007-08-22 14:00:40 -0700621 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (err)
623 return err;
624
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800625 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!x)
627 return err;
628
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700629 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
631 err = xfrm_state_add(x);
632 else
633 err = xfrm_state_update(x);
634
Tetsuo Handa2e710292014-04-22 21:48:30 +0900635 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (err < 0) {
638 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800639 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700640 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700643 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000644 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700645 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700646
647 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700648out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700649 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return err;
651}
652
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800653static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
654 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700655 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700656 int *errp)
657{
658 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000659 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700660 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000661 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700662
663 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
664 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000665 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700666 } else {
667 xfrm_address_t *saddr = NULL;
668
Thomas Graf35a7aa02007-08-22 14:00:40 -0700669 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700670 if (!saddr) {
671 err = -EINVAL;
672 goto out;
673 }
674
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800675 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000676 x = xfrm_state_lookup_byaddr(net, mark,
677 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800678 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700679 }
680
681 out:
682 if (!x && errp)
683 *errp = err;
684 return x;
685}
686
Christoph Hellwig22e70052007-01-02 15:22:30 -0800687static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700688 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800690 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700692 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700693 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700694 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800696 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700698 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
David S. Miller6f68dc32006-06-08 23:58:52 -0700700 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700701 goto out;
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700704 err = -EPERM;
705 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700708 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600709
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700710 if (err < 0)
711 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700712
713 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000714 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700715 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700716 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700718out:
Tetsuo Handa2e710292014-04-22 21:48:30 +0900719 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700720 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700721 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
725{
Mathias Krausef778a632012-09-19 11:33:39 +0000726 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 memcpy(&p->id, &x->id, sizeof(p->id));
728 memcpy(&p->sel, &x->sel, sizeof(p->sel));
729 memcpy(&p->lft, &x->lft, sizeof(p->lft));
730 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
731 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700732 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 p->mode = x->props.mode;
734 p->replay_window = x->props.replay_window;
735 p->reqid = x->props.reqid;
736 p->family = x->props.family;
737 p->flags = x->props.flags;
738 p->seq = x->km.seq;
739}
740
741struct xfrm_dump_info {
742 struct sk_buff *in_skb;
743 struct sk_buff *out_skb;
744 u32 nlmsg_seq;
745 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746};
747
Thomas Grafc0144be2007-08-22 13:55:43 -0700748static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
749{
Thomas Grafc0144be2007-08-22 13:55:43 -0700750 struct xfrm_user_sec_ctx *uctx;
751 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700752 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700753
754 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
755 if (attr == NULL)
756 return -EMSGSIZE;
757
758 uctx = nla_data(attr);
759 uctx->exttype = XFRMA_SEC_CTX;
760 uctx->len = ctx_size;
761 uctx->ctx_doi = s->ctx_doi;
762 uctx->ctx_alg = s->ctx_alg;
763 uctx->ctx_len = s->ctx_len;
764 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
765
766 return 0;
767}
768
Martin Willi4447bb32009-11-25 00:29:52 +0000769static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
770{
771 struct xfrm_algo *algo;
772 struct nlattr *nla;
773
774 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
775 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
776 if (!nla)
777 return -EMSGSIZE;
778
779 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000780 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000781 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
782 algo->alg_key_len = auth->alg_key_len;
783
784 return 0;
785}
786
Herbert Xu68325d32007-10-09 13:30:57 -0700787/* Don't change this without updating xfrm_sa_len! */
788static int copy_to_user_state_extra(struct xfrm_state *x,
789 struct xfrm_usersa_info *p,
790 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700792 int ret = 0;
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 copy_to_user_state(x, p);
795
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100796 if (x->props.extra_flags) {
797 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
798 x->props.extra_flags);
799 if (ret)
800 goto out;
801 }
802
David S. Miller1d1e34d2012-06-27 21:57:03 -0700803 if (x->coaddr) {
804 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
805 if (ret)
806 goto out;
807 }
808 if (x->lastused) {
809 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
810 if (ret)
811 goto out;
812 }
813 if (x->aead) {
814 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
815 if (ret)
816 goto out;
817 }
818 if (x->aalg) {
819 ret = copy_to_user_auth(x->aalg, skb);
820 if (!ret)
821 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
822 xfrm_alg_auth_len(x->aalg), x->aalg);
823 if (ret)
824 goto out;
825 }
826 if (x->ealg) {
827 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
828 if (ret)
829 goto out;
830 }
831 if (x->calg) {
832 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
833 if (ret)
834 goto out;
835 }
836 if (x->encap) {
837 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
838 if (ret)
839 goto out;
840 }
841 if (x->tfcpad) {
842 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
843 if (ret)
844 goto out;
845 }
846 ret = xfrm_mark_put(skb, &x->mark);
847 if (ret)
848 goto out;
dingzhif293a5e2014-10-30 09:39:36 +0100849 if (x->replay_esn)
David S. Miller1d1e34d2012-06-27 21:57:03 -0700850 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
851 xfrm_replay_state_esn_len(x->replay_esn),
852 x->replay_esn);
dingzhif293a5e2014-10-30 09:39:36 +0100853 else
854 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
855 &x->replay);
856 if (ret)
857 goto out;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700858 if (x->security)
859 ret = copy_sec_ctx(x->security, skb);
860out:
861 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700862}
863
864static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
865{
866 struct xfrm_dump_info *sp = ptr;
867 struct sk_buff *in_skb = sp->in_skb;
868 struct sk_buff *skb = sp->out_skb;
869 struct xfrm_usersa_info *p;
870 struct nlmsghdr *nlh;
871 int err;
872
Eric W. Biederman15e47302012-09-07 20:12:54 +0000873 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700874 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
875 if (nlh == NULL)
876 return -EMSGSIZE;
877
878 p = nlmsg_data(nlh);
879
880 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700881 if (err) {
882 nlmsg_cancel(skb, nlh);
883 return err;
884 }
Thomas Graf98250692007-08-22 12:47:26 -0700885 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Timo Teras4c563f72008-02-28 21:31:08 -0800889static int xfrm_dump_sa_done(struct netlink_callback *cb)
890{
891 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800892 struct sock *sk = cb->skb->sk;
893 struct net *net = sock_net(sk);
894
895 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800896 return 0;
897}
898
Nicolas Dichteld3623092014-02-14 15:30:36 +0100899static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
901{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800902 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800903 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 struct xfrm_dump_info info;
905
Timo Teras4c563f72008-02-28 21:31:08 -0800906 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
907 sizeof(cb->args) - sizeof(cb->args[0]));
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 info.in_skb = cb->skb;
910 info.out_skb = skb;
911 info.nlmsg_seq = cb->nlh->nlmsg_seq;
912 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800913
914 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100915 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100916 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100917 u8 proto = 0;
918 int err;
919
Timo Teras4c563f72008-02-28 21:31:08 -0800920 cb->args[0] = 1;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100921
922 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
923 xfrma_policy);
924 if (err < 0)
925 return err;
926
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100927 if (attrs[XFRMA_ADDRESS_FILTER]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100928 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
929 if (filter == NULL)
930 return -ENOMEM;
931
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100932 memcpy(filter, nla_data(attrs[XFRMA_ADDRESS_FILTER]),
Nicolas Dichteld3623092014-02-14 15:30:36 +0100933 sizeof(*filter));
934 }
935
936 if (attrs[XFRMA_PROTO])
937 proto = nla_get_u8(attrs[XFRMA_PROTO]);
938
939 xfrm_state_walk_init(walk, proto, filter);
Timo Teras4c563f72008-02-28 21:31:08 -0800940 }
941
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800942 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 return skb->len;
945}
946
947static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
948 struct xfrm_state *x, u32 seq)
949{
950 struct xfrm_dump_info info;
951 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000952 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Thomas Graf7deb2262007-08-22 13:57:39 -0700954 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (!skb)
956 return ERR_PTR(-ENOMEM);
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 info.in_skb = in_skb;
959 info.out_skb = skb;
960 info.nlmsg_seq = seq;
961 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Mathias Krause864745d2012-09-13 11:41:26 +0000963 err = dump_one_state(x, 0, &info);
964 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000966 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
969 return skb;
970}
971
Michal Kubecek21ee5432014-06-03 10:26:06 +0200972/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
973 * Must be called with RCU read lock.
974 */
975static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
976 u32 pid, unsigned int group)
977{
978 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
979
980 if (nlsk)
981 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
982 else
983 return -1;
984}
985
Thomas Graf7deb2262007-08-22 13:57:39 -0700986static inline size_t xfrm_spdinfo_msgsize(void)
987{
988 return NLMSG_ALIGN(4)
989 + nla_total_size(sizeof(struct xfrmu_spdinfo))
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200990 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
991 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
992 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
Thomas Graf7deb2262007-08-22 13:57:39 -0700993}
994
Alexey Dobriyane0710412010-01-23 13:37:10 +0000995static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000996 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700997{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700998 struct xfrmk_spdinfo si;
999 struct xfrmu_spdinfo spc;
1000 struct xfrmu_spdhinfo sph;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001001 struct xfrmu_spdhthresh spt4, spt6;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001002 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001003 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001004 u32 *f;
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001005 unsigned lseq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001006
Eric W. Biederman15e47302012-09-07 20:12:54 +00001007 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001008 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001009 return -EMSGSIZE;
1010
1011 f = nlmsg_data(nlh);
1012 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001013 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -07001014 spc.incnt = si.incnt;
1015 spc.outcnt = si.outcnt;
1016 spc.fwdcnt = si.fwdcnt;
1017 spc.inscnt = si.inscnt;
1018 spc.outscnt = si.outscnt;
1019 spc.fwdscnt = si.fwdscnt;
1020 sph.spdhcnt = si.spdhcnt;
1021 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001022
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001023 do {
1024 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1025
1026 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1027 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1028 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1029 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1030 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1031
David S. Miller1d1e34d2012-06-27 21:57:03 -07001032 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1033 if (!err)
1034 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001035 if (!err)
1036 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1037 if (!err)
1038 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001039 if (err) {
1040 nlmsg_cancel(skb, nlh);
1041 return err;
1042 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001043
Johannes Berg053c0952015-01-16 22:09:00 +01001044 nlmsg_end(skb, nlh);
1045 return 0;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001046}
1047
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001048static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1049 struct nlattr **attrs)
1050{
1051 struct net *net = sock_net(skb->sk);
1052 struct xfrmu_spdhthresh *thresh4 = NULL;
1053 struct xfrmu_spdhthresh *thresh6 = NULL;
1054
1055 /* selector prefixlen thresholds to hash policies */
1056 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1057 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1058
1059 if (nla_len(rta) < sizeof(*thresh4))
1060 return -EINVAL;
1061 thresh4 = nla_data(rta);
1062 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1063 return -EINVAL;
1064 }
1065 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1066 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1067
1068 if (nla_len(rta) < sizeof(*thresh6))
1069 return -EINVAL;
1070 thresh6 = nla_data(rta);
1071 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1072 return -EINVAL;
1073 }
1074
1075 if (thresh4 || thresh6) {
1076 write_seqlock(&net->xfrm.policy_hthresh.lock);
1077 if (thresh4) {
1078 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1079 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1080 }
1081 if (thresh6) {
1082 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1083 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1084 }
1085 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1086
1087 xfrm_policy_hash_rebuild(net);
1088 }
1089
1090 return 0;
1091}
1092
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001093static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001094 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001095{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001096 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001097 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001098 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001099 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001100 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001101
Thomas Graf7deb2262007-08-22 13:57:39 -07001102 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001103 if (r_skb == NULL)
1104 return -ENOMEM;
1105
Eric W. Biederman15e47302012-09-07 20:12:54 +00001106 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001107 BUG();
1108
Eric W. Biederman15e47302012-09-07 20:12:54 +00001109 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001110}
1111
Thomas Graf7deb2262007-08-22 13:57:39 -07001112static inline size_t xfrm_sadinfo_msgsize(void)
1113{
1114 return NLMSG_ALIGN(4)
1115 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1116 + nla_total_size(4); /* XFRMA_SAD_CNT */
1117}
1118
Alexey Dobriyane0710412010-01-23 13:37:10 +00001119static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001120 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001121{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001122 struct xfrmk_sadinfo si;
1123 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001124 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001125 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001126 u32 *f;
1127
Eric W. Biederman15e47302012-09-07 20:12:54 +00001128 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001129 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001130 return -EMSGSIZE;
1131
1132 f = nlmsg_data(nlh);
1133 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001134 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001135
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001136 sh.sadhmcnt = si.sadhmcnt;
1137 sh.sadhcnt = si.sadhcnt;
1138
David S. Miller1d1e34d2012-06-27 21:57:03 -07001139 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1140 if (!err)
1141 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1142 if (err) {
1143 nlmsg_cancel(skb, nlh);
1144 return err;
1145 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001146
Johannes Berg053c0952015-01-16 22:09:00 +01001147 nlmsg_end(skb, nlh);
1148 return 0;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001149}
1150
1151static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001152 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001153{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001154 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001155 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001156 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001157 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001158 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001159
Thomas Graf7deb2262007-08-22 13:57:39 -07001160 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001161 if (r_skb == NULL)
1162 return -ENOMEM;
1163
Eric W. Biederman15e47302012-09-07 20:12:54 +00001164 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001165 BUG();
1166
Eric W. Biederman15e47302012-09-07 20:12:54 +00001167 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001168}
1169
Christoph Hellwig22e70052007-01-02 15:22:30 -08001170static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001171 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001173 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001174 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 struct xfrm_state *x;
1176 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001177 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001179 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (x == NULL)
1181 goto out_noput;
1182
1183 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1184 if (IS_ERR(resp_skb)) {
1185 err = PTR_ERR(resp_skb);
1186 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001187 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189 xfrm_state_put(x);
1190out_noput:
1191 return err;
1192}
1193
Christoph Hellwig22e70052007-01-02 15:22:30 -08001194static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001195 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001197 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 struct xfrm_state *x;
1199 struct xfrm_userspi_info *p;
1200 struct sk_buff *resp_skb;
1201 xfrm_address_t *daddr;
1202 int family;
1203 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001204 u32 mark;
1205 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Thomas Graf7b67c852007-08-22 13:53:52 -07001207 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001208 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (err)
1210 goto out_noput;
1211
1212 family = p->info.family;
1213 daddr = &p->info.id.daddr;
1214
1215 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001216
1217 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001219 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001220 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 xfrm_state_put(x);
1222 x = NULL;
1223 }
1224 }
1225
1226 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001227 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 p->info.id.proto, daddr,
1229 &p->info.saddr, 1,
1230 family);
1231 err = -ENOENT;
1232 if (x == NULL)
1233 goto out_noput;
1234
Herbert Xu658b2192007-10-09 13:29:52 -07001235 err = xfrm_alloc_spi(x, p->min, p->max);
1236 if (err)
1237 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Herbert Xu658b2192007-10-09 13:29:52 -07001239 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (IS_ERR(resp_skb)) {
1241 err = PTR_ERR(resp_skb);
1242 goto out;
1243 }
1244
Eric W. Biederman15e47302012-09-07 20:12:54 +00001245 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247out:
1248 xfrm_state_put(x);
1249out_noput:
1250 return err;
1251}
1252
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001253static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 switch (dir) {
1256 case XFRM_POLICY_IN:
1257 case XFRM_POLICY_OUT:
1258 case XFRM_POLICY_FWD:
1259 break;
1260
1261 default:
1262 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 return 0;
1266}
1267
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001268static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001269{
1270 switch (type) {
1271 case XFRM_POLICY_TYPE_MAIN:
1272#ifdef CONFIG_XFRM_SUB_POLICY
1273 case XFRM_POLICY_TYPE_SUB:
1274#endif
1275 break;
1276
1277 default:
1278 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001279 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001280
1281 return 0;
1282}
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1285{
Fan Due682adf2013-11-07 17:47:48 +08001286 int ret;
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 switch (p->share) {
1289 case XFRM_SHARE_ANY:
1290 case XFRM_SHARE_SESSION:
1291 case XFRM_SHARE_USER:
1292 case XFRM_SHARE_UNIQUE:
1293 break;
1294
1295 default:
1296 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 switch (p->action) {
1300 case XFRM_POLICY_ALLOW:
1301 case XFRM_POLICY_BLOCK:
1302 break;
1303
1304 default:
1305 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 switch (p->sel.family) {
1309 case AF_INET:
1310 break;
1311
1312 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001313#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 break;
1315#else
1316 return -EAFNOSUPPORT;
1317#endif
1318
1319 default:
1320 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Fan Due682adf2013-11-07 17:47:48 +08001323 ret = verify_policy_dir(p->dir);
1324 if (ret)
1325 return ret;
1326 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1327 return -EINVAL;
1328
1329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
Thomas Graf5424f322007-08-22 14:01:33 -07001332static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001333{
Thomas Graf5424f322007-08-22 14:01:33 -07001334 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001335 struct xfrm_user_sec_ctx *uctx;
1336
1337 if (!rt)
1338 return 0;
1339
Thomas Graf5424f322007-08-22 14:01:33 -07001340 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001341 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001342}
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1345 int nr)
1346{
1347 int i;
1348
1349 xp->xfrm_nr = nr;
1350 for (i = 0; i < nr; i++, ut++) {
1351 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1352
1353 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1354 memcpy(&t->saddr, &ut->saddr,
1355 sizeof(xfrm_address_t));
1356 t->reqid = ut->reqid;
1357 t->mode = ut->mode;
1358 t->share = ut->share;
1359 t->optional = ut->optional;
1360 t->aalgos = ut->aalgos;
1361 t->ealgos = ut->ealgos;
1362 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001363 /* If all masks are ~0, then we allow all algorithms. */
1364 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001365 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367}
1368
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001369static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1370{
1371 int i;
1372
1373 if (nr > XFRM_MAX_DEPTH)
1374 return -EINVAL;
1375
1376 for (i = 0; i < nr; i++) {
1377 /* We never validated the ut->family value, so many
1378 * applications simply leave it at zero. The check was
1379 * never made and ut->family was ignored because all
1380 * templates could be assumed to have the same family as
1381 * the policy itself. Now that we will have ipv4-in-ipv6
1382 * and ipv6-in-ipv4 tunnels, this is no longer true.
1383 */
1384 if (!ut[i].family)
1385 ut[i].family = family;
1386
1387 switch (ut[i].family) {
1388 case AF_INET:
1389 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001390#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001391 case AF_INET6:
1392 break;
1393#endif
1394 default:
1395 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001396 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001397 }
1398
1399 return 0;
1400}
1401
Thomas Graf5424f322007-08-22 14:01:33 -07001402static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Thomas Graf5424f322007-08-22 14:01:33 -07001404 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 if (!rt) {
1407 pol->xfrm_nr = 0;
1408 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001409 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1410 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001411 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001413 err = validate_tmpl(nr, utmpl, pol->family);
1414 if (err)
1415 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Thomas Graf5424f322007-08-22 14:01:33 -07001417 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419 return 0;
1420}
1421
Thomas Graf5424f322007-08-22 14:01:33 -07001422static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001423{
Thomas Graf5424f322007-08-22 14:01:33 -07001424 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001425 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001426 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001427 int err;
1428
1429 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001430 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001431 type = upt->type;
1432 }
1433
1434 err = verify_policy_type(type);
1435 if (err)
1436 return err;
1437
1438 *tp = type;
1439 return 0;
1440}
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1443{
1444 xp->priority = p->priority;
1445 xp->index = p->index;
1446 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1447 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1448 xp->action = p->action;
1449 xp->flags = p->flags;
1450 xp->family = p->sel.family;
1451 /* XXX xp->share = p->share; */
1452}
1453
1454static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1455{
Mathias Krause7b789832012-09-19 11:33:40 +00001456 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1458 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1459 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1460 p->priority = xp->priority;
1461 p->index = xp->index;
1462 p->sel.family = xp->family;
1463 p->dir = dir;
1464 p->action = xp->action;
1465 p->flags = xp->flags;
1466 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1467}
1468
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001469static 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 -07001470{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001471 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int err;
1473
1474 if (!xp) {
1475 *errp = -ENOMEM;
1476 return NULL;
1477 }
1478
1479 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001480
Thomas Graf35a7aa02007-08-22 14:00:40 -07001481 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001482 if (err)
1483 goto error;
1484
Thomas Graf35a7aa02007-08-22 14:00:40 -07001485 if (!(err = copy_from_user_tmpl(xp, attrs)))
1486 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001487 if (err)
1488 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001490 xfrm_mark_get(attrs, &xp->mark);
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001493 error:
1494 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001495 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001496 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001497 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Christoph Hellwig22e70052007-01-02 15:22:30 -08001500static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001501 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001503 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001504 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001506 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int err;
1508 int excl;
1509
1510 err = verify_newpolicy_info(p);
1511 if (err)
1512 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001513 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001514 if (err)
1515 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001517 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (!xp)
1519 return err;
1520
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001521 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001522 * Aha! this is anti-netlink really i.e more pfkey derived
1523 * in netlink excl is a flag and you wouldnt need
1524 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1526 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001527 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001530 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 kfree(xp);
1532 return err;
1533 }
1534
Herbert Xuf60f6b82005-06-18 22:44:37 -07001535 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001536 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001537 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001538 km_policy_notify(xp, p->dir, &c);
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 xfrm_pol_put(xp);
1541
1542 return 0;
1543}
1544
1545static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1546{
1547 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1548 int i;
1549
1550 if (xp->xfrm_nr == 0)
1551 return 0;
1552
1553 for (i = 0; i < xp->xfrm_nr; i++) {
1554 struct xfrm_user_tmpl *up = &vec[i];
1555 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1556
Mathias Krause1f868402012-09-19 11:33:41 +00001557 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001559 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1561 up->reqid = kp->reqid;
1562 up->mode = kp->mode;
1563 up->share = kp->share;
1564 up->optional = kp->optional;
1565 up->aalgos = kp->aalgos;
1566 up->ealgos = kp->ealgos;
1567 up->calgos = kp->calgos;
1568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Thomas Grafc0144be2007-08-22 13:55:43 -07001570 return nla_put(skb, XFRMA_TMPL,
1571 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001572}
1573
Serge Hallyn0d681622006-07-24 23:30:44 -07001574static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1575{
1576 if (x->security) {
1577 return copy_sec_ctx(x->security, skb);
1578 }
1579 return 0;
1580}
1581
1582static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1583{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001584 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001585 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001586 return 0;
1587}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001588static inline size_t userpolicy_type_attrsize(void)
1589{
1590#ifdef CONFIG_XFRM_SUB_POLICY
1591 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1592#else
1593 return 0;
1594#endif
1595}
Serge Hallyn0d681622006-07-24 23:30:44 -07001596
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001597#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001598static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001599{
Thomas Grafc0144be2007-08-22 13:55:43 -07001600 struct xfrm_userpolicy_type upt = {
1601 .type = type,
1602 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001603
Thomas Grafc0144be2007-08-22 13:55:43 -07001604 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001605}
1606
1607#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001608static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001609{
1610 return 0;
1611}
1612#endif
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1615{
1616 struct xfrm_dump_info *sp = ptr;
1617 struct xfrm_userpolicy_info *p;
1618 struct sk_buff *in_skb = sp->in_skb;
1619 struct sk_buff *skb = sp->out_skb;
1620 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001621 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Eric W. Biederman15e47302012-09-07 20:12:54 +00001623 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001624 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1625 if (nlh == NULL)
1626 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Thomas Graf7b67c852007-08-22 13:53:52 -07001628 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001630 err = copy_to_user_tmpl(xp, skb);
1631 if (!err)
1632 err = copy_to_user_sec_ctx(xp, skb);
1633 if (!err)
1634 err = copy_to_user_policy_type(xp->type, skb);
1635 if (!err)
1636 err = xfrm_mark_put(skb, &xp->mark);
1637 if (err) {
1638 nlmsg_cancel(skb, nlh);
1639 return err;
1640 }
Thomas Graf98250692007-08-22 12:47:26 -07001641 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
Timo Teras4c563f72008-02-28 21:31:08 -08001645static int xfrm_dump_policy_done(struct netlink_callback *cb)
1646{
1647 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001648 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001649
Fan Du283bc9f2013-11-07 17:47:50 +08001650 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001651 return 0;
1652}
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1655{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001656 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001657 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 struct xfrm_dump_info info;
1659
Timo Teras4c563f72008-02-28 21:31:08 -08001660 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1661 sizeof(cb->args) - sizeof(cb->args[0]));
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 info.in_skb = cb->skb;
1664 info.out_skb = skb;
1665 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1666 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001667
1668 if (!cb->args[0]) {
1669 cb->args[0] = 1;
1670 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1671 }
1672
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001673 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 return skb->len;
1676}
1677
1678static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1679 struct xfrm_policy *xp,
1680 int dir, u32 seq)
1681{
1682 struct xfrm_dump_info info;
1683 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001684 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Thomas Graf7deb2262007-08-22 13:57:39 -07001686 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (!skb)
1688 return ERR_PTR(-ENOMEM);
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 info.in_skb = in_skb;
1691 info.out_skb = skb;
1692 info.nlmsg_seq = seq;
1693 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Mathias Krausec2546372012-09-14 09:58:32 +00001695 err = dump_one_policy(xp, dir, 0, &info);
1696 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001698 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700
1701 return skb;
1702}
1703
Christoph Hellwig22e70052007-01-02 15:22:30 -08001704static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001705 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001707 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 struct xfrm_policy *xp;
1709 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001710 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001712 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001714 struct xfrm_mark m;
1715 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Thomas Graf7b67c852007-08-22 13:53:52 -07001717 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1719
Thomas Graf35a7aa02007-08-22 14:00:40 -07001720 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001721 if (err)
1722 return err;
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 err = verify_policy_dir(p->dir);
1725 if (err)
1726 return err;
1727
1728 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001729 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001730 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001731 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001732 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001733
Thomas Graf35a7aa02007-08-22 14:00:40 -07001734 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001735 if (err)
1736 return err;
1737
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001738 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001739 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001740 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001741
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001742 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001743 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001744 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001745 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001746 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001747 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001748 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (xp == NULL)
1751 return -ENOENT;
1752
1753 if (!delete) {
1754 struct sk_buff *resp_skb;
1755
1756 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1757 if (IS_ERR(resp_skb)) {
1758 err = PTR_ERR(resp_skb);
1759 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001760 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001761 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001763 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09001764 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001765
1766 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001767 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001768
Herbert Xue7443892005-06-18 22:44:18 -07001769 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001770 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001771 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001772 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001773 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001776out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001777 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001778 if (delete && err == 0)
1779 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return err;
1781}
1782
Christoph Hellwig22e70052007-01-02 15:22:30 -08001783static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001784 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001786 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001787 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001788 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04001789 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Tetsuo Handa2e710292014-04-22 21:48:30 +09001791 err = xfrm_state_flush(net, p->proto, true);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001792 if (err) {
1793 if (err == -ESRCH) /* empty table */
1794 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001795 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001796 }
Herbert Xubf088672005-06-18 22:44:00 -07001797 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001798 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001799 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001800 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001801 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001802 km_state_notify(NULL, &c);
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return 0;
1805}
1806
Steffen Klassertd8647b72011-03-08 00:10:27 +00001807static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001808{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001809 size_t replay_size = x->replay_esn ?
1810 xfrm_replay_state_esn_len(x->replay_esn) :
1811 sizeof(struct xfrm_replay_state);
1812
Thomas Graf7deb2262007-08-22 13:57:39 -07001813 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001814 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001815 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001816 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001817 + nla_total_size(4) /* XFRM_AE_RTHR */
1818 + nla_total_size(4); /* XFRM_AE_ETHR */
1819}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001820
David S. Miller214e0052011-02-24 00:02:38 -05001821static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001822{
1823 struct xfrm_aevent_id *id;
1824 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001825 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001826
Eric W. Biederman15e47302012-09-07 20:12:54 +00001827 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001828 if (nlh == NULL)
1829 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001830
Thomas Graf7b67c852007-08-22 13:53:52 -07001831 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001832 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001833 id->sa_id.spi = x->id.spi;
1834 id->sa_id.family = x->props.family;
1835 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001836 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001837 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001838 id->flags = c->data.aevent;
1839
David S. Millerd0fde792012-03-29 04:02:26 -04001840 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001841 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1842 xfrm_replay_state_esn_len(x->replay_esn),
1843 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001844 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001845 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1846 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001847 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001848 if (err)
1849 goto out_cancel;
1850 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1851 if (err)
1852 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001853
David S. Miller1d1e34d2012-06-27 21:57:03 -07001854 if (id->flags & XFRM_AE_RTHR) {
1855 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1856 if (err)
1857 goto out_cancel;
1858 }
1859 if (id->flags & XFRM_AE_ETHR) {
1860 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1861 x->replay_maxage * 10 / HZ);
1862 if (err)
1863 goto out_cancel;
1864 }
1865 err = xfrm_mark_put(skb, &x->mark);
1866 if (err)
1867 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001868
Johannes Berg053c0952015-01-16 22:09:00 +01001869 nlmsg_end(skb, nlh);
1870 return 0;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001871
David S. Miller1d1e34d2012-06-27 21:57:03 -07001872out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001873 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001874 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001875}
1876
Christoph Hellwig22e70052007-01-02 15:22:30 -08001877static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001878 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001879{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001880 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001881 struct xfrm_state *x;
1882 struct sk_buff *r_skb;
1883 int err;
1884 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001885 u32 mark;
1886 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001887 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001888 struct xfrm_usersa_id *id = &p->sa_id;
1889
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001890 mark = xfrm_mark_get(attrs, &m);
1891
1892 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001893 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001894 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001895
1896 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1897 if (r_skb == NULL) {
1898 xfrm_state_put(x);
1899 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001900 }
1901
1902 /*
1903 * XXX: is this lock really needed - none of the other
1904 * gets lock (the concern is things getting updated
1905 * while we are still reading) - jhs
1906 */
1907 spin_lock_bh(&x->lock);
1908 c.data.aevent = p->flags;
1909 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001910 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001911
1912 if (build_aevent(r_skb, x, &c) < 0)
1913 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001914 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001915 spin_unlock_bh(&x->lock);
1916 xfrm_state_put(x);
1917 return err;
1918}
1919
Christoph Hellwig22e70052007-01-02 15:22:30 -08001920static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001921 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001922{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001923 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001924 struct xfrm_state *x;
1925 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001926 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001927 u32 mark = 0;
1928 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001929 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001930 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001931 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001932 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001933
Steffen Klassertd8647b72011-03-08 00:10:27 +00001934 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001935 return err;
1936
1937 /* pedantic mode - thou shalt sayeth replaceth */
1938 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1939 return err;
1940
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001941 mark = xfrm_mark_get(attrs, &m);
1942
1943 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 -08001944 if (x == NULL)
1945 return -ESRCH;
1946
1947 if (x->km.state != XFRM_STATE_VALID)
1948 goto out;
1949
Steffen Klassert4479ff72013-09-09 09:39:01 +02001950 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001951 if (err)
1952 goto out;
1953
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001954 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001955 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001956 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001957
1958 c.event = nlh->nlmsg_type;
1959 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001960 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001961 c.data.aevent = XFRM_AE_CU;
1962 km_state_notify(x, &c);
1963 err = 0;
1964out:
1965 xfrm_state_put(x);
1966 return err;
1967}
1968
Christoph Hellwig22e70052007-01-02 15:22:30 -08001969static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001970 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001972 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001973 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001974 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001975 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001976
Thomas Graf35a7aa02007-08-22 14:00:40 -07001977 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001978 if (err)
1979 return err;
1980
Tetsuo Handa2e710292014-04-22 21:48:30 +09001981 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001982 if (err) {
1983 if (err == -ESRCH) /* empty table */
1984 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001985 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001986 }
1987
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001988 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001989 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001990 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001991 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001992 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001993 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return 0;
1995}
1996
Christoph Hellwig22e70052007-01-02 15:22:30 -08001997static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001998 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001999{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002000 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002001 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07002002 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002003 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08002004 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002005 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002006 struct xfrm_mark m;
2007 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002008
Thomas Graf35a7aa02007-08-22 14:00:40 -07002009 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002010 if (err)
2011 return err;
2012
Timo Teräsc8bf4d02010-03-31 00:17:04 +00002013 err = verify_policy_dir(p->dir);
2014 if (err)
2015 return err;
2016
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002017 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002018 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002019 else {
Thomas Graf5424f322007-08-22 14:01:33 -07002020 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07002021 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002022
Thomas Graf35a7aa02007-08-22 14:00:40 -07002023 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002024 if (err)
2025 return err;
2026
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002027 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002028 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07002029 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002030
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002031 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07002032 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002033 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002034 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002035 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002036 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07002037 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002038 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002039 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08002040 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07002041
Timo Teräsea2dea92010-03-31 00:17:05 +00002042 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002043 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002044
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002045 err = 0;
2046 if (up->hard) {
2047 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002048 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002049 } else {
2050 // reset the timers here?
Jakub Wilk0c199a92015-07-18 14:41:51 +02002051 WARN(1, "Don't know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002052 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002053 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002054
2055out:
2056 xfrm_pol_put(xp);
2057 return err;
2058}
2059
Christoph Hellwig22e70052007-01-02 15:22:30 -08002060static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002061 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002062{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002063 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002064 struct xfrm_state *x;
2065 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002066 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002067 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002068 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002069 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002070
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002071 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 -08002072
David S. Miller3a765aa2007-02-26 14:52:21 -08002073 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002074 if (x == NULL)
2075 return err;
2076
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002077 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002078 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002079 if (x->km.state != XFRM_STATE_VALID)
2080 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002081 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002082
Joy Latten161a09e2006-11-27 13:11:54 -06002083 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002084 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002085 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002086 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002087 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002088out:
2089 spin_unlock_bh(&x->lock);
2090 xfrm_state_put(x);
2091 return err;
2092}
2093
Christoph Hellwig22e70052007-01-02 15:22:30 -08002094static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002095 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002096{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002097 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002098 struct xfrm_policy *xp;
2099 struct xfrm_user_tmpl *ut;
2100 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002101 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002102 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002103
Thomas Graf7b67c852007-08-22 13:53:52 -07002104 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002105 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002106 int err = -ENOMEM;
2107
2108 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002109 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002110
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002111 xfrm_mark_get(attrs, &mark);
2112
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002113 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002114 if (err)
2115 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002116
2117 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002118 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002119 if (!xp)
2120 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002121
2122 memcpy(&x->id, &ua->id, sizeof(ua->id));
2123 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2124 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002125 xp->mark.m = x->mark.m = mark.m;
2126 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002127 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002128 /* extract the templates and for each call km_key */
2129 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2130 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2131 memcpy(&x->id, &t->id, sizeof(x->id));
2132 x->props.mode = t->mode;
2133 x->props.reqid = t->reqid;
2134 x->props.family = ut->family;
2135 t->aalgos = ua->aalgos;
2136 t->ealgos = ua->ealgos;
2137 t->calgos = ua->calgos;
2138 err = km_query(x, t, xp);
2139
2140 }
2141
2142 kfree(x);
2143 kfree(xp);
2144
2145 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002146
2147bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002148 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002149free_state:
2150 kfree(x);
2151nomem:
2152 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002153}
2154
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002155#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002156static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002157 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002158 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002159{
Thomas Graf5424f322007-08-22 14:01:33 -07002160 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002161 struct xfrm_user_migrate *um;
2162 int i, num_migrate;
2163
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002164 if (k != NULL) {
2165 struct xfrm_user_kmaddress *uk;
2166
2167 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2168 memcpy(&k->local, &uk->local, sizeof(k->local));
2169 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2170 k->family = uk->family;
2171 k->reserved = uk->reserved;
2172 }
2173
Thomas Graf5424f322007-08-22 14:01:33 -07002174 um = nla_data(rt);
2175 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002176
2177 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2178 return -EINVAL;
2179
2180 for (i = 0; i < num_migrate; i++, um++, ma++) {
2181 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2182 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2183 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2184 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2185
2186 ma->proto = um->proto;
2187 ma->mode = um->mode;
2188 ma->reqid = um->reqid;
2189
2190 ma->old_family = um->old_family;
2191 ma->new_family = um->new_family;
2192 }
2193
2194 *num = i;
2195 return 0;
2196}
2197
2198static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002199 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002200{
Thomas Graf7b67c852007-08-22 13:53:52 -07002201 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002202 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002203 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002204 u8 type;
2205 int err;
2206 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002207 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002208
Thomas Graf35a7aa02007-08-22 14:00:40 -07002209 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002210 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002211
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002212 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2213
Thomas Graf5424f322007-08-22 14:01:33 -07002214 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002215 if (err)
2216 return err;
2217
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002218 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002219 if (err)
2220 return err;
2221
2222 if (!n)
2223 return 0;
2224
Fan Du8d549c42013-11-07 17:47:49 +08002225 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002226
2227 return 0;
2228}
2229#else
2230static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002231 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002232{
2233 return -ENOPROTOOPT;
2234}
2235#endif
2236
2237#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002238static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002239{
2240 struct xfrm_user_migrate um;
2241
2242 memset(&um, 0, sizeof(um));
2243 um.proto = m->proto;
2244 um.mode = m->mode;
2245 um.reqid = m->reqid;
2246 um.old_family = m->old_family;
2247 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2248 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2249 um.new_family = m->new_family;
2250 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2251 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2252
Thomas Grafc0144be2007-08-22 13:55:43 -07002253 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002254}
2255
David S. Miller183cad12011-02-24 00:28:01 -05002256static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002257{
2258 struct xfrm_user_kmaddress uk;
2259
2260 memset(&uk, 0, sizeof(uk));
2261 uk.family = k->family;
2262 uk.reserved = k->reserved;
2263 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002264 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002265
2266 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2267}
2268
2269static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002270{
2271 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002272 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2273 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2274 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002275}
2276
David S. Miller183cad12011-02-24 00:28:01 -05002277static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2278 int num_migrate, const struct xfrm_kmaddress *k,
2279 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002280{
David S. Miller183cad12011-02-24 00:28:01 -05002281 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002282 struct xfrm_userpolicy_id *pol_id;
2283 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002284 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002285
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002286 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2287 if (nlh == NULL)
2288 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002289
Thomas Graf7b67c852007-08-22 13:53:52 -07002290 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002291 /* copy data from selector, dir, and type to the pol_id */
2292 memset(pol_id, 0, sizeof(*pol_id));
2293 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2294 pol_id->dir = dir;
2295
David S. Miller1d1e34d2012-06-27 21:57:03 -07002296 if (k != NULL) {
2297 err = copy_to_user_kmaddress(k, skb);
2298 if (err)
2299 goto out_cancel;
2300 }
2301 err = copy_to_user_policy_type(type, skb);
2302 if (err)
2303 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002304 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002305 err = copy_to_user_migrate(mp, skb);
2306 if (err)
2307 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002308 }
2309
Johannes Berg053c0952015-01-16 22:09:00 +01002310 nlmsg_end(skb, nlh);
2311 return 0;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002312
2313out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002314 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002315 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002316}
2317
David S. Miller183cad12011-02-24 00:28:01 -05002318static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2319 const struct xfrm_migrate *m, int num_migrate,
2320 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002321{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002322 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002323 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002324
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002325 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002326 if (skb == NULL)
2327 return -ENOMEM;
2328
2329 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002330 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002331 BUG();
2332
Michal Kubecek21ee5432014-06-03 10:26:06 +02002333 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002334}
2335#else
David S. Miller183cad12011-02-24 00:28:01 -05002336static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2337 const struct xfrm_migrate *m, int num_migrate,
2338 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002339{
2340 return -ENOPROTOOPT;
2341}
2342#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002343
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002344#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002345
2346static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002347 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002348 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2349 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2350 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2351 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2352 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2353 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002354 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002355 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002356 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002357 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002358 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002359 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002360 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002361 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2362 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002363 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002364 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002365 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002366 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002367 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368};
2369
Thomas Graf492b5582005-05-03 14:26:40 -07002370#undef XMSGSIZE
2371
Thomas Grafcf5cb792007-08-22 13:59:04 -07002372static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002373 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2374 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2375 [XFRMA_LASTUSED] = { .type = NLA_U64},
2376 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002377 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002378 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2379 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2380 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2381 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2382 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2383 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2384 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2385 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2386 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2387 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2388 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2389 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2390 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2391 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002392 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002393 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002394 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002395 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002396 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002397 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002398 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002399};
2400
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002401static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2402 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2403 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2404};
2405
Mathias Krause05600a72013-02-24 14:10:27 +01002406static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002407 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002409 int (*done)(struct netlink_callback *);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002410 const struct nla_policy *nla_pol;
2411 int nla_max;
Thomas Graf492b5582005-05-03 14:26:40 -07002412} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2413 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2414 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2415 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002416 .dump = xfrm_dump_sa,
2417 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002418 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2419 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2420 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002421 .dump = xfrm_dump_policy,
2422 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002423 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002424 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002425 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002426 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2427 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002428 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002429 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2430 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002431 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2432 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002433 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002434 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002435 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2436 .nla_pol = xfrma_spd_policy,
2437 .nla_max = XFRMA_SPD_MAX },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002438 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439};
2440
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002441static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002443 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002444 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002445 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002446 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Fan Du74005992015-01-27 17:00:29 +08002448#ifdef CONFIG_COMPAT
2449 if (is_compat_task())
2450 return -ENOTSUPP;
2451#endif
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 type -= XFRM_MSG_BASE;
2458 link = &xfrm_dispatch[type];
2459
2460 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002461 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002462 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Thomas Graf492b5582005-05-03 14:26:40 -07002464 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2465 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002466 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002470 {
2471 struct netlink_dump_control c = {
2472 .dump = link->dump,
2473 .done = link->done,
2474 };
2475 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002479 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2480 link->nla_max ? : XFRMA_MAX,
2481 link->nla_pol ? : xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002482 if (err < 0)
2483 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002486 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Thomas Graf5424f322007-08-22 14:01:33 -07002488 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002491static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Fan Du283bc9f2013-11-07 17:47:50 +08002493 struct net *net = sock_net(skb->sk);
2494
2495 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002496 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002497 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
Thomas Graf7deb2262007-08-22 13:57:39 -07002500static inline size_t xfrm_expire_msgsize(void)
2501{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002502 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2503 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002504}
2505
David S. Miller214e0052011-02-24 00:02:38 -05002506static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
2508 struct xfrm_user_expire *ue;
2509 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002510 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Eric W. Biederman15e47302012-09-07 20:12:54 +00002512 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002513 if (nlh == NULL)
2514 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Thomas Graf7b67c852007-08-22 13:53:52 -07002516 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002518 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
David S. Miller1d1e34d2012-06-27 21:57:03 -07002520 err = xfrm_mark_put(skb, &x->mark);
2521 if (err)
2522 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002523
Johannes Berg053c0952015-01-16 22:09:00 +01002524 nlmsg_end(skb, nlh);
2525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
David S. Miller214e0052011-02-24 00:02:38 -05002528static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002530 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 struct sk_buff *skb;
2532
Thomas Graf7deb2262007-08-22 13:57:39 -07002533 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (skb == NULL)
2535 return -ENOMEM;
2536
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002537 if (build_expire(skb, x, c) < 0) {
2538 kfree_skb(skb);
2539 return -EMSGSIZE;
2540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Michal Kubecek21ee5432014-06-03 10:26:06 +02002542 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
2544
David S. Miller214e0052011-02-24 00:02:38 -05002545static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002546{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002547 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002548 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002549
Steffen Klassertd8647b72011-03-08 00:10:27 +00002550 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002551 if (skb == NULL)
2552 return -ENOMEM;
2553
2554 if (build_aevent(skb, x, c) < 0)
2555 BUG();
2556
Michal Kubecek21ee5432014-06-03 10:26:06 +02002557 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002558}
2559
David S. Miller214e0052011-02-24 00:02:38 -05002560static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002561{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002562 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002563 struct xfrm_usersa_flush *p;
2564 struct nlmsghdr *nlh;
2565 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002566 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002567
Thomas Graf7deb2262007-08-22 13:57:39 -07002568 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002569 if (skb == NULL)
2570 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002571
Eric W. Biederman15e47302012-09-07 20:12:54 +00002572 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002573 if (nlh == NULL) {
2574 kfree_skb(skb);
2575 return -EMSGSIZE;
2576 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002577
Thomas Graf7b67c852007-08-22 13:53:52 -07002578 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002579 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002580
Thomas Graf98250692007-08-22 12:47:26 -07002581 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002582
Michal Kubecek21ee5432014-06-03 10:26:06 +02002583 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002584}
2585
Thomas Graf7deb2262007-08-22 13:57:39 -07002586static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002587{
Thomas Graf7deb2262007-08-22 13:57:39 -07002588 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002589 if (x->aead)
2590 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002591 if (x->aalg) {
2592 l += nla_total_size(sizeof(struct xfrm_algo) +
2593 (x->aalg->alg_key_len + 7) / 8);
2594 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2595 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002596 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002597 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002598 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002599 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002600 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002601 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002602 if (x->tfcpad)
2603 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002604 if (x->replay_esn)
2605 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
dingzhif293a5e2014-10-30 09:39:36 +01002606 else
2607 l += nla_total_size(sizeof(struct xfrm_replay_state));
Herbert Xu68325d32007-10-09 13:30:57 -07002608 if (x->security)
2609 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2610 x->security->ctx_len);
2611 if (x->coaddr)
2612 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002613 if (x->props.extra_flags)
2614 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002615
Herbert Xud26f3982007-11-13 21:47:08 -08002616 /* Must count x->lastused as it may become non-zero behind our back. */
2617 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002618
2619 return l;
2620}
2621
David S. Miller214e0052011-02-24 00:02:38 -05002622static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002623{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002624 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002625 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002626 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002627 struct nlmsghdr *nlh;
2628 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002629 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002630 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002631
2632 headlen = sizeof(*p);
2633 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002634 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002635 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002636 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002637 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002638 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002639
Thomas Graf7deb2262007-08-22 13:57:39 -07002640 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002641 if (skb == NULL)
2642 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002643
Eric W. Biederman15e47302012-09-07 20:12:54 +00002644 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002645 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002646 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002647 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002648
Thomas Graf7b67c852007-08-22 13:53:52 -07002649 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002650 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002651 struct nlattr *attr;
2652
Thomas Graf7b67c852007-08-22 13:53:52 -07002653 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002654 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2655 id->spi = x->id.spi;
2656 id->family = x->props.family;
2657 id->proto = x->id.proto;
2658
Thomas Grafc0144be2007-08-22 13:55:43 -07002659 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002660 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002661 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002662 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002663
2664 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002665 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002666 err = copy_to_user_state_extra(x, p, skb);
2667 if (err)
2668 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002669
Thomas Graf98250692007-08-22 12:47:26 -07002670 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002671
Michal Kubecek21ee5432014-06-03 10:26:06 +02002672 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002673
David S. Miller1d1e34d2012-06-27 21:57:03 -07002674out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002675 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002676 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002677}
2678
David S. Miller214e0052011-02-24 00:02:38 -05002679static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002680{
2681
2682 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002683 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002684 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002685 case XFRM_MSG_NEWAE:
2686 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002687 case XFRM_MSG_DELSA:
2688 case XFRM_MSG_UPDSA:
2689 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002690 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002691 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002692 return xfrm_notify_sa_flush(c);
2693 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002694 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2695 c->event);
2696 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002697 }
2698
2699 return 0;
2700
2701}
2702
Thomas Graf7deb2262007-08-22 13:57:39 -07002703static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2704 struct xfrm_policy *xp)
2705{
2706 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2707 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002708 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002709 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2710 + userpolicy_type_attrsize();
2711}
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002714 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002716 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 struct xfrm_user_acquire *ua;
2718 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002719 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002721 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2722 if (nlh == NULL)
2723 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Thomas Graf7b67c852007-08-22 13:53:52 -07002725 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 memcpy(&ua->id, &x->id, sizeof(ua->id));
2727 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2728 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002729 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 ua->aalgos = xt->aalgos;
2731 ua->ealgos = xt->ealgos;
2732 ua->calgos = xt->calgos;
2733 ua->seq = x->km.seq = seq;
2734
David S. Miller1d1e34d2012-06-27 21:57:03 -07002735 err = copy_to_user_tmpl(xp, skb);
2736 if (!err)
2737 err = copy_to_user_state_sec_ctx(x, skb);
2738 if (!err)
2739 err = copy_to_user_policy_type(xp->type, skb);
2740 if (!err)
2741 err = xfrm_mark_put(skb, &xp->mark);
2742 if (err) {
2743 nlmsg_cancel(skb, nlh);
2744 return err;
2745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Johannes Berg053c0952015-01-16 22:09:00 +01002747 nlmsg_end(skb, nlh);
2748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
2750
2751static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002752 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002754 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Thomas Graf7deb2262007-08-22 13:57:39 -07002757 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 if (skb == NULL)
2759 return -ENOMEM;
2760
Fan Du65e07362012-08-15 10:13:47 +08002761 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 BUG();
2763
Michal Kubecek21ee5432014-06-03 10:26:06 +02002764 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
2766
2767/* User gives us xfrm_user_policy_info followed by an array of 0
2768 * or more templates.
2769 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002770static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 u8 *data, int len, int *dir)
2772{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002773 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2775 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2776 struct xfrm_policy *xp;
2777 int nr;
2778
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002779 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 case AF_INET:
2781 if (opt != IP_XFRM_POLICY) {
2782 *dir = -EOPNOTSUPP;
2783 return NULL;
2784 }
2785 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002786#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 case AF_INET6:
2788 if (opt != IPV6_XFRM_POLICY) {
2789 *dir = -EOPNOTSUPP;
2790 return NULL;
2791 }
2792 break;
2793#endif
2794 default:
2795 *dir = -EINVAL;
2796 return NULL;
2797 }
2798
2799 *dir = -EINVAL;
2800
2801 if (len < sizeof(*p) ||
2802 verify_newpolicy_info(p))
2803 return NULL;
2804
2805 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002806 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return NULL;
2808
Herbert Xua4f1bac2005-07-26 15:43:17 -07002809 if (p->dir > XFRM_POLICY_OUT)
2810 return NULL;
2811
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002812 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (xp == NULL) {
2814 *dir = -ENOBUFS;
2815 return NULL;
2816 }
2817
2818 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002819 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 copy_templates(xp, ut, nr);
2821
2822 *dir = p->dir;
2823
2824 return xp;
2825}
2826
Thomas Graf7deb2262007-08-22 13:57:39 -07002827static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2828{
2829 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2830 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2831 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002832 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002833 + userpolicy_type_attrsize();
2834}
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002837 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
2839 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002840 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002841 struct nlmsghdr *nlh;
2842 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Eric W. Biederman15e47302012-09-07 20:12:54 +00002844 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002845 if (nlh == NULL)
2846 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Thomas Graf7b67c852007-08-22 13:53:52 -07002848 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002850 err = copy_to_user_tmpl(xp, skb);
2851 if (!err)
2852 err = copy_to_user_sec_ctx(xp, skb);
2853 if (!err)
2854 err = copy_to_user_policy_type(xp->type, skb);
2855 if (!err)
2856 err = xfrm_mark_put(skb, &xp->mark);
2857 if (err) {
2858 nlmsg_cancel(skb, nlh);
2859 return err;
2860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 upe->hard = !!hard;
2862
Johannes Berg053c0952015-01-16 22:09:00 +01002863 nlmsg_end(skb, nlh);
2864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
2866
David S. Miller214e0052011-02-24 00:02:38 -05002867static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002869 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Thomas Graf7deb2262007-08-22 13:57:39 -07002872 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (skb == NULL)
2874 return -ENOMEM;
2875
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002876 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 BUG();
2878
Michal Kubecek21ee5432014-06-03 10:26:06 +02002879 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880}
2881
David S. Miller214e0052011-02-24 00:02:38 -05002882static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002883{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002884 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002885 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002886 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002887 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002888 struct nlmsghdr *nlh;
2889 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002890 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002891
2892 headlen = sizeof(*p);
2893 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002894 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002895 headlen = sizeof(*id);
2896 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002897 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002898 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002899 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002900
Thomas Graf7deb2262007-08-22 13:57:39 -07002901 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002902 if (skb == NULL)
2903 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002904
Eric W. Biederman15e47302012-09-07 20:12:54 +00002905 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002906 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002907 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002908 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002909
Thomas Graf7b67c852007-08-22 13:53:52 -07002910 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002911 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002912 struct nlattr *attr;
2913
Thomas Graf7b67c852007-08-22 13:53:52 -07002914 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002915 memset(id, 0, sizeof(*id));
2916 id->dir = dir;
2917 if (c->data.byid)
2918 id->index = xp->index;
2919 else
2920 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2921
Thomas Grafc0144be2007-08-22 13:55:43 -07002922 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002923 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002924 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002925 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002926
2927 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002928 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002929
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002930 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002931 err = copy_to_user_tmpl(xp, skb);
2932 if (!err)
2933 err = copy_to_user_policy_type(xp->type, skb);
2934 if (!err)
2935 err = xfrm_mark_put(skb, &xp->mark);
2936 if (err)
2937 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002938
Thomas Graf98250692007-08-22 12:47:26 -07002939 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002940
Michal Kubecek21ee5432014-06-03 10:26:06 +02002941 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002942
David S. Miller1d1e34d2012-06-27 21:57:03 -07002943out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002944 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002945 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002946}
2947
David S. Miller214e0052011-02-24 00:02:38 -05002948static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002949{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002950 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002951 struct nlmsghdr *nlh;
2952 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002953 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002954
Thomas Graf7deb2262007-08-22 13:57:39 -07002955 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002956 if (skb == NULL)
2957 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002958
Eric W. Biederman15e47302012-09-07 20:12:54 +00002959 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002960 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002961 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002962 goto out_free_skb;
2963 err = copy_to_user_policy_type(c->data.type, skb);
2964 if (err)
2965 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002966
Thomas Graf98250692007-08-22 12:47:26 -07002967 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002968
Michal Kubecek21ee5432014-06-03 10:26:06 +02002969 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002970
David S. Miller1d1e34d2012-06-27 21:57:03 -07002971out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002972 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002973 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002974}
2975
David S. Miller214e0052011-02-24 00:02:38 -05002976static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002977{
2978
2979 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002980 case XFRM_MSG_NEWPOLICY:
2981 case XFRM_MSG_UPDPOLICY:
2982 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002983 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002984 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002985 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002986 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002987 return xfrm_exp_policy_notify(xp, dir, c);
2988 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002989 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2990 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002991 }
2992
2993 return 0;
2994
2995}
2996
Thomas Graf7deb2262007-08-22 13:57:39 -07002997static inline size_t xfrm_report_msgsize(void)
2998{
2999 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3000}
3001
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003002static int build_report(struct sk_buff *skb, u8 proto,
3003 struct xfrm_selector *sel, xfrm_address_t *addr)
3004{
3005 struct xfrm_user_report *ur;
3006 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003007
Thomas Graf79b8b7f2007-08-22 12:46:53 -07003008 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3009 if (nlh == NULL)
3010 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003011
Thomas Graf7b67c852007-08-22 13:53:52 -07003012 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003013 ur->proto = proto;
3014 memcpy(&ur->sel, sel, sizeof(ur->sel));
3015
David S. Miller1d1e34d2012-06-27 21:57:03 -07003016 if (addr) {
3017 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3018 if (err) {
3019 nlmsg_cancel(skb, nlh);
3020 return err;
3021 }
3022 }
Johannes Berg053c0952015-01-16 22:09:00 +01003023 nlmsg_end(skb, nlh);
3024 return 0;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003025}
3026
Alexey Dobriyandb983c12008-11-25 17:51:01 -08003027static int xfrm_send_report(struct net *net, u8 proto,
3028 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003029{
3030 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003031
Thomas Graf7deb2262007-08-22 13:57:39 -07003032 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003033 if (skb == NULL)
3034 return -ENOMEM;
3035
3036 if (build_report(skb, proto, sel, addr) < 0)
3037 BUG();
3038
Michal Kubecek21ee5432014-06-03 10:26:06 +02003039 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003040}
3041
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003042static inline size_t xfrm_mapping_msgsize(void)
3043{
3044 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3045}
3046
3047static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3048 xfrm_address_t *new_saddr, __be16 new_sport)
3049{
3050 struct xfrm_user_mapping *um;
3051 struct nlmsghdr *nlh;
3052
3053 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3054 if (nlh == NULL)
3055 return -EMSGSIZE;
3056
3057 um = nlmsg_data(nlh);
3058
3059 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3060 um->id.spi = x->id.spi;
3061 um->id.family = x->props.family;
3062 um->id.proto = x->id.proto;
3063 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3064 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3065 um->new_sport = new_sport;
3066 um->old_sport = x->encap->encap_sport;
3067 um->reqid = x->props.reqid;
3068
Johannes Berg053c0952015-01-16 22:09:00 +01003069 nlmsg_end(skb, nlh);
3070 return 0;
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003071}
3072
3073static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3074 __be16 sport)
3075{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003076 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003077 struct sk_buff *skb;
3078
3079 if (x->id.proto != IPPROTO_ESP)
3080 return -EINVAL;
3081
3082 if (!x->encap)
3083 return -EINVAL;
3084
3085 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3086 if (skb == NULL)
3087 return -ENOMEM;
3088
3089 if (build_mapping(skb, x, ipaddr, sport) < 0)
3090 BUG();
3091
Michal Kubecek21ee5432014-06-03 10:26:06 +02003092 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003093}
3094
Horia Geanta0f245582014-02-12 16:20:06 +02003095static bool xfrm_is_alive(const struct km_event *c)
3096{
3097 return (bool)xfrm_acquire_is_on(c->net);
3098}
3099
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100static struct xfrm_mgr netlink_mgr = {
3101 .id = "netlink",
3102 .notify = xfrm_send_state_notify,
3103 .acquire = xfrm_send_acquire,
3104 .compile_policy = xfrm_compile_policy,
3105 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003106 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003107 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003108 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003109 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110};
3111
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003112static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
Patrick McHardybe336902006-03-20 22:40:54 -08003114 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003115 struct netlink_kernel_cfg cfg = {
3116 .groups = XFRMNLGRP_MAX,
3117 .input = xfrm_netlink_rcv,
3118 };
Patrick McHardybe336902006-03-20 22:40:54 -08003119
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003120 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003121 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003123 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003124 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return 0;
3126}
3127
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003128static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003129{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003130 struct net *net;
3131 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003132 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003133 synchronize_net();
3134 list_for_each_entry(net, net_exit_list, exit_list)
3135 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003136}
3137
3138static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003139 .init = xfrm_user_net_init,
3140 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003141};
3142
3143static int __init xfrm_user_init(void)
3144{
3145 int rv;
3146
3147 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3148
3149 rv = register_pernet_subsys(&xfrm_user_net_ops);
3150 if (rv < 0)
3151 return rv;
3152 rv = xfrm_register_km(&netlink_mgr);
3153 if (rv < 0)
3154 unregister_pernet_subsys(&xfrm_user_net_ops);
3155 return rv;
3156}
3157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158static void __exit xfrm_user_exit(void)
3159{
3160 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003161 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
3163
3164module_init(xfrm_user_init);
3165module_exit(xfrm_user_exit);
3166MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003167MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003168