blob: 8128594ab3797e4814e77ef80246456641458027 [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
Martin Willi4447bb32009-11-25 00:29:52 +0000292static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
293 struct nlattr *rta)
294{
295 struct xfrm_algo *ualg;
296 struct xfrm_algo_auth *p;
297 struct xfrm_algo_desc *algo;
298
299 if (!rta)
300 return 0;
301
302 ualg = nla_data(rta);
303
304 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
305 if (!algo)
306 return -ENOSYS;
307 *props = algo->desc.sadb_alg_id;
308
309 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
310 if (!p)
311 return -ENOMEM;
312
313 strcpy(p->alg_name, algo->name);
314 p->alg_key_len = ualg->alg_key_len;
315 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
316 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
317
318 *algpp = p;
319 return 0;
320}
321
322static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
323 struct nlattr *rta)
324{
325 struct xfrm_algo_auth *p, *ualg;
326 struct xfrm_algo_desc *algo;
327
328 if (!rta)
329 return 0;
330
331 ualg = nla_data(rta);
332
333 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
334 if (!algo)
335 return -ENOSYS;
Herbert Xu689f1c92014-09-18 16:38:18 +0800336 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000337 return -EINVAL;
338 *props = algo->desc.sadb_alg_id;
339
340 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
341 if (!p)
342 return -ENOMEM;
343
344 strcpy(p->alg_name, algo->name);
345 if (!p->alg_trunc_len)
346 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
347
348 *algpp = p;
349 return 0;
350}
351
Herbert Xu1a6509d2008-01-28 19:37:29 -0800352static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
353 struct nlattr *rta)
354{
355 struct xfrm_algo_aead *p, *ualg;
356 struct xfrm_algo_desc *algo;
357
358 if (!rta)
359 return 0;
360
361 ualg = nla_data(rta);
362
363 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
364 if (!algo)
365 return -ENOSYS;
366 *props = algo->desc.sadb_alg_id;
367
368 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
369 if (!p)
370 return -ENOMEM;
371
372 strcpy(p->alg_name, algo->name);
373 *algpp = p;
374 return 0;
375}
376
Steffen Klasserte2b19122011-03-28 19:47:30 +0000377static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
378 struct nlattr *rp)
379{
380 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000381 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000382
383 if (!replay_esn || !rp)
384 return 0;
385
386 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000387 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000388
Mathias Krauseecd79182012-09-20 10:01:49 +0000389 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000390 return -EINVAL;
391
392 return 0;
393}
394
Steffen Klassertd8647b72011-03-08 00:10:27 +0000395static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
396 struct xfrm_replay_state_esn **preplay_esn,
397 struct nlattr *rta)
398{
399 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000400 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000401
402 if (!rta)
403 return 0;
404
405 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000406 klen = xfrm_replay_state_esn_len(up);
407 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000408
Mathias Krauseecd79182012-09-20 10:01:49 +0000409 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000410 if (!p)
411 return -ENOMEM;
412
Mathias Krauseecd79182012-09-20 10:01:49 +0000413 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000414 if (!pp) {
415 kfree(p);
416 return -ENOMEM;
417 }
418
Mathias Krauseecd79182012-09-20 10:01:49 +0000419 memcpy(p, up, ulen);
420 memcpy(pp, up, ulen);
421
Steffen Klassertd8647b72011-03-08 00:10:27 +0000422 *replay_esn = p;
423 *preplay_esn = pp;
424
425 return 0;
426}
427
Joy Latten661697f2007-04-13 16:14:35 -0700428static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800429{
Trent Jaegerdf718372005-12-13 23:12:27 -0800430 int len = 0;
431
432 if (xfrm_ctx) {
433 len += sizeof(struct xfrm_user_sec_ctx);
434 len += xfrm_ctx->ctx_len;
435 }
436 return len;
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
440{
441 memcpy(&x->id, &p->id, sizeof(x->id));
442 memcpy(&x->sel, &p->sel, sizeof(x->sel));
443 memcpy(&x->lft, &p->lft, sizeof(x->lft));
444 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800445 x->props.replay_window = min_t(unsigned int, p->replay_window,
446 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 x->props.reqid = p->reqid;
448 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700449 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700451
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700452 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700453 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800456/*
457 * someday when pfkey also has support, we could have the code
458 * somehow made shareable and move it to xfrm_state.c - JHS
459 *
460*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000461static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
462 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800463{
Thomas Graf5424f322007-08-22 14:01:33 -0700464 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000465 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700466 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
467 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
468 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800469
Steffen Klassertd8647b72011-03-08 00:10:27 +0000470 if (re) {
471 struct xfrm_replay_state_esn *replay_esn;
472 replay_esn = nla_data(re);
473 memcpy(x->replay_esn, replay_esn,
474 xfrm_replay_state_esn_len(replay_esn));
475 memcpy(x->preplay_esn, replay_esn,
476 xfrm_replay_state_esn_len(replay_esn));
477 }
478
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800479 if (rp) {
480 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700481 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800482 memcpy(&x->replay, replay, sizeof(*replay));
483 memcpy(&x->preplay, replay, sizeof(*replay));
484 }
485
486 if (lt) {
487 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700488 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800489 x->curlft.bytes = ltime->bytes;
490 x->curlft.packets = ltime->packets;
491 x->curlft.add_time = ltime->add_time;
492 x->curlft.use_time = ltime->use_time;
493 }
494
Thomas Grafcf5cb792007-08-22 13:59:04 -0700495 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700496 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800497
Thomas Grafcf5cb792007-08-22 13:59:04 -0700498 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700499 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800500}
501
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800502static struct xfrm_state *xfrm_state_construct(struct net *net,
503 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700504 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 int *errp)
506{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800507 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 int err = -ENOMEM;
509
510 if (!x)
511 goto error_no_put;
512
513 copy_from_user_state(x, p);
514
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100515 if (attrs[XFRMA_SA_EXTRA_FLAGS])
516 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
517
Herbert Xu1a6509d2008-01-28 19:37:29 -0800518 if ((err = attach_aead(&x->aead, &x->props.ealgo,
519 attrs[XFRMA_ALG_AEAD])))
520 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000521 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
522 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000524 if (!x->props.aalgo) {
525 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
526 attrs[XFRMA_ALG_AUTH])))
527 goto error;
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
530 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700531 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto error;
533 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
534 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700535 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700537
538 if (attrs[XFRMA_ENCAP]) {
539 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
540 sizeof(*x->encap), GFP_KERNEL);
541 if (x->encap == NULL)
542 goto error;
543 }
544
Martin Willi35d28562010-12-08 04:37:49 +0000545 if (attrs[XFRMA_TFCPAD])
546 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
547
Thomas Graffd211502007-09-06 03:28:08 -0700548 if (attrs[XFRMA_COADDR]) {
549 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
550 sizeof(*x->coaddr), GFP_KERNEL);
551 if (x->coaddr == NULL)
552 goto error;
553 }
554
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000555 xfrm_mark_get(attrs, &x->mark);
556
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700557 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (err)
559 goto error;
560
Thomas Graffd211502007-09-06 03:28:08 -0700561 if (attrs[XFRMA_SEC_CTX] &&
562 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800563 goto error;
564
Steffen Klassertd8647b72011-03-08 00:10:27 +0000565 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
566 attrs[XFRMA_REPLAY_ESN_VAL])))
567 goto error;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800570 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800571 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800572 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800573
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000574 if ((err = xfrm_init_replay(x)))
575 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800576
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000577 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000578 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 return x;
581
582error:
583 x->km.state = XFRM_STATE_DEAD;
584 xfrm_state_put(x);
585error_no_put:
586 *errp = err;
587 return NULL;
588}
589
Christoph Hellwig22e70052007-01-02 15:22:30 -0800590static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700591 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800593 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700594 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 struct xfrm_state *x;
596 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700597 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Thomas Graf35a7aa02007-08-22 14:00:40 -0700599 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (err)
601 return err;
602
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800603 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!x)
605 return err;
606
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700607 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
609 err = xfrm_state_add(x);
610 else
611 err = xfrm_state_update(x);
612
Tetsuo Handa2e710292014-04-22 21:48:30 +0900613 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (err < 0) {
616 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800617 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700618 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700621 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000622 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700623 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700624
625 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700626out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700627 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return err;
629}
630
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800631static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
632 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700633 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700634 int *errp)
635{
636 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000637 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700638 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000639 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700640
641 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
642 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000643 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700644 } else {
645 xfrm_address_t *saddr = NULL;
646
Thomas Graf35a7aa02007-08-22 14:00:40 -0700647 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700648 if (!saddr) {
649 err = -EINVAL;
650 goto out;
651 }
652
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800653 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000654 x = xfrm_state_lookup_byaddr(net, mark,
655 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800656 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700657 }
658
659 out:
660 if (!x && errp)
661 *errp = err;
662 return x;
663}
664
Christoph Hellwig22e70052007-01-02 15:22:30 -0800665static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700666 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800668 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700670 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700671 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700672 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800674 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700676 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
David S. Miller6f68dc32006-06-08 23:58:52 -0700678 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700679 goto out;
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700682 err = -EPERM;
683 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700686 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600687
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700688 if (err < 0)
689 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700690
691 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000692 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700693 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700694 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700696out:
Tetsuo Handa2e710292014-04-22 21:48:30 +0900697 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700698 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700699 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
703{
Mathias Krausef778a632012-09-19 11:33:39 +0000704 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 memcpy(&p->id, &x->id, sizeof(p->id));
706 memcpy(&p->sel, &x->sel, sizeof(p->sel));
707 memcpy(&p->lft, &x->lft, sizeof(p->lft));
708 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
709 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700710 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 p->mode = x->props.mode;
712 p->replay_window = x->props.replay_window;
713 p->reqid = x->props.reqid;
714 p->family = x->props.family;
715 p->flags = x->props.flags;
716 p->seq = x->km.seq;
717}
718
719struct xfrm_dump_info {
720 struct sk_buff *in_skb;
721 struct sk_buff *out_skb;
722 u32 nlmsg_seq;
723 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724};
725
Thomas Grafc0144be2007-08-22 13:55:43 -0700726static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
727{
Thomas Grafc0144be2007-08-22 13:55:43 -0700728 struct xfrm_user_sec_ctx *uctx;
729 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700730 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700731
732 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
733 if (attr == NULL)
734 return -EMSGSIZE;
735
736 uctx = nla_data(attr);
737 uctx->exttype = XFRMA_SEC_CTX;
738 uctx->len = ctx_size;
739 uctx->ctx_doi = s->ctx_doi;
740 uctx->ctx_alg = s->ctx_alg;
741 uctx->ctx_len = s->ctx_len;
742 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
743
744 return 0;
745}
746
Martin Willi4447bb32009-11-25 00:29:52 +0000747static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
748{
749 struct xfrm_algo *algo;
750 struct nlattr *nla;
751
752 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
753 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
754 if (!nla)
755 return -EMSGSIZE;
756
757 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000758 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000759 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
760 algo->alg_key_len = auth->alg_key_len;
761
762 return 0;
763}
764
Herbert Xu68325d32007-10-09 13:30:57 -0700765/* Don't change this without updating xfrm_sa_len! */
766static int copy_to_user_state_extra(struct xfrm_state *x,
767 struct xfrm_usersa_info *p,
768 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700770 int ret = 0;
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 copy_to_user_state(x, p);
773
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100774 if (x->props.extra_flags) {
775 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
776 x->props.extra_flags);
777 if (ret)
778 goto out;
779 }
780
David S. Miller1d1e34d2012-06-27 21:57:03 -0700781 if (x->coaddr) {
782 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
783 if (ret)
784 goto out;
785 }
786 if (x->lastused) {
787 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
788 if (ret)
789 goto out;
790 }
791 if (x->aead) {
792 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
793 if (ret)
794 goto out;
795 }
796 if (x->aalg) {
797 ret = copy_to_user_auth(x->aalg, skb);
798 if (!ret)
799 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
800 xfrm_alg_auth_len(x->aalg), x->aalg);
801 if (ret)
802 goto out;
803 }
804 if (x->ealg) {
805 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
806 if (ret)
807 goto out;
808 }
809 if (x->calg) {
810 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
811 if (ret)
812 goto out;
813 }
814 if (x->encap) {
815 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
816 if (ret)
817 goto out;
818 }
819 if (x->tfcpad) {
820 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
821 if (ret)
822 goto out;
823 }
824 ret = xfrm_mark_put(skb, &x->mark);
825 if (ret)
826 goto out;
dingzhif293a5e2014-10-30 09:39:36 +0100827 if (x->replay_esn)
David S. Miller1d1e34d2012-06-27 21:57:03 -0700828 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
829 xfrm_replay_state_esn_len(x->replay_esn),
830 x->replay_esn);
dingzhif293a5e2014-10-30 09:39:36 +0100831 else
832 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
833 &x->replay);
834 if (ret)
835 goto out;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700836 if (x->security)
837 ret = copy_sec_ctx(x->security, skb);
838out:
839 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700840}
841
842static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
843{
844 struct xfrm_dump_info *sp = ptr;
845 struct sk_buff *in_skb = sp->in_skb;
846 struct sk_buff *skb = sp->out_skb;
847 struct xfrm_usersa_info *p;
848 struct nlmsghdr *nlh;
849 int err;
850
Eric W. Biederman15e47302012-09-07 20:12:54 +0000851 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700852 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
853 if (nlh == NULL)
854 return -EMSGSIZE;
855
856 p = nlmsg_data(nlh);
857
858 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700859 if (err) {
860 nlmsg_cancel(skb, nlh);
861 return err;
862 }
Thomas Graf98250692007-08-22 12:47:26 -0700863 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Timo Teras4c563f72008-02-28 21:31:08 -0800867static int xfrm_dump_sa_done(struct netlink_callback *cb)
868{
869 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800870 struct sock *sk = cb->skb->sk;
871 struct net *net = sock_net(sk);
872
873 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800874 return 0;
875}
876
Nicolas Dichteld3623092014-02-14 15:30:36 +0100877static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
879{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800880 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800881 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct xfrm_dump_info info;
883
Timo Teras4c563f72008-02-28 21:31:08 -0800884 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
885 sizeof(cb->args) - sizeof(cb->args[0]));
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 info.in_skb = cb->skb;
888 info.out_skb = skb;
889 info.nlmsg_seq = cb->nlh->nlmsg_seq;
890 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800891
892 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100893 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100894 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100895 u8 proto = 0;
896 int err;
897
Timo Teras4c563f72008-02-28 21:31:08 -0800898 cb->args[0] = 1;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100899
900 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
901 xfrma_policy);
902 if (err < 0)
903 return err;
904
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100905 if (attrs[XFRMA_ADDRESS_FILTER]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100906 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
907 if (filter == NULL)
908 return -ENOMEM;
909
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100910 memcpy(filter, nla_data(attrs[XFRMA_ADDRESS_FILTER]),
Nicolas Dichteld3623092014-02-14 15:30:36 +0100911 sizeof(*filter));
912 }
913
914 if (attrs[XFRMA_PROTO])
915 proto = nla_get_u8(attrs[XFRMA_PROTO]);
916
917 xfrm_state_walk_init(walk, proto, filter);
Timo Teras4c563f72008-02-28 21:31:08 -0800918 }
919
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800920 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 return skb->len;
923}
924
925static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
926 struct xfrm_state *x, u32 seq)
927{
928 struct xfrm_dump_info info;
929 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000930 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Thomas Graf7deb2262007-08-22 13:57:39 -0700932 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (!skb)
934 return ERR_PTR(-ENOMEM);
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 info.in_skb = in_skb;
937 info.out_skb = skb;
938 info.nlmsg_seq = seq;
939 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Mathias Krause864745d2012-09-13 11:41:26 +0000941 err = dump_one_state(x, 0, &info);
942 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000944 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946
947 return skb;
948}
949
Michal Kubecek21ee5432014-06-03 10:26:06 +0200950/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
951 * Must be called with RCU read lock.
952 */
953static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
954 u32 pid, unsigned int group)
955{
956 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
957
958 if (nlsk)
959 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
960 else
961 return -1;
962}
963
Thomas Graf7deb2262007-08-22 13:57:39 -0700964static inline size_t xfrm_spdinfo_msgsize(void)
965{
966 return NLMSG_ALIGN(4)
967 + nla_total_size(sizeof(struct xfrmu_spdinfo))
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200968 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
969 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
970 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
Thomas Graf7deb2262007-08-22 13:57:39 -0700971}
972
Alexey Dobriyane0710412010-01-23 13:37:10 +0000973static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000974 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700975{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700976 struct xfrmk_spdinfo si;
977 struct xfrmu_spdinfo spc;
978 struct xfrmu_spdhinfo sph;
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200979 struct xfrmu_spdhthresh spt4, spt6;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700980 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700981 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700982 u32 *f;
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200983 unsigned lseq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700984
Eric W. Biederman15e47302012-09-07 20:12:54 +0000985 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300986 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700987 return -EMSGSIZE;
988
989 f = nlmsg_data(nlh);
990 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000991 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700992 spc.incnt = si.incnt;
993 spc.outcnt = si.outcnt;
994 spc.fwdcnt = si.fwdcnt;
995 spc.inscnt = si.inscnt;
996 spc.outscnt = si.outscnt;
997 spc.fwdscnt = si.fwdscnt;
998 sph.spdhcnt = si.spdhcnt;
999 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001000
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001001 do {
1002 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1003
1004 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1005 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1006 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1007 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1008 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1009
David S. Miller1d1e34d2012-06-27 21:57:03 -07001010 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1011 if (!err)
1012 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001013 if (!err)
1014 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1015 if (!err)
1016 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001017 if (err) {
1018 nlmsg_cancel(skb, nlh);
1019 return err;
1020 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001021
1022 return nlmsg_end(skb, nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001023}
1024
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001025static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1026 struct nlattr **attrs)
1027{
1028 struct net *net = sock_net(skb->sk);
1029 struct xfrmu_spdhthresh *thresh4 = NULL;
1030 struct xfrmu_spdhthresh *thresh6 = NULL;
1031
1032 /* selector prefixlen thresholds to hash policies */
1033 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1034 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1035
1036 if (nla_len(rta) < sizeof(*thresh4))
1037 return -EINVAL;
1038 thresh4 = nla_data(rta);
1039 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1040 return -EINVAL;
1041 }
1042 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1043 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1044
1045 if (nla_len(rta) < sizeof(*thresh6))
1046 return -EINVAL;
1047 thresh6 = nla_data(rta);
1048 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1049 return -EINVAL;
1050 }
1051
1052 if (thresh4 || thresh6) {
1053 write_seqlock(&net->xfrm.policy_hthresh.lock);
1054 if (thresh4) {
1055 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1056 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1057 }
1058 if (thresh6) {
1059 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1060 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1061 }
1062 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1063
1064 xfrm_policy_hash_rebuild(net);
1065 }
1066
1067 return 0;
1068}
1069
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001070static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001071 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001072{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001073 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001074 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001075 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001076 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001077 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001078
Thomas Graf7deb2262007-08-22 13:57:39 -07001079 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001080 if (r_skb == NULL)
1081 return -ENOMEM;
1082
Eric W. Biederman15e47302012-09-07 20:12:54 +00001083 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001084 BUG();
1085
Eric W. Biederman15e47302012-09-07 20:12:54 +00001086 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001087}
1088
Thomas Graf7deb2262007-08-22 13:57:39 -07001089static inline size_t xfrm_sadinfo_msgsize(void)
1090{
1091 return NLMSG_ALIGN(4)
1092 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1093 + nla_total_size(4); /* XFRMA_SAD_CNT */
1094}
1095
Alexey Dobriyane0710412010-01-23 13:37:10 +00001096static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001097 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001098{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001099 struct xfrmk_sadinfo si;
1100 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001101 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001102 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001103 u32 *f;
1104
Eric W. Biederman15e47302012-09-07 20:12:54 +00001105 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001106 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001107 return -EMSGSIZE;
1108
1109 f = nlmsg_data(nlh);
1110 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001111 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001112
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001113 sh.sadhmcnt = si.sadhmcnt;
1114 sh.sadhcnt = si.sadhcnt;
1115
David S. Miller1d1e34d2012-06-27 21:57:03 -07001116 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1117 if (!err)
1118 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1119 if (err) {
1120 nlmsg_cancel(skb, nlh);
1121 return err;
1122 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001123
1124 return nlmsg_end(skb, nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001125}
1126
1127static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001128 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001129{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001130 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001131 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001132 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001133 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001134 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001135
Thomas Graf7deb2262007-08-22 13:57:39 -07001136 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001137 if (r_skb == NULL)
1138 return -ENOMEM;
1139
Eric W. Biederman15e47302012-09-07 20:12:54 +00001140 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001141 BUG();
1142
Eric W. Biederman15e47302012-09-07 20:12:54 +00001143 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001144}
1145
Christoph Hellwig22e70052007-01-02 15:22:30 -08001146static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001147 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001149 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001150 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 struct xfrm_state *x;
1152 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001153 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001155 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (x == NULL)
1157 goto out_noput;
1158
1159 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1160 if (IS_ERR(resp_skb)) {
1161 err = PTR_ERR(resp_skb);
1162 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001163 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 xfrm_state_put(x);
1166out_noput:
1167 return err;
1168}
1169
Christoph Hellwig22e70052007-01-02 15:22:30 -08001170static int xfrm_alloc_userspi(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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 struct xfrm_state *x;
1175 struct xfrm_userspi_info *p;
1176 struct sk_buff *resp_skb;
1177 xfrm_address_t *daddr;
1178 int family;
1179 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001180 u32 mark;
1181 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Thomas Graf7b67c852007-08-22 13:53:52 -07001183 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001184 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (err)
1186 goto out_noput;
1187
1188 family = p->info.family;
1189 daddr = &p->info.id.daddr;
1190
1191 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001192
1193 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001195 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001196 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 xfrm_state_put(x);
1198 x = NULL;
1199 }
1200 }
1201
1202 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001203 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 p->info.id.proto, daddr,
1205 &p->info.saddr, 1,
1206 family);
1207 err = -ENOENT;
1208 if (x == NULL)
1209 goto out_noput;
1210
Herbert Xu658b2192007-10-09 13:29:52 -07001211 err = xfrm_alloc_spi(x, p->min, p->max);
1212 if (err)
1213 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Herbert Xu658b2192007-10-09 13:29:52 -07001215 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (IS_ERR(resp_skb)) {
1217 err = PTR_ERR(resp_skb);
1218 goto out;
1219 }
1220
Eric W. Biederman15e47302012-09-07 20:12:54 +00001221 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223out:
1224 xfrm_state_put(x);
1225out_noput:
1226 return err;
1227}
1228
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001229static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 switch (dir) {
1232 case XFRM_POLICY_IN:
1233 case XFRM_POLICY_OUT:
1234 case XFRM_POLICY_FWD:
1235 break;
1236
1237 default:
1238 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 return 0;
1242}
1243
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001244static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001245{
1246 switch (type) {
1247 case XFRM_POLICY_TYPE_MAIN:
1248#ifdef CONFIG_XFRM_SUB_POLICY
1249 case XFRM_POLICY_TYPE_SUB:
1250#endif
1251 break;
1252
1253 default:
1254 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001255 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001256
1257 return 0;
1258}
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1261{
Fan Due682adf2013-11-07 17:47:48 +08001262 int ret;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 switch (p->share) {
1265 case XFRM_SHARE_ANY:
1266 case XFRM_SHARE_SESSION:
1267 case XFRM_SHARE_USER:
1268 case XFRM_SHARE_UNIQUE:
1269 break;
1270
1271 default:
1272 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 switch (p->action) {
1276 case XFRM_POLICY_ALLOW:
1277 case XFRM_POLICY_BLOCK:
1278 break;
1279
1280 default:
1281 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 switch (p->sel.family) {
1285 case AF_INET:
1286 break;
1287
1288 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001289#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 break;
1291#else
1292 return -EAFNOSUPPORT;
1293#endif
1294
1295 default:
1296 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Fan Due682adf2013-11-07 17:47:48 +08001299 ret = verify_policy_dir(p->dir);
1300 if (ret)
1301 return ret;
1302 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1303 return -EINVAL;
1304
1305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Thomas Graf5424f322007-08-22 14:01:33 -07001308static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001309{
Thomas Graf5424f322007-08-22 14:01:33 -07001310 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001311 struct xfrm_user_sec_ctx *uctx;
1312
1313 if (!rt)
1314 return 0;
1315
Thomas Graf5424f322007-08-22 14:01:33 -07001316 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001317 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001318}
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1321 int nr)
1322{
1323 int i;
1324
1325 xp->xfrm_nr = nr;
1326 for (i = 0; i < nr; i++, ut++) {
1327 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1328
1329 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1330 memcpy(&t->saddr, &ut->saddr,
1331 sizeof(xfrm_address_t));
1332 t->reqid = ut->reqid;
1333 t->mode = ut->mode;
1334 t->share = ut->share;
1335 t->optional = ut->optional;
1336 t->aalgos = ut->aalgos;
1337 t->ealgos = ut->ealgos;
1338 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001339 /* If all masks are ~0, then we allow all algorithms. */
1340 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001341 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343}
1344
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001345static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1346{
1347 int i;
1348
1349 if (nr > XFRM_MAX_DEPTH)
1350 return -EINVAL;
1351
1352 for (i = 0; i < nr; i++) {
1353 /* We never validated the ut->family value, so many
1354 * applications simply leave it at zero. The check was
1355 * never made and ut->family was ignored because all
1356 * templates could be assumed to have the same family as
1357 * the policy itself. Now that we will have ipv4-in-ipv6
1358 * and ipv6-in-ipv4 tunnels, this is no longer true.
1359 */
1360 if (!ut[i].family)
1361 ut[i].family = family;
1362
1363 switch (ut[i].family) {
1364 case AF_INET:
1365 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001366#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001367 case AF_INET6:
1368 break;
1369#endif
1370 default:
1371 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001372 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001373 }
1374
1375 return 0;
1376}
1377
Thomas Graf5424f322007-08-22 14:01:33 -07001378static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Thomas Graf5424f322007-08-22 14:01:33 -07001380 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 if (!rt) {
1383 pol->xfrm_nr = 0;
1384 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001385 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1386 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001387 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001389 err = validate_tmpl(nr, utmpl, pol->family);
1390 if (err)
1391 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Thomas Graf5424f322007-08-22 14:01:33 -07001393 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395 return 0;
1396}
1397
Thomas Graf5424f322007-08-22 14:01:33 -07001398static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001399{
Thomas Graf5424f322007-08-22 14:01:33 -07001400 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001401 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001402 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001403 int err;
1404
1405 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001406 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001407 type = upt->type;
1408 }
1409
1410 err = verify_policy_type(type);
1411 if (err)
1412 return err;
1413
1414 *tp = type;
1415 return 0;
1416}
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1419{
1420 xp->priority = p->priority;
1421 xp->index = p->index;
1422 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1423 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1424 xp->action = p->action;
1425 xp->flags = p->flags;
1426 xp->family = p->sel.family;
1427 /* XXX xp->share = p->share; */
1428}
1429
1430static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1431{
Mathias Krause7b789832012-09-19 11:33:40 +00001432 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1434 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1435 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1436 p->priority = xp->priority;
1437 p->index = xp->index;
1438 p->sel.family = xp->family;
1439 p->dir = dir;
1440 p->action = xp->action;
1441 p->flags = xp->flags;
1442 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1443}
1444
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001445static 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 -07001446{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001447 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 int err;
1449
1450 if (!xp) {
1451 *errp = -ENOMEM;
1452 return NULL;
1453 }
1454
1455 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001456
Thomas Graf35a7aa02007-08-22 14:00:40 -07001457 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001458 if (err)
1459 goto error;
1460
Thomas Graf35a7aa02007-08-22 14:00:40 -07001461 if (!(err = copy_from_user_tmpl(xp, attrs)))
1462 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001463 if (err)
1464 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001466 xfrm_mark_get(attrs, &xp->mark);
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001469 error:
1470 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001471 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001472 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001473 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Christoph Hellwig22e70052007-01-02 15:22:30 -08001476static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001477 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001479 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001480 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001482 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 int err;
1484 int excl;
1485
1486 err = verify_newpolicy_info(p);
1487 if (err)
1488 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001489 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001490 if (err)
1491 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001493 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (!xp)
1495 return err;
1496
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001497 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001498 * Aha! this is anti-netlink really i.e more pfkey derived
1499 * in netlink excl is a flag and you wouldnt need
1500 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1502 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001503 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001506 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 kfree(xp);
1508 return err;
1509 }
1510
Herbert Xuf60f6b82005-06-18 22:44:37 -07001511 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001512 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001513 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001514 km_policy_notify(xp, p->dir, &c);
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 xfrm_pol_put(xp);
1517
1518 return 0;
1519}
1520
1521static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1522{
1523 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1524 int i;
1525
1526 if (xp->xfrm_nr == 0)
1527 return 0;
1528
1529 for (i = 0; i < xp->xfrm_nr; i++) {
1530 struct xfrm_user_tmpl *up = &vec[i];
1531 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1532
Mathias Krause1f868402012-09-19 11:33:41 +00001533 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001535 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1537 up->reqid = kp->reqid;
1538 up->mode = kp->mode;
1539 up->share = kp->share;
1540 up->optional = kp->optional;
1541 up->aalgos = kp->aalgos;
1542 up->ealgos = kp->ealgos;
1543 up->calgos = kp->calgos;
1544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Thomas Grafc0144be2007-08-22 13:55:43 -07001546 return nla_put(skb, XFRMA_TMPL,
1547 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001548}
1549
Serge Hallyn0d681622006-07-24 23:30:44 -07001550static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1551{
1552 if (x->security) {
1553 return copy_sec_ctx(x->security, skb);
1554 }
1555 return 0;
1556}
1557
1558static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1559{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001560 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001561 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001562 return 0;
1563}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001564static inline size_t userpolicy_type_attrsize(void)
1565{
1566#ifdef CONFIG_XFRM_SUB_POLICY
1567 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1568#else
1569 return 0;
1570#endif
1571}
Serge Hallyn0d681622006-07-24 23:30:44 -07001572
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001573#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001574static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001575{
Thomas Grafc0144be2007-08-22 13:55:43 -07001576 struct xfrm_userpolicy_type upt = {
1577 .type = type,
1578 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001579
Thomas Grafc0144be2007-08-22 13:55:43 -07001580 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001581}
1582
1583#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001584static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001585{
1586 return 0;
1587}
1588#endif
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1591{
1592 struct xfrm_dump_info *sp = ptr;
1593 struct xfrm_userpolicy_info *p;
1594 struct sk_buff *in_skb = sp->in_skb;
1595 struct sk_buff *skb = sp->out_skb;
1596 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001597 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Eric W. Biederman15e47302012-09-07 20:12:54 +00001599 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001600 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1601 if (nlh == NULL)
1602 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Thomas Graf7b67c852007-08-22 13:53:52 -07001604 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001606 err = copy_to_user_tmpl(xp, skb);
1607 if (!err)
1608 err = copy_to_user_sec_ctx(xp, skb);
1609 if (!err)
1610 err = copy_to_user_policy_type(xp->type, skb);
1611 if (!err)
1612 err = xfrm_mark_put(skb, &xp->mark);
1613 if (err) {
1614 nlmsg_cancel(skb, nlh);
1615 return err;
1616 }
Thomas Graf98250692007-08-22 12:47:26 -07001617 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
Timo Teras4c563f72008-02-28 21:31:08 -08001621static int xfrm_dump_policy_done(struct netlink_callback *cb)
1622{
1623 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001624 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001625
Fan Du283bc9f2013-11-07 17:47:50 +08001626 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001627 return 0;
1628}
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1631{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001632 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001633 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 struct xfrm_dump_info info;
1635
Timo Teras4c563f72008-02-28 21:31:08 -08001636 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1637 sizeof(cb->args) - sizeof(cb->args[0]));
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 info.in_skb = cb->skb;
1640 info.out_skb = skb;
1641 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1642 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001643
1644 if (!cb->args[0]) {
1645 cb->args[0] = 1;
1646 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1647 }
1648
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001649 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 return skb->len;
1652}
1653
1654static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1655 struct xfrm_policy *xp,
1656 int dir, u32 seq)
1657{
1658 struct xfrm_dump_info info;
1659 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001660 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Thomas Graf7deb2262007-08-22 13:57:39 -07001662 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (!skb)
1664 return ERR_PTR(-ENOMEM);
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 info.in_skb = in_skb;
1667 info.out_skb = skb;
1668 info.nlmsg_seq = seq;
1669 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Mathias Krausec2546372012-09-14 09:58:32 +00001671 err = dump_one_policy(xp, dir, 0, &info);
1672 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001674 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676
1677 return skb;
1678}
1679
Christoph Hellwig22e70052007-01-02 15:22:30 -08001680static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001681 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001683 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 struct xfrm_policy *xp;
1685 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001686 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001688 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001690 struct xfrm_mark m;
1691 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Thomas Graf7b67c852007-08-22 13:53:52 -07001693 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1695
Thomas Graf35a7aa02007-08-22 14:00:40 -07001696 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001697 if (err)
1698 return err;
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 err = verify_policy_dir(p->dir);
1701 if (err)
1702 return err;
1703
1704 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001705 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001706 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001707 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001708 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001709
Thomas Graf35a7aa02007-08-22 14:00:40 -07001710 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001711 if (err)
1712 return err;
1713
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001714 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001715 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001716 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001717
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001718 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001719 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001720 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001721 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001722 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001723 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001724 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (xp == NULL)
1727 return -ENOENT;
1728
1729 if (!delete) {
1730 struct sk_buff *resp_skb;
1731
1732 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1733 if (IS_ERR(resp_skb)) {
1734 err = PTR_ERR(resp_skb);
1735 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001736 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001737 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001739 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09001740 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001741
1742 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001743 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001744
Herbert Xue7443892005-06-18 22:44:18 -07001745 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001746 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001747 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001748 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001749 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001752out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001753 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001754 if (delete && err == 0)
1755 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return err;
1757}
1758
Christoph Hellwig22e70052007-01-02 15:22:30 -08001759static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001760 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001762 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001763 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001764 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04001765 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Tetsuo Handa2e710292014-04-22 21:48:30 +09001767 err = xfrm_state_flush(net, p->proto, true);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001768 if (err) {
1769 if (err == -ESRCH) /* empty table */
1770 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001771 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001772 }
Herbert Xubf088672005-06-18 22:44:00 -07001773 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001774 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001775 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001776 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001777 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001778 km_state_notify(NULL, &c);
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return 0;
1781}
1782
Steffen Klassertd8647b72011-03-08 00:10:27 +00001783static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001784{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001785 size_t replay_size = x->replay_esn ?
1786 xfrm_replay_state_esn_len(x->replay_esn) :
1787 sizeof(struct xfrm_replay_state);
1788
Thomas Graf7deb2262007-08-22 13:57:39 -07001789 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001790 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001791 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001792 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001793 + nla_total_size(4) /* XFRM_AE_RTHR */
1794 + nla_total_size(4); /* XFRM_AE_ETHR */
1795}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001796
David S. Miller214e0052011-02-24 00:02:38 -05001797static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001798{
1799 struct xfrm_aevent_id *id;
1800 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001801 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001802
Eric W. Biederman15e47302012-09-07 20:12:54 +00001803 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001804 if (nlh == NULL)
1805 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001806
Thomas Graf7b67c852007-08-22 13:53:52 -07001807 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001808 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001809 id->sa_id.spi = x->id.spi;
1810 id->sa_id.family = x->props.family;
1811 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001812 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001813 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001814 id->flags = c->data.aevent;
1815
David S. Millerd0fde792012-03-29 04:02:26 -04001816 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001817 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1818 xfrm_replay_state_esn_len(x->replay_esn),
1819 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001820 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001821 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1822 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001823 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001824 if (err)
1825 goto out_cancel;
1826 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1827 if (err)
1828 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001829
David S. Miller1d1e34d2012-06-27 21:57:03 -07001830 if (id->flags & XFRM_AE_RTHR) {
1831 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1832 if (err)
1833 goto out_cancel;
1834 }
1835 if (id->flags & XFRM_AE_ETHR) {
1836 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1837 x->replay_maxage * 10 / HZ);
1838 if (err)
1839 goto out_cancel;
1840 }
1841 err = xfrm_mark_put(skb, &x->mark);
1842 if (err)
1843 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001844
Thomas Graf98250692007-08-22 12:47:26 -07001845 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001846
David S. Miller1d1e34d2012-06-27 21:57:03 -07001847out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001848 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001849 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001850}
1851
Christoph Hellwig22e70052007-01-02 15:22:30 -08001852static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001853 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001854{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001855 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001856 struct xfrm_state *x;
1857 struct sk_buff *r_skb;
1858 int err;
1859 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001860 u32 mark;
1861 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001862 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001863 struct xfrm_usersa_id *id = &p->sa_id;
1864
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001865 mark = xfrm_mark_get(attrs, &m);
1866
1867 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001868 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001869 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001870
1871 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1872 if (r_skb == NULL) {
1873 xfrm_state_put(x);
1874 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001875 }
1876
1877 /*
1878 * XXX: is this lock really needed - none of the other
1879 * gets lock (the concern is things getting updated
1880 * while we are still reading) - jhs
1881 */
1882 spin_lock_bh(&x->lock);
1883 c.data.aevent = p->flags;
1884 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001885 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001886
1887 if (build_aevent(r_skb, x, &c) < 0)
1888 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001889 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001890 spin_unlock_bh(&x->lock);
1891 xfrm_state_put(x);
1892 return err;
1893}
1894
Christoph Hellwig22e70052007-01-02 15:22:30 -08001895static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001896 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001897{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001898 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001899 struct xfrm_state *x;
1900 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001901 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001902 u32 mark = 0;
1903 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001904 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001905 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001906 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001907 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001908
Steffen Klassertd8647b72011-03-08 00:10:27 +00001909 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001910 return err;
1911
1912 /* pedantic mode - thou shalt sayeth replaceth */
1913 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1914 return err;
1915
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001916 mark = xfrm_mark_get(attrs, &m);
1917
1918 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 -08001919 if (x == NULL)
1920 return -ESRCH;
1921
1922 if (x->km.state != XFRM_STATE_VALID)
1923 goto out;
1924
Steffen Klassert4479ff72013-09-09 09:39:01 +02001925 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001926 if (err)
1927 goto out;
1928
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001929 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001930 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001931 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001932
1933 c.event = nlh->nlmsg_type;
1934 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001935 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001936 c.data.aevent = XFRM_AE_CU;
1937 km_state_notify(x, &c);
1938 err = 0;
1939out:
1940 xfrm_state_put(x);
1941 return err;
1942}
1943
Christoph Hellwig22e70052007-01-02 15:22:30 -08001944static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001945 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001947 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001948 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001949 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001950 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001951
Thomas Graf35a7aa02007-08-22 14:00:40 -07001952 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001953 if (err)
1954 return err;
1955
Tetsuo Handa2e710292014-04-22 21:48:30 +09001956 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001957 if (err) {
1958 if (err == -ESRCH) /* empty table */
1959 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001960 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001961 }
1962
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001963 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001964 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001965 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001966 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001967 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001968 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return 0;
1970}
1971
Christoph Hellwig22e70052007-01-02 15:22:30 -08001972static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001973 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001974{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001975 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001976 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001977 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001978 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001979 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001980 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001981 struct xfrm_mark m;
1982 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001983
Thomas Graf35a7aa02007-08-22 14:00:40 -07001984 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001985 if (err)
1986 return err;
1987
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001988 err = verify_policy_dir(p->dir);
1989 if (err)
1990 return err;
1991
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001992 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001993 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001994 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001995 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001996 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001997
Thomas Graf35a7aa02007-08-22 14:00:40 -07001998 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001999 if (err)
2000 return err;
2001
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002002 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002003 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07002004 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002005
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01002006 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07002007 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002008 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07002009 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002010 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002011 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07002012 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002013 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002014 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08002015 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07002016
Timo Teräsea2dea92010-03-31 00:17:05 +00002017 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002018 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002019
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002020 err = 0;
2021 if (up->hard) {
2022 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002023 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002024 } else {
2025 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00002026 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002027 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002028 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002029
2030out:
2031 xfrm_pol_put(xp);
2032 return err;
2033}
2034
Christoph Hellwig22e70052007-01-02 15:22:30 -08002035static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002036 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002037{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002038 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002039 struct xfrm_state *x;
2040 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002041 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002042 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002043 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002044 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002045
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002046 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 -08002047
David S. Miller3a765aa2007-02-26 14:52:21 -08002048 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002049 if (x == NULL)
2050 return err;
2051
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002052 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002053 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002054 if (x->km.state != XFRM_STATE_VALID)
2055 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002056 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002057
Joy Latten161a09e2006-11-27 13:11:54 -06002058 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002059 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002060 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06002061 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002062 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002063out:
2064 spin_unlock_bh(&x->lock);
2065 xfrm_state_put(x);
2066 return err;
2067}
2068
Christoph Hellwig22e70052007-01-02 15:22:30 -08002069static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002070 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002071{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002072 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002073 struct xfrm_policy *xp;
2074 struct xfrm_user_tmpl *ut;
2075 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002076 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002077 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002078
Thomas Graf7b67c852007-08-22 13:53:52 -07002079 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002080 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002081 int err = -ENOMEM;
2082
2083 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002084 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002085
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002086 xfrm_mark_get(attrs, &mark);
2087
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002088 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002089 if (err)
2090 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002091
2092 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002093 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002094 if (!xp)
2095 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002096
2097 memcpy(&x->id, &ua->id, sizeof(ua->id));
2098 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2099 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002100 xp->mark.m = x->mark.m = mark.m;
2101 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002102 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002103 /* extract the templates and for each call km_key */
2104 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2105 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2106 memcpy(&x->id, &t->id, sizeof(x->id));
2107 x->props.mode = t->mode;
2108 x->props.reqid = t->reqid;
2109 x->props.family = ut->family;
2110 t->aalgos = ua->aalgos;
2111 t->ealgos = ua->ealgos;
2112 t->calgos = ua->calgos;
2113 err = km_query(x, t, xp);
2114
2115 }
2116
2117 kfree(x);
2118 kfree(xp);
2119
2120 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002121
2122bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002123 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002124free_state:
2125 kfree(x);
2126nomem:
2127 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002128}
2129
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002130#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002131static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002132 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002133 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002134{
Thomas Graf5424f322007-08-22 14:01:33 -07002135 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002136 struct xfrm_user_migrate *um;
2137 int i, num_migrate;
2138
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002139 if (k != NULL) {
2140 struct xfrm_user_kmaddress *uk;
2141
2142 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2143 memcpy(&k->local, &uk->local, sizeof(k->local));
2144 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2145 k->family = uk->family;
2146 k->reserved = uk->reserved;
2147 }
2148
Thomas Graf5424f322007-08-22 14:01:33 -07002149 um = nla_data(rt);
2150 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002151
2152 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2153 return -EINVAL;
2154
2155 for (i = 0; i < num_migrate; i++, um++, ma++) {
2156 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2157 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2158 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2159 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2160
2161 ma->proto = um->proto;
2162 ma->mode = um->mode;
2163 ma->reqid = um->reqid;
2164
2165 ma->old_family = um->old_family;
2166 ma->new_family = um->new_family;
2167 }
2168
2169 *num = i;
2170 return 0;
2171}
2172
2173static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002174 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002175{
Thomas Graf7b67c852007-08-22 13:53:52 -07002176 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002177 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002178 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002179 u8 type;
2180 int err;
2181 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002182 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002183
Thomas Graf35a7aa02007-08-22 14:00:40 -07002184 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002185 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002186
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002187 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2188
Thomas Graf5424f322007-08-22 14:01:33 -07002189 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002190 if (err)
2191 return err;
2192
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002193 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002194 if (err)
2195 return err;
2196
2197 if (!n)
2198 return 0;
2199
Fan Du8d549c42013-11-07 17:47:49 +08002200 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002201
2202 return 0;
2203}
2204#else
2205static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002206 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002207{
2208 return -ENOPROTOOPT;
2209}
2210#endif
2211
2212#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002213static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002214{
2215 struct xfrm_user_migrate um;
2216
2217 memset(&um, 0, sizeof(um));
2218 um.proto = m->proto;
2219 um.mode = m->mode;
2220 um.reqid = m->reqid;
2221 um.old_family = m->old_family;
2222 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2223 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2224 um.new_family = m->new_family;
2225 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2226 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2227
Thomas Grafc0144be2007-08-22 13:55:43 -07002228 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002229}
2230
David S. Miller183cad12011-02-24 00:28:01 -05002231static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002232{
2233 struct xfrm_user_kmaddress uk;
2234
2235 memset(&uk, 0, sizeof(uk));
2236 uk.family = k->family;
2237 uk.reserved = k->reserved;
2238 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002239 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002240
2241 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2242}
2243
2244static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002245{
2246 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002247 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2248 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2249 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002250}
2251
David S. Miller183cad12011-02-24 00:28:01 -05002252static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2253 int num_migrate, const struct xfrm_kmaddress *k,
2254 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002255{
David S. Miller183cad12011-02-24 00:28:01 -05002256 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002257 struct xfrm_userpolicy_id *pol_id;
2258 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002259 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002260
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002261 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2262 if (nlh == NULL)
2263 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002264
Thomas Graf7b67c852007-08-22 13:53:52 -07002265 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002266 /* copy data from selector, dir, and type to the pol_id */
2267 memset(pol_id, 0, sizeof(*pol_id));
2268 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2269 pol_id->dir = dir;
2270
David S. Miller1d1e34d2012-06-27 21:57:03 -07002271 if (k != NULL) {
2272 err = copy_to_user_kmaddress(k, skb);
2273 if (err)
2274 goto out_cancel;
2275 }
2276 err = copy_to_user_policy_type(type, skb);
2277 if (err)
2278 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002279 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002280 err = copy_to_user_migrate(mp, skb);
2281 if (err)
2282 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002283 }
2284
Thomas Graf98250692007-08-22 12:47:26 -07002285 return nlmsg_end(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002286
2287out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002288 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002289 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002290}
2291
David S. Miller183cad12011-02-24 00:28:01 -05002292static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2293 const struct xfrm_migrate *m, int num_migrate,
2294 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002295{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002296 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002297 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002298
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002299 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002300 if (skb == NULL)
2301 return -ENOMEM;
2302
2303 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002304 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002305 BUG();
2306
Michal Kubecek21ee5432014-06-03 10:26:06 +02002307 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002308}
2309#else
David S. Miller183cad12011-02-24 00:28:01 -05002310static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2311 const struct xfrm_migrate *m, int num_migrate,
2312 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002313{
2314 return -ENOPROTOOPT;
2315}
2316#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002317
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002318#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002319
2320static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002321 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002322 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2323 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2324 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2325 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2326 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2327 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002328 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002329 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002330 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002331 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002332 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002333 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002334 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002335 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2336 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002337 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002338 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002339 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002340 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002341 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342};
2343
Thomas Graf492b5582005-05-03 14:26:40 -07002344#undef XMSGSIZE
2345
Thomas Grafcf5cb792007-08-22 13:59:04 -07002346static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002347 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2348 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2349 [XFRMA_LASTUSED] = { .type = NLA_U64},
2350 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002351 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002352 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2353 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2354 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2355 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2356 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2357 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2358 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2359 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2360 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2361 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2362 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2363 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2364 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2365 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002366 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002367 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002368 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002369 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002370 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002371 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002372 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002373};
2374
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002375static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2376 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2377 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2378};
2379
Mathias Krause05600a72013-02-24 14:10:27 +01002380static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002381 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002383 int (*done)(struct netlink_callback *);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002384 const struct nla_policy *nla_pol;
2385 int nla_max;
Thomas Graf492b5582005-05-03 14:26:40 -07002386} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2387 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2388 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2389 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002390 .dump = xfrm_dump_sa,
2391 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002392 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2393 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2394 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002395 .dump = xfrm_dump_policy,
2396 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002397 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002398 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002399 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002400 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2401 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002402 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002403 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2404 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002405 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2406 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002407 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002408 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002409 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2410 .nla_pol = xfrma_spd_policy,
2411 .nla_max = XFRMA_SPD_MAX },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002412 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413};
2414
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002415static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002417 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002418 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002419 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002420 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002424 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 type -= XFRM_MSG_BASE;
2427 link = &xfrm_dispatch[type];
2428
2429 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002430 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002431 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Thomas Graf492b5582005-05-03 14:26:40 -07002433 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2434 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002435 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002437 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002439 {
2440 struct netlink_dump_control c = {
2441 .dump = link->dump,
2442 .done = link->done,
2443 };
2444 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
2447
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002448 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2449 link->nla_max ? : XFRMA_MAX,
2450 link->nla_pol ? : xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002451 if (err < 0)
2452 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Thomas Graf5424f322007-08-22 14:01:33 -07002457 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002460static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
Fan Du283bc9f2013-11-07 17:47:50 +08002462 struct net *net = sock_net(skb->sk);
2463
2464 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002465 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002466 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Thomas Graf7deb2262007-08-22 13:57:39 -07002469static inline size_t xfrm_expire_msgsize(void)
2470{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002471 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2472 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002473}
2474
David S. Miller214e0052011-02-24 00:02:38 -05002475static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 struct xfrm_user_expire *ue;
2478 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002479 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Eric W. Biederman15e47302012-09-07 20:12:54 +00002481 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002482 if (nlh == NULL)
2483 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Thomas Graf7b67c852007-08-22 13:53:52 -07002485 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002487 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
David S. Miller1d1e34d2012-06-27 21:57:03 -07002489 err = xfrm_mark_put(skb, &x->mark);
2490 if (err)
2491 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002492
Thomas Graf98250692007-08-22 12:47:26 -07002493 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
David S. Miller214e0052011-02-24 00:02:38 -05002496static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002498 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 struct sk_buff *skb;
2500
Thomas Graf7deb2262007-08-22 13:57:39 -07002501 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (skb == NULL)
2503 return -ENOMEM;
2504
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002505 if (build_expire(skb, x, c) < 0) {
2506 kfree_skb(skb);
2507 return -EMSGSIZE;
2508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Michal Kubecek21ee5432014-06-03 10:26:06 +02002510 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
David S. Miller214e0052011-02-24 00:02:38 -05002513static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002514{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002515 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002516 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002517
Steffen Klassertd8647b72011-03-08 00:10:27 +00002518 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002519 if (skb == NULL)
2520 return -ENOMEM;
2521
2522 if (build_aevent(skb, x, c) < 0)
2523 BUG();
2524
Michal Kubecek21ee5432014-06-03 10:26:06 +02002525 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002526}
2527
David S. Miller214e0052011-02-24 00:02:38 -05002528static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002529{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002530 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002531 struct xfrm_usersa_flush *p;
2532 struct nlmsghdr *nlh;
2533 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002534 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002535
Thomas Graf7deb2262007-08-22 13:57:39 -07002536 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002537 if (skb == NULL)
2538 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002539
Eric W. Biederman15e47302012-09-07 20:12:54 +00002540 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002541 if (nlh == NULL) {
2542 kfree_skb(skb);
2543 return -EMSGSIZE;
2544 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002545
Thomas Graf7b67c852007-08-22 13:53:52 -07002546 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002547 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002548
Thomas Graf98250692007-08-22 12:47:26 -07002549 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002550
Michal Kubecek21ee5432014-06-03 10:26:06 +02002551 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002552}
2553
Thomas Graf7deb2262007-08-22 13:57:39 -07002554static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002555{
Thomas Graf7deb2262007-08-22 13:57:39 -07002556 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002557 if (x->aead)
2558 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002559 if (x->aalg) {
2560 l += nla_total_size(sizeof(struct xfrm_algo) +
2561 (x->aalg->alg_key_len + 7) / 8);
2562 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2563 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002564 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002565 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002566 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002567 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002568 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002569 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002570 if (x->tfcpad)
2571 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002572 if (x->replay_esn)
2573 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
dingzhif293a5e2014-10-30 09:39:36 +01002574 else
2575 l += nla_total_size(sizeof(struct xfrm_replay_state));
Herbert Xu68325d32007-10-09 13:30:57 -07002576 if (x->security)
2577 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2578 x->security->ctx_len);
2579 if (x->coaddr)
2580 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002581 if (x->props.extra_flags)
2582 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002583
Herbert Xud26f3982007-11-13 21:47:08 -08002584 /* Must count x->lastused as it may become non-zero behind our back. */
2585 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002586
2587 return l;
2588}
2589
David S. Miller214e0052011-02-24 00:02:38 -05002590static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002591{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002592 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002593 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002594 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002595 struct nlmsghdr *nlh;
2596 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002597 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002598 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002599
2600 headlen = sizeof(*p);
2601 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002602 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002603 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002604 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002605 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002606 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002607
Thomas Graf7deb2262007-08-22 13:57:39 -07002608 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002609 if (skb == NULL)
2610 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002611
Eric W. Biederman15e47302012-09-07 20:12:54 +00002612 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002613 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002614 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002615 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002616
Thomas Graf7b67c852007-08-22 13:53:52 -07002617 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002618 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002619 struct nlattr *attr;
2620
Thomas Graf7b67c852007-08-22 13:53:52 -07002621 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002622 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2623 id->spi = x->id.spi;
2624 id->family = x->props.family;
2625 id->proto = x->id.proto;
2626
Thomas Grafc0144be2007-08-22 13:55:43 -07002627 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002628 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002629 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002630 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002631
2632 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002633 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002634 err = copy_to_user_state_extra(x, p, skb);
2635 if (err)
2636 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002637
Thomas Graf98250692007-08-22 12:47:26 -07002638 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002639
Michal Kubecek21ee5432014-06-03 10:26:06 +02002640 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002641
David S. Miller1d1e34d2012-06-27 21:57:03 -07002642out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002643 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002644 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002645}
2646
David S. Miller214e0052011-02-24 00:02:38 -05002647static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002648{
2649
2650 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002651 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002652 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002653 case XFRM_MSG_NEWAE:
2654 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002655 case XFRM_MSG_DELSA:
2656 case XFRM_MSG_UPDSA:
2657 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002658 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002659 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002660 return xfrm_notify_sa_flush(c);
2661 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002662 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2663 c->event);
2664 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002665 }
2666
2667 return 0;
2668
2669}
2670
Thomas Graf7deb2262007-08-22 13:57:39 -07002671static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2672 struct xfrm_policy *xp)
2673{
2674 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2675 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002676 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002677 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2678 + userpolicy_type_attrsize();
2679}
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002682 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002684 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 struct xfrm_user_acquire *ua;
2686 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002687 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002689 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2690 if (nlh == NULL)
2691 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Thomas Graf7b67c852007-08-22 13:53:52 -07002693 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 memcpy(&ua->id, &x->id, sizeof(ua->id));
2695 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2696 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002697 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 ua->aalgos = xt->aalgos;
2699 ua->ealgos = xt->ealgos;
2700 ua->calgos = xt->calgos;
2701 ua->seq = x->km.seq = seq;
2702
David S. Miller1d1e34d2012-06-27 21:57:03 -07002703 err = copy_to_user_tmpl(xp, skb);
2704 if (!err)
2705 err = copy_to_user_state_sec_ctx(x, skb);
2706 if (!err)
2707 err = copy_to_user_policy_type(xp->type, skb);
2708 if (!err)
2709 err = xfrm_mark_put(skb, &xp->mark);
2710 if (err) {
2711 nlmsg_cancel(skb, nlh);
2712 return err;
2713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Thomas Graf98250692007-08-22 12:47:26 -07002715 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717
2718static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002719 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002721 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Thomas Graf7deb2262007-08-22 13:57:39 -07002724 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (skb == NULL)
2726 return -ENOMEM;
2727
Fan Du65e07362012-08-15 10:13:47 +08002728 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 BUG();
2730
Michal Kubecek21ee5432014-06-03 10:26:06 +02002731 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
2734/* User gives us xfrm_user_policy_info followed by an array of 0
2735 * or more templates.
2736 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002737static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 u8 *data, int len, int *dir)
2739{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002740 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2742 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2743 struct xfrm_policy *xp;
2744 int nr;
2745
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002746 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 case AF_INET:
2748 if (opt != IP_XFRM_POLICY) {
2749 *dir = -EOPNOTSUPP;
2750 return NULL;
2751 }
2752 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002753#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 case AF_INET6:
2755 if (opt != IPV6_XFRM_POLICY) {
2756 *dir = -EOPNOTSUPP;
2757 return NULL;
2758 }
2759 break;
2760#endif
2761 default:
2762 *dir = -EINVAL;
2763 return NULL;
2764 }
2765
2766 *dir = -EINVAL;
2767
2768 if (len < sizeof(*p) ||
2769 verify_newpolicy_info(p))
2770 return NULL;
2771
2772 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002773 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return NULL;
2775
Herbert Xua4f1bac2005-07-26 15:43:17 -07002776 if (p->dir > XFRM_POLICY_OUT)
2777 return NULL;
2778
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002779 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (xp == NULL) {
2781 *dir = -ENOBUFS;
2782 return NULL;
2783 }
2784
2785 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002786 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 copy_templates(xp, ut, nr);
2788
2789 *dir = p->dir;
2790
2791 return xp;
2792}
2793
Thomas Graf7deb2262007-08-22 13:57:39 -07002794static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2795{
2796 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2797 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2798 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002799 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002800 + userpolicy_type_attrsize();
2801}
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002804 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
2806 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002807 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002808 struct nlmsghdr *nlh;
2809 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Eric W. Biederman15e47302012-09-07 20:12:54 +00002811 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002812 if (nlh == NULL)
2813 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Thomas Graf7b67c852007-08-22 13:53:52 -07002815 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002817 err = copy_to_user_tmpl(xp, skb);
2818 if (!err)
2819 err = copy_to_user_sec_ctx(xp, skb);
2820 if (!err)
2821 err = copy_to_user_policy_type(xp->type, skb);
2822 if (!err)
2823 err = xfrm_mark_put(skb, &xp->mark);
2824 if (err) {
2825 nlmsg_cancel(skb, nlh);
2826 return err;
2827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 upe->hard = !!hard;
2829
Thomas Graf98250692007-08-22 12:47:26 -07002830 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831}
2832
David S. Miller214e0052011-02-24 00:02:38 -05002833static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002835 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Thomas Graf7deb2262007-08-22 13:57:39 -07002838 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 if (skb == NULL)
2840 return -ENOMEM;
2841
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002842 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 BUG();
2844
Michal Kubecek21ee5432014-06-03 10:26:06 +02002845 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
2847
David S. Miller214e0052011-02-24 00:02:38 -05002848static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002849{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002850 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002851 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002852 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002853 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002854 struct nlmsghdr *nlh;
2855 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002856 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002857
2858 headlen = sizeof(*p);
2859 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002860 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002861 headlen = sizeof(*id);
2862 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002863 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002864 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002865 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002866
Thomas Graf7deb2262007-08-22 13:57:39 -07002867 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002868 if (skb == NULL)
2869 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002870
Eric W. Biederman15e47302012-09-07 20:12:54 +00002871 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002872 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002873 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002874 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002875
Thomas Graf7b67c852007-08-22 13:53:52 -07002876 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002877 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002878 struct nlattr *attr;
2879
Thomas Graf7b67c852007-08-22 13:53:52 -07002880 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002881 memset(id, 0, sizeof(*id));
2882 id->dir = dir;
2883 if (c->data.byid)
2884 id->index = xp->index;
2885 else
2886 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2887
Thomas Grafc0144be2007-08-22 13:55:43 -07002888 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002889 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002890 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002891 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002892
2893 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002894 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002895
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002896 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002897 err = copy_to_user_tmpl(xp, skb);
2898 if (!err)
2899 err = copy_to_user_policy_type(xp->type, skb);
2900 if (!err)
2901 err = xfrm_mark_put(skb, &xp->mark);
2902 if (err)
2903 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002904
Thomas Graf98250692007-08-22 12:47:26 -07002905 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002906
Michal Kubecek21ee5432014-06-03 10:26:06 +02002907 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002908
David S. Miller1d1e34d2012-06-27 21:57:03 -07002909out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002910 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002911 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002912}
2913
David S. Miller214e0052011-02-24 00:02:38 -05002914static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002915{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002916 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002917 struct nlmsghdr *nlh;
2918 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002919 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002920
Thomas Graf7deb2262007-08-22 13:57:39 -07002921 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002922 if (skb == NULL)
2923 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002924
Eric W. Biederman15e47302012-09-07 20:12:54 +00002925 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002926 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002927 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002928 goto out_free_skb;
2929 err = copy_to_user_policy_type(c->data.type, skb);
2930 if (err)
2931 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002932
Thomas Graf98250692007-08-22 12:47:26 -07002933 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002934
Michal Kubecek21ee5432014-06-03 10:26:06 +02002935 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002936
David S. Miller1d1e34d2012-06-27 21:57:03 -07002937out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002938 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002939 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002940}
2941
David S. Miller214e0052011-02-24 00:02:38 -05002942static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002943{
2944
2945 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002946 case XFRM_MSG_NEWPOLICY:
2947 case XFRM_MSG_UPDPOLICY:
2948 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002949 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002950 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002951 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002952 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002953 return xfrm_exp_policy_notify(xp, dir, c);
2954 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002955 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2956 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002957 }
2958
2959 return 0;
2960
2961}
2962
Thomas Graf7deb2262007-08-22 13:57:39 -07002963static inline size_t xfrm_report_msgsize(void)
2964{
2965 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2966}
2967
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002968static int build_report(struct sk_buff *skb, u8 proto,
2969 struct xfrm_selector *sel, xfrm_address_t *addr)
2970{
2971 struct xfrm_user_report *ur;
2972 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002973
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002974 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2975 if (nlh == NULL)
2976 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002977
Thomas Graf7b67c852007-08-22 13:53:52 -07002978 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002979 ur->proto = proto;
2980 memcpy(&ur->sel, sel, sizeof(ur->sel));
2981
David S. Miller1d1e34d2012-06-27 21:57:03 -07002982 if (addr) {
2983 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2984 if (err) {
2985 nlmsg_cancel(skb, nlh);
2986 return err;
2987 }
2988 }
Thomas Graf98250692007-08-22 12:47:26 -07002989 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002990}
2991
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002992static int xfrm_send_report(struct net *net, u8 proto,
2993 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002994{
2995 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002996
Thomas Graf7deb2262007-08-22 13:57:39 -07002997 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002998 if (skb == NULL)
2999 return -ENOMEM;
3000
3001 if (build_report(skb, proto, sel, addr) < 0)
3002 BUG();
3003
Michal Kubecek21ee5432014-06-03 10:26:06 +02003004 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003005}
3006
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003007static inline size_t xfrm_mapping_msgsize(void)
3008{
3009 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3010}
3011
3012static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3013 xfrm_address_t *new_saddr, __be16 new_sport)
3014{
3015 struct xfrm_user_mapping *um;
3016 struct nlmsghdr *nlh;
3017
3018 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3019 if (nlh == NULL)
3020 return -EMSGSIZE;
3021
3022 um = nlmsg_data(nlh);
3023
3024 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3025 um->id.spi = x->id.spi;
3026 um->id.family = x->props.family;
3027 um->id.proto = x->id.proto;
3028 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3029 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3030 um->new_sport = new_sport;
3031 um->old_sport = x->encap->encap_sport;
3032 um->reqid = x->props.reqid;
3033
3034 return nlmsg_end(skb, nlh);
3035}
3036
3037static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3038 __be16 sport)
3039{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003040 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003041 struct sk_buff *skb;
3042
3043 if (x->id.proto != IPPROTO_ESP)
3044 return -EINVAL;
3045
3046 if (!x->encap)
3047 return -EINVAL;
3048
3049 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3050 if (skb == NULL)
3051 return -ENOMEM;
3052
3053 if (build_mapping(skb, x, ipaddr, sport) < 0)
3054 BUG();
3055
Michal Kubecek21ee5432014-06-03 10:26:06 +02003056 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003057}
3058
Horia Geanta0f245582014-02-12 16:20:06 +02003059static bool xfrm_is_alive(const struct km_event *c)
3060{
3061 return (bool)xfrm_acquire_is_on(c->net);
3062}
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064static struct xfrm_mgr netlink_mgr = {
3065 .id = "netlink",
3066 .notify = xfrm_send_state_notify,
3067 .acquire = xfrm_send_acquire,
3068 .compile_policy = xfrm_compile_policy,
3069 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003070 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003071 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003072 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003073 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074};
3075
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003076static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
Patrick McHardybe336902006-03-20 22:40:54 -08003078 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003079 struct netlink_kernel_cfg cfg = {
3080 .groups = XFRMNLGRP_MAX,
3081 .input = xfrm_netlink_rcv,
3082 };
Patrick McHardybe336902006-03-20 22:40:54 -08003083
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003084 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003085 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003087 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003088 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return 0;
3090}
3091
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003092static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003093{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003094 struct net *net;
3095 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003096 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003097 synchronize_net();
3098 list_for_each_entry(net, net_exit_list, exit_list)
3099 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003100}
3101
3102static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003103 .init = xfrm_user_net_init,
3104 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003105};
3106
3107static int __init xfrm_user_init(void)
3108{
3109 int rv;
3110
3111 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3112
3113 rv = register_pernet_subsys(&xfrm_user_net_ops);
3114 if (rv < 0)
3115 return rv;
3116 rv = xfrm_register_km(&netlink_mgr);
3117 if (rv < 0)
3118 unregister_pernet_subsys(&xfrm_user_net_ops);
3119 return rv;
3120}
3121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122static void __exit xfrm_user_exit(void)
3123{
3124 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003125 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
3128module_init(xfrm_user_init);
3129module_exit(xfrm_user_exit);
3130MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003131MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003132